This is a Go package designed to provide easy-to-use helpers for OpenTelemetry. The Tracing package focuses on creating, starting, and stopping SPANs with minimal setup, especially useful for functions or methods that require instrumentation, but don't have an existing instrumentation package.
The primary component of the package is the Tracer object that acts as a wrapper around the OpenTelemetry Tracer. Features
- Create, start, and stop SPANs with minimal setup.
- Add arguments to the created SPANs.
- Supports custom loggers through a generic Logger interface. Currently, zap, logrus, standard logger, and glog are supported.
First, you need to create a Tracer instance:
tracer := tracer.New("your-tracer-name", yourLogger)
Then, you can use Tracer to start a new SPAN:
ctx = tracer.StartSpan(ctx, "your-operation")
You can add parameters to your SPAN:
tracer.AddParams(yourParams)
And when you're done, you can end the SPAN:
tracer.EndSpan()
If you want to do these operations in one line, you can use TraceWithAttributes:
tracer := tracer.TraceWithAttributes(ctx, yourLogger, "your-tracer-name", "your-operation", yourParams)
Tracing package uses an agnostic logger so that the user can pass in any supported logger via an adapter method. Here's an example using zap logger:
zapLogger, _ := zap.NewProduction()
defer zapLogger.Sync()
logger := loggers.Zap(zapLogger.Sugar())
tracer := tracer.New("your-tracer-name", logger)
This package supports zap, logrus, standard logger, and glog.
For creating parameters for SPANs, you can use the BuildSpanParams method. This method accepts pairs of key and value arguments and builds a map out of them:
params := tracer.BuildSpanParams("key1", "value1", "key2", "value2")
tracer.AddParams(params)
License
This project is licensed under the MIT License. See the LICENSE file for details.