From ee36dcd78ce9c0b517355642af3c5e0b8b6d67d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cff-sdesai=E2=80=9D?= Date: Mon, 23 Jan 2023 17:19:55 +0530 Subject: [PATCH] Added otel-logback appender --- README.md | 15 +++++++++++- spring-cloud-sleuth-otel-slf4j/.classpath | 6 ----- spring-cloud-sleuth-otel-slf4j/README.md | 3 ++- spring-cloud-sleuth-otel-slf4j/pom.xml | 23 ++++++++++++------- .../com/uplight/service/TestServiceImpl.java | 23 +++++++++++++++++-- .../uplight/starter/ApplicationStarter.java | 7 +++++- .../src/main/resources/logback.xml | 6 +++++ 7 files changed, 64 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 5966106..912f34b 100644 --- a/README.md +++ b/README.md @@ -1 +1,14 @@ -# distributed-tracing-spring +# spring-cloud-sleuth-otel-slf4j + +##Run the Example + +cd /home/sdesai/Uplight/code/open-telemetry/spring-cloud-sleuth-otel-slf4j +mvn clean install +sudo docker build -f DockerFile -t spring-cloud-sleuth-otel-slf4j . +sudo docker compose up +Once application is up, hit http://localhost:8181/?testCount=1 from web browser to generate traces and logs + + +## view the signals +View the `trace signal` in Datadog: +[Datadog APM Traces](https://app.datadoghq.com/apm/traces?query=%40_top_level%3A1%20service%3Aspring-cloud-sleuth-otel-slf4j&cols=core_service%2Ccore_resource_name%2Clog_duration%2Clog_http.method%2Clog_http.status_code&env=none&historicalData=false&messageDisplay=inline&sort=desc&spanType=all&spanViewType=metadata&start=1669976026075&end=1669976926075&paused=false) diff --git a/spring-cloud-sleuth-otel-slf4j/.classpath b/spring-cloud-sleuth-otel-slf4j/.classpath index 002ad57..a5d9509 100644 --- a/spring-cloud-sleuth-otel-slf4j/.classpath +++ b/spring-cloud-sleuth-otel-slf4j/.classpath @@ -18,12 +18,6 @@ - - - - - - diff --git a/spring-cloud-sleuth-otel-slf4j/README.md b/spring-cloud-sleuth-otel-slf4j/README.md index ed62fd6..912f34b 100644 --- a/spring-cloud-sleuth-otel-slf4j/README.md +++ b/spring-cloud-sleuth-otel-slf4j/README.md @@ -2,10 +2,11 @@ ##Run the Example -cd distributed-tracing-spring/spring-cloud-sleuth-otel-slf4j +cd /home/sdesai/Uplight/code/open-telemetry/spring-cloud-sleuth-otel-slf4j mvn clean install sudo docker build -f DockerFile -t spring-cloud-sleuth-otel-slf4j . sudo docker compose up +Once application is up, hit http://localhost:8181/?testCount=1 from web browser to generate traces and logs ## view the signals diff --git a/spring-cloud-sleuth-otel-slf4j/pom.xml b/spring-cloud-sleuth-otel-slf4j/pom.xml index 8d6bb64..6dae970 100644 --- a/spring-cloud-sleuth-otel-slf4j/pom.xml +++ b/spring-cloud-sleuth-otel-slf4j/pom.xml @@ -22,7 +22,7 @@ org.springframework.boot spring-boot-starter-web - + org.springframework.boot spring-boot-starter-aop @@ -92,14 +92,21 @@ opentelemetry-instrumentation-annotations 1.22.1 - - + + - io.opentelemetry.instrumentation - opentelemetry-logback-appender-1.0 - 1.19.2-alpha - runtime + io.opentelemetry.instrumentation + opentelemetry-logback-appender-1.0 + 1.19.2-alpha + runtime + + + + io.opentelemetry.instrumentation + opentelemetry-logback-1.0 + 1.9.2-alpha + runtime @@ -122,4 +129,4 @@ - + \ No newline at end of file diff --git a/spring-cloud-sleuth-otel-slf4j/src/main/java/com/uplight/service/TestServiceImpl.java b/spring-cloud-sleuth-otel-slf4j/src/main/java/com/uplight/service/TestServiceImpl.java index c6b3631..3cd58a5 100644 --- a/spring-cloud-sleuth-otel-slf4j/src/main/java/com/uplight/service/TestServiceImpl.java +++ b/spring-cloud-sleuth-otel-slf4j/src/main/java/com/uplight/service/TestServiceImpl.java @@ -1,14 +1,26 @@ package com.uplight.service; +import java.util.Map; + import javax.annotation.PostConstruct; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.sleuth.annotation.NewSpan; import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; @Service public class TestServiceImpl implements TestService { + private static final Logger LOGGER = LoggerFactory.getLogger( TestServiceImpl.class ); + + @Autowired + private RestTemplate restTemplate; + @PostConstruct public void init() { System.out.println( "TestServiceImpl initalized.........................................." ); @@ -17,8 +29,15 @@ public void init() { @Override @NewSpan public void test() { - System.out.println( "In test method" ); - + Map copyOfContextMap = MDC.getCopyOfContextMap(); + // LOGGER.error( "copyOfContextMap=" + copyOfContextMap ); + // Map baggageAsMap = Baggage.current().asMap(); + // System.out.println( "baggageAsMap=" + baggageAsMap ); + // System.out.println( "calling another web application" ); + // + // + // ResponseEntity responseEntity = restTemplate.getForEntity( "http://172.22.0.1:8282/", String.class ); + // System.out.println( "Response received=" + responseEntity.getBody() ); } } diff --git a/spring-cloud-sleuth-otel-slf4j/src/main/java/com/uplight/starter/ApplicationStarter.java b/spring-cloud-sleuth-otel-slf4j/src/main/java/com/uplight/starter/ApplicationStarter.java index 2d55ed1..cd7fa32 100644 --- a/spring-cloud-sleuth-otel-slf4j/src/main/java/com/uplight/starter/ApplicationStarter.java +++ b/spring-cloud-sleuth-otel-slf4j/src/main/java/com/uplight/starter/ApplicationStarter.java @@ -3,7 +3,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; +import org.springframework.web.client.RestTemplate; @ComponentScan(basePackages = { "com.uplight.web", "com.uplight.service", "com.uplight.aspect" @@ -15,7 +17,10 @@ public static void main( String[] args ) { SpringApplication.run( ApplicationStarter.class, args ); } - + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } // @Bean // ScopeDecorator mdcScopeDecorator() { // return MDCScopeDecorator.newBuilder().clear().add( SingleCorrelationField.newBuilder( countryCodeField() ).flushOnUpdate().build() ).build(); diff --git a/spring-cloud-sleuth-otel-slf4j/src/main/resources/logback.xml b/spring-cloud-sleuth-otel-slf4j/src/main/resources/logback.xml index f7b8411..2a3ac87 100644 --- a/spring-cloud-sleuth-otel-slf4j/src/main/resources/logback.xml +++ b/spring-cloud-sleuth-otel-slf4j/src/main/resources/logback.xml @@ -1,5 +1,7 @@ + + @@ -7,6 +9,10 @@ ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} serviceName: %X{serviceName} requestId: %X{requestId} trace_id: %X{traceId} span_id: %X{spanId} - %msg%n + + + +