Skip to content

Commit

Permalink
Allow overriding span name in spring web library instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit committed Jul 21, 2023
1 parent d17876e commit ddc7ca5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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.http.HttpClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractorBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientExperimentalMetrics;
Expand All @@ -19,6 +20,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpResponse;

Expand All @@ -35,6 +38,10 @@ public final class SpringWebTelemetryBuilder {
SpringWebHttpAttributesGetter.INSTANCE, new SpringWebNetAttributesGetter());
private boolean emitExperimentalHttpClientMetrics = false;

@Nullable
private Function<SpanNameExtractor<HttpRequest>, ? extends SpanNameExtractor<? super HttpRequest>>
spanNameExtractorTransformer;

SpringWebTelemetryBuilder(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
}
Expand Down Expand Up @@ -72,6 +79,15 @@ public SpringWebTelemetryBuilder setCapturedResponseHeaders(List<String> respons
return this;
}

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

/**
* Configures the instrumentation to recognize an alternative set of HTTP request methods.
*
Expand Down Expand Up @@ -111,11 +127,16 @@ public SpringWebTelemetryBuilder setEmitExperimentalHttpClientMetrics(
public SpringWebTelemetry build() {
SpringWebHttpAttributesGetter httpAttributeGetter = SpringWebHttpAttributesGetter.INSTANCE;

SpanNameExtractor<HttpRequest> originalSpanNameExtractor =
HttpSpanNameExtractor.create(httpAttributeGetter);
SpanNameExtractor<? super HttpRequest> spanNameExtractor = originalSpanNameExtractor;
if (spanNameExtractorTransformer != null) {
spanNameExtractor = spanNameExtractorTransformer.apply(originalSpanNameExtractor);
}

InstrumenterBuilder<HttpRequest, ClientHttpResponse> builder =
Instrumenter.<HttpRequest, ClientHttpResponse>builder(
openTelemetry,
INSTRUMENTATION_NAME,
HttpSpanNameExtractor.create(httpAttributeGetter))
openTelemetry, INSTRUMENTATION_NAME, spanNameExtractor)
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributeGetter))
.addAttributesExtractor(httpAttributesExtractorBuilder.build())
.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.instrumenter.http.HttpRouteHolder;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractorBuilder;
Expand All @@ -18,6 +19,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Expand All @@ -34,6 +37,12 @@ public final class SpringWebMvcTelemetryBuilder {
HttpServerAttributesExtractor.builder(
SpringWebMvcHttpAttributesGetter.INSTANCE, SpringWebMvcNetAttributesGetter.INSTANCE);

@Nullable
private Function<
SpanNameExtractor<HttpServletRequest>,
? extends SpanNameExtractor<? super HttpServletRequest>>
spanNameExtractorTransformer;

SpringWebMvcTelemetryBuilder(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
}
Expand Down Expand Up @@ -71,6 +80,17 @@ public SpringWebMvcTelemetryBuilder setCapturedResponseHeaders(List<String> resp
return this;
}

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

/**
* Configures the instrumentation to recognize an alternative set of HTTP request methods.
*
Expand Down Expand Up @@ -98,11 +118,16 @@ public SpringWebMvcTelemetry build() {
SpringWebMvcHttpAttributesGetter httpAttributesGetter =
SpringWebMvcHttpAttributesGetter.INSTANCE;

SpanNameExtractor<HttpServletRequest> originalSpanNameExtractor =
HttpSpanNameExtractor.create(httpAttributesGetter);
SpanNameExtractor<? super HttpServletRequest> spanNameExtractor = originalSpanNameExtractor;
if (spanNameExtractorTransformer != null) {
spanNameExtractor = spanNameExtractorTransformer.apply(originalSpanNameExtractor);
}

Instrumenter<HttpServletRequest, HttpServletResponse> instrumenter =
Instrumenter.<HttpServletRequest, HttpServletResponse>builder(
openTelemetry,
INSTRUMENTATION_NAME,
HttpSpanNameExtractor.create(httpAttributesGetter))
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 @@ -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.instrumenter.http.HttpRouteHolder;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractorBuilder;
Expand All @@ -20,6 +21,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.Nullable;

/** A builder of {@link SpringWebMvcTelemetry}. */
public final class SpringWebMvcTelemetryBuilder {
Expand All @@ -34,6 +37,12 @@ public final class SpringWebMvcTelemetryBuilder {
HttpServerAttributesExtractor.builder(
SpringWebMvcHttpAttributesGetter.INSTANCE, SpringWebMvcNetAttributesGetter.INSTANCE);

@Nullable
private Function<
SpanNameExtractor<HttpServletRequest>,
? extends SpanNameExtractor<? super HttpServletRequest>>
spanNameExtractorTransformer;

SpringWebMvcTelemetryBuilder(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
}
Expand Down Expand Up @@ -71,6 +80,17 @@ public SpringWebMvcTelemetryBuilder setCapturedResponseHeaders(List<String> resp
return this;
}

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

/**
* Configures the instrumentation to recognize an alternative set of HTTP request methods.
*
Expand Down Expand Up @@ -98,11 +118,16 @@ public SpringWebMvcTelemetry build() {
SpringWebMvcHttpAttributesGetter httpAttributesGetter =
SpringWebMvcHttpAttributesGetter.INSTANCE;

SpanNameExtractor<HttpServletRequest> originalSpanNameExtractor =
HttpSpanNameExtractor.create(httpAttributesGetter);
SpanNameExtractor<? super HttpServletRequest> spanNameExtractor = originalSpanNameExtractor;
if (spanNameExtractorTransformer != null) {
spanNameExtractor = spanNameExtractorTransformer.apply(originalSpanNameExtractor);
}

Instrumenter<HttpServletRequest, HttpServletResponse> instrumenter =
Instrumenter.<HttpServletRequest, HttpServletResponse>builder(
openTelemetry,
INSTRUMENTATION_NAME,
HttpSpanNameExtractor.create(httpAttributesGetter))
openTelemetry, INSTRUMENTATION_NAME, spanNameExtractor)
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter))
.addAttributesExtractor(httpAttributesExtractorBuilder.build())
.addAttributesExtractors(additionalExtractors)
Expand Down

0 comments on commit ddc7ca5

Please sign in to comment.