Skip to content
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

Add baggage test to AbstractHttpServerTest #13027

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ class JavaagentTestArgumentsProvider(
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.internal.ServerImplBuilder=INFO",
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.internal.ManagedChannelImplBuilder=INFO",
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.perfmark.PerfMark=INFO",
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.Context=INFO"
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.Context=INFO",
"-Dotel.java.experimental.span-attributes.copy-from-baggage.include=*"
xiepuhuan marked this conversation as resolved.
Show resolved Hide resolved
)
}

Expand All @@ -108,12 +109,22 @@ afterEvaluate {
// this dependency.
dependsOn(agentForTesting.buildDependencies)

jvmArgumentProviders.add(JavaagentTestArgumentsProvider(agentShadowJar, shadowJar.archiveFile.get().asFile))
jvmArgumentProviders.add(
JavaagentTestArgumentsProvider(
agentShadowJar,
shadowJar.archiveFile.get().asFile
)
)

// We do fine-grained filtering of the classpath of this codebase's sources since Gradle's
// configurations will include transitive dependencies as well, which tests do often need.
classpath = classpath.filter {
if (file(layout.buildDirectory.dir("resources/main")).equals(it) || file(layout.buildDirectory.dir("classes/java/main")).equals(it)) {
if (file(layout.buildDirectory.dir("resources/main")).equals(it) || file(
layout.buildDirectory.dir(
"classes/java/main"
)
).equals(it)
) {
// The sources are packaged into the testing jar, so we need to exclude them from the test
// classpath, which automatically inherits them, to ensure our shaded versions are used.
return@filter false
Expand Down
11 changes: 9 additions & 2 deletions testing-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ group = "io.opentelemetry.javaagent"
sourceSets {
main {
val armeriaShadedDeps = project(":testing:armeria-shaded-for-testing")
output.dir(armeriaShadedDeps.file("build/extracted/shadow"), "builtBy" to ":testing:armeria-shaded-for-testing:extractShadowJar")
output.dir(
armeriaShadedDeps.file("build/extracted/shadow"),
"builtBy" to ":testing:armeria-shaded-for-testing:extractShadowJar"
)

val protoShadedDeps = project(":testing:proto-shaded-for-testing")
output.dir(protoShadedDeps.file("build/extracted/shadow"), "builtBy" to ":testing:proto-shaded-for-testing:extractShadowJar")
output.dir(
protoShadedDeps.file("build/extracted/shadow"),
"builtBy" to ":testing:proto-shaded-for-testing:extractShadowJar"
)
}
}

Expand Down Expand Up @@ -62,6 +68,7 @@ dependencies {
implementation("org.slf4j:jcl-over-slf4j")
implementation("org.slf4j:jul-to-slf4j")
implementation("io.opentelemetry:opentelemetry-exporter-logging")
implementation("io.opentelemetry.contrib:opentelemetry-baggage-processor")
api(project(":instrumentation-api-incubator"))

annotationProcessor("com.google.auto.service:auto-service")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator;
import io.opentelemetry.api.logs.LoggerProvider;
import io.opentelemetry.api.metrics.MeterProvider;
import io.opentelemetry.api.trace.TracerBuilder;
import io.opentelemetry.api.trace.TracerProvider;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.contrib.baggage.processor.BaggageSpanProcessor;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.common.CompletableResultCode;
Expand Down Expand Up @@ -73,13 +76,18 @@ public final class LibraryTestRunner extends InstrumentationTestRunner {
.addSpanProcessor(new FlushTrackingSpanProcessor())
.addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create()))
.addSpanProcessor(SimpleSpanProcessor.create(testSpanExporter))
.addSpanProcessor(BaggageSpanProcessor.allowAllBaggageKeys())
.build())
.setMeterProvider(SdkMeterProvider.builder().registerMetricReader(metricReader).build())
.setLoggerProvider(
SdkLoggerProvider.builder()
.addLogRecordProcessor(SimpleLogRecordProcessor.create(testLogRecordExporter))
.build())
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
.setPropagators(
ContextPropagators.create(
TextMapPropagator.composite(
W3CTraceContextPropagator.getInstance(),
W3CBaggagePropagator.getInstance())))
.buildAndRegisterGlobal();
openTelemetry = wrap(openTelemetrySdk);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package io.opentelemetry.instrumentation.testing.junit;

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

import io.opentelemetry.api.OpenTelemetry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,49 @@ void requestWithNonStandardHttpMethod() throws InterruptedException {
}
}

@Test
void extractSingleBaggage() {
String method = "GET";
AggregatedHttpRequest request =
AggregatedHttpRequest.of(
request(SUCCESS, method).headers().toBuilder()
.set("baggage", "test-baggage-key-1=test-baggage-value-1")
xiepuhuan marked this conversation as resolved.
Show resolved Hide resolved
.build());
client.execute(request).aggregate().join();
xiepuhuan marked this conversation as resolved.
Show resolved Hide resolved

testing.waitAndAssertTraces(
trace ->
trace.anySatisfy(
laurit marked this conversation as resolved.
Show resolved Hide resolved
span ->
assertServerSpan(assertThat(span), method, SUCCESS, SUCCESS.status)
.hasAttribute(
AttributeKey.stringKey("test-baggage-key-1"), "test-baggage-value-1")));
}

@Test
void extractMultiBaggage() {
assumeTrue(options.testExtractMultiBaggage);

String method = "GET";
AggregatedHttpRequest request =
AggregatedHttpRequest.of(
request(SUCCESS, method).headers().toBuilder()
.add("baggage", "test-baggage-key-1=test-baggage-value-1")
.add("baggage", "test-baggage-key-2=test-baggage-value-2")
.build());
client.execute(request).aggregate().join();

testing.waitAndAssertTraces(
trace ->
trace.anySatisfy(
span ->
assertServerSpan(assertThat(span), method, SUCCESS, SUCCESS.status)
.hasAttribute(
AttributeKey.stringKey("test-baggage-key-1"), "test-baggage-value-1")
.hasAttribute(
AttributeKey.stringKey("test-baggage-key-2"), "test-baggage-value-2")));
}

private static Bootstrap buildBootstrap(EventLoopGroup eventLoopGroup) {
Bootstrap bootstrap = new Bootstrap();
bootstrap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public final class HttpServerTestOptions {
boolean testNonStandardHttpMethod = true;
boolean verifyServerSpanEndTime = true;
boolean useHttp2 = false;
boolean testExtractMultiBaggage = false;
xiepuhuan marked this conversation as resolved.
Show resolved Hide resolved

HttpServerTestOptions() {}

Expand Down
Loading