-
-
Notifications
You must be signed in to change notification settings - Fork 527
Implement OTLP support #2853
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
Merged
Merged
Implement OTLP support #2853
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
sentry-opentelemetry/lib/sentry/opentelemetry/configuration.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "sentry/opentelemetry/otlp_setup" | ||
|
|
||
| module Sentry | ||
| class Configuration | ||
| # OTLP related configuration. | ||
| # @return [OTLP::Configuration] | ||
| attr_reader :otlp | ||
|
|
||
| after(:initialize) do | ||
| @otlp = OTLP::Configuration.new | ||
| end | ||
|
|
||
| after(:configured) do | ||
| Sentry::OpenTelemetry::OTLPSetup.setup(self) if otlp.enabled | ||
| end | ||
| end | ||
|
|
||
| module OTLP | ||
| class Configuration | ||
| attr_accessor :enabled | ||
| attr_accessor :setup_otlp_traces_exporter | ||
| attr_accessor :setup_propagator | ||
|
|
||
| def initialize | ||
| @enabled = false | ||
| @setup_otlp_traces_exporter = true | ||
| @setup_propagator = true | ||
| end | ||
| end | ||
| end | ||
| end | ||
31 changes: 31 additions & 0 deletions
31
sentry-opentelemetry/lib/sentry/opentelemetry/otlp_propagator.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Sentry | ||
| module OpenTelemetry | ||
| class OTLPPropagator < Propagator | ||
sl0thentr0py marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def inject( | ||
| carrier, | ||
| context: ::OpenTelemetry::Context.current, | ||
| setter: ::OpenTelemetry::Context::Propagation.text_map_setter | ||
| ) | ||
| span_context = ::OpenTelemetry::Trace.current_span(context).context | ||
| return unless span_context.valid? | ||
|
|
||
| setter.set(carrier, SENTRY_TRACE_HEADER_NAME, to_sentry_trace(span_context)) | ||
|
|
||
| baggage = context[SENTRY_BAGGAGE_KEY] | ||
| if baggage.is_a?(Sentry::Baggage) | ||
| baggage_string = baggage.serialize | ||
| setter.set(carrier, BAGGAGE_HEADER_NAME, baggage_string) unless baggage_string&.empty? | ||
| end | ||
sl0thentr0py marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| private | ||
|
|
||
| def to_sentry_trace(span_context) | ||
| sampled = span_context.trace_flags.sampled? ? "1" : "0" | ||
| "#{span_context.hex_trace_id}-#{span_context.hex_span_id}-#{sampled}" | ||
| end | ||
| end | ||
| end | ||
| end | ||
75 changes: 75 additions & 0 deletions
75
sentry-opentelemetry/lib/sentry/opentelemetry/otlp_setup.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # | ||
| require "sentry/opentelemetry/otlp_propagator" | ||
|
|
||
| module Sentry | ||
| module OpenTelemetry | ||
| module OTLPSetup | ||
| USER_AGENT = "sentry-ruby.otlp/#{Sentry::VERSION}" | ||
|
|
||
| class << self | ||
| def setup(config) | ||
| @dsn = config.dsn | ||
| @sdk_logger = config.sdk_logger | ||
| log_debug("[OTLP] Setting up OTLP integration") | ||
|
|
||
| setup_external_propagation_context | ||
| setup_otlp_exporter if config.otlp.setup_otlp_traces_exporter | ||
| setup_sentry_propagator if config.otlp.setup_propagator | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def log_debug(message) | ||
| @sdk_logger&.debug(message) | ||
| end | ||
|
|
||
| def log_warn(message) | ||
| @sdk_logger&.warn(message) | ||
| end | ||
|
|
||
| def setup_external_propagation_context | ||
| log_debug("[OTLP] Setting up trace linking for all events") | ||
|
|
||
| Sentry.register_external_propagation_context do | ||
| span_context = ::OpenTelemetry::Trace.current_span.context | ||
| span_context.valid? ? [span_context.hex_trace_id, span_context.hex_span_id] : nil | ||
| end | ||
| end | ||
|
|
||
| def setup_otlp_exporter | ||
| return unless @dsn | ||
|
|
||
| log_debug("[OTLP] Setting up OTLP exporter") | ||
|
|
||
| begin | ||
| require "opentelemetry/exporter/otlp" | ||
| rescue LoadError | ||
| log_warn("[OTLP] opentelemetry-exporter-otlp gem is not installed. " \ | ||
| "Please add it to your Gemfile to use the OTLP exporter.") | ||
| return | ||
| end | ||
|
|
||
| endpoint = "#{@dsn.server}#{@dsn.otlp_traces_endpoint}" | ||
| auth_header = @dsn.generate_auth_header(client: USER_AGENT) | ||
|
|
||
| log_debug("[OTLP] Sending traces to #{endpoint}") | ||
|
|
||
| exporter = ::OpenTelemetry::Exporter::OTLP::Exporter.new( | ||
| endpoint: endpoint, | ||
| headers: { "X-Sentry-Auth" => auth_header } | ||
| ) | ||
|
|
||
| span_processor = ::OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(exporter) | ||
| ::OpenTelemetry.tracer_provider.add_span_processor(span_processor) | ||
sl0thentr0py marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| def setup_sentry_propagator | ||
| log_debug("[OTLP] Setting up propagator for distributed tracing") | ||
| ::OpenTelemetry.propagation = OTLPPropagator.new | ||
sl0thentr0py marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
30 changes: 30 additions & 0 deletions
30
sentry-opentelemetry/spec/sentry/opentelemetry/configuration_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe Sentry::OTLP::Configuration do | ||
| subject { described_class.new } | ||
|
|
||
| describe "#initialize" do | ||
| it "sets default values" do | ||
| expect(subject.enabled).to eq(false) | ||
| expect(subject.setup_otlp_traces_exporter).to eq(true) | ||
| expect(subject.setup_propagator).to eq(true) | ||
| end | ||
| end | ||
|
|
||
| describe "accessors" do | ||
| it "allows setting enabled" do | ||
| subject.enabled = true | ||
| expect(subject.enabled).to eq(true) | ||
| end | ||
|
|
||
| it "allows setting setup_otlp_traces_exporter" do | ||
| subject.setup_otlp_traces_exporter = false | ||
| expect(subject.setup_otlp_traces_exporter).to eq(false) | ||
| end | ||
|
|
||
| it "allows setting setup_propagator" do | ||
| subject.setup_propagator = false | ||
| expect(subject.setup_propagator).to eq(false) | ||
| end | ||
| end | ||
| end |
145 changes: 145 additions & 0 deletions
145
sentry-opentelemetry/spec/sentry/opentelemetry/otlp_propagator_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
|
|
||
| RSpec.describe Sentry::OpenTelemetry::OTLPPropagator do | ||
| let(:tracer) { ::OpenTelemetry.tracer_provider.tracer('sentry', '1.0') } | ||
|
|
||
| before do | ||
| perform_basic_setup | ||
| perform_otel_setup | ||
| end | ||
|
|
||
| describe '#inject' do | ||
| let(:carrier) { {} } | ||
|
|
||
| it 'noops with invalid span_context' do | ||
| subject.inject(carrier) | ||
| expect(carrier).to eq({}) | ||
| end | ||
|
|
||
| context 'with valid span' do | ||
| it 'sets sentry-trace header on carrier' do | ||
| span = tracer.start_root_span('test') | ||
| ctx = ::OpenTelemetry::Trace.context_with_span(span) | ||
|
|
||
| subject.inject(carrier, context: ctx) | ||
|
|
||
| span_context = span.context | ||
| expected_trace = "#{span_context.hex_trace_id}-#{span_context.hex_span_id}-1" | ||
| expect(carrier['sentry-trace']).to eq(expected_trace) | ||
| end | ||
|
|
||
| it 'sets sampled flag to 0 when not sampled' do | ||
| span = tracer.start_root_span('test') | ||
| ctx = ::OpenTelemetry::Trace.context_with_span(span) | ||
|
|
||
| allow(span.context.trace_flags).to receive(:sampled?).and_return(false) | ||
| subject.inject(carrier, context: ctx) | ||
|
|
||
| span_context = span.context | ||
| expected_trace = "#{span_context.hex_trace_id}-#{span_context.hex_span_id}-0" | ||
| expect(carrier['sentry-trace']).to eq(expected_trace) | ||
| end | ||
| end | ||
|
|
||
| context 'with baggage in context' do | ||
| it 'sets baggage header on carrier' do | ||
| span = tracer.start_root_span('test') | ||
| ctx = ::OpenTelemetry::Trace.context_with_span(span) | ||
|
|
||
| baggage = Sentry::Baggage.new({ | ||
| 'trace_id' => 'abc123', | ||
| 'public_key' => 'key123' | ||
| }) | ||
| ctx = ctx.set_value(described_class::SENTRY_BAGGAGE_KEY, baggage) | ||
|
|
||
| subject.inject(carrier, context: ctx) | ||
|
|
||
| expect(carrier['baggage']).to include('sentry-trace_id=abc123') | ||
| expect(carrier['baggage']).to include('sentry-public_key=key123') | ||
| end | ||
|
|
||
| it 'does not set baggage header when baggage is empty' do | ||
| span = tracer.start_root_span('test') | ||
| ctx = ::OpenTelemetry::Trace.context_with_span(span) | ||
|
|
||
| baggage = Sentry::Baggage.new({}) | ||
| ctx = ctx.set_value(described_class::SENTRY_BAGGAGE_KEY, baggage) | ||
|
|
||
| subject.inject(carrier, context: ctx) | ||
|
|
||
| expect(carrier['baggage']).to be_nil | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe '#extract' do | ||
| let(:ctx) { ::OpenTelemetry::Context.empty } | ||
|
|
||
| it 'returns unchanged context without sentry-trace' do | ||
| carrier = {} | ||
| updated_ctx = subject.extract(carrier, context: ctx) | ||
| expect(updated_ctx).to eq(ctx) | ||
| end | ||
|
|
||
| it 'returns unchanged context with invalid sentry-trace' do | ||
| carrier = { 'sentry-trace' => '000-000-0' } | ||
| updated_ctx = subject.extract(carrier, context: ctx) | ||
| expect(updated_ctx).to eq(ctx) | ||
| end | ||
|
|
||
| context 'with valid sentry-trace header' do | ||
| let(:carrier) do | ||
| { 'sentry-trace' => 'd4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-1' } | ||
| end | ||
|
|
||
| it 'returns context with sentry-trace data' do | ||
| updated_ctx = subject.extract(carrier, context: ctx) | ||
|
|
||
| sentry_trace_data = updated_ctx[described_class::SENTRY_TRACE_KEY] | ||
| expect(sentry_trace_data).not_to be_nil | ||
|
|
||
| trace_id, parent_span_id, parent_sampled = sentry_trace_data | ||
| expect(trace_id).to eq('d4cda95b652f4a1592b449d5929fda1b') | ||
| expect(parent_span_id).to eq('6e0c63257de34c92') | ||
| expect(parent_sampled).to eq(true) | ||
| end | ||
|
|
||
| it 'returns context with correct span_context' do | ||
| updated_ctx = subject.extract(carrier, context: ctx) | ||
|
|
||
| span_context = ::OpenTelemetry::Trace.current_span(updated_ctx).context | ||
| expect(span_context.valid?).to eq(true) | ||
| expect(span_context.hex_trace_id).to eq('d4cda95b652f4a1592b449d5929fda1b') | ||
| expect(span_context.hex_span_id).to eq('6e0c63257de34c92') | ||
| expect(span_context.remote?).to eq(true) | ||
| end | ||
| end | ||
|
|
||
| context 'with sentry-trace and baggage headers' do | ||
| let(:carrier) do | ||
| { | ||
| 'sentry-trace' => 'd4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-1', | ||
| 'baggage' => 'sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b, sentry-public_key=key123' | ||
| } | ||
| end | ||
|
|
||
| it 'returns context with baggage' do | ||
| updated_ctx = subject.extract(carrier, context: ctx) | ||
|
|
||
| baggage = updated_ctx[described_class::SENTRY_BAGGAGE_KEY] | ||
| expect(baggage).to be_a(Sentry::Baggage) | ||
| expect(baggage.mutable).to eq(false) | ||
| expect(baggage.items['trace_id']).to eq('d4cda95b652f4a1592b449d5929fda1b') | ||
| expect(baggage.items['public_key']).to eq('key123') | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe '#fields' do | ||
| it 'returns header names' do | ||
| expect(subject.fields).to eq(['sentry-trace', 'baggage']) | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.