Class: Datadog::Statsd::Serialization::EventSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/statsd/serialization/event_serializer.rb

Constant Summary collapse

EVENT_BASIC_OPTIONS =
{
  date_happened:    'd:',
  hostname:         'h:',
  aggregation_key:  'k:',
  priority:         'p:',
  source_type_name: 's:',
  alert_type:       't:',
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(global_tags: []) ⇒ EventSerializer

Returns a new instance of EventSerializer.



16
17
18
# File 'lib/datadog/statsd/serialization/event_serializer.rb', line 16

def initialize(global_tags: [])
  @tag_serializer = TagSerializer.new(global_tags)
end

Instance Method Details

#format(title, text, options = EMPTY_OPTIONS) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/datadog/statsd/serialization/event_serializer.rb', line 20

def format(title, text, options = EMPTY_OPTIONS)
  title = escape(title)
  text = escape(text)

  String.new.tap do |event|
    event << '_e{'
    event << title.bytesize.to_s
    event << ','
    event << text.bytesize.to_s
    event << '}:'
    event << title
    event << '|'
    event << text

    # we are serializing the generic service check options
    # before serializing specialized options that need edge-cases
    EVENT_BASIC_OPTIONS.each do |option_key, shortcut|
      if value = options[option_key]
        event << '|'
        event << shortcut
        event << value.to_s.delete('|')
      end
    end

    # also returns the global tags from serializer
    if tags = tag_serializer.format(options[:tags])
      event << '|#'
      event << tags
    end

    if event.bytesize > MAX_EVENT_SIZE
      if options[:truncate_if_too_long]
        event.slice!(MAX_EVENT_SIZE..event.length)
      else
        raise "Event #{title} payload is too big (more that 8KB), event discarded"
      end
    end
  end
end