Skip to content

Commit

Permalink
Add random histogram to telemetrygen
Browse files Browse the repository at this point in the history
  • Loading branch information
shivanthzen committed Oct 17, 2024
1 parent e7ebc6e commit 00a6747
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
9 changes: 5 additions & 4 deletions cmd/telemetrygen/internal/metrics/metrics_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
type metricType string

const (
metricTypeGauge = "Gauge"
metricTypeSum = "Sum"
metricTypeGauge = "Gauge"
metricTypeSum = "Sum"
metricTypeHistogram = "Histogram"
)

// String is used both by fmt.Print and by Cobra in help text
Expand All @@ -22,11 +23,11 @@ func (e *metricType) String() string {
// 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":
case "Gauge", "Sum", "Histogram":
*e = metricType(v)
return nil
default:
return errors.New(`must be one of "Gauge" or "Sum"`)
return errors.New(`must be one of "Gauge", "Sum", "Histogram"`)
}
}

Expand Down
20 changes: 20 additions & 0 deletions cmd/telemetrygen/internal/metrics/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package metrics

import (
"context"
rand "math/rand/v2"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -38,6 +39,8 @@ func (w worker) simulateMetrics(res *resource.Resource, exporterFunc func() (sdk
w.logger.Error("failed to create the exporter", zap.Error(err))
return
}
randomPCG := rand.NewPCG(0, 0)
randomgenerator := rand.New(randomPCG)

defer func() {
w.logger.Info("stopping the exporter")
Expand Down Expand Up @@ -82,6 +85,23 @@ func (w worker) simulateMetrics(res *resource.Resource, exporterFunc func() (sdk
},
},
})
case metricTypeHistogram:
metrics = append(metrics, metricdata.Metrics{
Name: w.metricName,
Data: metricdata.Histogram[int64]{
Temporality: metricdata.DeltaTemporality,
DataPoints: []metricdata.HistogramDataPoint[int64]{
{
StartTime: time.Now().Add(-1 * time.Second),
Time: time.Now(),
Attributes: attribute.NewSet(signalAttrs...),
Exemplars: w.exemplars,
Count: uint64(randomgenerator.Int64N(10)),
Sum: randomgenerator.Int64N(10),
},
},
},
})
default:
w.logger.Fatal("unknown metric type")
}
Expand Down

0 comments on commit 00a6747

Please sign in to comment.