Skip to content

Commit 6de802b

Browse files
authored
Add agent configuration excursus (#21)
* add line numbers * add agent config excursus * simplify path
1 parent ff38906 commit 6de802b

File tree

4 files changed

+97
-6
lines changed

4 files changed

+97
-6
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Enable debug logs
2+
otel.javaagent.debug=true
3+
4+
# Collect spans for internal method
5+
otel.instrumentation.methods.include=io.novatec.todobackend.TodobackendApplication[someInternalMethod]
6+
7+
# Capture additional request headers
8+
# Try out: curl -X POST -H "custom: my-header" localhost:8080/todos/NEW
9+
otel.instrumentation.http.server.capture-request-headers=custom
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Enable debug logs
2+
otel.javaagent.debug=true
3+
4+
# Collect spans for internal method
5+
otel.instrumentation.methods.include=io.novatec.todobackend.TodobackendApplication[someInternalMethod]
6+
7+
# Capture additional request headers
8+
# Try out: curl -X POST -H "custom: my-header" localhost:8080/todos/NEW
9+
otel.instrumentation.http.server.capture-request-headers=custom

tutorial/content/exercises/instrumentation/automatic/zero-code/index.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ You can see multiple statements of the `otel.javaagent` but if you take a closer
238238
239239
This is how the auto-instrumentation works here. It uses a collection of instrumentation library to trace default components, which the Java application uses here.
240240
241-
However it makes another problem obvious: There are many spans being collected and it is hard to read on the console with the human eye.
241+
However, it makes another problem obvious: There are many spans being collected and it is hard to read on the console with the human eye.
242242
243243
If you execute another `curl` call in your other shell to add a new item, e.g.
244244
@@ -340,6 +340,79 @@ You can observe the different behaviour in the Jaeger console.
340340
If you are familiar with Java you can of course also look at the code in the folder: `src/main/java/io/novatec/todobackend`
341341
Open the TodobackendApplication.java with your VS Code built-in editor.
342342
343+
### Excursus - Configuration of Java agent
344+
345+
The OpenTelemetry Java agent provides a vast amount of features out of the box and normally requires no further configuration.
346+
Nevertheless, it provides several configuration properties to adapt its behaviour to your needs.
347+
Those properties can be set via `-D` flags, environment variables (as seen above) or properties file,
348+
which can be referenced with `otel.javaagent.configuration-file`.
349+
Below we will explain some useful agent properties.
350+
351+
There is a properties file in the `resources` directory, which can be used to test some configuration properties.
352+
Run the following command to start the Java application with the configuration file:
353+
354+
```sh
355+
java -javaagent:./opentelemetry-javaagent.jar \
356+
-Dotel.javaagent.configuration-file=src/main/resources/otel-config.properties \
357+
-jar target/todobackend-0.0.1-SNAPSHOT.jar
358+
```
359+
360+
Note, when changing the `otel-config.properties` file, you have to restart the application for the changes to take effect.
361+
362+
#### Debugging
363+
364+
When experimenting with the Java agent, it is always helpful to get more detailed insights from the agent.
365+
Thus, you can enable the debug logs by setting `otel.javaagent.debug` to true.
366+
367+
#### Limiting instrumentation
368+
369+
The Java agent contains a large amount of instrumentation modules for different technologies.
370+
Sometimes we would like to include only specific modules and disable everything else, for instance to save resources.
371+
The agent allows us to disable the auto instrumentation completely by setting
372+
`otel.instrumentation.common.default-enabled` to false. Then, you can include each module individually by setting
373+
`otel.instrumentation.[module-name].enabled` to true. You can find the list of all default module names in the
374+
[documentation](https://opentelemetry.io/docs/zero-code/java/agent/disable/#suppressing-specific-agent-instrumentation).
375+
376+
#### Creating additional spans
377+
378+
Each instrumentation module includes only a specific set of methods, which will be instrumented.
379+
If you would like to instrument additional methods to create more detailed traces of your application,
380+
you can use the property `otel.instrumentation.methods.include`.
381+
This property expects a list of methods, which are defined like this:
382+
`io.novatec.todobackend.TodobackendApplication[someInternalMethod, anotherMethod];io.another.Application[method]`
383+
384+
Later, we will learn about the `@WithSpan` annotation, which behaves similarly.
385+
386+
#### Capturing HTTP information
387+
388+
You can easily include additional information from HTTP headers to your spans.
389+
This allows you to extend the existing HTTP instrumentation.
390+
There are several properties depending on whether your application acts as a client or server and whether you
391+
would like to read the headers from requests or responses:
392+
393+
- `otel.instrumentation.http.client.capture-request-headers`
394+
- `otel.instrumentation.http.client.capture-response-headers`
395+
- `otel.instrumentation.http.server.capture-request-headers`
396+
- `otel.instrumentation.http.server.capture-response-headers`
397+
398+
All properties expect a comma-separated list of HTTP header names, which should be read and written into span attributes.
399+
400+
#### Agent extensions
401+
402+
The Java agent provides an extension API, which allows you to extend its behaviour with your custom logic.
403+
This also allows you to extend the instrumentation without changing the application codebase.
404+
For instance, you would like to collect business data with the Java agent. Since the agent itself doesn't know your
405+
business logic, you can include your extension to capture such data.
406+
407+
An extension resembles an additional JAR file, which has to be referenced via `otel.javaagent.extensions`.
408+
409+
There are some example for such extensions in [GitHub](https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/examples/extension).
410+
411+
<!-- Later, we can add another chapter to explain in detail how to write agent extensions by yourself -->
412+
413+
Take a look at the [OpenTelemetry documentation](https://opentelemetry.io/docs/zero-code/java/agent/)
414+
to learn more about the agent configuration.
415+
343416
### Instrumentation of the Python (Flask) component
344417

345418
Now that we have successfully auto-instrumented the Java part of the application, let's focus how to achieve similar results with the Python part. Leave the docker container and the Java part from the previous step up und running, we still need it now.

tutorial/content/exercises/instrumentation/manual_java/logs/index.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ After the app has started, send some request in your second terminal to call the
251251
After sending a request, you should see an output like this:
252252

253253
```ssh
254-
09:25:34.346 [http-nio-8080-exec-1] INFO i.n.t.TodobackendApplication - GET /todos/ []
255-
2024-07-21T09:25:34.346Z INFO 'GET /todos/' : dfd9f9db58f468fa274f48462b994098 f32233cb857d7177 [scopeInfo: io.novatec.todobackend.TodobackendApplication:] {http.request.method="GET"}
256-
09:25:34.346 [http-nio-8080-exec-1] INFO i.o.e.logging.LoggingSpanExporter - 'getTodos' : dfd9f9db58f468fa274f48462b994098 f32233cb857d7177 INTERNAL [tracer: io.novatec.todobackend.TodobackendApplication:0.1.0] {}
254+
1: 09:25:34.346 [http-nio-8080-exec-1] INFO i.n.t.TodobackendApplication - GET /todos/ []
255+
2: 2024-07-21T09:25:34.346Z INFO 'GET /todos/' : dfd9f9db58f468fa274f48462b994098 f32233cb857d7177 [scopeInfo: io.novatec.todobackend.TodobackendApplication:] {http.request.method="GET"}
256+
3: 09:25:34.346 [http-nio-8080-exec-1] INFO i.o.e.logging.LoggingSpanExporter - 'getTodos' : dfd9f9db58f468fa274f48462b994098 f32233cb857d7177 INTERNAL [tracer: io.novatec.todobackend.TodobackendApplication:0.1.0] {}
257257

258258
```
259259

@@ -263,9 +263,9 @@ The second line results from the emitted `LogRecord`. The output includes the ti
263263
the severity and the actual log message. Additionally, the `LogRecord` contains data from OpenTelemetry, like the
264264
current trace ID as well as span ID. That's why we have also set up the tracing SDK. If the current context does not
265265
contain any span while emitting the `LogRecord`, it will just show some zeros, indicating there is no current span.
266-
At the end the output contains the scope of the OpenTelemetry `Logger` as well as the set attributes.
266+
At the end, the output contains the scope of the OpenTelemetry `Logger` as well as the set attributes.
267267
268-
The last line results from the SpanExporter of the tracing SDk and does not bother us right now.
268+
The last line results from the SpanExporter of the tracing SDK and does not bother us right now.
269269
270270
#### Don't replace the existing log API
271271

0 commit comments

Comments
 (0)