diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d29a6eaca..c0f63490e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ----------------- diff --git a/docs/sources/reference/components/prometheus/prometheus.exporter.cloudwatch.md b/docs/sources/reference/components/prometheus/prometheus.exporter.cloudwatch.md index dc45e8b0a6..09bb1cd1b0 100644 --- a/docs/sources/reference/components/prometheus/prometheus.exporter.cloudwatch.md +++ b/docs/sources/reference/components/prometheus/prometheus.exporter.cloudwatch.md @@ -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 diff --git a/internal/component/prometheus/exporter/cloudwatch/config.go b/internal/component/prometheus/exporter/cloudwatch/config.go index 96c879f630..6f8ac98d12 100644 --- a/internal/component/prometheus/exporter/cloudwatch/config.go +++ b/internal/component/prometheus/exporter/cloudwatch/config.go @@ -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 @@ -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. @@ -274,7 +271,7 @@ func toYACEMetrics(ms []Metric, jobNilToZero *bool) []*yaceConf.Metric { Delay: 0, NilToZero: nilToZero, - AddCloudwatchTimestamp: &addCloudwatchTimestamp, + AddCloudwatchTimestamp: m.AddCloudwatchTimestamp, }) } return yaceMetrics diff --git a/internal/component/prometheus/exporter/cloudwatch/config_test.go b/internal/component/prometheus/exporter/cloudwatch/config_test.go index 7870538e15..3d772da794 100644 --- a/internal/component/prometheus/exporter/cloudwatch/config_test.go +++ b/internal/component/prometheus/exporter/cloudwatch/config_test.go @@ -208,6 +208,7 @@ custom_namespace "customEC2Metrics" { name = "cpu_usage_idle" statistics = ["Average"] period = "5m" + add_cloudwatch_timestamp = true } metric { @@ -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 } } ` @@ -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, }}, }, }, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, }}, }, }, @@ -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, }}, }, }, @@ -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, @@ -552,7 +543,7 @@ func TestCloudwatchComponentConfig(t *testing.T) { Length: 300, Delay: 0, NilToZero: falsePtr, - AddCloudwatchTimestamp: addCloudwatchTimestamp, + AddCloudwatchTimestamp: truePtr, }, { Name: "disk_free", @@ -561,7 +552,7 @@ func TestCloudwatchComponentConfig(t *testing.T) { Length: 300, Delay: 0, NilToZero: truePtr, - AddCloudwatchTimestamp: addCloudwatchTimestamp, + AddCloudwatchTimestamp: falsePtr, }, }, RoundingPeriod: nil,