Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distributed tracing support with Micrometer and OpenTelemetry #376

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,29 @@ image:./images/interceptors_003.png[]
This started is *natively* supported by `spring-cloud-sleuth` project. +
Please continue to https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/html/integrations.html#sleuth-rpc-grpc-integration[sleuth grpc integration].

=== Distributed tracing support (OpenTelemetry)

OpenTelemetry distributed tracing can enabled by including:
[source,groovy]
----
implementation 'io.opentelemetry:opentelemetry-exporter-otlp:[version]'
implementation 'io.micrometer:micrometer-tracing-bridge-otel:[version]'
----

Enable tracing for client by adding `TracingClientInterceptor` to `ManagedChannelBuilder`:
[source,java]
----
ManagedChannelBuilder.forAddress(host, port)
...
.intercept(TracingClientInterceptor())
.build()
----

Add tracing interceptor `TracingServerInterceptor` for your `GRpcService`:
[source,java]
----
@GRpcService(interceptors = {LogInterceptor.class, TracingServerInterceptor.class })
----

=== GRPC server metrics (Micrometer.io integration)

Expand Down
2 changes: 2 additions & 0 deletions grpc-client-spring-boot-starter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ signing {

dependencies {
api "io.grpc:grpc-api:${grpcVersion}"
compileOnly 'io.opentelemetry:opentelemetry-exporter-otlp:1.29.0'
compileOnly 'io.micrometer:micrometer-tracing-bridge-otel:1.1.4'
}
compileJava.dependsOn(processResources)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.lognet.springboot.grpc.tracing;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ForwardingClientCall;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.TraceId;

public class TracingClientInterceptor implements ClientInterceptor {

Check warning on line 13 in grpc-client-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingClientInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc-client-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingClientInterceptor.java#L13

Added line #L13 was not covered by tests
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {

return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {

Check warning on line 18 in grpc-client-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingClientInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc-client-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingClientInterceptor.java#L18

Added line #L18 was not covered by tests
@Override
public void start(ClientCall.Listener<RespT> responseListener, Metadata headers) {
Span currentSpan = Span.current();
String traceId = currentSpan.getSpanContext().getTraceId();
String spanId = currentSpan.getSpanContext().getSpanId();

Check warning on line 23 in grpc-client-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingClientInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc-client-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingClientInterceptor.java#L21-L23

Added lines #L21 - L23 were not covered by tests

if (!traceId.equals(TraceId.getInvalid())) {
headers.put(Metadata.Key.of("traceId", Metadata.ASCII_STRING_MARSHALLER), traceId);
headers.put(Metadata.Key.of("spanId", Metadata.ASCII_STRING_MARSHALLER), spanId);

Check warning on line 27 in grpc-client-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingClientInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc-client-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingClientInterceptor.java#L26-L27

Added lines #L26 - L27 were not covered by tests
}
super.start(responseListener, headers);
}

Check warning on line 30 in grpc-client-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingClientInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc-client-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingClientInterceptor.java#L29-L30

Added lines #L29 - L30 were not covered by tests
};
}
}
2 changes: 2 additions & 0 deletions grpc-spring-boot-starter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ dependencies {
compileOnly "org.springframework.boot:spring-boot-starter-actuator"
compileOnly "org.springframework.boot:spring-boot-starter-validation"
compileOnly 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
compileOnly 'io.opentelemetry:opentelemetry-exporter-otlp:1.29.0'
compileOnly 'io.micrometer:micrometer-tracing-bridge-otel:1.1.4'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

io.micrometer:micrometer-tracing-bridge-otel is it in-use ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image Used in OtelConfiguration, removed from client



testImplementation "javax.annotation:javax.annotation-api:1.3.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.lognet.springboot.grpc.autoconfigure.tracing;

import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import org.lognet.springboot.grpc.tracing.TracingServerInterceptor;
import org.springframework.boot.actuate.autoconfigure.tracing.otlp.OtlpProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(OtlpProperties.class)
public class OtelConfiguration {

Check warning on line 17 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/tracing/OtelConfiguration.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/tracing/OtelConfiguration.java#L17

Added line #L17 was not covered by tests
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be conditional on OpenTelemetrySdk class

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense


@Bean
public OtlpGrpcSpanExporter otlpExporter(OtlpProperties properties) {
OtlpGrpcSpanExporterBuilder builder = OtlpGrpcSpanExporter.builder().setEndpoint(properties.getEndpoint());
return builder.build();
}

Check warning on line 23 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/tracing/OtelConfiguration.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/tracing/OtelConfiguration.java#L22-L23

Added lines #L22 - L23 were not covered by tests

@Bean
public Tracer tracer(OtlpGrpcSpanExporter otlpExporter) {
SdkTracerProvider tracerProvider = SdkTracerProvider.builder()
.addSpanProcessor(BatchSpanProcessor.builder(otlpExporter).build())
.build();

OpenTelemetrySdk openTelemetry = OpenTelemetrySdk.builder()

Check warning on line 31 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/tracing/OtelConfiguration.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/tracing/OtelConfiguration.java#L29-L31

Added lines #L29 - L31 were not covered by tests
.setTracerProvider(tracerProvider)
.buildAndRegisterGlobal();

Check warning on line 34 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/tracing/OtelConfiguration.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/tracing/OtelConfiguration.java#L34

Added line #L34 was not covered by tests
return openTelemetry.getTracerProvider().get("grpc-spring-boot-starter");
}

Check warning on line 36 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/tracing/OtelConfiguration.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/tracing/OtelConfiguration.java#L36

Added line #L36 was not covered by tests

@Bean
public TracingServerInterceptor tracingServerInterceptor(Tracer tracer) {
return new TracingServerInterceptor(tracer);
}

Check warning on line 41 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/tracing/OtelConfiguration.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/tracing/OtelConfiguration.java#L41

Added line #L41 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.lognet.springboot.grpc.log;

import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class LogInterceptor implements ServerInterceptor {

Check warning on line 12 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/log/LogInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/log/LogInterceptor.java#L12

Added line #L12 was not covered by tests
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an example but LogInterceptor was not a part of grpc-spring-boot-starter. I can add it in a separate PR if needed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this class needed ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to use it from the library. Not sure why it doesn't exist. Removed


private final Logger logger = LoggerFactory.getLogger(LogInterceptor.class);

Check warning on line 14 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/log/LogInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/log/LogInterceptor.java#L14

Added line #L14 was not covered by tests

@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
logger.info(call.getMethodDescriptor().getFullMethodName());
return next.startCall(call, headers);

Check warning on line 20 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/log/LogInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/log/LogInterceptor.java#L19-L20

Added lines #L19 - L20 were not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.lognet.springboot.grpc.tracing;

import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;

public class TracingServerInterceptor implements ServerInterceptor {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added @order(20)? I don't see you use @Order. Have you had in mind any other mechanisms?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see this, we will need to move recovery and auth down 1

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've set HIGHEST_PRECEDENCE. Do I need to touch recovery and auth here? Please, let me know if you want to change order somehow


private final Tracer tracer;

public TracingServerInterceptor(Tracer tracer) {
this.tracer = tracer;
}

Check warning on line 22 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingServerInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingServerInterceptor.java#L20-L22

Added lines #L20 - L22 were not covered by tests

@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {

String traceId = headers.get(Metadata.Key.of("traceId", Metadata.ASCII_STRING_MARSHALLER));
String spanId = headers.get(Metadata.Key.of("spanId", Metadata.ASCII_STRING_MARSHALLER));

Check warning on line 29 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingServerInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingServerInterceptor.java#L28-L29

Added lines #L28 - L29 were not covered by tests

Context spanContext = createSpanContext(traceId, spanId);
SpanBuilder spanBuilder = tracer.spanBuilder("grpc-spring-boot-starter-span").setParent(spanContext);
Span span = spanBuilder.startSpan();
Context scopedContext = Context.current().with(span);

Check warning on line 35 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingServerInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingServerInterceptor.java#L31-L35

Added lines #L31 - L35 were not covered by tests
try (Scope scope = scopedContext.makeCurrent()) {
return next.startCall(call, headers);
} finally {

Check warning on line 38 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingServerInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingServerInterceptor.java#L37-L38

Added lines #L37 - L38 were not covered by tests
span.end();
}

Check warning on line 40 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingServerInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingServerInterceptor.java#L40

Added line #L40 was not covered by tests
}

private Context createSpanContext(String traceId, String spanId) {
SpanContext spanContext = SpanContext.createFromRemoteParent(
traceId != null ? traceId : "",
spanId != null ? spanId : "",

Check warning on line 46 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingServerInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingServerInterceptor.java#L46

Added line #L46 was not covered by tests
TraceFlags.getDefault(),
TraceState.getDefault()
);
return Context.current().with(Span.wrap(spanContext));
}

Check warning on line 51 in grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingServerInterceptor.java

View check run for this annotation

Codecov / codecov/patch

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/tracing/TracingServerInterceptor.java#L49-L51

Added lines #L49 - L51 were not covered by tests
}