Skip to content

Commit

Permalink
Feat: opentelemetry and loki 설정 for auth and reservation server
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwupark committed Dec 15, 2024
1 parent 450ce4f commit 64e8a95
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 58 deletions.
18 changes: 10 additions & 8 deletions MEME-AUTH/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies {
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

runtimeOnly 'com.mysql:mysql-connector-j'

// Spring Security
Expand All @@ -26,17 +25,20 @@ dependencies {

// Embedded Redis
implementation 'it.ozimov:embedded-redis:0.7.2'
runtimeOnly 'com.h2database:h2' // h2 추가

// Spring Boot Actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'

// Logstash에서 처리할 수 있는 JSON 형식으로 로그 메시지를 인코딩
implementation 'net.logstash.logback:logstash-logback-encoder:6.2'
// json 형태로 로그 출력
implementation "ch.qos.logback.contrib:logback-json-classic:0.1.5"
implementation "ch.qos.logback.contrib:logback-jackson:0.1.5"
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'

// OTEL Log Exporter whit LOGBACK appender
def OTEL_LOGBACK_VERSION = "2.10.0-alpha"
implementation "io.opentelemetry.instrumentation:opentelemetry-logback-appender-1.0:$OTEL_LOGBACK_VERSION"
implementation "io.opentelemetry.instrumentation:opentelemetry-logback-mdc-1.0:$OTEL_LOGBACK_VERSION"
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.micrometer:micrometer-registry-prometheus'

runtimeOnly 'com.h2database:h2' // h2 추가
implementation 'io.opentelemetry:opentelemetry-api:1.45.0'
}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.meme.auth.config;

import io.prometheus.client.exemplars.tracer.otel_agent.OpenTelemetryAgentSpanContextSupplier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PrometheusOtelConfiguration {
@Bean
public OpenTelemetryAgentSpanContextSupplier openTelemetryAgentSpanContextSupplier() {
return new OpenTelemetryAgentSpanContextSupplier();
}
}
25 changes: 19 additions & 6 deletions MEME-AUTH/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,26 @@ springdoc:
server:
port: 8081

logging:
level:
org:
springframework: DEBUG

management:
endpoints:
web:
exposure:
include: prometheus
include: prometheus
metrics:
tags:
application: service # add tag to each prometheus metric
distribution:
percentiles-histogram:
http:
server:
requests: 'true'
minimum-expected-value: # Publish fewer histogram buckets by clamping the range of expected values.
http.server.requests: 5ms
maximum-expected-value:
http.server.requests: 1000ms

logging:
pattern:
level: "trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %p"
level:
org.meme.service: DEBUG
21 changes: 21 additions & 0 deletions MEME-AUTH/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSX</timestampFormat>
<timestampFormatTimezoneId>Etc/UTC</timestampFormatTimezoneId>
<appendLineSeparator>true</appendLineSeparator>
</layout>
</encoder>
</appender>
<appender name="OpenTelemetry" class="io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender"/>
<appender name="OpenTelemetryConsole" class="io.opentelemetry.instrumentation.logback.mdc.v1_0.OpenTelemetryAppender">
<appender-ref ref="Console"/>
</appender>

<root level="info">
<appender-ref ref="OpenTelemetry"/>
<appender-ref ref="OpenTelemetryConsole"/>
</root>
</configuration>
21 changes: 14 additions & 7 deletions MEME-RESERVATION/build.gradle
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
dependencies {
implementation(project(":MEME-DOMAIN"))
implementation 'org.springframework.boot:spring-boot-starter-web'

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

runtimeOnly 'com.mysql:mysql-connector-j'

// Mockito
testImplementation 'org.mockito:mockito-core:5.8.0'
runtimeOnly 'com.h2database:h2' // h2 추가

// Logstash에서 처리할 수 있는 JSON 형식으로 로그 메시지를 인코딩
implementation 'net.logstash.logback:logstash-logback-encoder:6.2'

implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.micrometer:micrometer-registry-prometheus'
// Reddison
implementation 'org.redisson:redisson-spring-boot-starter:3.18.0'

// Swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'

// json 형태로 로그 출력
implementation "ch.qos.logback.contrib:logback-json-classic:0.1.5"
implementation "ch.qos.logback.contrib:logback-jackson:0.1.5"
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'

// OTEL Log Exporter whit LOGBACK appender
def OTEL_LOGBACK_VERSION = "2.10.0-alpha"
implementation "io.opentelemetry.instrumentation:opentelemetry-logback-appender-1.0:$OTEL_LOGBACK_VERSION"
implementation "io.opentelemetry.instrumentation:opentelemetry-logback-mdc-1.0:$OTEL_LOGBACK_VERSION"
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.micrometer:micrometer-registry-prometheus'
implementation 'io.opentelemetry:opentelemetry-api:1.45.0'
}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.meme.reservation.config;

import io.prometheus.client.exemplars.tracer.otel_agent.OpenTelemetryAgentSpanContextSupplier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PrometheusOtelConfiguration {
@Bean
public OpenTelemetryAgentSpanContextSupplier openTelemetryAgentSpanContextSupplier() {
return new OpenTelemetryAgentSpanContextSupplier();
}
}
20 changes: 19 additions & 1 deletion MEME-RESERVATION/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,22 @@ management:
endpoints:
web:
exposure:
include: prometheus
include: prometheus
metrics:
tags:
application: service # add tag to each prometheus metric
distribution:
percentiles-histogram:
http:
server:
requests: 'true'
minimum-expected-value: # Publish fewer histogram buckets by clamping the range of expected values.
http.server.requests: 5ms
maximum-expected-value:
http.server.requests: 1000ms

logging:
pattern:
level: "trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %p"
level:
org.meme.service: DEBUG
49 changes: 13 additions & 36 deletions MEME-RESERVATION/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -1,44 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- <appender> KafkaAppender 클래스를 사용하여 Kafka에 로그 메시지를 전송 -->
<appender name="LOG-KAFKA" class="com.github.danielwegener.logback.kafka.KafkaAppender">
<!-- <encoder> 로그 메시지를 Kafka가 처리할 수 있는 형식으로 변환 -->
<encoder class="com.github.danielwegener.logback.kafka.encoding.LayoutKafkaMessageEncoder">
<!-- <layout> 로그 메시지 출력 패턴 지정 -->
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date - %-5p %t %-25logger{5} %F:%L %m%n</pattern>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSX</timestampFormat>
<timestampFormatTimezoneId>Etc/UTC</timestampFormatTimezoneId>
<appendLineSeparator>true</appendLineSeparator>
</layout>
</encoder>
<!-- <topic> Kafka에 전송될 토픽의 이름을 지정 -->
<topic>meme-reservation-log</topic>
<!-- <keyingStrategy> 키 생성 전략을 Round Robin으로 설정 (모든 파티션에 균등하게 분산되도록 메시지들이 순환적으로 파티션에 할당) -->
<keyingStrategy class="com.github.danielwegener.logback.kafka.keying.RoundRobinKeyingStrategy"/>
<!-- <deliveryStrategy> 비동기적 방식으로 로그 메시지 전송 -->
<deliveryStrategy class="com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy"/>
<!-- <producerConfig> Kafka producer의 설정값 (순서대로, 재전송 횟수, 전송 서버 주소, 압축 타입, 데이터 전송 최대 대기 시간 -->
<producerConfig>retries=1</producerConfig>
<producerConfig>bootstrap.servers=localhost:9092</producerConfig>
<producerConfig>compression.type=snappy</producerConfig>
<producerConfig>max.block.ms=1000</producerConfig>
</appender>

<!-- <appender> 콘솔에 로그를 출력하기 위한 appender -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date - %-5p %t %-25logger{5} %F:%L %m%n</pattern>
</encoder>
<appender name="OpenTelemetry" class="io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender"/>
<appender name="OpenTelemetryConsole" class="io.opentelemetry.instrumentation.logback.mdc.v1_0.OpenTelemetryAppender">
<appender-ref ref="Console"/>
</appender>

<!--
<logger> elk 이름의 logger를 정의하여, INFO 레벨 메시지만 처리
<appender-ref> elk 이름의 logger가 사용할 appender 지정
-->
<logger name="MEME-RESERVATION" level="DEBUG" additivity="false">
<appender-ref ref="LOG-KAFKA"/>
<appender-ref ref="STDOUT"/>
</logger>

<!-- <root> 모든 logger에 대한 기본 설정을 정의 (INFO 레벨 이상의 메시지만 처리) -->
<root level="INFO">
<appender-ref ref="STDOUT"/>
<root level="info">
<appender-ref ref="OpenTelemetry"/>
<appender-ref ref="OpenTelemetryConsole"/>
</root>
</configuration>
44 changes: 44 additions & 0 deletions MEME-RESERVATION/src/main/resources/logback_kafka.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!--<configuration>-->
<!-- &lt;!&ndash; <appender> KafkaAppender 클래스를 사용하여 Kafka에 로그 메시지를 전송 &ndash;&gt;-->
<!-- <appender name="LOG-KAFKA" class="com.github.danielwegener.logback.kafka.KafkaAppender">-->
<!-- &lt;!&ndash; <encoder> 로그 메시지를 Kafka가 처리할 수 있는 형식으로 변환 &ndash;&gt;-->
<!-- <encoder class="com.github.danielwegener.logback.kafka.encoding.LayoutKafkaMessageEncoder">-->
<!-- &lt;!&ndash; <layout> 로그 메시지 출력 패턴 지정 &ndash;&gt;-->
<!-- <layout class="ch.qos.logback.classic.PatternLayout">-->
<!-- <pattern>%date - %-5p %t %-25logger{5} %F:%L %m%n</pattern>-->
<!-- </layout>-->
<!-- </encoder>-->
<!-- &lt;!&ndash; <topic> Kafka에 전송될 토픽의 이름을 지정 &ndash;&gt;-->
<!-- <topic>meme-reservation-log</topic>-->
<!-- &lt;!&ndash; <keyingStrategy> 키 생성 전략을 Round Robin으로 설정 (모든 파티션에 균등하게 분산되도록 메시지들이 순환적으로 파티션에 할당) &ndash;&gt;-->
<!-- <keyingStrategy class="com.github.danielwegener.logback.kafka.keying.RoundRobinKeyingStrategy"/>-->
<!-- &lt;!&ndash; <deliveryStrategy> 비동기적 방식으로 로그 메시지 전송 &ndash;&gt;-->
<!-- <deliveryStrategy class="com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy"/>-->
<!-- &lt;!&ndash; <producerConfig> Kafka producer의 설정값 (순서대로, 재전송 횟수, 전송 서버 주소, 압축 타입, 데이터 전송 최대 대기 시간 &ndash;&gt;-->
<!-- <producerConfig>retries=1</producerConfig>-->
<!-- <producerConfig>bootstrap.servers=localhost:9092</producerConfig>-->
<!-- <producerConfig>compression.type=snappy</producerConfig>-->
<!-- <producerConfig>max.block.ms=1000</producerConfig>-->
<!-- </appender>-->

<!-- &lt;!&ndash; <appender> 콘솔에 로그를 출력하기 위한 appender &ndash;&gt;-->
<!-- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">-->
<!-- <encoder>-->
<!-- <pattern>%date - %-5p %t %-25logger{5} %F:%L %m%n</pattern>-->
<!-- </encoder>-->
<!-- </appender>-->

<!-- &lt;!&ndash;-->
<!-- <logger> elk 이름의 logger를 정의하여, INFO 레벨 메시지만 처리-->
<!-- <appender-ref> elk 이름의 logger가 사용할 appender 지정-->
<!-- &ndash;&gt;-->
<!-- <logger name="MEME-RESERVATION" level="DEBUG" additivity="false">-->
<!-- <appender-ref ref="LOG-KAFKA"/>-->
<!-- <appender-ref ref="STDOUT"/>-->
<!-- </logger>-->

<!-- &lt;!&ndash; <root> 모든 logger에 대한 기본 설정을 정의 (INFO 레벨 이상의 메시지만 처리) &ndash;&gt;-->
<!-- <root level="INFO">-->
<!-- <appender-ref ref="STDOUT"/>-->
<!-- </root>-->
<!--</configuration>-->

0 comments on commit 64e8a95

Please sign in to comment.