@@ -11,11 +11,12 @@ import (
11
11
12
12
// Logger for metrics with default Context.
13
13
type Logger struct {
14
- out io.Writer
15
- timestamp int64
16
- defaultContext Context
17
- contexts []* Context
18
- values map [string ]interface {}
14
+ out io.Writer
15
+ timestamp int64
16
+ defaultContext Context
17
+ contexts []* Context
18
+ values map [string ]interface {}
19
+ withoutDimensions bool
19
20
}
20
21
21
22
// Context gives ability to add another MetricDirective section for Logger.
@@ -41,21 +42,40 @@ func WithTimestamp(t time.Time) LoggerOption {
41
42
}
42
43
}
43
44
45
+ // WithoutDimensions ignores default AWS Lambda related properties and dimensions.
46
+ func WithoutDimensions () LoggerOption {
47
+ return func (l * Logger ) {
48
+ l .withoutDimensions = true
49
+ }
50
+ }
51
+
44
52
// New creates logger with reasonable defaults for Lambda functions:
45
53
// - Prints to os.Stdout.
46
54
// - Context based on Lambda environment variables.
47
55
// - Timestamp set to the time when New was called.
48
56
// Specify LoggerOptions to customize the logger.
49
57
func New (opts ... LoggerOption ) * Logger {
58
+ l := Logger {
59
+ out : os .Stdout ,
60
+ timestamp : time .Now ().UnixNano () / int64 (time .Millisecond ),
61
+ }
62
+
63
+ // apply any options
64
+ for _ , opt := range opts {
65
+ opt (& l )
66
+ }
67
+
50
68
values := make (map [string ]interface {})
51
69
52
- // set default properties for lambda function
53
- fnName := os .Getenv ("AWS_LAMBDA_FUNCTION_NAME" )
54
- if fnName != "" {
55
- values ["executionEnvironment" ] = os .Getenv ("AWS_EXECUTION_ENV" )
56
- values ["memorySize" ] = os .Getenv ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE" )
57
- values ["functionVersion" ] = os .Getenv ("AWS_LAMBDA_FUNCTION_VERSION" )
58
- values ["logStreamId" ] = os .Getenv ("AWS_LAMBDA_LOG_STREAM_NAME" )
70
+ if ! l .withoutDimensions {
71
+ // set default properties for lambda function
72
+ fnName := os .Getenv ("AWS_LAMBDA_FUNCTION_NAME" )
73
+ if fnName != "" {
74
+ values ["executionEnvironment" ] = os .Getenv ("AWS_EXECUTION_ENV" )
75
+ values ["memorySize" ] = os .Getenv ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE" )
76
+ values ["functionVersion" ] = os .Getenv ("AWS_LAMBDA_FUNCTION_VERSION" )
77
+ values ["logStreamId" ] = os .Getenv ("AWS_LAMBDA_LOG_STREAM_NAME" )
78
+ }
59
79
}
60
80
61
81
// only collect traces which have been sampled
@@ -64,20 +84,10 @@ func New(opts ...LoggerOption) *Logger {
64
84
values ["traceId" ] = amznTraceID
65
85
}
66
86
67
- // create a default logger
68
- l := & Logger {
69
- out : os .Stdout ,
70
- defaultContext : newContext (values ),
71
- values : values ,
72
- timestamp : time .Now ().UnixNano () / int64 (time .Millisecond ),
73
- }
87
+ l .values = values
88
+ l .defaultContext = newContext (values , l .withoutDimensions )
74
89
75
- // apply any options
76
- for _ , opt := range opts {
77
- opt (l )
78
- }
79
-
80
- return l
90
+ return & l
81
91
}
82
92
83
93
// Dimension helps builds DimensionSet.
@@ -201,7 +211,7 @@ func (l *Logger) Log() {
201
211
202
212
// NewContext creates new context for given logger.
203
213
func (l * Logger ) NewContext () * Context {
204
- c := newContext (l .values )
214
+ c := newContext (l .values , l . withoutDimensions )
205
215
l .contexts = append (l .contexts , & c )
206
216
return & c
207
217
}
@@ -250,15 +260,16 @@ func (c *Context) MetricFloatAs(name string, value float64, unit MetricUnit) *Co
250
260
return c .put (name , value , unit )
251
261
}
252
262
253
- func newContext (values map [string ]interface {}) Context {
263
+ func newContext (values map [string ]interface {}, withoutDimensions bool ) Context {
254
264
var defaultDimensions []DimensionSet
255
-
256
- // set default dimensions for lambda function
257
- fnName := os .Getenv ("AWS_LAMBDA_FUNCTION_NAME" )
258
- if fnName != "" {
259
- defaultDimensions = []DimensionSet {{"ServiceName" , "ServiceType" }}
260
- values ["ServiceType" ] = "AWS::Lambda::Function"
261
- values ["ServiceName" ] = fnName
265
+ if ! withoutDimensions {
266
+ // set default dimensions for lambda function
267
+ fnName := os .Getenv ("AWS_LAMBDA_FUNCTION_NAME" )
268
+ if fnName != "" {
269
+ defaultDimensions = []DimensionSet {{"ServiceName" , "ServiceType" }}
270
+ values ["ServiceType" ] = "AWS::Lambda::Function"
271
+ values ["ServiceName" ] = fnName
272
+ }
262
273
}
263
274
264
275
return Context {
0 commit comments