-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraceroptions.go
More file actions
114 lines (93 loc) · 3.08 KB
/
Copy pathtraceroptions.go
File metadata and controls
114 lines (93 loc) · 3.08 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
package gqlhive
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/domonda/go-types/nullable"
"github.com/domonda/go-types/uu"
)
const defaultEndpoint = "https://app.graphql-hive.com/usage"
// WithEndpoint sets the endpoint to where the reports are sent.
// Defaults to "https://app.graphql-hive.com/usage".
func WithEndpoint(endpoint string) TracerOption {
return tracerOptionFn(func(tracer *Tracer) {
tracer.endpoint = endpoint
})
}
func defaultGenerateID(operation string, operationName nullable.TrimmedString) string {
return uu.IDv4().String()
}
// GenerateID creates unique operation IDs for the report.
type GenerateID func(operation string, operationName nullable.TrimmedString) string
// WithGenerateID sets the unique operation ID generator for the reports.
// Defaults to generating v4 UUIDs.
func WithGenerateID(fn GenerateID) TracerOption {
return tracerOptionFn(func(tracer *Tracer) {
tracer.generateID = fn
})
}
var defaultSendReportTimeout time.Duration = 3 * time.Second
// WithSendReportTimeout sets the report sending debounce timeout.
// Executed operations will queue up and then be flushed/sent to GraphQL Hive after the timeout expires.
func WithSendReportTimeout(timeout time.Duration) TracerOption {
return tracerOptionFn(func(tracer *Tracer) {
tracer.sendReportTimeout = timeout
})
}
func defaultSendReport(ctx context.Context, endpoint, target, token string, report *Report) error {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(report)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, "POST", endpoint+"/"+target, &buf)
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Add("X-Usage-API-Version", "2")
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
bodyStr := string(body)
if bodyStr == "" {
return fmt.Errorf("report sending failed with %s (no body)", res.Status)
} else {
return fmt.Errorf("report sending failed with %s: %s", res.Status, bodyStr)
}
}
return nil
}
// SendReport performs the actual report sending to GraphQL Hive.
type SendReport func(ctx context.Context, endpoint, target, token string, report *Report) error
// WithSendReport sets the report sender to GraphQL Hive.
func WithSendReport(fn SendReport) TracerOption {
return tracerOptionFn(func(tracer *Tracer) {
tracer.sendReport = fn
})
}
// WithLogger sets the logger to be used by the tracer.
// The logger is used for reporting errors during tracing. If set to nil, logging is disabled.
// You can use the standard Go logger or provide a custom implementation (e.g., logrus, zap).
func WithLogger(logger Logger) TracerOption {
return tracerOptionFn(func(tracer *Tracer) {
tracer.log = logger
})
}
var defaultLogger = NewLogger()
type TracerOption interface {
set(*Tracer)
}
type tracerOptionFn func(*Tracer)
func (fn tracerOptionFn) set(config *Tracer) {
fn(config)
}