Skip to content

Commit

Permalink
use java snippets from examples repo
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Jun 10, 2024
1 parent 02fc0dd commit c51ac1c
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 37 deletions.
43 changes: 23 additions & 20 deletions content/en/docs/zero-code/java/spring-boot-starter/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ title: Annotations
weight: 60
---

<!-- markdownlint-disable blanks-around-fences -->
<?code-excerpt path-base="examples/java/spring-starter"?>

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
Expand All @@ -22,36 +25,36 @@ span by annotating the method parameters with `@SpanAttribute`.
| ----------- | ------------------------------------------ | ------------- | --------------------------------- |
| `@WithSpan` | `otel.instrumentation.annotations.enabled` | true | Enables the WithSpan annotations. |

<!-- prettier-ignore-start -->
<?code-excerpt "src/main/java/otel/TracedClass.java"?>
```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) {}
}
```
<!-- prettier-ignore-end -->

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ cSpell:ignore: autoconfigurations
weight: 70
---

<!-- markdownlint-disable blanks-around-fences -->
<?code-excerpt path-base="examples/java/spring-starter"?>

Instead of using the OpenTelemetry Spring starter, you can use the OpenTelemetry
autoconfiguration features with an annotation or the Zipkin starter.

Expand All @@ -13,14 +16,19 @@ Autoconfiguration is natively supported by Spring Boot applications. To enable
these features in "vanilla" use `@EnableOpenTelemetry` to complete a component
scan of this package.

<!-- prettier-ignore-start -->
<?code-excerpt "src/main/java/otel/OpenTelemetryConfig.java"?>
```java
package otel;

import io.opentelemetry.instrumentation.spring.autoconfigure.EnableOpenTelemetry;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableOpenTelemetry
public class OpenTelemetryConfig {}
```
<!-- prettier-ignore-end -->

## Zipkin starter

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ weight: 40
cSpell:ignore: autoconfigurations autoconfigures logback webflux webmvc
---

<!-- markdownlint-disable blanks-around-fences -->
<?code-excerpt path-base="examples/java/spring-starter"?>

Out of the box instrumentation is available for several frameworks:

| Feature | Property | Default Value |
Expand Down Expand Up @@ -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:

<!-- prettier-ignore-start -->
<?code-excerpt "src/main/java/otel/RestTemplateConfig.java"?>
```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();
}
}
```

<?code-excerpt "src/main/java/otel/RestTemplateController.java"?>
```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();
}
}
```
<!-- prettier-ignore-end -->

The following ways of creating a `RestClient` are supported:

<!-- prettier-ignore-start -->
<?code-excerpt "src/main/java/otel/RestClientConfig.java"?>
```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();
}
}
```

<?code-excerpt "src/main/java/otel/RestClientController.java"?>
```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();
}
}
```
<!-- prettier-ignore-end -->

## Spring Web MVC Autoconfiguration

Expand All @@ -142,18 +196,43 @@ details, see

The following ways of creating a `WebClient` are supported:

<!-- prettier-ignore-start -->
<?code-excerpt "src/main/java/otel/WebClientConfig.java"?>
```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();
}
}
```

<?code-excerpt "src/main/java/otel/WebClientController.java"?>
```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();
}
}
```
<!-- prettier-ignore-end -->

## Kafka Instrumentation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ weight: 30
cSpell:ignore: customizer distro
---

<!-- markdownlint-disable blanks-around-fences -->
<?code-excerpt path-base="examples/java/spring-starter"?>

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
Expand Down Expand Up @@ -119,35 +122,44 @@ dependencies {

{{% /tab %}} {{< /tabpane>}}

<!-- prettier-ignore-start -->
<?code-excerpt "src/main/java/otel/FilterPaths.java"?>
```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() {
return p ->
p.addSamplerCustomizer(
(fallback, config) ->
RuleBasedRoutingSampler.builder(SpanKind.SERVER, fallback)
.drop(SemanticAttributes.URL_PATH, "^/actuator")
.drop(UrlAttributes.URL_PATH, "^/actuator")
.build());
}
}
```
<!-- prettier-ignore-end -->

### 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.

<!-- prettier-ignore-start -->
<?code-excerpt "src/main/java/otel/CustomAuth.java"?>
```java
package otel;

import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import java.util.Collections;
Expand All @@ -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 ->
Expand All @@ -181,6 +192,7 @@ public class OpenTelemetryConfig {
}
}
```
<!-- prettier-ignore-end -->

## Resource Providers

Expand Down

0 comments on commit c51ac1c

Please sign in to comment.