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

EmfMetricPublisher class added #5752

Open
wants to merge 19 commits into
base: feature/master/emf-metric-publisher
Choose a base branch
from

Conversation

Fred1155
Copy link
Contributor

@Fred1155 Fred1155 commented Dec 13, 2024

Motivation and Context

The existing CloudWatchMetricPublisher has limitations when used with AWS Lambda functions. When current implementation be applied to lambda functions, it can results in metrics being lost when the lambda function terminates before the scheduled batch upload. EMF allows metrics to be published through CloudWatch Logs using a structured JSON format, which CloudWatch automatically processes to extract and publish metrics. The proposed EMFMetricPublisher will convert SDK metric collections into EMF-formatted log entries, leveraging Lambda's as well as other execution environment that has built-in integration with cloudwatch logs such as ECS.

Modifications

Add a new metric publisher that transforms an metricCollection to a list of emf format Strings and publish it to CouldWatch using cloudwatch agents

Testing

  1. Unit test covering different cases of metricCollection in convertMetricCollectionToEMF method
  2. End to End test (In process)

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • I have added a changelog entry. Adding a new entry must be accomplished by running the scripts/new-change script and following the instructions. Commit the new file created by the script in .changes/next-release with your changes.
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • I confirm that this pull request can be released under the Apache 2 license

@Fred1155 Fred1155 requested a review from a team as a code owner December 13, 2024 23:01
Copy link
Contributor

@zoewangg zoewangg left a comment

Choose a reason for hiding this comment

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

Left some high level comments, still reviewing the implementation

import software.amazon.awssdk.utils.Logger;

/**
* A metric publisher implementation that converts metrics into CloudWatch Embedded Metric Format (EMF).
Copy link
Contributor

Choose a reason for hiding this comment

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

*
* <p>Example usage:</p>
* <pre>
* EmfMetricPublisher publisher = PublisherBuilder.dimensions(CoreMetric.SERVICE_ID)
Copy link
Contributor

Choose a reason for hiding this comment

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

*
* <p>If this is not specified, {@code false} will be used.
*/
public Builder unitTest(boolean unitTest) {
Copy link
Contributor

Choose a reason for hiding this comment

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

}

@Test
void testConvertMetricCollectionToEMF_EmptyCollection(){
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using parameterized tests if we are just testing with different input

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
2 Security Hotspots
3.5% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

metric-publishers/emf-metric-publisher/pom.xml Outdated Show resolved Hide resolved
// Write CloudWatchMetrics array
jsonWriter.writeFieldName("CloudWatchMetrics");
jsonWriter.writeStartArray();
jsonWriter.writeStartObject();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this correct? It seems this CloudWatchMetrics only has one object

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so. In the example https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Specification.html, CloudWatchMetrics should have one object, because we only have one namespace and one set of dimensions

<version>2.29.35-SNAPSHOT</version>
</parent>

<artifactId>emf-metric-publisher</artifactId>
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't have to change it now, but we should discuss it in API surface area review. Should we rename it to emf-metric-logger, since technically it's not publishing metrics?



private EmfMetricPublisher(Builder builder) {
EmfMetricConfiguration config = new EmfMetricConfiguration(
Copy link
Contributor

Choose a reason for hiding this comment

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

Per our previous discussion, I thought we are going to get rid of EmfMetricConfiguration class and just initialize fields in this class.

    private EmfMetricPublisher(Builder builder) {
        this.namespace = builder.namespace == null ? DEFAULT_NAMESPACE : builder.namespace;

         ...
        this.dimensionStrings = resolveDimensionStrings(builder);
    }

* Configure the LogGroupName key that will be put into the emf log to this publisher. This is a required key for
* using the CloudWatch agent to send embedded metric format logs that tells the agent which log group to use.
*
* <p>If this is not specified, {@code /aws/emf/metrics} will be used.
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious, why is /aws/emf/metrics the default log group?

I can't find LogGroup in the specification, could you point to me where it's defined? https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Specification.html

import software.amazon.awssdk.testutils.LogCaptor;


public class EmfMetricPublisherTest extends LogCaptor.LogCaptorTestBase{
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a test case where metricCollector throws an exception?

import software.amazon.awssdk.metrics.MetricCollector;
import software.amazon.awssdk.metrics.MetricLevel;

public class MetricEmfConverterTest {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a test case where metric records size is larger than 100?

metricCollector.reportMetric(HttpMetric.HTTP_STATUS_CODE, 404);
List<String> emfLogs = metricEmfConverterDefault.convertMetricCollectionToEmf(metricCollector.collect());

for (String emfLog : emfLogs) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If we only expect one log statement, let's just assert that instead of for loop. Same for other test cases.

assertThat(emfLogs).containsOnly(...)


private MetricEmfConverter metricEmfConverterDefault;
private MetricEmfConverter metricEmfConverterCustom;
private final EmfMetricConfiguration testConfigDefault = new EmfMetricConfiguration("AwsSdk/Test/JavaSdk2",
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this the default config though?

}

@Test
void ConvertMetricCollectionToEMF_metricLevel(){
Copy link
Contributor

Choose a reason for hiding this comment

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

How is this test different from ConvertMetricCollectionToEMF_metricCategory?

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

Successfully merging this pull request may close these issues.

2 participants