Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[extension/encoding/awscloudwatchmetricstreams_encoding] Support unmarshalling JSON-formatted CloudWatch Metric Streams #38419

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

constanca-m
Copy link

@constanca-m constanca-m commented Mar 6, 2025

Description

This PR implements the translation of JSON-formatted CloudWatch Metric Streams into OpenTelemetry metrics.

It receives a record, and splits that record by each new line. Each of these parts will correspond to a JSON CloudWatch Metric Stream. It then translates this metric into an OpenTelemetry metric. The obtained metrics from the record are returned.

It follows the same logic as https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/awsfirehosereceiver/internal/unmarshaler/cwmetricstream.

Link to tracking issue

Fixes #38407.

Testing

I have added unit tests to:

  • Check we can extract open telemetry metrics from JSON valid cloudwatch metric stream
  • Check there are no metrics extracted if the data does not have expected format (for example, no unit or no value)
  • Check we can obtain metrics from a record containing both valid and invalid cloudwatch metric stream.

You can easily read and study what we get to what we return in the testdata/json files of the extension.

Documentation

The coding comments and the README file should be enough for documentation.

Copy link
Contributor

@axw axw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @constanca-m! Looking good, main concerns are to do with how the tests are implemented.

Comment on lines 83 to 87
content, err := os.ReadFile(filesDirectory + "/" + test.metricExpectedFilename)
require.NoError(t, err)

expected, err := unmarshallerJSONMetric.UnmarshalMetrics(content)
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/pkg/golden here? In which case the expected metrics would need to be encoded as YAML.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have used json.Compact and changed to YAML for the expected file. Thanks for the suggestions! I think it looks better now.

Copy link
Contributor

@axw axw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks!

Comment on lines +89 to +92
// joinMetricsFromFile reads the metrics inside the files,
// and joins them in the format a record expects it to be:
// each metric is expected to be in 1 line, and every new
// line marks a new metric
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I like this approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support unmarshalling JSON-formatted CloudWatch Metric Streams
3 participants