2019-11-15 23:01:20 +02:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2019-11-26 21:47:15 +02:00
|
|
|
"context"
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2019-11-26 21:47:15 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/array"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/counter"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/gauge"
|
2019-11-15 23:01:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type CheckpointSet struct {
|
|
|
|
encoder export.LabelEncoder
|
|
|
|
updates []export.Record
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCheckpointSet(encoder export.LabelEncoder) *CheckpointSet {
|
|
|
|
return &CheckpointSet{
|
|
|
|
encoder: encoder,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CheckpointSet) Reset() {
|
|
|
|
p.updates = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CheckpointSet) Add(desc *export.Descriptor, agg export.Aggregator, labels ...core.KeyValue) {
|
|
|
|
encoded := p.encoder.Encode(labels)
|
|
|
|
elabels := export.NewLabels(labels, encoded, p.encoder)
|
|
|
|
|
|
|
|
p.updates = append(p.updates, export.NewRecord(desc, elabels, agg))
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:47:15 +02:00
|
|
|
func createNumber(desc *export.Descriptor, v float64) core.Number {
|
|
|
|
if desc.NumberKind() == core.Float64NumberKind {
|
|
|
|
return core.NewFloat64Number(v)
|
|
|
|
}
|
|
|
|
return core.NewInt64Number(int64(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CheckpointSet) AddGauge(desc *export.Descriptor, v float64, labels ...core.KeyValue) {
|
|
|
|
ctx := context.Background()
|
|
|
|
gagg := gauge.New()
|
|
|
|
_ = gagg.Update(ctx, createNumber(desc, v), desc)
|
|
|
|
gagg.Checkpoint(ctx, desc)
|
|
|
|
p.Add(desc, gagg, labels...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CheckpointSet) AddCounter(desc *export.Descriptor, v float64, labels ...core.KeyValue) {
|
|
|
|
ctx := context.Background()
|
|
|
|
cagg := counter.New()
|
|
|
|
_ = cagg.Update(ctx, createNumber(desc, v), desc)
|
|
|
|
cagg.Checkpoint(ctx, desc)
|
|
|
|
p.Add(desc, cagg, labels...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *CheckpointSet) AddMeasure(desc *export.Descriptor, v float64, labels ...core.KeyValue) {
|
|
|
|
ctx := context.Background()
|
|
|
|
magg := array.New()
|
|
|
|
_ = magg.Update(ctx, createNumber(desc, v), desc)
|
|
|
|
magg.Checkpoint(ctx, desc)
|
|
|
|
p.Add(desc, magg, labels...)
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
func (p *CheckpointSet) ForEach(f func(export.Record)) {
|
|
|
|
for _, r := range p.updates {
|
|
|
|
f(r)
|
|
|
|
}
|
|
|
|
}
|