Skip to content

Commit 4637fef

Browse files
committed
Add support for POWERTOOLS_METRICS_FUNCTION_NAME.
1 parent eb167f1 commit 4637fef

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

docs/core/metrics.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,12 @@ Visit the AWS documentation for a complete explanation for [Amazon CloudWatch co
111111

112112
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` |
118-
| **Disable Powertools Metrics** | Optionally, disables all Powertools metrics | `POWERTOOLS_METRICS_DISABLED` | N/A |
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+
| **Function name** | Function name used as dimension for the cold start metric | `POWERTOOLS_METRICS_FUNCTION_NAME` | `functionName` |
119+
| **Disable Powertools Metrics** | Optionally, disables all Powertools metrics | `POWERTOOLS_METRICS_DISABLED` | N/A |
119120

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

docs/index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,11 @@ Use the following [dependency matrix](https://github.com/eclipse-aspectj/aspectj
236236
**Explicit parameters take precedence over environment variables.**
237237

238238
| Environment variable | Description | Utility |
239-
|----------------------------------------|----------------------------------------------------------------------------------------|---------------------------|
239+
| -------------------------------------- | -------------------------------------------------------------------------------------- | ------------------------- |
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) |
242+
| **POWERTOOLS_METRICS_FUNCTION_NAME** | Function name used as dimension for the cold start metric | [Metrics](./core/metrics) |
243+
| **POWERTOOLS_METRICS_DISABLED** | Disables all flushing of metrics | [Metrics](./core/metrics) |
243244
| **POWERTOOLS_LOGGER_SAMPLE_RATE** | Debug log sampling | [Logging](./core/logging) |
244245
| **POWERTOOLS_LOG_LEVEL** | Sets logging level | [Logging](./core/logging) |
245246
| **POWERTOOLS_LOGGER_LOG_EVENT** | Enables/Disables whether to log the incoming event when using the aspect | [Logging](./core/logging) |

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@ public class LambdaMetricsAspect {
3737
public static final String TRACE_ID_PROPERTY = "xray_trace_id";
3838
public static final String REQUEST_ID_PROPERTY = "function_request_id";
3939
private static final String SERVICE_DIMENSION = "Service";
40+
private static final String FUNCTION_NAME_ENV_VAR = "POWERTOOLS_METRICS_FUNCTION_NAME";
4041

4142
private String functionName(Metrics metrics, Context context) {
4243
if (!"".equals(metrics.functionName())) {
4344
return metrics.functionName();
4445
}
46+
47+
String envFunctionName = System.getenv(FUNCTION_NAME_ENV_VAR);
48+
if (envFunctionName != null && !envFunctionName.isEmpty()) {
49+
return envFunctionName;
50+
}
51+
4552
return context != null ? context.getFunctionName() : null;
4653
}
4754

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ void shouldCaptureColdStartMetricWhenConfigured() throws Exception {
162162
}
163163

164164
@Test
165+
@SetEnvironmentVariable(key = "POWERTOOLS_METRICS_FUNCTION_NAME", value = "EnvFunctionName")
165166
void shouldNotIncludeServiceDimensionInColdStartMetricWhenServiceUndefined() throws Exception {
166167
// Given - no service name set, so it will use the default undefined value
167168
RequestHandler<Map<String, Object>, String> handler = new HandlerWithColdStartMetricsAnnotation();
@@ -187,6 +188,7 @@ void shouldNotIncludeServiceDimensionInColdStartMetricWhenServiceUndefined() thr
187188

188189
// FunctionName dimension should be present
189190
assertThat(coldStartNode.has("FunctionName")).isTrue();
191+
assertThat(coldStartNode.get("FunctionName").asText()).isEqualTo("EnvFunctionName");
190192
}
191193

192194
@Test

0 commit comments

Comments
 (0)