Skip to content

Commit

Permalink
Grpc smoke test (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalom938 authored May 8, 2024
1 parent 7da251b commit 9950442
Show file tree
Hide file tree
Showing 42 changed files with 1,209 additions and 215 deletions.
27 changes: 24 additions & 3 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
cancel-in-progress: false

jobs:

Expand All @@ -20,10 +20,10 @@ jobs:
- name: GIT Checkout
uses: actions/checkout@v3

- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '17'
distribution: temurin

# using wrapper version (supposed to be 7.5.1)
Expand All @@ -43,3 +43,24 @@ jobs:
name: digma-otel-agent-extension
path: ./agent-extension/build/libs/digma-otel-agent-extension*.jar
retention-days: 5

- name: Upload agent-extension tests report
uses: actions/upload-artifact@v3
with:
name: agent-extension-tests-report
path: ./agent-extension/build/reports/tests/
retention-days: 5

- name: Upload methods instrumentation tests report
uses: actions/upload-artifact@v3
with:
name: methods-instrumentation-tests-report
path: ./testing/methods/build/reports/tests/
retention-days: 5

- name: Upload java 7 instrumentation tests report
uses: actions/upload-artifact@v3
with:
name: java7-instrumentation-tests-report
path: ./testing/java7/build/reports/tests/
retention-days: 5
4 changes: 2 additions & 2 deletions .github/workflows/ci-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:
- name: GIT Checkout
uses: actions/checkout@v3

- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '17'
distribution: temurin

# using wrapper version (supposed to be 7.5.1)
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ build/*
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*


## Idea profiler
.profileconfig.json
20 changes: 20 additions & 0 deletions DEV_NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@


## Build
./gradlew clean build<br>
or<br>
./gradlew clean build -x test


## When running tests from Idea
turn off your Digma plugin observability :)

## New instrumentation advices
every new advice should be tested with java 7 bytecode.
see Java7CompatibilityTests.
read the comment there.



### Using podman with testcontainers
https://github.com/eugene-khyst/podman-testcontainers?tab=readme-ov-file#5
9 changes: 8 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 @@ -132,6 +132,7 @@ dependencies {
testImplementation("io.opentelemetry.proto:opentelemetry-proto:1.1.0-alpha")

testImplementation("org.junit.jupiter:junit-jupiter-api:${versions.junit}")
testImplementation("org.junit.jupiter:junit-jupiter-params:${versions.junit}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${versions.junit}")
testRuntimeOnly("ch.qos.logback:logback-classic:1.5.3")

Expand All @@ -142,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
@@ -1,6 +1,7 @@
package com.digma.otel.javaagent.extension.instrumentation.common;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import net.bytebuddy.asm.Advice;

import java.lang.reflect.Method;
Expand Down Expand Up @@ -33,10 +34,10 @@ public static void methodEnter(
targetClassName = method.getDeclaringClass().getName();
}

Span currentSpan = Span.current();
Span currentSpan = Java8BytecodeBridge.currentSpan();

currentSpan.setAttribute(stringKey("code.namespace"), targetClassName);
currentSpan.setAttribute(stringKey("code.function"), method.getName());
currentSpan.setAttribute("code.namespace", targetClassName);
currentSpan.setAttribute("code.function", method.getName());
}

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.digma.otel.javaagent.extension.instrumentation.common;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.LocalRootSpan;
import io.opentelemetry.instrumentation.api.internal.HttpRouteState;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import net.bytebuddy.asm.Advice;

import java.lang.reflect.Method;

import static io.opentelemetry.api.common.AttributeKey.stringKey;

public class DigmaServerAdvice {

//Note when declaring static variables here like a logger the advice doesn't run, strange bytebuddy issue

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void methodEnter(
@Advice.This Object target,
Expand All @@ -35,46 +31,34 @@ public static void methodEnter(
// taking local root span (servlet of tomcat or jetty) and set the code attributes on it

//Find http route span and set it
HttpRouteState routeStateNew = HttpRouteState.fromContextOrNull(Context.current());
HttpRouteState routeStateNew = HttpRouteState.fromContextOrNull(Java8BytecodeBridge.currentContext());
Span routeSpan = null;

if (routeStateNew != null) {

try {
routeSpan = routeStateNew.getSpan();
if (routeSpan != null) {

routeSpan.setAttribute(stringKey("code.namespace"), targetClassName);
routeSpan.setAttribute(stringKey("code.function"), method.getName());
routeSpan.setAttribute("code.namespace", targetClassName);
routeSpan.setAttribute("code.function", method.getName());
}
} catch (Error e) {

//do nothing
//ignore
}
}

//Fallback to previous behavior
else {

Span rootSpan = LocalRootSpan.current();
if (rootSpan != null) {
// System.out.println("DBG: setting local root span ");

rootSpan.setAttribute(stringKey("code.namespace"), targetClassName);
rootSpan.setAttribute(stringKey("code.function"), method.getName());
rootSpan.setAttribute("code.namespace", targetClassName);
rootSpan.setAttribute("code.function", method.getName());
}
}

Span span = Span.fromContextOrNull(Context.current());
Span span = Java8BytecodeBridge.spanFromContext(Java8BytecodeBridge.currentContext());
if (span != null) {

span.setAttribute(stringKey("code.namespace"), targetClassName);
span.setAttribute(stringKey("code.function"), method.getName());
span.setAttribute("code.namespace", targetClassName);
span.setAttribute("code.function", method.getName());
}


}



}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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());
}

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
Expand Up @@ -2,7 +2,6 @@

import com.digma.otel.instrumentation.common.DigmaSemanticAttributes;
import com.digma.otel.instrumentation.common.DigmaSemanticConventions;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
Expand Down Expand Up @@ -58,7 +57,7 @@ public static void methodExit(
testingResultValue = DigmaSemanticConventions.TestingResultValues.ERROR;
}
}
Span.current().setAttribute(DigmaSemanticAttributes.TESTING_RESULT, testingResultValue);
Java8BytecodeBridge.currentSpan().setAttribute(DigmaSemanticAttributes.TESTING_RESULT, testingResultValue);

if (scope == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ private ElementMatcher<? super MethodDescription> methodsFilterByAnnotation() {
"org.junit.runners.Parameterized.BeforeParam",
"org.junit.runners.Parameterized.Parameters"))
).or(
isAnnotatedWith(namedOneOf("io.micrometer.tracing.annotation.NewSpan",
isAnnotatedWith(namedOneOf(
"io.micrometer.tracing.annotation.NewSpan",
"io.micrometer.tracing.annotation.ContinueSpan",
"io.micrometer.observation.annotation.Observed"))
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.digma.otel.javaagent.extension.smoketest;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public abstract class AllJdksParameterizedTest extends IntegrationTest{


//Idea complains not finding this method, but it compiles and runs.
//see https://youtrack.jetbrains.com/issue/IDEA-349571/Idea-complains-about-local-annotation-with-ParameterizedTest-and-MethodSource-but-it-runs-with-gradle-and-from-run-configuration
protected static Stream<Arguments> allJdks() {
return IntStream.of(8, 11,17).mapToObj(Arguments::of);
// return IntStream.of(8, 11, 17,21).mapToObj(Arguments::of);
}

@ParameterizedTest(name = "jdk {0}")
@MethodSource("allJdks")
@Retention(RetentionPolicy.RUNTIME)
protected @interface TestAllJdks {
}


}
Loading

0 comments on commit 9950442

Please sign in to comment.