Skip to content

Commit

Permalink
Merge pull request #1 from marcelbirkner/extend-metrics
Browse files Browse the repository at this point in the history
Add Sum and Histogram metrics
  • Loading branch information
marcelbirkner authored Sep 13, 2023
2 parents 70c334b + cd81db7 commit 2487539
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/telemetrygen/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func Run(c *Config, exp sdkmetric.Exporter, logger *zap.Logger) error {
index: i,
}

go w.simulateMetrics(res, exp)
go w.simulateMetrics(res, exp, c.GetAttributes())
}
if c.TotalDuration > 0 {
time.Sleep(c.TotalDuration)
Expand Down
44 changes: 40 additions & 4 deletions cmd/telemetrygen/internal/metrics/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync/atomic"
"time"

"go.opentelemetry.io/otel/attribute"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/resource"
Expand All @@ -26,9 +27,12 @@ type worker struct {
index int // worker index
}

func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Exporter) {
func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Exporter, attributes []attribute.KeyValue) {
limiter := rate.NewLimiter(w.limitPerSecond, 1)

var i int64
value := 24.42
attrs := attribute.NewSet(attributes...)

for w.running.Load() {
rm := metricdata.ResourceMetrics{
Expand All @@ -37,12 +41,43 @@ func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Expor
{
Metrics: []metricdata.Metrics{
{
Name: "gen",
Name: "gen.metric.gauge",
Data: metricdata.Gauge[int64]{
DataPoints: []metricdata.DataPoint[int64]{
{
Time: time.Now(),
Value: i,
Attributes: attrs,
Time: time.Now(),
Value: i,
},
},
},
},
{
Name: "gen.metric.sum",
Data: metricdata.Sum[int64]{
IsMonotonic: true,
Temporality: metricdata.DeltaTemporality,
DataPoints: []metricdata.DataPoint[int64]{
{
Attributes: attrs,
StartTime: time.Now(),
Time: time.Now().Add(1 * time.Second),
Value: i,
},
},
},
},
{
Name: "gen.metric.histogram",
Data: metricdata.Histogram[float64]{
Temporality: metricdata.CumulativeTemporality,
DataPoints: []metricdata.HistogramDataPoint[float64]{
{
Attributes: attrs,
Sum: float64(float32(value)),
Max: metricdata.NewExtrema(float64(float32(value))),
Min: metricdata.NewExtrema(float64(float32(value))),
Count: 1,
},
},
},
Expand All @@ -51,6 +86,7 @@ func (w worker) simulateMetrics(res *resource.Resource, exporter sdkmetric.Expor
},
},
}

if err := exporter.Export(context.Background(), &rm); err != nil {
w.logger.Fatal("exporter failed", zap.Error(err))
}
Expand Down

0 comments on commit 2487539

Please sign in to comment.