Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record stats for serialization time #1628

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would y'all feel about adding a helper that accepts a block:

@metrics_reporter.record_duration(name, labels: labels) do
   # your code we want to measure goes here
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do this one more time, it's worth a refactor. I am not super keen on the block-based approach if all our callsites are full methods. I think I'd prefer:

def foo
  probe = start_probe('otel.otlp_exporter.foo')
  ...
ensure
  probe.finish
end

over:

def foo
  @metrics_reporter.record_duration('otel.otlp_exporter.foo') do
    ...
  end
end

simply because it avoids deeper indentation as we add instrumentation to existing methods. The latter is better for measurements of small chunks of code within a larger method.

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
Copy link
Contributor Author

@plantfansam plantfansam Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of mocking the metrics reporter, we could move InMemoryMetricsReporter from https://github.com/open-telemetry/opentelemetry-ruby/blob/2bcb4643b144300b0e18cba6ae61e93eb75bc5b0/exporter/zipkin/test/test_helper.rb#L43-L42 to opentelemetry-test-helpers if reviewers would like.

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
Loading