forked from wavefrontHQ/wavefront-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption.go
More file actions
185 lines (163 loc) · 5.31 KB
/
option.go
File metadata and controls
185 lines (163 loc) · 5.31 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
package senders
import (
"crypto/tls"
"log"
"net/http"
"time"
"github.com/wavefronthq/wavefront-sdk-go/internal/auth"
)
// Option Wavefront client configuration options
type Option func(*configuration)
type httpClientConfiguration struct {
Timeout time.Duration
TLSClientConfig *tls.Config
}
// APIToken configures the sender to use a Wavefront API Token for authentication
func APIToken(apiToken string) Option {
return func(c *configuration) {
c.Authentication = auth.APIToken{
Token: apiToken,
}
}
}
// A CSPOption sets optional configuration for CSP Authentication
type CSPOption func(any)
// CSPBaseURL sets an alternative base URL for the CSP server
func CSPBaseURL(baseURL string) CSPOption {
return func(authentication any) {
switch a := authentication.(type) {
case *auth.CSPClientCredentials:
a.BaseURL = baseURL
case *auth.CSPAPIToken:
a.BaseURL = baseURL
}
}
}
// CSPOrgID sets an explicit orgID for Client Credentials authentication
func CSPOrgID(orgID string) CSPOption {
return func(authentication any) {
switch a := authentication.(type) {
case auth.CSPClientCredentials:
a.OrgID = &orgID
}
}
}
// CSPAPIToken configures the sender to use a CSP API Token for authentication
func CSPAPIToken(cspAPIToken string, options ...CSPOption) Option {
return func(c *configuration) {
cspTokenAuth := auth.CSPAPIToken{
Token: cspAPIToken,
BaseURL: defaultCSPBaseURL,
}
for _, option := range options {
option(&cspTokenAuth)
}
c.Authentication = cspTokenAuth
}
}
// CSPClientCredentials configures the sender to use a CSP Client Credentials for authentication
func CSPClientCredentials(clientID string, clientSecret string, options ...CSPOption) Option {
return func(c *configuration) {
clientCredentials := &auth.CSPClientCredentials{
ClientID: clientID,
ClientSecret: clientSecret,
BaseURL: defaultCSPBaseURL,
}
for _, option := range options {
option(clientCredentials)
}
c.Authentication = *clientCredentials
}
}
// BatchSize set max batch of data sent per flush interval. Defaults to 10,000. recommended not to exceed 40,000.
func BatchSize(n int) Option {
return func(cfg *configuration) {
cfg.BatchSize = n
}
}
// MaxBufferSize set the size of internal buffers beyond which received data is dropped. Defaults to 50,000.
func MaxBufferSize(n int) Option {
return func(cfg *configuration) {
cfg.MaxBufferSize = n
}
}
// FlushIntervalSeconds set the interval (in seconds) at which to flush data to Wavefront. Defaults to 1 Second.
func FlushIntervalSeconds(n int) Option {
return func(cfg *configuration) {
cfg.FlushInterval = time.Second * time.Duration(n)
}
}
// FlushInterval set the interval at which to flush data to Wavefront. Defaults to 1 Second.
func FlushInterval(interval time.Duration) Option {
return func(cfg *configuration) {
cfg.FlushInterval = interval
}
}
// MetricsPort sets the port on which to report metrics. Default is 2878.
func MetricsPort(port int) Option {
return func(cfg *configuration) {
cfg.MetricsPort = port
}
}
// TracesPort sets the port on which to report traces. Default is 30001.
func TracesPort(port int) Option {
return func(cfg *configuration) {
cfg.TracesPort = port
}
}
// Timeout sets the HTTP timeout. Defaults to 10 seconds.
func Timeout(timeout time.Duration) Option {
return func(cfg *configuration) {
if cfg.HTTPClient != nil {
log.Println("using Timeout after setting the HTTPClient is not supported." +
"If you are using the HTTPClient Option, set Timeout on the HTTPClient directly")
}
cfg.httpClientConfiguration.Timeout = timeout
}
}
// HTTPClient sets the http.Client used to send data to Wavefront.
// Overrides TLSConfigOptions and Timeout.
func HTTPClient(client *http.Client) Option {
return func(cfg *configuration) {
cfg.HTTPClient = client
}
}
// TLSConfigOptions sets the tls.Config used by the HTTP Client to send data to Wavefront.
func TLSConfigOptions(tlsCfg *tls.Config) Option {
tlsCfgCopy := tlsCfg.Clone()
return func(cfg *configuration) {
if cfg.HTTPClient != nil {
log.Println("using TLSConfigOptions after setting the HTTPClient is not supported." +
"If you are using the HTTPClient Option, set TLSClientConfig on the HTTPClient directly")
}
cfg.httpClientConfiguration.TLSClientConfig = tlsCfgCopy
}
}
// SendInternalMetrics turns sending of internal SDK metrics on/off.
func SendInternalMetrics(enabled bool) Option {
return func(cfg *configuration) {
cfg.SendInternalMetrics = enabled
}
}
// SDKMetricsTags adds the additional tags provided in tags to all internal
// metrics this library reports. Clients can use multiple SDKMetricsTags
// calls when creating a sender. In that case, the sender sends all the
// tags from each of the SDKMetricsTags calls in addition to the standard
// "pid" and "version" tags to all internal metrics. The "pid" tag is the
// process ID; the "version" tag is the version of this SDK.
func SDKMetricsTags(tags map[string]string) Option {
// prevent caller from accidentally mutating this option.
copiedTags := copyTags(tags)
return func(cfg *configuration) {
for key, value := range copiedTags {
cfg.SDKMetricsTags[key] = value
}
}
}
func copyTags(orig map[string]string) map[string]string {
result := make(map[string]string, len(orig))
for key, value := range orig {
result[key] = value
}
return result
}