Skip to content

Commit

Permalink
Remove constant reference, fallback service name and simply analytic
Browse files Browse the repository at this point in the history
logic
  • Loading branch information
TonyCTHsu committed Jan 23, 2024
1 parent f56771b commit 286a691
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 40 deletions.
27 changes: 7 additions & 20 deletions lib/graphql/tracing/data_dog_tracing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ class DataDogTracing < PlatformTracing
}

def platform_trace(platform_key, key, data)
tracer.trace(platform_key, service: service_name, type: 'custom') do |span|
if defined?(Datadog::Tracing::Metadata::Ext) # Introduced in ddtrace 1.0
span.set_tag(Datadog::Tracing::Metadata::Ext::TAG_COMPONENT, 'graphql')
span.set_tag(Datadog::Tracing::Metadata::Ext::TAG_OPERATION, key)
end
tracer.trace(platform_key, service: options[:service], type: 'custom') do |span|
span.set_tag('component', 'graphql')
span.set_tag('operation', key)

if key == 'execute_multiplex'
operations = data[:multiplex].queries.map(&:selected_operation_name).join(', ')
Expand All @@ -32,10 +30,8 @@ def platform_trace(platform_key, key, data)
end
span.resource = resource if resource

# For top span of query, set the analytics sample rate tag, if available.
if analytics_enabled?
Datadog::Contrib::Analytics.set_sample_rate(span, analytics_sample_rate)
end
# [Deprecated] will be removed in the future
span.set_metric('_dd1.sr.eausr', analytics_sample_rate) if analytics_enabled?
end

if key == 'execute_query'
Expand All @@ -50,10 +46,6 @@ def platform_trace(platform_key, key, data)
end
end

def service_name
options.fetch(:service, 'ruby-graphql')
end

# Implement this method in a subclass to apply custom tags to datadog spans
# @param key [String] The event being traced
# @param data [Hash] The runtime data for this event (@see GraphQL::Tracing for keys for each event)
Expand All @@ -67,14 +59,9 @@ def tracer
options.fetch(:tracer, default_tracer)
end

def analytics_available?
defined?(Datadog::Contrib::Analytics) \
&& Datadog::Contrib::Analytics.respond_to?(:enabled?) \
&& Datadog::Contrib::Analytics.respond_to?(:set_sample_rate)
end

def analytics_enabled?
analytics_available? && Datadog::Contrib::Analytics.enabled?(options.fetch(:analytics_enabled, false))
# [Deprecated] options[:analytics_enabled] will be removed in the future
options.fetch(:analytics_enabled, false)
end

def analytics_sample_rate
Expand Down
20 changes: 0 additions & 20 deletions spec/support/datadog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ def self.clear_all
SPAN_TAGS.clear
end


module Contrib
module Analytics
def self.set_sample_rate(rate)
rate
end

def self.enabled?(_bool)
nil
end
end
end

class DummyTracer
def trace(platform_key, *args)
yield DummySpan.new
Expand All @@ -50,12 +37,5 @@ module Tracing
def self.trace(platform_key, *args)
yield DummySpan.new
end

module Metadata
module Ext
TAG_COMPONENT = 'component'
TAG_OPERATION = 'operation'
end
end
end
end

0 comments on commit 286a691

Please sign in to comment.