-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathMetricsLogger.java
179 lines (164 loc) · 6.73 KB
/
MetricsLogger.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
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* 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.cloudwatchlogs.emf.logger;
import java.util.concurrent.CompletableFuture;
import lombok.extern.slf4j.Slf4j;
import software.amazon.cloudwatchlogs.emf.environment.Environment;
import software.amazon.cloudwatchlogs.emf.environment.EnvironmentProvider;
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
import software.amazon.cloudwatchlogs.emf.model.MetricsContext;
import software.amazon.cloudwatchlogs.emf.model.Unit;
import software.amazon.cloudwatchlogs.emf.sinks.ISink;
/**
* An metrics logger. Use this interface to publish logs to CloudWatch Logs and extract metrics to
* CloudWatch Metrics asynchronously.
*/
@Slf4j
public class MetricsLogger {
private MetricsContext context;
private CompletableFuture<Environment> environmentFuture;
private EnvironmentProvider environmentProvider;
public MetricsLogger() {
this(new EnvironmentProvider());
}
public MetricsLogger(EnvironmentProvider environmentProvider) {
this(environmentProvider, new MetricsContext());
}
public MetricsLogger(EnvironmentProvider environmentProvider, MetricsContext metricsContext) {
context = metricsContext;
environmentFuture = environmentProvider.resolveEnvironment();
this.environmentProvider = environmentProvider;
}
/**
* Flushes the current context state to the configured sink. TODO: Support flush asynchronously
*/
public void flush() {
Environment environment;
try {
environment = environmentFuture.join();
} catch (Exception ex) {
log.info("Failed to resolve environment. Fallback to default environment: ", ex);
environment = environmentProvider.getDefaultEnvironment();
}
ISink sink = environment.getSink();
configureContextForEnvironment(context, environment);
sink.accept(context);
context = context.createCopyWithContext();
}
/**
* Set a property on the published metrics. This is stored in the emitted log data and you are
* not charged for this data by CloudWatch Metrics. These values can be values that are useful
* for searching on, but have too high cardinality to emit as dimensions to CloudWatch Metrics.
*
* @param key Property name
* @param value Property value
* @return the current logger
*/
public MetricsLogger putProperty(String key, Object value) {
this.context.putProperty(key, value);
return this;
}
/**
* Adds a dimension. This is generally a low cardinality key-value pair that is part of the
* metric identity. CloudWatch treats each unique combination of dimensions as a separate
* metric, even if the metrics have the same metric name.
*
* @param dimensions the DimensionSet to add
* @see <a
* href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html#Dimension">CloudWatch
* Dimensions</a>
* @return the current logger
*/
public MetricsLogger putDimensions(DimensionSet dimensions) {
context.putDimension(dimensions);
return this;
}
/**
* Overwrite all dimensions on this MetricsLogger instance.
*
* @param dimensionSets the dimensionSets to set.
* @see <a
* href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html#Dimension">CloudWatch
* Dimensions</a>
* @return the current logger
*/
public MetricsLogger setDimensions(DimensionSet... dimensionSets) {
context.setDimensions(dimensionSets);
return this;
}
/**
* Put a metric value. This value will be emitted to CloudWatch Metrics asyncronously and does
* not contribute to your account TPS limits. The value will also be available in your
* CloudWatch Logs
*
* @param key is the name of the metric
* @param value is the value of the metric
* @param unit is the unit of the metric value
* @return the current logger
*/
public MetricsLogger putMetric(String key, double value, Unit unit) {
this.context.putMetric(key, value, unit);
return this;
}
/**
* Put a metric value. This value will be emitted to CloudWatch Metrics asyncronously and does
* not contribute to your account TPS limits. The value will also be available in your
* CloudWatch Logs
*
* @param key the name of the metric
* @param value the value of the metric
* @return the current logger
*/
public MetricsLogger putMetric(String key, double value) {
this.context.putMetric(key, value, Unit.NONE);
return this;
}
/**
* Add a custom key-value pair to the Metadata object.
*
* @see <a
* href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Specification.html#CloudWatch_Embedded_Metric_Format_Specification_structure_metadata">CloudWatch
* Metadata</a>
* @param key the name of the key
* @param value the value associated with the key
* @return the current logger
*/
public MetricsLogger putMetadata(String key, Object value) {
this.context.putMetadata(key, value);
return this;
}
/**
* Set the CloudWatch namespace that metrics should be published to.
*
* @param namespace the namespace of the logs
* @return the current logger
*/
public MetricsLogger setNamespace(String namespace) {
this.context.setNamespace(namespace);
return this;
}
private void configureContextForEnvironment(MetricsContext context, Environment environment) {
if (context.hasDefaultDimensions()) {
return;
}
DimensionSet defaultDimension = new DimensionSet();
defaultDimension.addDimension("LogGroup", environment.getLogGroupName());
defaultDimension.addDimension("ServiceName", environment.getName());
defaultDimension.addDimension("ServiceType", environment.getType());
context.setDefaultDimensions(defaultDimension);
environment.configureContext(context);
}
}