Skip to content

Commit 58eb973

Browse files
committed
Add StatsdTracing
1 parent ec4d698 commit 58eb973

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ def transaction_name(query)
122122
def platform_key_cache(ctx)
123123
ctx.namespace(self.class)[:platform_key_cache] ||= {}
124124
end
125+
126+
# TODO migrate to this
127+
def cached_platform_key(ctx, cache_name,key)
128+
cache = ctx.namespace(self.class)[cache_name] ||= {}
129+
cache.fetch(key) { cache[key] = yield }
130+
end
125131
end
126132
end
127133
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
24+
def platform_trace(platform_key, key, data)
25+
@statsd.time(platform_key) do
26+
yield
27+
end
28+
end
29+
30+
def platform_field_key(type, field)
31+
"graphql.#{type.graphql_name}.#{field.graphql_name}"
32+
end
33+
34+
def platform_authorized_key(type)
35+
"graphql.authorized.#{type.graphql_name}"
36+
end
37+
38+
def platform_resolve_type_key(type)
39+
"graphql.resolve_type.#{type.graphql_name}"
40+
end
41+
end
42+
end
43+
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)