Skip to content

Commit

Permalink
prometheus.exporter.cloudwatch: add add_cloudwatch_timestamp field …
Browse files Browse the repository at this point in the history
…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 <131809008+clayton-cornell@users.noreply.github.com>

* fix changelog

---------

Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>
  • Loading branch information
captncraig and clayton-cornell authored Nov 6, 2024
1 parent 23fc404 commit 8ac3098
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 89 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ internal API changes are not present.
Main (unreleased)
-----------------

### Features

- Add `add_cloudwatch_timestamp` to `prometheus.exporter.cloudwatch` metrics. (@captncraig)

v1.5.0-rc.0
-----------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ on how to explore metrics, to easily pick the ones you need.
| `period` | `duration` | Refer to the [period][] section below. | | yes |
| `length` | `duration` | Refer to the [period][] section below. | Calculated based on `period`. Refer to [period][] for details. | no |
| `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 |
| `add_cloudwatch_timestamp` | `bool` | When `true`, use the timestamp from CloudWatch instead of the scrape time. | `false` | no

[period]: #period-and-length

Expand Down
17 changes: 7 additions & 10 deletions internal/component/prometheus/exporter/cloudwatch/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import (
"github.com/grafana/alloy/syntax"
)

// Since we are gathering metrics from CloudWatch and writing them in prometheus during each scrape, the timestamp
// used should be the scrape one
var addCloudwatchTimestamp = false

// Avoid producing absence of values in metrics
var defaultNilToZero = true

Expand Down Expand Up @@ -113,11 +109,12 @@ type Role struct {
type Dimensions map[string]string

type Metric struct {
Name string `alloy:"name,attr"`
Statistics []string `alloy:"statistics,attr"`
Period time.Duration `alloy:"period,attr"`
Length time.Duration `alloy:"length,attr,optional"`
NilToZero *bool `alloy:"nil_to_zero,attr,optional"`
Name string `alloy:"name,attr"`
Statistics []string `alloy:"statistics,attr"`
Period time.Duration `alloy:"period,attr"`
Length time.Duration `alloy:"length,attr,optional"`
NilToZero *bool `alloy:"nil_to_zero,attr,optional"`
AddCloudwatchTimestamp *bool `alloy:"add_cloudwatch_timestamp,attr,optional"`
}

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

NilToZero: nilToZero,
AddCloudwatchTimestamp: &addCloudwatchTimestamp,
AddCloudwatchTimestamp: m.AddCloudwatchTimestamp,
})
}
return yaceMetrics
Expand Down
149 changes: 70 additions & 79 deletions internal/component/prometheus/exporter/cloudwatch/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ custom_namespace "customEC2Metrics" {
name = "cpu_usage_idle"
statistics = ["Average"]
period = "5m"
add_cloudwatch_timestamp = true
}
metric {
Expand All @@ -216,6 +217,7 @@ custom_namespace "customEC2Metrics" {
period = "5m"
// setting nil_to_zero on the metric level
nil_to_zero = true
add_cloudwatch_timestamp = false
}
}
`
Expand Down Expand Up @@ -261,13 +263,12 @@ func TestCloudwatchComponentConfig(t *testing.T) {
},
},
Metrics: []*yaceModel.MetricConfig{{
Name: "CPUUsage",
Statistics: []string{"Sum", "Average"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: defaultNilToZero,
AddCloudwatchTimestamp: addCloudwatchTimestamp,
Name: "CPUUsage",
Statistics: []string{"Sum", "Average"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: defaultNilToZero,
}},
},
},
Expand All @@ -290,22 +291,20 @@ func TestCloudwatchComponentConfig(t *testing.T) {
CustomTags: []yaceModel.Tag{},
Metrics: []*yaceModel.MetricConfig{
{
Name: "NumberOfMessagesSent",
Statistics: []string{"Sum", "Average"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: defaultNilToZero,
AddCloudwatchTimestamp: addCloudwatchTimestamp,
Name: "NumberOfMessagesSent",
Statistics: []string{"Sum", "Average"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: defaultNilToZero,
},
{
Name: "NumberOfMessagesReceived",
Statistics: []string{"Sum", "Average"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: defaultNilToZero,
AddCloudwatchTimestamp: addCloudwatchTimestamp,
Name: "NumberOfMessagesReceived",
Statistics: []string{"Sum", "Average"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: defaultNilToZero,
},
},
RoundingPeriod: nil,
Expand All @@ -327,13 +326,12 @@ func TestCloudwatchComponentConfig(t *testing.T) {
CustomTags: []yaceModel.Tag{},
Metrics: []*yaceModel.MetricConfig{
{
Name: "CPUUtilization",
Statistics: []string{"Sum", "Maximum"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: defaultNilToZero,
AddCloudwatchTimestamp: addCloudwatchTimestamp,
Name: "CPUUtilization",
Statistics: []string{"Sum", "Maximum"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: defaultNilToZero,
},
},
RoundingPeriod: nil,
Expand All @@ -360,13 +358,12 @@ func TestCloudwatchComponentConfig(t *testing.T) {
DimensionNameRequirements: []string{"BucketName"},
Metrics: []*yaceModel.MetricConfig{
{
Name: "BucketSizeBytes",
Statistics: []string{"Sum"},
Period: 60,
Length: 3600,
Delay: 0,
NilToZero: defaultNilToZero,
AddCloudwatchTimestamp: addCloudwatchTimestamp,
Name: "BucketSizeBytes",
Statistics: []string{"Sum"},
Period: 60,
Length: 3600,
Delay: 0,
NilToZero: defaultNilToZero,
},
},
RoundingPeriod: nil,
Expand Down Expand Up @@ -397,22 +394,20 @@ func TestCloudwatchComponentConfig(t *testing.T) {
Namespace: "CustomEC2Metrics",
Metrics: []*yaceModel.MetricConfig{
{
Name: "cpu_usage_idle",
Statistics: []string{"Average"},
Period: 300,
Length: 300,
Delay: 0,
NilToZero: defaultNilToZero,
AddCloudwatchTimestamp: addCloudwatchTimestamp,
Name: "cpu_usage_idle",
Statistics: []string{"Average"},
Period: 300,
Length: 300,
Delay: 0,
NilToZero: defaultNilToZero,
},
{
Name: "disk_free",
Statistics: []string{"Average"},
Period: 300,
Length: 300,
Delay: 0,
NilToZero: defaultNilToZero,
AddCloudwatchTimestamp: addCloudwatchTimestamp,
Name: "disk_free",
Statistics: []string{"Average"},
Period: 300,
Length: 300,
Delay: 0,
NilToZero: defaultNilToZero,
},
},
RoundingPeriod: nil,
Expand Down Expand Up @@ -440,13 +435,12 @@ func TestCloudwatchComponentConfig(t *testing.T) {
},
},
Metrics: []*yaceModel.MetricConfig{{
Name: "CPUUsage",
Statistics: []string{"Sum", "Average"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: falsePtr,
AddCloudwatchTimestamp: addCloudwatchTimestamp,
Name: "CPUUsage",
Statistics: []string{"Sum", "Average"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: falsePtr,
}},
},
},
Expand All @@ -472,13 +466,12 @@ func TestCloudwatchComponentConfig(t *testing.T) {
},
},
Metrics: []*yaceModel.MetricConfig{{
Name: "CPUUsage",
Statistics: []string{"Sum", "Average"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: falsePtr,
AddCloudwatchTimestamp: addCloudwatchTimestamp,
Name: "CPUUsage",
Statistics: []string{"Sum", "Average"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: falsePtr,
}},
},
},
Expand All @@ -501,22 +494,20 @@ func TestCloudwatchComponentConfig(t *testing.T) {
CustomTags: []yaceModel.Tag{},
Metrics: []*yaceModel.MetricConfig{
{
Name: "NumberOfMessagesSent",
Statistics: []string{"Sum", "Average"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: falsePtr,
AddCloudwatchTimestamp: addCloudwatchTimestamp,
Name: "NumberOfMessagesSent",
Statistics: []string{"Sum", "Average"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: falsePtr,
},
{
Name: "NumberOfMessagesReceived",
Statistics: []string{"Sum", "Average"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: truePtr,
AddCloudwatchTimestamp: addCloudwatchTimestamp,
Name: "NumberOfMessagesReceived",
Statistics: []string{"Sum", "Average"},
Period: 60,
Length: 60,
Delay: 0,
NilToZero: truePtr,
},
},
RoundingPeriod: nil,
Expand Down Expand Up @@ -552,7 +543,7 @@ func TestCloudwatchComponentConfig(t *testing.T) {
Length: 300,
Delay: 0,
NilToZero: falsePtr,
AddCloudwatchTimestamp: addCloudwatchTimestamp,
AddCloudwatchTimestamp: truePtr,
},
{
Name: "disk_free",
Expand All @@ -561,7 +552,7 @@ func TestCloudwatchComponentConfig(t *testing.T) {
Length: 300,
Delay: 0,
NilToZero: truePtr,
AddCloudwatchTimestamp: addCloudwatchTimestamp,
AddCloudwatchTimestamp: falsePtr,
},
},
RoundingPeriod: nil,
Expand Down

0 comments on commit 8ac3098

Please sign in to comment.