Skip to content

Commit

Permalink
Add and configure OpenTelemetry, propagate tracing context to Dapr cl…
Browse files Browse the repository at this point in the history
…ient
  • Loading branch information
mthmulders committed Nov 16, 2021
1 parent 98164d8 commit b62a845
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 22 deletions.
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<packaging>pom</packaging>

<modules>
<module>shared</module>
<module>service-a</module>
<module>service-b</module>
<module>service-c</module>
Expand All @@ -21,6 +22,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>

<opentelemetry.version>1.9.0</opentelemetry.version>
<spring-boot.version>2.5.2</spring-boot.version>
<dapr-sdk.version>1.3.0</dapr-sdk.version>
</properties>
Expand Down Expand Up @@ -63,6 +65,28 @@
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-bom</artifactId>
<version>${opentelemetry.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId>
<version>1.9.0-alpha</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-extension-autoconfigure-spi</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-spring-boot-autoconfigure</artifactId>
<version>1.8.0-alpha</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
2 changes: 2 additions & 0 deletions run-service-a.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
set -euox pipefail

pushd service-a
export OTEL_SERVICE_NAME=service-a
export OTEL_TRACES_EXPORTER=zipkin
dapr run \
--app-id service-a \
--app-port 10001 \
Expand Down
2 changes: 2 additions & 0 deletions run-service-b.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
set -euox pipefail

pushd service-b
export OTEL_SERVICE_NAME=service-b
export OTEL_TRACES_EXPORTER=zipkin
dapr run \
--app-id service-b \
--app-port 10002 \
Expand Down
2 changes: 2 additions & 0 deletions run-service-c.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
set -euox pipefail

pushd service-c
export OTEL_SERVICE_NAME=service-c
export OTEL_TRACES_EXPORTER=zipkin
dapr run \
--app-id service-c \
--app-port 10003 \
Expand Down
25 changes: 25 additions & 0 deletions service-a/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,35 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-extension-autoconfigure-spi</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>io.dapr</groupId>
<artifactId>dapr-sdk</artifactId>
</dependency>
<dependency>
<groupId>it.mulders.dapr.issues</groupId>
<artifactId>shared</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
31 changes: 26 additions & 5 deletions service-a/src/main/java/servicea/ServiceApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.HttpExtension;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.instrumentation.spring.autoconfigure.EnableOpenTelemetry;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
Expand All @@ -13,9 +18,12 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import shared.DefaultTelemetryInjector;
import shared.TelemetryInjector;

import java.time.Duration;

@EnableOpenTelemetry
@SpringBootApplication
public class ServiceApplication {
public static void main(final String... args) {
Expand All @@ -27,32 +35,45 @@ class Endpoint {
private static final Logger log = LoggerFactory.getLogger(Endpoint.class);

private final DaprClient daprClient;
private final TelemetryInjector telemetryInjector;

public Endpoint(final DaprClient daprClient) {
public Endpoint(final DaprClient daprClient, final TelemetryInjector telemetryInjector) {
this.daprClient = daprClient;
this.telemetryInjector = telemetryInjector;
}

@PostMapping("/endpoint")
ResponseEntity<Void> endpoint(@RequestBody final String data) {
log.info("Service A handling request {}", data);
log.info("[{}] Service A handling request {}", Span.current().getSpanContext().getTraceId(), data);

var result = daprClient.invokeMethod(
"service-b",
"endpoint",
"hello from service a",
HttpExtension.POST,
Void.class
);
result.block(Duration.ofMillis(250));
).contextWrite(telemetryInjector);
result.block(Duration.ofMillis(500));

return ResponseEntity.accepted().build();
}
}

@Configuration
class ServiceConfiguration {
static class ServiceConfiguration {
@Bean
public DaprClient daprClient() {
return new DaprClientBuilder().build();
}

@Bean
public OpenTelemetrySdk openTelemetrySdk() {
return AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
}

@Bean
public TelemetryInjector telemetryInjector(final OpenTelemetry openTelemetry) {
return new DefaultTelemetryInjector(openTelemetry);
}
}
}
25 changes: 25 additions & 0 deletions service-b/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,35 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-extension-autoconfigure-spi</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>io.dapr</groupId>
<artifactId>dapr-sdk</artifactId>
</dependency>
<dependency>
<groupId>it.mulders.dapr.issues</groupId>
<artifactId>shared</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
33 changes: 27 additions & 6 deletions service-b/src/main/java/serviceb/ServiceApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.HttpExtension;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.instrumentation.spring.autoconfigure.EnableOpenTelemetry;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
Expand All @@ -13,9 +18,12 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import shared.DefaultTelemetryInjector;
import shared.TelemetryInjector;

import java.time.Duration;

@EnableOpenTelemetry
@SpringBootApplication
public class ServiceApplication {
public static void main(final String... args) {
Expand All @@ -27,32 +35,45 @@ class Endpoint {
private static final Logger log = LoggerFactory.getLogger(Endpoint.class);

private final DaprClient daprClient;
private final TelemetryInjector telemetryInjector;

public Endpoint(final DaprClient daprClient) {
public Endpoint(final DaprClient daprClient, final TelemetryInjector telemetryInjector) {
this.daprClient = daprClient;
this.telemetryInjector = telemetryInjector;
}

@PostMapping("/endpoint")
ResponseEntity<Void> endpoint(@RequestBody final String data) {
log.info("Service B handling request {}", data);
log.info("[{}] Service B handling request {}", Span.current().getSpanContext().getTraceId(), data);

var result = daprClient.invokeMethod(
"service-c",
"endpoint",
"hello from service b",
HttpExtension.POST,
Void.class
);
result.block(Duration.ofMillis(250));
).contextWrite(telemetryInjector);
result.block(Duration.ofMillis(500));

return ResponseEntity.accepted().build();
}
}

@Configuration
class ServiceConfiguration {
static class ServiceConfiguration {
@Bean
public DaprClient daprClient() {
return new DaprClientBuilder().build();
}

@Bean
public OpenTelemetrySdk openTelemetrySdk() {
return AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
}

@Bean
public TelemetryInjector telemetryInjector(final OpenTelemetry openTelemetry) {
return new DefaultTelemetryInjector(openTelemetry);
}
}
}
}
25 changes: 25 additions & 0 deletions service-c/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,35 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-extension-autoconfigure-spi</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>io.dapr</groupId>
<artifactId>dapr-sdk</artifactId>
</dependency>
<dependency>
<groupId>it.mulders.dapr.issues</groupId>
<artifactId>shared</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
32 changes: 21 additions & 11 deletions service-c/src/main/java/servicec/ServiceApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.HttpExtension;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.instrumentation.spring.autoconfigure.EnableOpenTelemetry;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
Expand All @@ -13,9 +17,10 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import shared.DefaultTelemetryInjector;
import shared.TelemetryInjector;

import java.time.Duration;

@EnableOpenTelemetry
@SpringBootApplication
public class ServiceApplication {
public static void main(final String... args) {
Expand All @@ -25,25 +30,30 @@ public static void main(final String... args) {
@RestController
class Endpoint {
private static final Logger log = LoggerFactory.getLogger(Endpoint.class);

private final DaprClient daprClient;

public Endpoint(final DaprClient daprClient) {
this.daprClient = daprClient;
}

@PostMapping("/endpoint")
ResponseEntity<Void> endpoint(@RequestBody final String data) {
log.info("Service C handling request {}", data);
log.info("[{}] Service C handling request {}", Span.current().getSpanContext().getTraceId(), data);

return ResponseEntity.accepted().build();
}
}

@Configuration
class ServiceConfiguration {
static class ServiceConfiguration {
@Bean
public DaprClient daprClient() {
return new DaprClientBuilder().build();
}

@Bean
public OpenTelemetrySdk openTelemetrySdk() {
return AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
}

@Bean
public TelemetryInjector telemetryInjector(final OpenTelemetry openTelemetry) {
return new DefaultTelemetryInjector(openTelemetry);
}
}
}
Loading

0 comments on commit b62a845

Please sign in to comment.