Skip to content

Commit

Permalink
ar serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
p committed Oct 28, 2024
1 parent 547b94d commit ca1eb13
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
6 changes: 4 additions & 2 deletions lib/datadog/di/contrib/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Datadog::DI::Serializer.register(condition: lambda { |value| ActiveRecord::Base === value }) \
do |serializer, value, name:, depth:|
serializer.type_serialized_entry(value.class,
serializer.serialize_value(value.attributes, depth: depth))
value_to_serialize = {
attributes: value.attributes,
}
serializer.serialize_value(value_to_serialize, depth: depth ? depth - 1 : nil, type: value.class)
end
4 changes: 2 additions & 2 deletions lib/datadog/di/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def type_serialized_entry(cls, serialized)
# (integers, strings, arrays, hashes).
#
# Respects string length, collection size and traversal depth limits.
def serialize_value(value, name: nil, depth: settings.dynamic_instrumentation.max_capture_depth)
cls = value.class
def serialize_value(value, name: nil, depth: settings.dynamic_instrumentation.max_capture_depth, type: nil)
cls = type || value.class

if redactor.redact_type?(value)
return {type: class_name(cls), notCapturedReason: "redactedType"}
Expand Down
41 changes: 38 additions & 3 deletions spec/datadog/di/serializer_rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
require 'sqlite3'
require "datadog/di/contrib/active_record"

class SerializerRailsSpecTestModel < ActiveRecord::Base
class SerializerRailsSpecTestEmptyModel < ActiveRecord::Base
end

class SerializerRailsSpecTestBasicModel < ActiveRecord::Base
end

RSpec.describe Datadog::DI::Serializer do
Expand All @@ -26,7 +29,10 @@ class SerializerRailsSpecTestModel < ActiveRecord::Base
ActiveRecord::Base.establish_connection('sqlite3::memory:')

ActiveRecord::Schema.define(version: 20161003090450) do
create_table 'serializer_rails_spec_test_models', force: :cascade do |t|
create_table 'serializer_rails_spec_test_empty_models', force: :cascade do |t|
end

create_table 'serializer_rails_spec_test_basic_models', force: :cascade do |t|
t.string 'title'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
Expand Down Expand Up @@ -55,7 +61,36 @@ class SerializerRailsSpecTestModel < ActiveRecord::Base
end

cases = [
{name: "AR model", input: -> { SerializerRailsSpecTestModel.new }, expected: {type: "NilClass", isNull: true}},
{name: "AR model with no attributes",
input: -> { SerializerRailsSpecTestEmptyModel.new },
expected: {type: "SerializerRailsSpecTestEmptyModel", entries: [[
{type: 'Symbol', value: 'attributes'},
{type: 'Hash', entries: [[{type: 'String', value: 'id'}, {type: 'NilClass', isNull: true}]]},
]]}},
{name: "AR model with empty attributes",
input: -> { SerializerRailsSpecTestBasicModel.new },
expected: {type: "SerializerRailsSpecTestBasicModel", entries: [[
{type: 'Symbol', value: 'attributes'},
{type: 'Hash', entries: [
[{type: 'String', value: 'id'}, {type: 'NilClass', isNull: true}],
[{type: 'String', value: 'title'}, {type: 'NilClass', isNull: true}],
[{type: 'String', value: 'created_at'}, {type: 'NilClass', isNull: true}],
[{type: 'String', value: 'updated_at'}, {type: 'NilClass', isNull: true}],
]},
]]}},
{name: "AR model with filled out attributes",
input: -> { SerializerRailsSpecTestBasicModel.new(
title: 'Hello, world!', created_at: Time.now, updated_at: Time.now) },
expected: {type: "SerializerRailsSpecTestBasicModel", entries: [[
{type: 'Symbol', value: 'attributes'},
{type: 'Hash', entries: [
[{type: 'String', value: 'id'}, {type: 'NilClass', isNull: true}],
[{type: 'String', value: 'title'}, {type: 'String', value: 'Hello, world!'}],
# TODO serialize Time, Date, DateTime types
[{type: 'String', value: 'created_at'}, {type: 'Time', notCapturedReason: 'depth'}],
[{type: 'String', value: 'updated_at'}, {type: 'Time', notCapturedReason: 'depth'}],
]},
]]}},
]

define_serialize_value_cases(cases)
Expand Down

0 comments on commit ca1eb13

Please sign in to comment.