tracer.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package telemetry
  2. import (
  3. "context"
  4. "github.com/honeycombio/otel-launcher-go/launcher"
  5. )
  6. // TracerConfig contains all config for setting up an otel tracer
  7. type TracerConfig struct {
  8. // ServiceName will show as service.name in spans
  9. ServiceName string
  10. // CollectorURL is the OLTP endpoint for receiving traces
  11. CollectorURL string
  12. Debug bool
  13. }
  14. // Tracer is a wrapper for an otel tracer
  15. type Tracer struct {
  16. config TracerConfig
  17. // TraceProvider *sdktrace.TracerProvider
  18. // Tracer trace.Tracer
  19. Shutdown func()
  20. }
  21. // InitTracer is using the Honeycomb and Lightstep partnership launcher for setting up opentelemetry
  22. // Make sure to run `defer tp.Shutdown(ctx)` after calling this function
  23. // to ensure that no traces are lost on exit
  24. func InitTracer(ctx context.Context, conf TracerConfig) (Tracer, error) {
  25. if conf.CollectorURL == "" {
  26. return Tracer{}, nil
  27. }
  28. tracer := Tracer{
  29. config: conf,
  30. }
  31. bsp := NewBaggageSpanProcessor()
  32. lnchr, err := launcher.ConfigureOpenTelemetry(
  33. launcher.WithServiceName(conf.ServiceName),
  34. launcher.WithExporterEndpoint(conf.CollectorURL),
  35. launcher.WithSpanProcessor(bsp),
  36. launcher.WithLogLevel("DEBUG"),
  37. launcher.WithMetricsEnabled(false), // can turn this on later
  38. launcher.WithExporterInsecure(true), // TODO: disable this before production usage
  39. // launcher.WithHeaders() // TODO: add in information about runtime environment
  40. )
  41. if err != nil {
  42. return tracer, err
  43. }
  44. tracer.Shutdown = lnchr
  45. return tracer, nil
  46. }