4
4
import java .util .function .Consumer ;
5
5
6
6
import software .amazon .cloudwatchlogs .emf .config .SystemWrapper ;
7
+ import software .amazon .cloudwatchlogs .emf .environment .EnvironmentProvider ;
7
8
import software .amazon .cloudwatchlogs .emf .logger .MetricsLogger ;
9
+ import software .amazon .cloudwatchlogs .emf .model .DimensionSet ;
8
10
import software .amazon .cloudwatchlogs .emf .model .MetricsContext ;
9
11
import software .amazon .cloudwatchlogs .emf .model .MetricsLoggerHelper ;
10
12
import software .amazon .cloudwatchlogs .emf .model .Unit ;
11
13
14
+ import static java .util .Objects .requireNonNull ;
12
15
import static java .util .Optional .ofNullable ;
13
16
import static software .amazon .lambda .powertools .core .internal .LambdaHandlerProcessor .getXrayTraceId ;
14
17
import static software .amazon .lambda .powertools .metrics .internal .LambdaMetricsAspect .REQUEST_ID_PROPERTY ;
22
25
*/
23
26
public final class MetricsUtils {
24
27
private static final MetricsLogger metricsLogger = new MetricsLogger ();
28
+ private static DimensionSet defaultDimensionSet ;
25
29
26
30
private MetricsUtils () {
27
31
}
@@ -35,11 +39,22 @@ public static MetricsLogger metricsLogger() {
35
39
return metricsLogger ;
36
40
}
37
41
42
+ /**
43
+ * Configure default dimension to be used by logger.
44
+ * By default, @{@link Metrics} annotation captures configured service as a dimension <i>Service</i>
45
+ * @param dimensionSet Default value of dimension set for logger
46
+ */
47
+ public static void defaultDimensionSet (final DimensionSet dimensionSet ) {
48
+ requireNonNull (dimensionSet , "Null dimension set not allowed" );
49
+ MetricsUtils .defaultDimensionSet = dimensionSet ;
50
+ }
51
+
52
+
38
53
/**
39
54
* Add and immediately flush a single metric. It will use the default namespace
40
55
* 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.
56
+ * It by default captures function_request_id as property if used together with {@link Metrics} annotation. It will also
57
+ * capture xray_trace_id as property if tracing is enabled.
43
58
*
44
59
* @param name the name of the metric
45
60
* @param value the value of the metric
@@ -50,7 +65,8 @@ public static void withSingleMetric(final String name,
50
65
final double value ,
51
66
final Unit unit ,
52
67
final Consumer <MetricsLogger > logger ) {
53
- MetricsLogger metricsLogger = new MetricsLogger ();
68
+ MetricsLogger metricsLogger = logger ();
69
+
54
70
try {
55
71
metricsLogger .setNamespace (defaultNameSpace ());
56
72
metricsLogger .putMetric (name , value , unit );
@@ -63,8 +79,8 @@ public static void withSingleMetric(final String name,
63
79
64
80
/**
65
81
* 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.
82
+ * It by default captures function_request_id as property if used together with {@link Metrics} annotation. It will also
83
+ * capture xray_trace_id as property if tracing is enabled.
68
84
*
69
85
* @param name the name of the metric
70
86
* @param value the value of the metric
@@ -77,7 +93,8 @@ public static void withSingleMetric(final String name,
77
93
final Unit unit ,
78
94
final String namespace ,
79
95
final Consumer <MetricsLogger > logger ) {
80
- MetricsLogger metricsLogger = new MetricsLogger ();
96
+ MetricsLogger metricsLogger = logger ();
97
+
81
98
try {
82
99
metricsLogger .setNamespace (namespace );
83
100
metricsLogger .putMetric (name , value , unit );
@@ -88,6 +105,14 @@ public static void withSingleMetric(final String name,
88
105
}
89
106
}
90
107
108
+ public static DimensionSet defaultDimensionSet () {
109
+ return defaultDimensionSet ;
110
+ }
111
+
112
+ public static boolean hasDefaultDimension () {
113
+ return defaultDimensionSet .getDimensionKeys ().size () > 0 ;
114
+ }
115
+
91
116
private static void captureRequestAndTraceId (MetricsLogger metricsLogger ) {
92
117
awsRequestId ().
93
118
ifPresent (requestId -> metricsLogger .putProperty (REQUEST_ID_PROPERTY , requestId ));
@@ -107,4 +132,14 @@ private static Optional<String> awsRequestId() {
107
132
return ofNullable (context .getProperty (REQUEST_ID_PROPERTY ))
108
133
.map (Object ::toString );
109
134
}
135
+
136
+ private static MetricsLogger logger () {
137
+ MetricsContext metricsContext = new MetricsContext ();
138
+
139
+ if (hasDefaultDimension ()) {
140
+ metricsContext .setDefaultDimensions (defaultDimensionSet ());
141
+ }
142
+
143
+ return new MetricsLogger (new EnvironmentProvider (), metricsContext );
144
+ }
110
145
}
0 commit comments