/
histogram.go
57 lines (51 loc) 路 1.62 KB
/
histogram.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package metrics
import (
"context"
"fmt"
"math/rand"
"time"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/sdk/metric"
"go.uber.org/zap"
)
// Histogram demonstrates how to record a distribution of individual values
func SimulateHistogram(mp *metric.MeterProvider, conf *Config, logger *zap.Logger) {
c := *conf
err := run(conf, logger, histogram(mp, c, logger))
if err != nil {
logger.Error("failed to run histogram", zap.Error(err))
}
}
// histogram generates a histogram metric
func histogram(mp *metric.MeterProvider, c Config, logger *zap.Logger) WorkerFunc {
return func(ctx context.Context) {
name := fmt.Sprintf("%v.metrics.histogram", c.ServiceName)
durRecorder, _ := mp.Meter(c.ServiceName).Int64Histogram(
name,
instrument.WithUnit("microseconds"),
instrument.WithDescription("Histogram demonstrates how to record a distribution of individual values"),
)
if c.TotalDuration > 0 {
logger.Info("generation duration", zap.Float64("seconds", c.TotalDuration.Seconds()))
loop:
for timeout := time.After(c.TotalDuration); ; {
select {
case <-timeout:
break loop
default:
}
logger.Info("generating", zap.String("name", name))
dur := time.Duration(rand.NormFloat64()*5000000) * time.Microsecond
durRecorder.Record(ctx, dur.Microseconds())
time.Sleep(time.Duration(c.Rate) * time.Second)
}
} else {
for {
logger.Info("generating", zap.String("name", name))
dur := time.Duration(rand.NormFloat64()*5000000) * time.Microsecond
durRecorder.Record(ctx, dur.Microseconds())
time.Sleep(time.Duration(c.Rate) * time.Second)
}
}
}
}