Skip to content

Commit

Permalink
Make metric-type configurable as a CLI parameter, remove metric attri…
Browse files Browse the repository at this point in the history
…butes
  • Loading branch information
marcelbirkner committed Sep 15, 2023
1 parent 1f8f7b0 commit 910e588
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 49 deletions.
4 changes: 4 additions & 0 deletions cmd/telemetrygen/internal/metrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
2 changes: 2 additions & 0 deletions cmd/telemetrygen/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
94 changes: 45 additions & 49 deletions cmd/telemetrygen/internal/metrics/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 910e588

Please sign in to comment.