From f4875a577bf7cc1c3b5705fabebe9bd5cb1c9afd Mon Sep 17 00:00:00 2001 From: Dany Marcoux Date: Mon, 28 Feb 2022 15:50:15 +0100 Subject: [PATCH] Instrument ScmWebhook To answer questions in the SCM/CI integration like: - Which SCM is the most used? - Which webhook event is the most frequent? --- .../scm_webhook_instrumentation.rb | 29 +++++++++++++++++++ src/api/app/models/scm_webhook.rb | 9 ++++-- 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/api/app/instrumentations/scm_webhook_instrumentation.rb diff --git a/src/api/app/instrumentations/scm_webhook_instrumentation.rb b/src/api/app/instrumentations/scm_webhook_instrumentation.rb new file mode 100644 index 00000000000..7b8f5ad8ee2 --- /dev/null +++ b/src/api/app/instrumentations/scm_webhook_instrumentation.rb @@ -0,0 +1,29 @@ +module ScmWebhookInstrumentation + extend ActiveSupport::Concern + + # Define callbacks with ActiveModel::Callback which is included in ActiveModel::Model + included do + define_model_callbacks :initialize + + after_initialize :track_webhook + end + + private + + def track_webhook + RabbitmqBus.send_to_bus('metrics', "scm_webhook,scm=#{@payload[:scm]},webhook_event=#{webhook_event} count=1") + end + + def webhook_event + case + when push_event? + 'push' + when tag_push_event? + 'tag_push' + when pull_request_event? + 'pull_request' + else + 'unsupported' + end + end +end diff --git a/src/api/app/models/scm_webhook.rb b/src/api/app/models/scm_webhook.rb index 9c0ce7389ea..220c305ce85 100644 --- a/src/api/app/models/scm_webhook.rb +++ b/src/api/app/models/scm_webhook.rb @@ -1,6 +1,7 @@ # Contains the payload extracted from a SCM webhook and provides helper methods to know which webhook event we're dealing with class ScmWebhook include ActiveModel::Model + include ScmWebhookInstrumentation # for run_callbacks attr_accessor :payload @@ -12,9 +13,11 @@ class ScmWebhook IGNORED_MERGE_REQUEST_ACTIONS = ['approved', 'unapproved'].freeze def initialize(attributes = {}) - super - # To safely navigate the hash and compare keys - @payload = attributes[:payload].deep_symbolize_keys + run_callbacks(:initialize) do + super + # To safely navigate the hash and compare keys + @payload = attributes[:payload].deep_symbolize_keys + end end def new_pull_request?