class Datadog::Statsd::Serialization::EventSerializer

Constants

EVENT_BASIC_OPTIONS

Attributes

tag_serializer[R]

Public Class Methods

new(global_tags: []) click to toggle source
# File lib/datadog/statsd/serialization/event_serializer.rb, line 16
def initialize(global_tags: [])
  @tag_serializer = TagSerializer.new(global_tags)
end

Public Instance Methods

format(title, text, options = EMPTY_OPTIONS) click to toggle source
# 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

Protected Instance Methods

escape(text) click to toggle source
# File lib/datadog/statsd/serialization/event_serializer.rb, line 63
def escape(text)
  text.delete('|').tap do |t|
    t.gsub!("\n", '\n')
  end
end