Skip to content

Commit

Permalink
Instrument ScmWebhook
Browse files Browse the repository at this point in the history
To answer questions in the SCM/CI integration like:
- Which SCM is the most used?
- Which webhook event is the most frequent?
  • Loading branch information
Dany Marcoux committed Feb 28, 2022
1 parent e14612f commit f4875a5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
29 changes: 29 additions & 0 deletions 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
9 changes: 6 additions & 3 deletions 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

Expand All @@ -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?
Expand Down

0 comments on commit f4875a5

Please sign in to comment.