io.opentelemetry.instrumentation.spring.web.v3_1.SpringWebTelemetry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opentelemetry-spring-web-3.1 Show documentation
Show all versions of opentelemetry-spring-web-3.1 Show documentation
Instrumentation of Java libraries using OpenTelemetry.
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.spring.web.v3_1;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.RestTemplate;
/** Entrypoint for instrumenting Spring {@link org.springframework.web.client.RestTemplate}. */
public final class SpringWebTelemetry {
/** Returns a new {@link SpringWebTelemetry} configured with the given {@link OpenTelemetry}. */
public static SpringWebTelemetry create(OpenTelemetry openTelemetry) {
return builder(openTelemetry).build();
}
/**
* Returns a new {@link SpringWebTelemetryBuilder} configured with the given {@link
* OpenTelemetry}.
*/
public static SpringWebTelemetryBuilder builder(OpenTelemetry openTelemetry) {
return new SpringWebTelemetryBuilder(openTelemetry);
}
private final Instrumenter instrumenter;
SpringWebTelemetry(Instrumenter instrumenter) {
this.instrumenter = instrumenter;
}
/**
* Returns a new {@link ClientHttpRequestInterceptor} that can be used with {@link
* RestTemplate#getInterceptors()}. For example:
*
* {@code
* restTemplate.getInterceptors().add(SpringWebTelemetry.create(openTelemetry).newInterceptor());
* }
*/
public ClientHttpRequestInterceptor newInterceptor() {
return new RestTemplateInterceptor(instrumenter);
}
}