File tree Expand file tree Collapse file tree 4 files changed +107
-0
lines changed
Expand file tree Collapse file tree 4 files changed +107
-0
lines changed Original file line number Diff line number Diff line change 77require "graphql/tracing/new_relic_tracing"
88require "graphql/tracing/scout_tracing"
99require "graphql/tracing/skylight_tracing"
10+ require "graphql/tracing/statsd_tracing"
1011require "graphql/tracing/prometheus_tracing"
1112
1213if defined? ( PrometheusExporter ::Server )
Original file line number Diff line number Diff 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
127133end
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments