Skip to content

Commit

Permalink
[cmd/telemetrygen] Allow configuring Sum and Gauge metrics via CLI (o…
Browse files Browse the repository at this point in the history
…pen-telemetry#26667)

**Description:** <Describe what has changed.>
Adding a feature - Allow configuring Sum and Gauge metrics in
`telemetrygen` via CLI

---------

Co-authored-by: Pablo Baeyens <pbaeyens31+github@gmail.com>
  • Loading branch information
2 people authored and jorgeancal committed Oct 9, 2023
1 parent b9515ff commit 323b9ed
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 16 deletions.
25 changes: 25 additions & 0 deletions .chloggen/telemetrygen-add-metric-types.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'enhancement'

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: cmd/telemetrygen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add CLI option for selecting different metric types

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [26667]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
5 changes: 5 additions & 0 deletions cmd/telemetrygen/internal/metrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ import (
type Config struct {
common.Config
NumMetrics int
MetricType metricType
}

// Flags registers config flags.
func (c *Config) Flags(fs *pflag.FlagSet) {
// Use Gauge as default metric type.
c.MetricType = metricTypeGauge

c.CommonFlags(fs)
fs.Var(&c.MetricType, "metric-type", "Metric type enum. must be one of 'Gauge' or 'Sum'")
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
36 changes: 36 additions & 0 deletions cmd/telemetrygen/internal/metrics/metrics_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package metrics

import (
"errors"
)

type metricType string

const (
metricTypeGauge = "Gauge"
metricTypeSum = "Sum"
)

// String is used both by fmt.Print and by Cobra in help text
func (e *metricType) String() string {
return string(*e)
}

// Set must have pointer receiver so it doesn't change the value of a copy
func (e *metricType) Set(v string) error {
switch v {
case "Gauge", "Sum":
*e = metricType(v)
return nil
default:
return errors.New(`must be one of "Gauge" or "Sum"`)
}
}

// Type is only used in help text
func (e *metricType) Type() string {
return "metricType"
}
52 changes: 36 additions & 16 deletions cmd/telemetrygen/internal/metrics/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,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 @@ -28,29 +29,48 @@ type worker struct {

func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Exporter) {
limiter := rate.NewLimiter(w.limitPerSecond, 1)
var i int64

var i int64
for w.running.Load() {
rm := metricdata.ResourceMetrics{
Resource: res,
ScopeMetrics: []metricdata.ScopeMetrics{
{
Metrics: []metricdata.Metrics{
var metrics []metricdata.Metrics

switch w.metricType {
case metricTypeGauge:
metrics = append(metrics, metricdata.Metrics{
Name: "gen",
Data: metricdata.Gauge[int64]{
DataPoints: []metricdata.DataPoint[int64]{
{
Time: time.Now(),
Value: i,
},
},
},
})
case metricTypeSum:
metrics = append(metrics, metricdata.Metrics{
Name: "gen",
Data: metricdata.Sum[int64]{
IsMonotonic: true,
Temporality: metricdata.CumulativeTemporality,
DataPoints: []metricdata.DataPoint[int64]{
{
Name: "gen",
Data: metricdata.Gauge[int64]{
DataPoints: []metricdata.DataPoint[int64]{
{
Time: time.Now(),
Value: i,
},
},
},
StartTime: time.Now().Add(-1 * time.Second),
Time: time.Now(),
Value: i,
},
},
},
},
})
default:
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 {
w.logger.Fatal("exporter failed", zap.Error(err))
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/telemetrygen/internal/metrics/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestFixedNumberOfMetrics(t *testing.T) {
WorkerCount: 1,
},
NumMetrics: 5,
MetricType: metricTypeSum,
}

exp := &mockExporter{}
Expand All @@ -69,6 +70,7 @@ func TestRateOfMetrics(t *testing.T) {
TotalDuration: time.Second / 2,
WorkerCount: 1,
},
MetricType: metricTypeSum,
}
exp := &mockExporter{}

Expand All @@ -88,6 +90,7 @@ func TestUnthrottled(t *testing.T) {
TotalDuration: 1 * time.Second,
WorkerCount: 1,
},
MetricType: metricTypeSum,
}
exp := &mockExporter{}

Expand Down

0 comments on commit 323b9ed

Please sign in to comment.