Skip to content

Commit 8ac3098

Browse files
prometheus.exporter.cloudwatch: add add_cloudwatch_timestamp field to metrics (#2043)
* add add_cloudwatch_timestamp field to cloudwatch metrics * changelog * basic docs * Update docs/sources/reference/components/prometheus/prometheus.exporter.cloudwatch.md Co-authored-by: Clayton Cornell <[email protected]> * fix changelog --------- Co-authored-by: Clayton Cornell <[email protected]>
1 parent 23fc404 commit 8ac3098

File tree

4 files changed

+82
-89
lines changed

4 files changed

+82
-89
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ internal API changes are not present.
1010
Main (unreleased)
1111
-----------------
1212

13+
### Features
14+
15+
- Add `add_cloudwatch_timestamp` to `prometheus.exporter.cloudwatch` metrics. (@captncraig)
16+
1317
v1.5.0-rc.0
1418
-----------------
1519

docs/sources/reference/components/prometheus/prometheus.exporter.cloudwatch.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ on how to explore metrics, to easily pick the ones you need.
317317
| `period` | `duration` | Refer to the [period][] section below. | | yes |
318318
| `length` | `duration` | Refer to the [period][] section below. | Calculated based on `period`. Refer to [period][] for details. | no |
319319
| `nil_to_zero` | `bool` | When `true`, `NaN` metric values are converted to 0. | The value of `nil_to_zero` in the parent [static][] or [discovery][] block. `true` if not set in the parent block. | no |
320+
| `add_cloudwatch_timestamp` | `bool` | When `true`, use the timestamp from CloudWatch instead of the scrape time. | `false` | no
320321

321322
[period]: #period-and-length
322323

internal/component/prometheus/exporter/cloudwatch/config.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ import (
1515
"github.com/grafana/alloy/syntax"
1616
)
1717

18-
// Since we are gathering metrics from CloudWatch and writing them in prometheus during each scrape, the timestamp
19-
// used should be the scrape one
20-
var addCloudwatchTimestamp = false
21-
2218
// Avoid producing absence of values in metrics
2319
var defaultNilToZero = true
2420

@@ -113,11 +109,12 @@ type Role struct {
113109
type Dimensions map[string]string
114110

115111
type Metric struct {
116-
Name string `alloy:"name,attr"`
117-
Statistics []string `alloy:"statistics,attr"`
118-
Period time.Duration `alloy:"period,attr"`
119-
Length time.Duration `alloy:"length,attr,optional"`
120-
NilToZero *bool `alloy:"nil_to_zero,attr,optional"`
112+
Name string `alloy:"name,attr"`
113+
Statistics []string `alloy:"statistics,attr"`
114+
Period time.Duration `alloy:"period,attr"`
115+
Length time.Duration `alloy:"length,attr,optional"`
116+
NilToZero *bool `alloy:"nil_to_zero,attr,optional"`
117+
AddCloudwatchTimestamp *bool `alloy:"add_cloudwatch_timestamp,attr,optional"`
121118
}
122119

123120
// SetToDefault implements syntax.Defaulter.
@@ -274,7 +271,7 @@ func toYACEMetrics(ms []Metric, jobNilToZero *bool) []*yaceConf.Metric {
274271
Delay: 0,
275272

276273
NilToZero: nilToZero,
277-
AddCloudwatchTimestamp: &addCloudwatchTimestamp,
274+
AddCloudwatchTimestamp: m.AddCloudwatchTimestamp,
278275
})
279276
}
280277
return yaceMetrics

internal/component/prometheus/exporter/cloudwatch/config_test.go

Lines changed: 70 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ custom_namespace "customEC2Metrics" {
208208
name = "cpu_usage_idle"
209209
statistics = ["Average"]
210210
period = "5m"
211+
add_cloudwatch_timestamp = true
211212
}
212213
213214
metric {
@@ -216,6 +217,7 @@ custom_namespace "customEC2Metrics" {
216217
period = "5m"
217218
// setting nil_to_zero on the metric level
218219
nil_to_zero = true
220+
add_cloudwatch_timestamp = false
219221
}
220222
}
221223
`
@@ -261,13 +263,12 @@ func TestCloudwatchComponentConfig(t *testing.T) {
261263
},
262264
},
263265
Metrics: []*yaceModel.MetricConfig{{
264-
Name: "CPUUsage",
265-
Statistics: []string{"Sum", "Average"},
266-
Period: 60,
267-
Length: 60,
268-
Delay: 0,
269-
NilToZero: defaultNilToZero,
270-
AddCloudwatchTimestamp: addCloudwatchTimestamp,
266+
Name: "CPUUsage",
267+
Statistics: []string{"Sum", "Average"},
268+
Period: 60,
269+
Length: 60,
270+
Delay: 0,
271+
NilToZero: defaultNilToZero,
271272
}},
272273
},
273274
},
@@ -290,22 +291,20 @@ func TestCloudwatchComponentConfig(t *testing.T) {
290291
CustomTags: []yaceModel.Tag{},
291292
Metrics: []*yaceModel.MetricConfig{
292293
{
293-
Name: "NumberOfMessagesSent",
294-
Statistics: []string{"Sum", "Average"},
295-
Period: 60,
296-
Length: 60,
297-
Delay: 0,
298-
NilToZero: defaultNilToZero,
299-
AddCloudwatchTimestamp: addCloudwatchTimestamp,
294+
Name: "NumberOfMessagesSent",
295+
Statistics: []string{"Sum", "Average"},
296+
Period: 60,
297+
Length: 60,
298+
Delay: 0,
299+
NilToZero: defaultNilToZero,
300300
},
301301
{
302-
Name: "NumberOfMessagesReceived",
303-
Statistics: []string{"Sum", "Average"},
304-
Period: 60,
305-
Length: 60,
306-
Delay: 0,
307-
NilToZero: defaultNilToZero,
308-
AddCloudwatchTimestamp: addCloudwatchTimestamp,
302+
Name: "NumberOfMessagesReceived",
303+
Statistics: []string{"Sum", "Average"},
304+
Period: 60,
305+
Length: 60,
306+
Delay: 0,
307+
NilToZero: defaultNilToZero,
309308
},
310309
},
311310
RoundingPeriod: nil,
@@ -327,13 +326,12 @@ func TestCloudwatchComponentConfig(t *testing.T) {
327326
CustomTags: []yaceModel.Tag{},
328327
Metrics: []*yaceModel.MetricConfig{
329328
{
330-
Name: "CPUUtilization",
331-
Statistics: []string{"Sum", "Maximum"},
332-
Period: 60,
333-
Length: 60,
334-
Delay: 0,
335-
NilToZero: defaultNilToZero,
336-
AddCloudwatchTimestamp: addCloudwatchTimestamp,
329+
Name: "CPUUtilization",
330+
Statistics: []string{"Sum", "Maximum"},
331+
Period: 60,
332+
Length: 60,
333+
Delay: 0,
334+
NilToZero: defaultNilToZero,
337335
},
338336
},
339337
RoundingPeriod: nil,
@@ -360,13 +358,12 @@ func TestCloudwatchComponentConfig(t *testing.T) {
360358
DimensionNameRequirements: []string{"BucketName"},
361359
Metrics: []*yaceModel.MetricConfig{
362360
{
363-
Name: "BucketSizeBytes",
364-
Statistics: []string{"Sum"},
365-
Period: 60,
366-
Length: 3600,
367-
Delay: 0,
368-
NilToZero: defaultNilToZero,
369-
AddCloudwatchTimestamp: addCloudwatchTimestamp,
361+
Name: "BucketSizeBytes",
362+
Statistics: []string{"Sum"},
363+
Period: 60,
364+
Length: 3600,
365+
Delay: 0,
366+
NilToZero: defaultNilToZero,
370367
},
371368
},
372369
RoundingPeriod: nil,
@@ -397,22 +394,20 @@ func TestCloudwatchComponentConfig(t *testing.T) {
397394
Namespace: "CustomEC2Metrics",
398395
Metrics: []*yaceModel.MetricConfig{
399396
{
400-
Name: "cpu_usage_idle",
401-
Statistics: []string{"Average"},
402-
Period: 300,
403-
Length: 300,
404-
Delay: 0,
405-
NilToZero: defaultNilToZero,
406-
AddCloudwatchTimestamp: addCloudwatchTimestamp,
397+
Name: "cpu_usage_idle",
398+
Statistics: []string{"Average"},
399+
Period: 300,
400+
Length: 300,
401+
Delay: 0,
402+
NilToZero: defaultNilToZero,
407403
},
408404
{
409-
Name: "disk_free",
410-
Statistics: []string{"Average"},
411-
Period: 300,
412-
Length: 300,
413-
Delay: 0,
414-
NilToZero: defaultNilToZero,
415-
AddCloudwatchTimestamp: addCloudwatchTimestamp,
405+
Name: "disk_free",
406+
Statistics: []string{"Average"},
407+
Period: 300,
408+
Length: 300,
409+
Delay: 0,
410+
NilToZero: defaultNilToZero,
416411
},
417412
},
418413
RoundingPeriod: nil,
@@ -440,13 +435,12 @@ func TestCloudwatchComponentConfig(t *testing.T) {
440435
},
441436
},
442437
Metrics: []*yaceModel.MetricConfig{{
443-
Name: "CPUUsage",
444-
Statistics: []string{"Sum", "Average"},
445-
Period: 60,
446-
Length: 60,
447-
Delay: 0,
448-
NilToZero: falsePtr,
449-
AddCloudwatchTimestamp: addCloudwatchTimestamp,
438+
Name: "CPUUsage",
439+
Statistics: []string{"Sum", "Average"},
440+
Period: 60,
441+
Length: 60,
442+
Delay: 0,
443+
NilToZero: falsePtr,
450444
}},
451445
},
452446
},
@@ -472,13 +466,12 @@ func TestCloudwatchComponentConfig(t *testing.T) {
472466
},
473467
},
474468
Metrics: []*yaceModel.MetricConfig{{
475-
Name: "CPUUsage",
476-
Statistics: []string{"Sum", "Average"},
477-
Period: 60,
478-
Length: 60,
479-
Delay: 0,
480-
NilToZero: falsePtr,
481-
AddCloudwatchTimestamp: addCloudwatchTimestamp,
469+
Name: "CPUUsage",
470+
Statistics: []string{"Sum", "Average"},
471+
Period: 60,
472+
Length: 60,
473+
Delay: 0,
474+
NilToZero: falsePtr,
482475
}},
483476
},
484477
},
@@ -501,22 +494,20 @@ func TestCloudwatchComponentConfig(t *testing.T) {
501494
CustomTags: []yaceModel.Tag{},
502495
Metrics: []*yaceModel.MetricConfig{
503496
{
504-
Name: "NumberOfMessagesSent",
505-
Statistics: []string{"Sum", "Average"},
506-
Period: 60,
507-
Length: 60,
508-
Delay: 0,
509-
NilToZero: falsePtr,
510-
AddCloudwatchTimestamp: addCloudwatchTimestamp,
497+
Name: "NumberOfMessagesSent",
498+
Statistics: []string{"Sum", "Average"},
499+
Period: 60,
500+
Length: 60,
501+
Delay: 0,
502+
NilToZero: falsePtr,
511503
},
512504
{
513-
Name: "NumberOfMessagesReceived",
514-
Statistics: []string{"Sum", "Average"},
515-
Period: 60,
516-
Length: 60,
517-
Delay: 0,
518-
NilToZero: truePtr,
519-
AddCloudwatchTimestamp: addCloudwatchTimestamp,
505+
Name: "NumberOfMessagesReceived",
506+
Statistics: []string{"Sum", "Average"},
507+
Period: 60,
508+
Length: 60,
509+
Delay: 0,
510+
NilToZero: truePtr,
520511
},
521512
},
522513
RoundingPeriod: nil,
@@ -552,7 +543,7 @@ func TestCloudwatchComponentConfig(t *testing.T) {
552543
Length: 300,
553544
Delay: 0,
554545
NilToZero: falsePtr,
555-
AddCloudwatchTimestamp: addCloudwatchTimestamp,
546+
AddCloudwatchTimestamp: truePtr,
556547
},
557548
{
558549
Name: "disk_free",
@@ -561,7 +552,7 @@ func TestCloudwatchComponentConfig(t *testing.T) {
561552
Length: 300,
562553
Delay: 0,
563554
NilToZero: truePtr,
564-
AddCloudwatchTimestamp: addCloudwatchTimestamp,
555+
AddCloudwatchTimestamp: falsePtr,
565556
},
566557
},
567558
RoundingPeriod: nil,

0 commit comments

Comments
 (0)