Skip to content

Commit ed4adf7

Browse files
authored
[telemetrygen] Add support to set metric name (#32840)
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 33b1573 commit ed4adf7

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

.chloggen/add-metric-name.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: telemetrygen
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add support to set metric name
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [32840]
14+
15+
# If your change doesn't affect end users or the exported elements of any package,
16+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
17+
# Optional: The change log or logs in which this entry should be included.
18+
# e.g. '[user]' or '[user, api]'
19+
# Include 'user' if the change is relevant to end users.
20+
# Include 'api' if there is a change to a library API.
21+
# Default: '[user]'
22+
change_logs: [user]

cmd/telemetrygen/internal/metrics/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import (
1313
type Config struct {
1414
common.Config
1515
NumMetrics int
16+
MetricName string
1617
MetricType metricType
1718
}
1819

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

2426
c.CommonFlags(fs)

cmd/telemetrygen/internal/metrics/metrics.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func Run(c *Config, exp func() (sdkmetric.Exporter, error), logger *zap.Logger)
9393
wg.Add(1)
9494
w := worker{
9595
numMetrics: c.NumMetrics,
96+
metricName: c.MetricName,
9697
metricType: c.MetricType,
9798
limitPerSecond: limit,
9899
totalDuration: c.TotalDuration,

cmd/telemetrygen/internal/metrics/worker.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
type worker struct {
2121
running *atomic.Bool // pointer to shared flag that indicates it's time to stop the test
22+
metricName string // name of metric to generate
2223
metricType metricType // type of metric to generate
2324
numMetrics int // how many metrics the worker has to generate (only when duration==0)
2425
totalDuration time.Duration // how long to run the test for (overrides `numMetrics`)
@@ -51,7 +52,7 @@ func (w worker) simulateMetrics(res *resource.Resource, exporterFunc func() (sdk
5152
switch w.metricType {
5253
case metricTypeGauge:
5354
metrics = append(metrics, metricdata.Metrics{
54-
Name: "gen",
55+
Name: w.metricName,
5556
Data: metricdata.Gauge[int64]{
5657
DataPoints: []metricdata.DataPoint[int64]{
5758
{
@@ -64,7 +65,7 @@ func (w worker) simulateMetrics(res *resource.Resource, exporterFunc func() (sdk
6465
})
6566
case metricTypeSum:
6667
metrics = append(metrics, metricdata.Metrics{
67-
Name: "gen",
68+
Name: w.metricName,
6869
Data: metricdata.Sum[int64]{
6970
IsMonotonic: true,
7071
Temporality: metricdata.CumulativeTemporality,

cmd/telemetrygen/internal/metrics/worker_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@ func TestSumNoTelemetryAttrs(t *testing.T) {
141141
rms := m.rms
142142
for i := 0; i < qty; i++ {
143143
ms := rms[i].ScopeMetrics[0].Metrics[0]
144+
assert.Equal(t, "test", ms.Name)
144145
// @note update when telemetrygen allow other metric types
145146
attr := ms.Data.(metricdata.Sum[int64]).DataPoints[0].Attributes
146-
assert.Equal(t, attr.Len(), 0, "it shouldn't have attrs here")
147+
assert.Equal(t, 0, attr.Len(), "it shouldn't have attrs here")
147148
}
148149
}
149150

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

@@ -195,9 +197,10 @@ func TestSumSingleTelemetryAttr(t *testing.T) {
195197
rms := m.rms
196198
for i := 0; i < qty; i++ {
197199
ms := rms[i].ScopeMetrics[0].Metrics[0]
200+
assert.Equal(t, "test", ms.Name)
198201
// @note update when telemetrygen allow other metric types
199202
attr := ms.Data.(metricdata.Sum[int64]).DataPoints[0].Attributes
200-
assert.Equal(t, attr.Len(), 1, "it must have a single attribute here")
203+
assert.Equal(t, 1, attr.Len(), "it must have a single attribute here")
201204
actualValue, _ := attr.Value(telemetryAttrKeyOne)
202205
assert.Equal(t, actualValue.AsString(), telemetryAttrValueOne, "it should be "+telemetryAttrValueOne)
203206
}
@@ -224,9 +227,10 @@ func TestGaugeSingleTelemetryAttr(t *testing.T) {
224227
rms := m.rms
225228
for i := 0; i < qty; i++ {
226229
ms := rms[i].ScopeMetrics[0].Metrics[0]
230+
assert.Equal(t, "test", ms.Name)
227231
// @note update when telemetrygen allow other metric types
228232
attr := ms.Data.(metricdata.Gauge[int64]).DataPoints[0].Attributes
229-
assert.Equal(t, attr.Len(), 1, "it must have a single attribute here")
233+
assert.Equal(t, 1, attr.Len(), "it must have a single attribute here")
230234
actualValue, _ := attr.Value(telemetryAttrKeyOne)
231235
assert.Equal(t, actualValue.AsString(), telemetryAttrValueOne, "it should be "+telemetryAttrValueOne)
232236
}
@@ -303,6 +307,7 @@ func configWithNoAttributes(metric metricType, qty int) *Config {
303307
TelemetryAttributes: nil,
304308
},
305309
NumMetrics: qty,
310+
MetricName: "test",
306311
MetricType: metric,
307312
}
308313
}
@@ -314,6 +319,7 @@ func configWithOneAttribute(metric metricType, qty int) *Config {
314319
TelemetryAttributes: common.KeyValue{telemetryAttrKeyOne: telemetryAttrValueOne},
315320
},
316321
NumMetrics: qty,
322+
MetricName: "test",
317323
MetricType: metric,
318324
}
319325
}

0 commit comments

Comments
 (0)