Skip to content

Commit 3738d62

Browse files
committed
introduce Metrics.addProperty and use it instead of addMetadata
1 parent a97906c commit 3738d62

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/Metrics.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ default void addDimension(String key, String value) {
9393
*/
9494
void addMetadata(String key, Object value);
9595

96+
/**
97+
* Add property
98+
*
99+
* @param key the name of the property
100+
* @param value the value of the property
101+
*/
102+
void addProperty(String key, Object value);
103+
96104
/**
97105
* Set default dimensions
98106
*

powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/internal/EmfMetricsLogger.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class EmfMetricsLogger implements Metrics {
5454
private final AtomicBoolean raiseOnEmptyMetrics = new AtomicBoolean(false);
5555
private String namespace;
5656
private Map<String, String> defaultDimensions = new HashMap<>();
57-
private final Map<String, Object> metadata = new HashMap<>();
57+
private final Map<String, Object> properties = new HashMap<>();
5858
private final AtomicBoolean hasMetrics = new AtomicBoolean(false);
5959

6060
public EmfMetricsLogger(EnvironmentProvider environmentProvider, MetricsContext metricsContext) {
@@ -92,7 +92,12 @@ public void addDimension(software.amazon.lambda.powertools.metrics.model.Dimensi
9292
@Override
9393
public void addMetadata(String key, Object value) {
9494
emfLogger.putMetadata(key, value);
95-
metadata.put(key, value);
95+
}
96+
97+
@Override
98+
public void addProperty(String key, Object value) {
99+
emfLogger.putProperty(key, value);
100+
properties.put(key, value);
96101
}
97102

98103
@Override
@@ -235,7 +240,7 @@ public void flushMetrics(Consumer<Metrics> metricsConsumer) {
235240
if (!defaultDimensions.isEmpty()) {
236241
metrics.setDefaultDimensions(software.amazon.lambda.powertools.metrics.model.DimensionSet.of(defaultDimensions));
237242
}
238-
metadata.forEach(metrics::addMetadata);
243+
properties.forEach(metrics::addProperty);
239244

240245
metricsConsumer.accept(metrics);
241246

powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/internal/LambdaMetricsAspect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public Object around(ProceedingJoinPoint pjp,
9292

9393
// Add trace ID metadata if available
9494
LambdaHandlerProcessor.getXrayTraceId()
95-
.ifPresent(traceId -> metricsInstance.addMetadata(TRACE_ID_PROPERTY, traceId));
95+
.ifPresent(traceId -> metricsInstance.addProperty(TRACE_ID_PROPERTY, traceId));
9696

9797
captureColdStartMetricIfEnabled(extractContext(pjp), metrics);
9898

@@ -115,7 +115,7 @@ private void captureColdStartMetricIfEnabled(Context extractedContext, FlushMetr
115115
Metrics metricsInstance = MetricsFactory.getMetricsInstance();
116116
// This can be null e.g. during unit tests when mocking the Lambda context
117117
if (extractedContext.getAwsRequestId() != null) {
118-
metricsInstance.addMetadata(REQUEST_ID_PROPERTY, extractedContext.getAwsRequestId());
118+
metricsInstance.addProperty(REQUEST_ID_PROPERTY, extractedContext.getAwsRequestId());
119119
}
120120

121121
// Only capture cold start metrics if enabled on annotation

powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/internal/EmfMetricsLoggerTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ void shouldFlushMetrics() throws Exception {
453453
metrics.setDefaultDimensions(DimensionSet.of("CustomDim", "CustomValue"));
454454
metrics.addDimension(DimensionSet.of("CustomDim2", "CustomValue2"));
455455
metrics.addMetadata("CustomMetadata", "MetadataValue");
456+
metrics.addProperty("CustomProperty", "PropertyValue");
456457

457458
// When
458459
metrics.flushMetrics(m -> {
@@ -473,8 +474,9 @@ void shouldFlushMetrics() throws Exception {
473474
assertThat(rootNode.get("CustomDim2")).isNull();
474475
assertThat(rootNode.get("_aws").get("CloudWatchMetrics").get(0).get("Namespace").asText())
475476
.isEqualTo("MainNamespace");
476-
assertThat(rootNode.get("_aws").has("CustomMetadata")).isTrue();
477-
assertThat(rootNode.get("_aws").get("CustomMetadata").asText()).isEqualTo("MetadataValue");
477+
assertThat(rootNode.get("_aws").has("CustomMetadata")).isFalse();
478+
assertThat(rootNode.has("CustomProperty")).isTrue();
479+
assertThat(rootNode.get("CustomProperty").asText()).isEqualTo("PropertyValue");
478480
}
479481

480482
@Test

powertools-metrics/src/test/java/software/amazon/lambda/powertools/metrics/testutils/TestMetrics.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public void addMetadata(String key, Object value) {
3737
// Test placeholder
3838
}
3939

40+
@Override
41+
public void addProperty(String key, Object value) {
42+
// Test placeholder
43+
}
44+
4045
@Override
4146
public void setDefaultDimensions(DimensionSet dimensionSet) {
4247
// Test placeholder

0 commit comments

Comments
 (0)