-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenv.go
114 lines (95 loc) · 1.88 KB
/
env.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package metrics
import (
"fmt"
"sync"
"sync/atomic"
"github.com/nats-io/nats"
)
func NewEnvironment(nc *nats.Conn, subject string) (*Environment, error) {
env := &Environment{
subject: subject,
dimlock: sync.Mutex{},
globalDims: DimMap{},
}
if nc != nil {
econn, err := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
if err != nil {
return nil, err
}
env.nc = econn
}
if err := env.isReady(); err != nil {
return nil, err
}
return env, nil
}
type Environment struct {
subject string
globalDims DimMap
dimlock sync.Mutex
nc *nats.EncodedConn
tracer func(m *RawMetric)
// some metrics stuff
timersSent int64
countersSent int64
gaugesSent int64
}
func (e *Environment) send(m *RawMetric) error {
if err := e.isReady(); err != nil {
return err
}
switch m.Type {
case CounterType:
atomic.AddInt64(&e.countersSent, 1)
case TimerType:
atomic.AddInt64(&e.timersSent, 1)
case GaugeType:
atomic.AddInt64(&e.gaugesSent, 1)
default:
return UnknownMetricTypeError{errString{fmt.Sprintf("unknown metric type: %s", m.Type)}}
}
if e.tracer != nil {
go e.tracer(m)
}
if e.nc == nil {
return nil
}
return e.nc.Publish(e.subject, &m)
}
func (e *Environment) AddDimension(k string, v interface{}) {
e.dimlock.Lock()
defer e.dimlock.Unlock()
e.globalDims[k] = v
}
func (e *Environment) isReady() error {
if e.subject == "" {
return InitError{errString{"No subject provided"}}
}
return nil
}
func addAll(into DimMap, from DimMap) {
if into != nil {
if from != nil {
for k, v := range from {
into[k] = v
}
}
}
}
func (e *Environment) newMetric(name string, t MetricType, dims DimMap) *metric {
m := &metric{
RawMetric: RawMetric{
Name: name,
Type: t,
Dims: make(DimMap),
},
env: e,
dimlock: sync.Mutex{},
}
if dims != nil {
for k, v := range dims {
m.AddDimension(k, v)
}
}
return m
}