Class: ScoutApm::Logging::Loggers::OpenTelemetry::SDK::Logs::LoggerProvider

Inherits:
Logs::LoggerProvider show all
Defined in:
lib/scout_apm/logging/loggers/opentelemetry/sdk/logs/logger_provider.rb

Overview

The SDK implementation of OpenTelemetry::Logs::LoggerProvider.

Instance Method Summary collapse

Constructor Details

#initialize(resource: OpenTelemetry::SDK::Resources::Resource.create) ⇒ OpenTelemetry::SDK::Logs::LoggerProvider

Returns a new LoggerProvider instance.

Parameters:



26
27
28
29
30
31
# File 'lib/scout_apm/logging/loggers/opentelemetry/sdk/logs/logger_provider.rb', line 26

def initialize(resource: OpenTelemetry::SDK::Resources::Resource.create)
  @log_record_processors = []
  @mutex = Mutex.new
  @resource = resource
  @stopped = false
end

Instance Method Details

#add_log_record_processor(log_record_processor) ⇒ Object

Adds a new log record processor to this LoggerProvider’s log_record_processors.

Parameters:



55
56
57
58
59
60
61
62
63
64
# File 'lib/scout_apm/logging/loggers/opentelemetry/sdk/logs/logger_provider.rb', line 55

def add_log_record_processor(log_record_processor)
  @mutex.synchronize do
    if @stopped
      OpenTelemetry.logger.warn('calling LoggerProvider#' \
        'add_log_record_processor after shutdown.')
      return
    end
    @log_record_processors = @log_record_processors.dup.push(log_record_processor)
  end
end

#force_flush(timeout: nil) ⇒ Integer

Immediately export all ScoutApm::Logging::Loggers::OpenTelemetry::SDK::Logs::LogRecords that have not yet been exported for all the registered ScoutApm::Logging::Loggers::OpenTelemetry::SDK::Logs::LogRecordProcessors.

This method should only be called in cases where it is absolutely necessary, such as when using some FaaS providers that may suspend the process after an invocation, but before the ScoutApm::Logging::Loggers::OpenTelemetry::SDK::Logs::LogRecordProcessor exports the completed ScoutApm::Logging::Loggers::OpenTelemetry::SDK::Logs::LogRecords.

Parameters:

  • timeout (optional Numeric) (defaults to: nil)

    An optional timeout in seconds.

Returns:

  • (Integer)

    Export::SUCCESS if no error occurred, Export::FAILURE if a non-specific failure occurred, Export::TIMEOUT if a timeout occurred.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/scout_apm/logging/loggers/opentelemetry/sdk/logs/logger_provider.rb', line 109

def force_flush(timeout: nil)
  @mutex.synchronize do
    return Export::SUCCESS if @stopped

    start_time = OpenTelemetry::Common::Utilities.timeout_timestamp
    results = @log_record_processors.map do |processor|
      remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time)
      return Export::TIMEOUT if remaining_timeout&.zero?

      processor.force_flush(timeout: remaining_timeout)
    end

    results.max || Export::SUCCESS
  end
end

#logger(name:, version: nil) ⇒ OpenTelemetry::SDK::Logs::Logger

Parameters:

  • name (String)

    Instrumentation package name

  • version (optional String) (defaults to: nil)

    Instrumentation package version

Returns:



39
40
41
42
43
44
45
46
47
48
# File 'lib/scout_apm/logging/loggers/opentelemetry/sdk/logs/logger_provider.rb', line 39

def logger(name:, version: nil)
  version ||= ''

  if !name.is_a?(String) || name.empty?
    OpenTelemetry.logger.warn('LoggerProvider#logger called with an ' \
      "invalid name. Name provided: #{name.inspect}")
  end

  Logger.new(name, version, self)
end

#on_emit(timestamp: nil, observed_timestamp: nil, severity_text: nil, severity_number: nil, body: nil, attributes: nil, trace_id: nil, span_id: nil, trace_flags: nil, instrumentation_scope: nil, context: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/scout_apm/logging/loggers/opentelemetry/sdk/logs/logger_provider.rb', line 126

def on_emit(timestamp: nil,
            observed_timestamp: nil,
            severity_text: nil,
            severity_number: nil,
            body: nil,
            attributes: nil,
            trace_id: nil,
            span_id: nil,
            trace_flags: nil,
            instrumentation_scope: nil,
            context: nil)

  log_record = LogRecord.new(timestamp: timestamp,
                            observed_timestamp: observed_timestamp,
                            severity_text: severity_text,
                            severity_number: severity_number,
                            body: body,
                            attributes: attributes,
                            trace_id: trace_id,
                            span_id: span_id,
                            trace_flags: trace_flags,
                            resource: @resource,
                            instrumentation_scope: instrumentation_scope)

  @log_record_processors.each { |processor| processor.on_emit(log_record, context) }
end

#shutdown(timeout: nil) ⇒ Integer

Attempts to stop all the activity for this LoggerProvider. Calls ScoutApm::Logging::Loggers::OpenTelemetry::SDK::Logs::LogRecordProcessor#shutdown for all registered ScoutApm::Logging::Loggers::OpenTelemetry::SDK::Logs::LogRecordProcessors.

This operation may block until all log records are processed. Must be called before turning off the main application to ensure all data are processed and exported.

After this is called all newly created ScoutApm::Logging::Loggers::OpenTelemetry::SDK::Logs::LogRecords will be no-op.

Parameters:

  • timeout (optional Numeric) (defaults to: nil)

    An optional timeout in seconds.

Returns:

  • (Integer)

    Export::SUCCESS if no error occurred, Export::FAILURE if a non-specific failure occurred, Export::TIMEOUT if a timeout occurred.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/scout_apm/logging/loggers/opentelemetry/sdk/logs/logger_provider.rb', line 78

def shutdown(timeout: nil)
  @mutex.synchronize do
    if @stopped
      OpenTelemetry.logger.warn('LoggerProvider#shutdown called multiple times.')
      return Export::FAILURE
    end

    start_time = OpenTelemetry::Common::Utilities.timeout_timestamp
    results = @log_record_processors.map do |processor|
      remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time)
      break [Export::TIMEOUT] if remaining_timeout&.zero?

      processor.shutdown(timeout: remaining_timeout)
    end

    @stopped = true
    results.max || Export::SUCCESS
  end
end