steffan-westcott / clj-otel

An idiomatic Clojure API for adding telemetry to your libraries and applications using OpenTelemetry.
https://cljdoc.org/d/com.github.steffan-westcott/clj-otel-api
Apache License 2.0
183 stars 12 forks source link

Request: Handle `captured-request-headers` passed to `wrap-server-span` even when `create-span?` is false #17

Closed devurandom closed 3 months ago

devurandom commented 3 months ago

Currently steffan-westcott.clj-otel.api.trace.http/wrap-server-span only uses captured-request-headers when create-span? is passed:

   (cond-> handler
     :always            (wrap-bound-context)
     create-span?       (wrap-new-server-span create-span-opts)
     create-span?       (wrap-server-request-attrs create-span-opts)
     (not create-span?) (wrap-existing-server-span))

It would be nice if wrap-server-span / steffan-westcott.clj-otel.api.trace.http/wrap-server-request-attrs would capture the request headers either way. Maybe through some variant of steffan-westcott.clj-otel.api.trace.http/server-request-attrs that does not set all attributes, but do only this last part of it:

(cond-> ,,,
      captured-request-headers
      (merge-headers-attrs "http.request.header." captured-request-headers headers))

The alternative is setting the system property otel.instrumentation.http.server.capture-request-headers or environment variable OTEL_INSTRUMENTATION_HTTP_SERVER_CAPTURE_REQUEST_HEADERS that the OpenTelemetry Java Agent understands: https://opentelemetry.io/docs/languages/java/automatic/configuration/#capturing-http-request-and-response-headers

steffan-westcott commented 3 months ago

When using the middleware wrap-server-span, the appropriate setting for create-span? is dictated by the presence of the OpenTelemetry instrumentation agent:

When using the agent, it should be configured as described in the documentation. The operation and configuration of the agent is outside the scope of clj-otel, as it is not part of the OpenTelemetry API. The agent is responsible for creating server spans for supported HTTP servers. As you have noted, part of the agent configuration specifies which request headers are captured as attributes on the server span. When using the agent, it creates the server span with the desired header attributes, clj-otel has no control over this.

In your scenario, create-span? is false and (for correct operation) the application is run with the agent. The agent will create server spans, and the agent configuration specifies which headers are added. clj-otel has no influence on agent operation.