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

Allow library instrumentations to override span name #11355

Merged
merged 2 commits into from
May 17, 2024
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 @@ -20,6 +20,7 @@
import io.opentelemetry.semconv.incubating.PeerIncubatingAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.apache.dubbo.rpc.Result;

Expand All @@ -32,6 +33,12 @@ public final class DubboTelemetryBuilder {
@Nullable private String peerService;
private final List<AttributesExtractor<DubboRequest, Result>> attributesExtractors =
new ArrayList<>();
private Function<
SpanNameExtractor<DubboRequest>, ? extends SpanNameExtractor<? super DubboRequest>>
clientSpanNameExtractorTransformer = Function.identity();
private Function<
SpanNameExtractor<DubboRequest>, ? extends SpanNameExtractor<? super DubboRequest>>
serverSpanNameExtractorTransformer = Function.identity();

DubboTelemetryBuilder(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
Expand All @@ -53,28 +60,50 @@ public DubboTelemetryBuilder addAttributesExtractor(
return this;
}

/** Sets custom client {@link SpanNameExtractor} via transform function. */
@CanIgnoreReturnValue
public DubboTelemetryBuilder setClientSpanNameExtractor(
Function<SpanNameExtractor<DubboRequest>, ? extends SpanNameExtractor<? super DubboRequest>>
clientSpanNameExtractor) {
this.clientSpanNameExtractorTransformer = clientSpanNameExtractor;
return this;
}

/** Sets custom server {@link SpanNameExtractor} via transform function. */
@CanIgnoreReturnValue
public DubboTelemetryBuilder setServerSpanNameExtractor(
Function<SpanNameExtractor<DubboRequest>, ? extends SpanNameExtractor<? super DubboRequest>>
serverSpanNameExtractor) {
this.serverSpanNameExtractorTransformer = serverSpanNameExtractor;
return this;
}

/**
* Returns a new {@link DubboTelemetry} with the settings of this {@link DubboTelemetryBuilder}.
*/
public DubboTelemetry build() {
DubboRpcAttributesGetter rpcAttributesGetter = DubboRpcAttributesGetter.INSTANCE;
SpanNameExtractor<DubboRequest> spanNameExtractor =
RpcSpanNameExtractor.create(rpcAttributesGetter);
SpanNameExtractor<? super DubboRequest> clientSpanNameExtractor =
clientSpanNameExtractorTransformer.apply(spanNameExtractor);
SpanNameExtractor<? super DubboRequest> serverSpanNameExtractor =
serverSpanNameExtractorTransformer.apply(spanNameExtractor);
DubboClientNetworkAttributesGetter netClientAttributesGetter =
new DubboClientNetworkAttributesGetter();
DubboNetworkServerAttributesGetter netServerAttributesGetter =
new DubboNetworkServerAttributesGetter();

InstrumenterBuilder<DubboRequest, Result> serverInstrumenterBuilder =
Instrumenter.<DubboRequest, Result>builder(
openTelemetry, INSTRUMENTATION_NAME, spanNameExtractor)
openTelemetry, INSTRUMENTATION_NAME, serverSpanNameExtractor)
.addAttributesExtractor(RpcServerAttributesExtractor.create(rpcAttributesGetter))
.addAttributesExtractor(NetworkAttributesExtractor.create(netServerAttributesGetter))
.addAttributesExtractors(attributesExtractors);

InstrumenterBuilder<DubboRequest, Result> clientInstrumenterBuilder =
Instrumenter.<DubboRequest, Result>builder(
openTelemetry, INSTRUMENTATION_NAME, spanNameExtractor)
openTelemetry, INSTRUMENTATION_NAME, clientSpanNameExtractor)
.addAttributesExtractor(RpcClientAttributesExtractor.create(rpcAttributesGetter))
.addAttributesExtractor(ServerAttributesExtractor.create(netClientAttributesGetter))
.addAttributesExtractor(NetworkAttributesExtractor.create(netClientAttributesGetter))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractorBuilder;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientMetrics;
Expand All @@ -22,6 +23,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import org.apache.http.HttpResponse;

/** A builder for {@link ApacheHttpClientTelemetry}. */
Expand All @@ -38,6 +40,10 @@ public final class ApacheHttpClientTelemetryBuilder {
HttpClientAttributesExtractor.builder(ApacheHttpClientHttpAttributesGetter.INSTANCE);
private final HttpSpanNameExtractorBuilder<ApacheHttpClientRequest> httpSpanNameExtractorBuilder =
HttpSpanNameExtractor.builder(ApacheHttpClientHttpAttributesGetter.INSTANCE);
private Function<
SpanNameExtractor<ApacheHttpClientRequest>,
? extends SpanNameExtractor<? super ApacheHttpClientRequest>>
spanNameExtractorTransformer = Function.identity();
private boolean emitExperimentalHttpClientMetrics = false;

ApacheHttpClientTelemetryBuilder(OpenTelemetry openTelemetry) {
Expand Down Expand Up @@ -111,17 +117,30 @@ public ApacheHttpClientTelemetryBuilder setEmitExperimentalHttpClientMetrics(
return this;
}

/** Sets custom {@link SpanNameExtractor} via transform function. */
@CanIgnoreReturnValue
public ApacheHttpClientTelemetryBuilder setSpanNameExtractor(
Function<
SpanNameExtractor<ApacheHttpClientRequest>,
? extends SpanNameExtractor<? super ApacheHttpClientRequest>>
spanNameExtractorTransformer) {
this.spanNameExtractorTransformer = spanNameExtractorTransformer;
return this;
}

/**
* Returns a new {@link ApacheHttpClientTelemetry} configured with this {@link
* ApacheHttpClientTelemetryBuilder}.
*/
public ApacheHttpClientTelemetry build() {
ApacheHttpClientHttpAttributesGetter httpAttributesGetter =
ApacheHttpClientHttpAttributesGetter.INSTANCE;
SpanNameExtractor<? super ApacheHttpClientRequest> spanNameExtractor =
spanNameExtractorTransformer.apply(httpSpanNameExtractorBuilder.build());

InstrumenterBuilder<ApacheHttpClientRequest, HttpResponse> builder =
Instrumenter.<ApacheHttpClientRequest, HttpResponse>builder(
openTelemetry, INSTRUMENTATION_NAME, httpSpanNameExtractorBuilder.build())
openTelemetry, INSTRUMENTATION_NAME, spanNameExtractor)
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(httpAttributesExtractorBuilder.build())
.addAttributesExtractors(additionalExtractors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractorBuilder;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientMetrics;
Expand All @@ -22,6 +23,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import org.apache.hc.core5.http.HttpResponse;

/** A builder for {@link ApacheHttpClient5Telemetry}. */
Expand All @@ -40,7 +42,10 @@ public final class ApacheHttpClient5TelemetryBuilder {
private final HttpSpanNameExtractorBuilder<ApacheHttpClient5Request>
httpSpanNameExtractorBuilder =
HttpSpanNameExtractor.builder(ApacheHttpClient5HttpAttributesGetter.INSTANCE);

private Function<
SpanNameExtractor<ApacheHttpClient5Request>,
? extends SpanNameExtractor<? super ApacheHttpClient5Request>>
spanNameExtractorTransformer = Function.identity();
private boolean emitExperimentalHttpClientMetrics = false;

ApacheHttpClient5TelemetryBuilder(OpenTelemetry openTelemetry) {
Expand Down Expand Up @@ -115,17 +120,30 @@ public ApacheHttpClient5TelemetryBuilder setEmitExperimentalHttpClientMetrics(
return this;
}

/** Sets custom {@link SpanNameExtractor} via transform function. */
@CanIgnoreReturnValue
public ApacheHttpClient5TelemetryBuilder setSpanNameExtractor(
Function<
SpanNameExtractor<ApacheHttpClient5Request>,
? extends SpanNameExtractor<? super ApacheHttpClient5Request>>
spanNameExtractorTransformer) {
this.spanNameExtractorTransformer = spanNameExtractorTransformer;
return this;
}

/**
* Returns a new {@link ApacheHttpClient5Telemetry} configured with this {@link
* ApacheHttpClient5TelemetryBuilder}.
*/
public ApacheHttpClient5Telemetry build() {
ApacheHttpClient5HttpAttributesGetter httpAttributesGetter =
ApacheHttpClient5HttpAttributesGetter.INSTANCE;
SpanNameExtractor<? super ApacheHttpClient5Request> spanNameExtractor =
spanNameExtractorTransformer.apply(httpSpanNameExtractorBuilder.build());

InstrumenterBuilder<ApacheHttpClient5Request, HttpResponse> builder =
Instrumenter.<ApacheHttpClient5Request, HttpResponse>builder(
openTelemetry, INSTRUMENTATION_NAME, httpSpanNameExtractorBuilder.build())
openTelemetry, INSTRUMENTATION_NAME, spanNameExtractor)
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(httpAttributesExtractorBuilder.build())
.addAttributesExtractors(additionalExtractors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractorBuilder;
Expand Down Expand Up @@ -63,6 +64,12 @@ public final class ArmeriaTelemetryBuilder {
HttpSpanNameExtractor.builder(ArmeriaHttpClientAttributesGetter.INSTANCE);
private final HttpSpanNameExtractorBuilder<RequestContext> httpServerSpanNameExtractorBuilder =
HttpSpanNameExtractor.builder(ArmeriaHttpServerAttributesGetter.INSTANCE);
private Function<
SpanNameExtractor<RequestContext>, ? extends SpanNameExtractor<? super RequestContext>>
clientSpanNameExtractorTransformer = Function.identity();
private Function<
SpanNameExtractor<RequestContext>, ? extends SpanNameExtractor<? super RequestContext>>
serverSpanNameExtractorTransformer = Function.identity();

private final HttpServerRouteBuilder<RequestContext> httpServerRouteBuilder =
HttpServerRoute.builder(ArmeriaHttpServerAttributesGetter.INSTANCE);
Expand Down Expand Up @@ -210,18 +217,43 @@ public ArmeriaTelemetryBuilder setEmitExperimentalHttpServerMetrics(
return this;
}

/** Sets custom client {@link SpanNameExtractor} via transform function. */
@CanIgnoreReturnValue
public ArmeriaTelemetryBuilder setClientSpanNameExtractor(
Function<
SpanNameExtractor<RequestContext>,
? extends SpanNameExtractor<? super RequestContext>>
clientSpanNameExtractor) {
this.clientSpanNameExtractorTransformer = clientSpanNameExtractor;
return this;
}

/** Sets custom server {@link SpanNameExtractor} via transform function. */
@CanIgnoreReturnValue
public ArmeriaTelemetryBuilder setServerSpanNameExtractor(
Function<
SpanNameExtractor<RequestContext>,
? extends SpanNameExtractor<? super RequestContext>>
serverSpanNameExtractor) {
this.serverSpanNameExtractorTransformer = serverSpanNameExtractor;
return this;
}

public ArmeriaTelemetry build() {
ArmeriaHttpClientAttributesGetter clientAttributesGetter =
ArmeriaHttpClientAttributesGetter.INSTANCE;
ArmeriaHttpServerAttributesGetter serverAttributesGetter =
ArmeriaHttpServerAttributesGetter.INSTANCE;

SpanNameExtractor<? super RequestContext> clientSpanNameExtractor =
clientSpanNameExtractorTransformer.apply(httpClientSpanNameExtractorBuilder.build());
SpanNameExtractor<? super RequestContext> serverSpanNameExtractor =
serverSpanNameExtractorTransformer.apply(httpServerSpanNameExtractorBuilder.build());

InstrumenterBuilder<ClientRequestContext, RequestLog> clientInstrumenterBuilder =
Instrumenter.builder(
openTelemetry, INSTRUMENTATION_NAME, httpClientSpanNameExtractorBuilder.build());
Instrumenter.builder(openTelemetry, INSTRUMENTATION_NAME, clientSpanNameExtractor);
InstrumenterBuilder<ServiceRequestContext, RequestLog> serverInstrumenterBuilder =
Instrumenter.builder(
openTelemetry, INSTRUMENTATION_NAME, httpServerSpanNameExtractorBuilder.build());
Instrumenter.builder(openTelemetry, INSTRUMENTATION_NAME, serverSpanNameExtractor);

Stream.of(clientInstrumenterBuilder, serverInstrumenterBuilder)
.forEach(instrumenter -> instrumenter.addAttributesExtractors(additionalExtractors));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.internal.HttpConstants;
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractorBuilder;
import io.opentelemetry.instrumentation.elasticsearch.rest.internal.ElasticsearchRestInstrumenterFactory;
Expand All @@ -17,6 +18,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import org.elasticsearch.client.Response;

public final class ElasticsearchRest7TelemetryBuilder {
Expand All @@ -27,6 +29,10 @@ public final class ElasticsearchRest7TelemetryBuilder {
private final List<AttributesExtractor<ElasticsearchRestRequest, Response>> attributesExtractors =
new ArrayList<>();
private Set<String> knownMethods = HttpConstants.KNOWN_METHODS;
private Function<
SpanNameExtractor<ElasticsearchRestRequest>,
? extends SpanNameExtractor<? super ElasticsearchRestRequest>>
spanNameExtractorTransformer = Function.identity();

ElasticsearchRest7TelemetryBuilder(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
Expand Down Expand Up @@ -62,14 +68,30 @@ public ElasticsearchRest7TelemetryBuilder setKnownMethods(Set<String> knownMetho
return this;
}

/** Sets custom {@link SpanNameExtractor} via transform function. */
@CanIgnoreReturnValue
public ElasticsearchRest7TelemetryBuilder setSpanNameExtractor(
Function<
SpanNameExtractor<ElasticsearchRestRequest>,
? extends SpanNameExtractor<? super ElasticsearchRestRequest>>
spanNameExtractorTransformer) {
this.spanNameExtractorTransformer = spanNameExtractorTransformer;
return this;
}

/**
* Returns a new {@link ElasticsearchRest7Telemetry} with the settings of this {@link
* ElasticsearchRest7TelemetryBuilder}.
*/
public ElasticsearchRest7Telemetry build() {
Instrumenter<ElasticsearchRestRequest, Response> instrumenter =
ElasticsearchRestInstrumenterFactory.create(
openTelemetry, INSTRUMENTATION_NAME, attributesExtractors, knownMethods, false);
openTelemetry,
INSTRUMENTATION_NAME,
attributesExtractors,
spanNameExtractorTransformer,
knownMethods,
false);

return new ElasticsearchRest7Telemetry(instrumenter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
import java.util.Collections;
import java.util.function.Function;
import org.elasticsearch.client.Response;

public final class ElasticsearchRestJavaagentInstrumenterFactory {
Expand All @@ -28,6 +29,7 @@ public static Instrumenter<ElasticsearchRestRequest, Response> create(
GlobalOpenTelemetry.get(),
instrumentationName,
Collections.emptyList(),
Function.identity(),
CommonConfig.get().getKnownHttpRequestMethods(),
CAPTURE_SEARCH_QUERY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import org.elasticsearch.client.Response;

/**
Expand All @@ -26,17 +28,22 @@ public static Instrumenter<ElasticsearchRestRequest, Response> create(
OpenTelemetry openTelemetry,
String instrumentationName,
List<AttributesExtractor<ElasticsearchRestRequest, Response>> attributesExtractors,
Function<
SpanNameExtractor<ElasticsearchRestRequest>,
? extends SpanNameExtractor<? super ElasticsearchRestRequest>>
spanNameExtractorTransformer,
Set<String> knownMethods,
boolean captureSearchQuery) {
ElasticsearchDbAttributesGetter dbClientAttributesGetter =
new ElasticsearchDbAttributesGetter(captureSearchQuery);
ElasticsearchClientAttributeExtractor esClientAtrributesExtractor =
new ElasticsearchClientAttributeExtractor(knownMethods);
ElasticsearchSpanNameExtractor nameExtractor =
new ElasticsearchSpanNameExtractor(dbClientAttributesGetter);
SpanNameExtractor<? super ElasticsearchRestRequest> spanNameExtractor =
spanNameExtractorTransformer.apply(
new ElasticsearchSpanNameExtractor(dbClientAttributesGetter));

return Instrumenter.<ElasticsearchRestRequest, Response>builder(
openTelemetry, instrumentationName, nameExtractor)
openTelemetry, instrumentationName, spanNameExtractor)
.addAttributesExtractor(DbClientAttributesExtractor.create(dbClientAttributesGetter))
.addAttributesExtractor(esClientAtrributesExtractor)
.addAttributesExtractors(attributesExtractors)
Expand Down
Loading
Loading