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

Backport datadog span_type change to 2.0.x #4789

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
9 changes: 1 addition & 8 deletions guides/queries/tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,10 @@ To add [Datadog](https://www.datadoghq.com) instrumentation:

```ruby
class MySchema < GraphQL::Schema
use(GraphQL::Tracing::DataDogTracing, options)
use(GraphQL::Tracing::DataDogTracing)
end
```

You may provide `options` as a `Hash` with the following values:

| Key | Description | Default |
| --- | ----------- | ------- |
| `service` | Service name used for `graphql` instrumentation | `'ruby-graphql'` |
| `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |

For more details about Datadog's tracing API, check out the [Ruby documentation](https://github.com/DataDog/dd-trace-rb/blob/master/docs/GettingStarted.md) or the [APM documentation](https://docs.datadoghq.com/tracing/) for more product information.

## Prometheus
Expand Down
55 changes: 21 additions & 34 deletions lib/graphql/tracing/data_dog_trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
module GraphQL
module Tracing
module DataDogTrace
# @param tracer [#trace] Deprecated
# @param analytics_enabled [Boolean] Deprecated
# @param analytics_sample_rate [Float] Deprecated
def initialize(tracer: nil, analytics_enabled: false, analytics_sample_rate: 1.0, service: "ruby-graphql", **rest)
def initialize(tracer: nil, analytics_enabled: false, analytics_sample_rate: 1.0, service: nil, **rest)
if tracer.nil?
tracer = defined?(Datadog::Tracing) ? Datadog::Tracing : Datadog.tracer
end
@tracer = tracer

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

@analytics_enabled = analytics_available && Datadog::Contrib::Analytics.enabled?(analytics_enabled)
@analytics_enabled = analytics_enabled
@analytics_sample_rate = analytics_sample_rate

@service_name = service
@has_prepare_span = respond_to?(:prepare_span)
super
Expand All @@ -34,12 +32,9 @@ def initialize(tracer: nil, analytics_enabled: false, analytics_sample_rate: 1.0
}.each do |trace_method, trace_key|
module_eval <<-RUBY, __FILE__, __LINE__
def #{trace_method}(**data)
@tracer.trace("#{trace_key}", service: @service_name) do |span|
span.span_type = 'custom'
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, '#{trace_method}')
end
@tracer.trace("#{trace_key}", service: @service_name, type: 'custom') do |span|
span.set_tag('component', 'graphql')
span.set_tag('operation', '#{trace_method}')

#{
if trace_method == 'execute_multiplex'
Expand All @@ -54,10 +49,8 @@ def #{trace_method}(**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
RUBY
elsif trace_method == 'execute_query'
<<-RUBY
Expand Down Expand Up @@ -89,12 +82,10 @@ def execute_field_span(span_key, query, field, ast_node, arguments, object)
nil
end
if platform_key && trace_field
@tracer.trace(platform_key, service: @service_name) do |span|
span.span_type = 'custom'
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, span_key)
end
@tracer.trace(platform_key, service: @service_name, type: 'custom') do |span|
span.set_tag('component', 'graphql')
span.set_tag('operation', span_key)

if @has_prepare_span
prepare_span_data = { query: query, field: field, ast_node: ast_node, arguments: arguments, object: object }
prepare_span(span_key, prepare_span_data, span)
Expand Down Expand Up @@ -125,12 +116,10 @@ def authorized(query:, type:, object:)

def authorized_span(span_key, object, type, query)
platform_key = @platform_key_cache[DataDogTrace].platform_authorized_key_cache[type]
@tracer.trace(platform_key, service: @service_name) do |span|
span.span_type = 'custom'
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, span_key)
end
@tracer.trace(platform_key, service: @service_name, type: 'custom') do |span|
span.set_tag('component', 'graphql')
span.set_tag('operation', span_key)

if @has_prepare_span
prepare_span(span_key, {object: object, type: type, query: query}, span)
end
Expand Down Expand Up @@ -158,12 +147,10 @@ def resolve_type_lazy(object:, type:, query:)

def resolve_type_span(span_key, object, type, query)
platform_key = @platform_key_cache[DataDogTrace].platform_resolve_type_key_cache[type]
@tracer.trace(platform_key, service: @service_name) do |span|
span.span_type = 'custom'
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, span_key)
end
@tracer.trace(platform_key, service: @service_name, type: 'custom') do |span|
span.set_tag('component', 'graphql')
span.set_tag('operation', span_key)

if @has_prepare_span
prepare_span(span_key, {object: object, type: type, query: query}, span)
end
Expand Down
28 changes: 7 additions & 21 deletions lib/graphql/tracing/data_dog_tracing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ class DataDogTracing < PlatformTracing
}

def platform_trace(platform_key, key, data)
tracer.trace(platform_key, service: service_name) do |span|
span.span_type = 'custom'
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 @@ -33,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 @@ -51,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 @@ -65,18 +56,13 @@ def prepare_span(key, data, span)
def tracer
default_tracer = defined?(Datadog::Tracing) ? Datadog::Tracing : Datadog.tracer

# [Deprecated] options[:tracer] will be removed in the future
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?
# [Deprecated] options[:analytics_enabled] will be removed in the future
analytics_available? && Datadog::Contrib::Analytics.enabled?(options.fetch(:analytics_enabled, false))
options.fetch(:analytics_enabled, false)
end

def analytics_sample_rate
Expand Down
21 changes: 0 additions & 21 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 @@ -41,7 +28,6 @@ def resource=(resource_name)
SPAN_RESOURCE_NAMES << resource_name
end

def span_type=(*args)end
def set_tag(key, value)
SPAN_TAGS << [key, value]
end
Expand All @@ -51,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
Loading