From 6fdde01bbdb2984e9607b8e650de5068c2709916 Mon Sep 17 00:00:00 2001 From: st0012 Date: Sat, 2 Jul 2022 15:22:05 +0100 Subject: [PATCH] Support sending measurements via TransactionEvent --- sentry-ruby/lib/sentry/event.rb | 1 + sentry-ruby/lib/sentry/transaction_event.rb | 8 +++++++ sentry-ruby/spec/sentry/transaction_spec.rb | 26 +++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/sentry-ruby/lib/sentry/event.rb b/sentry-ruby/lib/sentry/event.rb index 0a76f3f4a..187c50547 100644 --- a/sentry-ruby/lib/sentry/event.rb +++ b/sentry-ruby/lib/sentry/event.rb @@ -66,6 +66,7 @@ def initialize(configuration:, integration_meta: nil, message: nil) @trusted_proxies = configuration.trusted_proxies @stacktrace_builder = configuration.stacktrace_builder @rack_env_whitelist = configuration.rack_env_whitelist + @custom_measurements = configuration.custom_measurements @message = (message || "").byteslice(0..MAX_MESSAGE_SIZE_IN_BYTES) end diff --git a/sentry-ruby/lib/sentry/transaction_event.rb b/sentry-ruby/lib/sentry/transaction_event.rb index 389ca45b0..086cacc2c 100644 --- a/sentry-ruby/lib/sentry/transaction_event.rb +++ b/sentry-ruby/lib/sentry/transaction_event.rb @@ -11,6 +11,9 @@ class TransactionEvent < Event # @return [Hash, nil] attr_accessor :dynamic_sampling_context + # @return [Hash] + attr_accessor :measurements + # @return [Float, nil] attr_reader :start_timestamp @@ -26,6 +29,10 @@ def initialize(transaction:, **options) self.tags = transaction.tags self.dynamic_sampling_context = transaction.get_baggage.dynamic_sampling_context + if @custom_measurements + self.measurements = transaction.measurements + end + finished_spans = transaction.span_recorder.spans.select { |span| span.timestamp && span != transaction } self.spans = finished_spans.map(&:to_hash) end @@ -42,6 +49,7 @@ def to_hash data = super data[:spans] = @spans.map(&:to_hash) if @spans data[:start_timestamp] = @start_timestamp + data[:measurements] = @measurements data end end diff --git a/sentry-ruby/spec/sentry/transaction_spec.rb b/sentry-ruby/spec/sentry/transaction_spec.rb index 409df3e7a..31157fd58 100644 --- a/sentry-ruby/spec/sentry/transaction_spec.rb +++ b/sentry-ruby/spec/sentry/transaction_spec.rb @@ -507,6 +507,32 @@ expect(subject.name).to eq("") end end + + context "config.custom_measurements = true" do + before do + Sentry.configuration.custom_measurements = true + end + + it "adds measurements the event" do + subject.set_measurement("metric.foo", 0.5, "second") + subject.finish + + transaction = events.last.to_hash + expect(transaction[:measurements]).to eq( + { "metric.foo" => { value: 0.5, unit: "second" } } + ) + end + end + + context "config.custom_measurements = false" do + it "adds measurements the event" do + subject.set_measurement("metric.foo", 0.5, "second") + subject.finish + + transaction = events.last.to_hash + expect(transaction[:measurements]).to eq(nil) + end + end end describe "#get_baggage" do