1
1
package software .amazon .lambda .powertools .metrics ;
2
2
3
+ import java .util .Optional ;
3
4
import java .util .function .Consumer ;
4
5
5
6
import software .amazon .cloudwatchlogs .emf .config .SystemWrapper ;
8
9
import software .amazon .cloudwatchlogs .emf .model .MetricsLoggerHelper ;
9
10
import software .amazon .cloudwatchlogs .emf .model .Unit ;
10
11
12
+ import static java .util .Optional .ofNullable ;
13
+ import static software .amazon .lambda .powertools .core .internal .LambdaHandlerProcessor .getXrayTraceId ;
14
+ import static software .amazon .lambda .powertools .metrics .internal .LambdaMetricsAspect .REQUEST_ID_PROPERTY ;
15
+ import static software .amazon .lambda .powertools .metrics .internal .LambdaMetricsAspect .TRACE_ID_PROPERTY ;
16
+
11
17
/**
12
18
* A class used to retrieve the instance of the {@code MetricsLogger} used by
13
19
* {@code Metrics}.
@@ -31,21 +37,24 @@ public static MetricsLogger metricsLogger() {
31
37
32
38
/**
33
39
* Add and immediately flush a single metric. It will use the default namespace
34
- * specific either on {@link Metrics} annotation or via POWERTOOLS_METRICS_NAMESPACE env var.
40
+ * specified either on {@link Metrics} annotation or via POWERTOOLS_METRICS_NAMESPACE env var.
41
+ * It by default captures AwsRequestId as property if used together with {@link Metrics} annotation. It will also
42
+ * capture XrayTraceId as property if tracing is enabled.
35
43
*
36
44
* @param name the name of the metric
37
45
* @param value the value of the metric
38
46
* @param unit the unit type of the metric
39
47
* @param logger the MetricsLogger
40
48
*/
41
- public static void withSingleMetricOnDefaultNameSpace (final String name ,
42
- final double value ,
43
- final Unit unit ,
44
- final Consumer <MetricsLogger > logger ) {
49
+ public static void withSingleMetric (final String name ,
50
+ final double value ,
51
+ final Unit unit ,
52
+ final Consumer <MetricsLogger > logger ) {
45
53
MetricsLogger metricsLogger = new MetricsLogger ();
46
54
try {
47
55
metricsLogger .setNamespace (defaultNameSpace ());
48
56
metricsLogger .putMetric (name , value , unit );
57
+ captureRequestAndTraceId (metricsLogger );
49
58
logger .accept (metricsLogger );
50
59
} finally {
51
60
metricsLogger .flush ();
@@ -54,6 +63,8 @@ public static void withSingleMetricOnDefaultNameSpace(final String name,
54
63
55
64
/**
56
65
* Add and immediately flush a single metric.
66
+ * It by default captures AwsRequestId as property if used together with {@link Metrics} annotation. It will also
67
+ * capture XrayTraceId as property if tracing is enabled.
57
68
*
58
69
* @param name the name of the metric
59
70
* @param value the value of the metric
@@ -70,15 +81,30 @@ public static void withSingleMetric(final String name,
70
81
try {
71
82
metricsLogger .setNamespace (namespace );
72
83
metricsLogger .putMetric (name , value , unit );
84
+ captureRequestAndTraceId (metricsLogger );
73
85
logger .accept (metricsLogger );
74
86
} finally {
75
87
metricsLogger .flush ();
76
88
}
77
89
}
78
90
91
+ private static void captureRequestAndTraceId (MetricsLogger metricsLogger ) {
92
+ awsRequestId ().
93
+ ifPresent (requestId -> metricsLogger .putProperty (REQUEST_ID_PROPERTY , requestId ));
94
+
95
+ getXrayTraceId ()
96
+ .ifPresent (traceId -> metricsLogger .putProperty (TRACE_ID_PROPERTY , traceId ));
97
+ }
98
+
79
99
private static String defaultNameSpace () {
80
100
MetricsContext context = MetricsLoggerHelper .metricsContext ();
81
101
return "aws-embedded-metrics" .equals (context .getNamespace ()) ?
82
102
SystemWrapper .getenv ("POWERTOOLS_METRICS_NAMESPACE" ): context .getNamespace ();
83
103
}
104
+
105
+ private static Optional <String > awsRequestId () {
106
+ MetricsContext context = MetricsLoggerHelper .metricsContext ();
107
+ return ofNullable (context .getProperty (REQUEST_ID_PROPERTY ))
108
+ .map (Object ::toString );
109
+ }
84
110
}
0 commit comments