/
config.go
231 lines (194 loc) · 5.53 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
// 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 (
"fmt"
"net"
"os"
"sync"
"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"
)
var privateCfg = newPrivateConfig()
func newPrivateConfig() *privateConfig {
ret := &privateConfig{
daemonAddr: &net.UDPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 2000,
},
logLevel: log.InfoLvl,
logFormat: "%Date(2006-01-02T15:04:05Z07:00) [%Level] %Msg%n",
}
ss, err := sampling.NewLocalizedStrategy()
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 privateConfig 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
}
// Configure overrides default configuration options with customer-defined values.
func Configure(c Config) error {
privateCfg.Lock()
defer privateCfg.Unlock()
var errors exception.MultiError
var daemonAddress string
if addr := os.Getenv("AWS_XRAY_DAEMON_ADDRESS"); addr != "" {
daemonAddress = addr
} else if c.DaemonAddr != "" {
daemonAddress = c.DaemonAddr
}
if daemonAddress != "" {
addr, err := net.ResolveUDPAddr("udp", daemonAddress)
if err == nil {
privateCfg.daemonAddr = addr
go refreshEmitter()
} else {
errors = append(errors, err)
}
}
if c.SamplingStrategy != nil {
privateCfg.samplingStrategy = c.SamplingStrategy
}
if c.ExceptionFormattingStrategy != nil {
privateCfg.exceptionFormattingStrategy = c.ExceptionFormattingStrategy
}
if c.StreamingStrategy != nil {
privateCfg.streamingStrategy = c.StreamingStrategy
}
cms := os.Getenv("AWS_XRAY_CONTEXT_MISSING")
if cms != "" {
if cms == ctxmissing.RuntimeErrorStrategy {
cm := ctxmissing.NewDefaultRuntimeErrorStrategy()
privateCfg.contextMissingStrategy = cm
} else if cms == ctxmissing.LogErrorStrategy {
cm := ctxmissing.NewDefaultLogErrorStrategy()
privateCfg.contextMissingStrategy = cm
}
} else if c.ContextMissingStrategy != nil {
privateCfg.contextMissingStrategy = c.ContextMissingStrategy
}
if c.ServiceVersion != "" {
privateCfg.serviceVersion = c.ServiceVersion
}
privateCfg.logLevel, privateCfg.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 *privateConfig) DaemonAddr() *net.UDPAddr {
c.RLock()
defer c.RUnlock()
return c.daemonAddr
}
func (c *privateConfig) SamplingStrategy() sampling.Strategy {
c.RLock()
defer c.RUnlock()
return c.samplingStrategy
}
func (c *privateConfig) StreamingStrategy() StreamingStrategy {
c.RLock()
defer c.RUnlock()
return c.streamingStrategy
}
func (c *privateConfig) ExceptionFormattingStrategy() exception.FormattingStrategy {
c.RLock()
defer c.RUnlock()
return c.exceptionFormattingStrategy
}
func (c *privateConfig) ContextMissingStrategy() ctxmissing.Strategy {
c.RLock()
defer c.RUnlock()
return c.contextMissingStrategy
}
func (c *privateConfig) ServiceVersion() string {
c.RLock()
defer c.RUnlock()
return c.serviceVersion
}
func (c *privateConfig) LogLevel() log.LogLevel {
c.RLock()
defer c.RUnlock()
return c.logLevel
}
func (c *privateConfig) LogFormat() string {
c.RLock()
defer c.RUnlock()
return c.logFormat
}