initial metrics

This commit is contained in:
Georg Gadinger 2023-02-13 20:13:32 +01:00
parent c44ea79cae
commit bc6806aa89
9 changed files with 109 additions and 0 deletions

View File

@ -117,3 +117,5 @@ gem "openssl", "~> 3.1"
# mail 2.8.0 breaks sendmail usage: https://github.com/mikel/mail/issues/1538 # mail 2.8.0 breaks sendmail usage: https://github.com/mikel/mail/issues/1538
gem "mail", "~> 2.7.1" gem "mail", "~> 2.7.1"
gem "prometheus-client", "~> 4.0"

View File

@ -279,6 +279,7 @@ GEM
pg (1.4.5) pg (1.4.5)
pghero (3.1.0) pghero (3.1.0)
activerecord (>= 6) activerecord (>= 6)
prometheus-client (4.0.0)
public_suffix (4.0.7) public_suffix (4.0.7)
puma (6.1.0) puma (6.1.0)
nio4r (~> 2.0) nio4r (~> 2.0)
@ -522,6 +523,7 @@ DEPENDENCIES
openssl (~> 3.1) openssl (~> 3.1)
pg pg
pghero pghero
prometheus-client (~> 4.0)
puma puma
pundit (~> 2.3) pundit (~> 2.3)
questiongenerator (~> 1.1) questiongenerator (~> 1.1)

View File

@ -37,6 +37,7 @@ class InboxController < ApplicationController
user: current_user) user: current_user)
inbox = Inbox.create!(user: current_user, question_id: question.id, new: true) inbox = Inbox.create!(user: current_user, question_id: question.id, new: true)
increment_metric
respond_to do |format| respond_to do |format|
format.turbo_stream do format.turbo_stream do
@ -85,4 +86,14 @@ class InboxController < ApplicationController
# using .dup to not modify @inbox -- useful in tests # using .dup to not modify @inbox -- useful in tests
@inbox&.dup&.update_all(new: false) # rubocop:disable Rails/SkipsModelValidations @inbox&.dup&.update_all(new: false) # rubocop:disable Rails/SkipsModelValidations
end end
def increment_metric
Retrospring::Metrics::QUESTIONS_ASKED.increment(
labels: {
anonymous: true,
followers: false,
generated: true,
}
)
end
end end

View File

@ -0,0 +1,17 @@
# frozen_string_literal: true
require "prometheus/client/formats/text"
class MetricsController < ActionController::API
include ActionController::MimeResponds
def show
render plain: metrics
end
private
def metrics
Prometheus::Client::Formats::Text.marshal(Prometheus::Client.registry)
end
end

View File

@ -49,6 +49,9 @@ Rails.application.routes.draw do
get "/linkfilter", to: "link_filter#index", as: :linkfilter get "/linkfilter", to: "link_filter#index", as: :linkfilter
get "/manifest.json", to: "manifests#show", as: :webapp_manifest get "/manifest.json", to: "manifests#show", as: :webapp_manifest
# TODO: limit this endpoint
get "/metrics", to: "metrics#show"
# Devise routes # Devise routes
devise_for :users, path: "user", skip: %i[sessions registrations] devise_for :users, path: "user", skip: %i[sessions registrations]
as :user do as :user do

View File

@ -0,0 +1,36 @@
# frozen_string_literal: true
module Retrospring
module Metrics
PROMETHEUS = Prometheus::Client.registry
# avoid re-registering metrics to make autoreloader happy:
class << self
%i[counter gauge histogram summary].each do |meth|
define_method meth do |name, *args, **kwargs|
PROMETHEUS.public_send(meth, name, *args, **kwargs)
rescue Prometheus::Client::Registry::AlreadyRegisteredError
raise unless Rails.env.development?
PROMETHEUS.unregister name
retry
end
end
end
VERSION_INFO = gauge(
:retrospring_version_info,
docstring: "Information about the currently running version",
labels: [:version],
preset_labels: {
version: Retrospring::Version.to_s,
}
).tap { _1.set 1 }
QUESTIONS_ASKED = counter(
:retrospring_questions_asked_total,
docstring: "How many questions got asked",
labels: %i[anonymous followers generated]
)
end
end

View File

@ -18,6 +18,7 @@ module UseCase
return if filtered?(question) return if filtered?(question)
increment_asked_count increment_asked_count
increment_metric
inbox = ::Inbox.create!(user: target_user, question:, new: true) inbox = ::Inbox.create!(user: target_user, question:, new: true)
notify(inbox) notify(inbox)
@ -92,6 +93,16 @@ module UseCase
source_user.save source_user.save
end end
def increment_metric
Retrospring::Metrics::QUESTIONS_ASKED.increment(
labels: {
anonymous:,
followers: false,
generated: false,
}
)
end
def filtered?(question) def filtered?(question)
target_user.mute_rules.any? { |rule| rule.applies_to? question } || target_user.mute_rules.any? { |rule| rule.applies_to? question } ||
(anonymous && AnonymousBlock.where(identifier: question.author_identifier, user_id: [target_user.id, nil]).any?) || (anonymous && AnonymousBlock.where(identifier: question.author_identifier, user_id: [target_user.id, nil]).any?) ||

View File

@ -17,6 +17,7 @@ module UseCase
) )
increment_asked_count increment_asked_count
increment_metric
QuestionWorker.perform_async(source_user_id, question.id) QuestionWorker.perform_async(source_user_id, question.id)
@ -33,6 +34,16 @@ module UseCase
source_user.save source_user.save
end end
def increment_metric
Retrospring::Metrics::QUESTIONS_ASKED.increment(
labels: {
anonymous: false,
followers: true,
generated: false,
}
)
end
def source_user def source_user
@source_user ||= ::User.find(source_user_id) @source_user ||= ::User.find(source_user_id)
end end

View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
require "rails_helper"
describe MetricsController, type: :controller do
describe "#show" do
subject { get :show }
it "returns the metrics" do
# ensure we have at least a metric set
Retrospring::Metrics::VERSION_INFO.set 1
expect(subject.body).to include "retrospring_version_info"
end
end
end