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

Add #prepare_span hook for custom Datadog tags #4067

Merged
merged 2 commits into from May 18, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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