Skip to content

Commit

Permalink
Merge pull request #7 from mtagstyle/master
Browse files Browse the repository at this point in the history
Add the option to set log group as per https://docs.aws.amazon.com/Am…
  • Loading branch information
prozz authored Sep 30, 2021
2 parents c558611 + 5e4df5a commit 7ab9c16
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
5 changes: 3 additions & 2 deletions emf/emf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package emf

// Metadata struct as defined in AWS Embedded Metrics Format spec.
type Metadata struct {
Timestamp int64 `json:"Timestamp"`
Metrics []MetricDirective `json:"CloudWatchMetrics"`
Timestamp int64 `json:"Timestamp"`
Metrics []MetricDirective `json:"CloudWatchMetrics"`
LogGroupName string `json:"LogGroupName,omitempty"`
}

// MetricDirective struct as defined in AWS Embedded Metrics Format spec.
Expand Down
13 changes: 11 additions & 2 deletions emf/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Logger struct {
contexts []*Context
values map[string]interface{}
withoutDimensions bool
logGroupName string
}

// Context gives ability to add another MetricDirective section for Logger.
Expand Down Expand Up @@ -49,6 +50,13 @@ func WithoutDimensions() LoggerOption {
}
}

// WithLogGroup sets the log group when ingesting metrics into Cloudwatch Logging Agent.
func WithLogGroup(logGroup string) LoggerOption {
return func(l *Logger) {
l.logGroupName = logGroup
}
}

// New creates logger with reasonable defaults for Lambda functions:
// - Prints to os.Stdout.
// - Context based on Lambda environment variables.
Expand Down Expand Up @@ -202,8 +210,9 @@ func (l *Logger) Log() {
}

l.values["_aws"] = Metadata{
Timestamp: l.timestamp,
Metrics: metrics,
Timestamp: l.timestamp,
Metrics: metrics,
LogGroupName: l.logGroupName,
}
buf, _ := json.Marshal(l.values)
_, _ = fmt.Fprintln(l.out, string(buf))
Expand Down
10 changes: 10 additions & 0 deletions emf/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ func TestEmf(t *testing.T) {
},
expected: "testdata/17.json",
},
{
name: "with log group",
new: func(buf *bytes.Buffer) *emf.Logger {
return emf.New(emf.WithLogGroup("test_log_group"), emf.WithWriter(buf))
},
given: func(logger *emf.Logger) {
logger.Metric("foo", 33)
},
expected: "testdata/18.json",
},
}

for _, tc := range tcs {
Expand Down
19 changes: 19 additions & 0 deletions emf/testdata/18.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"_aws": {
"Timestamp": "<<PRESENCE>>",
"LogGroupName": "test_log_group",
"CloudWatchMetrics": [
{
"Metrics": [
{
"Name": "foo",
"Unit": "None"
}
],
"Namespace": "aws-embedded-metrics",
"Dimensions": null
}
]
},
"foo": 33
}

0 comments on commit 7ab9c16

Please sign in to comment.