-
Notifications
You must be signed in to change notification settings - Fork 90
Mertics default dimensions #329
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
Changes from all commits
6164c3f
9f76dc8
7899590
3670461
3144d03
02e43bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package software.amazon.lambda.powertools.metrics.handlers; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger; | ||
import software.amazon.cloudwatchlogs.emf.model.Unit; | ||
import software.amazon.lambda.powertools.metrics.Metrics; | ||
import software.amazon.lambda.powertools.metrics.MetricsUtils; | ||
|
||
import static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger; | ||
import static software.amazon.lambda.powertools.metrics.MetricsUtils.withSingleMetric; | ||
|
||
public class PowertoolsMetricsEnabledDefaultNoDimensionHandler implements RequestHandler<Object, Object> { | ||
|
||
static { | ||
MetricsUtils.defaultDimensions(); | ||
} | ||
|
||
@Override | ||
@Metrics(namespace = "ExampleApplication", service = "booking") | ||
public Object handleRequest(Object input, Context context) { | ||
MetricsLogger metricsLogger = metricsLogger(); | ||
metricsLogger.putMetric("Metric1", 1, Unit.BYTES); | ||
|
||
withSingleMetric("Metric2", 1, Unit.COUNT, log -> {}); | ||
|
||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
import software.amazon.lambda.powertools.metrics.ValidationException; | ||
import software.amazon.lambda.powertools.metrics.handlers.PowertoolsMetricsColdStartEnabledHandler; | ||
import software.amazon.lambda.powertools.metrics.handlers.PowertoolsMetricsEnabledDefaultDimensionHandler; | ||
import software.amazon.lambda.powertools.metrics.handlers.PowertoolsMetricsEnabledDefaultNoDimensionHandler; | ||
import software.amazon.lambda.powertools.metrics.handlers.PowertoolsMetricsEnabledHandler; | ||
import software.amazon.lambda.powertools.metrics.handlers.PowertoolsMetricsEnabledStreamHandler; | ||
import software.amazon.lambda.powertools.metrics.handlers.PowertoolsMetricsExceptionWhenNoMetricsHandler; | ||
|
@@ -49,7 +50,6 @@ public class LambdaMetricsAspectTest { | |
private final PrintStream originalOut = System.out; | ||
private final ObjectMapper mapper = new ObjectMapper(); | ||
private RequestHandler<Object, Object> requestHandler; | ||
private RequestStreamHandler streamHandler; | ||
|
||
|
||
@BeforeAll | ||
|
@@ -80,7 +80,7 @@ public void metricsWithoutColdStart() { | |
mocked.when(() -> SystemWrapper.getenv("AWS_EMF_ENVIRONMENT")).thenReturn("Lambda"); | ||
internalWrapper.when(() -> getenv("_X_AMZN_TRACE_ID")).thenReturn("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\""); | ||
|
||
MetricsUtils.defaultDimensionSet(new DimensionSet()); | ||
MetricsUtils.defaultDimensions(null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not quite sure I follow what setting it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its more of a limitation of how MockedStatic works and test specific. Ux for setting no dimension is now added as part of a test as well https://github.com/awslabs/aws-lambda-powertools-java/blob/02e43bf22fbdea163ae5663049d447d3c8c65f52/powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/handlers/PowertoolsMetricsEnabledDefaultNoDimensionHandler.java#L16 |
||
requestHandler = new PowertoolsMetricsEnabledHandler(); | ||
requestHandler.handleRequest("input", context); | ||
|
||
|
@@ -154,14 +154,53 @@ public void metricsWithDefaultDimensionSpecified() { | |
} | ||
} | ||
|
||
@Test | ||
public void metricsWithDefaultNoDimensionSpecified() { | ||
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class); | ||
MockedStatic<software.amazon.lambda.powertools.core.internal.SystemWrapper> internalWrapper = mockStatic(software.amazon.lambda.powertools.core.internal.SystemWrapper.class)) { | ||
|
||
mocked.when(() -> SystemWrapper.getenv("AWS_EMF_ENVIRONMENT")).thenReturn("Lambda"); | ||
internalWrapper.when(() -> getenv("_X_AMZN_TRACE_ID")).thenReturn("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\""); | ||
|
||
requestHandler = new PowertoolsMetricsEnabledDefaultNoDimensionHandler(); | ||
|
||
requestHandler.handleRequest("input", context); | ||
|
||
assertThat(out.toString().split("\n")) | ||
.hasSize(2) | ||
.satisfies(s -> { | ||
Map<String, Object> logAsJson = readAsJson(s[0]); | ||
|
||
assertThat(logAsJson) | ||
.containsEntry("Metric2", 1.0) | ||
.containsKey("_aws") | ||
.containsEntry("xray_trace_id", "1-5759e988-bd862e3fe1be46a994272793") | ||
.containsEntry("function_request_id", "123ABC"); | ||
|
||
Map<String, Object> aws = (Map<String, Object>) logAsJson.get("_aws"); | ||
|
||
assertThat(aws.get("CloudWatchMetrics")) | ||
.asString() | ||
.contains("Namespace=ExampleApplication"); | ||
|
||
logAsJson = readAsJson(s[1]); | ||
|
||
assertThat(logAsJson) | ||
.containsEntry("Metric1", 1.0) | ||
.containsEntry("function_request_id", "123ABC") | ||
.containsKey("_aws"); | ||
}); | ||
} | ||
} | ||
|
||
@Test | ||
public void metricsWithColdStart() { | ||
|
||
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) { | ||
|
||
mocked.when(() -> SystemWrapper.getenv("AWS_EMF_ENVIRONMENT")).thenReturn("Lambda"); | ||
|
||
MetricsUtils.defaultDimensionSet(new DimensionSet()); | ||
MetricsUtils.defaultDimensions(null); | ||
requestHandler = new PowertoolsMetricsColdStartEnabledHandler(); | ||
|
||
requestHandler.handleRequest("input", context); | ||
|
@@ -196,7 +235,7 @@ public void noColdStartMetricsWhenColdStartDone() { | |
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) { | ||
mocked.when(() -> SystemWrapper.getenv("AWS_EMF_ENVIRONMENT")).thenReturn("Lambda"); | ||
|
||
MetricsUtils.defaultDimensionSet(new DimensionSet()); | ||
MetricsUtils.defaultDimensions(null); | ||
requestHandler = new PowertoolsMetricsColdStartEnabledHandler(); | ||
|
||
requestHandler.handleRequest("input", context); | ||
|
@@ -241,8 +280,8 @@ public void metricsWithStreamHandler() throws IOException { | |
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) { | ||
mocked.when(() -> SystemWrapper.getenv("AWS_EMF_ENVIRONMENT")).thenReturn("Lambda"); | ||
|
||
MetricsUtils.defaultDimensionSet(new DimensionSet()); | ||
streamHandler = new PowertoolsMetricsEnabledStreamHandler(); | ||
MetricsUtils.defaultDimensions(null); | ||
RequestStreamHandler streamHandler = new PowertoolsMetricsEnabledStreamHandler(); | ||
|
||
streamHandler.handleRequest(new ByteArrayInputStream(new byte[]{}), new ByteArrayOutputStream(), context); | ||
|
||
|
@@ -264,7 +303,7 @@ public void exceptionWhenNoMetricsEmitted() { | |
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) { | ||
mocked.when(() -> SystemWrapper.getenv("AWS_EMF_ENVIRONMENT")).thenReturn("Lambda"); | ||
|
||
MetricsUtils.defaultDimensionSet(new DimensionSet()); | ||
MetricsUtils.defaultDimensions(null); | ||
requestHandler = new PowertoolsMetricsExceptionWhenNoMetricsHandler(); | ||
|
||
assertThatExceptionOfType(ValidationException.class) | ||
|
@@ -279,7 +318,7 @@ public void noExceptionWhenNoMetricsEmitted() { | |
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) { | ||
mocked.when(() -> SystemWrapper.getenv("AWS_EMF_ENVIRONMENT")).thenReturn("Lambda"); | ||
|
||
MetricsUtils.defaultDimensionSet(new DimensionSet()); | ||
MetricsUtils.defaultDimensions(null); | ||
requestHandler = new PowertoolsMetricsNoExceptionWhenNoMetricsHandler(); | ||
|
||
requestHandler.handleRequest("input", context); | ||
|
@@ -299,7 +338,7 @@ public void noExceptionWhenNoMetricsEmitted() { | |
public void allowWhenNoDimensionsSet() { | ||
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) { | ||
mocked.when(() -> SystemWrapper.getenv("AWS_EMF_ENVIRONMENT")).thenReturn("Lambda"); | ||
MetricsUtils.defaultDimensionSet(new DimensionSet()); | ||
MetricsUtils.defaultDimensions(null); | ||
|
||
requestHandler = new PowertoolsMetricsNoDimensionsHandler(); | ||
requestHandler.handleRequest("input", context); | ||
|
@@ -321,7 +360,7 @@ public void exceptionWhenTooManyDimensionsSet() { | |
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) { | ||
mocked.when(() -> SystemWrapper.getenv("AWS_EMF_ENVIRONMENT")).thenReturn("Lambda"); | ||
|
||
MetricsUtils.defaultDimensionSet(new DimensionSet()); | ||
MetricsUtils.defaultDimensions(null); | ||
|
||
requestHandler = new PowertoolsMetricsTooManyDimensionsHandler(); | ||
|
||
|
@@ -336,7 +375,7 @@ public void metricsPublishedEvenHandlerThrowsException() { | |
try (MockedStatic<SystemWrapper> mocked = mockStatic(SystemWrapper.class)) { | ||
mocked.when(() -> SystemWrapper.getenv("AWS_EMF_ENVIRONMENT")).thenReturn("Lambda"); | ||
|
||
MetricsUtils.defaultDimensionSet(new DimensionSet()); | ||
MetricsUtils.defaultDimensions(null); | ||
requestHandler = new PowertoolsMetricsWithExceptionInHandler(); | ||
|
||
assertThatExceptionOfType(IllegalStateException.class) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is this for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a failing build on docs and some research suggested to use it coz of some git behaviour changes