diff --git a/content/en/docs/zero-code/java/spring-boot-starter/annotations.md b/content/en/docs/zero-code/java/spring-boot-starter/annotations.md index 3d3b9ce0b518..bb1b90110718 100644 --- a/content/en/docs/zero-code/java/spring-boot-starter/annotations.md +++ b/content/en/docs/zero-code/java/spring-boot-starter/annotations.md @@ -3,6 +3,9 @@ title: Annotations weight: 60 --- + + + For most users, the out-of-the-box instrumentation is completely sufficient and nothing more has to be done. Sometimes, however, users wish to create [spans](/docs/concepts/signals/traces/#spans) for their own custom code without @@ -22,36 +25,36 @@ span by annotating the method parameters with `@SpanAttribute`. | ----------- | ------------------------------------------ | ------------- | --------------------------------- | | `@WithSpan` | `otel.instrumentation.annotations.enabled` | true | Enables the WithSpan annotations. | + + ```java -import org.springframework.stereotype.Component; +package otel; -import io.opentelemetry.instrumentation.annotations.SpanAttribute; -import io.opentelemetry.instrumentation.annotations.WithSpan; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.instrumentation.annotations.SpanAttribute; +import io.opentelemetry.instrumentation.annotations.WithSpan; +import org.springframework.stereotype.Component; -/** - * Test WithSpan - */ +/** Test WithSpan */ @Component public class TracedClass { - @WithSpan - public void tracedMethod() { - } + @WithSpan + public void tracedMethod() {} - @WithSpan(value="span name") - public void tracedMethodWithName() { - Span currentSpan = Span.current(); - currentSpan.addEvent("ADD EVENT TO tracedMethodWithName SPAN"); - currentSpan.setAttribute("isTestAttribute", true); - } + @WithSpan(value = "span name") + public void tracedMethodWithName() { + Span currentSpan = Span.current(); + currentSpan.addEvent("ADD EVENT TO tracedMethodWithName SPAN"); + currentSpan.setAttribute("isTestAttribute", true); + } - @WithSpan(kind = SpanKind.CLIENT) - public void tracedClientSpan() { - } + @WithSpan(kind = SpanKind.CLIENT) + public void tracedClientSpan() {} - public void tracedMethodWithAttribute(@SpanAttribute("attributeName") String parameter) { - } + public void tracedMethodWithAttribute(@SpanAttribute("attributeName") String parameter) {} } ``` + + diff --git a/content/en/docs/zero-code/java/spring-boot-starter/other-spring-autoconfig.md b/content/en/docs/zero-code/java/spring-boot-starter/other-spring-autoconfig.md index ed2676c4b95b..d6baa69e0a3d 100644 --- a/content/en/docs/zero-code/java/spring-boot-starter/other-spring-autoconfig.md +++ b/content/en/docs/zero-code/java/spring-boot-starter/other-spring-autoconfig.md @@ -4,6 +4,9 @@ cSpell:ignore: autoconfigurations weight: 70 --- + + + Instead of using the OpenTelemetry Spring starter, you can use the OpenTelemetry autoconfiguration features with an annotation or the Zipkin starter. @@ -13,7 +16,11 @@ Autoconfiguration is natively supported by Spring Boot applications. To enable these features in "vanilla" use `@EnableOpenTelemetry` to complete a component scan of this package. + + ```java +package otel; + import io.opentelemetry.instrumentation.spring.autoconfigure.EnableOpenTelemetry; import org.springframework.context.annotation.Configuration; @@ -21,6 +28,7 @@ import org.springframework.context.annotation.Configuration; @EnableOpenTelemetry public class OpenTelemetryConfig {} ``` + ## Zipkin starter diff --git a/content/en/docs/zero-code/java/spring-boot-starter/out-of-the-box-instrumentation.md b/content/en/docs/zero-code/java/spring-boot-starter/out-of-the-box-instrumentation.md index 1c560731ba60..8b2fda360521 100644 --- a/content/en/docs/zero-code/java/spring-boot-starter/out-of-the-box-instrumentation.md +++ b/content/en/docs/zero-code/java/spring-boot-starter/out-of-the-box-instrumentation.md @@ -4,6 +4,9 @@ weight: 40 cSpell:ignore: autoconfigurations autoconfigures logback webflux webmvc --- + + + Out of the box instrumentation is available for several frameworks: | Feature | Property | Default Value | @@ -90,33 +93,84 @@ supported for spring web versions 3.1+. To learn more about the OpenTelemetry The following ways of creating a `RestTemplate` are supported: + + ```java -@Bean -public RestTemplate restTemplate() { +package otel; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class RestTemplateConfig { + + @Bean + public RestTemplate restTemplate() { return new RestTemplate(); + } } ``` + ```java -public MyService(RestTemplateBuilder restTemplateBuilder) { - this.restTemplate = restTemplateBuilder.build(); +package otel; + +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +@RestController +public class RestTemplateController { + + private final RestTemplate restTemplate; + + public RestTemplateController(RestTemplateBuilder restTemplateBuilder) { + restTemplate = restTemplateBuilder.rootUri("http://localhost:8080").build(); + } } ``` + The following ways of creating a `RestClient` are supported: + + ```java -@Bean -public RestClient restClient() { +package otel; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestClient; + +@Configuration +public class RestClientConfig { + + @Bean + public RestClient restClient() { return RestClient.create(); + } } ``` + ```java -public MyService(RestClient.Builder restClientBuilder) { - this.restClient = restClientBuilder.build(); +package otel; + +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestClient; + +@RestController +public class RestClientController { + + private final RestClient restClient; + + public RestClientController(RestClient.Builder restClientBuilder) { + restClient = restClientBuilder.baseUrl("http://localhost:8080").build(); + } } ``` + ## Spring Web MVC Autoconfiguration @@ -142,18 +196,43 @@ details, see The following ways of creating a `WebClient` are supported: + + ```java -@Bean -public WebClient webClient() { +package otel; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.reactive.function.client.WebClient; + +@Configuration +public class WebClientConfig { + + @Bean + public WebClient webClient() { return WebClient.create(); + } } ``` + ```java -public MyService(WebClient.Builder webClientBuilder) { - this.webClient = webClientBuilder.build(); +package otel; + +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.reactive.function.client.WebClient; + +@RestController +public class WebClientController { + + private final WebClient webClient; + + public WebClientController(WebClient.Builder webClientBuilder) { + webClient = webClientBuilder.baseUrl("http://localhost:8080").build(); + } } ``` + ## Kafka Instrumentation diff --git a/content/en/docs/zero-code/java/spring-boot-starter/sdk-configuration.md b/content/en/docs/zero-code/java/spring-boot-starter/sdk-configuration.md index d9be4f4a5a73..4819f29d2cdb 100644 --- a/content/en/docs/zero-code/java/spring-boot-starter/sdk-configuration.md +++ b/content/en/docs/zero-code/java/spring-boot-starter/sdk-configuration.md @@ -4,6 +4,9 @@ weight: 30 cSpell:ignore: customizer distro --- + + + This spring starter supports [configuration metadata](https://docs.spring.io/spring-boot/docs/current/reference/html/configuration-metadata.html), which means that you can see and autocomplete all available properties in your @@ -119,16 +122,20 @@ dependencies { {{% /tab %}} {{< /tabpane>}} + + ```java +package otel; + import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.contrib.sampler.RuleBasedRoutingSampler; import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; -import io.opentelemetry.semconv.SemanticAttributes; +import io.opentelemetry.semconv.UrlAttributes; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration -public class OpenTelemetryConfig { +public class FilterPaths { @Bean public AutoConfigurationCustomizerProvider otelCustomizer() { @@ -136,18 +143,23 @@ public class OpenTelemetryConfig { p.addSamplerCustomizer( (fallback, config) -> RuleBasedRoutingSampler.builder(SpanKind.SERVER, fallback) - .drop(SemanticAttributes.URL_PATH, "^/actuator") + .drop(UrlAttributes.URL_PATH, "^/actuator") .build()); } } ``` + ### Configure the exporter programmatically You can also configure OTLP exporters programmatically. This configuration replaces the default OTLP exporter and adds a custom header to the requests. + + ```java +package otel; + import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter; import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; import java.util.Collections; @@ -156,8 +168,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration -public class OpenTelemetryConfig { - +public class CustomAuth { @Bean public AutoConfigurationCustomizerProvider otelCustomizer() { return p -> @@ -181,6 +192,7 @@ public class OpenTelemetryConfig { } } ``` + ## Resource Providers