-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(graphql-hive.rb): clean up file, move config to class #47
refactor(graphql-hive.rb): clean up file, move config to class #47
Conversation
return yield unless should_collect_usage? | ||
return yield unless platform_key == "execute_multiplex" | ||
return yield unless data[:multiplex] | ||
|
||
queries = data[:multiplex].queries | ||
return yield if queries.empty? | ||
|
||
timestamp = generate_timestamp | ||
results, duration = measure_execution { yield } | ||
|
||
report_usage(timestamp, queries, results, duration) | ||
results | ||
end | ||
|
||
def should_collect_usage? | ||
@configuration.enabled && @configuration.collect_usage | ||
end | ||
|
||
def generate_timestamp | ||
(Time.now.utc.to_f * 1000).to_i | ||
end | ||
|
||
def measure_execution | ||
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
results = yield | ||
ending = Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
duration = ((ending - starting) * (10**9)).to_i | ||
|
||
[results, duration] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor for legibility. No functional changes here.
c6e64be
to
7154962
Compare
7154962
to
71a2a7f
Compare
bcabdd8
to
116d813
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The refactor here for the new config class looks great!
I requested changes because I think we need to add the logic back for the on_exit
method.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thank you for adding this file!
lib/graphql-hive.rb
Outdated
at_exit do | ||
GraphQL::Hive.instance&.on_exit | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'll still need this when the server exits / shuts down. Was this intentional? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, this was sort of intentional but the work isn't finished. Hooking in to at_exit
could cause hive to exit before web workers are finished processing requests. The gem exposes on_exit
so that Puma apps can call on_exit
in the shutdown sequence. In a Puma app with multiple workers, there would be a race condition between at_exit
and on_exit
.
It makes more sense to me to delete these lines and rely on the on_exit
hook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add these lines back in to keep this PR scoped to config changes and make a followup to delete this and update the readme to make life-cycle events more clear for single-worker environments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah gotcha! I assume if we remove this, all single worker apps would need to call on_exit
method in the usage reporter manually, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right. In a separate PR I'll update the docs to indicate that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One question but this is great and these changes make the code a lot cleaner and easier to read!
if @report_schema && (@reporting.dig(:author) || !@reporting.dig(:commit)) | ||
@logger.warn("GraphQL Hive `author` and `commit` options are required. Disabling Schema Reporting.") | ||
@report_schema = false | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should @report_schema
also be false if the the token isn't set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Fixed in 478fdb2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This made me realize the configs are a bit confusing. It is possible to have:
{
enabled: false
report_schema: true
token: `a-valid-token`
}
I'll put some thought into the configs for a future PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks good to me! this refactor makes this class way more legible
This is a refactor of graphql-hive.rb that moves configuration information to a
Configuration
class. Later, this will allow us to configure GraphQL Hive outside of a schema.rb file.