Skip to content

Commit 8754010

Browse files
committed
fix rest instrumentation
1 parent b4a314e commit 8754010

File tree

1 file changed

+32
-24
lines changed
  • instrumentation/elasticsearch/elasticsearch-rest-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/rest/v7_0

1 file changed

+32
-24
lines changed

instrumentation/elasticsearch/elasticsearch-rest-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/rest/v7_0/RestClientInstrumentation.java

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,43 @@ public void transform(TypeTransformer transformer) {
5555
public static class AdviceScope {
5656
private final ElasticsearchRestRequest request;
5757
private final Context context;
58+
private final Context parentContext;
5859
private final Scope scope;
5960

60-
public AdviceScope(ElasticsearchRestRequest request, Context context, Scope scope) {
61+
private AdviceScope(
62+
ElasticsearchRestRequest request, Context parentContext, Context context, Scope scope) {
6163
this.request = request;
64+
this.parentContext = parentContext;
6265
this.context = context;
6366
this.scope = scope;
6467
}
6568

66-
public void end(@Nullable Throwable throwable) {
69+
@Nullable
70+
public static AdviceScope start(ElasticsearchRestRequest request, Context parentContext) {
71+
if (!instrumenter().shouldStart(parentContext, request)) {
72+
return null;
73+
}
74+
Context context = instrumenter().start(parentContext, request);
75+
return new AdviceScope(request, parentContext, context, context.makeCurrent());
76+
}
77+
78+
public ResponseListener wrapListener(ResponseListener responseListener) {
79+
return new RestResponseListener(
80+
responseListener, parentContext, instrumenter(), context, request);
81+
}
82+
83+
public void endWithListener(@Nullable Throwable throwable) {
6784
scope.close();
6885
if (throwable != null) {
6986
instrumenter().end(context, request, null, throwable);
7087
}
7188
// span ended in RestResponseListener
7289
}
90+
91+
public void endWithResponse(@Nullable Throwable throwable, @Nullable Response response) {
92+
scope.close();
93+
instrumenter().end(context, request, response, throwable);
94+
}
7395
}
7496

7597
@SuppressWarnings("unused")
@@ -78,31 +100,23 @@ public static class PerformRequestAdvice {
78100
@Nullable
79101
@Advice.OnMethodEnter(suppress = Throwable.class)
80102
public static AdviceScope onEnter(@Advice.Argument(0) Request request) {
81-
82-
Context parentContext = currentContext();
83103
ElasticsearchRestRequest otelRequest =
84104
ElasticsearchRestRequest.create(
85105
request.getMethod(),
86106
request.getEndpoint(),
87107
// set by elasticsearch-api-client instrumentation
88108
ENDPOINT_DEFINITION.get(request),
89109
request.getEntity());
90-
if (!instrumenter().shouldStart(parentContext, otelRequest)) {
91-
return null;
92-
}
93-
94-
Context context = instrumenter().start(parentContext, otelRequest);
95-
return new AdviceScope(otelRequest, context, context.makeCurrent());
110+
return AdviceScope.start(otelRequest, currentContext());
96111
}
97112

98113
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
99114
public static void stopSpan(
100-
@Advice.Return Response response,
101-
@Advice.Thrown Throwable throwable,
115+
@Advice.Return @Nullable Response response,
116+
@Advice.Thrown @Nullable Throwable throwable,
102117
@Advice.Enter @Nullable AdviceScope adviceScope) {
103-
104118
if (adviceScope != null) {
105-
adviceScope.end(throwable);
119+
adviceScope.endWithResponse(throwable, response);
106120
}
107121
}
108122
}
@@ -115,25 +129,19 @@ public static class PerformRequestAsyncAdvice {
115129
public static Object[] onEnter(
116130
@Advice.Argument(0) Request request,
117131
@Advice.Argument(1) ResponseListener originalResponseListener) {
118-
119132
ResponseListener responseListener = originalResponseListener;
120-
Context parentContext = currentContext();
121133
ElasticsearchRestRequest otelRequest =
122134
ElasticsearchRestRequest.create(
123135
request.getMethod(),
124136
request.getEndpoint(),
125137
// set by elasticsearch-api-client instrumentation
126138
ENDPOINT_DEFINITION.get(request),
127139
request.getEntity());
128-
if (!instrumenter().shouldStart(parentContext, otelRequest)) {
140+
AdviceScope adviceScope = AdviceScope.start(otelRequest, currentContext());
141+
if (adviceScope == null) {
129142
return new Object[] {null, responseListener};
130143
}
131-
132-
Context context = instrumenter().start(parentContext, otelRequest);
133-
AdviceScope adviceScope = new AdviceScope(otelRequest, context, context.makeCurrent());
134-
responseListener =
135-
new RestResponseListener(
136-
responseListener, parentContext, instrumenter(), context, otelRequest);
144+
responseListener = adviceScope.wrapListener(responseListener);
137145
return new Object[] {adviceScope, responseListener};
138146
}
139147

@@ -142,7 +150,7 @@ public static void stopSpan(
142150
@Advice.Thrown @Nullable Throwable throwable, @Advice.Enter Object[] enterResult) {
143151
AdviceScope adviceScope = (AdviceScope) enterResult[0];
144152
if (adviceScope != null) {
145-
adviceScope.end(throwable);
153+
adviceScope.endWithListener(throwable);
146154
}
147155
}
148156
}

0 commit comments

Comments
 (0)