taoensso / timbre

Pure Clojure/Script logging library
https://www.taoensso.com/timbre
Eclipse Public License 1.0
1.44k stars 171 forks source link

OpenTelemetry Protocol (OTLP) appender #374

Closed devurandom closed 4 months ago

devurandom commented 1 year ago

Requires com.github.steffan-westcott/clj-otel-api.

With Java Agent

Activate an appender configured by the OpenTelemetry Java Agent:

(let [logger-provider (.getLogsBridge (GlobalOpenTelemetry/get))
      appender (taoensso.timbre.appenders.community.otlp/otlp-appender logger-provider)]
  (taoensso.timbre/merge-config! {:appenders {:otlp appender}}))

Note: When relying on the OpenTelemetry Java Agent 1.x, you need to explicitly enable the logs exporter with OTEL_LOGS_EXPORTER=otlp. This will become the default with the release of Java Agent 2.0, cf.

Without Java Agent

If you want autoconfiguration without the Java Agent, you also need io.opentelemetry/opentelemetry-sdk-extension-autoconfigure and io.opentelemetry/opentelemetry-exporter-otlp on the classpath.

Create an autoconfigured appender and activate it:

(let [logger-provider (.getSdkLoggerProvider
                        (.getOpenTelemetrySdk
                          (.build
                            (AutoConfiguredOpenTelemetrySdk/builder))))
      appender (taoensso.timbre.appenders.community.otlp/otlp-appender logger-provider)]
  (taoensso.timbre/merge-config! {:appenders {:otlp appender}}))

If you already have an instance of GlobalOpenTelemetry, e.g. created by the OpenTelemetry Java Agent, you need to prevent setting the newly created SDK as the global default:

(.build
  (doto (AutoConfiguredOpenTelemetrySdk/builder)
    (.setResultAsGlobal false)))

I took inspiration from taoensso.timbre.appenders.community.sentry.

Once https://github.com/steffan-westcott/clj-otel/issues/8 is implemented, the actual log emission should be replaced with using clj-otel's API.

ptaoussanis commented 7 months ago

@devurandom Hi Dennis, thanks for this! Will take a proper look next time I'm doing batched work on Timbre πŸ‘ No need to worry about rebasing, I'll take care of that during review.

devurandom commented 4 months ago

@ptaoussanis Hi Peter! Is there anything I can do to help get this merged?

ptaoussanis commented 4 months ago

@devurandom Hi Dennis, apologies for the delay! I would have included this in yesterday's update (v6.4.0), but that was a bit of a rush and mostly intended as a hotfix release.

I've manually merged your PR here, thanks for all the work on this! πŸ™

I made a few superficial changes, just to try keep a ~consistent style between the various appenders.

Please let me know if you're happy with that commit, in which case I'll aim to cut a new release in the next few days.

Cheers! :-)

devurandom commented 4 months ago

Hi Peter! Thanks! I looked at git diff 39c6e310a62013b694116fd1c66db5b5eb3cd6c0..0bf280901f1174ac7c783dc2f5ff1f8e1ba32e1a -- src/taoensso/timbre/appenders/community/otlp.clj to review the changes. The code looks good and I noticed no issues.

Re: For agent v2.x: the logs exporter should be enabled by default [1].

By now v2.0 of the Java SDK has been released, so we could just link to its changelog (instead of linking to a GH issue and the changelog with the early notification): https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/v2.0.0/CHANGELOG.md#version-200-2024-01-12

Is there a particular reason you use URL shorteners for links in comments? I imagine they would be inconvenient if they ever become unavailable. It would be hard to figure out what they linked to and also archives would probably only have indexed the original link, but not the shortened one.

ptaoussanis commented 4 months ago

Thanks for the review!

Closing, this has now included as part of the v6.5.0 release πŸ‘

Final commit: f3ce2b5