Skip to content

Commit

Permalink
fix queue, refactor client
Browse files Browse the repository at this point in the history
  • Loading branch information
cassidycodes committed Oct 9, 2024
1 parent 9435bcd commit 44a52ec
Show file tree
Hide file tree
Showing 15 changed files with 227 additions and 135 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ group :development do
gem "standardrb", "~> 1"
gem "timecop", "~> 0.9"
gem "super_diff", "~> 0.13"
gem "webmock", "~> 3"
end
24 changes: 24 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,45 @@ PATH
remote: .
specs:
graphql-hive (0.4.4)
faraday (< 3)
graphql (>= 2.3, < 3)

GEM
remote: https://rubygems.org/
specs:
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
ast (2.4.2)
attr_extras (7.1.0)
base64 (0.2.0)
bigdecimal (3.1.8)
crack (1.0.0)
bigdecimal
rexml
diff-lcs (1.5.1)
faraday (2.12.0)
faraday-net_http (>= 2.0, < 3.4)
json
logger
faraday-net_http (3.3.0)
net-http
graphql (2.3.7)
base64
hashdiff (1.1.1)
json (2.7.2)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
logger (1.6.1)
net-http (0.4.1)
uri
optimist (3.1.0)
parallel (1.26.3)
parser (3.3.5.0)
ast (~> 2.4.1)
racc
patience_diff (1.2.0)
optimist (~> 3.0)
public_suffix (6.0.1)
racc (1.8.1)
rainbow (3.1.1)
rake (13.2.1)
Expand Down Expand Up @@ -78,6 +96,11 @@ GEM
patience_diff
timecop (0.9.10)
unicode-display_width (2.6.0)
uri (0.13.1)
webmock (3.24.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
arm64-darwin-23
Expand All @@ -91,6 +114,7 @@ DEPENDENCIES
standardrb (~> 1)
super_diff (~> 0.13)
timecop (~> 0.9)
webmock (~> 3)

BUNDLED WITH
2.5.15
1 change: 1 addition & 0 deletions graphql-hive.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
end

spec.add_dependency "graphql", ">= 2.3", "< 3"
spec.add_dependency "faraday", "< 3"
end
2 changes: 1 addition & 1 deletion k6/graphql-api/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ GEM
mustermann (2.0.2)
ruby2_keywords (~> 0.0.1)
nio4r (2.7.3)
puma (6.4.2)
puma (6.4.3)
nio4r (~> 2.0)
racc (1.8.0)
rack (2.2.9)
Expand Down
9 changes: 4 additions & 5 deletions lib/graphql-hive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,19 @@ def initialize(options = {})
@logger
)
buffer = GraphQL::Hive::OperationsBuffer.new(
queue: @queue,
queue: Queue.new,
sampler: sampler,
client: @client,
options: options,
logger: @logger
logger: @logger,
size: opts[:buffer_size],
client_info: options[:client_info]
)
reporting_thread = GraphQL::Hive::ReportingThread.new(
buffer: buffer,
logger: @logger
)
queue = Queue.new
@usage_reporter = GraphQL::Hive::UsageReporter.new(
reporting_thread: reporting_thread,
queue: queue,
logger: @logger
)
if @enabled
Expand Down
71 changes: 32 additions & 39 deletions lib/graphql-hive/client.rb
Original file line number Diff line number Diff line change
@@ -1,57 +1,50 @@
# frozen_string_literal: true

require "net/http"
require "uri"
require "faraday"
require "json"

module GraphQL
class Hive < GraphQL::Tracing::PlatformTracing
# API client
class Client
USAGE_API_VERSION = "2"
def initialize(token:, logger:, port: "443", endpoint: "app.graphql-hive.com")
@port = port.to_s
@use_ssl = port == "443"
@scheme = @use_ssl ? "https" : "http"
@endpoint = endpoint
@token = token
port = port.to_s
use_ssl = port == "443"
scheme = use_ssl ? "https" : "http"
url = "#{scheme}://#{endpoint}:#{port}"
@logger = logger
@connection = setup_connection(url, token, logger)
end

def send(path, body, _log_type)
uri =
URI::HTTP.build(
scheme: @http_scheme,
host: @endpoint,
port: @port,
path: path
)

http = setup_http(uri)
request = build_request(uri, body)
response = http.request(request)

@logger.debug(response.inspect)
@logger.debug(response.body.inspect)
rescue => e
@logger.fatal("Failed to send data: #{e}")
response = @connection.post(path) do |req|
req.body = JSON.generate(body)
end
rescue Faraday::Error => e
@logger.fatal("GraphQL::Hive::Client encountered an error: #{e.message}")
end

def setup_http(uri)
http = ::Net::HTTP.new(uri.host, uri.port)
http.use_ssl = @use_ssl
http.read_timeout = 2
http
private

def setup_connection(url, token, logger)
Faraday.new(
url: url,
headers: build_headers(token)
) do |conn|
conn.request :json
conn.response :logger, logger, headers: false
conn.adapter Faraday.default_adapter
end
end

def build_request(uri, body)
request = Net::HTTP::Post.new(uri.request_uri)
request["Authorization"] = @token
request["X-Usage-API-Version"] = "2"
request["content-type"] = "application/json"
request["User-Agent"] = "Hive@#{Graphql::Hive::VERSION}"
request["graphql-client-name"] = "Hive Ruby Client"
request["graphql-client-version"] = Graphql::Hive::VERSION
request.body = JSON.generate(body)
request
def build_headers(token)
{
"Authorization" => token,
"X-Usage-API-Version" => USAGE_API_VERSION,
"User-Agent" => "Hive@#{Graphql::Hive::VERSION}",
"graphql-client-name" => "Hive Ruby Client",
"graphql-client-version" => Graphql::Hive::VERSION
}
end
end
end
Expand Down
13 changes: 9 additions & 4 deletions lib/graphql-hive/operations_buffer.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
# frozen_string_literal: true

require "forwardable"

module GraphQL
class Hive < GraphQL::Tracing::PlatformTracing
class OperationsBuffer
def initialize(queue:, sampler:, client:, logger:, options: {})
extend Forwardable
def_delegators :@queue, :push, :close

def initialize(queue:, sampler:, client:, logger:, size:, client_info: nil)
@queue = queue
@sampler = sampler
@client = client
@logger = logger
@max_buffer_size = options[:buffer_size]
@max_buffer_size = size
@client_info = client_info
@mutex = Mutex.new
@buffer = []
@options = options
end

def run
Expand All @@ -34,7 +39,7 @@ def flush_buffer
puts "Buffer is full, sending report."
report = GraphQL::Hive::Report.new(
operations: @buffer,
client_info: @options[:client_info]
client_info: @client_info
).to_json
@client.send(:"/usage", report, :usage)
@buffer.clear
Expand Down
7 changes: 7 additions & 0 deletions lib/graphql-hive/reporting_thread.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# frozen_string_literal: true

require "forwardable"

module GraphQL
class Hive < GraphQL::Tracing::PlatformTracing
class ReportingThread
extend Forwardable

def_delegators :@buffer, :push

def initialize(buffer:, logger:)
@buffer = buffer
@logger = logger
Expand All @@ -22,6 +28,7 @@ def start_thread
end

def join_thread
@buffer.close
@thread&.join
end
end
Expand Down
6 changes: 2 additions & 4 deletions lib/graphql-hive/usage_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ def self.instance
@@instance
end

def initialize(reporting_thread:, queue:, logger:)
def initialize(reporting_thread:, logger:)
@@instance = self
@reporting_thread = reporting_thread
@queue = queue
@logger = logger
end

def add_operation(operation)
@queue.push(operation)
@reporting_thread.push(operation)
end

def on_exit
@logger.debug("Shutting down usage reporter")
@queue.close
@reporting_thread.join_thread
end

Expand Down
Loading

0 comments on commit 44a52ec

Please sign in to comment.