Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure special tags are always strings #1556

Merged
merged 2 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/ddtrace/configuration/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ def build_profiler(settings, agent_settings)

def build_tracer_tags(settings)
settings.tags.dup.tap do |tags|
tags['env'] = settings.env unless settings.env.nil?
tags['version'] = settings.version unless settings.version.nil?
tags[Ext::Environment::TAG_ENV] = settings.env unless settings.env.nil?
tags[Ext::Environment::TAG_VERSION] = settings.version unless settings.version.nil?
end
end

Expand Down
21 changes: 16 additions & 5 deletions lib/ddtrace/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

require 'time'
require 'ddtrace/utils'
require 'ddtrace/ext/distributed'
require 'ddtrace/ext/errors'
require 'ddtrace/ext/http'
require 'ddtrace/ext/net'
require 'ddtrace/ext/priority'
require 'ddtrace/environment'
require 'ddtrace/analytics'
Expand Down Expand Up @@ -38,6 +41,17 @@ class Span
# This limit is for numeric tags because uint64 could end up rounded.
NUMERIC_TAG_SIZE_RANGE = (-1 << 53..1 << 53).freeze

# 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.
# @see https://github.com/DataDog/datadog-agent/blob/2ae2cdd315bcda53166dd8fa0dedcfc448087b9d/pkg/trace/stats/aggregation.go#L13-L17
ENSURE_AGENT_TAGS = {
Ext::DistributedTracing::ORIGIN_KEY => true,
Ext::Environment::TAG_VERSION => true,
Ext::HTTP::STATUS_CODE => true,
Ext::NET::TAG_HOSTNAME => true
}.freeze

attr_accessor :name, :service, :resource, :span_type,
:span_id, :trace_id, :parent_id,
:status, :sampled,
Expand Down Expand Up @@ -101,11 +115,8 @@ def set_tag(key, value = nil)
# Keys must be unique between tags and metrics
@metrics.delete(key)

# Ensure `http.status_code` is always a string so it is added to
# @meta instead of @metrics
# DEV: This is necessary because the agent looks to `meta['http.status_code']` for
# tagging necessary metrics
value = value.to_s if key == Ext::HTTP::STATUS_CODE
# 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
Expand Down
21 changes: 21 additions & 0 deletions spec/ddtrace/span_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -425,13 +425,34 @@
end
end

context 'given _dd.hostname' do
let(:key) { '_dd.hostname' }
let(:value) { 1 }

it_behaves_like 'meta tag'
end

context 'given _dd.origin' do
let(:key) { '_dd.origin' }
let(:value) { 2 }

it_behaves_like 'meta tag'
end

context 'given http.status_code' do
let(:key) { 'http.status_code' }
let(:value) { 200 }

it_behaves_like 'meta tag'
end

context 'given version' do
let(:key) { 'version' }
let(:value) { 3 }

it_behaves_like 'meta tag'
end

context 'given a numeric tag' do
let(:key) { 'system.pid' }
let(:value) { 123 }
Expand Down