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

Adds prometheus metrics collection and endpoint #798

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ gem 'jwt'
gem 'lograge'
gem 'mitlibraries-theme', git: 'https://github.com/mitlibraries/mitlibraries-theme', tag: 'v1.2'
gem 'opensearch-ruby'
gem 'prometheus-client'
gem 'puma'
gem 'rack-attack'
gem 'rack-cors'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ GEM
pathutil (0.16.2)
forwardable-extended (~> 2.6)
pg (1.5.4)
prometheus-client (4.2.2)
psych (5.1.2)
stringio
public_suffix (5.0.4)
Expand Down Expand Up @@ -433,6 +434,7 @@ DEPENDENCIES
mitlibraries-theme!
opensearch-ruby
pg
prometheus-client
puma
rack-attack
rack-cors
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ locally.
- `PREFERRED_DOMAIN` - set this to the domain you would like to to use. Any
other requests that come to the app will redirect to the root of this domain.
This is useful to prevent access to herokuapp.com domains.
- `PROMETHEUS` - If present, enables the Prometheus metrics endpoint and the feature flag to capture metrics
- `REQUESTS_PER_PERIOD` - requests allowed before throttling. Default is 100.
- `REQUEST_PERIOD` - number of minutes for the period in `REQUESTS_PER_PERIOD`.
Default is 1.
Expand Down
9 changes: 6 additions & 3 deletions app/controllers/graphql_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ def execute
# current_user: current_user,
tracers: [request_tracer]
}
result = TimdexSchema.execute(query, variables: variables,
context: context,
operation_name: operation_name)
result = TimdexSchema.execute(query, variables:,
context:,
operation_name:)

Timdex::GraphqlQueriesTotal.increment if Flipflop.enabled?(:prometheus)

render json: result
rescue StandardError => e
raise e unless Rails.env.development?
Expand Down
21 changes: 21 additions & 0 deletions app/graphql/timdex_field_usage_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ def result
Rails.logger.info("GraphQL used fields: #{@used_fields.to_a}")
Rails.logger.info("GraphQL used deprecated fields: #{@used_deprecated_fields.to_a}")
Rails.logger.info("GraphQL used deprecated arguments: #{@used_deprecated_arguments.to_a}")

if Flipflop.enabled?(:prometheus)
# Increment deprecated field usage counter per field
@used_deprecated_fields.each do |field|
Timdex::GraphqlDeprecatedFieldUsage.increment(labels: { field: })
end

# Increment field usage counter per field
@used_fields.each do |field|
next if @used_deprecated_fields.include?(field)
next if field.start_with?('_')

Timdex::GraphqlFieldUsage.increment(labels: { field: })
end

# Increment deprecated argument counter per argument
@used_deprecated_arguments.each do |field|
Timdex::GraphqlDeprecatedArguments.increment(labels: { field: })
end
end

{
used_fields: @used_fields.to_a,
used_deprecated_fields: @used_deprecated_fields.to_a,
Expand Down
10 changes: 10 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@ require_relative 'config/environment'

run Rails.application
Rails.application.load_server

if ENV.fetch('PROMETHEUS', false).present?
require 'rack'
require 'prometheus/middleware/collector'
require 'prometheus/middleware/exporter'

use Rack::Deflater
use Prometheus::Middleware::Collector
use Prometheus::Middleware::Exporter
end
4 changes: 4 additions & 0 deletions config/features.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
# Strategies will be used in the order listed here.
strategy :session
strategy :default

feature :prometheus,
default: ENV.fetch('PROMETHEUS', false),
description: "Enable prometheus metrics endpoint"
end
14 changes: 14 additions & 0 deletions config/initializers/prometheus_metrics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# flipflip features are available to use in initializers so we cheat a bit here.
return unless ENV.fetch('PROMETHEUS', false).present?

require 'prometheus/client'
require 'prometheus/client/push'

prometheus = Prometheus::Client.registry
Timdex::GraphqlQueriesTotal = prometheus.counter(:graphql_queries_total, docstring: 'A counter of GraphQL requests made')

Timdex::GraphqlFieldUsage = prometheus.counter(:graphql_field_usage, docstring: 'A counter of GraphQL fields requested', labels: [:service, :field], preset_labels: { service: "timdex-api" })

Timdex::GraphqlDeprecatedFieldUsage = prometheus.counter(:graphql_deprecated_field_usage, docstring: 'A counter of deprecated GraphQL fields requested', labels: [:service, :field], preset_labels: { service: "timdex-api" })

Timdex::GraphqlDeprecatedArguments = prometheus.counter(:graphql_deprecated_argument_usage, docstring: 'A counter of deprecated GraphQL arguments requested', labels: [:service, :field], preset_labels: { service: "timdex-api" })