Merge pull request #1148 from Retrospring/moar-metrics

metrics: add counter for created/destroyed users
This commit is contained in:
Karina Kwiatek 2023-04-29 09:54:53 +02:00 committed by GitHub
commit 55090a42f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 12 deletions

View file

@ -134,3 +134,6 @@ Style/EndlessMethod:
Style/TrailingCommaInHashLiteral: Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: consistent_comma EnforcedStyleForMultiline: consistent_comma
Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: consistent_comma

View file

@ -85,8 +85,14 @@ class User < ApplicationRecord # rubocop:disable Metrics/ClassLength
end end
end end
after_destroy do
Retrospring::Metrics::USERS_DESTROYED.increment
end
after_create do after_create do
Profile.create(user_id: id) if Profile.where(user_id: id).count.zero? Profile.create(user_id: id) if Profile.where(user_id: id).count.zero?
Retrospring::Metrics::USERS_CREATED.increment
end end
# use the screen name as parameter for url helpers # use the screen name as parameter for url helpers

View file

@ -24,55 +24,65 @@ module Retrospring
labels: [:version], labels: [:version],
preset_labels: { preset_labels: {
version: Retrospring::Version.to_s, version: Retrospring::Version.to_s,
} },
).tap { _1.set 1 } ).tap { _1.set 1 }
QUESTIONS_ASKED = counter( QUESTIONS_ASKED = counter(
:retrospring_questions_asked_total, :retrospring_questions_asked_total,
docstring: "How many questions got asked", docstring: "How many questions got asked",
labels: %i[anonymous followers generated] labels: %i[anonymous followers generated],
) )
QUESTIONS_ANSWERED = counter( QUESTIONS_ANSWERED = counter(
:retrospring_questions_answered_total, :retrospring_questions_answered_total,
docstring: "How many questions got answered" docstring: "How many questions got answered",
) )
COMMENTS_CREATED = counter( COMMENTS_CREATED = counter(
:retrospring_comments_created_total, :retrospring_comments_created_total,
docstring: "How many comments got created" docstring: "How many comments got created",
)
USERS_CREATED = counter(
:retrospring_users_created_total,
docstring: "How many users got created",
)
USERS_DESTROYED = counter(
:retrospring_users_destroyed_total,
docstring: "How many users deleted their accounts",
) )
# metrics from Sidekiq::Stats.new # metrics from Sidekiq::Stats.new
SIDEKIQ = { SIDEKIQ = {
processed: gauge( processed: gauge(
:sidekiq_processed, :sidekiq_processed,
docstring: "Number of jobs processed by Sidekiq" docstring: "Number of jobs processed by Sidekiq",
), ),
failed: gauge( failed: gauge(
:sidekiq_failed, :sidekiq_failed,
docstring: "Number of jobs that failed" docstring: "Number of jobs that failed",
), ),
scheduled_size: gauge( scheduled_size: gauge(
:sidekiq_scheduled_jobs, :sidekiq_scheduled_jobs,
docstring: "Number of jobs that are enqueued" docstring: "Number of jobs that are enqueued",
), ),
retry_size: gauge( retry_size: gauge(
:sidekiq_retried_jobs, :sidekiq_retried_jobs,
docstring: "Number of jobs that are being retried" docstring: "Number of jobs that are being retried",
), ),
dead_size: gauge( dead_size: gauge(
:sidekiq_dead_jobs, :sidekiq_dead_jobs,
docstring: "Number of jobs that are dead" docstring: "Number of jobs that are dead",
), ),
processes_size: gauge( processes_size: gauge(
:sidekiq_processes, :sidekiq_processes,
docstring: "Number of active Sidekiq processes" docstring: "Number of active Sidekiq processes",
), ),
queue_enqueued: gauge( queue_enqueued: gauge(
:sidekiq_queues_enqueued, :sidekiq_queues_enqueued,
docstring: "Number of enqueued jobs per queue", docstring: "Number of enqueued jobs per queue",
labels: %i[queue] labels: %i[queue],
), ),
}.freeze }.freeze
end end

View file

@ -10,7 +10,7 @@ RSpec.describe User, type: :model do
@user = User.new( @user = User.new(
screen_name: "FunnyMeme2004", screen_name: "FunnyMeme2004",
password: "y_u_no_secure_password?", password: "y_u_no_secure_password?",
email: "nice.meme@nsa.gov" email: "nice.meme@nsa.gov",
) )
Profile.new(user: @user) Profile.new(user: @user)
end end
@ -33,6 +33,45 @@ RSpec.describe User, type: :model do
end end
end end
describe "callbacks" do
describe "before_destroy" do
it "marks reports about this user as deleted" do
other_user = FactoryBot.create(:user)
other_user.report me, "va tutto benissimo"
expect { me.destroy }
.to change { Reports::User.find_by(target_id: me.id).deleted? }
.from(false)
.to(true)
end
end
describe "after_destroy" do
it "increments the users_destroyed metric" do
expect { me.destroy }.to change { Retrospring::Metrics::USERS_DESTROYED.values.values.sum }.by(1)
end
end
describe "after_create" do
subject :user do
User.create!(
screen_name: "konqi",
email: "konqi@example.rrerr.net",
password: "dragonsRQt5",
)
end
it "creates a profile for the user" do
expect { user }.to change { Profile.count }.by(1)
expect(Profile.find_by(user:).user).to eq(user)
end
it "increments the users_created metric" do
expect { user }.to change { Retrospring::Metrics::USERS_CREATED.values.values.sum }.by(1)
end
end
end
describe "custom sharing url validation" do describe "custom sharing url validation" do
subject do subject do
FactoryBot.build(:user, sharing_custom_url: url).tap(&:validate).errors[:sharing_custom_url] FactoryBot.build(:user, sharing_custom_url: url).tap(&:validate).errors[:sharing_custom_url]