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

Http server builder #11651

Merged
merged 14 commits into from
Oct 7, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanStatusExtractor;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -64,15 +65,24 @@ public final class DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> {
private boolean emitExperimentalHttpClientMetrics = false;
private Consumer<InstrumenterBuilder<REQUEST, RESPONSE>> builderCustomizer = b -> {};

public DefaultHttpClientInstrumenterBuilder(
String instrumentationName,
OpenTelemetry openTelemetry,
HttpClientAttributesGetter<REQUEST, RESPONSE> attributesGetter,
TextMapSetter<REQUEST> headerSetter) {
this(instrumentationName, openTelemetry, attributesGetter);
this.headerSetter = Objects.requireNonNull(headerSetter, "headerSetter");
}

public DefaultHttpClientInstrumenterBuilder(
String instrumentationName,
OpenTelemetry openTelemetry,
HttpClientAttributesGetter<REQUEST, RESPONSE> attributesGetter) {
this.instrumentationName = instrumentationName;
this.openTelemetry = openTelemetry;
this.instrumentationName = Objects.requireNonNull(instrumentationName, "instrumentationName");
this.openTelemetry = Objects.requireNonNull(openTelemetry, "openTelemetry");
this.attributesGetter = Objects.requireNonNull(attributesGetter, "attributesGetter");
httpSpanNameExtractorBuilder = HttpSpanNameExtractor.builder(attributesGetter);
httpAttributesExtractorBuilder = HttpClientAttributesExtractor.builder(attributesGetter);
this.attributesGetter = attributesGetter;
}

/**
Expand Down Expand Up @@ -141,13 +151,6 @@ public DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> setKnownMethods(
return this;
}

@CanIgnoreReturnValue
public DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> setHeaderSetter(
@Nullable TextMapSetter<REQUEST> headerSetter) {
this.headerSetter = headerSetter;
return this;
}

/**
* Configures the instrumentation to emit experimental HTTP client metrics.
*
Expand Down Expand Up @@ -193,7 +196,6 @@ public DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE> setBuilderCustomi
}

public Instrumenter<REQUEST, RESPONSE> build() {

SpanNameExtractor<? super REQUEST> spanNameExtractor =
spanNameExtractorTransformer.apply(httpSpanNameExtractorBuilder.build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanStatusExtractor;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -53,7 +54,7 @@ public final class DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> {
httpAttributesExtractorBuilder;
private final HttpSpanNameExtractorBuilder<REQUEST> httpSpanNameExtractorBuilder;

@Nullable private final TextMapGetter<REQUEST> headerGetter;
@Nullable private TextMapGetter<REQUEST> headerGetter;
zeitlinger marked this conversation as resolved.
Show resolved Hide resolved
private Function<SpanNameExtractor<? super REQUEST>, ? extends SpanNameExtractor<? super REQUEST>>
spanNameExtractorTransformer = Function.identity();
private final HttpServerRouteBuilder<REQUEST> httpServerRouteBuilder;
Expand All @@ -65,14 +66,21 @@ public DefaultHttpServerInstrumenterBuilder(
String instrumentationName,
OpenTelemetry openTelemetry,
HttpServerAttributesGetter<REQUEST, RESPONSE> attributesGetter,
@Nullable TextMapGetter<REQUEST> headerGetter) {
this.instrumentationName = instrumentationName;
this.openTelemetry = openTelemetry;
TextMapGetter<REQUEST> headerGetter) {
this(instrumentationName, openTelemetry, attributesGetter);
this.headerGetter = Objects.requireNonNull(headerGetter, "headerGetter");
}

public DefaultHttpServerInstrumenterBuilder(
String instrumentationName,
OpenTelemetry openTelemetry,
HttpServerAttributesGetter<REQUEST, RESPONSE> attributesGetter) {
this.instrumentationName = Objects.requireNonNull(instrumentationName, "instrumentationName");
this.openTelemetry = Objects.requireNonNull(openTelemetry, "openTelemetry");
this.attributesGetter = Objects.requireNonNull(attributesGetter, "attributesGetter");
httpAttributesExtractorBuilder = HttpServerAttributesExtractor.builder(attributesGetter);
httpSpanNameExtractorBuilder = HttpSpanNameExtractor.builder(attributesGetter);
httpServerRouteBuilder = HttpServerRoute.builder(attributesGetter);
this.attributesGetter = attributesGetter;
this.headerGetter = headerGetter;
}

/**
Expand Down Expand Up @@ -172,6 +180,15 @@ public DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> setBuilderCustomi
}

public Instrumenter<REQUEST, RESPONSE> build() {
InstrumenterBuilder<REQUEST, RESPONSE> builder = instrumenterBuilder();

if (headerGetter != null) {
return builder.buildServerInstrumenter(headerGetter);
}
return builder.buildInstrumenter(SpanKindExtractor.alwaysServer());
}

public InstrumenterBuilder<REQUEST, RESPONSE> instrumenterBuilder() {
SpanNameExtractor<? super REQUEST> spanNameExtractor =
spanNameExtractorTransformer.apply(httpSpanNameExtractorBuilder.build());

Expand All @@ -190,11 +207,7 @@ public Instrumenter<REQUEST, RESPONSE> build() {
.addOperationMetrics(HttpServerExperimentalMetrics.get());
}
builderCustomizer.accept(builder);

if (headerGetter != null) {
return builder.buildServerInstrumenter(headerGetter);
}
return builder.buildInstrumenter(SpanKindExtractor.alwaysServer());
return builder;
}

@CanIgnoreReturnValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,20 @@

import akka.http.scaladsl.model.HttpRequest;
import akka.http.scaladsl.model.HttpResponse;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpExperimentalAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpServerExperimentalMetrics;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerMetrics;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute;
import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanNameExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanStatusExtractor;
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.JavaagentHttpServerInstrumenters;
import io.opentelemetry.javaagent.instrumentation.akkahttp.AkkaHttpUtil;

public final class AkkaHttpServerSingletons {

private static final Instrumenter<HttpRequest, HttpResponse> INSTRUMENTER;

static {
AkkaHttpServerAttributesGetter httpAttributesGetter = new AkkaHttpServerAttributesGetter();
InstrumenterBuilder<HttpRequest, HttpResponse> builder =
Instrumenter.<HttpRequest, HttpResponse>builder(
GlobalOpenTelemetry.get(),
AkkaHttpUtil.instrumentationName(),
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(AgentCommonConfig.get().getKnownHttpRequestMethods())
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(
HttpServerAttributesExtractor.builder(httpAttributesGetter)
.setCapturedRequestHeaders(AgentCommonConfig.get().getServerRequestHeaders())
.setCapturedResponseHeaders(AgentCommonConfig.get().getServerResponseHeaders())
.setKnownMethods(AgentCommonConfig.get().getKnownHttpRequestMethods())
.build())
.addOperationMetrics(HttpServerMetrics.get())
.addContextCustomizer(
HttpServerRoute.builder(httpAttributesGetter)
.setKnownMethods(AgentCommonConfig.get().getKnownHttpRequestMethods())
.build());
if (AgentCommonConfig.get().shouldEmitExperimentalHttpServerTelemetry()) {
builder
.addAttributesExtractor(HttpExperimentalAttributesExtractor.create(httpAttributesGetter))
.addOperationMetrics(HttpServerExperimentalMetrics.get());
}
INSTRUMENTER = builder.buildServerInstrumenter(AkkaHttpServerHeaders.INSTANCE);
INSTRUMENTER =
JavaagentHttpServerInstrumenters.create(
AkkaHttpUtil.instrumentationName(),
new AkkaHttpServerAttributesGetter(),
AkkaHttpServerHeaders.INSTANCE);
Comment on lines -28 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, nice

}

public static Instrumenter<HttpRequest, HttpResponse> instrumenter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ private ArmeriaInstrumenterBuilderFactory() {}
public static DefaultHttpClientInstrumenterBuilder<ClientRequestContext, RequestLog>
getClientBuilder(OpenTelemetry openTelemetry) {
return new DefaultHttpClientInstrumenterBuilder<>(
INSTRUMENTATION_NAME, openTelemetry, ArmeriaHttpClientAttributesGetter.INSTANCE)
.setHeaderSetter(ClientRequestContextSetter.INSTANCE);
INSTRUMENTATION_NAME,
openTelemetry,
ArmeriaHttpClientAttributesGetter.INSTANCE,
ClientRequestContextSetter.INSTANCE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,8 @@

package io.opentelemetry.javaagent.instrumentation.grizzly;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpExperimentalAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpServerExperimentalMetrics;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerMetrics;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute;
import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanNameExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanStatusExtractor;
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.JavaagentHttpServerInstrumenters;
import io.opentelemetry.javaagent.bootstrap.servlet.AppServerBridge;
import org.glassfish.grizzly.http.HttpRequestPacket;
import org.glassfish.grizzly.http.HttpResponsePacket;
Expand All @@ -25,43 +16,22 @@ public final class GrizzlySingletons {
private static final Instrumenter<HttpRequestPacket, HttpResponsePacket> INSTRUMENTER;

static {
GrizzlyHttpAttributesGetter httpAttributesGetter = new GrizzlyHttpAttributesGetter();

InstrumenterBuilder<HttpRequestPacket, HttpResponsePacket> builder =
Instrumenter.<HttpRequestPacket, HttpResponsePacket>builder(
GlobalOpenTelemetry.get(),
"io.opentelemetry.grizzly-2.3",
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(AgentCommonConfig.get().getKnownHttpRequestMethods())
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(
HttpServerAttributesExtractor.builder(httpAttributesGetter)
.setCapturedRequestHeaders(AgentCommonConfig.get().getServerRequestHeaders())
.setCapturedResponseHeaders(AgentCommonConfig.get().getServerResponseHeaders())
.setKnownMethods(AgentCommonConfig.get().getKnownHttpRequestMethods())
.build())
.addOperationMetrics(HttpServerMetrics.get());
if (AgentCommonConfig.get().shouldEmitExperimentalHttpServerTelemetry()) {
builder
.addAttributesExtractor(HttpExperimentalAttributesExtractor.create(httpAttributesGetter))
.addOperationMetrics(HttpServerExperimentalMetrics.get());
}
INSTRUMENTER =
builder
.addContextCustomizer(
(context, request, attributes) ->
new AppServerBridge.Builder()
.captureServletAttributes()
.recordException()
.init(context))
.addContextCustomizer(
(context, httpRequestPacket, startAttributes) -> GrizzlyErrorHolder.init(context))
.addContextCustomizer(
HttpServerRoute.builder(httpAttributesGetter)
.setKnownMethods(AgentCommonConfig.get().getKnownHttpRequestMethods())
.build())
.buildServerInstrumenter(HttpRequestHeadersGetter.INSTANCE);
JavaagentHttpServerInstrumenters.create(
"io.opentelemetry.grizzly-2.3",
new GrizzlyHttpAttributesGetter(),
HttpRequestHeadersGetter.INSTANCE,
builder ->
builder
.addContextCustomizer(
(context, request, attributes) ->
new AppServerBridge.Builder()
.captureServletAttributes()
.recordException()
.init(context))
.addContextCustomizer(
(context, httpRequestPacket, startAttributes) ->
GrizzlyErrorHolder.init(context)));
}

public static Instrumenter<HttpRequestPacket, HttpResponsePacket> instrumenter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ private JettyHttpClientInstrumenterBuilderFactory() {}
public static DefaultHttpClientInstrumenterBuilder<Request, Response> create(
OpenTelemetry openTelemetry) {
return new DefaultHttpClientInstrumenterBuilder<>(
INSTRUMENTATION_NAME, openTelemetry, JettyClientHttpAttributesGetter.INSTANCE)
.setHeaderSetter(HttpHeaderSetter.INSTANCE);
INSTRUMENTATION_NAME,
openTelemetry,
JettyClientHttpAttributesGetter.INSTANCE,
HttpHeaderSetter.INSTANCE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ private JettyHttpClientInstrumenterBuilderFactory() {}
public static DefaultHttpClientInstrumenterBuilder<Request, Response> create(
OpenTelemetry openTelemetry) {
return new DefaultHttpClientInstrumenterBuilder<>(
INSTRUMENTATION_NAME, openTelemetry, JettyClientHttpAttributesGetter.INSTANCE)
.setHeaderSetter(HttpHeaderSetter.INSTANCE);
INSTRUMENTATION_NAME,
openTelemetry,
JettyClientHttpAttributesGetter.INSTANCE,
HttpHeaderSetter.INSTANCE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,8 @@

package io.opentelemetry.javaagent.instrumentation.jetty.v12_0;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpExperimentalAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.http.HttpServerExperimentalMetrics;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerMetrics;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute;
import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanNameExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanStatusExtractor;
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.JavaagentHttpServerInstrumenters;
import io.opentelemetry.javaagent.bootstrap.servlet.AppServerBridge;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
Expand All @@ -26,39 +17,18 @@ public final class Jetty12Singletons {
private static final Instrumenter<Request, Response> INSTRUMENTER;

static {
Jetty12HttpAttributesGetter httpAttributesGetter = new Jetty12HttpAttributesGetter();

InstrumenterBuilder<Request, Response> builder =
Instrumenter.<Request, Response>builder(
GlobalOpenTelemetry.get(),
INSTRUMENTATION_NAME,
HttpSpanNameExtractor.builder(httpAttributesGetter)
.setKnownMethods(AgentCommonConfig.get().getKnownHttpRequestMethods())
.build())
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(
HttpServerAttributesExtractor.builder(httpAttributesGetter)
.setCapturedRequestHeaders(AgentCommonConfig.get().getServerRequestHeaders())
.setCapturedResponseHeaders(AgentCommonConfig.get().getServerResponseHeaders())
.setKnownMethods(AgentCommonConfig.get().getKnownHttpRequestMethods())
.build())
.addContextCustomizer(
HttpServerRoute.builder(httpAttributesGetter)
.setKnownMethods(AgentCommonConfig.get().getKnownHttpRequestMethods())
.build())
.addContextCustomizer(
(context, request, attributes) ->
new AppServerBridge.Builder()
.captureServletAttributes()
.recordException()
.init(context))
.addOperationMetrics(HttpServerMetrics.get());
if (AgentCommonConfig.get().shouldEmitExperimentalHttpServerTelemetry()) {
builder
.addAttributesExtractor(HttpExperimentalAttributesExtractor.create(httpAttributesGetter))
.addOperationMetrics(HttpServerExperimentalMetrics.get());
}
INSTRUMENTER = builder.buildServerInstrumenter(Jetty12TextMapGetter.INSTANCE);
INSTRUMENTER =
JavaagentHttpServerInstrumenters.create(
INSTRUMENTATION_NAME,
new Jetty12HttpAttributesGetter(),
Jetty12TextMapGetter.INSTANCE,
builder ->
builder.addContextCustomizer(
(context, request, attributes) ->
new AppServerBridge.Builder()
.captureServletAttributes()
.recordException()
.init(context)));
}

private static final Jetty12Helper HELPER = new Jetty12Helper(INSTRUMENTER);
Expand Down
Loading
Loading