Class: OpenTelemetry::Exporter::OTLP::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/exporter/otlp/exporter.rb

Overview

An OpenTelemetry trace exporter that sends spans over HTTP as Protobuf encoded OTLP ExportTraceServiceRequests.

Instance Method Summary collapse

Constructor Details

#initialize(endpoint: config_opt('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', 'OTEL_EXPORTER_OTLP_ENDPOINT', default: 'https://localhost:4317/v1/traces'), certificate_file: config_opt('OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE', 'OTEL_EXPORTER_OTLP_CERTIFICATE'), headers: config_opt('OTEL_EXPORTER_OTLP_TRACES_HEADERS', 'OTEL_EXPORTER_OTLP_HEADERS'), compression: config_opt('OTEL_EXPORTER_OTLP_TRACES_COMPRESSION', 'OTEL_EXPORTER_OTLP_COMPRESSION'), timeout: config_opt('OTEL_EXPORTER_OTLP_TRACES_TIMEOUT', 'OTEL_EXPORTER_OTLP_TIMEOUT', default: 10), metrics_reporter: nil) ⇒ Exporter

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/opentelemetry/exporter/otlp/exporter.rb', line 34

def initialize(endpoint: config_opt('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', 'OTEL_EXPORTER_OTLP_ENDPOINT', default: 'https://localhost:4317/v1/traces'), # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
               certificate_file: config_opt('OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE', 'OTEL_EXPORTER_OTLP_CERTIFICATE'),
               headers: config_opt('OTEL_EXPORTER_OTLP_TRACES_HEADERS', 'OTEL_EXPORTER_OTLP_HEADERS'),
               compression: config_opt('OTEL_EXPORTER_OTLP_TRACES_COMPRESSION', 'OTEL_EXPORTER_OTLP_COMPRESSION'),
               timeout: config_opt('OTEL_EXPORTER_OTLP_TRACES_TIMEOUT', 'OTEL_EXPORTER_OTLP_TIMEOUT', default: 10),
               metrics_reporter: nil)
  raise ArgumentError, "invalid url for OTLP::Exporter #{endpoint}" if invalid_url?(endpoint)
  raise ArgumentError, "unsupported compression key #{compression}" unless compression.nil? || compression == 'gzip'
  raise ArgumentError, 'headers must be comma-separated k=v pairs or a Hash' unless valid_headers?(headers)

  @uri = if endpoint == ENV['OTEL_EXPORTER_OTLP_ENDPOINT']
           URI("#{endpoint}/v1/traces")
         else
           URI(endpoint)
         end

  @http = Net::HTTP.new(@uri.host, @uri.port)
  @http.use_ssl = @uri.scheme == 'https'
  @http.ca_file = certificate_file unless certificate_file.nil?
  @http.keep_alive_timeout = KEEP_ALIVE_TIMEOUT

  @path = @uri.path
  @headers = case headers
             when String then CSV.parse(headers, col_sep: '=', row_sep: ',').to_h
             when Hash then headers
             end
  @timeout = timeout.to_f
  @compression = compression
  @metrics_reporter = metrics_reporter || OpenTelemetry::SDK::Trace::Export::MetricsReporter
  @shutdown = false
end

Instance Method Details

#export(span_data, timeout: nil) ⇒ Integer

Called to export sampled SDK::Trace::SpanData structs.

Parameters:

  • span_data (Enumerable<OpenTelemetry::SDK::Trace::SpanData>)

    the list of recorded SDK::Trace::SpanData structs to be exported.

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

    An optional timeout in seconds.

Returns:

  • (Integer)

    the result of the export.



73
74
75
76
77
# File 'lib/opentelemetry/exporter/otlp/exporter.rb', line 73

def export(span_data, timeout: nil)
  return FAILURE if @shutdown

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

#force_flush(timeout: nil) ⇒ Object

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

Parameters:

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

    An optional timeout in seconds.



84
85
86
# File 'lib/opentelemetry/exporter/otlp/exporter.rb', line 84

def force_flush(timeout: nil)
  SUCCESS
end

#shutdown(timeout: nil) ⇒ Object

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

Parameters:

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

    An optional timeout in seconds.



93
94
95
96
97
# File 'lib/opentelemetry/exporter/otlp/exporter.rb', line 93

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