From 407b67a1cc6e3a10003b56ac531620d0c4038060 Mon Sep 17 00:00:00 2001 From: Marcel Birkner Date: Tue, 12 Sep 2023 13:03:52 +0200 Subject: [PATCH 1/3] Add Sum and Histogram metrics --- cmd/telemetrygen/internal/metrics/worker.go | 40 ++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/cmd/telemetrygen/internal/metrics/worker.go b/cmd/telemetrygen/internal/metrics/worker.go index 6056cc5eb4ab..646fb97ed06f 100644 --- a/cmd/telemetrygen/internal/metrics/worker.go +++ b/cmd/telemetrygen/internal/metrics/worker.go @@ -9,6 +9,7 @@ import ( "sync/atomic" "time" + "go.opentelemetry.io/otel/attribute" sdkmetric "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/metric/metricdata" "go.opentelemetry.io/otel/sdk/resource" @@ -28,7 +29,13 @@ type worker struct { func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Exporter) { limiter := rate.NewLimiter(w.limitPerSecond, 1) + var i int64 + value := 24.42 + attrs := attribute.NewSet(attribute.KeyValue{ + Key: attribute.Key("node_id"), + Value: attribute.StringValue("test"), + }) for w.running.Load() { rm := metricdata.ResourceMetrics{ @@ -37,7 +44,7 @@ func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Expor { Metrics: []metricdata.Metrics{ { - Name: "gen", + Name: "gen.metric.gauge", Data: metricdata.Gauge[int64]{ DataPoints: []metricdata.DataPoint[int64]{ { @@ -47,10 +54,41 @@ func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Expor }, }, }, + { + Name: "gen.metric.sum", + Data: metricdata.Sum[int64]{ + IsMonotonic: true, + Temporality: metricdata.DeltaTemporality, + DataPoints: []metricdata.DataPoint[int64]{ + { + Attributes: attrs, + StartTime: time.Now(), + Time: time.Now().Add(1 * time.Second), + Value: i, + }, + }, + }, + }, + { + Name: "gen.metric.histogram", + Data: metricdata.Histogram[float64]{ + Temporality: metricdata.CumulativeTemporality, + DataPoints: []metricdata.HistogramDataPoint[float64]{ + { + Attributes: attrs, + Sum: float64(float32(value)), + Max: metricdata.NewExtrema(float64(float32(value))), + Min: metricdata.NewExtrema(float64(float32(value))), + Count: 1, + }, + }, + }, + }, }, }, }, } + if err := exporter.Export(context.Background(), &rm); err != nil { w.logger.Fatal("exporter failed", zap.Error(err)) } From 89c4a3ff4c9545576758218817cb799f405f49aa Mon Sep 17 00:00:00 2001 From: Marcel Birkner Date: Wed, 13 Sep 2023 14:51:04 +0200 Subject: [PATCH 2/3] Update metrics.go --- cmd/telemetrygen/internal/metrics/metrics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/telemetrygen/internal/metrics/metrics.go b/cmd/telemetrygen/internal/metrics/metrics.go index 6418f7228a95..e39ceaba6e7b 100644 --- a/cmd/telemetrygen/internal/metrics/metrics.go +++ b/cmd/telemetrygen/internal/metrics/metrics.go @@ -112,7 +112,7 @@ func Run(c *Config, exp sdkmetric.Exporter, logger *zap.Logger) error { index: i, } - go w.simulateMetrics(res, exp) + go w.simulateMetrics(res, exp, c.GetAttributes()) } if c.TotalDuration > 0 { time.Sleep(c.TotalDuration) From cd81db75569fb196473fbb4fd701a7b99f7825ef Mon Sep 17 00:00:00 2001 From: Marcel Birkner Date: Wed, 13 Sep 2023 14:51:17 +0200 Subject: [PATCH 3/3] Update worker.go --- cmd/telemetrygen/internal/metrics/worker.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/cmd/telemetrygen/internal/metrics/worker.go b/cmd/telemetrygen/internal/metrics/worker.go index 646fb97ed06f..75c89f259e85 100644 --- a/cmd/telemetrygen/internal/metrics/worker.go +++ b/cmd/telemetrygen/internal/metrics/worker.go @@ -27,15 +27,12 @@ type worker struct { index int // worker index } -func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Exporter) { +func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Exporter, attributes []attribute.KeyValue) { limiter := rate.NewLimiter(w.limitPerSecond, 1) var i int64 value := 24.42 - attrs := attribute.NewSet(attribute.KeyValue{ - Key: attribute.Key("node_id"), - Value: attribute.StringValue("test"), - }) + attrs := attribute.NewSet(attributes...) for w.running.Load() { rm := metricdata.ResourceMetrics{ @@ -48,8 +45,9 @@ func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Expor Data: metricdata.Gauge[int64]{ DataPoints: []metricdata.DataPoint[int64]{ { - Time: time.Now(), - Value: i, + Attributes: attrs, + Time: time.Now(), + Value: i, }, }, },