-
Notifications
You must be signed in to change notification settings - Fork 37
/
agent.go
184 lines (153 loc) · 4.05 KB
/
agent.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package instana
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os"
"strconv"
"time"
)
const (
agentDiscoveryURL = "/com.instana.plugin.golang.discovery"
agentTracesURL = "/com.instana.plugin.golang/traces."
agentDataURL = "/com.instana.plugin.golang."
agentEventURL = "/com.instana.plugin.generic.event"
agentProfilesURL = "/com.instana.plugin.golang/profiles."
agentDefaultHost = "localhost"
agentDefaultPort = 42699
agentHeader = "Instana Agent"
)
type agentResponse struct {
Pid uint32 `json:"pid"`
HostID string `json:"agentUuid"`
}
type discoveryS struct {
PID int `json:"pid"`
Name string `json:"name"`
Args []string `json:"args"`
Fd string `json:"fd"`
Inode string `json:"inode"`
}
type fromS struct {
PID string `json:"e"`
HostID string `json:"h"`
}
type agentS struct {
sensor *sensorS
fsm *fsmS
from *fromS
host string
client *http.Client
}
func (r *agentS) init() {
r.client = &http.Client{Timeout: 5 * time.Second}
r.fsm = r.initFsm()
r.setFrom(&fromS{})
}
func (r *agentS) makeURL(prefix string) string {
return r.makeHostURL(r.host, prefix)
}
func (r *agentS) makeHostURL(host string, prefix string) string {
envPort := os.Getenv("INSTANA_AGENT_PORT")
port := agentDefaultPort
if r.sensor.options.AgentPort > 0 {
return r.makeFullURL(host, r.sensor.options.AgentPort, prefix)
}
if envPort == "" {
return r.makeFullURL(host, port, prefix)
}
port, err := strconv.Atoi(envPort)
if err != nil {
return r.makeFullURL(host, agentDefaultPort, prefix)
}
return r.makeFullURL(host, port, prefix)
}
func (r *agentS) makeFullURL(host string, port int, prefix string) string {
var buffer bytes.Buffer
buffer.WriteString("http://")
buffer.WriteString(host)
buffer.WriteString(":")
buffer.WriteString(strconv.Itoa(port))
buffer.WriteString(prefix)
if prefix[len(prefix)-1:] == "." && r.from.PID != "" {
buffer.WriteString(r.from.PID)
}
return buffer.String()
}
func (r *agentS) head(url string) (string, error) {
return r.request(url, "HEAD", nil)
}
func (r *agentS) request(url string, method string, data interface{}) (string, error) {
return r.fullRequestResponse(url, method, data, nil, "")
}
func (r *agentS) requestResponse(url string, method string, data interface{}, ret interface{}) (string, error) {
return r.fullRequestResponse(url, method, data, ret, "")
}
func (r *agentS) requestHeader(url string, method string, header string) (string, error) {
return r.fullRequestResponse(url, method, nil, nil, header)
}
func (r *agentS) fullRequestResponse(url string, method string, data interface{}, body interface{}, header string) (string, error) {
var j []byte
var ret string
var err error
var resp *http.Response
var req *http.Request
if data != nil {
j, err = json.Marshal(data)
}
if err == nil {
if j != nil {
req, err = http.NewRequest(method, url, bytes.NewBuffer(j))
} else {
req, err = http.NewRequest(method, url, nil)
}
// Uncomment this to dump json payloads
// log.debug(bytes.NewBuffer(j))
if err == nil {
req.Header.Set("Content-Type", "application/json")
resp, err = r.client.Do(req)
if err == nil {
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
err = errors.New(resp.Status)
} else {
if body != nil {
var b []byte
b, err = ioutil.ReadAll(resp.Body)
json.Unmarshal(b, body)
}
if header != "" {
ret = resp.Header.Get(header)
}
}
}
}
}
if err != nil {
// Ignore errors while in announced stated (before ready) as
// this is the time where the entity is registering in the Instana
// backend and it will return 404 until it's done.
if !r.sensor.agent.fsm.fsm.Is("announced") {
log.info(err, url)
}
}
return ret, err
}
func (r *agentS) setFrom(from *fromS) {
r.from = from
}
func (r *agentS) setHost(host string) {
r.host = host
}
func (r *agentS) reset() {
r.fsm.reset()
}
func (r *sensorS) initAgent() *agentS {
log.debug("initializing agent")
ret := new(agentS)
ret.sensor = r
ret.init()
return ret
}