1# frozen_string_literal: true
2#
3module Gitlab
4  module Tracking
5    module Destinations
6      class SnowplowMicro < Snowplow
7        include ::Gitlab::Utils::StrongMemoize
8        extend ::Gitlab::Utils::Override
9
10        DEFAULT_URI = 'http://localhost:9090'
11
12        override :options
13        def options(group)
14          super.update(
15            protocol: uri.scheme,
16            port: uri.port,
17            force_secure_tracker: false
18          ).transform_keys! { |key| key.to_s.camelize(:lower).to_sym }
19        end
20
21        override :enabled?
22        def enabled?
23          true
24        end
25
26        override :hostname
27        def hostname
28          "#{uri.host}:#{uri.port}"
29        end
30
31        def uri
32          strong_memoize(:snowplow_uri) do
33            uri = URI(ENV['SNOWPLOW_MICRO_URI'] || DEFAULT_URI)
34            uri = URI("http://#{ENV['SNOWPLOW_MICRO_URI']}") unless %w[http https].include?(uri.scheme)
35            uri
36          end
37        end
38
39        private
40
41        override :cookie_domain
42        def cookie_domain
43          '.gitlab.com'
44        end
45
46        override :protocol
47        def protocol
48          uri.scheme
49        end
50      end
51    end
52  end
53end
54