1# frozen_string_literal: true
2
3# Module to support correlation_id and additional job details.
4module Gitlab
5  module Marginalia
6    module Comment
7      private
8
9      def jid
10        bg_job["jid"] if bg_job.present?
11      end
12
13      def job_class
14        bg_job["class"] if bg_job.present?
15      end
16
17      def correlation_id
18        if bg_job.present?
19          bg_job["correlation_id"]
20        else
21          Labkit::Correlation::CorrelationId.current_id
22        end
23      end
24
25      def bg_job
26        job = ::Marginalia::Comment.marginalia_job
27
28        # We are using 'Marginalia::SidekiqInstrumentation' which does not support 'ActiveJob::Base'.
29        # Gitlab also uses 'ActionMailer::MailDeliveryJob' which inherits from ActiveJob::Base.
30        # So below condition is used to return metadata for such jobs.
31        if job.is_a?(ActionMailer::MailDeliveryJob)
32          {
33            "class" => job.arguments.first,
34            "jid"   => job.job_id
35          }
36        else
37          job
38        end
39      end
40
41      def endpoint_id
42        Labkit::Context.current&.get_attribute(:caller_id)
43      end
44
45      def db_config_name
46        ::Gitlab::Database.db_config_name(marginalia_adapter)
47      end
48    end
49  end
50end
51