stellate 0.0.3 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/stellate.rb +77 -32
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e0373037e16a2f147c83f1083499e75f740ec4e92e426ca4382d9855227b1eb
4
- data.tar.gz: a1e8ecf717260acb276e5ae32d7997f823830278b48c24f487ab552aed75a77d
3
+ metadata.gz: 1c1a2d292de035b6fce0fd0caa8d9e2c1a03aa5034e8b18e3da3e212f5c1690b
4
+ data.tar.gz: 4e71e1ec928e81c737083c48766644d606180bdb83dee4f41989f201bc8bc135
5
5
  SHA512:
6
- metadata.gz: 6060f2aa4eed8b6d52f0494496cb9a16bd5471cc88b0c04b1f6d230439071cee1ad228950aaaf819e70755624e74062f19d216b8d3c1a535493a3318808ede28
7
- data.tar.gz: 77328738460721879b96ca8fdcd1a45defe31501e6bbaac4e442f31723107d5daae16b46f99feeb8ca47488b9e578d9e72c8019139e4aac26f8514f5a9dc4e3b
6
+ metadata.gz: 021e9f325d3434df6ee2d004a06b543cbe475e9784ffc068c544f0d48d45eb44978642d41bf0eda20482629012435d049ccedac9d0c7be39f5ae810aa6652e08
7
+ data.tar.gz: b74619cf8782d5afac11d2f32330ae33dcaa3269b51f5876d01df30884502847160031b49f8445cc50cb02ff43f9b05f4c1c3f140c5d4b418f07762ff1bcf826
data/lib/stellate.rb CHANGED
@@ -4,7 +4,7 @@ require 'uri'
4
4
  require 'net/http'
5
5
 
6
6
  module Stellate
7
- VERSION = '0.0.3'
7
+ VERSION = '1.0.0'
8
8
 
9
9
  # Extend your GraphQL::Schema with this module to enable easy Stellate
10
10
  # Metrics Logging.
@@ -18,10 +18,16 @@ module Stellate
18
18
  # arguments as `GraphQL::Schema.execute()`, and also the following:
19
19
  # - `headers` (`ActionDispatch::Http::Headers`): The HTTP headers from the
20
20
  # incoming request
21
+ # - `callback` (`Method`): If passed, this will be called with a lambda as
22
+ # argument. Calling the passed lambda will log the request to Stellate
23
+ # via HTTP. This is useful if you want to move this request into a non-
24
+ # blocking process (e.g. using Sidekiq) rather than the default behavior
25
+ # of performing a blocking HTTP request which can increase overall
26
+ # response times of your API.
21
27
  def execute_with_logging(query_str = nil, **kwargs)
22
28
  starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
23
29
 
24
- result = execute(query_str, **kwargs.except(:service_name, :token, :headers))
30
+ result = execute(query_str, **kwargs.except(:headers, :callback))
25
31
 
26
32
  ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
27
33
  elapsed = (ending - starting) * 1000
@@ -48,14 +54,14 @@ module Stellate
48
54
 
49
55
  response = result.to_json
50
56
  payload = {
51
- "operation": query_str,
52
- "variableHash": create_blake3_hash(kwargs[:variables].to_json),
53
- "method": kwargs[:method].is_a?(String) ? kwargs[:method] : 'POST',
54
- "elapsed": elapsed.round,
55
- "responseSize": response.length,
56
- "responseHash": create_blake3_hash(response),
57
- "statusCode": 200,
58
- "operationName": kwargs[:operation_name]
57
+ 'operation' => query_str,
58
+ 'variableHash' => create_blake3_hash(kwargs[:variables].to_json),
59
+ 'method' => kwargs[:method].is_a?(String) ? kwargs[:method] : 'POST',
60
+ 'elapsed' => elapsed.round,
61
+ 'responseSize' => response.length,
62
+ 'responseHash' => create_blake3_hash(response),
63
+ 'statusCode' => 200,
64
+ 'operationName' => kwargs[:operation_name]
59
65
  }
60
66
 
61
67
  errors = result['errors']
@@ -65,22 +71,29 @@ module Stellate
65
71
  forwarded_for = headers['X-Forwarded-For']
66
72
  ips = forwarded_for.is_a?(String) ? forwarded_for.split(',') : []
67
73
 
68
- payload[:id] = ips[0] || headers['True-Client-Ip'] || headers['X-Real-Ip']
69
- payload[:userAgent] = headers['User-Agent']
70
- payload[:referer] = headers['referer']
74
+ payload['ip'] = ips[0] || headers['True-Client-Ip'] || headers['X-Real-Ip']
75
+ payload['userAgent'] = headers['User-Agent']
76
+ payload['referer'] = headers['referer']
71
77
  end
72
78
 
73
- # TODO: make this an async request to avoid blocking the response
74
- begin
75
- res = Net::HTTP.post(
76
- URI("https://#{@stellate_service_name}.stellate.sh/log"),
77
- payload.to_json,
79
+ stellate_request = {
80
+ 'url' => "https://#{@stellate_service_name}.stellate.sh/log",
81
+ 'headers' => {
78
82
  'Content-Type' => 'application/json',
79
83
  'Stellate-Logging-Token' => @stellate_token
80
- )
81
- puts "Failed to log metrics to Stellate: #{res.body}" if res.code.to_i >= 300
82
- rescue StandardError => e
83
- puts "Failed to log metrics to Stellate: #{e}"
84
+ },
85
+ 'body' => payload.to_json
86
+ }
87
+
88
+ callback = kwargs[:callback]
89
+ # The former check handles methods, the latter handles lambdas
90
+ if callback.is_a?(Method) || callback.respond_to?(:call)
91
+ callback.call(stellate_request)
92
+ # This handles symbols that contain methods
93
+ elsif callback.is_a?(Symbol) && method(callback).is_a?(Method)
94
+ method(callback).call(stellate_request)
95
+ else
96
+ run_stellate_request(stellate_request)
84
97
  end
85
98
 
86
99
  result
@@ -98,7 +111,15 @@ module Stellate
98
111
  # Use this plugin in your GraphQL::Schema to automatically sync your GraphQL
99
112
  # schema with your Stellate service.
100
113
  class SchemaSyncing
101
- def self.use(schema)
114
+ # The first argument is the GraphQLSchema class that this plugin is used
115
+ # on. It accepts the following names arguments:
116
+ # - `callback` (`Method`): If passed, this will be called with a lambda as
117
+ # argument. Calling the passed lambda will sync the schema to Stellate
118
+ # via HTTP. This is useful if you want to move this request into a non-
119
+ # blocking process (e.g. using Sidekiq) rather than the default behavior
120
+ # of performing a blocking HTTP request which can increase overall
121
+ # response times of your API.
122
+ def self.use(schema, **kwargs)
102
123
  unless schema.stellate_service_name.is_a?(String)
103
124
  puts 'Missing service name in order to sync schema to Stellate'
104
125
  return
@@ -111,20 +132,44 @@ module Stellate
111
132
 
112
133
  introspection = JSON.parse(schema.to_json)['data']
113
134
 
114
- # TODO: make this an async request to avoid blocking the request
115
- begin
116
- res = Net::HTTP.post(
117
- URI("https://#{schema.stellate_service_name}.stellate.sh/schema"),
118
- { schema: introspection }.to_json,
135
+ stellate_request = {
136
+ 'url' => "https://#{schema.stellate_service_name}.stellate.sh/schema",
137
+ 'headers' => {
119
138
  'Content-Type' => 'application/json',
120
139
  'Stellate-Schema-Token' => schema.stellate_token
121
- )
122
- puts "Failed to sync schema to Stellate: #{res.body}" if res.code.to_i >= 300
123
- rescue StandardError => e
124
- puts "Failed to sync schema to Stellate: #{e}"
140
+ },
141
+ 'body' => { schema: introspection }.to_json
142
+ }
143
+
144
+ callback = kwargs[:callback]
145
+ # The former check handles methods, the latter handles lambdas
146
+ if callback.is_a?(Method) || callback.respond_to?(:call)
147
+ callback.call(stellate_request)
148
+ # This handles symbols that contain methods
149
+ elsif callback.is_a?(Symbol) && method(callback).is_a?(Method)
150
+ method(callback).call(stellate_request)
151
+ else
152
+ run_stellate_request(stellate_request)
125
153
  end
126
154
  end
127
155
  end
156
+
157
+ def self.run_stellate_request(stellate_request)
158
+ url = URI.parse(stellate_request['url'])
159
+
160
+ http = Net::HTTP.new(url.host, url.port)
161
+ http.use_ssl = true
162
+
163
+ req = Net::HTTP::Post.new(url)
164
+ stellate_request['headers'].each do |key, value|
165
+ req[key] = value
166
+ end
167
+ req.body = stellate_request['body']
168
+ res = http.request(req)
169
+ puts "HTTP request to Stellate failed: #{res.body}" if res.code.to_i >= 300
170
+ rescue StandardError => e
171
+ puts "HTTP request to Stellate failed: #{e}"
172
+ end
128
173
  end
129
174
 
130
175
  def create_blake3_hash(str)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stellate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stellate
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-14 00:00:00.000000000 Z
11
+ date: 2024-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-http