We often get asked how to copy http request/response headers into OpenTelemetry span attributes. The java instrumentation agent does not do this by default, primarily because it risks leaking sensitive data. Fortunately, there is a way to instruct the java agent to collect http headers and attach them to your spans.
This project is a quick demonstrative example of capturing http headers as span attributes.
The main application is HttpHeaderAttributesMain. This application spins up a small HTTP server on port 8182, and a client that sends a request to the server once a second.
This is an unusual ab/use of HTTP and is only intended to demonstrate how to tell the java instrumentation to copy headers into attributes.
The client sends a POST request with a demeanor HTTP header and the name of a person in the request body.
POST /greeting HTTP/1.1
The server responds with an originator header whose value is the uppercase version of the person's name. The body contains the person's name and an emoji corresponding to their demeanor.
HTTP/1.1 200 OK
Date: Thu, 15 Dec 2022 21:39:27 GMT
Benjamin => 🥱
To build and run the example, simply run the gradle build target run. You must have java 11+ installed and an otel collector running on localhost.
Configuration is provided to the java agent via system properties passed on the commandline. No manual code changes required!