Class: OpenTelemetry::SDK::Logs::LoggerProvider

Inherits:
Logs::LoggerProvider
  • Object
show all
Defined in:
lib/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, log_record_limits: LogRecordLimits::DEFAULT) ⇒ OpenTelemetry::SDK::Logs::LoggerProvider

Returns a new LoggerProvider instance.

Parameters:

  • resource (optional Resource) (defaults to: OpenTelemetry::SDK::Resources::Resource.create)

    The resource to associate with new LogRecords created by OpenTelemetry::SDK::Logs::Loggers created by this LoggerProvider.

  • log_record_limits (optional LogRecordLimits) (defaults to: LogRecordLimits::DEFAULT)

    The limits for attributes count and attribute length for LogRecords.



28
29
30
31
32
33
34
35
36
# File 'lib/opentelemetry/sdk/logs/logger_provider.rb', line 28

def initialize(resource: OpenTelemetry::SDK::Resources::Resource.create, log_record_limits: LogRecordLimits::DEFAULT)
  @log_record_processors = []
  @log_record_limits = log_record_limits
  @mutex = Mutex.new
  @resource = resource
  @stopped = false
  @registry = {}
  @registry_mutex = Mutex.new
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:



62
63
64
65
66
67
68
69
70
71
# File 'lib/opentelemetry/sdk/logs/logger_provider.rb', line 62

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 OpenTelemetry::SDK::Logs::LogRecords that have not yet been exported for all the registered 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 OpenTelemetry::SDK::Logs::LogRecordProcessor exports the completed 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.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/opentelemetry/sdk/logs/logger_provider.rb', line 116

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

Returns an OpenTelemetry::SDK::Logs::Logger instance.

Parameters:

  • name (String)

    Instrumentation package name

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

    Instrumentation package version

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/opentelemetry/sdk/logs/logger_provider.rb', line 44

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

  @registry_mutex.synchronize do
    @registry[Key.new(name, version)] ||= Logger.new(name, version, self)
  end
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.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/opentelemetry/sdk/logs/logger_provider.rb', line 133

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)
  return if @stopped

  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_limits: @log_record_limits)

  @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 OpenTelemetry::SDK::Logs::LogRecordProcessor#shutdown for all registered 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 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.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/opentelemetry/sdk/logs/logger_provider.rb', line 85

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