Skip to content

trustbloc/logutil-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Release License Godocs

Build Status codecov Go Report Card

logutil-go

General purpose field enabled logging module. This allows logs to be attribute labelled instead of having long string logs.

Following are the functions available in the logger:

  • Debug - Logs a message at the debug level.
  • Debugc - Logs a message at the debug level with the given context.
  • Info - Logs a message at the info level.
  • Infoc - Logs a message at the info level with the given context.
  • Warn - Logs a message at the warn level.
  • Warnc - Logs a message at the warn level with the given context.
  • Error - Logs a message at the error level.
  • Errorc - Logs a message at the error level with the given context.

The methods that accept a context parameter are used to log trace information. The trace information is extracted from the context and logged as part of the log message.

Following are the additional fields that are logged when trace information is found in the provided context:

  • trace_id: (from OTel)
  • span_id: (from OTel)
  • parent_span_id: (from OTel)
  • correlation_id: (from Bagage)

For example:

{"level":"debug","ts":"2024-11-04T19:37:32.844Z","logger":"controller","caller":"controller.go:63","msg":"Received request","trace_id":"b20283308c97befd8606ab8932e1d476","span_id":"f32eec4232b5d3e4","parent_span_id":"221262d93c002aab","correlation_id":"2A1E11A0"}

Correlation ID

The correlation ID is used to correlate logs across services. The correlation ID is passed in the request header and is propagated to all the services that are called as part of the request. The correlation ID is logged as part of the log message. The following functions are available to work with the correlation ID:

  • FromContext returns the correlation ID from the given context. A correlation ID is searched in the Bagage member, 'X-Correlation-Id', If a correlation ID is not found in the baggage then:
    • If GenerateUUIDIfNotFound option is set, a new UUID is generated and set on the returned context.
    • If GenerateNewFixedLengthIfNotFound option is set, a new fixed-length correlation ID is generated and set on the returned context.
    • If WithValue is set then the given correlation ID is set on the returned context.
    • If none of the above options is specified then the existing context and empty string are returned.
  • correlationid.HTTPTransport is a RoundTripper that sets the X-Correlation-Id request header for outgoing requests.
  • correlationidecho.Middleware is middleware for the Echo HTTP server that extracts the X-Correlation-Id request header and sets it in the request context Baggage.
  • correlationidmux.Middleware is middleware for the Gorilla Mux HTTP server that extracts the X-Correlation-Id request header and sets it in the request context Baggage.

Enabling OTel tracing, including Baggage

In order to ensure tracing information is propogated across services, the following steps are required:

// Propagate trace context via traceparent and tracestate headers (https://www.w3.org/TR/trace-context/)
// and baggage items (https://www.w3.org/TR/baggage/).
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))