@@ -11,11 +11,12 @@ import (
1111
1212// Logger for metrics with default Context.
1313type 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
1920}
2021
2122// Context gives ability to add another MetricDirective section for Logger.
@@ -41,21 +42,40 @@ func WithTimestamp(t time.Time) LoggerOption {
4142 }
4243}
4344
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+
4452// New creates logger with reasonable defaults for Lambda functions:
4553// - Prints to os.Stdout.
4654// - Context based on Lambda environment variables.
4755// - Timestamp set to the time when New was called.
4856// Specify LoggerOptions to customize the logger.
4957func 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+
5068 values := make (map [string ]interface {})
5169
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+ }
5979 }
6080
6181 // only collect traces which have been sampled
@@ -64,20 +84,10 @@ func New(opts ...LoggerOption) *Logger {
6484 values ["traceId" ] = amznTraceID
6585 }
6686
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 )
7489
75- // apply any options
76- for _ , opt := range opts {
77- opt (l )
78- }
79-
80- return l
90+ return & l
8191}
8292
8393// Dimension helps builds DimensionSet.
@@ -201,7 +211,7 @@ func (l *Logger) Log() {
201211
202212// NewContext creates new context for given logger.
203213func (l * Logger ) NewContext () * Context {
204- c := newContext (l .values )
214+ c := newContext (l .values , l . withoutDimensions )
205215 l .contexts = append (l .contexts , & c )
206216 return & c
207217}
@@ -250,15 +260,16 @@ func (c *Context) MetricFloatAs(name string, value float64, unit MetricUnit) *Co
250260 return c .put (name , value , unit )
251261}
252262
253- func newContext (values map [string ]interface {}) Context {
263+ func newContext (values map [string ]interface {}, withoutDimensions bool ) Context {
254264 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+ }
262273 }
263274
264275 return Context {
0 commit comments