File tree 4 files changed +43
-4
lines changed
4 files changed +43
-4
lines changed Original file line number Diff line number Diff line change @@ -3,8 +3,9 @@ package emf
3
3
4
4
// Metadata struct as defined in AWS Embedded Metrics Format spec.
5
5
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"`
8
9
}
9
10
10
11
// 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 {
17
17
contexts []* Context
18
18
values map [string ]interface {}
19
19
withoutDimensions bool
20
+ logGroupName string
20
21
}
21
22
22
23
// Context gives ability to add another MetricDirective section for Logger.
@@ -49,6 +50,13 @@ func WithoutDimensions() LoggerOption {
49
50
}
50
51
}
51
52
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
+
52
60
// New creates logger with reasonable defaults for Lambda functions:
53
61
// - Prints to os.Stdout.
54
62
// - Context based on Lambda environment variables.
@@ -202,8 +210,9 @@ func (l *Logger) Log() {
202
210
}
203
211
204
212
l .values ["_aws" ] = Metadata {
205
- Timestamp : l .timestamp ,
206
- Metrics : metrics ,
213
+ Timestamp : l .timestamp ,
214
+ Metrics : metrics ,
215
+ LogGroupName : l .logGroupName ,
207
216
}
208
217
buf , _ := json .Marshal (l .values )
209
218
_ , _ = 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) {
189
189
},
190
190
expected : "testdata/17.json" ,
191
191
},
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
+ },
192
202
}
193
203
194
204
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