Skip to content

Commit

Permalink
[exporter/awsemfexporter] Always set Timestamp & Version for EMF v0 (#10
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sky333999 committed May 8, 2023
1 parent 354aa71 commit 9defe6b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 6 additions & 3 deletions exporter/awsemfexporter/metric_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ func translateCWMetricToEMF(cWMetric *cWMetrics, config *Config) *cwlogs.Event {
}
}

// For backwards compatibility, if EMF v0, always include version & timestamp (even for non-EMF events)
if config.Version == "0" {
fieldMap["Version"] = "0"
fieldMap["Timestamp"] = fmt.Sprint(cWMetric.timestampMs)
}

// Create EMF metrics if there are measurements
// https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Specification.html#CloudWatch_Embedded_Metric_Format_Specification_structure
if len(cWMetric.measurements) > 0 {
Expand Down Expand Up @@ -405,11 +411,8 @@ func translateCWMetricToEMF(cWMetric *cWMetrics, config *Config) *cwlogs.Event {
"Timestamp": "1668387032641"
}
*/
fieldMap["Version"] = "0"
fieldMap["Timestamp"] = fmt.Sprint(cWMetric.timestampMs)
fieldMap["CloudWatchMetrics"] = cWMetric.measurements
}

}

pleMsg, err := json.Marshal(fieldMap)
Expand Down
5 changes: 5 additions & 0 deletions exporter/awsemfexporter/metric_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,11 @@ func TestTranslateCWMetricToEMF(t *testing.T) {
measurements: nil,
expectedEMFLogEvent: "{\"OTelLib\":\"cloudwatch-otel\",\"Sources\":[\"cadvisor\",\"pod\",\"calculated\"],\"kubernetes\":{\"container_name\":\"cloudwatch-agent\",\"docker\":{\"container_id\":\"fc1b0a4c3faaa1808e187486a3a90cbea883dccaf2e2c46d4069d663b032a1ca\"},\"host\":\"ip-192-168-58-245.ec2.internal\",\"labels\":{\"controller-revision-hash\":\"5bdbf497dc\",\"name\":\"cloudwatch-agent\",\"pod-template-generation\":\"1\"},\"namespace_name\":\"amazon-cloudwatch\",\"pod_id\":\"e23f3413-af2e-4a98-89e0-5df2251e7f05\",\"pod_name\":\"cloudwatch-agent-26bl6\",\"pod_owners\":[{\"owner_kind\":\"DaemonSet\",\"owner_name\":\"cloudwatch-agent\"}]},\"spanCounter\":0,\"spanName\":\"test\"}",
},
"WithNoMeasurementAndEMFV0": {
emfVersion: "0",
measurements: nil,
expectedEMFLogEvent: "{\"OTelLib\":\"cloudwatch-otel\",\"Sources\":[\"cadvisor\",\"pod\",\"calculated\"],\"Timestamp\":\"1596151098037\",\"Version\":\"0\",\"kubernetes\":{\"container_name\":\"cloudwatch-agent\",\"docker\":{\"container_id\":\"fc1b0a4c3faaa1808e187486a3a90cbea883dccaf2e2c46d4069d663b032a1ca\"},\"host\":\"ip-192-168-58-245.ec2.internal\",\"labels\":{\"controller-revision-hash\":\"5bdbf497dc\",\"name\":\"cloudwatch-agent\",\"pod-template-generation\":\"1\"},\"namespace_name\":\"amazon-cloudwatch\",\"pod_id\":\"e23f3413-af2e-4a98-89e0-5df2251e7f05\",\"pod_name\":\"cloudwatch-agent-26bl6\",\"pod_owners\":[{\"owner_kind\":\"DaemonSet\",\"owner_name\":\"cloudwatch-agent\"}]},\"spanCounter\":0,\"spanName\":\"test\"}",
},
}

for name, tc := range testCases {
Expand Down

0 comments on commit 9defe6b

Please sign in to comment.