gitlab-rspec-metrics-exporter 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/gitlab/rspec_metrics_exporter/config_helper.rb +9 -42
- data/lib/gitlab/rspec_metrics_exporter/env.rb +35 -0
- data/lib/gitlab/rspec_metrics_exporter/pipeline.rb +46 -0
- data/lib/gitlab/rspec_metrics_exporter/test_metrics.rb +17 -20
- data/lib/gitlab/rspec_metrics_exporter/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ae316f64101cc9f5b26f75d48b0ace55d2ad77ce5be00255d8710b45c491a93c
|
|
4
|
+
data.tar.gz: c59d9c429b33c598886b500d4623a674b381041e876c34c97421182b28f1be6f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: db8e097dba5d2d37150167abfd98f37fa25c73e6f557993f3dfaa281ba355ed643d513ba3bfc8a8e1ce5fa434ba110d839869a76d896e544cc8e114b54d57aaa
|
|
7
|
+
data.tar.gz: b896db0dde1695eeaae0e48d44576790c22d35139bbe9b3f522e4fe42c90d3da33f432241b3f99971918c9de243e330ce50833cdf703cb6347c92bbedb10d28f
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require "logger"
|
|
4
4
|
|
|
5
5
|
require_relative "config"
|
|
6
|
+
require_relative "env"
|
|
6
7
|
require_relative "formatter"
|
|
7
8
|
|
|
8
9
|
module Gitlab
|
|
@@ -14,9 +15,9 @@ module Gitlab
|
|
|
14
15
|
# require "gitlab/rspec_metrics_exporter/config_helper"
|
|
15
16
|
# Gitlab::RSpecMetricsExporter::ConfigHelper.configure!("backend_unit")
|
|
16
17
|
class ConfigHelper
|
|
17
|
-
STABLE_EE_BRANCH_REGEX = /^[\d-]+-stable-ee$/
|
|
18
|
-
|
|
19
18
|
class << self
|
|
19
|
+
include Env
|
|
20
|
+
|
|
20
21
|
def configure!(run_type = nil)
|
|
21
22
|
return unless ENV.fetch("CI", nil) && ENV.fetch("GLCI_EXPORT_TEST_METRICS", "true") == "true"
|
|
22
23
|
|
|
@@ -45,17 +46,13 @@ module Gitlab
|
|
|
45
46
|
@logger ||= Logger.new($stdout)
|
|
46
47
|
end
|
|
47
48
|
|
|
48
|
-
def present?(value)
|
|
49
|
-
!value.nil? && !value.to_s.empty?
|
|
50
|
-
end
|
|
51
|
-
|
|
52
49
|
def observer_not_fully_configured?(config)
|
|
53
|
-
if [config.observer_url, ENV.fetch("GLCI_OBSERVER_URL", nil)].none? { |opt|
|
|
50
|
+
if [config.observer_url, ENV.fetch("GLCI_OBSERVER_URL", nil)].none? { |opt| env_present?(opt) }
|
|
54
51
|
logger.warn("Observer url is not configured!. Set GLCI_OBSERVER_URL environment variable or set observer_url in the exporter config.") # rubocop:disable Layout/LineLength
|
|
55
52
|
return true
|
|
56
53
|
end
|
|
57
54
|
|
|
58
|
-
if [config.observer_token, ENV.fetch("GLCI_OBSERVER_AUTH_TOKEN", nil)].none? { |opt|
|
|
55
|
+
if [config.observer_token, ENV.fetch("GLCI_OBSERVER_AUTH_TOKEN", nil)].none? { |opt| env_present?(opt) }
|
|
59
56
|
logger.warn("Observer auth token is not configured!. Set GLCI_OBSERVER_AUTH_TOKEN environment variable or set observer_token in the exporter config.") # rubocop:disable Layout/LineLength
|
|
60
57
|
return true
|
|
61
58
|
end
|
|
@@ -64,47 +61,21 @@ module Gitlab
|
|
|
64
61
|
end
|
|
65
62
|
|
|
66
63
|
def configure_exporter!(config, run_type)
|
|
67
|
-
config.run_type = run_type || default_run_type unless
|
|
68
|
-
config.custom_metrics_proc = custom_metrics_proc
|
|
64
|
+
config.run_type = run_type || default_run_type unless env_present?(config.run_type)
|
|
69
65
|
|
|
70
66
|
configure_observer!(config)
|
|
71
67
|
end
|
|
72
68
|
|
|
73
69
|
def configure_observer!(config)
|
|
74
|
-
config.observer_url = observer_url unless
|
|
75
|
-
config.observer_token = observer_token unless
|
|
70
|
+
config.observer_url = observer_url unless env_present?(config.observer_url)
|
|
71
|
+
config.observer_token = observer_token unless env_present?(config.observer_token)
|
|
76
72
|
end
|
|
77
73
|
|
|
78
74
|
def warn_missing_observer_variables
|
|
79
|
-
missing = REQUIRED_OBSERVER_ENV_VARS.reject { |var|
|
|
75
|
+
missing = REQUIRED_OBSERVER_ENV_VARS.reject { |var| env_present?(ENV.fetch(var, nil)) }
|
|
80
76
|
logger.warn("Test metrics export is enabled but missing environment variables: #{missing.join(', ')}")
|
|
81
77
|
end
|
|
82
78
|
|
|
83
|
-
def custom_metrics_proc
|
|
84
|
-
proc do |_example|
|
|
85
|
-
{ pipeline_type: pipeline_type, ci_pipeline_id: ci_pipeline_id }
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
def default_branch?
|
|
90
|
-
ENV["CI_COMMIT_REF_NAME"] == ENV["CI_DEFAULT_BRANCH"]
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
def pipeline_type
|
|
94
|
-
@pipeline_type ||= detect_pipeline_type
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
def detect_pipeline_type
|
|
98
|
-
return "default_branch_scheduled_pipeline" if default_branch? && present?(ENV.fetch("SCHEDULE_TYPE", nil))
|
|
99
|
-
return "default_branch_pipeline" if default_branch?
|
|
100
|
-
return "stable_branch_pipeline" if ENV["CI_COMMIT_REF_NAME"]&.match?(STABLE_EE_BRANCH_REGEX)
|
|
101
|
-
return "backport_merge_request_pipeline" if ENV["CI_MERGE_REQUEST_TARGET_BRANCH_NAME"]&.match?(STABLE_EE_BRANCH_REGEX)
|
|
102
|
-
return "merge_request_pipeline" if present?(ENV["CI_MERGE_REQUEST_IID"])
|
|
103
|
-
return "downstream_pipeline" if ENV["CI_PIPELINE_SOURCE"] == "pipeline"
|
|
104
|
-
|
|
105
|
-
"unknown"
|
|
106
|
-
end
|
|
107
|
-
|
|
108
79
|
def default_run_type
|
|
109
80
|
@default_run_type ||= ENV.fetch("GLCI_TEST_METRICS_RUN_TYPE") { ENV.fetch("CI_JOB_NAME", nil) }
|
|
110
81
|
end
|
|
@@ -116,10 +87,6 @@ module Gitlab
|
|
|
116
87
|
def observer_token
|
|
117
88
|
ENV.fetch("GLCI_OBSERVER_AUTH_TOKEN", nil)
|
|
118
89
|
end
|
|
119
|
-
|
|
120
|
-
def ci_pipeline_id
|
|
121
|
-
(ENV["PARENT_PIPELINE_ID"] || ENV.fetch("CI_PIPELINE_ID", nil)).to_i
|
|
122
|
-
end
|
|
123
90
|
end
|
|
124
91
|
end
|
|
125
92
|
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Gitlab
|
|
4
|
+
module RSpecMetricsExporter
|
|
5
|
+
module Env
|
|
6
|
+
# Fetch an env var, returning nil if it is unset or empty.
|
|
7
|
+
#
|
|
8
|
+
# @param name [String]
|
|
9
|
+
# @return [String, nil]
|
|
10
|
+
def env_fetch(name)
|
|
11
|
+
value = ENV.fetch(name, nil)
|
|
12
|
+
env_present?(value) ? value : nil
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# @param value [Object]
|
|
16
|
+
# @return [Boolean]
|
|
17
|
+
def env_present?(value)
|
|
18
|
+
!value.nil? && !value.to_s.empty?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Parse a string into an Integer, returning nil for nil, empty, or
|
|
22
|
+
# non-numeric input. Strict: trailing non-digits are rejected.
|
|
23
|
+
#
|
|
24
|
+
# @param value [String, nil]
|
|
25
|
+
# @return [Integer, nil]
|
|
26
|
+
def env_parse_int_or_nil(value)
|
|
27
|
+
return nil unless env_present?(value)
|
|
28
|
+
|
|
29
|
+
Integer(value, 10)
|
|
30
|
+
rescue ArgumentError, TypeError
|
|
31
|
+
nil
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "env"
|
|
4
|
+
|
|
5
|
+
module Gitlab
|
|
6
|
+
module RSpecMetricsExporter
|
|
7
|
+
# Pipeline classification and CI pipeline id resolution.
|
|
8
|
+
#
|
|
9
|
+
# Mixed into the metric builder to expose `pipeline_type` and
|
|
10
|
+
# `ci_pipeline_id` derived from CI environment variables. Methods are
|
|
11
|
+
# instance methods on the including class.
|
|
12
|
+
module Pipeline
|
|
13
|
+
include Env
|
|
14
|
+
|
|
15
|
+
STABLE_EE_BRANCH_REGEX = /^[\d-]+-stable-ee$/
|
|
16
|
+
|
|
17
|
+
# @return [String] GitLab pipeline classification, or "unknown".
|
|
18
|
+
def pipeline_type
|
|
19
|
+
return "default_branch_scheduled_pipeline" if default_branch? && env_fetch("SCHEDULE_TYPE")
|
|
20
|
+
return "default_branch_pipeline" if default_branch?
|
|
21
|
+
return "stable_branch_pipeline" if stable_ee_branch?("CI_COMMIT_REF_NAME")
|
|
22
|
+
return "backport_merge_request_pipeline" if stable_ee_branch?("CI_MERGE_REQUEST_TARGET_BRANCH_NAME")
|
|
23
|
+
return "merge_request_pipeline" if env_fetch("CI_MERGE_REQUEST_IID")
|
|
24
|
+
return "downstream_pipeline" if env_fetch("CI_PIPELINE_SOURCE") == "pipeline"
|
|
25
|
+
|
|
26
|
+
"unknown"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# @return [Integer, nil] PARENT_PIPELINE_ID when set, otherwise CI_PIPELINE_ID.
|
|
30
|
+
def ci_pipeline_id
|
|
31
|
+
env_parse_int_or_nil(env_fetch("PARENT_PIPELINE_ID") || env_fetch("CI_PIPELINE_ID"))
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def default_branch?
|
|
37
|
+
ref = env_fetch("CI_COMMIT_REF_NAME")
|
|
38
|
+
!ref.nil? && ref == env_fetch("CI_DEFAULT_BRANCH")
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def stable_ee_branch?(env_var)
|
|
42
|
+
env_fetch(env_var)&.match?(STABLE_EE_BRANCH_REGEX) || false
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -4,10 +4,14 @@ require "openssl"
|
|
|
4
4
|
require "time"
|
|
5
5
|
|
|
6
6
|
require_relative "config"
|
|
7
|
+
require_relative "env"
|
|
8
|
+
require_relative "pipeline"
|
|
7
9
|
|
|
8
10
|
module Gitlab
|
|
9
11
|
module RSpecMetricsExporter
|
|
10
12
|
class TestMetrics
|
|
13
|
+
include Pipeline
|
|
14
|
+
|
|
11
15
|
def initialize(example, timestamp)
|
|
12
16
|
@example = example
|
|
13
17
|
@timestamp = timestamp
|
|
@@ -55,7 +59,8 @@ module Gitlab
|
|
|
55
59
|
feature_category: example.metadata[:feature_category] || "",
|
|
56
60
|
test_retried: config.test_retried_proc.call(example),
|
|
57
61
|
run_type: run_type,
|
|
58
|
-
spec_file_path_prefix: config.spec_file_path_prefix
|
|
62
|
+
spec_file_path_prefix: config.spec_file_path_prefix,
|
|
63
|
+
pipeline_type: pipeline_type
|
|
59
64
|
}
|
|
60
65
|
end
|
|
61
66
|
|
|
@@ -64,15 +69,17 @@ module Gitlab
|
|
|
64
69
|
# @return [Hash]
|
|
65
70
|
def ci_metrics
|
|
66
71
|
{
|
|
67
|
-
ci_project_id:
|
|
68
|
-
ci_project_path:
|
|
72
|
+
ci_project_id: env_parse_int_or_nil(env_fetch("CI_PROJECT_ID")),
|
|
73
|
+
ci_project_path: env_fetch("CI_PROJECT_PATH"),
|
|
69
74
|
ci_job_name: ci_job_name,
|
|
70
|
-
ci_job_id:
|
|
71
|
-
ci_pipeline_id:
|
|
72
|
-
ci_merge_request_iid: (
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
75
|
+
ci_job_id: env_parse_int_or_nil(env_fetch("CI_JOB_ID")),
|
|
76
|
+
ci_pipeline_id: ci_pipeline_id,
|
|
77
|
+
ci_merge_request_iid: env_parse_int_or_nil(
|
|
78
|
+
env_fetch("CI_MERGE_REQUEST_IID") || env_fetch("TOP_UPSTREAM_MERGE_REQUEST_IID")
|
|
79
|
+
),
|
|
80
|
+
ci_branch: env_fetch("CI_COMMIT_REF_NAME"),
|
|
81
|
+
ci_target_branch: env_fetch("CI_MERGE_REQUEST_TARGET_BRANCH_NAME"),
|
|
82
|
+
ci_server_url: env_fetch("CI_SERVER_URL")
|
|
76
83
|
}
|
|
77
84
|
end
|
|
78
85
|
|
|
@@ -122,7 +129,7 @@ module Gitlab
|
|
|
122
129
|
#
|
|
123
130
|
# @return [String]
|
|
124
131
|
def ci_job_name
|
|
125
|
-
|
|
132
|
+
env_fetch("CI_JOB_NAME")&.gsub(%r{ \d{1,2}/\d{1,2}}, "")
|
|
126
133
|
end
|
|
127
134
|
|
|
128
135
|
# Example location
|
|
@@ -187,16 +194,6 @@ module Gitlab
|
|
|
187
194
|
config.run_type || ci_job_name || "unknown"
|
|
188
195
|
end
|
|
189
196
|
|
|
190
|
-
# Return non empty environment variable value
|
|
191
|
-
#
|
|
192
|
-
# @param [String] name
|
|
193
|
-
# @return [String, nil]
|
|
194
|
-
def env(name)
|
|
195
|
-
return unless ENV[name] && !ENV[name].empty?
|
|
196
|
-
|
|
197
|
-
ENV.fetch(name)
|
|
198
|
-
end
|
|
199
|
-
|
|
200
197
|
# Metrics value cast to a valid type
|
|
201
198
|
#
|
|
202
199
|
# @param value [Object]
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: gitlab-rspec-metrics-exporter
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Developer Experience
|
|
@@ -159,7 +159,9 @@ files:
|
|
|
159
159
|
- lib/gitlab/rspec_metrics_exporter/client.rb
|
|
160
160
|
- lib/gitlab/rspec_metrics_exporter/config.rb
|
|
161
161
|
- lib/gitlab/rspec_metrics_exporter/config_helper.rb
|
|
162
|
+
- lib/gitlab/rspec_metrics_exporter/env.rb
|
|
162
163
|
- lib/gitlab/rspec_metrics_exporter/formatter.rb
|
|
164
|
+
- lib/gitlab/rspec_metrics_exporter/pipeline.rb
|
|
163
165
|
- lib/gitlab/rspec_metrics_exporter/test_metrics.rb
|
|
164
166
|
- lib/gitlab/rspec_metrics_exporter/version.rb
|
|
165
167
|
homepage: https://gitlab.com/gitlab-org/quality/analytics/test-metrics-exporters
|
|
@@ -183,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
183
185
|
- !ruby/object:Gem::Version
|
|
184
186
|
version: '0'
|
|
185
187
|
requirements: []
|
|
186
|
-
rubygems_version: 4.0.
|
|
188
|
+
rubygems_version: 4.0.10
|
|
187
189
|
specification_version: 4
|
|
188
190
|
summary: RSpec formatter that exports test execution metrics to GitLab Observer.
|
|
189
191
|
test_files: []
|