From 1b2493366becf6cd1526bc2929eaa44bd3857c2a Mon Sep 17 00:00:00 2001 From: Min Xia Date: Mon, 10 Jun 2024 12:09:20 -0700 Subject: [PATCH] Group metrics randomly for one EMF event with a 50% probability --- exporter/awsemfexporter/grouped_metric.go | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/exporter/awsemfexporter/grouped_metric.go b/exporter/awsemfexporter/grouped_metric.go index dd3426cdc2e9..a9f8af3b1924 100644 --- a/exporter/awsemfexporter/grouped_metric.go +++ b/exporter/awsemfexporter/grouped_metric.go @@ -5,7 +5,9 @@ package awsemfexporter // import "github.com/open-telemetry/opentelemetry-collec import ( "encoding/json" + "math/rand" "strings" + "time" "go.opentelemetry.io/collector/pdata/pmetric" "go.uber.org/zap" @@ -77,6 +79,16 @@ func addToGroupedMetric( } } + // add metric name as label attribute so it will not be grouped with other metrics in 50% chance + if addMetricNameInGroupKey(dp.name) { + labels["metric.name"] = dp.name + config.logger.Warn( + "Add MetricName attribute as group key", + zap.String("Name", dp.name), + zap.Any("Labels", labels), + ) + } + metric := &metricInfo{ value: dp.value, unit: translateUnit(pmd, descriptor), @@ -112,6 +124,22 @@ func addToGroupedMetric( return nil } +// addMetricNameInGroupKey returns true or false with equal probability. +func addMetricNameInGroupKey(metricName string) bool { + validMetrics := map[string]bool{ + "Latency": true, + "Fault": true, + "Error": true, + } + + if validMetrics[metricName] { + rand.Seed(time.Now().UnixNano()) + return rand.Intn(2) == 0 + } + + return false +} + type kubernetesObj struct { ContainerName string `json:"container_name,omitempty"` Docker *internalDockerObj `json:"docker,omitempty"`