forked from newrelic/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_config.go
154 lines (139 loc) · 4.76 KB
/
internal_config.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package newrelic
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"github.com/VadimBelov/go-agent/internal"
"github.com/VadimBelov/go-agent/internal/logger"
"github.com/VadimBelov/go-agent/internal/utilization"
)
func copyDestConfig(c AttributeDestinationConfig) AttributeDestinationConfig {
cp := c
if nil != c.Include {
cp.Include = make([]string, len(c.Include))
copy(cp.Include, c.Include)
}
if nil != c.Exclude {
cp.Exclude = make([]string, len(c.Exclude))
copy(cp.Exclude, c.Exclude)
}
return cp
}
func copyConfigReferenceFields(cfg Config) Config {
cp := cfg
if nil != cfg.Labels {
cp.Labels = make(map[string]string, len(cfg.Labels))
for key, val := range cfg.Labels {
cp.Labels[key] = val
}
}
if nil != cfg.ErrorCollector.IgnoreStatusCodes {
ignored := make([]int, len(cfg.ErrorCollector.IgnoreStatusCodes))
copy(ignored, cfg.ErrorCollector.IgnoreStatusCodes)
cp.ErrorCollector.IgnoreStatusCodes = ignored
}
cp.Attributes = copyDestConfig(cfg.Attributes)
cp.ErrorCollector.Attributes = copyDestConfig(cfg.ErrorCollector.Attributes)
cp.TransactionEvents.Attributes = copyDestConfig(cfg.TransactionEvents.Attributes)
return cp
}
const (
agentLanguage = "go"
)
func transportSetting(t http.RoundTripper) interface{} {
if nil == t {
return nil
}
return fmt.Sprintf("%T", t)
}
func loggerSetting(lg Logger) interface{} {
if nil == lg {
return nil
}
if _, ok := lg.(logger.ShimLogger); ok {
return nil
}
return fmt.Sprintf("%T", lg)
}
const (
// https://source.datanerd.us/agents/agent-specs/blob/master/Custom-Host-Names.md
hostByteLimit = 255
)
type settings Config
func (s settings) MarshalJSON() ([]byte, error) {
c := Config(s)
transport := c.Transport
c.Transport = nil
logger := c.Logger
c.Logger = nil
js, err := json.Marshal(c)
if nil != err {
return nil, err
}
fields := make(map[string]interface{})
err = json.Unmarshal(js, &fields)
if nil != err {
return nil, err
}
// The License field is not simply ignored by adding the `json:"-"` tag
// to it since we want to allow consumers to populate Config from JSON.
delete(fields, `License`)
fields[`Transport`] = transportSetting(transport)
fields[`Logger`] = loggerSetting(logger)
return json.Marshal(fields)
}
func configConnectJSONInternal(c Config, pid int, util *utilization.Data, e internal.Environment, version string) ([]byte, error) {
return json.Marshal([]interface{}{struct {
Pid int `json:"pid"`
Language string `json:"language"`
Version string `json:"agent_version"`
Host string `json:"host"`
HostDisplayName string `json:"display_host,omitempty"`
Settings interface{} `json:"settings"`
AppName []string `json:"app_name"`
HighSecurity bool `json:"high_security"`
HighSecurityOverride HighSecurityFeature `json:"high_security_override"`
Labels internal.Labels `json:"labels,omitempty"`
Environment internal.Environment `json:"environment"`
Identifier string `json:"identifier"`
Util *utilization.Data `json:"utilization"`
}{
Pid: pid,
Language: agentLanguage,
Version: version,
Host: internal.StringLengthByteLimit(util.Hostname, hostByteLimit),
HostDisplayName: internal.StringLengthByteLimit(c.HostDisplayName, hostByteLimit),
Settings: (settings)(c),
AppName: strings.Split(c.AppName, ";"),
HighSecurity: c.HighSecurity,
HighSecurityOverride: c.HighSecurityOverride,
Labels: internal.Labels(c.Labels),
Environment: e,
// This identifier field is provided to avoid:
// https://newrelic.atlassian.net/browse/DSCORE-778
//
// This identifier is used by the collector to look up the real
// agent. If an identifier isn't provided, the collector will
// create its own based on the first appname, which prevents a
// single daemon from connecting "a;b" and "a;c" at the same
// time.
//
// Providing the identifier below works around this issue and
// allows users more flexibility in using application rollups.
Identifier: c.AppName,
Util: util,
}})
}
func configConnectJSON(c Config) ([]byte, error) {
env := internal.NewEnvironment()
util := utilization.Gather(utilization.Config{
DetectAWS: c.Utilization.DetectAWS,
DetectDocker: c.Utilization.DetectDocker,
LogicalProcessors: c.Utilization.LogicalProcessors,
TotalRAMMIB: c.Utilization.TotalRAMMIB,
BillingHostname: c.Utilization.BillingHostname,
}, c.Logger)
return configConnectJSONInternal(c, os.Getpid(), util, env, Version)
}