Collecting Logrus logs with OpenTelemetry
OpenTelemetry Logrus is a plugin for Logrus logging library that provides integration with OpenTelemetry. Logrus is a popular logging library for Go programming language that provides a simple and easy-to-use API for logging messages in your applications.
OpenTelemetry Logrus automatically injects trace and span IDs for each logging statement, allowing you to correlate your logs with other telemetry data from your application.
What is OpenTelemetry?
OpenTelemetry is an open-source observability framework that aims to standardize and simplify the collection, processing, and export of telemetry data from applications and systems.
OpenTelemetry supports multiple programming languages and platforms, making it suitable for a wide range of applications and environments.
OpenTelemetry enables developers to instrument their code and collect telemetry data, which can then be exported to various OpenTelemetry backends or observability platforms for analysis and visualization.
Logrus instrumentation
To install otellogrus instrumentation:
go get github.com/uptrace/opentelemetry-go-extra/otellogrus
Usage
You need to install an otellogrus.Hook
and use logrus.WithContext
to propagate the active span.
import (
"github.com/uptrace/opentelemetry-go-extra/otellogrus"
"github.com/sirupsen/logrus"
)
// Instrument logrus.
logrus.AddHook(otellogrus.NewHook(otellogrus.WithLevels(
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
)))
// Use ctx to pass the active span.
logrus.WithContext(ctx).
WithError(errors.New("hello world")).
WithField("foo", "bar").
Error("something failed")
Options
otellogrus.NewHook accepts the following options:
otellogrus.WithLevels(logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel, logrus.WarnLevel)
sets the logrus logging levels on which the hook is fired.WithErrorStatusLevel(logrus.ErrorLevel)
sets the minimal logrus logging level on which the span status is set to codes.Error.
What is Uptrace?
Uptrace is a OpenTelemetry APM that supports distributed tracing, metrics, and logs. You can use it to monitor applications and troubleshoot issues.
Uptrace comes with an intuitive query builder, rich dashboards, alerting rules with notifications, and integrations for most languages and frameworks.
Uptrace can process billions of spans and metrics on a single server and allows you to monitor your applications at 10x lower cost.
In just a few minutes, you can try Uptrace by visiting the cloud demo (no login required) or running it locally with Docker. The source code is available on GitHub.
What's next?
Next, instrument more operations to get a more detailed picture. Try to prioritize network calls, disk operations, database queries, error and logs.
You can also create your own instrumentations using OpenTelemetry Go Tracing API.