Skip to content

Commit

Permalink
Format Date and Time objects stored in sample data
Browse files Browse the repository at this point in the history
Format the Date and Time objects in a human-friendly way. Previously,
dates and times stored in sample data, like session data, would be shown
as `#<Date>` and `#<Time>`.

They will now be show as `#<Date: 2024-09-11>` and
`#<Time: Time: 2024-09-12T13:14:15+02:00>`.
(The time zone will be based on the setting in Ruby.)

The UTC offset is included in the Time object on purpose. Casting it to
UTC will lose the timezone information.
  • Loading branch information
tombruijn committed Sep 12, 2024
1 parent 0b41476 commit ceb2f0e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: patch
type: change
---

Format the Date and Time objects in a human-friendly way. Previously, dates and times stored in sample data, like session data, would be shown as `#<Date>` and `#<Time>`. Now they will show as `#<Date: 2024-09-11>` and `#<Time: Time: 2024-09-12T13:14:15+02:00>` (UTC offset may be different for your time objects depending on the server setting).
4 changes: 4 additions & 0 deletions lib/appsignal/utils/hash_sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def sanitize_value(value, filter_keys, seen)
sanitize_array(value, filter_keys, seen)
when TrueClass, FalseClass, NilClass, Integer, String, Symbol, Float
unmodified(value)
when Time
"#<Time: #{value.iso8601}>"
when Date
"#<Date: #{value.iso8601}>"
else
inspected(value)
end
Expand Down
11 changes: 11 additions & 0 deletions spec/lib/appsignal/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,17 @@
expect(transaction).to include_session_data(block_data)
end

it "adds certain Ruby objects as Strings" do
transaction.add_session_data("time" => Time.utc(2024, 9, 12, 13, 14, 15))
transaction.add_session_data("date" => Date.new(2024, 9, 11))

transaction._sample
expect(transaction).to include_session_data(
"time" => "#<Time: 2024-09-12T13:14:15Z>",
"date" => "#<Date: 2024-09-11>"
)
end

it "logs an error if an error occurred storing the session data" do
transaction.add_session_data { raise "uh oh" }

Expand Down
18 changes: 16 additions & 2 deletions spec/lib/appsignal/utils/hash_sanitizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:float => 0.0,
:bool_true => true,
:bool_false => false,
:date => Date.new(2024, 9, 11),
# Non-recursive appearances of the same array instance
:some_arrays => [some_array, some_array],
# Non-recursive appearances of the same hash instance
Expand Down Expand Up @@ -57,12 +58,12 @@
expect(subject[:float]).to eq(0.0)
expect(subject[:bool_true]).to be(true)
expect(subject[:bool_false]).to be(false)
expect(subject[:date]).to eq("#<Date: 2024-09-11>")
expect(subject[:nil]).to be_nil
expect(subject[:int]).to eq(1)
expect(subject[:int64]).to eq(1 << 64)
expect(subject[:some_arrays]).to eq([[1, 2, 3], [1, 2, 3]])
expect(subject[:some_hashes]).to eq({ :a => { :a => 1, :b => 2 },
:b => { :a => 1, :b => 2 } })
expect(subject[:some_hashes]).to eq(:a => { :a => 1, :b => 2 }, :b => { :a => 1, :b => 2 })
end

it "does not change the original params" do
Expand All @@ -71,6 +72,19 @@
expect(params[:hash][:nested_array][2]).to eq(file)
end

describe "time objects" do
it "returns a hash with normalized Time objects" do
args = {
:time_in_utc => Time.utc(2024, 9, 12, 13, 14, 15),
:time_with_timezone => Time.new(2024, 9, 12, 13, 14, 15, "+0900")
}
expect(described_class.sanitize(args)).to eq(
:time_in_utc => "#<Time: 2024-09-12T13:14:15Z>",
:time_with_timezone => "#<Time: 2024-09-12T13:14:15+09:00>"
)
end
end

describe ":hash key" do
subject { sanitized_params[:hash] }

Expand Down

0 comments on commit ceb2f0e

Please sign in to comment.