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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# frozen_string_literal: true
module Gitlab
module Usage
class MetricDefinition
METRIC_SCHEMA_PATH = Rails.root.join('config', 'metrics', 'schema', '**', '*.json')
AVAILABLE_STATUSES = %w[active broken].to_set.freeze
VALID_SERVICE_PING_STATUSES = %w[active broken].to_set.freeze
InvalidError = Class.new(RuntimeError)
attr_reader :path
attr_reader :attributes
def initialize(path, opts = {})
@path = path
@attributes = opts
end
def key
key_path
end
def events
events_from_new_structure || events_from_old_structure || {}
end
def to_context
return unless %w[redis redis_hll].include?(data_source)
Gitlab::Tracking::ServicePingContext.new(data_source: data_source, event: events.each_key.first)
end
def to_h
attributes
end
def json_schema
return unless has_json_schema?
@json_schema ||= Gitlab::Json.parse(File.read(json_schema_path))
end
def json_schema_path
return '' unless has_json_schema?
Rails.root.join(attributes[:value_json_schema])
end
def has_json_schema?
attributes[:value_type] == 'object' && attributes[:value_json_schema].present?
end
def validate!
errors.each do |error|
error_message = <<~ERROR_MSG
Error type: #{error['type']}
Data: #{error['data']}
Path: #{error['data_pointer']}
Details: #{error['details']}
Metric file: #{path}
ERROR_MSG
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(InvalidError.new(error_message))
end
end
def category_to_lowercase
attributes[:data_category]&.downcase!
end
def available?
AVAILABLE_STATUSES.include?(attributes[:status])
end
def valid_service_ping_status?
VALID_SERVICE_PING_STATUSES.include?(attributes[:status])
end
alias_method :to_dictionary, :to_h
class << self
def paths
@paths ||= [Rails.root.join('config', 'metrics', '[^agg]*', '*.yml')]
end
def definitions
@definitions ||= load_all!
end
def all
@all ||= definitions.map { |_key_path, definition| definition }
end
def not_removed
all.select { |definition| definition.attributes[:status] != 'removed' }.index_by(&:key_path)
end
def with_instrumentation_class
all.select { |definition| definition.attributes[:instrumentation_class].present? && definition.available? }
end
def context_for(key_path)
definitions[key_path]&.to_context
end
def schemers
@schemers ||= Dir[METRIC_SCHEMA_PATH].map do |path|
::JSONSchemer.schema(Pathname.new(path))
end
end
def dump_metrics_yaml
@metrics_yaml ||= definitions.values.map(&:to_h).map(&:deep_stringify_keys).to_yaml
end
private
def load_all!
paths.each_with_object({}) do |glob_path, definitions|
load_all_from_path!(definitions, glob_path)
end
end
def load_from_file(path)
definition = File.read(path)
definition = YAML.safe_load(definition)
definition.deep_symbolize_keys!
self.new(path, definition).tap(&:category_to_lowercase)
rescue StandardError => e
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(InvalidError.new(e.message))
end
def load_all_from_path!(definitions, glob_path)
Dir.glob(glob_path).each do |path|
definition = load_from_file(path)
if previous = definitions[definition.key]
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(InvalidError.new("Metric '#{definition.key}' is already defined in '#{previous.path}'"))
end
definitions[definition.key] = definition
end
end
end
private
def errors
result = []
self.class.schemers.each do |schemer|
# schemer.validate returns an Enumerator object
schemer.validate(attributes.deep_stringify_keys).each do |error|
result << error
end
end
result
end
def method_missing(method, *args)
attributes[method] || super
end
def respond_to_missing?(method, *args)
attributes[method].present? || super
end
def events_from_new_structure
events = attributes[:events]
return unless events
events.to_h { |event| [event[:name], event[:unique]&.to_sym] }
end
def events_from_old_structure
options_events = attributes.dig(:options, :events)
return unless options_events
options_events.index_with { nil }
end
end
end
end
Gitlab::Usage::MetricDefinition.prepend_mod_with('Gitlab::Usage::MetricDefinition')
|