-
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 4 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 |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
*/ | ||
public final class MetricsUtils { | ||
private static final MetricsLogger metricsLogger = new MetricsLogger(); | ||
private static DimensionSet defaultDimensionSet; | ||
private static DimensionSet[] defaultDimensions; | ||
|
||
private MetricsUtils() { | ||
} | ||
|
@@ -39,14 +39,29 @@ public static MetricsLogger metricsLogger() { | |
return metricsLogger; | ||
} | ||
|
||
/** | ||
* Configure default dimension to be used by logger. | ||
* By default, @{@link Metrics} annotation captures configured service as a dimension <i>Service</i> | ||
* @param dimensionSets Default value of dimensions set for logger | ||
*/ | ||
public static void defaultDimensions(final DimensionSet... dimensionSets) { | ||
MetricsUtils.defaultDimensions = dimensionSets; | ||
} | ||
|
||
/** | ||
* Configure default dimension to be used by logger. | ||
* By default, @{@link Metrics} annotation captures configured service as a dimension <i>Service</i> | ||
* @param dimensionSet Default value of dimension set for logger | ||
* @deprecated use {@link #defaultDimensions(DimensionSet...)} instead | ||
* | ||
*/ | ||
@Deprecated | ||
public static void defaultDimensionSet(final DimensionSet dimensionSet) { | ||
requireNonNull(dimensionSet, "Null dimension set not allowed"); | ||
MetricsUtils.defaultDimensionSet = dimensionSet; | ||
|
||
if(dimensionSet.getDimensionKeys().size() > 0) { | ||
MetricsUtils.defaultDimensions = new DimensionSet[]{dimensionSet}; | ||
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 think |
||
} | ||
} | ||
|
||
|
||
|
@@ -105,12 +120,12 @@ public static void withSingleMetric(final String name, | |
} | ||
} | ||
|
||
public static DimensionSet defaultDimensionSet() { | ||
return defaultDimensionSet; | ||
public static DimensionSet[] defaultDimensions() { | ||
return defaultDimensions; | ||
} | ||
|
||
public static boolean hasDefaultDimension() { | ||
return null != defaultDimensionSet && defaultDimensionSet.getDimensionKeys().size() > 0; | ||
return null != defaultDimensions; | ||
} | ||
|
||
private static void captureRequestAndTraceId(MetricsLogger metricsLogger) { | ||
|
@@ -137,7 +152,8 @@ private static MetricsLogger logger() { | |
MetricsContext metricsContext = new MetricsContext(); | ||
|
||
if (hasDefaultDimension()) { | ||
metricsContext.setDefaultDimensions(defaultDimensionSet()); | ||
metricsContext.setDefaultDimensions(new DimensionSet()); | ||
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 think this is not needed as 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. That is true. |
||
metricsContext.setDimensions(defaultDimensions); | ||
} | ||
|
||
return new MetricsLogger(new EnvironmentProvider(), metricsContext); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,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 +79,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); | ||
|
||
|
@@ -161,7 +160,7 @@ public void metricsWithColdStart() { | |
|
||
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 +195,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 +240,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 +263,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((DimensionSet) 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. nit: I see other statement not having to use the type casting, is it really needed here? 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. No, its not . Just a miss |
||
requestHandler = new PowertoolsMetricsExceptionWhenNoMetricsHandler(); | ||
|
||
assertThatExceptionOfType(ValidationException.class) | ||
|
@@ -279,7 +278,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 +298,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 +320,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 +335,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