Welcome to mirror list, hosted at ThFree Co, Russian Federation.

service_ping_context.rb « tracking « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d31ca69a10c494daf62dd7cd4e4e2109037ff356 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

module Gitlab
  module Tracking
    class ServicePingContext
      SCHEMA_URL = 'iglu:com.gitlab/gitlab_service_ping/jsonschema/1-0-0'
      REDISHLL_SOURCE = :redis_hll
      REDIS_SOURCE = :redis

      ALLOWED_SOURCES = [REDISHLL_SOURCE, REDIS_SOURCE].freeze

      def initialize(data_source:, event: nil, key_path: nil)
        check_configuration(data_source, event, key_path)

        @payload = { data_source: data_source }

        payload[:event_name] = event if data_source.eql? REDISHLL_SOURCE
        payload[:key_path] = key_path if data_source.eql? REDIS_SOURCE
      end

      def to_context
        SnowplowTracker::SelfDescribingJson.new(SCHEMA_URL, payload)
      end

      def to_h
        {
          schema: SCHEMA_URL,
          data: @payload
        }
      end

      private

      attr_reader :payload

      def check_configuration(data_source, event, key_path)
        unless ALLOWED_SOURCES.include?(data_source)
          configuration_error("#{data_source} is not acceptable data source for ServicePingContext")
        end

        if REDISHLL_SOURCE.eql?(data_source) && event.nil?
          configuration_error("event attribute can not be missing for #{REDISHLL_SOURCE} data source")
        end

        return unless REDIS_SOURCE.eql?(data_source) && key_path.nil?

        configuration_error("key_path attribute can not be missing for #{REDIS_SOURCE} data source")
      end

      def configuration_error(message)
        Gitlab::ErrorTracking.track_and_raise_for_dev_exception(ArgumentError.new(message))
      end
    end
  end
end