Class: SemanticLogger::Formatters::OpenTelemetry

Inherits:
Base
  • Object
show all
Defined in:
lib/semantic_logger/formatters/open_telemetry.rb

Overview

Maps a log entry to an OpenTelemetry log record.

Returns the keyword arguments for the OpenTelemetry Logs API's Logger#on_emit, other than the context, which the appender captures:

severity_text:   the level, such as "info"
severity_number: the OpenTelemetry severity number of the level
timestamp:       the time the entry was logged
body:            the message, or nil when the entry has none
attributes:      the named tags and the payload, then the entry's own fields

Each named tag and payload key is an attribute of its own, the payload taking precedence over a named tag of the same name. The entry's own fields use the attribute names of the OpenTelemetry semantic conventions where there is one, and are otherwise prefixed with "semantic_logger.", so that they do not collide with the application's keys:

exception.type, exception.message, exception.stacktrace (with each exception that caused it)
code.file.path, code.line.number (where the entry was logged, when it has a call site or
captured a backtrace), code.function.name (when it has a call site)
thread.name, process.pid
semantic_logger.name, semantic_logger.tags, semantic_logger.duration_ms,
semantic_logger.metric, semantic_logger.metric_amount
semantic_logger.dimensions.<key>, one for each dimension of a metric

A named tag or payload key named like one of these is prefixed with its source, as in "payload.exception.type", so that logged data cannot replace or forge the entry's own fields.

The host, application, and environment are not attributes: they describe the process, which OpenTelemetry reports in the resource of every export. The process id is, since the resource holds the id of the process that configured OpenTelemetry, not of a forked child.

An attribute value is a String, Integer, Float, true, or false, or an Array of them of one type, since the OpenTelemetry SDK reports any other value as an error, which, when that error is logged to this appender with the same named tags, repeats until the stack overflows. See #coerce_value.

Constant Summary collapse

EXCEPTION_TYPE =

The entry's own attributes.

"exception.type".freeze
EXCEPTION_MESSAGE =
"exception.message".freeze
EXCEPTION_STACKTRACE =
"exception.stacktrace".freeze
CODE_FILE_PATH =
"code.file.path".freeze
CODE_LINE_NUMBER =
"code.line.number".freeze
CODE_FUNCTION_NAME =
"code.function.name".freeze
THREAD_NAME =
"thread.name".freeze
PROCESS_PID =
"process.pid".freeze
NAME =
"semantic_logger.name".freeze
TAGS =
"semantic_logger.tags".freeze
DURATION_MS =
"semantic_logger.duration_ms".freeze
METRIC =
"semantic_logger.metric".freeze
METRIC_AMOUNT =
"semantic_logger.metric_amount".freeze
DIMENSIONS_PREFIX =
"semantic_logger.dimensions.".freeze
ENTRY_ATTRIBUTES =
[
  EXCEPTION_TYPE, EXCEPTION_MESSAGE, EXCEPTION_STACKTRACE, CODE_FILE_PATH, CODE_LINE_NUMBER,
  CODE_FUNCTION_NAME, THREAD_NAME, PROCESS_PID, NAME, TAGS, DURATION_MS, METRIC, METRIC_AMOUNT
].to_h { |name| [name, true] }.freeze
NAMED_TAGS_PREFIX =

Prefixes for a named tag or payload key named like one of the entry's own attributes.

"named_tags.".freeze
PAYLOAD_PREFIX =
"payload.".freeze

Constants inherited from Base

Base::CONTROL_CHARS, Base::CONTROL_CHAR_ESCAPES, Base::PRECISION, Base::UNICODE_CONTROL_CHARS

Instance Attribute Summary

Attributes inherited from Base

#backtrace_cleaner, #escape_control_chars, #log, #log_application, #log_environment, #log_exception_details, #log_host, #logger, #precision, #time_format

Instance Method Summary collapse

Methods inherited from Base

build_time_format, #initialize, #pid, #time

Constructor Details

This class inherits a constructor from SemanticLogger::Formatters::Base

Instance Method Details

#attributes ⇒ Object

Returns [Hash] the named tags and the payload, then the entry's own fields.

A payload that is not a Hash, such as a String parsed from a request body, is submitted as a single "payload" attribute.



91
92
93
94
95
96
97
98
99
100
# File 'lib/semantic_logger/formatters/open_telemetry.rb', line 91

def attributes
  attributes = {}
  add_logged(attributes, NAMED_TAGS_PREFIX, log.named_tags) if log.named_tags.is_a?(Hash)
  if log.payload?
    add_logged(attributes, PAYLOAD_PREFIX, log.payload.is_a?(Hash) ? log.payload : {payload: log.payload})
  end
  add_entry(attributes)
  add_exception(attributes)
  attributes
end

#body ⇒ Object

Returns [String] the message, or nil when the entry has none, such as a metric only entry, or an exception logged without a message.



83
84
85
# File 'lib/semantic_logger/formatters/open_telemetry.rb', line 83

def body
  log.cleansed_message if log.message
end

#call(log, logger) ⇒ Object

Returns [Hash] the keyword arguments for Logger#on_emit.

Unlike the JSON formatters there is no single .to_json boundary here: the record is handed to the OTLP exporter, which requires valid UTF-8. Cleanse the whole structure so binary / non UTF-8 strings cannot break the export.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/semantic_logger/formatters/open_telemetry.rb', line 66

def call(log, logger)
  self.log    = log
  self.logger = logger

  Utils.encode_utf8(
    {
      severity_text:   log.level.to_s,
      severity_number: severity_number(log.level),
      timestamp:       log.time,
      body:            body,
      attributes:      attributes
    }
  )
end