9
9
"sync/atomic"
10
10
"time"
11
11
12
- "go.opentelemetry.io/otel/attribute"
13
12
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
14
13
"go.opentelemetry.io/otel/sdk/metric/metricdata"
15
14
"go.opentelemetry.io/otel/sdk/resource"
@@ -19,6 +18,7 @@ import (
19
18
20
19
type worker struct {
21
20
running * atomic.Bool // pointer to shared flag that indicates it's time to stop the test
21
+ metricType metricType // type of metric to generate
22
22
numMetrics int // how many metrics the worker has to generate (only when duration==0)
23
23
totalDuration time.Duration // how long to run the test for (overrides `numMetrics`)
24
24
limitPerSecond rate.Limit // how many metrics per second to generate
@@ -31,63 +31,59 @@ func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Expor
31
31
limiter := rate .NewLimiter (w .limitPerSecond , 1 )
32
32
33
33
var i int64
34
- value := 24.42
35
- attrs := attribute .NewSet (attribute.KeyValue {
36
- Key : attribute .Key ("status.code" ),
37
- Value : attribute .StringValue ("STATUS_CODE_OK" ),
38
- })
39
-
40
34
for w .running .Load () {
41
- rm := metricdata.ResourceMetrics {
42
- Resource : res ,
43
- ScopeMetrics : []metricdata.ScopeMetrics {
44
- {
45
- Metrics : []metricdata.Metrics {
35
+ var metrics []metricdata.Metrics
36
+ if w .metricType == metricTypeGauge || w .metricType == metricTypeAll {
37
+ metrics = append (metrics , metricdata.Metrics {
38
+ Name : "gen.metric.gauge" ,
39
+ Data : metricdata.Gauge [int64 ]{
40
+ DataPoints : []metricdata.DataPoint [int64 ]{
46
41
{
47
- Name : "gen.metric.gauge" ,
48
- Data : metricdata.Gauge [int64 ]{
49
- DataPoints : []metricdata.DataPoint [int64 ]{
50
- {
51
- Attributes : attrs ,
52
- Time : time .Now (),
53
- Value : i ,
54
- },
55
- },
56
- },
42
+ StartTime : time .Now (),
43
+ Time : time .Now ().Add (1 * time .Second ),
44
+ Value : i ,
57
45
},
46
+ },
47
+ },
48
+ })
49
+ } else if w .metricType == metricTypeSum || w .metricType == metricTypeAll {
50
+ metrics = append (metrics , metricdata.Metrics {
51
+ Name : "gen.metric.sum" ,
52
+ Data : metricdata.Sum [int64 ]{
53
+ IsMonotonic : true ,
54
+ Temporality : metricdata .DeltaTemporality ,
55
+ DataPoints : []metricdata.DataPoint [int64 ]{
58
56
{
59
- Name : "gen.metric.sum" ,
60
- Data : metricdata.Sum [int64 ]{
61
- IsMonotonic : true ,
62
- Temporality : metricdata .DeltaTemporality ,
63
- DataPoints : []metricdata.DataPoint [int64 ]{
64
- {
65
- Attributes : attrs ,
66
- StartTime : time .Now (),
67
- Time : time .Now ().Add (1 * time .Second ),
68
- Value : i ,
69
- },
70
- },
71
- },
57
+ StartTime : time .Now (),
58
+ Time : time .Now ().Add (1 * time .Second ),
59
+ Value : i ,
72
60
},
61
+ },
62
+ },
63
+ })
64
+ } else if w .metricType == metricTypeHistogram || w .metricType == metricTypeAll {
65
+ value := 24.42
66
+ metrics = append (metrics , metricdata.Metrics {
67
+ Name : "gen.metric.histogram" ,
68
+ Data : metricdata.Histogram [float64 ]{
69
+ Temporality : metricdata .CumulativeTemporality ,
70
+ DataPoints : []metricdata.HistogramDataPoint [float64 ]{
73
71
{
74
- Name : "gen.metric.histogram" ,
75
- Data : metricdata.Histogram [float64 ]{
76
- Temporality : metricdata .CumulativeTemporality ,
77
- DataPoints : []metricdata.HistogramDataPoint [float64 ]{
78
- {
79
- Attributes : attrs ,
80
- Sum : float64 (float32 (value )),
81
- Max : metricdata .NewExtrema (float64 (float32 (value ))),
82
- Min : metricdata .NewExtrema (float64 (float32 (value ))),
83
- Count : 1 ,
84
- },
85
- },
86
- },
72
+ Sum : float64 (float32 (value )),
73
+ Max : metricdata .NewExtrema (float64 (float32 (value ))),
74
+ Min : metricdata .NewExtrema (float64 (float32 (value ))),
75
+ Count : 1 ,
87
76
},
88
77
},
89
78
},
90
- },
79
+ })
80
+ } else {
81
+ w .logger .Fatal ("unknown metric type" )
82
+ }
83
+
84
+ rm := metricdata.ResourceMetrics {
85
+ Resource : res ,
86
+ ScopeMetrics : []metricdata.ScopeMetrics {{Metrics : metrics }},
91
87
}
92
88
93
89
if err := exporter .Export (context .Background (), & rm ); err != nil {
0 commit comments