Skip to content

GraalVM native support for the OpenTelemetry annotations #11757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 10, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.annotations;

import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;

class OpenTelemetryAnnotationsRuntimeHints implements RuntimeHintsRegistrar {

@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints
.reflection()
.registerType(
TypeReference.of(
"io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.annotations.InstrumentationWithSpanAspect"),
hint -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
org.springframework.aot.hint.RuntimeHintsRegistrar=\
io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.annotations.OpenTelemetryAnnotationsRuntimeHints
1 change: 1 addition & 0 deletions smoke-tests-otel-starter/spring-boot-2/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
implementation("org.apache.commons:commons-dbcp2")
implementation("org.springframework.kafka:spring-kafka")
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
implementation("org.springframework.boot:spring-boot-starter-aop")
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))

implementation(project(":smoke-tests-otel-starter:spring-boot-common"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
implementation("org.apache.commons:commons-dbcp2")
implementation("org.springframework.kafka:spring-kafka")
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
implementation("org.springframework.boot:spring-boot-starter-aop")
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))

implementation(project(":smoke-tests-otel-starter:spring-boot-common"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void restClient() {
private static void assertClient(TraceAssert traceAssert) {
traceAssert.hasSpansSatisfyingExactly(
span -> AbstractOtelSpringStarterSmokeTest.assertClientSpan(span, "/ping"),
span -> span.hasKind(SpanKind.SERVER).hasAttribute(HttpAttributes.HTTP_ROUTE, "/ping"));
span -> span.hasKind(SpanKind.SERVER).hasAttribute(HttpAttributes.HTTP_ROUTE, "/ping"),
span -> withSpanAssert(span));
}
}
1 change: 1 addition & 0 deletions smoke-tests-otel-starter/spring-boot-3/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
implementation("org.apache.commons:commons-dbcp2")
implementation("org.springframework.kafka:spring-kafka")
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
implementation("org.springframework.boot:spring-boot-starter-aop")
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))

implementation(project(":smoke-tests-otel-starter:spring-boot-common"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {
compileOnly("org.testcontainers:junit-jupiter")
compileOnly("org.testcontainers:kafka")
compileOnly("org.testcontainers:mongodb")
compileOnly("org.springframework.boot:spring-boot-starter-aop")

api(project(":smoke-tests-otel-starter:spring-smoke-testing"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ void shouldSendTelemetry() {
equalTo(ClientAttributes.CLIENT_ADDRESS, "127.0.0.1"),
satisfies(
ServerAttributes.SERVER_PORT,
integerAssert -> integerAssert.isNotZero()))));
integerAssert -> integerAssert.isNotZero())),
span -> withSpanAssert(span)));

// Metric
testing.waitAndAssertMetrics(
Expand Down Expand Up @@ -236,8 +237,8 @@ void restTemplate() {
traceAssert.hasSpansSatisfyingExactly(
span -> assertClientSpan(span, "/ping"),
span ->
span.hasKind(SpanKind.SERVER)
.hasAttribute(HttpAttributes.HTTP_ROUTE, "/ping")));
span.hasKind(SpanKind.SERVER).hasAttribute(HttpAttributes.HTTP_ROUTE, "/ping"),
span -> withSpanAssert(span)));
}

public static void assertClientSpan(SpanDataAssert span, String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ public class OtelSpringStarterSmokeTestController {
public static final String TEST_HISTOGRAM = "histogram-test-otel-spring-starter";
public static final String METER_SCOPE_NAME = "scope";
private final LongHistogram histogram;
private final SpringComponent component;

public OtelSpringStarterSmokeTestController(OpenTelemetry openTelemetry) {
public OtelSpringStarterSmokeTestController(
OpenTelemetry openTelemetry, SpringComponent springComponent) {
Meter meter = openTelemetry.getMeter(METER_SCOPE_NAME);
histogram = meter.histogramBuilder(TEST_HISTOGRAM).ofLongs().build();
this.component = springComponent;
}

@GetMapping(PING)
public String ping() {
histogram.record(10);
component.withSpanMethod("from-controller");
return "pong";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.spring.smoketest;

import io.opentelemetry.instrumentation.annotations.SpanAttribute;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import org.springframework.stereotype.Component;

@Component
public class SpringComponent {

@SuppressWarnings("MethodCanBeStatic")
@WithSpan
public void withSpanMethod(@SpanAttribute String paramName) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.sdk.testing.assertj.SpanDataAssert;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -61,4 +63,9 @@ void checkSpringLogs(CapturedOutput output) {
}
});
}

static SpanDataAssert withSpanAssert(SpanDataAssert span) {
return span.hasName("SpringComponent.withSpanMethod")
.hasAttribute(AttributeKey.stringKey("paramName"), "from-controller");
}
}
Loading