|
1 | 1 | /*
|
2 |
| - * Copyright 2021 Google LLC |
| 2 | + * Copyright 2024 Google LLC |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
23 | 23 | import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;
|
24 | 24 | import io.grpc.Metadata;
|
25 | 25 | import io.grpc.MethodDescriptor;
|
26 |
| -import io.grpc.Status; |
27 | 26 | import io.opentelemetry.api.OpenTelemetry;
|
28 | 27 | import io.opentelemetry.context.Context;
|
29 |
| -import io.opentelemetry.context.propagation.ContextPropagators; |
| 28 | +import io.opentelemetry.context.propagation.TextMapPropagator; |
30 | 29 | import io.opentelemetry.context.propagation.TextMapSetter;
|
31 | 30 |
|
32 |
| -public class TraceContextInterceptor implements ClientInterceptor { |
33 |
| - |
34 |
| - private final ContextPropagators propagators; |
| 31 | +/** |
| 32 | + * Intercepts all gRPC calls and injects trace context related headers to propagate trace context to |
| 33 | + * Spanner. This class takes reference from OpenTelemetry's JAVA instrumentation library for gRPC. |
| 34 | + * https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/9ecf7965aa455d41ea8cc0761b6c6b6eeb106324/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingClientInterceptor.java#L27 |
| 35 | + */ |
| 36 | +class TraceContextInterceptor implements ClientInterceptor { |
35 | 37 |
|
36 |
| - TraceContextInterceptor(OpenTelemetry openTelemetry) { |
37 |
| - this.propagators = openTelemetry.getPropagators(); |
38 |
| - } |
| 38 | + private final TextMapPropagator textMapPropagator; |
39 | 39 |
|
40 |
| - enum MetadataSetter implements TextMapSetter<Metadata> { |
41 |
| - INSTANCE; |
| 40 | + TraceContextInterceptor(OpenTelemetry openTelemetry) { |
| 41 | + this.textMapPropagator = openTelemetry.getPropagators().getTextMapPropagator(); |
| 42 | + } |
42 | 43 |
|
43 |
| - @SuppressWarnings("null") |
44 |
| - @Override |
45 |
| - public void set(Metadata carrier, String key, String value) { |
46 |
| - carrier.put(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER), value); |
47 |
| - } |
48 |
| - } |
| 44 | + enum MetadataSetter implements TextMapSetter<Metadata> { |
| 45 | + INSTANCE; |
49 | 46 |
|
| 47 | + @SuppressWarnings("null") |
50 | 48 | @Override
|
51 |
| - public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, |
52 |
| - CallOptions callOptions, Channel next) { |
53 |
| - return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) { |
54 |
| - @Override |
55 |
| - public void start(Listener<RespT> responseListener, Metadata headers) { |
56 |
| - Context parentContext = Context.current(); |
57 |
| - |
58 |
| - propagators.getTextMapPropagator().inject(parentContext, headers, MetadataSetter.INSTANCE); |
59 |
| - |
60 |
| - super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) { |
61 |
| - @Override |
62 |
| - public void onHeaders(Metadata headers) { |
63 |
| - super.onHeaders(headers); |
64 |
| - } |
| 49 | + public void set(Metadata carrier, String key, String value) { |
| 50 | + carrier.put(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER), value); |
| 51 | + } |
| 52 | + } |
65 | 53 |
|
66 |
| - @Override |
67 |
| - public void onClose(Status status, Metadata trailers) { |
68 |
| - super.onClose(status, trailers); |
69 |
| - } |
70 |
| - }, headers); |
71 |
| - } |
72 |
| - }; |
| 54 | + private static final class NoopSimpleForwardingClientCallListener<RespT> |
| 55 | + extends SimpleForwardingClientCallListener<RespT> { |
| 56 | + public NoopSimpleForwardingClientCallListener(ClientCall.Listener<RespT> responseListener) { |
| 57 | + super(responseListener); |
73 | 58 | }
|
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( |
| 63 | + MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) { |
| 64 | + return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) { |
| 65 | + @Override |
| 66 | + public void start(Listener<RespT> responseListener, Metadata headers) { |
| 67 | + Context parentContext = Context.current(); |
| 68 | + textMapPropagator.inject(parentContext, headers, MetadataSetter.INSTANCE); |
| 69 | + super.start(new NoopSimpleForwardingClientCallListener<RespT>(responseListener), headers); |
| 70 | + } |
| 71 | + }; |
| 72 | + } |
74 | 73 | }
|
0 commit comments