Skip to content

Commit c19e09f

Browse files
committed
Add Transaction#set_measurement
1 parent 27e2099 commit c19e09f

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

sentry-ruby/lib/sentry/transaction.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class Transaction < Span
2222
# @return [String]
2323
attr_reader :parent_sampled
2424

25+
# The measurements added to the transaction.
26+
# @return [Hash]
27+
attr_reader :measurements
28+
2529
# @deprecated Use Sentry.get_current_hub instead.
2630
attr_reader :hub
2731

@@ -40,9 +44,11 @@ def initialize(name: nil, parent_sampled: nil, hub:, **options)
4044
@hub = hub
4145
@configuration = hub.configuration # to be removed
4246
@tracing_enabled = hub.configuration.tracing_enabled?
47+
@custom_measurements_enabled = hub.configuration.experiments.custom_measurements
4348
@traces_sampler = hub.configuration.traces_sampler
4449
@traces_sample_rate = hub.configuration.traces_sample_rate
4550
@logger = hub.configuration.logger
51+
@measurements = {}
4652
init_span_recorder
4753
end
4854

@@ -94,6 +100,20 @@ def deep_dup
94100
copy
95101
end
96102

103+
# Sets a custom measurement on the transaction.
104+
# @param name [String] name of the measurement
105+
# @param value [Float] value of the measurement
106+
# @param unit [String] unit of the measurement
107+
# @return [void]
108+
def set_measurement(name, value, unit = "")
109+
unless @custom_measurements_enabled
110+
log_debug("[Tracing] Experimental custom_measurements feature is disabled")
111+
return
112+
end
113+
114+
@measurements[name] = { value: value, unit: unit }
115+
end
116+
97117
# Sets initial sampling decision of the transaction.
98118
# @param sampling_context [Hash] a context Hash that'll be passed to `traces_sampler` (if provided).
99119
# @return [void]

sentry-ruby/spec/sentry/transaction_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,50 @@
121121
end
122122
end
123123

124+
describe "#set_measurement" do
125+
context "when config.experiments.custom_measurements = false" do
126+
let(:string_io) { StringIO.new }
127+
128+
before { Sentry.configuration.logger = Logger.new(string_io) }
129+
130+
it "logs the warning message" do
131+
subject.set_measurement("metric.foo", 0.1, "second")
132+
133+
expect(string_io.string).to include("[Tracing] Experimental custom_measurements feature is disabled")
134+
end
135+
end
136+
137+
context "when config.experiments.custom_measurements = true" do
138+
before do
139+
Sentry.configuration.experiments.custom_measurements = true
140+
end
141+
142+
it "sets the measurement" do
143+
subject.set_measurement("metric.foo", 0.1, "second")
144+
subject.set_measurement("metric.bar", 1.0, "minute")
145+
subject.set_measurement("metric.baz", 1.0)
146+
147+
expect(subject.measurements).to eq(
148+
{
149+
"metric.foo" => { value: 0.1, unit: "second" },
150+
"metric.bar" => { value: 1.0, unit: "minute" },
151+
"metric.baz" => { value: 1.0, unit: "" },
152+
}
153+
)
154+
155+
subject.set_measurement("metric.foo", 2, "second")
156+
157+
expect(subject.measurements).to eq(
158+
{
159+
"metric.foo" => { value: 2, unit: "second" },
160+
"metric.bar" => { value: 1.0, unit: "minute" },
161+
"metric.baz" => { value: 1.0, unit: "" },
162+
}
163+
)
164+
end
165+
end
166+
end
167+
124168
describe "#start_child" do
125169
it "initializes a new child Span and assigns the 'transaction' attribute with itself" do
126170
# create subject span and wait for a sec for making time difference

0 commit comments

Comments
 (0)