Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sentry-ruby/lib/sentry/hub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ def capture_log_event(message, **options)
def capture_metric(name:, type:, value:, unit: nil, attributes: nil, integration: nil)
return unless current_client

# JSON has no NaN or Infinity, and one such value would fail the whole batch
if value.is_a?(Float) && !value.finite?
configuration.log_debug("[Metrics] Dropping metric #{name} with non-finite value #{value}")
return
end

metric = MetricEvent.new(
name: name,
value: value,
Expand Down
5 changes: 3 additions & 2 deletions sentry-ruby/lib/sentry/utils/telemetry_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ def attribute_hash(raw_value)
when Integer
{ value: value, type: "integer" }
when Float
{ value: value, type: "double" }
# JSON has no NaN or Infinity, so those are sent as strings
value.finite? ? { value: value, type: "double" } : { value: value.to_s, type: "string" }
else
begin
{ value: JSON.generate(value), type: "string" }
rescue
{ value: value, type: "string" }
{ value: value.inspect, type: "string" }
end
end

Expand Down
13 changes: 13 additions & 0 deletions sentry-ruby/spec/sentry/log_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@
expect(hash[:attributes]["float_attr"]).to eq({ value: 3.14, type: "double" })
end

it "serializes a non-finite float parameter as a string" do
event = described_class.new(
level: :info,
body: "Ratio is %s",
attributes: { parameters: [Float::NAN] }
)

hash = event.to_h

expect(hash[:attributes]["sentry.message.parameter.0"]).to eq({ value: "NaN", type: "string" })
expect { JSON.generate(hash) }.not_to raise_error
end

it "serializes user attributes correctly" do
# Enable send_default_pii so user attributes are added
Sentry.configuration.send_default_pii = true
Expand Down
25 changes: 25 additions & 0 deletions sentry-ruby/spec/sentry/metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@
end
end

describe "non-finite floats" do
it "sends non-finite float attributes as strings so the envelope can be serialized" do
Sentry.metrics.count("test.counter", attributes: { "nan" => Float::NAN, "inf" => -Float::INFINITY, "list" => [Float::INFINITY] })

Sentry.get_current_client.flush

attributes = sentry_metrics.first[:attributes]

expect(attributes["nan"]).to eq({ type: "string", value: "NaN" })
expect(attributes["inf"]).to eq({ type: "string", value: "-Infinity" })
expect(attributes["list"]).to eq({ type: "string", value: "[Infinity]" })
expect { sentry_envelopes.first.items.first.serialize }.not_to raise_error
end

it "drops a metric with a non-finite value without losing the rest of the batch" do
Sentry.metrics.gauge("test.nan", Float::NAN)
Sentry.metrics.distribution("test.inf", Float::INFINITY)
Sentry.metrics.count("test.counter")

Sentry.get_current_client.flush

expect(sentry_metrics.map { |metric| metric[:name] }).to eq(["test.counter"])
end
end

describe ".gauge" do
it "sends a gauge metric" do
Sentry.metrics.gauge("test.gauge", 42.5)
Expand Down
Loading