Skip to content

Commit

Permalink
inject trace data to headers
Browse files Browse the repository at this point in the history
  • Loading branch information
chenlujjj committed Jan 7, 2025
1 parent 0190c13 commit 7781c8c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.opentelemetry.javaagent.instrumentation.jsonrpc4j.v1_6;

import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
Expand All @@ -11,13 +10,15 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.jsonrpc4j.v1_6.HeadersSetter;
import io.opentelemetry.instrumentation.jsonrpc4j.v1_6.SimpleJsonRpcRequest;
import io.opentelemetry.instrumentation.jsonrpc4j.v1_6.SimpleJsonRpcResponse;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import java.util.Map;

public class JsonRpcClientBuilderInstrumentation implements TypeInstrumentation {

Expand Down Expand Up @@ -53,9 +54,10 @@ public static class InvokeAdvice {
public static void onEnter(
@Advice.Argument(0) String methodName,
@Advice.Argument(1) Object argument,
@Advice.Argument(3) Map<String, String> extraHeaders,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = currentContext();
Context parentContext = Context.current();
SimpleJsonRpcRequest request = new SimpleJsonRpcRequest(
methodName,
argument
Expand All @@ -65,13 +67,16 @@ public static void onEnter(
}

context = JsonRpcSingletons.CLIENT_INSTRUMENTER.start(parentContext, request);
JsonRpcSingletons.PROPAGATORS.getTextMapPropagator().inject(context, extraHeaders, HeadersSetter.INSTANCE);

scope = context.makeCurrent();
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(
@Advice.Argument(0) String methodName,
@Advice.Argument(1) Object argument,
@Advice.Argument(3) Map<String, String> extraHeaders,
@Advice.Return Object result,
@Advice.Thrown Throwable throwable,
@Advice.Local("otelContext") Context context,
Expand All @@ -82,6 +87,7 @@ public static void onExit(

scope.close();
JsonRpcSingletons.CLIENT_INSTRUMENTER.end(context, new SimpleJsonRpcRequest(methodName, argument), new SimpleJsonRpcResponse(result), throwable);
System.out.println(extraHeaders);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.googlecode.jsonrpc4j.InvocationListener;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.jsonrpc4j.v1_6.JsonRpcTelemetry;
import io.opentelemetry.instrumentation.jsonrpc4j.v1_6.SimpleJsonRpcRequest;
Expand All @@ -14,6 +15,7 @@ public final class JsonRpcSingletons {

public static final Instrumenter<SimpleJsonRpcRequest, SimpleJsonRpcResponse> CLIENT_INSTRUMENTER;

public static final ContextPropagators PROPAGATORS;

static {
JsonRpcTelemetry telemetry =
Expand All @@ -22,6 +24,7 @@ public final class JsonRpcSingletons {

SERVER_INVOCATION_LISTENER = telemetry.newServerInvocationListener();
CLIENT_INSTRUMENTER = telemetry.getClientInstrumenter();
PROPAGATORS = telemetry.getPropagators();
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.opentelemetry.instrumentation.jsonrpc4j.v1_6;

import io.opentelemetry.context.propagation.TextMapSetter;
import java.util.Map;

public enum HeadersSetter implements TextMapSetter<Map<String, String>> {
INSTANCE;

@Override
public void set(Map<String, String> carrier, String key, String value) {
assert carrier != null;
carrier.put(key, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ public static JsonRpcTelemetryBuilder builder(OpenTelemetry openTelemetry) {

private final Instrumenter<JsonRpcRequest, JsonRpcResponse> serverInstrumenter;
private final Instrumenter<SimpleJsonRpcRequest, SimpleJsonRpcResponse> clientInstrumenter;
private final ContextPropagators propagators;

JsonRpcTelemetry(
Instrumenter<JsonRpcRequest, JsonRpcResponse> serverInstrumenter,
Instrumenter<SimpleJsonRpcRequest, SimpleJsonRpcResponse> clientInstrumenter,
ContextPropagators propagators) {
this.serverInstrumenter = serverInstrumenter;
this.clientInstrumenter = clientInstrumenter;
this.propagators = propagators;
}


Expand All @@ -34,4 +36,8 @@ public InvocationListener newServerInvocationListener() {
public Instrumenter<SimpleJsonRpcRequest, SimpleJsonRpcResponse> getClientInstrumenter() {
return clientInstrumenter;
}

public ContextPropagators getPropagators() {
return propagators;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ public JsonRpcTelemetryBuilder addServerAttributeExtractor(
return this;
}




public JsonRpcTelemetry build() {
SpanNameExtractor<SimpleJsonRpcRequest> clientSpanNameExtractor = new JsonRpcClientSpanNameExtractor();
SpanNameExtractor<JsonRpcRequest> serverSpanNameExtractor = new JsonRpcServerSpanNameExtractor();
Expand Down

0 comments on commit 7781c8c

Please sign in to comment.