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

influxdb_tools.rb « support « qa « qa - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e817b096864bea40987855ea866fd12daf3c2aca (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# frozen_string_literal: true

require "active_support/core_ext/module/delegation"

module QA
  module Support
    # Common tools for use with influxdb metrics setup
    #
    module InfluxdbTools
      # @return [String] bucket for storing all test run metrics
      INFLUX_TEST_METRICS_BUCKET = "e2e-test-stats"
      # @return [String] bucket for storing metrics from main runs
      INFLUX_MAIN_TEST_METRICS_BUCKET = "e2e-test-stats-main"
      # @return [Array] live environment names
      LIVE_ENVS = %w[staging staging-canary staging-ref canary preprod production].freeze

      private

      delegate :ci_project_name, to: "QA::Runtime::Env"

      # Query client
      #
      # @return [QueryApi]
      def query_api
        @query_api ||= influx_client.create_query_api
      end

      # Write client
      #
      # @return [WriteApi]
      def write_api
        @write_api ||= influx_client.create_write_api
      end

      # InfluxDb client
      #
      # @return [InfluxDB2::Client]
      def influx_client
        @influx_client ||= InfluxDB2::Client.new(
          ENV["QA_INFLUXDB_URL"] || raise("Missing QA_INFLUXDB_URL env variable"),
          ENV["QA_INFLUXDB_TOKEN"] || raise("Missing QA_INFLUXDB_TOKEN env variable"),
          bucket: INFLUX_TEST_METRICS_BUCKET,
          org: "gitlab-qa",
          precision: InfluxDB2::WritePrecision::NANOSECOND
        )
      end

      # Test run type
      # Automatically infer for staging (`gstg`, `gstg-cny`, `gstg-ref`), canary, preprod or production env
      #
      # @return [String, nil]
      def run_type
        @run_type ||= begin
          return env('QA_RUN_TYPE') if env('QA_RUN_TYPE')
          return unless LIVE_ENVS.include?(ci_project_name)

          test_subset = if env('NO_ADMIN') == 'true'
                          'sanity-no-admin'
                        elsif env('SMOKE_ONLY') == 'true'
                          'sanity'
                        else
                          'full'
                        end

          "#{ci_project_name}-#{test_subset}"
        end
      end

      # Merge request iid
      #
      # @return [String]
      def merge_request_iid
        env('CI_MERGE_REQUEST_IID') || env('TOP_UPSTREAM_MERGE_REQUEST_IID')
      end

      # Return non empty environment variable value
      #
      # @param [String] name
      # @return [String, nil]
      def env(name)
        return unless ENV[name] && !ENV[name].empty?

        ENV[name]
      end
    end
  end
end