io.opentelemetry.instrumentation.spring.webmvc.v5_3.SpringWebMvcTelemetryBuilder Maven / Gradle / Ivy
Show all versions of opentelemetry-spring-webmvc-5.3 Show documentation
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.spring.webmvc.v5_3;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpServerInstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractorBuilder;
import io.opentelemetry.instrumentation.spring.webmvc.v5_3.internal.Experimental;
import io.opentelemetry.instrumentation.spring.webmvc.v5_3.internal.SpringMvcBuilderUtil;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** A builder of {@link SpringWebMvcTelemetry}. */
public final class SpringWebMvcTelemetryBuilder {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.spring-webmvc-5.3";
private final DefaultHttpServerInstrumenterBuilder
builder;
static {
SpringMvcBuilderUtil.setBuilderExtractor(builder -> builder.builder);
Experimental.setSetEmitExperimentalTelemetry(
(builder, emit) -> builder.builder.setEmitExperimentalHttpServerMetrics(emit));
}
SpringWebMvcTelemetryBuilder(OpenTelemetry openTelemetry) {
builder =
DefaultHttpServerInstrumenterBuilder.create(
INSTRUMENTATION_NAME,
openTelemetry,
SpringWebMvcHttpAttributesGetter.INSTANCE,
JavaxHttpServletRequestGetter.INSTANCE);
}
/**
* Adds an additional {@link AttributesExtractor} to invoke to set attributes to instrumented
* items.
*/
@CanIgnoreReturnValue
public SpringWebMvcTelemetryBuilder addAttributesExtractor(
AttributesExtractor attributesExtractor) {
builder.addAttributesExtractor(attributesExtractor);
return this;
}
/**
* Configures the HTTP request headers that will be captured as span attributes.
*
* @param requestHeaders A list of HTTP header names.
*/
@CanIgnoreReturnValue
public SpringWebMvcTelemetryBuilder setCapturedRequestHeaders(List requestHeaders) {
builder.setCapturedRequestHeaders(requestHeaders);
return this;
}
/**
* Configures the HTTP response headers that will be captured as span attributes.
*
* @param responseHeaders A list of HTTP header names.
*/
@CanIgnoreReturnValue
public SpringWebMvcTelemetryBuilder setCapturedResponseHeaders(List responseHeaders) {
builder.setCapturedResponseHeaders(responseHeaders);
return this;
}
/** Sets custom {@link SpanNameExtractor} via transform function. */
@CanIgnoreReturnValue
public SpringWebMvcTelemetryBuilder setSpanNameExtractor(
Function<
SpanNameExtractor super HttpServletRequest>,
? extends SpanNameExtractor super HttpServletRequest>>
spanNameExtractor) {
builder.setSpanNameExtractor(spanNameExtractor);
return this;
}
/**
* Configures the instrumentation to recognize an alternative set of HTTP request methods.
*
* By default, this instrumentation defines "known" methods as the ones listed in RFC9110 and the PATCH
* method defined in RFC5789.
*
*
Note: calling this method overrides the default known method sets completely; it does
* not supplement it.
*
* @param knownMethods A set of recognized HTTP request methods.
* @see HttpServerAttributesExtractorBuilder#setKnownMethods(Set)
*/
@CanIgnoreReturnValue
public SpringWebMvcTelemetryBuilder setKnownMethods(Set knownMethods) {
builder.setKnownMethods(knownMethods);
return this;
}
/**
* Configures the instrumentation to emit experimental HTTP server metrics.
*
* @param emitExperimentalHttpServerMetrics {@code true} if the experimental HTTP server metrics
* are to be emitted.
* @deprecated Use {@link Experimental#setEmitExperimentalTelemetry(SpringWebMvcTelemetryBuilder,
* boolean)} instead.
*/
@Deprecated
@CanIgnoreReturnValue
public SpringWebMvcTelemetryBuilder setEmitExperimentalHttpServerMetrics(
boolean emitExperimentalHttpServerMetrics) {
builder.setEmitExperimentalHttpServerMetrics(emitExperimentalHttpServerMetrics);
return this;
}
/**
* Returns a new {@link SpringWebMvcTelemetry} with the settings of this {@link
* SpringWebMvcTelemetryBuilder}.
*/
public SpringWebMvcTelemetry build() {
return new SpringWebMvcTelemetry(builder.build());
}
}