-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathMetricsUtils.java
190 lines (168 loc) · 7.78 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright 2023 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package software.amazon.lambda.powertools.metrics;
import static java.util.Optional.ofNullable;
import static software.amazon.lambda.powertools.common.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;
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.exception.InvalidMetricException;
import software.amazon.cloudwatchlogs.emf.exception.InvalidNamespaceException;
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 software.amazon.lambda.powertools.metrics.exception.InvalidMetricNamespaceException;
/**
* A class used to retrieve the instance of the {@code MetricsLogger} used by
* {@code Metrics}.
* <p>
* {@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;
}
/**
* 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);
} catch (InvalidNamespaceException e) {
throw new InvalidMetricNamespaceException(e);
} catch (InvalidMetricException e) {
throw new software.amazon.lambda.powertools.metrics.exception.InvalidMetricException(e);
} 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);
} catch (InvalidNamespaceException e) {
throw new InvalidMetricNamespaceException(e);
} catch (InvalidMetricException e) {
throw new software.amazon.lambda.powertools.metrics.exception.InvalidMetricException(e);
} finally {
metricsLogger.flush();
}
}
/**
* Provide and immediately flush a {@link MetricsLogger}. It uses 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 logger the MetricsLogger
*/
public static void withMetricsLogger(final Consumer<MetricsLogger> logger) {
MetricsLogger metricsLogger = logger();
try {
metricsLogger.setNamespace(defaultNameSpace());
captureRequestAndTraceId(metricsLogger);
logger.accept(metricsLogger);
} catch (InvalidNamespaceException e) {
throw new InvalidMetricNamespaceException(e);
} 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);
}
}