diff --git a/cmd/telemetrygen/internal/metrics/config.go b/cmd/telemetrygen/internal/metrics/config.go index dbcd5f72ebe0..3038f32d0ab4 100644 --- a/cmd/telemetrygen/internal/metrics/config.go +++ b/cmd/telemetrygen/internal/metrics/config.go @@ -13,10 +13,14 @@ import ( type Config struct { common.Config NumMetrics int + MetricType metricType } // Flags registers config flags. func (c *Config) Flags(fs *pflag.FlagSet) { + c.MetricType = metricTypeGauge + c.CommonFlags(fs) + fs.Var(&c.MetricType, "metric-type", "Metric type enum. must be one of 'gauge', 'sum', 'histogram' or 'all'") fs.IntVar(&c.NumMetrics, "metrics", 1, "Number of metrics to generate in each worker (ignored if duration is provided)") } diff --git a/cmd/telemetrygen/internal/metrics/metrics.go b/cmd/telemetrygen/internal/metrics/metrics.go index 6418f7228a95..310796cb9563 100644 --- a/cmd/telemetrygen/internal/metrics/metrics.go +++ b/cmd/telemetrygen/internal/metrics/metrics.go @@ -28,6 +28,7 @@ func Start(cfg *Config) error { if err != nil { return err } + logger.Info("starting the metrics generator with configuration", zap.Any("config", cfg)) grpcExpOpt := []otlpmetricgrpc.Option{ otlpmetricgrpc.WithEndpoint(cfg.Endpoint), @@ -104,6 +105,7 @@ func Run(c *Config, exp sdkmetric.Exporter, logger *zap.Logger) error { wg.Add(1) w := worker{ numMetrics: c.NumMetrics, + metricType: c.MetricType, limitPerSecond: limit, totalDuration: c.TotalDuration, running: running, diff --git a/cmd/telemetrygen/internal/metrics/worker.go b/cmd/telemetrygen/internal/metrics/worker.go index a0107eca3fcc..809cfef40e61 100644 --- a/cmd/telemetrygen/internal/metrics/worker.go +++ b/cmd/telemetrygen/internal/metrics/worker.go @@ -9,7 +9,6 @@ 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" @@ -19,6 +18,7 @@ import ( type worker struct { running *atomic.Bool // pointer to shared flag that indicates it's time to stop the test + metricType metricType // type of metric to generate numMetrics int // how many metrics the worker has to generate (only when duration==0) totalDuration time.Duration // how long to run the test for (overrides `numMetrics`) limitPerSecond rate.Limit // how many metrics per second to generate @@ -31,63 +31,59 @@ func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Expor limiter := rate.NewLimiter(w.limitPerSecond, 1) var i int64 - value := 24.42 - attrs := attribute.NewSet(attribute.KeyValue{ - Key: attribute.Key("status.code"), - Value: attribute.StringValue("STATUS_CODE_OK"), - }) - for w.running.Load() { - rm := metricdata.ResourceMetrics{ - Resource: res, - ScopeMetrics: []metricdata.ScopeMetrics{ - { - Metrics: []metricdata.Metrics{ + var metrics []metricdata.Metrics + if w.metricType == metricTypeGauge || w.metricType == metricTypeAll { + metrics = append(metrics, metricdata.Metrics{ + Name: "gen.metric.gauge", + Data: metricdata.Gauge[int64]{ + DataPoints: []metricdata.DataPoint[int64]{ { - Name: "gen.metric.gauge", - Data: metricdata.Gauge[int64]{ - DataPoints: []metricdata.DataPoint[int64]{ - { - Attributes: attrs, - Time: time.Now(), - Value: i, - }, - }, - }, + StartTime: time.Now(), + Time: time.Now().Add(1 * time.Second), + Value: i, }, + }, + }, + }) + } else if w.metricType == metricTypeSum || w.metricType == metricTypeAll { + metrics = append(metrics, metricdata.Metrics{ + Name: "gen.metric.sum", + Data: metricdata.Sum[int64]{ + IsMonotonic: true, + Temporality: metricdata.DeltaTemporality, + DataPoints: []metricdata.DataPoint[int64]{ { - 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, - }, - }, - }, + StartTime: time.Now(), + Time: time.Now().Add(1 * time.Second), + Value: i, }, + }, + }, + }) + } else if w.metricType == metricTypeHistogram || w.metricType == metricTypeAll { + value := 24.42 + metrics = append(metrics, metricdata.Metrics{ + Name: "gen.metric.histogram", + Data: metricdata.Histogram[float64]{ + Temporality: metricdata.CumulativeTemporality, + DataPoints: []metricdata.HistogramDataPoint[float64]{ { - 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, - }, - }, - }, + Sum: float64(float32(value)), + Max: metricdata.NewExtrema(float64(float32(value))), + Min: metricdata.NewExtrema(float64(float32(value))), + Count: 1, }, }, }, - }, + }) + } else { + w.logger.Fatal("unknown metric type") + } + + rm := metricdata.ResourceMetrics{ + Resource: res, + ScopeMetrics: []metricdata.ScopeMetrics{{Metrics: metrics}}, } if err := exporter.Export(context.Background(), &rm); err != nil {