-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtransport.go
More file actions
585 lines (514 loc) · 15.6 KB
/
transport.go
File metadata and controls
585 lines (514 loc) · 15.6 KB
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package statsig
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
const (
StatsigAPI = "https://statsigapi.net/v1"
StatsigCDN = "https://api.statsigcdn.com/v1"
)
const (
maxRetries = 5
backoffMultiplier = 10
defaultTimeout = 3 * time.Second
)
const (
maxRequestPathLength = 64
downloadConfigSpecsEndpoint = "download_config_specs"
getIDListsEndpoint = "get_id_lists"
downloadIDListFileEndpoint = "download_id_list_file"
networkRequestLatencyMetric = "network_request.latency"
requestPathTag = "request_path"
statusCodeTag = "status_code"
isSuccessTag = "is_success"
sdkKeyTag = "sdk_key"
sourceServiceTag = "source_service"
)
type transport struct {
sdkKey string
metadata statsigMetadata // Safe to read from but not thread safe to write into. If value needs to change, please ensure thread safety.
client *http.Client
options *Options
}
func newHTTPClient(options *Options) *http.Client {
if options.HTTPClient != nil {
return options.HTTPClient
}
timeout := options.NetworkTimeout
if timeout == 0 {
timeout = defaultTimeout
}
return &http.Client{
Timeout: timeout,
Transport: options.Transport,
}
}
func newTransport(secret string, options *Options) *transport {
defer func() {
if err := recover(); err != nil {
Logger().LogError(err)
}
}()
return &transport{
metadata: getStatsigMetadata(),
sdkKey: secret,
client: newHTTPClient(options),
options: options,
}
}
type RequestOptions struct {
retries int
backoff time.Duration
header map[string]string
}
func (opts *RequestOptions) fill_defaults() {
if opts.backoff == 0 {
opts.backoff = time.Second
}
}
func (transport *transport) download_config_specs(sinceTime int64, responseBody interface{}, diagnostics *marker, context *initContext) (*http.Response, error) {
if diagnostics != nil {
diagnostics.downloadConfigSpecs().networkRequest().start().mark()
}
var endpoint string
if transport.options.DisableCDN {
if sinceTime == 0 {
endpoint = "/download_config_specs"
} else {
endpoint = fmt.Sprintf("/download_config_specs?sinceTime=%d", sinceTime)
}
} else {
if sinceTime == 0 {
endpoint = fmt.Sprintf("/download_config_specs/%s.json", transport.sdkKey)
} else {
endpoint = fmt.Sprintf("/download_config_specs/%s.json?sinceTime=%d", transport.sdkKey, sinceTime)
}
}
options := RequestOptions{}
if transport.options.FallbackToStatsigAPI {
options.retries = 1
}
return transport.get(endpoint, responseBody, options, diagnostics, context)
}
func (transport *transport) get_id_lists(responseBody interface{}, diagnostics *marker) (*http.Response, error) {
if diagnostics != nil {
diagnostics.getIdListSources().networkRequest().start().mark()
}
options := RequestOptions{}
if transport.options.FallbackToStatsigAPI {
options.retries = 1
}
return transport.post("/get_id_lists", nil, responseBody, options, diagnostics)
}
func (transport *transport) get_id_list(url string, headers map[string]string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, &TransportError{Err: err}
}
for k, v := range headers {
req.Header.Set(k, v)
}
requestStart := time.Now()
res, err := transport.client.Do(req)
durationMs := float64(time.Since(requestStart).Milliseconds())
statusCode := "none"
success := false
if res != nil {
statusCode = strconv.Itoa(res.StatusCode)
success = successfulStatusCode(res.StatusCode)
}
transport.logNetworkRequestLatency(url, statusCode, success, durationMs)
if err != nil {
var statusCode int
if res != nil {
statusCode = res.StatusCode
}
return res, &TransportError{
RequestMetadata: &RequestMetadata{
StatusCode: statusCode,
Endpoint: url,
Retries: 0,
},
Err: err}
}
return res, nil
}
func (transport *transport) log_event(
event []interface{},
responseBody interface{},
options RequestOptions,
) (*http.Response, error) {
input := logEventInput{
Events: event,
StatsigMetadata: transport.metadata,
}
if options.header == nil {
options.header = make(map[string]string)
}
options.header["statsig-event-count"] = strconv.Itoa(len(event))
return transport.post("/log_event", input, responseBody, options, nil)
}
func (transport *transport) post(
endpoint string,
body interface{},
responseBody interface{},
options RequestOptions,
diagnostics *marker,
) (*http.Response, error) {
return transport.doRequest("POST", endpoint, body, responseBody, options, diagnostics, nil)
}
func (transport *transport) get(
endpoint string,
responseBody interface{},
options RequestOptions,
diagnostics *marker,
context *initContext,
) (*http.Response, error) {
return transport.doRequest("GET", endpoint, nil, responseBody, options, diagnostics, context)
}
func (transport *transport) buildRequest(method, endpoint string, body interface{}, header map[string]string, context *initContext) (*http.Request, error) {
if transport.options.LocalMode {
return nil, nil
}
baseAPI := transport.getBaseAPI(endpoint, false)
var bodyBuf io.Reader
if body != nil {
bodyBytes, err := json.Marshal(body)
if err != nil {
return nil, err
}
bodyBuf = bytes.NewBuffer(bodyBytes)
if strings.Contains(endpoint, "log_event") {
var compressedBody bytes.Buffer
gz := gzip.NewWriter(&compressedBody)
_, _ = gz.Write(bodyBytes)
_ = gz.Close()
bodyBuf = &compressedBody
}
} else {
if method == "POST" {
bodyBuf = bytes.NewBufferString("{}")
}
}
url, err := transport.buildURL(endpoint, false, context)
if err != nil {
transport.logDownloadConfigSpecsRequestError(endpoint, baseAPI, err)
return nil, err
}
req, err := http.NewRequest(method, url.String(), bodyBuf)
if err != nil {
transport.logDownloadConfigSpecsRequestError(endpoint, baseAPI, err)
return nil, err
}
req.Header.Add("STATSIG-API-KEY", transport.sdkKey)
req.Header.Set("Content-Type", "application/json")
if strings.Contains(endpoint, "log_event") {
req.Header.Set("Content-Encoding", "gzip")
}
req.Header.Add("STATSIG-CLIENT-TIME", strconv.FormatInt(getUnixMilli(), 10))
req.Header.Add("STATSIG-SERVER-SESSION-ID", transport.metadata.SessionID)
req.Header.Add("STATSIG-SDK-TYPE", transport.metadata.SDKType)
req.Header.Add("STATSIG-SDK-VERSION", transport.metadata.SDKVersion)
req.Header.Add("STATSIG-SDK-LANGUAGE-VERSION", transport.metadata.LanguageVersion)
for k, v := range header {
req.Header.Add(k, v)
}
return req, nil
}
func (t *transport) getBaseAPI(path string, isRetry bool) string {
useDefaultAPI := isRetry && t.options.FallbackToStatsigAPI
endpoint := strings.TrimPrefix(path, "/v1")
if strings.Contains(endpoint, "download_config_specs") {
if useDefaultAPI {
return StatsigCDN
} else {
return defaultString(t.options.APIOverrides.DownloadConfigSpecs, defaultString(t.options.API, StatsigCDN))
}
} else if strings.Contains(endpoint, "get_id_list") {
if useDefaultAPI {
return StatsigAPI
} else {
return defaultString(t.options.APIOverrides.GetIDLists, defaultString(t.options.API, StatsigAPI))
}
} else if strings.Contains(endpoint, "log_event") {
if useDefaultAPI {
return StatsigAPI
} else {
return defaultString(t.options.APIOverrides.LogEvent, defaultString(t.options.API, StatsigAPI))
}
} else {
if useDefaultAPI {
return StatsigAPI
} else {
return defaultString(t.options.API, StatsigAPI)
}
}
}
func (t *transport) buildURL(path string, isRetry bool, context *initContext) (*url.URL, error) {
api := t.getBaseAPI(path, isRetry)
endpoint := strings.TrimPrefix(path, "/v1")
if strings.Contains(endpoint, "download_config_specs") && context != nil {
context.setCurrentSourceAPI(getAPIFromURL(api))
}
return url.Parse(strings.TrimSuffix(api, "/") + endpoint)
}
// getAPIFromURL normalizes a URL for observability tags:
// if the path starts with /v<digits>, keep only that version prefix.
func getAPIFromURL(rawURL string) string {
schemeEnd := strings.Index(rawURL, "://")
if schemeEnd == -1 {
return rawURL
}
afterScheme := rawURL[schemeEnd+3:]
slash := strings.Index(afterScheme, "/")
if slash == -1 {
return rawURL
}
base := rawURL[:schemeEnd+3+slash]
path := rawURL[schemeEnd+3+slash:]
if !strings.HasPrefix(path, "/v") {
return base
}
rest := path[2:]
if len(rest) == 0 {
return base
}
digitCount := 0
for ; digitCount < len(rest); digitCount++ {
if rest[digitCount] < '0' || rest[digitCount] > '9' {
break
}
}
if digitCount == 0 {
return base
}
if digitCount == len(rest) || rest[digitCount] == '/' {
return base + "/v" + rest[:digitCount]
}
return base
}
func isVersionSegment(segment string) bool {
if len(segment) <= 1 || segment[0] != 'v' {
return false
}
for i := 1; i < len(segment); i++ {
if segment[i] < '0' || segment[i] > '9' {
return false
}
}
return true
}
func isLatencyLoggableEndpoint(endpoint string) bool {
return endpoint == downloadConfigSpecsEndpoint ||
endpoint == getIDListsEndpoint ||
endpoint == downloadIDListFileEndpoint
}
func getNetworkSourceServiceAndRequestPath(rawURL string) (string, string) {
parsed, err := url.Parse(rawURL)
if err != nil {
return "", "/"
}
sourceService := parsed.Scheme + "://" + parsed.Host
normalizedPath := strings.Trim(parsed.EscapedPath(), "/")
if normalizedPath == "" {
return sourceService, "/"
}
segments := strings.Split(normalizedPath, "/")
for idx, segment := range segments {
if !isLatencyLoggableEndpoint(segment) {
continue
}
if idx > 0 && isVersionSegment(segments[idx-1]) {
requestPath := "/" + segments[idx-1] + "/" + segment
if idx-1 > 0 {
sourceService = sourceService + "/" + strings.Join(segments[:idx-1], "/")
}
return sourceService, requestPath
}
return sourceService, "/" + segment
}
fallbackPath := normalizedPath
if len(fallbackPath) > maxRequestPathLength {
fallbackPath = fallbackPath[:maxRequestPathLength]
}
return sourceService, "/" + fallbackPath
}
func shouldLogNetworkRequestLatency(rawURL string) bool {
_, requestPath := getNetworkSourceServiceAndRequestPath(rawURL)
return requestPath == "/"+downloadConfigSpecsEndpoint ||
requestPath == "/"+getIDListsEndpoint ||
requestPath == "/"+downloadIDListFileEndpoint ||
requestPath == "/v1/"+downloadConfigSpecsEndpoint ||
requestPath == "/v1/"+getIDListsEndpoint ||
requestPath == "/v1/"+downloadIDListFileEndpoint
}
func (transport *transport) logNetworkRequestLatency(rawURL string, statusCode string, success bool, durationMs float64) {
if !shouldLogNetworkRequestLatency(rawURL) {
return
}
sourceService, requestPath := getNetworkSourceServiceAndRequestPath(rawURL)
Logger().Distribution(networkRequestLatencyMetric, durationMs, map[string]interface{}{
requestPathTag: requestPath,
statusCodeTag: statusCode,
isSuccessTag: strconv.FormatBool(success),
sdkKeyTag: getLoggableSDKKey(transport.sdkKey),
sourceServiceTag: sourceService,
})
}
func (t *transport) logDownloadConfigSpecsRequestError(endpoint string, baseAPI string, err error) {
if err == nil || !strings.Contains(endpoint, "download_config_specs") {
return
}
Logger().LogError(fmt.Errorf(
"failed to build download_config_specs request (base_api=%s, endpoint=%s): %w",
baseAPI,
endpoint,
err,
))
}
func (t *transport) updateRequestForRetry(r *http.Request, context *initContext) *http.Request {
retryURL, err := t.buildURL(r.URL.Path, true, context)
if err == nil && strings.Compare(r.URL.Host, retryURL.Host) != 0 {
retryRequest, err := http.NewRequest(r.Method, retryURL.String(), r.Body)
if err == nil {
for k, v := range r.Header {
retryRequest.Header[k] = v
}
return retryRequest
}
}
return nil
}
func (transport *transport) doRequest(
method string,
endpoint string,
in interface{},
out interface{},
options RequestOptions,
diagnostics *marker,
context *initContext,
) (*http.Response, error) {
request, err := transport.buildRequest(method, endpoint, in, options.header, context)
if request == nil || err != nil {
if err != nil {
return nil, &TransportError{Err: err}
}
return nil, nil
}
options.fill_defaults()
response, attempts, err := retry(options.retries, time.Duration(options.backoff), func() (*http.Response, bool, error) {
requestStart := time.Now()
response, err := transport.client.Do(request)
durationMs := float64(time.Since(requestStart).Milliseconds())
statusCode := "none"
success := false
if response != nil {
statusCode = strconv.Itoa(response.StatusCode)
success = successfulStatusCode(response.StatusCode)
}
transport.logNetworkRequestLatency(request.URL.String(), statusCode, success, durationMs)
if diagnostics != nil {
diagnostics.end()
if response != nil {
diagnostics.success(successfulStatusCode(response.StatusCode))
diagnostics.statusCode(response.StatusCode)
diagnostics.sdkRegion(safeGetFirst(response.Header["X-Statsig-Region"]))
} else {
diagnostics.success(false)
}
diagnostics.mark()
}
// Check if we can fallback to default API (before handling errors)
retryRequest := transport.updateRequestForRetry(request, context)
canFallbackToDefault := retryRequest != nil
if retryRequest != nil {
request = retryRequest
}
if err != nil {
shouldRetry := transport.shouldRetryRequest(err, response, canFallbackToDefault)
return response, shouldRetry, err
}
drainAndCloseBody := func() {
if response.Body != nil {
// Drain body to re-use the same connection
_, _ = io.Copy(io.Discard, response.Body)
CloseBodyIgnoreErrors(response.Body)
}
}
defer drainAndCloseBody()
if successfulStatusCode(response.StatusCode) {
return response, false, transport.parseResponse(response, out)
}
shouldRetry := transport.shouldRetryRequest(nil, response, canFallbackToDefault)
return response, shouldRetry, fmt.Errorf("%s", response.Status)
})
if err != nil {
if response == nil {
return response, &TransportError{Err: err}
}
return response, &TransportError{
RequestMetadata: &RequestMetadata{
StatusCode: response.StatusCode,
Endpoint: endpoint,
Retries: attempts,
},
Err: err,
}
}
return response, nil
}
func (transport *transport) shouldRetryRequest(err error, response *http.Response, canFallbackToDefault bool) bool {
// If there's an error, retry only if we can fallback to default API
if err != nil {
return transport.options.FallbackToStatsigAPI && canFallbackToDefault
}
// If successful status code, no need to retry
if successfulStatusCode(response.StatusCode) {
return false
}
// For non-successful status codes, retry if:
// 1. Status code is retryable (408, 500, 502, 503, 504, 522, 524, 599), OR
// 2. We can fallback to default API (allows retry even for non-retryable codes like 404)
return retryableStatusCode(response.StatusCode) || (transport.options.FallbackToStatsigAPI && canFallbackToDefault)
}
func (transport *transport) parseResponse(response *http.Response, out interface{}) error {
if out == nil {
return nil
}
return json.NewDecoder(response.Body).Decode(&out)
}
func retry(retries int, backoff time.Duration, fn func() (*http.Response, bool, error)) (*http.Response, int, error) {
attempts := 0
for {
if response, retry, err := fn(); retry {
if retries <= 0 {
return response, attempts, err
}
retries--
attempts++
time.Sleep(backoff)
backoff = backoff * backoffMultiplier
} else {
return response, attempts, err
}
}
}
func retryableStatusCode(code int) bool {
switch code {
case 408, 500, 502, 503, 504, 522, 524, 599:
return true
default:
return false
}
}
func successfulStatusCode(code int) bool {
return code >= 200 && code < 300
}