-
Notifications
You must be signed in to change notification settings - Fork 39
/
agent_communicator.go
226 lines (169 loc) · 5.55 KB
/
agent_communicator.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// (c) Copyright IBM Corp. 2022
package instana
import (
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
)
// agentCommunicator is a collection of data and actions to be executed against the agent.
type agentCommunicator struct {
// host is the agent host. It can be updated via default gateway or a new client announcement.
host string
// port id the agent port.
port string
// from is the agent information sent with each span in the "from" (span.f) section. it's format is as follows:
// {e: "entityId", h: "hostAgentId", hl: trueIfServerlessPlatform, cp: "The cloud provider for a hostless span"}
// Only span.f.e is mandatory.
from *fromS
// client is an HTTP client
client httpClient
// l is the Instana logger
l LeveledLogger
}
// buildURL builds an Agent URL based on the sufix for the different Agent services.
func (a *agentCommunicator) buildURL(sufix string) string {
url := "http://" + a.host + ":" + a.port + sufix
if sufix[len(sufix)-1:] == "." && a.from.EntityID != "" {
url += a.from.EntityID
}
return url
}
// checkForSuccessResponse checks for a successful GET operation with the agent host
func (a *agentCommunicator) checkForSuccessResponse() bool {
url := a.buildURL("/")
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
a.l.Debug("Error creating request while attempting to retrieve the 'Server' response: ", err.Error())
return false
}
resp, err := a.client.Do(req)
if err != nil || resp == nil {
a.l.Debug("No response from the agent while attempting to retrieve the 'Server' response: ", err.Error())
return false
}
defer func() {
io.CopyN(ioutil.Discard, resp.Body, 256<<10)
resp.Body.Close()
}()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
a.l.Debug("Unexpected response from the agent host server. Status code: ", resp.StatusCode)
return false
}
a.l.Debug("Expected response from Agent! Status code: ", resp.StatusCode)
return true
}
// agentResponse attempts to retrieve the agent response containing its configuration
func (a *agentCommunicator) agentResponse(d *discoveryS) *agentResponse {
jsonData, _ := json.Marshal(d)
var resp agentResponse
u := a.buildURL(agentDiscoveryURL)
req, err := http.NewRequest(http.MethodPut, u, bytes.NewBuffer(jsonData))
if err != nil {
a.l.Debug("Error creating request to the agent while attempting to get the response: ", err.Error())
return nil
}
res, err := a.client.Do(req)
if res == nil {
a.l.Debug("No response from the agent while attempting to get the response: ", err.Error())
return nil
}
defer func() {
io.CopyN(ioutil.Discard, res.Body, 256<<10)
res.Body.Close()
}()
badResponse := res.StatusCode < 200 || res.StatusCode >= 300
if err != nil || badResponse {
a.l.Debug("Error requesting response data from the agent: ", err, "; Bad response: ", badResponse)
return nil
}
respBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
a.l.Debug("Error reading res.Body while attempting to get response data from the agent: ", err.Error())
return nil
}
err = json.Unmarshal(respBytes, &resp)
if err != nil {
a.l.Debug("Error unmarshaling body while attempting to get response data from the agent: ", err.Error())
return nil
}
return &resp
}
// pingAgent send a HEAD request to the agent and returns true if it receives a response from it
func (a *agentCommunicator) pingAgent() bool {
u := a.buildURL(agentDataURL)
req, err := http.NewRequest(http.MethodHead, u, nil)
if err != nil {
a.l.Debug("Error preparing request while attempting to ping the agent: ", err.Error())
return false
}
resp, err := a.client.Do(req)
if err != nil || resp == nil {
a.l.Debug("Error pinging the agent: ", err.Error(), ", response: ", resp)
return false
}
defer func() {
io.CopyN(ioutil.Discard, resp.Body, 256<<10)
resp.Body.Close()
}()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
a.l.Debug("Agent ping failed, response: ", resp.StatusCode, " with message ", resp.Status, "; URL: ", u)
return false
}
a.l.Debug("Agent ping ok!")
return true
}
// sendDataToAgent makes a POST to the agent sending some data as payload. eg: spans, events or metrics
func (a *agentCommunicator) sendDataToAgent(suffix string, data interface{}) error {
url := a.buildURL(suffix)
ctx, cancel := context.WithTimeout(context.Background(), clientTimeout)
defer cancel()
var r *bytes.Buffer
if data != nil {
b, err := json.Marshal(data)
if err != nil {
a.l.Debug("Sending data to agent marshaling failed: ", err.Error())
return err
}
r = bytes.NewBuffer(b)
if r.Len() > maxContentLength {
return payloadTooLargeErr
}
}
req, err := http.NewRequest(http.MethodPost, url, r)
if err != nil {
a.l.Debug("Sending data to agent request creation failed: ", err.Error())
return err
}
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/json")
resp, err := a.client.Do(req)
if resp == nil {
a.l.Debug("Sending data to agent: response nil for URL ", url)
}
if resp != nil {
respCode := resp.StatusCode
if respCode < 200 || respCode >= 300 {
a.l.Debug("Sending data to agent: response code: ", resp.StatusCode, "-", resp.Status, "; ", url)
}
io.CopyN(ioutil.Discard, resp.Body, 256<<10)
resp.Body.Close()
}
if err != nil {
a.l.Debug("Sending data to agent request failed: ", err.Error())
}
return err
}
func newAgentCommunicator(host, port string, from *fromS, logger LeveledLogger) *agentCommunicator {
return &agentCommunicator{
host: host,
port: port,
from: from,
client: &http.Client{
Timeout: announceTimeout,
},
l: logger,
}
}