Skip to content

Commit

Permalink
[telemetrygen] Add support to set metric name (open-telemetry#32840)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed May 3, 2024
1 parent 33b1573 commit ed4adf7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
22 changes: 22 additions & 0 deletions .chloggen/add-metric-name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Use this changelog template to create an entry for release notes.

# 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: telemetrygen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add support to set metric name

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

# 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]
2 changes: 2 additions & 0 deletions cmd/telemetrygen/internal/metrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (
type Config struct {
common.Config
NumMetrics int
MetricName string
MetricType metricType
}

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

c.CommonFlags(fs)
Expand Down
1 change: 1 addition & 0 deletions cmd/telemetrygen/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func Run(c *Config, exp func() (sdkmetric.Exporter, error), logger *zap.Logger)
wg.Add(1)
w := worker{
numMetrics: c.NumMetrics,
metricName: c.MetricName,
metricType: c.MetricType,
limitPerSecond: limit,
totalDuration: c.TotalDuration,
Expand Down
5 changes: 3 additions & 2 deletions cmd/telemetrygen/internal/metrics/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

type worker struct {
running *atomic.Bool // pointer to shared flag that indicates it's time to stop the test
metricName string // name of metric to generate
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`)
Expand Down Expand Up @@ -51,7 +52,7 @@ func (w worker) simulateMetrics(res *resource.Resource, exporterFunc func() (sdk
switch w.metricType {
case metricTypeGauge:
metrics = append(metrics, metricdata.Metrics{
Name: "gen",
Name: w.metricName,
Data: metricdata.Gauge[int64]{
DataPoints: []metricdata.DataPoint[int64]{
{
Expand All @@ -64,7 +65,7 @@ func (w worker) simulateMetrics(res *resource.Resource, exporterFunc func() (sdk
})
case metricTypeSum:
metrics = append(metrics, metricdata.Metrics{
Name: "gen",
Name: w.metricName,
Data: metricdata.Sum[int64]{
IsMonotonic: true,
Temporality: metricdata.CumulativeTemporality,
Expand Down
14 changes: 10 additions & 4 deletions cmd/telemetrygen/internal/metrics/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ func TestSumNoTelemetryAttrs(t *testing.T) {
rms := m.rms
for i := 0; i < qty; i++ {
ms := rms[i].ScopeMetrics[0].Metrics[0]
assert.Equal(t, "test", ms.Name)
// @note update when telemetrygen allow other metric types
attr := ms.Data.(metricdata.Sum[int64]).DataPoints[0].Attributes
assert.Equal(t, attr.Len(), 0, "it shouldn't have attrs here")
assert.Equal(t, 0, attr.Len(), "it shouldn't have attrs here")
}
}

Expand All @@ -168,9 +169,10 @@ func TestGaugeNoTelemetryAttrs(t *testing.T) {
rms := m.rms
for i := 0; i < qty; i++ {
ms := rms[i].ScopeMetrics[0].Metrics[0]
assert.Equal(t, "test", ms.Name)
// @note update when telemetrygen allow other metric types
attr := ms.Data.(metricdata.Gauge[int64]).DataPoints[0].Attributes
assert.Equal(t, attr.Len(), 0, "it shouldn't have attrs here")
assert.Equal(t, 0, attr.Len(), "it shouldn't have attrs here")
}
}

Expand All @@ -195,9 +197,10 @@ func TestSumSingleTelemetryAttr(t *testing.T) {
rms := m.rms
for i := 0; i < qty; i++ {
ms := rms[i].ScopeMetrics[0].Metrics[0]
assert.Equal(t, "test", ms.Name)
// @note update when telemetrygen allow other metric types
attr := ms.Data.(metricdata.Sum[int64]).DataPoints[0].Attributes
assert.Equal(t, attr.Len(), 1, "it must have a single attribute here")
assert.Equal(t, 1, attr.Len(), "it must have a single attribute here")
actualValue, _ := attr.Value(telemetryAttrKeyOne)
assert.Equal(t, actualValue.AsString(), telemetryAttrValueOne, "it should be "+telemetryAttrValueOne)
}
Expand All @@ -224,9 +227,10 @@ func TestGaugeSingleTelemetryAttr(t *testing.T) {
rms := m.rms
for i := 0; i < qty; i++ {
ms := rms[i].ScopeMetrics[0].Metrics[0]
assert.Equal(t, "test", ms.Name)
// @note update when telemetrygen allow other metric types
attr := ms.Data.(metricdata.Gauge[int64]).DataPoints[0].Attributes
assert.Equal(t, attr.Len(), 1, "it must have a single attribute here")
assert.Equal(t, 1, attr.Len(), "it must have a single attribute here")
actualValue, _ := attr.Value(telemetryAttrKeyOne)
assert.Equal(t, actualValue.AsString(), telemetryAttrValueOne, "it should be "+telemetryAttrValueOne)
}
Expand Down Expand Up @@ -303,6 +307,7 @@ func configWithNoAttributes(metric metricType, qty int) *Config {
TelemetryAttributes: nil,
},
NumMetrics: qty,
MetricName: "test",
MetricType: metric,
}
}
Expand All @@ -314,6 +319,7 @@ func configWithOneAttribute(metric metricType, qty int) *Config {
TelemetryAttributes: common.KeyValue{telemetryAttrKeyOne: telemetryAttrValueOne},
},
NumMetrics: qty,
MetricName: "test",
MetricType: metric,
}
}
Expand Down

0 comments on commit ed4adf7

Please sign in to comment.