Skip to content
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

Merged
merged 8 commits into from
Jan 2, 2025

Conversation

cassiascheffer
Copy link
Collaborator

@cassiascheffer cassiascheffer commented Jan 2, 2025

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.

Comment on lines +54 to +82
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]
Copy link
Collaborator Author

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.

@cassiascheffer cassiascheffer marked this pull request as ready for review January 2, 2025 16:23
Copy link
Collaborator

@aryascripts aryascripts left a 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.

Comment on lines +5 to +6
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).
Copy link
Collaborator

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!

Comment on lines 191 to 193
at_exit do
GraphQL::Hive.instance&.on_exit
end
Copy link
Collaborator

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? 🤔

Copy link
Collaborator Author

@cassiascheffer cassiascheffer Jan 2, 2025

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.

Copy link
Collaborator Author

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.

Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

Copy link

@kathrynkodama kathrynkodama left a 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!

Comment on lines +37 to +40
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

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?

Copy link
Collaborator Author

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

Copy link
Collaborator Author

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.

Copy link
Contributor

@al-yanna al-yanna left a 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

@cassiascheffer cassiascheffer merged commit 30daf71 into rperryng:master Jan 2, 2025
4 checks passed
@cassiascheffer cassiascheffer deleted the refactor-config branch January 2, 2025 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants