Skip to content
This repository was archived by the owner on Mar 12, 2026. It is now read-only.

Commit d28e9f2

Browse files
author
Albin Kerouanton
authored
Add a flag to publish metrics with high resolution (#32)
So far, this tool is only publishing metrics with high resolution when the original prometheus metrics have the label __cw_high_res. However, in our case we're running exporters and prom-to-cloudwatch as sidecars of ECS tasks. This tool being directly connected to the exporters, we can't add the high res label.
1 parent 73e3615 commit d28e9f2

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"github.com/gobwas/glob"
1616
)
1717

18+
var defaultForceHighRes, _ = strconv.ParseBool(os.Getenv("FORCE_HIGH_RES"))
19+
1820
var (
1921
awsAccessKeyId = flag.String("aws_access_key_id", os.Getenv("AWS_ACCESS_KEY_ID"), "AWS access key Id with permissions to publish CloudWatch metrics")
2022
awsSecretAccessKey = flag.String("aws_secret_access_key", os.Getenv("AWS_SECRET_ACCESS_KEY"), "AWS secret access key with permissions to publish CloudWatch metrics")
@@ -33,6 +35,7 @@ var (
3335
excludeMetrics = flag.String("exclude_metrics", os.Getenv("EXCLUDE_METRICS"), "Never publish the specified metrics (comma-separated list of glob patterns, e.g. 'tomcat_*')")
3436
includeDimensionsForMetrics = flag.String("include_dimensions_for_metrics", os.Getenv("INCLUDE_DIMENSIONS_FOR_METRICS"), "Only publish the specified dimensions for metrics (semi-colon-separated key values of comma-separated dimensions of METRIC=dim1,dim2;, e.g. 'flink_jobmanager=job_id')")
3537
excludeDimensionsForMetrics = flag.String("exclude_dimensions_for_metrics", os.Getenv("EXCLUDE_DIMENSIONS_FOR_METRICS"), "Never publish the specified dimensions for metrics (semi-colon-separated key values of comma-separated dimensions of METRIC=dim1,dim2;, e.g. 'flink_jobmanager=job,host;zk_up=host,pod;')")
38+
forceHighRes = flag.Bool("force_high_res", defaultForceHighRes, "Publish all metrics with high resolution, even when original metrics don't have the label "+cwHighResLabel)
3639
)
3740

3841
// kevValMustParse takes a string and exits with a message if it cannot parse as KEY=VALUE
@@ -180,6 +183,7 @@ func main() {
180183
ExcludeMetrics: excludeMetricsList,
181184
ExcludeDimensionsForMetrics: excludeDimensionsForMetricsList,
182185
IncludeDimensionsForMetrics: includeDimensionsForMetricsList,
186+
ForceHighRes: *forceHighRes,
183187
}
184188

185189
if *prometheusScrapeInterval != "" {

prometheus_to_cloudwatch.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ type Config struct {
108108

109109
// Exclude certain dimensions from the specified metrics
110110
ExcludeDimensionsForMetrics []MatcherWithStringSet
111+
112+
// ForceHighRes forces all exported metrics to be sent as custom high-resolution metrics.
113+
ForceHighRes bool
111114
}
112115

113116
// Bridge pushes metrics to AWS CloudWatch
@@ -125,6 +128,7 @@ type Bridge struct {
125128
excludeMetrics []glob.Glob
126129
includeDimensionsForMetrics []MatcherWithStringSet
127130
excludeDimensionsForMetrics []MatcherWithStringSet
131+
forceHighRes bool
128132
}
129133

130134
// NewBridge initializes and returns a pointer to a Bridge using the
@@ -151,6 +155,7 @@ func NewBridge(c *Config) (*Bridge, error) {
151155
b.excludeMetrics = c.ExcludeMetrics
152156
b.includeDimensionsForMetrics = c.IncludeDimensionsForMetrics
153157
b.excludeDimensionsForMetrics = c.ExcludeDimensionsForMetrics
158+
b.forceHighRes = c.ForceHighRes
154159

155160
if c.CloudWatchPublishInterval > 0 {
156161
b.cloudWatchPublishInterval = c.CloudWatchPublishInterval
@@ -327,7 +332,7 @@ func appendDatum(data []*cloudwatch.MetricDatum, name string, s *model.Sample, b
327332
SetValue(value).
328333
SetTimestamp(s.Timestamp.Time()).
329334
SetDimensions(append(kubeStateDimensions, getAdditionalDimensions(b)...)).
330-
SetStorageResolution(getResolution(metric)).
335+
SetStorageResolution(b.getResolution(metric)).
331336
SetUnit(getUnit(metric))
332337
data = append(data, datum)
333338

@@ -338,7 +343,7 @@ func appendDatum(data []*cloudwatch.MetricDatum, name string, s *model.Sample, b
338343
SetValue(value).
339344
SetTimestamp(s.Timestamp.Time()).
340345
SetDimensions(append(replacedDimensions, getAdditionalDimensions(b)...)).
341-
SetStorageResolution(getResolution(metric)).
346+
SetStorageResolution(b.getResolution(metric)).
342347
SetUnit(getUnit(metric))
343348
data = append(data, replacedDimensionDatum)
344349
}
@@ -465,7 +470,10 @@ func getAdditionalDimensions(b *Bridge) []*cloudwatch.Dimension {
465470
}
466471

467472
// Returns 1 if the metric contains a __cw_high_res label, otherwise returns 60
468-
func getResolution(m model.Metric) int64 {
473+
func (b *Bridge) getResolution(m model.Metric) int64 {
474+
if b.forceHighRes {
475+
return 1
476+
}
469477
if _, ok := m[cwHighResLabel]; ok {
470478
return 1
471479
}

0 commit comments

Comments
 (0)