Skip to content

Commit 05fe15e

Browse files
committed
Add support for POWERTOOLS_METRICS_DISABLED environment variable.
1 parent ba09a50 commit 05fe15e

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed

docs/core/metrics.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,18 @@ Visit the AWS documentation for a complete explanation for [Amazon CloudWatch co
109109

110110
## Getting started
111111

112-
Metrics has two global settings that will be used across all metrics emitted. Use your application or main service as the metric namespace to easily group all metrics:
112+
Metrics has three global settings that will be used across all metrics emitted. Use your application or main service as the metric namespace to easily group all metrics:
113113

114-
| Setting | Description | Environment variable | Decorator parameter |
115-
| -------------------- | ------------------------------------------------------------------------------- | ------------------------------ | ------------------- |
116-
| **Metric namespace** | Logical container where all metrics will be placed e.g. `ServerlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace` |
117-
| **Service** | Optionally, sets **service** metric dimension across all metrics e.g. `payment` | `POWERTOOLS_SERVICE_NAME` | `service` |
114+
| Setting | Description | Environment variable | Decorator parameter |
115+
| ------------------------------ | ------------------------------------------------------------------------------- | ------------------------------ | ------------------- |
116+
| **Metric namespace** | Logical container where all metrics will be placed e.g. `ServerlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace` |
117+
| **Service** | Optionally, sets **service** metric dimension across all metrics e.g. `payment` | `POWERTOOLS_SERVICE_NAME` | `service` |
118+
| **Disable Powertools Metrics** | Optionally, disables all Powertools metrics | `POWERTOOLS_METRICS_DISABLED` | N/A |
118119

119120
!!! tip "Use your application or main service as the metric namespace to easily group all metrics"
120121

122+
!!! info "`POWERTOOLS_METRICS_DISABLED` will not disable default metrics created by AWS services."
123+
121124
### Order of Precedence of `MetricsLogger` configuration
122125

123126
The `MetricsLogger` Singleton can be configured by three different interfaces. The following order of precedence applies:

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ Use the following [dependency matrix](https://github.com/eclipse-aspectj/aspectj
239239
|----------------------------------------|----------------------------------------------------------------------------------------|---------------------------|
240240
| **POWERTOOLS_SERVICE_NAME** | Sets service name used for tracing namespace, metrics dimension and structured logging | All |
241241
| **POWERTOOLS_METRICS_NAMESPACE** | Sets namespace used for metrics | [Metrics](./core/metrics) |
242+
| **POWERTOOLS_METRICS_DISABLED** | Disables all flushing of metrics | [Metrics](./core/metrics) |
242243
| **POWERTOOLS_LOGGER_SAMPLE_RATE** | Debug log sampling | [Logging](./core/logging) |
243244
| **POWERTOOLS_LOG_LEVEL** | Sets logging level | [Logging](./core/logging) |
244245
| **POWERTOOLS_LOGGER_LOG_EVENT** | Enables/Disables whether to log the incoming event when using the aspect | [Logging](./core/logging) |
245246
| **POWERTOOLS_TRACER_CAPTURE_RESPONSE** | Enables/Disables tracing mode to capture method response | [Tracing](./core/tracing) |
246247
| **POWERTOOLS_TRACER_CAPTURE_ERROR** | Enables/Disables tracing mode to capture method error | [Tracing](./core/tracing) |
247-

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class EmfMetricsLogger implements MetricsLogger {
4545
private static final String TRACE_ID_PROPERTY = "xray_trace_id";
4646
private static final String REQUEST_ID_PROPERTY = "function_request_id";
4747
private static final String COLD_START_METRIC = "ColdStart";
48+
private static final String METRICS_DISABLED_ENV_VAR = "POWERTOOLS_METRICS_DISABLED";
4849

4950
private final software.amazon.cloudwatchlogs.emf.logger.MetricsLogger emfLogger;
5051
private final EnvironmentProvider environmentProvider;
@@ -142,6 +143,11 @@ public void clearDefaultDimensions() {
142143

143144
@Override
144145
public void flush() {
146+
if (isMetricsDisabled()) {
147+
LOGGER.debug("Metrics are disabled, skipping flush");
148+
return;
149+
}
150+
145151
Validator.validateNamespace(namespace);
146152

147153
if (!hasMetrics.get()) {
@@ -158,6 +164,11 @@ public void flush() {
158164
public void captureColdStartMetric(Context context,
159165
software.amazon.lambda.powertools.metrics.model.DimensionSet dimensions) {
160166
if (isColdStart()) {
167+
if (isMetricsDisabled()) {
168+
LOGGER.debug("Metrics are disabled, skipping cold start metric capture");
169+
return;
170+
}
171+
161172
Validator.validateNamespace(namespace);
162173

163174
software.amazon.cloudwatchlogs.emf.logger.MetricsLogger coldStartLogger = new software.amazon.cloudwatchlogs.emf.logger.MetricsLogger();
@@ -203,6 +214,11 @@ public void captureColdStartMetric(software.amazon.lambda.powertools.metrics.mod
203214
@Override
204215
public void flushSingleMetric(String name, double value, MetricUnit unit, String namespace,
205216
software.amazon.lambda.powertools.metrics.model.DimensionSet dimensions) {
217+
if (isMetricsDisabled()) {
218+
LOGGER.debug("Metrics are disabled, skipping single metric flush");
219+
return;
220+
}
221+
206222
Validator.validateNamespace(namespace);
207223

208224
// Create a new logger for this single metric
@@ -235,6 +251,11 @@ public void flushSingleMetric(String name, double value, MetricUnit unit, String
235251
singleMetricLogger.flush();
236252
}
237253

254+
private boolean isMetricsDisabled() {
255+
String disabledValue = System.getenv(METRICS_DISABLED_ENV_VAR);
256+
return "true".equalsIgnoreCase(disabledValue);
257+
}
258+
238259
private Unit convertUnit(MetricUnit unit) {
239260
switch (unit) {
240261
case SECONDS:

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.jupiter.params.ParameterizedTest;
3232
import org.junit.jupiter.params.provider.Arguments;
3333
import org.junit.jupiter.params.provider.MethodSource;
34+
import org.junitpioneer.jupiter.SetEnvironmentVariable;
3435

3536
import com.amazonaws.services.lambda.runtime.Context;
3637
import com.fasterxml.jackson.databind.JsonNode;
@@ -458,4 +459,43 @@ void shouldFlushSingleMetricWithoutDimensions() throws Exception {
458459
.isEqualTo("SingleNamespace");
459460
}
460461

462+
@Test
463+
@SetEnvironmentVariable(key = "POWERTOOLS_METRICS_DISABLED", value = "true")
464+
void shouldNotFlushMetricsWhenDisabled() {
465+
// When
466+
metricsLogger.addMetric("test-metric", 100, MetricUnit.COUNT);
467+
metricsLogger.flush();
468+
469+
// Then
470+
String emfOutput = outputStreamCaptor.toString().trim();
471+
assertThat(emfOutput).isEmpty();
472+
}
473+
474+
@Test
475+
@SetEnvironmentVariable(key = "POWERTOOLS_METRICS_DISABLED", value = "true")
476+
void shouldNotCaptureColdStartMetricWhenDisabled() {
477+
// Given
478+
Context testContext = new TestContext();
479+
480+
// When
481+
metricsLogger.captureColdStartMetric(testContext);
482+
483+
// Then
484+
String emfOutput = outputStreamCaptor.toString().trim();
485+
assertThat(emfOutput).isEmpty();
486+
}
487+
488+
@Test
489+
@SetEnvironmentVariable(key = "POWERTOOLS_METRICS_DISABLED", value = "true")
490+
void shouldNotFlushSingleMetricWhenDisabled() {
491+
// Given
492+
DimensionSet dimensions = DimensionSet.of("CustomDim", "CustomValue");
493+
494+
// When
495+
metricsLogger.flushSingleMetric("single-metric", 200, MetricUnit.COUNT, "SingleNamespace", dimensions);
496+
497+
// Then
498+
String emfOutput = outputStreamCaptor.toString().trim();
499+
assertThat(emfOutput).isEmpty();
500+
}
461501
}

0 commit comments

Comments
 (0)