Skip to content

Commit 7ab9c16

Browse files
authored
Merge pull request #7 from mtagstyle/master
Add the option to set log group as per https://docs.aws.amazon.com/Am…
2 parents c558611 + 5e4df5a commit 7ab9c16

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

emf/emf.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package emf
33

44
// Metadata struct as defined in AWS Embedded Metrics Format spec.
55
type Metadata struct {
6-
Timestamp int64 `json:"Timestamp"`
7-
Metrics []MetricDirective `json:"CloudWatchMetrics"`
6+
Timestamp int64 `json:"Timestamp"`
7+
Metrics []MetricDirective `json:"CloudWatchMetrics"`
8+
LogGroupName string `json:"LogGroupName,omitempty"`
89
}
910

1011
// MetricDirective struct as defined in AWS Embedded Metrics Format spec.

emf/logger.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Logger struct {
1717
contexts []*Context
1818
values map[string]interface{}
1919
withoutDimensions bool
20+
logGroupName string
2021
}
2122

2223
// Context gives ability to add another MetricDirective section for Logger.
@@ -49,6 +50,13 @@ func WithoutDimensions() LoggerOption {
4950
}
5051
}
5152

53+
// WithLogGroup sets the log group when ingesting metrics into Cloudwatch Logging Agent.
54+
func WithLogGroup(logGroup string) LoggerOption {
55+
return func(l *Logger) {
56+
l.logGroupName = logGroup
57+
}
58+
}
59+
5260
// New creates logger with reasonable defaults for Lambda functions:
5361
// - Prints to os.Stdout.
5462
// - Context based on Lambda environment variables.
@@ -202,8 +210,9 @@ func (l *Logger) Log() {
202210
}
203211

204212
l.values["_aws"] = Metadata{
205-
Timestamp: l.timestamp,
206-
Metrics: metrics,
213+
Timestamp: l.timestamp,
214+
Metrics: metrics,
215+
LogGroupName: l.logGroupName,
207216
}
208217
buf, _ := json.Marshal(l.values)
209218
_, _ = fmt.Fprintln(l.out, string(buf))

emf/logger_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ func TestEmf(t *testing.T) {
189189
},
190190
expected: "testdata/17.json",
191191
},
192+
{
193+
name: "with log group",
194+
new: func(buf *bytes.Buffer) *emf.Logger {
195+
return emf.New(emf.WithLogGroup("test_log_group"), emf.WithWriter(buf))
196+
},
197+
given: func(logger *emf.Logger) {
198+
logger.Metric("foo", 33)
199+
},
200+
expected: "testdata/18.json",
201+
},
192202
}
193203

194204
for _, tc := range tcs {

emf/testdata/18.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"_aws": {
3+
"Timestamp": "<<PRESENCE>>",
4+
"LogGroupName": "test_log_group",
5+
"CloudWatchMetrics": [
6+
{
7+
"Metrics": [
8+
{
9+
"Name": "foo",
10+
"Unit": "None"
11+
}
12+
],
13+
"Namespace": "aws-embedded-metrics",
14+
"Dimensions": null
15+
}
16+
]
17+
},
18+
"foo": 33
19+
}

0 commit comments

Comments
 (0)