Skip to content

Commit

Permalink
Add namespace and log group name setting (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Nov 5, 2023
1 parent f7a83eb commit b3ec34a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
8 changes: 8 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ func (l *Logger) Dimension(key, value string) *Logger {
return l
}

// Namespace sets the namespace for the Logger.
func (l *Logger) Namespace(namespace string) *Logger {
l.mu.Lock()
defer l.mu.Unlock()
l.metrics.Namespace = namespace
return l
}

// Flush outputs the collected metrics to stdout so they can be discovered by CloudWatch.
//
// Typical usage in a lambda handler would be to populate metrics throughout and then
Expand Down
16 changes: 16 additions & 0 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ func TestMetricsLog(t *testing.T) {
},
want: "env.json",
},
{
name: "namespace",
logFn: func(logger *metrics.Logger) {
logger.Namespace("MyNameSpace").Add("Foo", 27, unit.Percent, metrics.StandardResolution)
},
want: "namespace.json",
},
{
name: "log group name",
logFn: func(logger *metrics.Logger) {
logger.Namespace("MyNameSpace").Add("Foo", 27, unit.Percent, metrics.StandardResolution)
fn := metrics.LogGroupName("MyLogGroup")
fn(logger)
},
want: "log-group-name.json",
},
}

for _, tt := range tests {
Expand Down
32 changes: 32 additions & 0 deletions testdata/log-group-name.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"_aws": {
"Timestamp": "<<PRESENCE>>",
"LogGroupName": "MyLogGroup",
"CloudWatchMetrics": [
{
"Namespace": "MyNameSpace",
"Dimensions": [
[
"ServiceName",
"ServiceType"
]
],
"Metrics": [
{
"Name": "Foo",
"Unit": "Percent",
"StorageResolution": 60
}
]
}
]
},
"executionEnvironment": "",
"functionName": "",
"functionVersion": "",
"logStreamId": "",
"memorySize": "",
"ServiceName": "",
"ServiceType": "AWS::Lambda::Function",
"Foo": 27
}
31 changes: 31 additions & 0 deletions testdata/namespace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"_aws": {
"Timestamp": "<<PRESENCE>>",
"CloudWatchMetrics": [
{
"Namespace": "MyNameSpace",
"Dimensions": [
[
"ServiceName",
"ServiceType"
]
],
"Metrics": [
{
"Name": "Foo",
"Unit": "Percent",
"StorageResolution": 60
}
]
}
]
},
"executionEnvironment": "",
"functionName": "",
"functionVersion": "",
"logStreamId": "",
"memorySize": "",
"ServiceName": "",
"ServiceType": "AWS::Lambda::Function",
"Foo": 27
}

0 comments on commit b3ec34a

Please sign in to comment.