Skip to content

Commit 377fe79

Browse files
fixes and options for gRPC instrumentation
1 parent f49bf3c commit 377fe79

File tree

5 files changed

+69
-26
lines changed

5 files changed

+69
-26
lines changed

instrumentation/grpc-1.6/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
| System property | Type | Default | Description |
44
|-------------------------------------------------------------|---------|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
5+
| `otel.instrumentation.grpc.message-events` | Boolean | `true` | Determines whether to add span event for each individual message received and sent. Set this to false in case of streaming large volumes of messages. |
56
| `otel.instrumentation.grpc.experimental-span-attributes` | Boolean | `false` | Enable the capture of experimental span attributes. |
67
| `otel.instrumentation.grpc.capture-metadata.client.request` | String | | A comma-separated list of request metadata keys. gRPC client instrumentation will capture metadata values corresponding to configured keys as span attributes. |
78
| `otel.instrumentation.grpc.capture-metadata.server.request` | String | | A comma-separated list of request metadata keys. gRPC server instrumentation will capture metadata values corresponding to configured keys as span attributes. |

instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcSingletons.java

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public final class GrpcSingletons {
2727
private static final AtomicReference<Context.Storage> STORAGE_REFERENCE = new AtomicReference<>();
2828

2929
static {
30+
boolean addMessageEvents =
31+
AgentInstrumentationConfig.get()
32+
.getBoolean("otel.instrumentation.grpc.message-events", true);
33+
3034
boolean experimentalSpanAttributes =
3135
AgentInstrumentationConfig.get()
3236
.getBoolean("otel.instrumentation.grpc.experimental-span-attributes", false);
@@ -40,6 +44,7 @@ public final class GrpcSingletons {
4044

4145
GrpcTelemetry telemetry =
4246
GrpcTelemetry.builder(GlobalOpenTelemetry.get())
47+
.setAddMessageEvents(addMessageEvents)
4348
.setCaptureExperimentalSpanAttributes(experimentalSpanAttributes)
4449
.setCapturedClientRequestMetadata(clientRequestMetadata)
4550
.setCapturedServerRequestMetadata(serverRequestMetadata)

instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetry.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,19 @@ public static GrpcTelemetryBuilder builder(OpenTelemetry openTelemetry) {
2929
private final Instrumenter<GrpcRequest, Status> clientInstrumenter;
3030
private final ContextPropagators propagators;
3131
private final boolean captureExperimentalSpanAttributes;
32+
private final boolean addMessageEvents;
3233

3334
GrpcTelemetry(
3435
Instrumenter<GrpcRequest, Status> serverInstrumenter,
3536
Instrumenter<GrpcRequest, Status> clientInstrumenter,
3637
ContextPropagators propagators,
37-
boolean captureExperimentalSpanAttributes) {
38+
boolean captureExperimentalSpanAttributes,
39+
boolean addMessageEvents) {
3840
this.serverInstrumenter = serverInstrumenter;
3941
this.clientInstrumenter = clientInstrumenter;
4042
this.propagators = propagators;
4143
this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes;
44+
this.addMessageEvents = addMessageEvents;
4245
}
4346

4447
/**
@@ -54,6 +57,7 @@ public ClientInterceptor newClientInterceptor() {
5457
* io.grpc.ServerBuilder#intercept(ServerInterceptor)}.
5558
*/
5659
public ServerInterceptor newServerInterceptor() {
57-
return new TracingServerInterceptor(serverInstrumenter, captureExperimentalSpanAttributes);
60+
return new TracingServerInterceptor(
61+
serverInstrumenter, captureExperimentalSpanAttributes, addMessageEvents);
5862
}
5963
}

instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/GrpcTelemetryBuilder.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public final class GrpcTelemetryBuilder {
4949
additionalServerExtractors = new ArrayList<>();
5050

5151
private boolean captureExperimentalSpanAttributes;
52+
private boolean addMessageEvents = true;
5253
private List<String> capturedClientRequestMetadata = Collections.emptyList();
5354
private List<String> capturedServerRequestMetadata = Collections.emptyList();
5455

@@ -130,6 +131,16 @@ public GrpcTelemetryBuilder setPeerService(String peerService) {
130131
return this;
131132
}
132133

134+
/**
135+
* Determines whether to add span event for each individual message received and sent. The default
136+
* is true. Set this to false in case of streaming large volumes of messages.
137+
*/
138+
@CanIgnoreReturnValue
139+
public GrpcTelemetryBuilder setAddMessageEvents(boolean addMessageEvents) {
140+
this.addMessageEvents = addMessageEvents;
141+
return this;
142+
}
143+
133144
/**
134145
* Sets whether experimental attributes should be set to spans. These attributes may be changed or
135146
* removed in the future, so only enable this if you know you do not require attributes filled by
@@ -211,6 +222,7 @@ public GrpcTelemetry build() {
211222
// So we go ahead and inject manually in this instrumentation.
212223
clientInstrumenterBuilder.buildInstrumenter(SpanKindExtractor.alwaysClient()),
213224
openTelemetry.getPropagators(),
214-
captureExperimentalSpanAttributes);
225+
captureExperimentalSpanAttributes,
226+
addMessageEvents);
215227
}
216228
}

instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingServerInterceptor.java

+44-23
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,27 @@ final class TracingServerInterceptor implements ServerInterceptor {
3333
private static final String RECEIVED = "RECEIVED";
3434

3535
@SuppressWarnings("rawtypes")
36-
private static final AtomicLongFieldUpdater<TracingServerCall> MESSAGE_ID_UPDATER =
37-
AtomicLongFieldUpdater.newUpdater(TracingServerCall.class, "messageId");
36+
private static final AtomicLongFieldUpdater<TracingServerCall> SENT_MESSAGE_ID_UPDATER =
37+
AtomicLongFieldUpdater.newUpdater(TracingServerCall.class, "sentMessageId");
38+
39+
@SuppressWarnings("rawtypes")
40+
private static final AtomicLongFieldUpdater<TracingServerCall> RECEIVED_MESSAGE_ID_UPDATER =
41+
AtomicLongFieldUpdater.newUpdater(TracingServerCall.class, "receivedMessageId");
3842

3943
private static final Metadata.Key<String> AUTHORITY_KEY =
4044
InternalMetadata.keyOf(":authority", Metadata.ASCII_STRING_MARSHALLER);
4145

4246
private final Instrumenter<GrpcRequest, Status> instrumenter;
4347
private final boolean captureExperimentalSpanAttributes;
48+
private final boolean addMessageEvents;
4449

4550
TracingServerInterceptor(
46-
Instrumenter<GrpcRequest, Status> instrumenter, boolean captureExperimentalSpanAttributes) {
51+
Instrumenter<GrpcRequest, Status> instrumenter,
52+
boolean captureExperimentalSpanAttributes,
53+
boolean addMessageEvents) {
4754
this.instrumenter = instrumenter;
4855
this.captureExperimentalSpanAttributes = captureExperimentalSpanAttributes;
56+
this.addMessageEvents = addMessageEvents;
4957
}
5058

5159
@Override
@@ -87,7 +95,10 @@ final class TracingServerCall<REQUEST, RESPONSE>
8795

8896
// Used by MESSAGE_ID_UPDATER
8997
@SuppressWarnings("UnusedVariable")
90-
volatile long messageId;
98+
volatile long sentMessageId;
99+
100+
@SuppressWarnings("UnusedVariable")
101+
volatile long receivedMessageId;
91102

92103
TracingServerCall(
93104
ServerCall<REQUEST, RESPONSE> delegate, Context context, GrpcRequest request) {
@@ -106,10 +117,11 @@ public void sendMessage(RESPONSE message) {
106117
try (Scope ignored = context.makeCurrent()) {
107118
super.sendMessage(message);
108119
}
109-
Span span = Span.fromContext(context);
110-
Attributes attributes =
111-
Attributes.of(MESSAGE_TYPE, SENT, MESSAGE_ID, MESSAGE_ID_UPDATER.incrementAndGet(this));
112-
span.addEvent("message", attributes);
120+
long messageId = SENT_MESSAGE_ID_UPDATER.incrementAndGet(this);
121+
if (addMessageEvents) {
122+
Attributes attributes = Attributes.of(MESSAGE_TYPE, SENT, MESSAGE_ID, messageId);
123+
Span.fromContext(context).addEvent("message", attributes);
124+
}
113125
}
114126

115127
@Override
@@ -134,15 +146,27 @@ final class TracingServerCallListener
134146
this.request = request;
135147
}
136148

149+
private void end(Context context, GrpcRequest request, Status response, Throwable error) {
150+
if (captureExperimentalSpanAttributes) {
151+
Span span = Span.fromContext(context);
152+
span.setAttribute(
153+
"grpc.messages.received", RECEIVED_MESSAGE_ID_UPDATER.get(TracingServerCall.this));
154+
span.setAttribute(
155+
"grpc.messages.sent", SENT_MESSAGE_ID_UPDATER.get(TracingServerCall.this));
156+
if (Status.CANCELLED.equals(status)) {
157+
span.setAttribute("grpc.canceled", true);
158+
}
159+
}
160+
instrumenter.end(context, request, response, error);
161+
}
162+
137163
@Override
138164
public void onMessage(REQUEST message) {
139-
Attributes attributes =
140-
Attributes.of(
141-
MESSAGE_TYPE,
142-
RECEIVED,
143-
MESSAGE_ID,
144-
MESSAGE_ID_UPDATER.incrementAndGet(TracingServerCall.this));
145-
Span.fromContext(context).addEvent("message", attributes);
165+
long messageId = RECEIVED_MESSAGE_ID_UPDATER.incrementAndGet(TracingServerCall.this);
166+
if (addMessageEvents) {
167+
Attributes attributes = Attributes.of(MESSAGE_TYPE, RECEIVED, MESSAGE_ID, messageId);
168+
Span.fromContext(context).addEvent("message", attributes);
169+
}
146170
delegate().onMessage(message);
147171
}
148172

@@ -160,36 +184,33 @@ public void onHalfClose() {
160184
public void onCancel() {
161185
try {
162186
delegate().onCancel();
163-
if (captureExperimentalSpanAttributes) {
164-
Span.fromContext(context).setAttribute("grpc.canceled", true);
165-
}
166187
} catch (Throwable e) {
167-
instrumenter.end(context, request, Status.UNKNOWN, e);
188+
end(context, request, Status.UNKNOWN, e);
168189
throw e;
169190
}
170-
instrumenter.end(context, request, Status.CANCELLED, null);
191+
end(context, request, Status.CANCELLED, null);
171192
}
172193

173194
@Override
174195
public void onComplete() {
175196
try {
176197
delegate().onComplete();
177198
} catch (Throwable e) {
178-
instrumenter.end(context, request, Status.UNKNOWN, e);
199+
end(context, request, Status.UNKNOWN, e);
179200
throw e;
180201
}
181202
if (status == null) {
182203
status = Status.UNKNOWN;
183204
}
184-
instrumenter.end(context, request, status, status.getCause());
205+
end(context, request, status, status.getCause());
185206
}
186207

187208
@Override
188209
public void onReady() {
189210
try {
190211
delegate().onReady();
191212
} catch (Throwable e) {
192-
instrumenter.end(context, request, Status.UNKNOWN, e);
213+
end(context, request, Status.UNKNOWN, e);
193214
throw e;
194215
}
195216
}

0 commit comments

Comments
 (0)