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

Merged
Show file tree
Hide file tree
Changes from all 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=test-baggage-key-1,test-baggage-key-2"
)
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@

package io.opentelemetry.instrumentation.spring.webmvc.v5_3;

import io.opentelemetry.context.propagation.TextMapGetter;
import static java.util.Collections.emptyIterator;

import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;

enum JavaxHttpServletRequestGetter implements TextMapGetter<HttpServletRequest> {
enum JavaxHttpServletRequestGetter implements ExtendedTextMapGetter<HttpServletRequest> {
INSTANCE;

@Override
Expand All @@ -21,4 +25,24 @@ public Iterable<String> keys(HttpServletRequest carrier) {
public String get(HttpServletRequest carrier, String key) {
return carrier.getHeader(key);
}

@Override
public Iterator<String> getAll(HttpServletRequest carrier, String key) {
Enumeration<String> values = carrier.getHeaders(key);
if (values == null) {
return emptyIterator();
}

return new Iterator<String>() {
@Override
public boolean hasNext() {
return values.hasMoreElements();
}

@Override
public String next() {
return values.nextElement();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ protected void configure(HttpServerTestOptions options) {
}
return expectedHttpRoute(endpoint, method);
});

options.setTestExtractMultiBaggage(true);
}

@Test
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 @@ -5,15 +5,20 @@

package io.opentelemetry.instrumentation.testing;

import static java.util.Arrays.asList;

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 +78,22 @@ public final class LibraryTestRunner extends InstrumentationTestRunner {
.addSpanProcessor(new FlushTrackingSpanProcessor())
.addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create()))
.addSpanProcessor(SimpleSpanProcessor.create(testSpanExporter))
.addSpanProcessor(
new BaggageSpanProcessor(
baggageKey ->
asList("test-baggage-key-1", "test-baggage-key-2")
.contains(baggageKey)))
.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,57 @@ void requestWithNonStandardHttpMethod() throws InterruptedException {
}
}

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

assertThat(response.status().code()).isEqualTo(SUCCESS.getStatus());
assertThat(response.contentUtf8()).isEqualTo(SUCCESS.getBody());

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()
// adding baggage header in w3c baggage format
.add("baggage", "test-baggage-key-1=test-baggage-value-1")
.add("baggage", "test-baggage-key-2=test-baggage-value-2")
.build());
AggregatedHttpResponse response = client.execute(request).aggregate().join();

assertThat(response.status().code()).isEqualTo(SUCCESS.getStatus());
assertThat(response.contentUtf8()).isEqualTo(SUCCESS.getBody());

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 Expand Up @@ -230,6 +231,12 @@ public HttpServerTestOptions useHttp2() {
return setUseHttp2(true);
}

@CanIgnoreReturnValue
public HttpServerTestOptions setTestExtractMultiBaggage(boolean testExtractMultiBaggage) {
this.testExtractMultiBaggage = testExtractMultiBaggage;
return this;
}

@FunctionalInterface
public interface SpanNameMapper {

Expand Down
Loading