Skip to content

feat(metrics): New metrics module implementation with support for Metrics providers and usage without annotations #1863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
2a79c02
Rewrite metrics module with interface abstraction and provider patter…
phipag Jun 2, 2025
4249766
Update default dimension and namespace logic to have the precedence: …
phipag Jun 3, 2025
9961072
Small cosmetic changes.
phipag Jun 3, 2025
f1e9acb
Add unit tests.
phipag Jun 4, 2025
6b81805
Update metrics documentation with new interface.
phipag Jun 4, 2025
86cb3ce
Make unit tests compatible with GraalVM. It is not supported to mock …
phipag Jun 4, 2025
483766c
Add GraalVM support bullet points to docs pages.
phipag Jun 4, 2025
a14c43c
Update GraalVM metadata files after re-implementing metrics module.
phipag Jun 4, 2025
17c865d
Fix spotbugs issue. This is expected for this factory class for Metri…
phipag Jun 4, 2025
4085b0d
Fix SonarCube findings.
phipag Jun 4, 2025
ead827b
Rename back namespace to ServerlessAirline example.
phipag Jun 4, 2025
ac56a5b
Fix DimensionSet validation to be inline with most recent specification.
phipag Jun 4, 2025
feeefdc
Overload void addDimension(DimensionSet dimensionSet) to be able to a…
phipag Jun 4, 2025
a178128
Update setDefaultDimensions to take a DimensionSet instead of a Map.
phipag Jun 4, 2025
4515ee4
Add warning if not emitting metrics and raiseOnEmptyMetrics is false.
phipag Jun 4, 2025
cc74f3f
Remove unused imports.
phipag Jun 4, 2025
37de57d
Add namespace validation. Refacator into own Validator class. Require…
phipag Jun 5, 2025
36dd713
Update docs with DimensionSet.of instead of Map.of. Fix naming consis…
phipag Jun 5, 2025
e0df19b
Add example for overwriting dimension of cold start metric.
phipag Jun 5, 2025
0a79cec
Add note that metadata needs to be added before flushing.
phipag Jun 5, 2025
7ef4e69
Remove test classes from GRM reflect-config.json.
phipag Jun 5, 2025
b05634d
Rename pushSingleMetric to flushSingleMetric.
phipag Jun 5, 2025
ba09a50
Add example for high cardinality dimensions using DimensionSet.
phipag Jun 5, 2025
05fe15e
Add support for POWERTOOLS_METRICS_DISABLED environment variable.
phipag Jun 5, 2025
eb167f1
Only add service name dimension if the service is actually defined.
phipag Jun 5, 2025
4637fef
Add support for POWERTOOLS_METRICS_FUNCTION_NAME.
phipag Jun 5, 2025
c7c9ed5
Add Testing your code section to docs.
phipag Jun 5, 2025
e4a3464
Rename @Metrics to @FlushMetrics.
phipag Jun 5, 2025
a635610
Rename MetricsLogger to Metrics, rename MetricsLoggerBuilder to Metri…
phipag Jun 5, 2025
ef3347a
Add support for setTimestamp.
phipag Jun 5, 2025
6b82ea5
Update regex for namespace validation.
phipag Jun 5, 2025
120b8d0
Fix namespace regex and fix metrics e2e tests.
phipag Jun 5, 2025
26a4b1d
Add comment in empty callAt method to satisfy pmd_analyse.
phipag Jun 6, 2025
0017e8b
Replace usages of standalone Powertools wording with alternative word…
phipag Jun 6, 2025
3d02853
Reduce cognitive complexity of around() method in LambdaMetricsAspect.
phipag Jun 6, 2025
53d054d
Re-generate GRM files after applying MetricsLogger renaming.
phipag Jun 6, 2025
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
11 changes: 6 additions & 5 deletions docs/core/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,12 @@ Visit the AWS documentation for a complete explanation for [Amazon CloudWatch co

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:

| Setting | Description | Environment variable | Decorator parameter |
| ------------------------------ | ------------------------------------------------------------------------------- | ------------------------------ | ------------------- |
| **Metric namespace** | Logical container where all metrics will be placed e.g. `ServerlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace` |
| **Service** | Optionally, sets **service** metric dimension across all metrics e.g. `payment` | `POWERTOOLS_SERVICE_NAME` | `service` |
| **Disable Powertools Metrics** | Optionally, disables all Powertools metrics | `POWERTOOLS_METRICS_DISABLED` | N/A |
| Setting | Description | Environment variable | Decorator parameter |
| ------------------------------ | ------------------------------------------------------------------------------- | ---------------------------------- | ------------------- |
| **Metric namespace** | Logical container where all metrics will be placed e.g. `ServerlessAirline` | `POWERTOOLS_METRICS_NAMESPACE` | `namespace` |
| **Service** | Optionally, sets **service** metric dimension across all metrics e.g. `payment` | `POWERTOOLS_SERVICE_NAME` | `service` |
| **Function name** | Function name used as dimension for the cold start metric | `POWERTOOLS_METRICS_FUNCTION_NAME` | `functionName` |
| **Disable Powertools Metrics** | Optionally, disables all Powertools metrics | `POWERTOOLS_METRICS_DISABLED` | N/A |

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

Expand Down
5 changes: 3 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,11 @@ Use the following [dependency matrix](https://github.com/eclipse-aspectj/aspectj
**Explicit parameters take precedence over environment variables.**

| Environment variable | Description | Utility |
|----------------------------------------|----------------------------------------------------------------------------------------|---------------------------|
| -------------------------------------- | -------------------------------------------------------------------------------------- | ------------------------- |
| **POWERTOOLS_SERVICE_NAME** | Sets service name used for tracing namespace, metrics dimension and structured logging | All |
| **POWERTOOLS_METRICS_NAMESPACE** | Sets namespace used for metrics | [Metrics](./core/metrics) |
| **POWERTOOLS_METRICS_DISABLED** | Disables all flushing of metrics | [Metrics](./core/metrics) |
| **POWERTOOLS_METRICS_FUNCTION_NAME** | Function name used as dimension for the cold start metric | [Metrics](./core/metrics) |
| **POWERTOOLS_METRICS_DISABLED** | Disables all flushing of metrics | [Metrics](./core/metrics) |
| **POWERTOOLS_LOGGER_SAMPLE_RATE** | Debug log sampling | [Logging](./core/logging) |
| **POWERTOOLS_LOG_LEVEL** | Sets logging level | [Logging](./core/logging) |
| **POWERTOOLS_LOGGER_LOG_EVENT** | Enables/Disables whether to log the incoming event when using the aspect | [Logging](./core/logging) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@
public static final String TRACE_ID_PROPERTY = "xray_trace_id";
public static final String REQUEST_ID_PROPERTY = "function_request_id";
private static final String SERVICE_DIMENSION = "Service";
private static final String FUNCTION_NAME_ENV_VAR = "POWERTOOLS_METRICS_FUNCTION_NAME";

private String functionName(Metrics metrics, Context context) {
if (!"".equals(metrics.functionName())) {
return metrics.functionName();
}

String envFunctionName = System.getenv(FUNCTION_NAME_ENV_VAR);
if (envFunctionName != null && !envFunctionName.isEmpty()) {
return envFunctionName;
}

return context != null ? context.getFunctionName() : null;
}

Expand All @@ -54,8 +61,8 @@

@SuppressWarnings({ "EmptyMethod" })
@Pointcut("@annotation(metrics)")
public void callAt(Metrics metrics) {
}

Check failure on line 65 in powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/internal/LambdaMetricsAspect.java

View workflow job for this annotation

GitHub Actions / pmd_analyse

Document empty method body

Uncommented Empty Method Body finds instances where a method body does not contain statements, but there is no comment. By explicitly commenting empty method bodies it is easier to distinguish between intentional (commented) and unintentional empty methods. UncommentedEmptyMethodBody (Priority: 1, Ruleset: Documentation) https://docs.pmd-code.org/pmd-doc-7.14.0/pmd_rules_java_documentation.html#uncommentedemptymethodbody

@Around(value = "callAt(metrics) && execution(@Metrics * *.*(..))", argNames = "pjp,metrics")
public Object around(ProceedingJoinPoint pjp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ void shouldCaptureColdStartMetricWhenConfigured() throws Exception {
}

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

// FunctionName dimension should be present
assertThat(coldStartNode.has("FunctionName")).isTrue();
assertThat(coldStartNode.get("FunctionName").asText()).isEqualTo("EnvFunctionName");
}

@Test
Expand Down
Loading