Skip to content

Commit f1f3e70

Browse files
committed
use flushMetrics in captureColdStartMetric
1 parent 3738d62 commit f1f3e70

File tree

2 files changed

+21
-50
lines changed

2 files changed

+21
-50
lines changed

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

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -179,45 +179,16 @@ public void flush() {
179179
public void captureColdStartMetric(Context context,
180180
software.amazon.lambda.powertools.metrics.model.DimensionSet dimensions) {
181181
if (isColdStart()) {
182-
if (isMetricsDisabled()) {
183-
LOGGER.debug("Metrics are disabled, skipping cold start metric capture");
184-
return;
185-
}
186-
187-
Validator.validateNamespace(namespace);
188-
189-
software.amazon.cloudwatchlogs.emf.logger.MetricsLogger coldStartLogger = new software.amazon.cloudwatchlogs.emf.logger.MetricsLogger();
190-
191-
try {
192-
coldStartLogger.setNamespace(namespace);
193-
} catch (Exception e) {
194-
LOGGER.error("Namespace cannot be set for cold start metrics due to an error in EMF", e);
195-
}
196-
197-
coldStartLogger.putMetric(COLD_START_METRIC, 1, Unit.COUNT);
198-
199-
// Set dimensions if provided
200-
if (dimensions != null) {
201-
DimensionSet emfDimensionSet = new DimensionSet();
202-
dimensions.getDimensions().forEach((key, val) -> {
203-
try {
204-
emfDimensionSet.addDimension(key, val);
205-
} catch (Exception e) {
206-
// Ignore dimension errors
207-
}
208-
});
209-
coldStartLogger.setDimensions(emfDimensionSet);
210-
}
211-
212-
// Add request ID from context if available
213-
if (context != null && context.getAwsRequestId() != null) {
214-
coldStartLogger.putProperty(REQUEST_ID_PROPERTY, context.getAwsRequestId());
215-
}
216-
217-
// Add trace ID using the standard logic
218-
getXrayTraceId().ifPresent(traceId -> coldStartLogger.putProperty(TRACE_ID_PROPERTY, traceId));
219-
220-
coldStartLogger.flush();
182+
flushMetrics(metrics -> {
183+
if (context != null && context.getAwsRequestId() != null) {
184+
metrics.addProperty(REQUEST_ID_PROPERTY, context.getAwsRequestId());
185+
}
186+
getXrayTraceId().ifPresent(traceId -> metrics.addProperty(TRACE_ID_PROPERTY, traceId));
187+
if (dimensions != null) {
188+
metrics.setDefaultDimensions(dimensions);
189+
}
190+
metrics.addMetric(COLD_START_METRIC, 1, MetricUnit.COUNT);
191+
});
221192
}
222193
}
223194

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,40 +107,40 @@ public Object around(ProceedingJoinPoint pjp,
107107
return pjp.proceed(proceedArgs);
108108
}
109109

110-
private void captureColdStartMetricIfEnabled(Context extractedContext, FlushMetrics metrics) {
110+
private void captureColdStartMetricIfEnabled(Context extractedContext, FlushMetrics flushMetrics) {
111111
if (extractedContext == null) {
112112
return;
113113
}
114114

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

121121
// Only capture cold start metrics if enabled on annotation
122-
if (metrics.captureColdStart()) {
122+
if (flushMetrics.captureColdStart()) {
123123
// Get function name from annotation or context
124-
String funcName = functionName(metrics, extractedContext);
124+
String funcName = functionName(flushMetrics, extractedContext);
125125

126-
DimensionSet coldStartDimensions = new DimensionSet();
126+
DimensionSet dimensionSet = new DimensionSet();
127127

128128
// Get service name from metrics instance default dimensions or fallback
129-
String serviceName = metricsInstance.getDefaultDimensions().getDimensions().getOrDefault(
129+
String serviceName = metrics.getDefaultDimensions().getDimensions().getOrDefault(
130130
SERVICE_DIMENSION,
131-
serviceNameWithFallback(metrics));
131+
serviceNameWithFallback(flushMetrics));
132132

133133
// Only add service if it is not undefined
134134
if (!LambdaConstants.SERVICE_UNDEFINED.equals(serviceName)) {
135-
coldStartDimensions.addDimension(SERVICE_DIMENSION, serviceName);
135+
dimensionSet.addDimension(SERVICE_DIMENSION, serviceName);
136136
}
137137

138138
// Add function name
139139
if (funcName != null) {
140-
coldStartDimensions.addDimension("FunctionName", funcName);
140+
dimensionSet.addDimension("FunctionName", funcName);
141141
}
142142

143-
metricsInstance.captureColdStartMetric(extractedContext, coldStartDimensions);
143+
metrics.captureColdStartMetric(extractedContext, dimensionSet);
144144
}
145145
}
146146
}

0 commit comments

Comments
 (0)