Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmd/telemetrygen] Allow configuring Sum and Gauge metrics via CLI #26667

Merged
merged 22 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
407b67a
Add Sum and Histogram metrics
marcelbirkner Sep 12, 2023
89c4a3f
Update metrics.go
marcelbirkner Sep 13, 2023
cd81db7
Update worker.go
marcelbirkner Sep 13, 2023
2487539
Merge pull request #1 from marcelbirkner/extend-metrics
marcelbirkner Sep 13, 2023
82537b4
Use status.code as attribute
marcelbirkner Sep 13, 2023
2685e6b
Merge branch 'main' into add-metrics-to-telemetrygen
marcelbirkner Sep 13, 2023
a88abc8
Merge branch 'main' into add-metrics-to-telemetrygen
marcelbirkner Sep 14, 2023
1f8f7b0
Merge branch 'main' into add-metrics-to-telemetrygen
marcelbirkner Sep 15, 2023
910e588
Make metric-type configurable as a CLI parameter, remove metric attri…
marcelbirkner Sep 15, 2023
a2c96eb
Make metric-type configurable as a CLI parameter, remove metric attri…
marcelbirkner Sep 15, 2023
13455a7
Keep Gauge as default metric type
marcelbirkner Sep 15, 2023
9c36dd8
Update cmd/telemetrygen/internal/metrics/worker.go
marcelbirkner Sep 15, 2023
e49ae71
Update cmd/telemetrygen/internal/metrics/worker.go
marcelbirkner Sep 15, 2023
c000f9d
Removing Histograms and ALL, reverting metric name
marcelbirkner Sep 15, 2023
f92f7e9
Add changelog
marcelbirkner Sep 15, 2023
9091f55
Add license header
marcelbirkner Sep 15, 2023
d6ddb63
gocritic: switch ifelse with case statement
marcelbirkner Sep 15, 2023
4757222
Formattin
marcelbirkner Sep 15, 2023
0fc8070
Fix unit tests
marcelbirkner Sep 15, 2023
6a9c231
Merge branch 'main' into add-metrics-to-telemetrygen
marcelbirkner Sep 15, 2023
5681037
Merge branch 'main' into add-metrics-to-telemetrygen
marcelbirkner Sep 18, 2023
4e73213
Update cmd/telemetrygen/internal/metrics/worker.go
marcelbirkner Sep 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
33 changes: 33 additions & 0 deletions cmd/telemetrygen/internal/metrics/metrics_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package metrics
mx-psi marked this conversation as resolved.
Show resolved Hide resolved

import (
"errors"
)

type metricType string
mx-psi marked this conversation as resolved.
Show resolved Hide resolved

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"
}
50 changes: 34 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,46 @@ type worker struct {

func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Exporter) {
mx-psi marked this conversation as resolved.
Show resolved Hide resolved
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
if w.metricType == metricTypeGauge {
metrics = append(metrics, metricdata.Metrics{
Name: "gen",
Data: metricdata.Gauge[int64]{
DataPoints: []metricdata.DataPoint[int64]{
{
Name: "gen",
Data: metricdata.Gauge[int64]{
DataPoints: []metricdata.DataPoint[int64]{
{
Time: time.Now(),
Value: i,
},
},
},
Time: time.Now(),
Value: i,
},
},
},
},
})
} else if w.metricType == metricTypeSum {
metrics = append(metrics, metricdata.Metrics{
Name: "gen",
Data: metricdata.Sum[int64]{
IsMonotonic: true,
Temporality: metricdata.DeltaTemporality,
marcelbirkner marked this conversation as resolved.
Show resolved Hide resolved
DataPoints: []metricdata.DataPoint[int64]{
{
StartTime: time.Now().Add(-1 * time.Second),
Time: time.Now(),
Value: i,
},
},
},
})
} 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 {
w.logger.Fatal("exporter failed", zap.Error(err))
}
Expand Down
Loading