Class: Datadog::Statsd::Serialization::ServiceCheckSerializer

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

Constant Summary collapse

SERVICE_CHECK_BASIC_OPTIONS =
{
  timestamp: 'd:',
  hostname:  'h:',
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(global_tags: []) ⇒ ServiceCheckSerializer

Returns a new instance of ServiceCheckSerializer.



12
13
14
# File 'lib/datadog/statsd/serialization/service_check_serializer.rb', line 12

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

Instance Method Details

#format(name, status, options = EMPTY_OPTIONS) ⇒ Object



16
17
18
19
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
# File 'lib/datadog/statsd/serialization/service_check_serializer.rb', line 16

def format(name, status, options = EMPTY_OPTIONS)
  String.new.tap do |service_check|
    # line basics
    service_check << "_sc"
    service_check << "|"
    service_check << name.to_s
    service_check << "|"
    service_check << status.to_s

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

    if message = options[:message]
      service_check << '|m:'
      service_check << escape_message(message)
    end

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