Skip to content

Commit 252b30a

Browse files
author
Robert Mosolgo
authored
Merge pull request #2996 from rmosolgo/statsd-tracing
Add Statsd tracing
2 parents ec4d698 + 43795f4 commit 252b30a

File tree

4 files changed

+125
-15
lines changed

4 files changed

+125
-15
lines changed

lib/graphql/tracing.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require "graphql/tracing/new_relic_tracing"
88
require "graphql/tracing/scout_tracing"
99
require "graphql/tracing/skylight_tracing"
10+
require "graphql/tracing/statsd_tracing"
1011
require "graphql/tracing/prometheus_tracing"
1112

1213
if defined?(PrometheusExporter::Server)

lib/graphql/tracing/platform_tracing.rb

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,19 @@ def trace(key, data)
3232
trace_field = true # implemented with instrumenter
3333
else
3434
field = data[:field]
35-
cache = platform_key_cache(data.fetch(:query).context)
36-
platform_key = cache.fetch(field) do
37-
cache[field] = platform_field_key(data[:owner], field)
38-
end
39-
4035
return_type = field.type.unwrap
4136
trace_field = if return_type.kind.scalar? || return_type.kind.enum?
4237
(field.trace.nil? && @trace_scalars) || field.trace
4338
else
4439
true
4540
end
41+
42+
platform_key = if trace_field
43+
context = data.fetch(:query).context
44+
cached_platform_key(context, field) { platform_field_key(data[:owner], field) }
45+
else
46+
nil
47+
end
4648
end
4749

4850
if platform_key && trace_field
@@ -53,20 +55,16 @@ def trace(key, data)
5355
yield
5456
end
5557
when "authorized", "authorized_lazy"
56-
cache = platform_key_cache(data.fetch(:context))
5758
type = data.fetch(:type)
58-
platform_key = cache.fetch(type) do
59-
cache[type] = platform_authorized_key(type)
60-
end
59+
context = data.fetch(:context)
60+
platform_key = cached_platform_key(context, type) { platform_authorized_key(type) }
6161
platform_trace(platform_key, key, data) do
6262
yield
6363
end
6464
when "resolve_type", "resolve_type_lazy"
65-
cache = platform_key_cache(data.fetch(:context))
6665
type = data.fetch(:type)
67-
platform_key = cache.fetch(type) do
68-
cache[type] = platform_resolve_type_key(type)
69-
end
66+
context = data.fetch(:context)
67+
platform_key = cached_platform_key(context, type) { platform_resolve_type_key(type) }
7068
platform_trace(platform_key, key, data) do
7169
yield
7270
end
@@ -119,8 +117,20 @@ def transaction_name(query)
119117

120118
attr_reader :options
121119

122-
def platform_key_cache(ctx)
123-
ctx.namespace(self.class)[:platform_key_cache] ||= {}
120+
# Different kind of schema objects have different kinds of keys:
121+
#
122+
# - Object types: `.authorized`
123+
# - Union/Interface types: `.resolve_type`
124+
# - Fields: execution
125+
#
126+
# So, they can all share one cache.
127+
#
128+
# If the key isn't present, the given block is called and the result is cached for `key`.
129+
#
130+
# @return [String]
131+
def cached_platform_key(ctx, key)
132+
cache = ctx.namespace(self.class)[:platform_key_cache] ||= {}
133+
cache.fetch(key) { cache[key] = yield }
124134
end
125135
end
126136
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
module GraphQL
4+
module Tracing
5+
class StatsdTracing < PlatformTracing
6+
self.platform_keys = {
7+
'lex' => "graphql.lex",
8+
'parse' => "graphql.parse",
9+
'validate' => "graphql.validate",
10+
'analyze_query' => "graphql.analyze_query",
11+
'analyze_multiplex' => "graphql.analyze_multiplex",
12+
'execute_multiplex' => "graphql.execute_multiplex",
13+
'execute_query' => "graphql.execute_query",
14+
'execute_query_lazy' => "graphql.execute_query_lazy",
15+
}
16+
17+
# @param statsd [Object] A statsd client
18+
def initialize(statsd:, **rest)
19+
@statsd = statsd
20+
super(**rest)
21+
end
22+
23+
def platform_trace(platform_key, key, data)
24+
@statsd.time(platform_key) do
25+
yield
26+
end
27+
end
28+
29+
def platform_field_key(type, field)
30+
"graphql.#{type.graphql_name}.#{field.graphql_name}"
31+
end
32+
33+
def platform_authorized_key(type)
34+
"graphql.authorized.#{type.graphql_name}"
35+
end
36+
37+
def platform_resolve_type_key(type)
38+
"graphql.resolve_type.#{type.graphql_name}"
39+
end
40+
end
41+
end
42+
end
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
require "spec_helper"
3+
4+
describe GraphQL::Tracing::StatsdTracing do
5+
module MockStatsd
6+
class << self
7+
def time(key)
8+
self.timings << key
9+
yield
10+
end
11+
12+
def timings
13+
@timings
14+
end
15+
16+
def clear
17+
@timings = []
18+
end
19+
end
20+
end
21+
22+
class StatsdTestSchema < GraphQL::Schema
23+
class Query < GraphQL::Schema::Object
24+
field :int, Integer, null: false
25+
26+
def int
27+
1
28+
end
29+
end
30+
31+
query(Query)
32+
33+
use GraphQL::Execution::Interpreter
34+
use GraphQL::Analysis::AST
35+
use GraphQL::Tracing::StatsdTracing, statsd: MockStatsd
36+
end
37+
38+
before do
39+
MockStatsd.clear
40+
end
41+
42+
it "gathers timings" do
43+
StatsdTestSchema.execute("query X { int }")
44+
expected_timings = [
45+
"graphql.execute_multiplex",
46+
"graphql.analyze_multiplex",
47+
"graphql.lex",
48+
"graphql.parse",
49+
"graphql.validate",
50+
"graphql.analyze_query",
51+
"graphql.execute_query",
52+
"graphql.authorized.Query",
53+
"graphql.execute_query_lazy"
54+
]
55+
assert_equal expected_timings, MockStatsd.timings
56+
end
57+
end

0 commit comments

Comments
 (0)