|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.metrics.publishers.emf; |
| 17 | + |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.List; |
| 23 | +import software.amazon.awssdk.annotations.Immutable; |
| 24 | +import software.amazon.awssdk.annotations.SdkPublicApi; |
| 25 | +import software.amazon.awssdk.annotations.ThreadSafe; |
| 26 | +import software.amazon.awssdk.core.metrics.CoreMetric; |
| 27 | +import software.amazon.awssdk.metrics.MetricCategory; |
| 28 | +import software.amazon.awssdk.metrics.MetricCollection; |
| 29 | +import software.amazon.awssdk.metrics.MetricLevel; |
| 30 | +import software.amazon.awssdk.metrics.MetricPublisher; |
| 31 | +import software.amazon.awssdk.metrics.SdkMetric; |
| 32 | +import software.amazon.awssdk.metrics.publishers.emf.internal.EmfMetricConfiguration; |
| 33 | +import software.amazon.awssdk.metrics.publishers.emf.internal.MetricEmfConverter; |
| 34 | +import software.amazon.awssdk.utils.Logger; |
| 35 | + |
| 36 | +/** |
| 37 | + * A metric publisher implementation that converts metrics into CloudWatch Embedded Metric Format (EMF). |
| 38 | + * EMF allows metrics to be published through CloudWatch Logs using a structured JSON format, which |
| 39 | + * CloudWatch automatically extracts and processes into metrics. |
| 40 | + * |
| 41 | + * <p> |
| 42 | + * This publisher is particularly well-suited for serverless environments like AWS Lambda and container |
| 43 | + * environments like Amazon ECS that have built-in integration with CloudWatch Logs. Using EMF eliminates |
| 44 | + * the need for separate metric publishing infrastructure as metrics are automatically extracted from |
| 45 | + * log entries. |
| 46 | + * </p> |
| 47 | + * |
| 48 | + * <p> |
| 49 | + * The EMF publisher converts metric collections into JSON-formatted log entries that conform to the |
| 50 | + * CloudWatch EMF specification. The logGroupName field is required for EMF to work. |
| 51 | + * CloudWatch automatically processes these logs to generate corresponding metrics that can be used for |
| 52 | + * monitoring and alerting. |
| 53 | + * </p> |
| 54 | + * |
| 55 | + * @snippet |
| 56 | + * // Create a EmfMetricLoggingPublisher using a custom namespace. |
| 57 | + * MetricPublisher emfMetricLoggingPublisher = EmfMetricLoggingPublisher.builder() |
| 58 | + * .logGroupName("myLogGroupName") |
| 59 | + * .namespace("myApplication") |
| 60 | + * .build(); |
| 61 | + * |
| 62 | + * @see MetricPublisher The base interface for metric publishers |
| 63 | + * @see MetricCollection For the collection of metrics to be published |
| 64 | + * @see EmfMetricConfiguration For configuration options |
| 65 | + * @see MetricEmfConverter For the conversion logic |
| 66 | + * |
| 67 | + */ |
| 68 | + |
| 69 | +@ThreadSafe |
| 70 | +@Immutable |
| 71 | +@SdkPublicApi |
| 72 | +public final class EmfMetricLoggingPublisher implements MetricPublisher { |
| 73 | + |
| 74 | + private static final Logger logger = Logger.loggerFor(EmfMetricLoggingPublisher.class); |
| 75 | + private final MetricEmfConverter metricConverter; |
| 76 | + |
| 77 | + |
| 78 | + private EmfMetricLoggingPublisher(Builder builder) { |
| 79 | + EmfMetricConfiguration config = new EmfMetricConfiguration.Builder() |
| 80 | + .namespace(builder.namespace) |
| 81 | + .logGroupName(builder.logGroupName) |
| 82 | + .dimensions(builder.dimensions) |
| 83 | + .metricLevel(builder.metricLevel) |
| 84 | + .metricCategories(builder.metricCategories) |
| 85 | + .build(); |
| 86 | + |
| 87 | + this.metricConverter = new MetricEmfConverter(config); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + public static Builder builder() { |
| 92 | + return new Builder(); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + @Override |
| 97 | + public void publish(MetricCollection metricCollection) { |
| 98 | + if (metricCollection == null) { |
| 99 | + logger.warn(() -> "Null metric collection passed to the publisher"); |
| 100 | + return; |
| 101 | + } |
| 102 | + try { |
| 103 | + List<String> emfStrings = metricConverter.convertMetricCollectionToEmf(metricCollection); |
| 104 | + for (String emfString : emfStrings) { |
| 105 | + logger.info(() -> emfString); |
| 106 | + } |
| 107 | + } catch (Exception e) { |
| 108 | + logger.error(() -> "Failed to log metrics in EMF format", e); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Closes this metric publisher. This implementation is empty as the EMF metric logging publisher |
| 114 | + * does not maintain any resources that require explicit cleanup. |
| 115 | + */ |
| 116 | + @Override |
| 117 | + public void close() { |
| 118 | + } |
| 119 | + |
| 120 | + public static final class Builder { |
| 121 | + private String namespace; |
| 122 | + private String logGroupName; |
| 123 | + private Collection<SdkMetric<String>> dimensions; |
| 124 | + private Collection<MetricCategory> metricCategories; |
| 125 | + private MetricLevel metricLevel; |
| 126 | + |
| 127 | + private Builder() { |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Configure the namespace that will be put into the emf log to this publisher. |
| 132 | + * |
| 133 | + * <p>If this is not specified, {@code AwsSdk/JavaSdk2} will be used. |
| 134 | + */ |
| 135 | + public Builder namespace(String namespace) { |
| 136 | + this.namespace = namespace; |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Configure the {@link SdkMetric} that are used to define the Dimension Set Array that will be put into the emf log to |
| 142 | + * this |
| 143 | + * publisher. |
| 144 | + * |
| 145 | + * <p>If this is not specified, {@link CoreMetric#SERVICE_ID} and {@link CoreMetric#OPERATION_NAME} will be used. |
| 146 | + */ |
| 147 | + public Builder dimensions(Collection<SdkMetric<String>> dimensions) { |
| 148 | + this.dimensions = new ArrayList<>(dimensions); |
| 149 | + return this; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @see #dimensions(SdkMetric[]) |
| 154 | + */ |
| 155 | + @SafeVarargs |
| 156 | + public final Builder dimensions(SdkMetric<String>... dimensions) { |
| 157 | + return dimensions(Arrays.asList(dimensions)); |
| 158 | + } |
| 159 | + |
| 160 | + |
| 161 | + /** |
| 162 | + * Configure the {@link MetricCategory}s that should be uploaded to CloudWatch. |
| 163 | + * |
| 164 | + * <p>If this is not specified, {@link MetricCategory#ALL} is used. |
| 165 | + * |
| 166 | + * <p>All {@link SdkMetric}s are associated with at least one {@code MetricCategory}. This setting determines which |
| 167 | + * category of metrics uploaded to CloudWatch. Any metrics {@link #publish(MetricCollection)}ed that do not fall under |
| 168 | + * these configured categories are ignored. |
| 169 | + * |
| 170 | + * <p>Note: If there are {@link #dimensions(Collection)} configured that do not fall under these {@code MetricCategory} |
| 171 | + * values, the dimensions will NOT be ignored. In other words, the metric category configuration only affects which |
| 172 | + * metrics are uploaded to CloudWatch, not which values can be used for {@code dimensions}. |
| 173 | + */ |
| 174 | + public Builder metricCategories(Collection<MetricCategory> metricCategories) { |
| 175 | + this.metricCategories = new ArrayList<>(metricCategories); |
| 176 | + return this; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * @see #metricCategories(Collection) |
| 181 | + */ |
| 182 | + public Builder metricCategories(MetricCategory... metricCategories) { |
| 183 | + return metricCategories(Arrays.asList(metricCategories)); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Configure the LogGroupName key that will be put into the emf log to this publisher. This is required when using |
| 188 | + * the CloudWatch agent to send embedded metric format logs that tells the agent which log |
| 189 | + * group to use. |
| 190 | + * |
| 191 | + * <p> If this is not specified, for AWS lambda environments, {@code AWS_LAMBDA_LOG_GROUP_NAME} |
| 192 | + * is used. |
| 193 | + * This field is required and must not be null or empty for non-lambda environments. |
| 194 | + * @throws NullPointerException if non-lambda environment and logGroupName is null |
| 195 | + */ |
| 196 | + public Builder logGroupName(String logGroupName) { |
| 197 | + this.logGroupName = logGroupName; |
| 198 | + return this; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Configure the {@link MetricLevel} that should be uploaded to CloudWatch. |
| 203 | + * |
| 204 | + * <p>If this is not specified, {@link MetricLevel#INFO} is used. |
| 205 | + * |
| 206 | + * <p>All {@link SdkMetric}s are associated with one {@code MetricLevel}. This setting determines which level of metrics |
| 207 | + * uploaded to CloudWatch. Any metrics {@link #publish(MetricCollection)}ed that do not fall under these configured |
| 208 | + * categories are ignored. |
| 209 | + * |
| 210 | + * <p>Note: If there are {@link #dimensions(Collection)} configured that do not fall under this {@code MetricLevel} |
| 211 | + * values, the dimensions will NOT be ignored. In other words, the metric category configuration only affects which |
| 212 | + * metrics are uploaded to CloudWatch, not which values can be used for {@code dimensions}. |
| 213 | + */ |
| 214 | + public Builder metricLevel(MetricLevel metricLevel) { |
| 215 | + this.metricLevel = metricLevel; |
| 216 | + return this; |
| 217 | + } |
| 218 | + |
| 219 | + |
| 220 | + /** |
| 221 | + * Build a {@link EmfMetricLoggingPublisher} using the configuration currently configured on this publisher. |
| 222 | + */ |
| 223 | + public EmfMetricLoggingPublisher build() { |
| 224 | + return new EmfMetricLoggingPublisher(this); |
| 225 | + } |
| 226 | + |
| 227 | + } |
| 228 | +} |
0 commit comments