ํฐ์คํ ๋ฆฌ ๋ทฐ
Spring Cloud Sleuth [2] - ๋ถ์ฐ ์์คํ ๊ณผ ๋์ ๊ณผ์
kkoon9 2022. 4. 6. 18:10๐ป
‘๋ถ์ฐ ์์คํ ์ ๋ฆฌํ์คํธ ์ถ์ ์ ์ํ Spring cloud Sleuth’, 'spring docs'๋ฅผ ๊ณต๋ถํ๋ฉด์ ์ ๋ฆฌํ ํฌ์คํ ์ ๋๋ค.
Spring Cloud Sleuth
Spring Cloud Sleuth automatically instruments all your Spring applications, so you should not have to do anything to activate it. The instrumentation is added by using a variety of technologies according to the stack that is available. For example, for a s
docs.spring.io
Microservices์์ Request ์ถ์ ์์์ ๋ฌธ์ ์
A ์๋ฒ์ B ์๋ฒ๊ฐ ์๋ค๊ณ ๊ฐ์ ํด๋ณด์.
- A ์๋ฒ์ 2๊ฐ์ ์์ฒญ์ด ๋ค์ด์์ B์๋ฒ์ ์์๋๋ก ์์ฒญ์ ๋ณด๋ธ๋ค๊ณ ํ๋๋ผ๋ B ์๋ฒ์์๋ ์์์ ์๊ด์์ด ์ฒ๋ฆฌํ ์ ์๋ค.
- A์ B ์ฌ์ด์ ์๋ณํ ์ ์๋ ๊ฐ์ด ์๋ค๊ณ ํ๋ค๋ฉด ์ฐ๊ด๊ด๊ณ๋ ํ์
ํ๊ธฐ ํ๋ค์ด์ง๋ค.
- ์์ ๋ณ์๋ฅผ ๋ฐ์ด๋ฃ์ด์ ํด๊ฒฐํ ์๋ ์๋ค. ⇒ ์ด๊ฒ์ ๋ํ ๋ฌธ์ ์ ์ ์ค์ต ํฌ์คํ ์์ ๋ค๋ฃฌ๋ค.
- A์์ ํธ์ถํ ์๊ฐ๊ณผ B์์์ ์๋ฌ ์๊ฐ์ด ๋ง์ ์๊ฐ์ด ์ฐจ์ด๋ ์ ์๋ค.
- ์ด๋ ๊ฒ ๋๋ฉด ํ ๋์ ์ฐ๊ด๊ด๊ณ๋ฅผ ํ์ ํ๊ธฐ ํ๋ค์ด์ง๋ค.
- ๋ง์ดํฌ๋ก์๋น์ค์ ํน์ง ์ค ํ๋์ธ ํด๋ฆฌ๊ธ๋์ ์ ์งํ๊ธฐ ์ํด์ ์ค๊ฐ์ Redis, Kafka, RabbitMQ์ ๊ฐ์ ๋ฉ์์ง ํ๋ฅผ ๊ฒฝ์ ๊ฐ ํ์ํ ์ ์๋ค.
๐ค
์ด๋ฌํ ๋ฌธ์ ์ ์ Spring Cloud์์ ์ ๊ณตํ๋ Sleuth๋ก ํด๊ฒฐํ ์ ์๋ค!
๋ถ์ฐ ์์คํ ์์ ์์ฒญ์ ์ถ์ ํ๋ ๋ฐฉ๋ฒ์ ‘Distributed tracing’ ๋ผ๊ณ ํ๋๋ฐ, ํ ๋ฒ ์์๋ณด์.
Distributed tracing(๋ถ์ฐ ํธ๋ ์ด์ค)
opentracing ํํ์ด์ง์ ์๊ฐ๋์ด ์๋ Distributed tracing์ ๋ํ ๊ธ์ ๋ฒ์ญํ๋ค.
What is Distributed Tracing?
Distributed tracing, also called distributed request tracing, is a method used to profile and monitor applications, especially those built using a microservices architecture. Distributed tracing helps pinpoint where failures occur and what causes poor perf
opentracing.io
Distributed tracing์ ์ ์
Distributed Request Tracing์ด๋ผ๊ณ ๋ ํ๋ฉฐ, ์ ํ๋ฆฌ์ผ์ด์ , ํนํ ๋ง์ดํฌ๋ก ์๋น์ค ์ํคํ ์ฒ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌ์ถ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ํ๋กํ์ผ๋ง ๋ฐ ๋ชจ๋ํฐ๋ง์ ์ฌ์ฉ๋๋ ๋ฐฉ๋ฒ์ด๋ค.
๋ถ์ฐ ํธ๋ ์ด์ค๋ฅผ ์ฌ์ฉํ๋ฉด ์ฅ์ ๊ฐ ๋ฐ์ํ๋ ์ฅ์์ ์ฑ๋ฅ ์ ํ๋ฅผ ์ผ์ผํค๋ ์์ธ์ ์ ํํ๊ฒ ํ์ ํ ์ ์๋ค.
๐ค
Distributed tracing๋ฅผ ๋๊ฐ ์ฌ์ฉํด์ผ ํ ๊น?
IT ํ๊ณผ DevOps ํ์ ๋ถ์ฐ ํธ๋ ์ด์ค๋ฅผ ์ฌ์ฉํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ชจ๋ํฐ๋งํ ์ ์๋ค.
๋ถ์ฐ ํธ๋ ์ด์ค๋ ๋ง์ดํฌ๋ก ์๋น์ค ๋ฑ์ ์ต์ ๋ถ์ฐ ์ํํธ์จ์ด ์ํคํ ์ฒ๋ฅผ ๋๋ฒ๊น ๋ฐ ๋ชจ๋ํฐ๋งํ๋ ๋ฐ ์ ํฉํ๋ค.
๊ฐ๋ฐ์๋ ๋ถ์ฐ ์ถ์ ์ ์ฌ์ฉํ์ฌ ์ฝ๋๋ฅผ ๋๋ฒ๊น ํ๊ณ ์ต์ ํํ ์ ์๋ค.
์ฆ, ํจ์จ์ ์ธ ์ฝ๋์ ์ ์ง๋ณด์๋ฅผ ์ํด ์ ๋ฐ์ ์ธ ๊ฐ๋ฐ์ ๋ชจ๋๊ฐ ์ฌ์ฉํด์ผ ํ๋ค.
๋ค์์ sleuth์ ๋์ ๊ณผ์ ์ ์์๋ณด์.
๋ค์ ํญ์์๋ ์ ๋ฒ ํฌ์คํ ๋ ์ดํด๋ณธ ์๋์ ์ด๋ฏธ์ง์ ๋ํ๋ ์์ ๋ํด ์ค๋ช ํ๋ค.
Distributed Tracing with Zipkin
์ ์ด๋ฏธ์ง์์๋ 7๊ฐ์ span์ด ์กด์ฌํ๋ค.
Zipkin ๋ด ํธ๋ ์ด์ค์ ์ ๊ทผํ๋ฉด ๋ค์ ์ด๋ฏธ์ง์ ๊ฐ์ด ๋ ๋ฒ์งธ ํธ๋ ์ด์ค์์ 7๊ฐ์์ ํ์ธํ ์ ์๋ค.
๋จ, ํน์ ํธ๋ ์ด์ค๋ฅผ ์ ํํ๋ฉด ๋ค์ ์ด๋ฏธ์ง์ ๊ฐ์ด 4๊ฐ์ span์ด ํ์๋๋ค.
ํน์ trace๋ฅผ ์ ํํ๋ฉด Marge๋ span์ด ํ์๋๋ค.
์ฆ, Zipkin์ sr, ss ์ด๋ ธํ ์ด์ ๋๋ cr, cs ์ด๋ ธํ ์ด์ ์ด ์๋ 2๊ฐ์ span์ด ๋ณด๋ด๋ ๊ฒฝ์ฐ 1๊ฐ์ ์คํฌ์ผ๋ก ํ์๋๋ค.
๐ค
์ฒซ ๋ฒ์งธ ์์์๋ span์ด 7๊ฐ, ๋ ๋ฒ์งธ ์์์๋ 4๊ฐ๊ฐ ๋์ค๋ ์ด์ ๊ฐ ๋ญ๊น?
- ์ ์ฌ์ง์ ๋ณด๋ฉด Service1์์๋ 1๊ฐ์ span์ http://start์์ ๊ฐ์ ธ์จ๋ค.
- sr๊ณผ ss ์ด๋ ธํ ์ด์ ์ ๊ฐ์ง๊ณ ์๋ค.
- Service1์์ Service2๋ฅผ RPC ํธ์ถ์ ํตํด http://foo๋ก 2๊ฐ์ span์ด ์ ์ก๋๋ค.
- Service1์์ cs์ cr ์ด๋ ธํ ์ด์ ์ด ๋ฐ์ํ๋ค.
- Service2์์ sr๊ณผ ss ์ด๋ ธํ ์ด์ ์ด ๋ฐ์ํ๋ค.
- Service2์์ Service3๋ฅผ RPC ํธ์ถ์ ํตํด http://bar๋ก 2๊ฐ์ span์ด ์ ์ก๋๋ค.
- Service2์์ cs์ cr ์ด๋ ธํ ์ด์ ์ด ๋ฐ์ํ๋ค.
- Service3์์ sr๊ณผ ss ์ด๋ ธํ ์ด์ ์ด ๋ฐ์ํ๋ค.
- Service2์์ Service4๋ฅผ RPC ํธ์ถ์ ํตํด http://baz๋ก 2๊ฐ์ span์ด ์ ์ก๋๋ค.
- Service2์์ cs์ cr ์ด๋ ธํ ์ด์ ์ด ๋ฐ์ํ๋ค.
- Service4์์ sr๊ณผ ss ์ด๋ ธํ ์ด์ ์ด ๋ฐ์ํ๋ค.
๋ฐ๋ผ์ ๋ฌผ๋ฆฌ์ (Physical) span๋ฅผ ๊ณ์ฐํ๋ฉด
- http:/start์์ 1๊ฐ
- service1์์ ํธ์ถํ service2์์ 2๊ฐ,
- service2 ํธ์ถ ์๋น์ค3์์ 2๊ฐ,
- service2 ํธ์ถ ์๋น์ค4์์ 2๊ฐ
์ด 7๊ฐ์ span์ด ์๋ค.
๋ ผ๋ฆฌ์ (Logical)์ผ๋ก๋ 4๊ฐ์ span์ ๋ณผ ์ ์๋ค.
๊ทธ ์ด์ ๋ ํ๋์ span๊ณผ RPC ํธ์ถ๊ณผ ๊ด๋ จ๋ ์ธ ๊ฐ์ span์ด ์๊ธฐ ๋๋ฌธ์ด๋ค.
Visualizing errors
Zipkin์ ์ฌ์ฉํ๋ฉด ์ถ์ ์์ ์ค๋ฅ๋ฅผ ์๊ฐํํ ์ ์๋ค.
์์ธ๊ฐ ๋์ ธ์ง๊ณ ์กํ์ง ์์์ ๋, ์ฐ๋ฆฌ๋ ์ ์ ํ ํ๊ทธ๋ฅผ span์ ์ค์ ํ๋ค๋ฉด Zipkin์ด ์ ์ ํ๊ฒ ๋ณด์ ํ ์ ์๋ค.
Tracing ๋ชฉ๋ก์์ ์์ธ๊ฐ ๋ฐ์ํ๋ฉด ๋นจ๊ฐ์์ผ๋ก ๋ํ๋๋ค.
์ด ์ถ์ ์ ํด๋ฆญํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ ์ฌํ ๊ทธ๋ฆผ์ด ํ์๋๋ค.
๊ทธ๋ฐ ๋ค์ span ์ค ํ๋๋ฅผ ํด๋ฆญํ๋ฉด ๋ค์์ด ํ์๋๋ค.
span์๋ error์ ์ด์ ๋ฐ error์ ๊ด๋ จ๋ ์ ์ฒด ์คํ trace๊ฐ ํ์๋๋ค.
Distributed Tracing with Brave
2.0.0 ๋ฒ์ ๋ถํฐ spring cloud sleuth๋ brave๋ฅผ trace ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์ฌ์ฉํ๋ค.
๋ฐ๋ผ์ sleuth๋ ๋ ์ด์ ์ฝํ ์คํธ๋ฅผ ์ ์ฅํ์ง ์๊ณ brave์๊ฒ ์์ ์ ์์ํ๋ค.
slueth๊ฐ brave์ ๋ค๋ฅธ ๋ช ๋ช ๋ฐ ํ๊ทธ ๊ท์น์ ๊ฐ์ง๊ณ ์๊ธฐ ๋๋ฌธ์, ์ด์ ๋ถํฐ ๋ธ๋ ์ด๋ธ์ ๊ท์น์ ๋ฐ๋ฅธ๋ค.
ํ์ง๋ง ๊ธฐ์กด Sleuth ์ ๊ทผ ๋ฐฉ์์ ์ฌ์ฉํ๊ณ ์ถ๋ค๋ฉด spring.sleuth.http.legacy.enable ํ๋กํผํฐ๋ฅผ true๋ก ์ค์ ํ๋ค.
JSON Logback with Logstash
๋ก๊ทธ๋ ํ ์คํธ ํ์ผ์ด ์๋๋ผ Logstash๊ฐ ์ฆ์ ์ ํํ ์ ์๋ JSON ํ์ผ์ ์ ์ฅํ๋ ค๊ณ ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
์ด๋ ๊ฒ ํ๋ ค๋ฉด ๋ค์์ ์ํํด์ผ ํ๋ค.
(๊ฐ๋ ์ฑ์ ์ํด groupId:artifactId:version ํ๊ธฐ๋ฒ์์ ์ข ์์ฑ์ ์ ๋ฌํ๋ค).
Dependencies Setup
- Logback์ด classpath์ ์๋์ง ํ์ธํด์ผ ํ๋ค. (ch.qos.logback:logback-core)
- Logstash Logback ์ธ์ฝ๋ฉ์ ์ถ๊ฐํ๋ค.
- ๋ง์ฝ 4.6์ ์ฌ์ฉํ๋ ค๋ฉด net.logstash.logback:logstash-logback-encoder:4.6 ์ถ๊ฐํ๋ค.
logback-spring.xml์ ๋ค์๊ณผ ๊ฐ๋ค.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<springProperty scope="context" name="springAppName" source="spring.application.name"/>
<!-- Example for logging into the build folder of your project -->
<property name="LOG_FILE" value="${BUILD_FOLDER:-build}/${springAppName}"/>
<!-- You can override this to have a custom pattern -->
<property name="CONSOLE_LOG_PATTERN"
value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
<!-- Appender to log to console -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<!-- Minimum logging level to be presented in the console logs-->
<level>DEBUG</level>
</filter>
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
<!-- Appender to log to file -->
<appender name="flatfile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
<!-- Appender to log to file in a JSON format -->
<appender name="logstash" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}.json</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}.json.%d{yyyy-MM-dd}.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<timeZone>UTC</timeZone>
</timestamp>
<pattern>
<pattern>
{
"severity": "%level",
"service": "${springAppName:-}",
"trace": "%X{traceId:-}",
"span": "%X{spanId:-}",
"baggage": "%X{key:-}",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger{40}",
"rest": "%message"
}
</pattern>
</pattern>
</providers>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="console"/>
<!-- uncomment this to have also JSON logs -->
<!--<appender-ref ref="logstash"/>-->
<!--<appender-ref ref="flatfile"/>-->
</root>
</configuration>
์ ํ๋ฆฌ์ผ์ด์ ์ ์ ๋ณด๋ฅผ JSON ํ์์ผ๋ก build/${spring.application.name}.json ํ์ผ์ ๊ธฐ๋กํ๋ค.
Propagating(์ ํ) Span Context
span context๋ ํ๋ก์ธ์ค ๊ฒฝ๊ณ๋ฅผ ๋์ด ํ์ span์ผ๋ก ์ ํ๋์ด์ผ ํ๋(Propagated) ์ํ๋ค.
span context์ ์ผ๋ถ๋ Baggage๋ผ๊ณ ํ๋ค.
trace ๋ฐ sapn ID๋ span context์ ํ์ ๋ถ๋ถ์ด๋ค.
Baggage์ ์ ํ ์ฌํญ์ด๋ค.
๐ป
ํ์ฌ Baggage ํ๋ชฉ์ ๊ฐ์๋ ํฌ๊ธฐ์๋ ์ ํ์ด ์๋ค.
๊ทธ๋ฌ๋ ๋๋ฌด ๋ง์ผ๋ฉด ์์คํ ์ฒ๋ฆฌ๋์ด ๊ฐ์ํ๊ฑฐ๋ RPC ์ง์ฐ ์๊ฐ์ด ์ฆ๊ฐํ ์ ์๋ค.
๊ทน๋จ์ ์ธ ๊ฒฝ์ฐ, ๋๋ฌด ๋ง์ Baggage์ด ์ ์ก ์์ค ๋ฉ์์ง ๋๋ ํค๋ ์ฉ๋์ ์ด๊ณผํ์ฌ
์์ฉ ํ๋ก๊ทธ๋จ์ ์์์ํฌ ์ ์๋ค.
๋ค์ ์๋ Baggage์ span์ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ด๋ค.
Span initialSpan = this.tracer.nextSpan().name("span").start();
ExtraFieldPropagation.set(initialSpan.context(), "foo", "bar");
ExtraFieldPropagation.set(initialSpan.context(), "UPPER_CASE", "someValue");
Baggage vs. Span Tags
Baggage์ trace๊ณผ ํจ๊ป ์ด๋ํ๋ค.
(๋ชจ๋ child span๋ parent์ baggage๋ฅผ ํฌํจํ๋ค)
Zipkin์ baggage์ ๋ํ ์ง์์ด ์๊ณ ๊ทธ ์ ๋ณด๋ฅผ ๋ฐ์ง ๋ชปํ๋ค.
Tag๋ ํน์ span์ ๋ถ์ฐฉ๋๋ค.
๋ค์ ๋งํด์, ๊ทธ๊ฒ๋ค์ ํน์ span์๋ง ํ์๋๋ค.
๊ทธ๋ฌ๋ ๊ฒ์๋ tag ๊ฐ์ ๊ฐ๋ span์ด ์กด์ฌํ๋ค๊ณ ๊ฐ์ ํ๋ฉด tag๋ณ๋ก ๊ฒ์ํ์ฌ trace๋ฅผ ์ฐพ์ ์ ์๋ค.
Baggage๋ฅผ ๊ธฐ์ค์ผ๋ก span์ ์กฐํํ๋ ค๋ฉด root span์ ํด๋น ํญ๋ชฉ์ tag๋ก ์ถ๊ฐํด์ผ ํ๋ค.
'Sping Framework' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
XML In Java [2]. XML String to VO (0) | 2022.04.23 |
---|---|
XML In Java [1]. VO to XML String (0) | 2022.04.23 |
Spring Cloud Sleuth [1] - ์ฉ์ด (0) | 2022.04.04 |
Spring Boot์์ ์๋ฌ ์ฒ๋ฆฌํ๊ธฐ (0) | 2022.04.03 |
Spring Boot์ Sentry ๋ผ์น๊ธฐ [2] (1) | 2022.04.03 |
- Total
- Today
- Yesterday
- Kotlin
- Spring Boot
- ํด๋ฆฐ ์ํคํ ์ฒ
- kkoon9
- ํ ๋ผํผ
- BAEKJOON
- ์ดํฉํฐ๋ธ ์๋ฐ
- Algorithm
- programmers
- ์๊ณ ๋ฆฌ์ฆ
- Spring
- ํด๋ฆฐ ์ฝ๋
- AWS
- C++
- ๋ฐฑ์ค
- Effective Java
- BOJ
- JPA
- Java
- ๋์์ธํจํด
- ์ฝํ
- ์ ๊ทํํ์
- kotest
- ์ดํํฐ๋ธ ์๋ฐ
- MSA
- ํ๋ก๊ทธ๋๋จธ์ค
- node.js
- ๊ฐ์ฒด์งํฅ
- ๋์์ธ ํจํด
- Olympiad
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |