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