Module: Datadog::Tracing::Metadata::Tagging

Included in:
TraceOperation
Defined in:
lib/datadog/tracing/metadata/tagging.rb

Overview

Adds metadata & metric tag behavior

Constant Summary collapse

NUMERIC_TAG_SIZE_RANGE =

This limit is for numeric tags because uint64 could end up rounded.

(-1 << 53..1 << 53).freeze
ENSURE_AGENT_TAGS =

Some associated values should always be sent as Tags, never as Metrics, regardless if their value is numeric or not. The Datadog agent will look for these values only as Tags, not Metrics.

{
  Ext::Distributed::TAG_ORIGIN => true,
  Core::Environment::Ext::TAG_VERSION => true,
  Ext::HTTP::TAG_STATUS_CODE => true,
  Ext::NET::TAG_HOSTNAME => true
}.freeze

Instance Method Summary collapse

Instance Method Details

#clear_metric(key) ⇒ Object

This method removes a metric for the given key. It acts like #clear_tag.



107
108
109
# File 'lib/datadog/tracing/metadata/tagging.rb', line 107

def clear_metric(key)
  metrics.delete(key)
end

#clear_tag(key) ⇒ Object

This method removes a tag for the given key.



76
77
78
# File 'lib/datadog/tracing/metadata/tagging.rb', line 76

def clear_tag(key)
  meta.delete(key)
end

#get_metric(key) ⇒ Object

Return the metric with the given key, nil if it doesn’t exist.



87
88
89
# File 'lib/datadog/tracing/metadata/tagging.rb', line 87

def get_metric(key)
  metrics[key] || meta[key]
end

#get_tag(key) ⇒ Object Also known as: []

Return the tag with the given key, nil if it doesn’t exist.



26
27
28
# File 'lib/datadog/tracing/metadata/tagging.rb', line 26

def get_tag(key)
  meta[key] || metrics[key]
end

#has_tag?(tag) ⇒ Boolean

Returns true if the provided ‘tag` was set to a non-nil value. False otherwise.

Parameters:

  • tag (String)

    the tag or metric to check for presence

Returns:

  • (Boolean)

    if the tag is present and not nil



71
72
73
# File 'lib/datadog/tracing/metadata/tagging.rb', line 71

def has_tag?(tag) # rubocop:disable Naming/PredicateName
  !get_tag(tag).nil? # nil is considered not present, thus we can't use `Hash#has_key?`
end

#set_metric(key, value) ⇒ Object

This method sets a tag with a floating point value for the given key. It acts like ‘set_tag()` and it simply add a tag without further processing.



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/datadog/tracing/metadata/tagging.rb', line 93

def set_metric(key, value)
  # Keys must be unique between tags and metrics
  meta.delete(key)

  # enforce that the value is a floating point number
  value = Float(value)

  # Encode strings in UTF-8 to facilitate downstream serialization
  metrics[Core::Utils.utf8_encode(key)] = value
rescue StandardError => e
  Datadog.logger.debug("Unable to set the metric #{key}, ignoring it. Caused by: #{e}")
end

#set_tag(key, value = nil) ⇒ Object Also known as: []=

Set the given key / value tag pair on the span. Keys and values must be strings. A valid example is:

span.set_tag('http.method', request.method)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/datadog/tracing/metadata/tagging.rb', line 34

def set_tag(key, value = nil)
  # Keys must be unique between tags and metrics
  metrics.delete(key)

  # DEV: This is necessary because the agent looks at `meta[key]`, not `metrics[key]`.
  value = value.to_s if ENSURE_AGENT_TAGS[key]

  # NOTE: Adding numeric tags as metrics is stop-gap support
  #       for numeric typed tags. Eventually they will become
  #       tags again.
  # Any numeric that is not an integer greater than max size is logged as a metric.
  # Everything else gets logged as a tag.
  if value.is_a?(Numeric) && !(value.is_a?(Integer) && !NUMERIC_TAG_SIZE_RANGE.cover?(value))
    set_metric(key, value)
  else
    # Encode strings in UTF-8 to facilitate downstream serialization
    meta[Core::Utils.utf8_encode(key)] = Core::Utils.utf8_encode(value)
  end
rescue StandardError => e
  Datadog.logger.debug("Unable to set the tag #{key}, ignoring it. Caused by: #{e}")
end

#set_tags(hash) ⇒ Object

Sets tags from given hash, for each key in hash it sets the tag with that key and associated value from the hash. It is shortcut for ‘set_tag`. Keys and values of the hash must be strings. Note that nested hashes are not supported. A valid example is:

span.set_tags({ "http.method" => "GET", "user.id" => "234" })


62
63
64
# File 'lib/datadog/tracing/metadata/tagging.rb', line 62

def set_tags(hash)
  hash.each { |k, v| set_tag(k, v) }
end

#tagsObject

Returns a copy of all metadata. Keys for ‘@meta` and `@metrics` don’t collide, by construction.



113
114
115
# File 'lib/datadog/tracing/metadata/tagging.rb', line 113

def tags
  meta.merge(metrics)
end