Skip to main content

Go TraceId and SpanId injection into logs

This page describes how to configure traceId and spanId data injection into user logs in GoLang applications using Logrus logging library. Essentially, you only need to add tracing dependencies and injecting the span_id and trace_id into relevant logs.

Logrus instrumentation

  1. Import dependency:

    import (
    oteltrace ""
  2. Configure logrus to format logs and extract span data by spanName:

    func main() {
    // Ensure logrus behaves like TTY is disabled
    DisableColors: true,
    FullTimestamp: true,
  3. Prepare a function which will return logrus fields with span_id and trace_id:

    func LogrusFields(span oteltrace.Span) logrus.Fields {
    return logrus.Fields{
    "span_id": span.SpanContext().SpanID().String(),
    "trace_id": span.SpanContext().TraceID().String(),
  4. Use the fields function in your logging, whenever there's a tracing context available:

    _, span := tracer.Start(ctx, "spanName", ...)
    defer span.End()
    logrus.WithFields(helper.LogrusFields(span)).Info("Some message...")
Privacy Statement
Terms of Use

Copyright © 2024 by Sumo Logic, Inc.