-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extending with custom manual instrumentation #5789
Open
zeitlinger
wants to merge
11
commits into
open-telemetry:main
Choose a base branch
from
zeitlinger:extending-with-custom-manual-instrumenation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+183
−4
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
723668d
explain manual instrumentation
zeitlinger 4a1224c
explain manual instrumentation
zeitlinger de10d10
explain manual instrumentation
zeitlinger d90c278
Apply suggestions from code review
zeitlinger d04ebc9
spelling
zeitlinger e9a51ee
using API
zeitlinger bd1255b
using API
zeitlinger c2e70af
Update content/en/docs/zero-code/java/agent/api.md
zeitlinger ba348bb
using API
zeitlinger 1356d4f
using API
zeitlinger 217dc46
Apply suggestions from code review
zeitlinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
--- | ||
title: Extending instrumentations with the API | ||
linkTitle: Extend with the API | ||
description: | ||
Use the OpenTelemetry API in combination with the Java agent to extend the | ||
automatically generated telemetry with custom spans and metrics | ||
weight: 21 | ||
--- | ||
|
||
## Introduction | ||
|
||
In addition to the out-of-the-box instrumentation, you can extend the Java agent | ||
with custom manual instrumentation using the OpenTelemetry API. This allows you | ||
to create [spans](/docs/concepts/signals/traces/#spans) and | ||
[metrics](/docs/concepts/signals/metrics) for your own code without doing too | ||
many code changes. | ||
|
||
## Dependencies | ||
|
||
Add a dependency on the `opentelemetry-api` library. | ||
|
||
### Maven | ||
|
||
```xml | ||
<dependencies> | ||
<dependency> | ||
<groupId>io.opentelemetry</groupId> | ||
<artifactId>opentelemetry-api</artifactId> | ||
<version>{{% param vers.otel %}}</version> | ||
</dependency> | ||
</dependencies> | ||
``` | ||
|
||
### Gradle | ||
|
||
```groovy | ||
dependencies { | ||
implementation('io.opentelemetry:opentelemetry-api:{{% param vers.otel %}}') | ||
} | ||
``` | ||
|
||
## OpenTelemetry | ||
|
||
The Java agent is a special case where `GlobalOpenTelemetry` is set by the | ||
agent. Simply call `GlobalOpenTelemetry.get()` to access the `OpenTelemetry` | ||
instance. | ||
|
||
## Span | ||
|
||
{{% alert title="Note" color="info" %}} | ||
|
||
For the most common use cases, use the `@WithSpan` annotation instead of manual | ||
instrumentation. See [Annotations](../annotations) for more information. | ||
|
||
{{% /alert %}} | ||
|
||
```java | ||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.trace.Tracer; | ||
|
||
Tracer tracer = GlobalOpenTelemetry.getTracer("application"); | ||
``` | ||
|
||
Use the `Tracer` to create a span as explained in the | ||
[Span](/docs/languages/java/api/#span) section. | ||
|
||
A full example can be found in the [example repository]. | ||
|
||
## Meter | ||
|
||
```java | ||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.metrics.Meter; | ||
|
||
Meter meter = GlobalOpenTelemetry.getMeter("application"); | ||
``` | ||
|
||
Use the `Meter` to create a counter, gauge or histogram as explained in the | ||
[Meter](/docs/languages/java/api/#meter) section. | ||
|
||
A full example can be found in the [example repository]. | ||
|
||
[example repository]: | ||
https://github.com/open-telemetry/opentelemetry-java-examples/tree/main/javaagent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
--- | ||
title: Extending instrumentations with the API | ||
linkTitle: Extend with the API | ||
description: | ||
Use the OpenTelemetry API in combination with the Spring Boot starter to | ||
extend the automatically generated telemetry with custom spans and metrics | ||
weight: 21 | ||
--- | ||
|
||
## Introduction | ||
|
||
In addition to the out-of-the-box instrumentation, you can extend the Spring | ||
starter with custom manual instrumentation using the OpenTelemetry API. This | ||
allows you to create [spans](/docs/concepts/signals/traces/#spans) and | ||
[metrics](/docs/concepts/signals/metrics) for your own code without doing too | ||
many code changes. | ||
|
||
The required dependencies are already included in the Spring Boot starter. | ||
|
||
## OpenTelemetry | ||
|
||
The Spring Boot starter is a special case where `OpenTelemetry` is available as | ||
a Spring bean. Simply inject `OpenTelemetry` into your Spring components. | ||
|
||
## Span | ||
|
||
{{% alert title="Note" color="info" %}} | ||
|
||
For the most common use cases, use the `@WithSpan` annotation instead of manual | ||
instrumentation. See [Annotations](../annotations) for more information. | ||
|
||
{{% /alert %}} | ||
|
||
```java | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.trace.Tracer; | ||
|
||
@Controller | ||
public class MyController { | ||
private final Tracer tracer; | ||
|
||
public MyController(OpenTelemetry openTelemetry) { | ||
this.tracer = openTelemetry.getTracer("application"); | ||
} | ||
} | ||
``` | ||
|
||
Use the `Tracer` to create a span as explained in the | ||
[Span](/docs/languages/java/api/#span) section. | ||
|
||
A full example can be found in the [example repository]. | ||
|
||
## Meter | ||
|
||
```java | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.metrics.Meter; | ||
|
||
@Controller | ||
public class MyController { | ||
private final Meter meter; | ||
|
||
public MyController(OpenTelemetry openTelemetry) { | ||
this.meter = openTelemetry.getMeter("application"); | ||
} | ||
} | ||
``` | ||
|
||
Use the `Meter` to create a counter, gauge or histogram as explained in the | ||
[Meter](/docs/languages/java/api/#meter) section. | ||
|
||
A full example can be found in the [example repository]. | ||
|
||
[example repository]: | ||
https://github.com/open-telemetry/opentelemetry-java-examples/tree/main/spring-native |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about the organization of the additions in this file
Maybe could just add two small notes right above here, something like "If you are using the Java agent, see X for information about how to obtain an OpenTelemetry instance" (and similar note for Spring Boot starter)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree - I think an alert could be good. Something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's useful if to have a hint it the ToC - which is not possible with an info box
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@svrnm do you have a recommendation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@trask @jack-berg wdyt?