Skip to content

Commit

Permalink
Support sending measurements via TransactionEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Feb 2, 2023
1 parent f9f2912 commit 6fdde01
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions sentry-ruby/lib/sentry/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions sentry-ruby/lib/sentry/transaction_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
26 changes: 26 additions & 0 deletions sentry-ruby/spec/sentry/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,32 @@
expect(subject.name).to eq("<unlabeled transaction>")
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
Expand Down

0 comments on commit 6fdde01

Please sign in to comment.