百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

graphql-metrics

> 后端框架
开源

从 Ruby 应用程序和 graphql gem 提供的 GraphQL 查询中提取您想要的尽可能多的细节。

174 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

从 Ruby 应用程序和 graphql gem 提供的 GraphQL 查询中提取您想要的尽可能多的细节。

GraphQL Metrics

Extract as much detail as you want from GraphQL queries, served up from your Ruby app and the graphql gem. Compatible with the graphql-batch gem, to extract batch-loaded fields resolution timings.

Be sure to read the CHANGELOG to stay updated on feature additions, breaking changes made to this gem.

Installation

Add this line to your application's Gemfile:

ruby
gem 'graphql-metrics'

You can require it with in your code as needed with:

ruby
require 'graphql/metrics'

Or globally in the Gemfile with:

ruby
gem 'graphql-metrics', require: 'graphql/metrics'

And then execute:

$ bundle

Or install it yourself as:

$ gem install graphql-metrics

Usage

Get started by defining your own Analyzer, inheriting from GraphQL::Metrics::Analyzer.

The following analyzer demonstrates a simple way to capture commonly used metrics sourced from key parts of your schema definition, the query document being served, as well as runtime query and resolver timings. In this toy example, all of this data is simply stored on the GraphQL::Query context, under a namespace to avoid collisions with other analyzers etc.

What you do with these captured metrics is up to you!

NOTE: If any non-graphql-ruby gem-related exceptions occur in your application during query document parsing and validation, runtime metrics for queries (like query_duration, etc.) as well as field resolver timings (like resolver_timings, lazy_resolver_timings) may not be present in the extracted metrics hash, even if you opt to collect them.

Define your own analyzer subclass

…

Make use of your analyzer

Add the GraphQL::Metrics plugin to your schema. This opts you in to capturing all static and runtime metrics seen above.

ruby
class Schema < GraphQL::Schema
  query QueryRoot
  mutation MutationRoot

  use GraphQL::Metrics, analyzer: SimpleAnalyzer
end

Optionally, only gather static metrics

If you don't care to capture field timings metrics, they can be disabled with the capture_field_timings option:

ruby
class Schema < GraphQL::Schema
  query QueryRoot
  mutation MutationRoot

  use GraphQL::Metrics, analyzer: SimpleAnalyzer, capture_field_timings: false
end

Your analyzer will still be called with query_extracted, field_extracted, but with timings metrics omitted. argument_extracted will work exactly the same.

And if you want to disable tracing metrics entirely, use the capture_timings option:

ruby
class Schema < GraphQL::Schema
  query QueryRoot
  mutation MutationRoot

  use GraphQL::Metrics, analyzer: SimpleAnalyzer, capture_timings: false
end

Order of execution

Because of the structure of graphql-ruby's plugin architecture, it may be difficult to build an intuition around the order in which methods defined on GraphQL::Metrics::Instrumentation, GraphQL::Metrics::Trace and subclasses of GraphQL::Metrics::Analyzer run.

Although you ideally will not need to care about these details if you are simply using this gem to gather metrics in your application as intended, here's a breakdown of the order of execution of the methods involved:

When used as instrumentation, an analyzer and tracing, the order of execution is usually:

  • Instrumentation.execute_multiplex (context setup)
  • Trace.capture_lexing_time
  • Trace.capture_parsing_time
  • Trace.capture_validation_time
  • Trace.capture_analysis_time
  • Analyzer#initialize (bit more context setup, instance vars setup)
  • Analyzer#result
  • Trace.capture_query_start_time
  • Trace.trace_field (n times)
  • Instrumentation.execute_multiplex (call query and field callbacks, now that we have all static and runtime metrics gathered)
  • Analyzer#extract_query
  • Analyzer#query_extracted
  • Analyzer#extract_fields_with_runtime_metrics
    • calls Analyzer#field_extracted n times

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bundle exec rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Shopify/graphql-metrics. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the GraphQL::Metrics project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Issues· 3 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubygraphql

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架