-
-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathmodel.go
More file actions
290 lines (243 loc) · 7.65 KB
/
Copy pathmodel.go
File metadata and controls
290 lines (243 loc) · 7.65 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright 2024 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package model
import (
"time"
"github.com/grafana/regexp"
)
const (
DefaultPeriodSeconds = int64(300)
DefaultLengthSeconds = int64(300)
)
type JobsConfig struct {
StsRegion string
DiscoveryJobs []DiscoveryJob
StaticJobs []StaticJob
CustomNamespaceJobs []CustomNamespaceJob
}
type DiscoveryJob struct {
Regions []string
Namespace string
Roles []Role
SearchTags []SearchTag
CustomTags []Tag
DimensionNameRequirements []string
Metrics []*MetricConfig
RoundingPeriod *int64
RecentlyActiveOnly bool
ExportedTagsOnMetrics []string
IncludeContextOnInfoMetrics bool
DimensionsRegexps []DimensionsRegexp
}
type StaticJob struct {
Name string
Regions []string
Roles []Role
Namespace string
CustomTags []Tag
Dimensions []Dimension
Metrics []*MetricConfig
}
type CustomNamespaceJob struct {
Regions []string
Name string
Namespace string
RoundingPeriod *int64
RecentlyActiveOnly bool
Roles []Role
Metrics []*MetricConfig
CustomTags []Tag
DimensionNameRequirements []string
}
type Role struct {
RoleArn string
ExternalID string
}
type MetricConfig struct {
Name string
Statistics []string
Period int64
Length int64
Delay int64
NilToZero bool
AddCloudwatchTimestamp bool
ExportAllDataPoints bool
}
type DimensionsRegexp struct {
Regexp *regexp.Regexp
DimensionsNames []string
}
type LabelSet map[string]struct{}
type Tag struct {
Key string
Value string
}
type SearchTag struct {
Key string
Value *regexp.Regexp // Used for regex matching (when ExactMatch is false)
ExactValue string // Used for exact matching (when ExactMatch is true)
ExactMatch bool // When true, use ExactValue for exact string comparison
}
type Dimension struct {
Name string
Value string
}
type Metric struct {
// The dimensions for the metric.
Dimensions []Dimension
MetricName string
Namespace string
}
type CloudwatchMetricResult struct {
Context *ScrapeContext
Data []*CloudwatchData
}
type TaggedResourceResult struct {
Context *ScrapeContext
Data []*TaggedResource
}
type ScrapeContext struct {
Region string
AccountID string
AccountAlias string
CustomTags []Tag
}
// CloudwatchData is an internal representation of a CloudWatch
// metric with attached data points, metric and resource information.
type CloudwatchData struct {
MetricName string
// ResourceName will have different values depending on the job type
// DiscoveryJob = Resource ARN associated with the metric or global when it could not be associated but shouldn't be dropped
// StaticJob = Resource Name from static job config
// CustomNamespace = Custom Namespace job name
ResourceName string
Namespace string
Tags []Tag
Dimensions []Dimension
// GetMetricDataProcessingParams includes necessary fields to run GetMetricData
GetMetricDataProcessingParams *GetMetricDataProcessingParams
// MetricMigrationParams holds configuration values necessary when migrating the resulting metrics
MetricMigrationParams MetricMigrationParams
// GetMetricsDataResult is an optional field and will be non-nil when metric data was populated from the GetMetricsData API (Discovery and CustomNamespace jobs)
GetMetricDataResult *GetMetricDataResult
// GetMetricStatisticsResult is an optional field and will be non-nil when metric data was populated from the GetMetricStatistics API (static jobs)
GetMetricStatisticsResult *GetMetricStatisticsResult
}
type GetMetricStatisticsResult struct {
Results []*MetricStatisticsResult
Statistics []string
}
type MetricStatisticsResult struct {
// The average of the metric values that correspond to the data point.
Average *float64
// The percentile statistic for the data point.
ExtendedStatistics map[string]*float64
// The maximum metric value for the data point.
Maximum *float64
// The minimum metric value for the data point.
Minimum *float64
// The number of metric values that contributed to the aggregate value of this
// data point.
SampleCount *float64
// The sum of the metric values for the data point.
Sum *float64
// The time stamp used for the data point.
Timestamp *time.Time
}
type GetMetricDataProcessingParams struct {
// QueryID is a value internal to processing used for mapping results from GetMetricData their original request
QueryID string
// The statistic to be used to call GetMetricData
Statistic string
// Fields which impact the start and endtime for
Period int64
Length int64
Delay int64
}
type MetricMigrationParams struct {
NilToZero bool
AddCloudwatchTimestamp bool
ExportAllDataPoints bool
}
type GetMetricDataResult struct {
Statistic string
DataPoints []DataPoint
}
type DataPoint struct {
Value *float64
Timestamp time.Time
}
// TaggedResource is an AWS resource with tags
type TaggedResource struct {
// ARN is the unique AWS ARN (Amazon Resource Name) of the resource
ARN string
// Namespace identifies the resource type (e.g. EC2)
Namespace string
// Region is the AWS regions that the resource belongs to
Region string
// Tags is a set of tags associated to the resource
Tags []Tag
}
// FilterThroughTags returns true if all filterTags match
// with tags of the TaggedResource, returns false otherwise.
func (r TaggedResource) FilterThroughTags(filterTags []SearchTag) bool {
if len(filterTags) == 0 {
return true
}
tagFilterMatches := 0
for _, resourceTag := range r.Tags {
for _, filterTag := range filterTags {
if resourceTag.Key == filterTag.Key {
var matches bool
if filterTag.ExactMatch {
matches = resourceTag.Value == filterTag.ExactValue
} else {
matches = filterTag.Value.MatchString(resourceTag.Value)
}
if !matches {
return false
}
// A resource needs to match all SearchTags to be returned, so we track the number of tag filter
// matches to ensure it matches the number of tag filters at the end
tagFilterMatches++
}
}
}
return tagFilterMatches == len(filterTags)
}
// MetricTags returns a list of tags built from the tags of
// TaggedResource, if exportedTags is not empty.
//
// Returned tags have as key the key from exportedTags, and
// as value the value from the corresponding tag of the resource,
// if it exists (otherwise an empty string).
func (r TaggedResource) MetricTags(exportedTags []string) []Tag {
if len(exportedTags) == 0 {
return []Tag{}
}
tags := make([]Tag, 0, len(exportedTags))
for _, tagName := range exportedTags {
tag := Tag{
Key: tagName,
}
for _, resourceTag := range r.Tags {
if resourceTag.Key == tagName {
tag.Value = resourceTag.Value
break
}
}
// Always add the tag, even if it's empty, to ensure the same labels are present on all metrics for a single service
tags = append(tags, tag)
}
return tags
}