-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsync_gauge.go
47 lines (41 loc) · 1.26 KB
/
sync_gauge.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
package metrics
import (
"context"
"sync"
"go.opentelemetry.io/otel/attribute"
api "go.opentelemetry.io/otel/metric"
)
type Int64WithAttributes struct {
value int64
attrs attribute.Set
}
type Int64SyncGauge struct {
gauge api.Int64ObservableGauge
mutex *sync.RWMutex
attrsValueMap map[string]*Int64WithAttributes
}
func NewInt64SyncGauge(meter api.Meter, name string, options ...api.Int64ObservableGaugeOption) (*Int64SyncGauge, error) {
mutex := &sync.RWMutex{}
attrsValueMap := make(map[string]*Int64WithAttributes)
callback := func(ctx context.Context, observer api.Int64Observer) error {
mutex.RLock()
defer mutex.RUnlock()
for _, entry := range attrsValueMap {
observer.Observe(entry.value, api.WithAttributeSet(entry.attrs))
}
return nil
}
options = append(options, api.WithInt64Callback(callback))
gauge, err := meter.Int64ObservableGauge(name, options...)
if err != nil {
return nil, err
}
return &Int64SyncGauge{gauge, mutex, attrsValueMap}, nil
}
func (g *Int64SyncGauge) Set(value int64, attr ...attribute.KeyValue) {
g.mutex.Lock()
defer g.mutex.Unlock()
attrs := attribute.NewSet(attr...)
encodedAttrs := attrs.Encoded(attribute.DefaultEncoder())
g.attrsValueMap[encodedAttrs] = &Int64WithAttributes{value, attrs}
}