Class: SemanticLogger::Appender::OpenTelemetry

Inherits:
Subscriber
  • Object
show all
Defined in:
lib/semantic_logger/appender/open_telemetry.rb

Constant Summary collapse

CAPTURE_CONTEXT =
->(log) { log.capture(:open_telemetry, ::OpenTelemetry::Context.current) }

Constants inherited from Subscriber

Subscriber::MESSAGE_ERRORS

Instance Attribute Summary collapse

Attributes inherited from Subscriber

#application, #environment, #formatter, #host, #logger, #metrics

Instance Method Summary collapse

Methods inherited from Subscriber

#batch_all_or_nothing?, #batch_by_default?, #batch_refusal, #console_output?, #console_stream, #fails_any_batch?, #inspect, #inspect_details, #message_error?, #not_sent?, #rejected_entry, #should_log?, #wrote_nothing?

Constructor Details

#initialize(name: "SemanticLogger", version: SemanticLogger::VERSION, formatter: nil, **args) ⇒ OpenTelemetry

Create a Open Telemetry Logger appender instance.

Parameters name: [String] Name of the Open Telemetry logger, which is the instrumentation scope of every log record it emits. Default: "SemanticLogger"

version: [String]
Version of that instrumentation scope.
Default: SemanticLogger::VERSION

The formatter maps each log entry to an Open Telemetry log record, returning the keyword arguments for Logger#on_emit other than context:. Default: SemanticLogger::Formatters::OpenTelemetry

A metric-only entry, a metric without a message or an exception, is left out, since it is meant for the metric subscribers. To send them as log records with no body, their metric in the attributes, set metrics: true.

Example SemanticLogger.add_appender(appender: :open_telemetry)



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/semantic_logger/appender/open_telemetry.rb', line 42

def initialize(name: "SemanticLogger",
               version: SemanticLogger::VERSION,
               formatter: nil,
               **args,
               &)
  # Base#initialize (via Subscriber) overwrites @name with the class name,
  # so call super first and then assign our own attributes.
  super(formatter: formatter, **args, &)

  @name      = name
  @version   = version
  @provider  = ::OpenTelemetry.logger_provider
  # Not @logger, which holds the internal logger that this appender reports its own problems
  # to, as every appender does (see Subscriber#logger).
  @open_telemetry_logger = @provider.logger(name: @name, version: @version)

  # Capture the current Open Telemetry context when a log entry is captured.
  # Prevents duplicate subscribers as long as it is from a constant.
  SemanticLogger.on_log(CAPTURE_CONTEXT)
end

Instance Attribute Details

#name ⇒ Object (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/semantic_logger/appender/open_telemetry.rb', line 16

def name
  @name
end

#provider ⇒ Object (readonly)

Returns the value of attribute provider.



16
17
18
# File 'lib/semantic_logger/appender/open_telemetry.rb', line 16

def provider
  @provider
end

#version ⇒ Object (readonly)

Returns the value of attribute version.



16
17
18
# File 'lib/semantic_logger/appender/open_telemetry.rb', line 16

def version
  @version
end

Instance Method Details

#close ⇒ Object

Close the appender and release resources.



82
83
84
85
86
87
88
89
90
# File 'lib/semantic_logger/appender/open_telemetry.rb', line 82

def close
  return unless @provider

  @provider.shutdown if @provider.respond_to?(:shutdown)
rescue StandardError => e
  logger.warn("Shutdown failed", e)
ensure
  @provider = nil
end

#flush ⇒ Object

Flush all pending logs.



72
73
74
75
76
77
78
79
# File 'lib/semantic_logger/appender/open_telemetry.rb', line 72

def flush
  return unless @provider

  @provider.force_flush if @provider.respond_to?(:force_flush)
rescue StandardError => e
  # Swallow to avoid noisy shutdown exceptions.
  logger.warn("Flush failed", e)
end

#log(log) ⇒ Object

Emits the log record the formatter returns, in the Open Telemetry context captured when the entry was logged, so that the record is correlated with the trace and span active then.



65
66
67
68
69
# File 'lib/semantic_logger/appender/open_telemetry.rb', line 65

def log(log)
  context = log.captured(:open_telemetry) || ::OpenTelemetry::Context.current
  @open_telemetry_logger.on_emit(**format_log(log), context: context)
  true
end