Class: Sentry::Agents::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/sentry/agents/serializer.rb

Overview

Handles data serialization for span attributes

Provides utilities for converting various data types to strings suitable for Sentry span attributes, with truncation and filtering.

Class Method Summary collapse

Class Method Details

.filter(data) ⇒ Hash

Apply custom data filter if configured



61
62
63
64
65
66
# File 'lib/sentry/agents/serializer.rb', line 61

def filter(data)
  filter_proc = Sentry::Agents.configuration.data_filter
  return data unless filter_proc

  filter_proc.call(data.dup)
end

.serialize(value, max_length: nil) ⇒ String?

Serialize a value for use in span attributes

Examples:

Serializer.serialize({ key: "value" })
# => '{"key":"value"}'

Serializer.serialize("a" * 2000, max_length: 100)
# => "aaa...aaa..." (truncated to 100 chars)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sentry/agents/serializer.rb', line 27

def serialize(value, max_length: nil)
  max_length ||= Sentry::Agents.configuration.max_string_length

  result = case value
           when String
             value
           when Hash, Array
             value.to_json
           when NilClass
             return nil
           else
             value.to_s
           end

  truncate(result, max_length)
end

.truncate(str, max_length) ⇒ String

Truncate a string to the specified maximum length



50
51
52
53
54
# File 'lib/sentry/agents/serializer.rb', line 50

def truncate(str, max_length)
  return str if str.nil? || str.length <= max_length

  "#{str[0...max_length]}..."
end