File tree Expand file tree Collapse file tree 4 files changed +43
-4
lines changed Expand file tree Collapse file tree 4 files changed +43
-4
lines changed Original file line number Diff line number Diff line change @@ -3,8 +3,9 @@ package emf
33
44// Metadata struct as defined in AWS Embedded Metrics Format spec.
55type 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.
Original file line number Diff line number Diff 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 ))
Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments