Class: ScoutApm::Logging::Loggers::OpenTelemetry::Exporter::OTLP::LogsExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/logging/loggers/opentelemetry/exporter/exporter/otlp/logs_exporter.rb

Overview

An OpenTelemetry log exporter that sends log records over HTTP as Protobuf encoded OTLP ExportLogsServiceRequests.

Constant Summary collapse

DEFAULT_USER_AGENT =
"OTel-OTLP-Exporter-Ruby/#{OpenTelemetry::Exporter::OTLP::VERSION} Ruby/#{RUBY_VERSION} (#{RUBY_PLATFORM}; #{RUBY_ENGINE}/#{RUBY_ENGINE_VERSION})".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint: ::OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_LOGS_ENDPOINT', 'OTEL_EXPORTER_OTLP_ENDPOINT', default: 'http://localhost:4318/v1/logs'), certificate_file: ::OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE', 'OTEL_EXPORTER_OTLP_CERTIFICATE'), ssl_verify_mode: LogsExporter.ssl_verify_mode, headers: ::OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_LOGS_HEADERS', 'OTEL_EXPORTER_OTLP_HEADERS', default: {}), compression: ::OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_LOGS_COMPRESSION', 'OTEL_EXPORTER_OTLP_COMPRESSION', default: 'gzip'), timeout: ::OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_LOGS_TIMEOUT', 'OTEL_EXPORTER_OTLP_TIMEOUT', default: 10)) ⇒ LogsExporter

Returns a new instance of LogsExporter.

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/scout_apm/logging/loggers/opentelemetry/exporter/exporter/otlp/logs_exporter.rb', line 52

def initialize(endpoint: ::OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_LOGS_ENDPOINT', 'OTEL_EXPORTER_OTLP_ENDPOINT', default: 'http://localhost:4318/v1/logs'),
              certificate_file: ::OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_LOGS_CERTIFICATE', 'OTEL_EXPORTER_OTLP_CERTIFICATE'),
              ssl_verify_mode: LogsExporter.ssl_verify_mode,
              headers: ::OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_LOGS_HEADERS', 'OTEL_EXPORTER_OTLP_HEADERS', default: {}),
              compression: ::OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_LOGS_COMPRESSION', 'OTEL_EXPORTER_OTLP_COMPRESSION', default: 'gzip'),
              timeout: ::OpenTelemetry::Common::Utilities.config_opt('OTEL_EXPORTER_OTLP_LOGS_TIMEOUT', 'OTEL_EXPORTER_OTLP_TIMEOUT', default: 10))
  raise ArgumentError, "invalid url for OTLP::Exporter #{endpoint}" unless ::OpenTelemetry::Common::Utilities.valid_url?(endpoint)
  raise ArgumentError, "unsupported compression key #{compression}" unless compression.nil? || %w[gzip none].include?(compression)

  @uri = if endpoint == ENV['OTEL_EXPORTER_OTLP_ENDPOINT']
          URI.join(endpoint, 'v1/logs')
        else
          URI(endpoint)
        end

  @http = http_connection(@uri, ssl_verify_mode, certificate_file)

  @path = @uri.path
  @headers = prepare_headers(headers)
  @timeout = timeout.to_f
  @compression = compression
  @shutdown = false
end

Class Method Details

.ssl_verify_modeObject



42
43
44
45
46
47
48
49
50
# File 'lib/scout_apm/logging/loggers/opentelemetry/exporter/exporter/otlp/logs_exporter.rb', line 42

def self.ssl_verify_mode
  if ENV.key?('OTEL_RUBY_EXPORTER_OTLP_SSL_VERIFY_PEER')
    OpenSSL::SSL::VERIFY_PEER
  elsif ENV.key?('OTEL_RUBY_EXPORTER_OTLP_SSL_VERIFY_NONE')
    OpenSSL::SSL::VERIFY_NONE
  else
    OpenSSL::SSL::VERIFY_PEER
  end
end

Instance Method Details

#export(log_record_data, timeout: nil) ⇒ Integer

Called to export sampled SDK::Logs::LogRecordData structs.

Parameters:

Returns:

  • (Integer)

    the result of the export.



83
84
85
86
87
88
# File 'lib/scout_apm/logging/loggers/opentelemetry/exporter/exporter/otlp/logs_exporter.rb', line 83

def export(log_record_data, timeout: nil)
  OpenTelemetry.logger.error('Logs Exporter tried to export, but it has already shut down') if @shutdown
  return FAILURE if @shutdown

  send_bytes(encode(log_record_data), timeout: timeout)
end

#force_flush(timeout: nil) ⇒ Object

Called when SDK::Logs::LoggerProvider#force_flush is called, if this exporter is registered to a SDK::Logs::LoggerProvider object.

Parameters:

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

    An optional timeout in seconds.



95
96
97
# File 'lib/scout_apm/logging/loggers/opentelemetry/exporter/exporter/otlp/logs_exporter.rb', line 95

def force_flush(timeout: nil)
  SUCCESS
end

#shutdown(timeout: nil) ⇒ Object

Called when SDK::Logs::LoggerProvider#shutdown is called, if this exporter is registered to a SDK::Logs::LoggerProvider object.

Parameters:

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

    An optional timeout in seconds.



104
105
106
107
108
# File 'lib/scout_apm/logging/loggers/opentelemetry/exporter/exporter/otlp/logs_exporter.rb', line 104

def shutdown(timeout: nil)
  @shutdown = true
  @http.finish if @http.started?
  SUCCESS
end