Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static void withSingleMetric(final String name,
final double value,
final Unit unit,
final Consumer<MetricsLogger> logger) {
withMetricLogger(metricsLogger -> {
withMetricsLogger(metricsLogger -> {
metricsLogger.putMetric(name, value, unit);
logger.accept(metricsLogger);
});
Expand All @@ -103,7 +103,7 @@ public static void withSingleMetric(final String name,
final Unit unit,
final String namespace,
final Consumer<MetricsLogger> logger) {
withMetricLogger(metricsLogger -> {
withMetricsLogger(metricsLogger -> {
metricsLogger.setNamespace(namespace);
metricsLogger.putMetric(name, value, unit);
logger.accept(metricsLogger);
Expand All @@ -118,7 +118,7 @@ public static void withSingleMetric(final String name,
*
* @param logger the MetricsLogger
*/
public static void withMetricLogger(final Consumer<MetricsLogger> logger) {
public static void withMetricsLogger(final Consumer<MetricsLogger> logger) {
MetricsLogger metricsLogger = logger();

try {
Expand All @@ -130,6 +130,21 @@ public static void withMetricLogger(final Consumer<MetricsLogger> logger) {
}
}

/**
* Provide and immediately flush a {@link MetricsLogger}. It uses the default namespace
* specified either on {@link Metrics} annotation or via POWERTOOLS_METRICS_NAMESPACE env var.
* It by default captures function_request_id as property if used together with {@link Metrics} annotation. It will also
* capture xray_trace_id as property if tracing is enabled.
*
* @param logger the MetricsLogger
*
* @deprecated use {@link MetricsUtils#withMetricsLogger} instead
*/
@Deprecated
public static void withMetricLogger(final Consumer<MetricsLogger> logger) {
withMetricsLogger(logger);
}

public static DimensionSet[] getDefaultDimensions() {
return Arrays.copyOf(defaultDimensions, defaultDimensions.length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Map;
import java.util.function.Consumer;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -12,6 +13,7 @@
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import software.amazon.cloudwatchlogs.emf.config.SystemWrapper;
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
import software.amazon.cloudwatchlogs.emf.model.Unit;

Expand Down Expand Up @@ -124,13 +126,29 @@ void singleMetricsCaptureUtilityWithDefaultNameSpace() {

@Test
void metricsLoggerCaptureUtilityWithDefaultNameSpace() {
testWithNewLogger(MetricsUtils::withMetricsLogger);
}

@Test
void deprecatedMetricLoggerCaptureUtilityWithDefaultNameSpace() {
testWithNewLogger(MetricsUtils::withMetricLogger);
}

@Test
void shouldThrowExceptionWhenDefaultDimensionIsNull() {
assertThatNullPointerException()
.isThrownBy(() -> MetricsUtils.defaultDimensionSet(null))
.withMessage("Null dimension set not allowed");
}

private void testWithNewLogger(Consumer<Consumer<MetricsLogger>> methodToTest) {
Copy link
Contributor

Choose a reason for hiding this comment

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

strange to name it NewLogger when you use it with the deprecated one...

Suggested change
private void testWithNewLogger(Consumer<Consumer<MetricsLogger>> methodToTest) {
private void testLogger(Consumer<Consumer<MetricsLogger>> loggerToTest) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

New was as opposed to the default MetricsUtils.metricsLogger() and MetricsUtils.withMetric(s)Logger essentially providing new ones. However, happy to call it testLogger instead to avoid any confusion

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");
mocked.when(() -> SystemWrapper.getenv("POWERTOOLS_METRICS_NAMESPACE")).thenReturn("GlobalName");
internalWrapper.when(() -> getenv("_X_AMZN_TRACE_ID")).thenReturn("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\"");

MetricsUtils.withMetricLogger(metricsLogger -> {
methodToTest.accept(metricsLogger -> {
metricsLogger.setDimensions(DimensionSet.of("Dimension1", "Value1"));
metricsLogger.putMetric("Metric1", 1, Unit.COUNT);
});
Expand All @@ -154,13 +172,6 @@ void metricsLoggerCaptureUtilityWithDefaultNameSpace() {
}
}

@Test
void shouldThrowExceptionWhenDefaultDimensionIsNull() {
assertThatNullPointerException()
.isThrownBy(() -> MetricsUtils.defaultDimensionSet(null))
.withMessage("Null dimension set not allowed");
}

private Map<String, Object> readAsJson(String s) {
try {
return mapper.readValue(s, Map.class);
Expand Down