Skip to content

Commit 8694506

Browse files
dingsdaxclaude
andcommitted
fix(yabeda): Normalize plural Yabeda units to Sentry's singular form
Yabeda plugins declare units as plural Ruby symbols (:seconds, :milliseconds, :bytes) while Sentry's canonical unit format is singular (second, millisecond, byte). Without normalization, distribution metrics with units are stored under the wrong unit string, causing dashboard queries to return empty results. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 581c12e commit 8694506

3 files changed

Lines changed: 51 additions & 8 deletions

File tree

sentry-yabeda/lib/sentry/yabeda/adapter.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,8 @@ def metric_name(metric)
6868
[metric.group, metric.name].compact.join(".")
6969
end
7070

71-
# TODO: Normalize Yabeda unit symbols (e.g. :milliseconds) to Sentry's
72-
# canonical singular strings (e.g. "millisecond") once units are visible
73-
# in the Sentry product. See https://develop.sentry.dev/sdk/foundations/state-management/scopes/attributes/#units
7471
def unit_for(metric)
75-
metric.unit&.to_s
72+
metric.unit&.to_s&.chomp("s")
7673
end
7774
end
7875
end

sentry-yabeda/spec/sentry/yabeda/adapter_spec.rb

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def build_metric(type, name:, group: nil, unit: nil)
8585
expect(Sentry.metrics).to receive(:gauge).with(
8686
"process.memory_usage",
8787
1024,
88-
unit: "bytes",
88+
unit: "byte",
8989
attributes: nil
9090
)
9191

@@ -101,7 +101,7 @@ def build_metric(type, name:, group: nil, unit: nil)
101101
expect(Sentry.metrics).to receive(:distribution).with(
102102
"rails.request_duration",
103103
150.5,
104-
unit: "milliseconds",
104+
unit: "millisecond",
105105
attributes: tags
106106
)
107107

@@ -117,14 +117,60 @@ def build_metric(type, name:, group: nil, unit: nil)
117117
expect(Sentry.metrics).to receive(:distribution).with(
118118
"http.response_size",
119119
2048,
120-
unit: "bytes",
120+
unit: "byte",
121121
attributes: tags
122122
)
123123

124124
adapter.perform_summary_observe!(summary, tags, 2048)
125125
end
126126
end
127127

128+
describe "unit normalization" do
129+
it "converts plural yabeda units to Sentry's singular form" do
130+
perform_basic_setup
131+
132+
histogram = build_metric(:histogram, name: :duration, group: :rails, unit: :seconds)
133+
expect(Sentry.metrics).to receive(:distribution).with(
134+
"rails.duration", 1.5, unit: "second", attributes: nil
135+
)
136+
137+
adapter.perform_histogram_measure!(histogram, {}, 1.5)
138+
end
139+
140+
it "converts milliseconds to millisecond" do
141+
perform_basic_setup
142+
143+
histogram = build_metric(:histogram, name: :latency, unit: :milliseconds)
144+
expect(Sentry.metrics).to receive(:distribution).with(
145+
"latency", 250.0, unit: "millisecond", attributes: nil
146+
)
147+
148+
adapter.perform_histogram_measure!(histogram, {}, 250.0)
149+
end
150+
151+
it "passes nil when unit is not set" do
152+
perform_basic_setup
153+
154+
gauge = build_metric(:gauge, name: :threads)
155+
expect(Sentry.metrics).to receive(:gauge).with(
156+
"threads", 5, unit: nil, attributes: nil
157+
)
158+
159+
adapter.perform_gauge_set!(gauge, {}, 5)
160+
end
161+
162+
it "leaves already-singular units unchanged" do
163+
perform_basic_setup
164+
165+
gauge = build_metric(:gauge, name: :uptime, unit: :second)
166+
expect(Sentry.metrics).to receive(:gauge).with(
167+
"uptime", 3600, unit: "second", attributes: nil
168+
)
169+
170+
adapter.perform_gauge_set!(gauge, {}, 3600)
171+
end
172+
end
173+
128174
describe "registration methods (no-ops)" do
129175
it "accepts register_counter! without error" do
130176
expect { adapter.register_counter!(double) }.not_to raise_error

sentry-yabeda/spec/sentry/yabeda/integration_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
expect(metric[:name]).to eq("myapp.response_time")
7979
expect(metric[:type]).to eq(:distribution)
8080
expect(metric[:value]).to eq(150.5)
81-
expect(metric[:unit]).to eq("milliseconds")
81+
expect(metric[:unit]).to eq("millisecond")
8282
expect(metric[:attributes][:controller]).to eq({ type: "string", value: "orders" })
8383
expect(metric[:attributes][:action]).to eq({ type: "string", value: "index" })
8484
end

0 commit comments

Comments
 (0)