Skip to content

Commit 50cfcf6

Browse files
committed
make geode indy-ready
1 parent 397e621 commit 50cfcf6

File tree

1 file changed

+53
-45
lines changed

1 file changed

+53
-45
lines changed

instrumentation/geode-1.4/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/geode/GeodeRegionInstrumentation.java

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.opentelemetry.context.Scope;
2020
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2121
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
22+
import javax.annotation.Nullable;
2223
import net.bytebuddy.asm.Advice;
2324
import net.bytebuddy.description.type.TypeDescription;
2425
import net.bytebuddy.matcher.ElementMatcher;
@@ -61,76 +62,83 @@ public void transform(TypeTransformer transformer) {
6162
this.getClass().getName() + "$QueryAdvice");
6263
}
6364

64-
@SuppressWarnings("unused")
65-
public static class SimpleAdvice {
65+
public static class AdviceScope {
66+
public GeodeRequest request;
67+
public Context context;
68+
public Scope scope;
6669

67-
@Advice.OnMethodEnter(suppress = Throwable.class)
68-
public static void onEnter(
69-
@Advice.This Region<?, ?> region,
70-
@Advice.Origin("#m") String methodName,
71-
@Advice.Local("otelRequest") GeodeRequest request,
72-
@Advice.Local("otelContext") Context context,
73-
@Advice.Local("otelScope") Scope scope) {
70+
public AdviceScope(GeodeRequest request, Context context, Scope scope) {
71+
this.request = request;
72+
this.context = context;
73+
this.scope = scope;
74+
}
7475

76+
@Nullable
77+
public static AdviceScope start(
78+
Region<?, ?> region, String methodName, @Nullable String query) {
7579
Context parentContext = currentContext();
76-
request = GeodeRequest.create(region, methodName, null);
80+
GeodeRequest request = GeodeRequest.create(region, methodName, query);
7781
if (!instrumenter().shouldStart(parentContext, request)) {
78-
return;
82+
return null;
83+
}
84+
85+
Context context = instrumenter().start(parentContext, request);
86+
return new AdviceScope(request, context, context.makeCurrent());
87+
}
88+
89+
public void end(@Nullable Throwable throwable) {
90+
if (scope != null) {
91+
scope.close();
7992
}
93+
instrumenter().end(context, request, null, throwable);
94+
}
95+
}
96+
97+
@SuppressWarnings("unused")
98+
public static class SimpleAdvice {
8099

81-
context = instrumenter().start(parentContext, request);
82-
scope = context.makeCurrent();
100+
public static class AdviceLocals {
101+
public GeodeRequest request;
102+
public Context context;
103+
public Scope scope;
104+
}
105+
106+
@Nullable
107+
@Advice.OnMethodEnter(suppress = Throwable.class)
108+
public static AdviceScope onEnter(
109+
@Advice.This Region<?, ?> region, @Advice.Origin("#m") String methodName) {
110+
return AdviceScope.start(region, methodName, null);
83111
}
84112

85113
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
86114
public static void stopSpan(
87-
@Advice.Thrown Throwable throwable,
88-
@Advice.Local("otelRequest") GeodeRequest request,
89-
@Advice.Local("otelContext") Context context,
90-
@Advice.Local("otelScope") Scope scope) {
91-
if (scope == null) {
92-
return;
115+
@Advice.Thrown @Nullable Throwable throwable,
116+
@Advice.Enter @Nullable AdviceScope adviceScope) {
117+
if (adviceScope != null) {
118+
adviceScope.end(throwable);
93119
}
94-
95-
scope.close();
96-
instrumenter().end(context, request, null, throwable);
97120
}
98121
}
99122

100123
@SuppressWarnings("unused")
101124
public static class QueryAdvice {
102125

126+
@Nullable
103127
@Advice.OnMethodEnter(suppress = Throwable.class)
104-
public static void onEnter(
128+
public static AdviceScope onEnter(
105129
@Advice.This Region<?, ?> region,
106130
@Advice.Origin("#m") String methodName,
107-
@Advice.Argument(0) String query,
108-
@Advice.Local("otelRequest") GeodeRequest request,
109-
@Advice.Local("otelContext") Context context,
110-
@Advice.Local("otelScope") Scope scope) {
111-
112-
Context parentContext = currentContext();
113-
request = GeodeRequest.create(region, methodName, query);
114-
if (!instrumenter().shouldStart(parentContext, request)) {
115-
return;
116-
}
117-
118-
context = instrumenter().start(parentContext, request);
119-
scope = context.makeCurrent();
131+
@Advice.Argument(0) String query) {
132+
return AdviceScope.start(region, methodName, query);
120133
}
121134

122135
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
123136
public static void stopSpan(
124-
@Advice.Thrown Throwable throwable,
125-
@Advice.Local("otelRequest") GeodeRequest request,
126-
@Advice.Local("otelContext") Context context,
127-
@Advice.Local("otelScope") Scope scope) {
128-
if (scope == null) {
129-
return;
137+
@Advice.Thrown @Nullable Throwable throwable,
138+
@Advice.Enter @Nullable AdviceScope adviceScope) {
139+
if (adviceScope != null) {
140+
adviceScope.end(throwable);
130141
}
131-
132-
scope.close();
133-
instrumenter().end(context, request, null, throwable);
134142
}
135143
}
136144
}

0 commit comments

Comments
 (0)