Monitoring logs with Uptrace
To receive notifications about specific logs and errors, see Alerts and notifications.
Infrastructure logs
To monitor infrastructure logs, you can use Vector (Heroku, Fly) and FluentBit integrations.
If you are using AWS, you can also send CloudWatch Logs directly to Uptrace.
Application logs
There are two approaches to recording application logs with OpenTelemetry:
- OpenTelemetry Logging Libraries - Use bridges for popular logging libraries (recommended)
- Span Events - Add log events directly to spans
Using logging libraries
The recommended approach is to use OpenTelemetry bridges for your existing logging library. This provides automatic trace correlation and a familiar logging API.
import (
"go.uber.org/zap"
"github.com/uptrace/opentelemetry-go-extra/otelzap"
)
// Wrap zap logger with otelzap
log := otelzap.New(zap.NewExample())
// Pass context to correlate with traces
log.Ctx(ctx).Error("request failed",
zap.Error(err),
zap.String("user_id", "12345"),
)
Language-specific guides:
- OpenTelemetry Zap - Go Zap logging integration
- OpenTelemetry Slog - Go standard library slog
- OpenTelemetry Logrus - Go Logrus integration
Using span events
You can also record logs as OpenTelemetry span events. Set the event name to log and use semantic attributes for context:
| Attribute | Description |
|---|---|
log.severity | Log level: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, PANIC |
log.message | The log message |
code.function | Function name (optional) |
code.filepath | File path (optional) |
code.lineno | Line number (optional) |
span := trace.SpanFromContext(ctx)
span.AddEvent("log", trace.WithAttributes(
attribute.String("log.severity", "ERROR"),
attribute.String("log.message", "request failed"),
attribute.String("code.function", "org.FetchUser"),
attribute.String("code.filepath", "org/user.go"),
attribute.Int("code.lineno", 123),
attribute.String("user_id", "12345"),
))
For more information on OpenTelemetry Logs and open-source tools for log management, see Open-Source Log Management Tools.
Grouping logs together
You can control how Uptrace groups logs together by providing grouping.fingerprint attribute which can be a string or a number (hash/id):
log.severity = "info"
log.message = "unstructured log message 123 456 789"
grouping.fingerprint = "unstructured log message"
You can further customize logs grouping using grouping rules.
Propagating trace context
When collecting third-party logs with Vector or FluentBit, trace context is not automatically propagated and logs can't be linked with spans.
To propagate context and associate a log record with a span, include these attributes in your log messages:
| Attribute | Format | Description |
|---|---|---|
trace_id | 32 hex characters | Links log to a distributed trace |
span_id | 16 hex characters | Links log to a specific span |
trace_flags | W3C format | Indicates if trace is sampled (optional) |
Example log message:
request failed trace_id=958180131ddde684c1dbda1aeacf51d3 span_id=0cf859e4f7510204
Example JSON log:
{
"message": "request failed",
"level": "error",
"trace_id": "958180131ddde684c1dbda1aeacf51d3",
"span_id": "0cf859e4f7510204",
"user_id": "12345"
}
See also
- OpenTelemetry Logs - Complete logs specification guide
- Structured logging - What is structured logging
- OpenTelemetry Slog - Go standard library logging
- OpenTelemetry Zap - Go Zap logging integration
- Vector - Collect logs with Vector
- FluentBit - Collect logs with FluentBit