diff --git a/sentry-yabeda/lib/sentry/yabeda/adapter.rb b/sentry-yabeda/lib/sentry/yabeda/adapter.rb index 114fc0a5c..fb67308d6 100644 --- a/sentry-yabeda/lib/sentry/yabeda/adapter.rb +++ b/sentry-yabeda/lib/sentry/yabeda/adapter.rb @@ -68,11 +68,8 @@ def metric_name(metric) [metric.group, metric.name].compact.join(".") end - # TODO: Normalize Yabeda unit symbols (e.g. :milliseconds) to Sentry's - # canonical singular strings (e.g. "millisecond") once units are visible - # in the Sentry product. See https://develop.sentry.dev/sdk/foundations/state-management/scopes/attributes/#units def unit_for(metric) - metric.unit&.to_s + metric.unit&.to_s&.chomp("s") end end end diff --git a/sentry-yabeda/spec/sentry/yabeda/adapter_spec.rb b/sentry-yabeda/spec/sentry/yabeda/adapter_spec.rb index 70ba42e46..ea7b02890 100644 --- a/sentry-yabeda/spec/sentry/yabeda/adapter_spec.rb +++ b/sentry-yabeda/spec/sentry/yabeda/adapter_spec.rb @@ -85,7 +85,7 @@ def build_metric(type, name:, group: nil, unit: nil) expect(Sentry.metrics).to receive(:gauge).with( "process.memory_usage", 1024, - unit: "bytes", + unit: "byte", attributes: nil ) @@ -101,7 +101,7 @@ def build_metric(type, name:, group: nil, unit: nil) expect(Sentry.metrics).to receive(:distribution).with( "rails.request_duration", 150.5, - unit: "milliseconds", + unit: "millisecond", attributes: tags ) @@ -117,7 +117,7 @@ def build_metric(type, name:, group: nil, unit: nil) expect(Sentry.metrics).to receive(:distribution).with( "http.response_size", 2048, - unit: "bytes", + unit: "byte", attributes: tags ) @@ -125,6 +125,52 @@ def build_metric(type, name:, group: nil, unit: nil) end end + describe "unit normalization" do + it "converts plural yabeda units to Sentry's singular form" do + perform_basic_setup + + histogram = build_metric(:histogram, name: :duration, group: :rails, unit: :seconds) + expect(Sentry.metrics).to receive(:distribution).with( + "rails.duration", 1.5, unit: "second", attributes: nil + ) + + adapter.perform_histogram_measure!(histogram, {}, 1.5) + end + + it "converts milliseconds to millisecond" do + perform_basic_setup + + histogram = build_metric(:histogram, name: :latency, unit: :milliseconds) + expect(Sentry.metrics).to receive(:distribution).with( + "latency", 250.0, unit: "millisecond", attributes: nil + ) + + adapter.perform_histogram_measure!(histogram, {}, 250.0) + end + + it "passes nil when unit is not set" do + perform_basic_setup + + gauge = build_metric(:gauge, name: :threads) + expect(Sentry.metrics).to receive(:gauge).with( + "threads", 5, unit: nil, attributes: nil + ) + + adapter.perform_gauge_set!(gauge, {}, 5) + end + + it "leaves already-singular units unchanged" do + perform_basic_setup + + gauge = build_metric(:gauge, name: :uptime, unit: :second) + expect(Sentry.metrics).to receive(:gauge).with( + "uptime", 3600, unit: "second", attributes: nil + ) + + adapter.perform_gauge_set!(gauge, {}, 3600) + end + end + describe "registration methods (no-ops)" do it "accepts register_counter! without error" do expect { adapter.register_counter!(double) }.not_to raise_error diff --git a/sentry-yabeda/spec/sentry/yabeda/integration_spec.rb b/sentry-yabeda/spec/sentry/yabeda/integration_spec.rb index 5ae59654a..d90222977 100644 --- a/sentry-yabeda/spec/sentry/yabeda/integration_spec.rb +++ b/sentry-yabeda/spec/sentry/yabeda/integration_spec.rb @@ -78,7 +78,7 @@ expect(metric[:name]).to eq("myapp.response_time") expect(metric[:type]).to eq(:distribution) expect(metric[:value]).to eq(150.5) - expect(metric[:unit]).to eq("milliseconds") + expect(metric[:unit]).to eq("millisecond") expect(metric[:attributes][:controller]).to eq({ type: "string", value: "orders" }) expect(metric[:attributes][:action]).to eq({ type: "string", value: "index" }) end