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.



109
110
111
# File 'lib/datadog/tracing/metadata/tagging.rb', line 109

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

#clear_tag(key) ⇒ Object

This method removes a tag for the given key.



78
79
80
# File 'lib/datadog/tracing/metadata/tagging.rb', line 78

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.



89
90
91
# File 'lib/datadog/tracing/metadata/tagging.rb', line 89

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.



28
29
30
# File 'lib/datadog/tracing/metadata/tagging.rb', line 28

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



73
74
75
# File 'lib/datadog/tracing/metadata/tagging.rb', line 73

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.



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

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)


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

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" })


64
65
66
# File 'lib/datadog/tracing/metadata/tagging.rb', line 64

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.



115
116
117
# File 'lib/datadog/tracing/metadata/tagging.rb', line 115

def tags
  meta.merge(metrics)
end