Skip to content

Commit

Permalink
fix grpc interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
shalom938 committed May 7, 2024
1 parent 3910402 commit af9a408
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 85 deletions.
8 changes: 7 additions & 1 deletion agent-extension/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ ext {
// various instrumentations
springFramework : "5.3.24",
springKafka : "2.7.1",
grpc : "1.6.0",
grpc : "1.63.0",
]

deps = [
Expand Down Expand Up @@ -143,6 +143,12 @@ dependencies {
testImplementation("org.springframework:spring-context:${versions.springFramework}")
testImplementation("org.springframework:spring-web:${versions.springFramework}")
testImplementation("org.springframework.kafka:spring-kafka:${versions.springKafka}")
testImplementation("io.grpc:grpc-core:${versions.grpc}")
testImplementation("io.grpc:grpc-stub:${versions.grpc}")
testImplementation("io.grpc:grpc-okhttp:${versions.grpc}")
testImplementation("io.grpc:grpc-protobuf:${versions.grpc}")



//TODO remove when start using io.opentelemetry.instrumentation.javaagent-instrumentation plugin
add("codegen", "io.opentelemetry.javaagent:opentelemetry-javaagent-tooling:${versions.opentelemetryJavaagentAlpha}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public abstract class DigmaTypeInstrumentation implements TypeInstrumentation {

@Override
public final ElementMatcher<TypeDescription> typeMatcher() {
return hasClassFileVersionAtLeast(ClassFileVersion.JAVA_V7).and(digmaTypeMatcher());
// return hasClassFileVersionAtLeast(ClassFileVersion.JAVA_V8).and(digmaTypeMatcher());
// return hasClassFileVersionAtLeast(ClassFileVersion.JAVA_V7).and(digmaTypeMatcher());
return hasClassFileVersionAtLeast(ClassFileVersion.JAVA_V8).and(digmaTypeMatcher());
}

public abstract ElementMatcher<TypeDescription> digmaTypeMatcher();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -34,4 +35,12 @@ public List<TypeInstrumentation> typeInstrumentations() {
return Collections.singletonList(new DigmaGrpcServerBuilderInstrumentation());
}

@Override
public List<String> getAdditionalHelperClassNames() {
List<String> classNames = new ArrayList<>();
classNames.add("io.opentelemetry.instrumentation.api.instrumenter.util.ClassAndMethod");
classNames.add("io.opentelemetry.instrumentation.api.instrumenter.code.CodeAttributesGetter");
classNames.add("io.opentelemetry.instrumentation.api.instrumenter.util.AutoValue_ClassAndMethod");
return classNames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@ public ElementMatcher<TypeDescription> digmaTypeMatcher() {
@Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
isMethod().and(isPublic()).and(named("build")).and(takesArguments(0)),
DigmaGrpcServerBuilderInstrumentation.class.getName() + "$DigmaBuildAdvice");
isMethod().and(isPublic()).and(named("build")).and(takesArguments(0)),
DigmaGrpcServerBuilderInstrumentation.class.getName() + "$DigmaBuildAdvice");
}

@SuppressWarnings("unused")
public static class DigmaBuildAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.This ServerBuilder<?> serverBuilder) {

@Advice.This ServerBuilder<?> serverBuilder) {
serverBuilder.intercept(DigmaTracingServerInterceptor.create());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,87 @@
package com.digma.otel.javaagent.extension.smoketest;

import com.digma.otel.javaagent.extension.AgentExtensionVersion;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
import io.opentelemetry.proto.collector.trace.v1.TraceServiceGrpc;
import org.junit.jupiter.api.Assertions;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.wait.strategy.WaitStrategy;

public class GrpcIntegrationTests extends AllJdksParameterizedTest{
import java.io.IOException;
import java.time.Duration;
import java.util.Collection;
import java.util.jar.Attributes;
import java.util.jar.JarFile;

//todo : complete ,
// see https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/5df8a5a0a0e90fc7a867f8f013bd050f132a090b/smoke-tests
public class GrpcIntegrationTests extends AllJdksParameterizedTest{

@Override
protected String getTargetImage(int jdk) {
//todo: find the tag
return "ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-grpc:jdk";
return "ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-grpc:jdk"
+ jdk
+ "-20230228.4289437270";
}

@Override
protected WaitStrategy getTargetWaitStrategy() {
return null;
return Wait.forLogMessage(".*Server started.*", 1)
.withStartupTimeout(Duration.ofMinutes(1));
}

@TestAllJdks
public void extensionsAreLoadedFromJar(int jdk) throws IOException, InterruptedException {
startTarget("/opentelemetry-extensions.jar", jdk);

testAndVerify();

stopTarget();
}


@TestAllJdks
public void extensionsAreLoadedFromFolder(int jdk) throws IOException, InterruptedException {
startTarget("/", jdk);

testAndVerify();

stopTarget();
}

@TestAllJdks
public void extensionsAreLoadedFromJavaagent(int jdk) throws IOException, InterruptedException {
startTargetWithExtendedAgent(jdk);

testAndVerify();

stopTarget();
}




private void testAndVerify() throws IOException, InterruptedException {

ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost",target.getMappedPort(8080))
.usePlaintext()
.build();
TraceServiceGrpc.TraceServiceBlockingStub stub = TraceServiceGrpc.newBlockingStub(managedChannel);
stub.export(ExportTraceServiceRequest.getDefaultInstance());

String currentAgentVersion = (String) new JarFile(agentPath).getManifest().getMainAttributes().get(Attributes.Name.IMPLEMENTATION_VERSION);

Collection<ExportTraceServiceRequest> traces = waitForTraces();


Assertions.assertEquals(1, countSpansByName(traces, "opentelemetry.proto.collector.trace.v1.TraceService/Export"));
Assertions.assertEquals(1, countSpansByName(traces, "TestService.withSpan"));

Assertions.assertEquals(1, countSpansByAttributeValue(traces, "code.function", "export"));
Assertions.assertEquals(2, countSpansByAttributeValue(traces, "code.namespace", "io.opentelemetry.smoketest.grpc.TestService"));

Assertions.assertEquals(2, countResourcesByValue(traces, "digma.agent.version", AgentExtensionVersion.VERSION));
Assertions.assertNotEquals(
0, countResourcesByValue(traces, "telemetry.distro.version", currentAgentVersion));
}
}
1 change: 1 addition & 0 deletions instrumentation/grpc-16/library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
runtimeOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi:${OPENTELEMETRY_VERSION}")
runtimeOnly("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api:${OPENTELEMETRY_VERSION}")
runtimeOnly("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-semconv:${OPENTELEMETRY_ALPHA_VERSION}")
compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api:${OPENTELEMETRY_ALPHA_VERSION}")
compileOnly("io.opentelemetry.instrumentation:opentelemetry-grpc-1.6:${OPENTELEMETRY_ALPHA_VERSION}")
compileOnly("io.grpc:grpc-core:$grpcVersion")

Expand Down
Loading

0 comments on commit af9a408

Please sign in to comment.