Skip to content

Commit

Permalink
Merge pull request #4067 from rmosolgo/datadog-custom-span-tags
Browse files Browse the repository at this point in the history
Add #prepare_span hook for custom Datadog tags
  • Loading branch information
rmosolgo committed May 18, 2022
2 parents 6d544ef + d60dae3 commit f82155e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/graphql/tracing/data_dog_tracing.rb
Expand Up @@ -45,6 +45,8 @@ def platform_trace(platform_key, key, data)
span.set_tag(:query_string, data[:query].query_string)
end

prepare_span(key, data, span)

yield
end
end
Expand All @@ -53,6 +55,13 @@ 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)
# @param span [Datadog::Tracing::SpanOperation] The datadog span for this event
def prepare_span(key, data, span)
end

def tracer
default_tracer = defined?(Datadog::Tracing) ? Datadog::Tracing : Datadog.tracer

Expand Down
4 changes: 4 additions & 0 deletions lib/graphql/tracing/platform_tracing.rb
Expand Up @@ -10,6 +10,10 @@ module Tracing
class PlatformTracing
class << self
attr_accessor :platform_keys

def inherited(child_class)
child_class.platform_keys = self.platform_keys
end
end

def initialize(options = {})
Expand Down
4 changes: 2 additions & 2 deletions spec/graphql/schema/introspection_system_spec.rb
Expand Up @@ -247,10 +247,10 @@ class HidingIntrospectionSchema < GraphQL::Schema
module HideIntrospectionByContext
def visible?(ctx)
super && if introspection?
!ctx[:hide_introspection]
!ctx[:hide_introspection]
else
true
end
end
end
end

Expand Down
28 changes: 28 additions & 0 deletions spec/graphql/tracing/data_dog_tracing_spec.rb
Expand Up @@ -18,6 +18,16 @@ class TestSchema < GraphQL::Schema
query(Query)
use(GraphQL::Tracing::DataDogTracing)
end

class CustomTracerTestSchema < GraphQL::Schema
class CustomDataDogTracing < GraphQL::Tracing::DataDogTracing
def prepare_span(trace_key, data, span)
span.set_tag("custom:#{trace_key}", data.keys.join(","))
end
end
query(Query)
use(CustomDataDogTracing)
end
end

before do
Expand Down Expand Up @@ -47,4 +57,22 @@ class TestSchema < GraphQL::Schema
assert_includes Datadog::SPAN_TAGS, ['component', 'graphql']
assert_includes Datadog::SPAN_TAGS, ['operation', 'execute_multiplex']
end

it "sets custom tags tags" do
DataDogTest::CustomTracerTestSchema.execute("{ int }")
expected_custom_tags = [
["custom:lex", "query_string"],
["custom:parse", "query_string"],
["custom:execute_multiplex", "multiplex"],
["custom:analyze_multiplex", "multiplex"],
["custom:validate", "validate,query"],
["custom:analyze_query", "query"],
["custom:execute_query", "query"],
["custom:authorized", "context,type,object,path"],
["custom:execute_query_lazy", "multiplex,query"],
]

actual_custom_tags = Datadog::SPAN_TAGS.reject { |t| t[0] == "operation" || t[0] == "component" || t[0].is_a?(Symbol) }
assert_equal expected_custom_tags, actual_custom_tags
end
end

0 comments on commit f82155e

Please sign in to comment.