Skip to content

Commit 78e6f6a

Browse files
authored
Merge pull request #457 from DataDog/tyler/grpc-async
Enable executor instrumentation for gRPC server calls
2 parents 51d74f6 + 63bee37 commit 78e6f6a

1 file changed

Lines changed: 43 additions & 4 deletions

File tree

dd-java-agent/instrumentation/grpc-1.5/src/main/java/datadog/trace/instrumentation/grpc/server/TracingServerInterceptor.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import datadog.trace.api.DDSpanTypes;
66
import datadog.trace.api.DDTags;
7+
import datadog.trace.context.TraceScope;
78
import io.grpc.ForwardingServerCallListener;
89
import io.grpc.Metadata;
910
import io.grpc.ServerCall;
@@ -54,6 +55,11 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
5455
spanBuilder.asChildOf(spanContext);
5556
}
5657
final Scope scope = spanBuilder.startActive(false);
58+
59+
if (scope instanceof TraceScope) {
60+
((TraceScope) scope).setAsyncPropagation(true);
61+
}
62+
5763
final Span span = scope.span();
5864

5965
final ServerCall.Listener<ReqT> result;
@@ -66,6 +72,9 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
6672
span.finish();
6773
throw e;
6874
} finally {
75+
if (scope instanceof TraceScope) {
76+
((TraceScope) scope).setAsyncPropagation(false);
77+
}
6978
scope.close();
7079
}
7180

@@ -95,6 +104,9 @@ public void onMessage(final ReqT message) {
95104
.withTag(DDTags.SPAN_TYPE, DDSpanTypes.RPC)
96105
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER)
97106
.startActive(true);
107+
if (scope instanceof TraceScope) {
108+
((TraceScope) scope).setAsyncPropagation(true);
109+
}
98110
try {
99111
delegate().onMessage(message);
100112
} catch (final RuntimeException | Error e) {
@@ -104,14 +116,23 @@ public void onMessage(final ReqT message) {
104116
this.span.finish();
105117
throw e;
106118
} finally {
119+
if (scope instanceof TraceScope) {
120+
((TraceScope) scope).setAsyncPropagation(false);
121+
}
107122
scope.close();
108123
}
109124
}
110125

111126
@Override
112127
public void onHalfClose() {
113-
try (final Scope ignored = tracer.scopeManager().activate(span, false)) {
128+
try (final Scope scope = tracer.scopeManager().activate(span, false)) {
129+
if (scope instanceof TraceScope) {
130+
((TraceScope) scope).setAsyncPropagation(true);
131+
}
114132
delegate().onHalfClose();
133+
if (scope instanceof TraceScope) {
134+
((TraceScope) scope).setAsyncPropagation(false);
135+
}
115136
} catch (final RuntimeException | Error e) {
116137
Tags.ERROR.set(span, true);
117138
span.log(Collections.singletonMap(ERROR_OBJECT, e));
@@ -123,9 +144,15 @@ public void onHalfClose() {
123144
@Override
124145
public void onCancel() {
125146
// Finishes span.
126-
try (final Scope ignored = tracer.scopeManager().activate(span, true)) {
147+
try (final Scope scope = tracer.scopeManager().activate(span, true)) {
148+
if (scope instanceof TraceScope) {
149+
((TraceScope) scope).setAsyncPropagation(true);
150+
}
127151
delegate().onCancel();
128152
span.setTag("canceled", true);
153+
if (scope instanceof TraceScope) {
154+
((TraceScope) scope).setAsyncPropagation(false);
155+
}
129156
} catch (final RuntimeException | Error e) {
130157
Tags.ERROR.set(span, true);
131158
span.log(Collections.singletonMap(ERROR_OBJECT, e));
@@ -137,8 +164,14 @@ public void onCancel() {
137164
@Override
138165
public void onComplete() {
139166
// Finishes span.
140-
try (final Scope ignored = tracer.scopeManager().activate(span, true)) {
167+
try (final Scope scope = tracer.scopeManager().activate(span, true)) {
168+
if (scope instanceof TraceScope) {
169+
((TraceScope) scope).setAsyncPropagation(true);
170+
}
141171
delegate().onComplete();
172+
if (scope instanceof TraceScope) {
173+
((TraceScope) scope).setAsyncPropagation(false);
174+
}
142175
} catch (final RuntimeException | Error e) {
143176
Tags.ERROR.set(span, true);
144177
span.log(Collections.singletonMap(ERROR_OBJECT, e));
@@ -149,8 +182,14 @@ public void onComplete() {
149182

150183
@Override
151184
public void onReady() {
152-
try (final Scope ignored = tracer.scopeManager().activate(span, false)) {
185+
try (final Scope scope = tracer.scopeManager().activate(span, false)) {
186+
if (scope instanceof TraceScope) {
187+
((TraceScope) scope).setAsyncPropagation(true);
188+
}
153189
delegate().onReady();
190+
if (scope instanceof TraceScope) {
191+
((TraceScope) scope).setAsyncPropagation(false);
192+
}
154193
} catch (final RuntimeException | Error e) {
155194
Tags.ERROR.set(span, true);
156195
span.log(Collections.singletonMap(ERROR_OBJECT, e));

0 commit comments

Comments
 (0)