Skip to content

Commit

Permalink
feat: add stats for serialization time in otlp exporter (#1628)
Browse files Browse the repository at this point in the history
Co-authored-by: Francis Bogsanyi <francis.bogsanyi@shopify.com>
  • Loading branch information
plantfansam and fbogsany committed Apr 17, 2024
1 parent a446199 commit 6bae4e1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions exporter/otlp/lib/opentelemetry/exporter/otlp/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def backoff?(retry_count:, reason:, retry_after: nil) # rubocop:disable Metrics/
end

def encode(span_data) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
Opentelemetry::Proto::Collector::Trace::V1::ExportTraceServiceRequest.encode(
Opentelemetry::Proto::Collector::Trace::V1::ExportTraceServiceRequest.new(
resource_spans: span_data
Expand Down Expand Up @@ -298,6 +299,11 @@ def encode(span_data) # rubocop:disable Metrics/MethodLength, Metrics/Cyclomatic
rescue StandardError => e
OpenTelemetry.handle_error(exception: e, message: 'unexpected error in OTLP::Exporter#encode')
nil
ensure
stop = Process.clock_gettime(Process::CLOCK_MONOTONIC)
duration_ms = 1000.0 * (stop - start)
@metrics_reporter.record_value('otel.otlp_exporter.encode_duration',
value: duration_ms)
end

def as_otlp_span(span_data) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
Expand Down
11 changes: 11 additions & 0 deletions exporter/otlp/test/opentelemetry/exporter/otlp/exporter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,17 @@
_(result).must_equal(SUCCESS)
end

it 'records metrics' do
metrics_reporter = Minitest::Mock.new
exporter = OpenTelemetry::Exporter::OTLP::Exporter.new(metrics_reporter: metrics_reporter)
stub_request(:post, 'http://localhost:4318/v1/traces').to_timeout.then.to_return(status: 200)
metrics_reporter.expect(:record_value, nil) { |m, _, _| m == 'otel.otlp_exporter.encode_duration' }
metrics_reporter.expect(:record_value, nil) { |m, _, _| m == 'otel.otlp_exporter.message.uncompressed_size' }
metrics_reporter.expect(:record_value, nil) { |m, _, _| m == 'otel.otlp_exporter.message.compressed_size' }
metrics_reporter.expect(:add_to_counter, nil) { |m, _, _| m == 'otel.otlp_exporter.failure' }
exporter.export([OpenTelemetry::TestHelpers.create_span_data])
end

it 'retries on timeout' do
stub_request(:post, 'http://localhost:4318/v1/traces').to_timeout.then.to_return(status: 200)
span_data = OpenTelemetry::TestHelpers.create_span_data
Expand Down

0 comments on commit 6bae4e1

Please sign in to comment.