-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathMetricsUtils.java
161 lines (138 loc) · 6.17 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package software.amazon.lambda.powertools.metrics;
import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;
import software.amazon.cloudwatchlogs.emf.config.SystemWrapper;
import software.amazon.cloudwatchlogs.emf.environment.EnvironmentProvider;
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
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.Objects.requireNonNull;
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 static DimensionSet[] defaultDimensions;
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;
}
/**
* Configure default dimension to be used by logger.
* By default, @{@link Metrics} annotation captures configured service as a dimension <i>Service</i>
* @param dimensionSets Default value of dimensions set for logger
*/
public static void defaultDimensions(final DimensionSet... dimensionSets) {
MetricsUtils.defaultDimensions = dimensionSets;
}
/**
* Configure default dimension to be used by logger.
* By default, @{@link Metrics} annotation captures configured service as a dimension <i>Service</i>
* @param dimensionSet Default value of dimension set for logger
* @deprecated use {@link #defaultDimensions(DimensionSet...)} instead
*
*/
@Deprecated
public static void defaultDimensionSet(final DimensionSet dimensionSet) {
requireNonNull(dimensionSet, "Null dimension set not allowed");
if(dimensionSet.getDimensionKeys().size() > 0) {
defaultDimensions(dimensionSet);
}
}
/**
* 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 function_request_id as property if used together with {@link Metrics} annotation. It will also
* capture xray_trace_id 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 = logger();
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 function_request_id as property if used together with {@link Metrics} annotation. It will also
* capture xray_trace_id 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 = logger();
try {
metricsLogger.setNamespace(namespace);
metricsLogger.putMetric(name, value, unit);
captureRequestAndTraceId(metricsLogger);
logger.accept(metricsLogger);
} finally {
metricsLogger.flush();
}
}
public static DimensionSet[] getDefaultDimensions() {
return Arrays.copyOf(defaultDimensions, defaultDimensions.length);
}
public static boolean hasDefaultDimension() {
return null != defaultDimensions;
}
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);
}
private static MetricsLogger logger() {
MetricsContext metricsContext = new MetricsContext();
if (hasDefaultDimension()) {
metricsContext.setDimensions(defaultDimensions);
}
return new MetricsLogger(new EnvironmentProvider(), metricsContext);
}
}