forked from aws/aws-xray-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
286 lines (236 loc) · 7.07 KB
/
config.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
// Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
package xray
import (
"context"
"fmt"
"net"
"os"
"sync"
"github.com/aws/aws-xray-sdk-go/daemoncfg"
"github.com/aws/aws-xray-sdk-go/strategy/ctxmissing"
"github.com/aws/aws-xray-sdk-go/strategy/exception"
"github.com/aws/aws-xray-sdk-go/strategy/sampling"
log "github.com/cihub/seelog"
)
// SDKVersion records the current X-Ray Go SDK version.
const SDKVersion = "1.0.0-rc.7"
// SDKType records which X-Ray SDK customer uses.
const SDKType = "X-Ray for Go"
// SDK provides the shape for unmarshalling an SDK struct.
type SDK struct {
Version string `json:"sdk_version,omitempty"`
Type string `json:"sdk,omitempty"`
RuleName string `json:"sampling_rule_name,omitempty"`
}
var globalCfg = newGlobalConfig()
func newGlobalConfig() *globalConfig {
ret := &globalConfig{}
// Set the logging configuration to the defaults
ret.logLevel, ret.logFormat = loadLogConfig("", "")
ret.daemonAddr = daemoncfg.GetDaemonEndpoints().UDPAddr
ss, err := sampling.NewCentralizedStrategy()
if err != nil {
panic(err)
}
ret.samplingStrategy = ss
efs, err := exception.NewDefaultFormattingStrategy()
if err != nil {
panic(err)
}
ret.exceptionFormattingStrategy = efs
sts, err := NewDefaultStreamingStrategy()
if err != nil {
panic(err)
}
ret.streamingStrategy = sts
cm := ctxmissing.NewDefaultRuntimeErrorStrategy()
ret.contextMissingStrategy = cm
return ret
}
type globalConfig struct {
sync.RWMutex
daemonAddr *net.UDPAddr
serviceVersion string
samplingStrategy sampling.Strategy
streamingStrategy StreamingStrategy
exceptionFormattingStrategy exception.FormattingStrategy
contextMissingStrategy ctxmissing.Strategy
logLevel log.LogLevel
logFormat string
}
// Config is a set of X-Ray configurations.
type Config struct {
DaemonAddr string
ServiceVersion string
SamplingStrategy sampling.Strategy
StreamingStrategy StreamingStrategy
ExceptionFormattingStrategy exception.FormattingStrategy
ContextMissingStrategy ctxmissing.Strategy
LogLevel string
LogFormat string
}
// ContextWithConfig returns context with given configuration settings.
func ContextWithConfig(ctx context.Context, c Config) (context.Context, error) {
var errors exception.MultiError
daemonEndpoints, er := daemoncfg.GetDaemonEndpointsFromString(c.DaemonAddr)
if daemonEndpoints != nil {
go refreshEmitterWithAddress(daemonEndpoints.UDPAddr)
configureStrategy(c.SamplingStrategy, daemonEndpoints)
} else if er != nil {
errors = append(errors, er)
}
cms := os.Getenv("AWS_XRAY_CONTEXT_MISSING")
if cms != "" {
if cms == ctxmissing.RuntimeErrorStrategy {
cm := ctxmissing.NewDefaultRuntimeErrorStrategy()
c.ContextMissingStrategy = cm
} else if cms == ctxmissing.LogErrorStrategy {
cm := ctxmissing.NewDefaultLogErrorStrategy()
c.ContextMissingStrategy = cm
}
}
loadLogConfig(c.LogLevel, c.LogFormat)
var err error
switch len(errors) {
case 0:
err = nil
case 1:
err = errors[0]
default:
err = errors
}
return context.WithValue(ctx, RecorderContextKey{}, &c), err
}
func configureStrategy(s sampling.Strategy, daemonEndpoints *daemoncfg.DaemonEndpoints) {
if s == nil {
return
}
strategy, ok := s.(*sampling.CentralizedStrategy)
if ok {
strategy.LoadDaemonEndpoints(daemonEndpoints)
}
}
// Configure overrides default configuration options with customer-defined values.
func Configure(c Config) error {
globalCfg.Lock()
defer globalCfg.Unlock()
var errors exception.MultiError
if c.SamplingStrategy != nil {
globalCfg.samplingStrategy = c.SamplingStrategy
}
daemonEndpoints, er := daemoncfg.GetDaemonEndpointsFromString(c.DaemonAddr)
if daemonEndpoints != nil {
globalCfg.daemonAddr = daemonEndpoints.UDPAddr
go refreshEmitter()
configureStrategy(globalCfg.samplingStrategy, daemonEndpoints)
} else if er != nil {
errors = append(errors, er)
}
if c.ExceptionFormattingStrategy != nil {
globalCfg.exceptionFormattingStrategy = c.ExceptionFormattingStrategy
}
if c.StreamingStrategy != nil {
globalCfg.streamingStrategy = c.StreamingStrategy
}
cms := os.Getenv("AWS_XRAY_CONTEXT_MISSING")
if cms != "" {
if cms == ctxmissing.RuntimeErrorStrategy {
cm := ctxmissing.NewDefaultRuntimeErrorStrategy()
globalCfg.contextMissingStrategy = cm
} else if cms == ctxmissing.LogErrorStrategy {
cm := ctxmissing.NewDefaultLogErrorStrategy()
globalCfg.contextMissingStrategy = cm
}
} else if c.ContextMissingStrategy != nil {
globalCfg.contextMissingStrategy = c.ContextMissingStrategy
}
if c.ServiceVersion != "" {
globalCfg.serviceVersion = c.ServiceVersion
}
globalCfg.logLevel, globalCfg.logFormat = loadLogConfig(c.LogLevel, c.LogFormat)
switch len(errors) {
case 0:
return nil
case 1:
return errors[0]
default:
return errors
}
}
func loadLogConfig(logLevel string, logFormat string) (log.LogLevel, string) {
var level log.LogLevel
var format string
switch logLevel {
case "trace":
level = log.TraceLvl
case "debug":
level = log.DebugLvl
case "info":
level = log.InfoLvl
case "warn":
level = log.WarnLvl
case "error":
level = log.ErrorLvl
default:
level = log.InfoLvl
logLevel = "info"
}
if logFormat != "" {
format = logFormat
} else {
format = "%Date(2006-01-02T15:04:05Z07:00) [%Level] %Msg%n"
}
writer, _ := log.NewConsoleWriter()
logger, err := log.LoggerFromWriterWithMinLevelAndFormat(writer, level, format)
if err != nil {
panic(fmt.Errorf("failed to create logs as StdOut: %v", err))
}
log.ReplaceLogger(logger)
return level, format
}
func (c *globalConfig) DaemonAddr() *net.UDPAddr {
c.RLock()
defer c.RUnlock()
return c.daemonAddr
}
func (c *globalConfig) SamplingStrategy() sampling.Strategy {
c.RLock()
defer c.RUnlock()
return c.samplingStrategy
}
func (c *globalConfig) StreamingStrategy() StreamingStrategy {
c.RLock()
defer c.RUnlock()
return c.streamingStrategy
}
func (c *globalConfig) ExceptionFormattingStrategy() exception.FormattingStrategy {
c.RLock()
defer c.RUnlock()
return c.exceptionFormattingStrategy
}
func (c *globalConfig) ContextMissingStrategy() ctxmissing.Strategy {
c.RLock()
defer c.RUnlock()
return c.contextMissingStrategy
}
func (c *globalConfig) ServiceVersion() string {
c.RLock()
defer c.RUnlock()
return c.serviceVersion
}
func (c *globalConfig) LogLevel() log.LogLevel {
c.RLock()
defer c.RUnlock()
return c.logLevel
}
func (c *globalConfig) LogFormat() string {
c.RLock()
defer c.RUnlock()
return c.logFormat
}