This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
agent_client.go
413 lines (362 loc) · 11.3 KB
/
agent_client.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package blackfire
// TODO: AgentTimeout
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"regexp"
"runtime"
"strconv"
"strings"
"github.com/blackfireio/go-blackfire/bf_format"
"github.com/blackfireio/go-blackfire/pprof_reader"
"github.com/blackfireio/osinfo"
"github.com/rs/zerolog"
)
type agentClient struct {
agentNetwork string
agentAddress string
signingEndpoint *url.URL
signingAuth string
serverID string
serverToken string
links []*linksMap
profiles []*Profile
logger *zerolog.Logger
signingResponse *signingResponseData
signingResponseIsConsumed bool
}
type linksMap map[string]map[string]string
func NewAgentClient(configuration *Configuration) (*agentClient, error) {
agentNetwork, agentAddress, err := parseNetworkAddressString(configuration.AgentSocket)
if err != nil {
return nil, err
}
signingEndpoint := configuration.HTTPEndpoint
signingEndpoint.Path = path.Join(signingEndpoint.Path, "/api/v1/signing")
signingResponse, err := signingResponseFromBFQuery(configuration.BlackfireQuery)
if err != nil {
return nil, err
}
a := &agentClient{
agentNetwork: agentNetwork,
agentAddress: agentAddress,
signingEndpoint: signingEndpoint,
signingAuth: fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(configuration.ClientID+":"+configuration.ClientToken))),
links: make([]*linksMap, 10),
profiles: make([]*Profile, 10),
logger: configuration.Logger,
serverID: configuration.ServerID,
serverToken: configuration.ServerToken,
signingResponse: signingResponse,
signingResponseIsConsumed: signingResponse == nil,
}
return a, nil
}
func (c *agentClient) CurrentBlackfireQuery() (string, error) {
if err := c.updateSigningRequest(); err != nil {
return "", err
}
return c.signingResponse.QueryString, nil
}
func (c *agentClient) LastProfiles() []*Profile {
profiles := []*Profile{}
for _, profile := range c.profiles {
if profile == nil {
continue
}
c.logger.Debug().Msgf("Blackfire: Get profile data for %s", profile.UUID)
if err := profile.load(c.signingAuth); err != nil {
c.logger.Debug().Msgf("Blackfire: Unable to get profile data for %s: %s", profile.UUID, err)
continue
}
profiles = append(profiles, profile)
}
return profiles
}
func (c *agentClient) ProbeOptions() bf_format.ProbeOptions {
return c.signingResponse.Options
}
func (c *agentClient) getGoVersion() string {
return fmt.Sprintf("go-%s", runtime.Version()[2:])
}
func (c *agentClient) getBlackfireProbeHeader(hasBlackfireYaml bool) string {
builder := strings.Builder{}
builder.WriteString(c.getGoVersion())
if hasBlackfireYaml {
builder.WriteString(", blackfire_yml")
}
if c.signingResponse.Options.IsTimespanFlagSet() {
builder.WriteString(", timespan")
}
return builder.String()
}
func (c *agentClient) loadBlackfireYaml() (data []byte, err error) {
filenames := []string{".blackfire.yml", ".blackfire.yaml"}
var filename string
for _, filename = range filenames {
if data, err = ioutil.ReadFile(filename); err == nil {
c.logger.Debug().Msgf("Loaded %s", filename)
break
} else if os.IsNotExist(err) {
c.logger.Debug().Msgf("%s does not exist", filename)
} else {
return nil, err
}
}
if os.IsNotExist(err) {
err = nil
}
return
}
func (c *agentClient) sendBlackfireYaml(conn *agentConnection, contents []byte) (err error) {
if err = conn.WriteStringHeader("Blackfire-Yaml-Size", strconv.Itoa(len(contents))); err != nil {
return
}
c.logger.Debug().Str("blackfire.yml", string(contents)).Msgf("Send blackfire.yml, size %d", len(contents))
err = conn.WriteRawData(contents)
return
}
func (c *agentClient) sendProfilePrologue(conn *agentConnection) (err error) {
// https://private.blackfire.io/knowledge-base/protocol/profiler/04-sending.html
bfQuery, err := c.CurrentBlackfireQuery()
if err != nil {
return
}
var osVersion url.Values
if osVersion, err = getProfileOSHeaderValue(); err != nil {
return
}
var blackfireYaml []byte
if blackfireYaml, err = c.loadBlackfireYaml(); err != nil {
return
}
hasBlackfireYaml := blackfireYaml != nil
// These must be done separately from the rest of the headers because they
// either must be sent in a specific order, or use nonstandard encoding.
var orderedHeaders []string
if c.serverID != "" && c.serverToken != "" {
orderedHeaders = append(orderedHeaders, fmt.Sprintf("Blackfire-Auth: %v:%v", c.serverID, c.serverToken))
}
orderedHeaders = append(orderedHeaders, fmt.Sprintf("Blackfire-Query: %s", bfQuery))
orderedHeaders = append(orderedHeaders, fmt.Sprintf("Blackfire-Probe: %s", c.getBlackfireProbeHeader(hasBlackfireYaml)))
unorderedHeaders := make(map[string]interface{})
unorderedHeaders["os-version"] = osVersion
// We've now consumed the current Blackfire query, and must fetch a new one next time.
c.signingResponseIsConsumed = true
// Send the ordered headers first, then wait for the Blackfire-Response,
// then send the unordered headers.
if err = conn.WriteOrderedHeaders(orderedHeaders); err != nil {
return
}
if hasBlackfireYaml {
if err = conn.WriteEndOfHeaders(); err != nil {
return
}
var responseName string
var responseValue string
if responseName, responseValue, err = conn.ReadEncodedHeader(); err != nil {
return
}
switch responseName {
case "Blackfire-Response":
var values url.Values
if values, err = url.ParseQuery(responseValue); err != nil {
return
}
if result := values.Get("blackfire_yml"); result == "true" {
if err = c.sendBlackfireYaml(conn, blackfireYaml); err != nil {
return
}
}
case "Blackfire-Error":
return fmt.Errorf(strings.TrimSpace(responseValue))
default:
return fmt.Errorf("Unexpected agent response: %s", responseValue)
}
}
if err = conn.WriteHeaders(unorderedHeaders); err != nil {
return
}
err = conn.WriteEndOfHeaders()
return
}
func (c *agentClient) SendProfile(profile *pprof_reader.Profile, title string) (err error) {
var conn *agentConnection
if conn, err = newAgentConnection(c.agentNetwork, c.agentAddress, c.logger); err != nil {
return
}
defer func() {
if err == nil {
c.logger.Debug().Msgf("Profile sent")
err = conn.Close()
} else {
// We want the error that occurred earlier, not an error from close.
conn.Close()
}
}()
if err = c.sendProfilePrologue(conn); err != nil {
return
}
var response http.Header
if response, err = conn.ReadResponse(); err != nil {
return err
}
if response.Get("Blackfire-Error") != "" {
return fmt.Errorf("Blackfire-Error: %s", response.Get("Blackfire-Error"))
}
profileBuffer := new(bytes.Buffer)
if err := bf_format.WriteBFFormat(profile, profileBuffer, c.ProbeOptions(), title); err != nil {
return err
}
encodedProfile := profileBuffer.Bytes()
c.logger.Debug().Str("contents", string(encodedProfile)).Msg("Blackfire: Send profile")
if err = conn.WriteRawData(encodedProfile); err != nil {
return
}
return
}
func (c *agentClient) updateSigningRequest() (err error) {
if !c.signingResponseIsConsumed {
return
}
var response *http.Response
c.logger.Debug().Msgf("Blackfire: Get authorization from %s", c.signingEndpoint)
request, err := http.NewRequest("POST", c.signingEndpoint.String(), nil)
if err != nil {
return
}
request.Header.Add("Authorization", c.signingAuth)
c.logger.Debug().Msg("Blackfire: Send signing request")
client := http.DefaultClient
response, err = client.Do(request)
if err != nil {
return
}
if response.StatusCode != 201 {
return fmt.Errorf("Signing request to %s failed: %s", c.signingEndpoint, response.Status)
}
var responseData []byte
responseData, err = ioutil.ReadAll(response.Body)
if err != nil {
return
}
c.logger.Debug().Interface("response", string(responseData)).Msg("Blackfire: Receive signing response")
err = json.Unmarshal(responseData, &c.signingResponse)
if err != nil {
return fmt.Errorf("JSON error: %v", err)
}
if c.signingResponse.QueryString == "" {
return fmt.Errorf("Signing response blackfire query was empty")
}
profileURL, ok := c.signingResponse.Links["profile"]
if !ok {
return fmt.Errorf("Signing response blackfire profile URL was empty")
}
c.links = append([]*linksMap{&c.signingResponse.Links}, c.links[:9]...)
c.profiles = append([]*Profile{{
UUID: c.signingResponse.UUID,
URL: c.signingResponse.Links["graph_url"]["href"],
APIURL: profileURL["href"],
}}, c.profiles[:9]...)
c.signingResponseIsConsumed = false
return
}
var nonOptionQueryFields = map[string]bool{
"expires": true,
"userId": true,
"agentIds": true,
"collabToken": true,
"signature": true,
}
func signingResponseFromBFQuery(query string) (response *signingResponseData, err error) {
if query == "" {
return
}
values, err := url.ParseQuery(query)
if err != nil {
return
}
firstValue := func(values url.Values, key string) string {
if vArr := values[key]; vArr != nil {
if len(vArr) > 0 {
return vArr[0]
}
}
return ""
}
expires, err := strconv.ParseUint(firstValue(values, "expires"), 10, 64)
if err != nil {
return
}
response = newSigningResponseData()
response.Agents = values["agentIds"]
response.CollabToken = firstValue(values, "collabToken")
response.Expires = expires
response.QueryString = query
response.Signature = firstValue(values, "signature")
response.UserID = firstValue(values, "userId")
for key, arrValues := range values {
if nonOptionQueryFields[key] {
continue
}
if len(arrValues) < 1 {
continue
}
response.Options[key] = arrValues[0]
}
return
}
func parseNetworkAddressString(agentSocket string) (network string, address string, err error) {
re := regexp.MustCompile(`^([^:]+)://(.*)`)
matches := re.FindAllStringSubmatch(agentSocket, -1)
if matches == nil {
err = fmt.Errorf("Could not parse agent socket value: [%v]", agentSocket)
return
}
network = matches[0][1]
address = matches[0][2]
return
}
func getProfileOSHeaderValue() (values url.Values, err error) {
var info *osinfo.OSInfo
info, err = osinfo.GetOSInfo()
if err != nil {
return
}
values = make(url.Values)
values["family"] = []string{info.Family}
values["arch"] = []string{info.Architecture}
values["id"] = []string{info.ID}
values["version"] = []string{info.Version}
if len(info.Codename) > 0 {
values["codename"] = []string{info.Codename}
}
if len(info.Build) > 0 {
values["build"] = []string{info.Build}
}
return values, nil
}
type signingResponseData struct {
UserID string `json:"userId"`
ProfileSlot string `json:"profileSlot"`
CollabToken string `json:"collabToken"`
Agents []string `json:"agents"`
Expires uint64 `json:"expires,string"`
Signature string `json:"signature"`
Options bf_format.ProbeOptions `json:"options"`
Links linksMap `json:"_links"`
UUID string `json:"uuid"`
QueryString string `json:"query_string"`
}
func newSigningResponseData() *signingResponseData {
s := new(signingResponseData)
s.Options = make(bf_format.ProbeOptions)
return s
}