44import java .util .function .Consumer ;
55
66import software .amazon .cloudwatchlogs .emf .config .SystemWrapper ;
7+ import software .amazon .cloudwatchlogs .emf .environment .EnvironmentProvider ;
78import software .amazon .cloudwatchlogs .emf .logger .MetricsLogger ;
9+ import software .amazon .cloudwatchlogs .emf .model .DimensionSet ;
810import software .amazon .cloudwatchlogs .emf .model .MetricsContext ;
911import software .amazon .cloudwatchlogs .emf .model .MetricsLoggerHelper ;
1012import software .amazon .cloudwatchlogs .emf .model .Unit ;
1113
14+ import static java .util .Objects .requireNonNull ;
1215import static java .util .Optional .ofNullable ;
1316import static software .amazon .lambda .powertools .core .internal .LambdaHandlerProcessor .getXrayTraceId ;
1417import static software .amazon .lambda .powertools .metrics .internal .LambdaMetricsAspect .REQUEST_ID_PROPERTY ;
2225 */
2326public final class MetricsUtils {
2427 private static final MetricsLogger metricsLogger = new MetricsLogger ();
28+ private static DimensionSet defaultDimensionSet ;
2529
2630 private MetricsUtils () {
2731 }
@@ -35,11 +39,22 @@ public static MetricsLogger metricsLogger() {
3539 return metricsLogger ;
3640 }
3741
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+
3853 /**
3954 * Add and immediately flush a single metric. It will use the default namespace
4055 * 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.
4358 *
4459 * @param name the name of the metric
4560 * @param value the value of the metric
@@ -50,7 +65,8 @@ public static void withSingleMetric(final String name,
5065 final double value ,
5166 final Unit unit ,
5267 final Consumer <MetricsLogger > logger ) {
53- MetricsLogger metricsLogger = new MetricsLogger ();
68+ MetricsLogger metricsLogger = logger ();
69+
5470 try {
5571 metricsLogger .setNamespace (defaultNameSpace ());
5672 metricsLogger .putMetric (name , value , unit );
@@ -63,8 +79,8 @@ public static void withSingleMetric(final String name,
6379
6480 /**
6581 * 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.
6884 *
6985 * @param name the name of the metric
7086 * @param value the value of the metric
@@ -77,7 +93,8 @@ public static void withSingleMetric(final String name,
7793 final Unit unit ,
7894 final String namespace ,
7995 final Consumer <MetricsLogger > logger ) {
80- MetricsLogger metricsLogger = new MetricsLogger ();
96+ MetricsLogger metricsLogger = logger ();
97+
8198 try {
8299 metricsLogger .setNamespace (namespace );
83100 metricsLogger .putMetric (name , value , unit );
@@ -88,6 +105,14 @@ public static void withSingleMetric(final String name,
88105 }
89106 }
90107
108+ public static DimensionSet defaultDimensionSet () {
109+ return defaultDimensionSet ;
110+ }
111+
112+ public static boolean hasDefaultDimension () {
113+ return defaultDimensionSet .getDimensionKeys ().size () > 0 ;
114+ }
115+
91116 private static void captureRequestAndTraceId (MetricsLogger metricsLogger ) {
92117 awsRequestId ().
93118 ifPresent (requestId -> metricsLogger .putProperty (REQUEST_ID_PROPERTY , requestId ));
@@ -107,4 +132,14 @@ private static Optional<String> awsRequestId() {
107132 return ofNullable (context .getProperty (REQUEST_ID_PROPERTY ))
108133 .map (Object ::toString );
109134 }
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+ }
110145}
0 commit comments