Class: NexusSemanticLogger::DatadogFormatter

Inherits:
SemanticLogger::Formatters::Raw
  • Object
show all
Defined in:
lib/nexus_semantic_logger/datadog_formatter.rb

Overview

Some attributes are reserved for use by Datadog.

https://docs.datadoghq.com/logs/log_configuration/attributes_naming_convention/#reserved-attributes
 host     Supported by super.
 source   Overridden by this class.
 status   Supported by super as level, this class moves to status.
 date     Supported by super as time, this class configures as date.
 message  Supported by super.
 env      Added by this class.
 service  Added by this class.

Instance Method Summary collapse

Constructor Details

#initialize(service) ⇒ DatadogFormatter

Returns a new instance of DatadogFormatter.



15
16
17
18
# File 'lib/nexus_semantic_logger/datadog_formatter.rb', line 15

def initialize(service)
  super(time_format: :iso_8601, time_key: :date)
  @service = service
end

Instance Method Details

#call(log, logger) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/nexus_semantic_logger/datadog_formatter.rb', line 20

def call(log, logger)
  hash = super(log, logger).clone
  hash[:source] = :rails
  level = hash.delete(:level)
  hash[:status] = level
  hash[:railsenv] = Rails.env
  hash[:service] = @service
  hash.delete(:application)
  hash.delete(:environment)
  hash.delete('')
  # datadog correlation inserted via log_tags, but datadog expects them in the root hash.
  named_tags = hash.delete(:named_tags)
  if named_tags.is_a?(Hash)
    hash.deep_merge!(named_tags)
  end
  hash_to_json(hash)
end

#hash_to_json(hash) ⇒ Object

Serialise hash to json while ensuring we don’t abort due to an infinite loop. SystemStackError while serialising indicates an infinite loop- determine which key is affected.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nexus_semantic_logger/datadog_formatter.rb', line 40

def hash_to_json(hash)
  hash.to_json
rescue SystemStackError
  as_json_serialise_errors = []
  hash.keys.each do |key|
    hash[key].as_json
  rescue SystemStackError
    hash.delete(key)
    as_json_serialise_errors << key
  end
  hash[:as_json_serialise_errors] = as_json_serialise_errors
  hash.to_json
end