-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgraphql-hive.rb
193 lines (162 loc) · 4.95 KB
/
graphql-hive.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# frozen_string_literal: true
require "logger"
require "securerandom"
require "graphql-hive/version"
require "graphql-hive/usage_reporter"
require "graphql-hive/client"
require "graphql-hive/sampler"
require "graphql-hive/sampling/basic_sampler"
require "graphql-hive/sampling/dynamic_sampler"
require "graphql"
module GraphQL
# GraphQL Hive usage collector and schema reporter
class Hive < GraphQL::Tracing::PlatformTracing
@@schema = nil
@@instance = nil
@usage_reporter = nil
@client = nil
REPORT_SCHEMA_MUTATION = <<~MUTATION
mutation schemaPublish($input: SchemaPublishInput!) {
schemaPublish(input: $input) {
__typename
}
}
MUTATION
DEFAULT_OPTIONS = {
enabled: true,
debug: false,
port: "443",
collect_usage: true,
read_operations: true,
report_schema: true,
buffer_size: 50,
queue_size: 1000,
logger: nil,
collect_usage_sampling: 1.0
}.freeze
self.platform_keys = {
"lex" => "lex",
"parse" => "parse",
"validate" => "validate",
"analyze_query" => "analyze_query",
"analyze_multiplex" => "analyze_multiplex",
"execute_multiplex" => "execute_multiplex",
"execute_query" => "execute_query",
"execute_query_lazy" => "execute_query_lazy"
}
def initialize(options = {})
opts = DEFAULT_OPTIONS.merge(options)
initialize_options!(opts)
super(opts)
@@instance = self
@client = GraphQL::Hive::Client.new(opts)
@usage_reporter = GraphQL::Hive::UsageReporter.new(opts, @client)
# buffer
@report = {
size: 0,
map: {},
operations: []
}
send_report_schema(@@schema) if @@schema && opts[:report_schema] && @options[:enabled]
end
def self.instance
@@instance
end
def self.use(schema, **kwargs)
@@schema = schema
super
end
# called on trace events
def platform_trace(platform_key, _key, data)
return yield unless @options[:enabled] && @options[:collect_usage]
if platform_key == "execute_multiplex"
if data[:multiplex]
queries = data[:multiplex].queries
timestamp = (Time.now.utc.to_f * 1000).to_i
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
results = yield
ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
elapsed = ending - starting
duration = (elapsed.to_f * (10**9)).to_i
report_usage(timestamp, queries, results, duration) unless queries.empty?
results
else
yield
end
else
yield
end
end
# compat
def platform_authorized_key(type)
"#{type.graphql_name}.authorized.graphql"
end
# compat
def platform_resolve_type_key(type)
"#{type.graphql_name}.resolve_type.graphql"
end
# compat
def platform_field_key(type, field)
"graphql.#{type.name}.#{field.name}"
end
def on_exit
@usage_reporter.on_exit
end
def on_start
@usage_reporter.on_start
end
private
def initialize_options!(options)
if options[:logger].nil?
options[:logger] = Logger.new($stderr)
original_formatter = Logger::Formatter.new
options[:logger].formatter = proc { |severity, datetime, progname, msg|
msg = msg.respond_to?(:dump) ? msg.dump : msg
original_formatter.call(severity, datetime, progname, "[hive] #{msg}")
}
options[:logger].level = options[:debug] ? Logger::DEBUG : Logger::INFO
end
if !options.include?(:token) && (!options.include?(:enabled) || options.enabled)
options[:logger].warn("`token` options is missing")
options[:enabled] = false
false
elsif options[:report_schema] &&
(
!options.include?(:reporting) ||
(
options.include?(:reporting) && (
!options[:reporting].include?(:author) || !options[:reporting].include?(:commit)
)
)
)
options[:logger].warn("`reporting.author` and `reporting.commit` options are required")
false
end
true
end
def report_usage(timestamp, queries, results, duration)
@usage_reporter.add_operation([timestamp, queries, results, duration])
end
def send_report_schema(schema)
sdl = GraphQL::Schema::Printer.new(schema).print_schema
body = {
query: REPORT_SCHEMA_MUTATION,
operationName: "schemaPublish",
variables: {
input: {
sdl: sdl,
author: @options[:reporting][:author],
commit: @options[:reporting][:commit],
service: @options[:reporting][:service_name],
url: @options[:reporting][:service_url],
force: true
}
}
}
@client.send(:"/registry", body, :"report-schema")
end
end
end
at_exit do
GraphQL::Hive.instance&.on_exit
end