-
Notifications
You must be signed in to change notification settings - Fork 13
/
metric_destination.go
119 lines (102 loc) · 2.73 KB
/
metric_destination.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
115
116
117
118
119
package circonus
import (
"fmt"
"os"
"strings"
"github.com/circonus-labs/circonus-unified-agent/config"
"github.com/circonus-labs/circonus-unified-agent/cua"
circmgr "github.com/circonus-labs/circonus-unified-agent/internal/circonus"
"github.com/maier/go-trapmetrics"
)
type metricDestination struct {
metrics *trapmetrics.TrapMetrics
id string
queuedMetrics int64
}
// getMetricDestination returns a destination for the plugin identified by a plugin and plugin instance id
func (c *Circonus) getMetricDestination(m cua.Metric) *metricDestination {
plugin := m.Origin()
instanceID := m.OriginInstance()
// default - used in two cases:
// 1. plugin cannot be identified
// 2. user as enabled one_check
var defaultDest *metricDestination
if defaultDestination != nil {
defaultDest = defaultDestination
}
if c.OneCheck || plugin == "" {
return defaultDest
}
if config.IsDefaultInstanceID(instanceID) {
// host metrics - the "default" plugins which are enabled by default
// but can be controlled via the (ENABLE_DEFAULT_PLUGINS env var
// any value other than "false" will enable the default plugins)
if config.IsDefaultPlugin(plugin) {
hostDest := defaultDest
if config.DefaultPluginsEnabled() {
if hostDestination != nil {
hostDest = hostDestination
}
}
return hostDest
}
// agent metrics - metrics the agent emits about itself - always enabled
if config.IsAgentPlugin(plugin) {
agentDest := defaultDest
if agentDestination != nil {
agentDest = agentDestination
}
return agentDest
}
}
id := plugin
if instanceID != "" {
id += ":" + instanceID
}
metricGroup := ""
if plugin == "stackdriver_circonus" {
metricGroup = c.getMetricGroupTag(m)
if metricGroup != "" {
parts := strings.SplitN(metricGroup, "/", 2)
if len(parts) > 0 {
id += ":" + parts[0]
metricGroup = parts[0]
} else {
id += ":" + metricGroup
}
}
}
c.RLock()
d, found := c.metricDestinations[id]
c.RUnlock()
if found {
return d
}
if err := c.initMetricDestination(id, plugin+" "+instanceID+" "+metricGroup, instanceID); err == nil {
if d, ok := c.metricDestinations[id]; ok {
return d
}
} else {
c.Log.Errorf("error initializing metric destination: %s", err)
os.Exit(1) //nolint:gocritic
}
return defaultDest
}
func (c *Circonus) initMetricDestination(id, name, instanceID string) error {
c.Lock()
defer c.Unlock()
plugID := id
if id == "*" {
plugID = "default"
name = "default"
}
dest, err := circmgr.NewMetricDestination(id, name, instanceID, c.CheckNamePrefix, c.Log)
if err != nil {
return fmt.Errorf("new metric destination: %w", err)
}
c.metricDestinations[id] = &metricDestination{
metrics: dest,
id: plugID,
}
return nil
}