|
15 | 15 |
|
16 | 16 | package software.amazon.awssdk.metrics;
|
17 | 17 |
|
| 18 | +import static software.amazon.awssdk.utils.StringUtils.repeat; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import org.slf4j.event.Level; |
18 | 23 | import software.amazon.awssdk.annotations.SdkPublicApi;
|
19 | 24 | import software.amazon.awssdk.utils.Logger;
|
| 25 | +import software.amazon.awssdk.utils.Validate; |
20 | 26 |
|
21 | 27 | /**
|
22 |
| - * An implementation of {@link MetricPublisher} that writes all published metrics to the logs at the INFO level under the |
23 |
| - * {@code software.amazon.awssdk.metrics.LoggingMetricPublisher} namespace. |
| 28 | + * An implementation of {@link MetricPublisher} that logs all published metrics under the {@code |
| 29 | + * software.amazon.awssdk.metrics.LoggingMetricPublisher} namespace. |
| 30 | + * <p> |
| 31 | + * {@link LoggingMetricPublisher} can be configured with a {@link Level} to control the log level at which metrics are recorded |
| 32 | + * and a {@link Format} to control the format that metrics are printed in. |
| 33 | + * <p> |
| 34 | + * {@link Format#PLAIN} can be used to print all metrics on a single line. E.g., |
| 35 | + * <pre> |
| 36 | + * Metrics published: MetricCollection(name=ApiCall, metrics=[MetricRecord(metric=MarshallingDuration, value=PT0.000202197S), |
| 37 | + * MetricRecord(metric=RetryCount, value=0), MetricRecord(metric=ApiCallSuccessful, value=true), |
| 38 | + * MetricRecord(metric=OperationName, value=HeadObject), MetricRecord(metric=ApiCallDuration, value=PT0.468369S), |
| 39 | + * MetricRecord(metric=CredentialsFetchDuration, value=PT0.000003191S), MetricRecord(metric=ServiceId, value=S3)], |
| 40 | + * children=[MetricCollection(name=ApiCallAttempt, metrics=[MetricRecord(metric=SigningDuration, value=PT0.000667268S), |
| 41 | + * MetricRecord(metric=ServiceCallDuration, value=PT0.460529977S), MetricRecord(metric=AwsExtendedRequestId, |
| 42 | + * value=jY/Co5Ge6WjRYk78kGOYQ4Z/CqUBr6pAAPZtexgOQR3Iqs3QP0OfZz3fDraQiXtmx7eXCZ4sbO0=), MetricRecord(metric=HttpStatusCode, |
| 43 | + * value=200), MetricRecord(metric=BackoffDelayDuration, value=PT0S), MetricRecord(metric=AwsRequestId, value=6SJ82R65SADHX098)], |
| 44 | + * children=[MetricCollection(name=HttpClient, metrics=[MetricRecord(metric=AvailableConcurrency, value=0), |
| 45 | + * MetricRecord(metric=LeasedConcurrency, value=0), MetricRecord(metric=ConcurrencyAcquireDuration, value=PT0.230757S), |
| 46 | + * MetricRecord(metric=PendingConcurrencyAcquires, value=0), MetricRecord(metric=MaxConcurrency, value=50), |
| 47 | + * MetricRecord(metric=HttpClientName, value=NettyNio)], children=[])])]) |
| 48 | + * </pre> |
| 49 | + * {@link Format#PRETTY} can be used to print metrics over multiple lines in a readable fashion suitable for debugging. E.g., |
| 50 | + * <pre> |
| 51 | + * [18e5092e] ApiCall |
| 52 | + * [18e5092e] ┌────────────────────────────────────────┐ |
| 53 | + * [18e5092e] │ MarshallingDuration=PT0.000227427S │ |
| 54 | + * [18e5092e] │ RetryCount=0 │ |
| 55 | + * [18e5092e] │ ApiCallSuccessful=true │ |
| 56 | + * [18e5092e] │ OperationName=HeadObject │ |
| 57 | + * [18e5092e] │ ApiCallDuration=PT0.541751S │ |
| 58 | + * [18e5092e] │ CredentialsFetchDuration=PT0.00000306S │ |
| 59 | + * [18e5092e] │ ServiceId=S3 │ |
| 60 | + * [18e5092e] └────────────────────────────────────────┘ |
| 61 | + * [18e5092e] ApiCallAttempt |
| 62 | + * [18e5092e] ┌───────────────────────────────────────────────────────────────────────────────────────────────────┐ |
| 63 | + * [18e5092e] │ SigningDuration=PT0.000974924S │ |
| 64 | + * [18e5092e] │ ServiceCallDuration=PT0.531462375S │ |
| 65 | + * [18e5092e] │ AwsExtendedRequestId=eGfwjV3mSwQZQD4YxHLswYguvhQoGcDTkr2jRvpio37a6QmhWd18C8wagC8LkBzzcnOOKoMuiXw= │ |
| 66 | + * [18e5092e] │ HttpStatusCode=200 │ |
| 67 | + * [18e5092e] │ BackoffDelayDuration=PT0S │ |
| 68 | + * [18e5092e] │ AwsRequestId=ED46TP7NN62DDG4Q │ |
| 69 | + * [18e5092e] └───────────────────────────────────────────────────────────────────────────────────────────────────┘ |
| 70 | + * [18e5092e] HttpClient |
| 71 | + * [18e5092e] ┌────────────────────────────────────────┐ |
| 72 | + * [18e5092e] │ AvailableConcurrency=0 │ |
| 73 | + * [18e5092e] │ LeasedConcurrency=0 │ |
| 74 | + * [18e5092e] │ ConcurrencyAcquireDuration=PT0.235851S │ |
| 75 | + * [18e5092e] │ PendingConcurrencyAcquires=0 │ |
| 76 | + * [18e5092e] │ MaxConcurrency=50 │ |
| 77 | + * [18e5092e] │ HttpClientName=NettyNio │ |
| 78 | + * [18e5092e] └────────────────────────────────────────┘ |
| 79 | + * </pre> |
| 80 | + * Note that the output format may be subject to small changes in future versions and should not be relied upon as a strict public |
| 81 | + * contract. |
24 | 82 | */
|
25 | 83 | @SdkPublicApi
|
26 | 84 | public final class LoggingMetricPublisher implements MetricPublisher {
|
| 85 | + |
| 86 | + public enum Format { |
| 87 | + PLAIN, |
| 88 | + PRETTY |
| 89 | + } |
| 90 | + |
27 | 91 | private static final Logger LOGGER = Logger.loggerFor(LoggingMetricPublisher.class);
|
| 92 | + private static final Integer PRETTY_INDENT_SIZE = 4; |
| 93 | + |
| 94 | + private final Level logLevel; |
| 95 | + private final Format format; |
28 | 96 |
|
29 |
| - private LoggingMetricPublisher() { |
| 97 | + private LoggingMetricPublisher(Level logLevel, Format format) { |
| 98 | + this.logLevel = Validate.notNull(logLevel, "logLevel"); |
| 99 | + this.format = Validate.notNull(format, "format"); |
30 | 100 | }
|
31 | 101 |
|
| 102 | + /** |
| 103 | + * Create a {@link LoggingMetricPublisher} with the default configuration of {@link Level#INFO} and {@link Format#PLAIN}. |
| 104 | + */ |
32 | 105 | public static LoggingMetricPublisher create() {
|
33 |
| - return new LoggingMetricPublisher(); |
| 106 | + return new LoggingMetricPublisher(Level.INFO, Format.PLAIN); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Create a {@link LoggingMetricPublisher} with a custom {@link Format} and log {@link Level}. |
| 111 | + * |
| 112 | + * @param logLevel the SLF4J log level to log metrics with |
| 113 | + * @param format the format to print the metrics with (see class-level documentation for examples) |
| 114 | + */ |
| 115 | + public static LoggingMetricPublisher create(Level logLevel, Format format) { |
| 116 | + return new LoggingMetricPublisher(logLevel, format); |
34 | 117 | }
|
35 | 118 |
|
36 | 119 | @Override
|
37 |
| - public void publish(MetricCollection metricCollection) { |
38 |
| - LOGGER.info(() -> "Metrics published: " + metricCollection); |
| 120 | + public void publish(MetricCollection metrics) { |
| 121 | + if (!LOGGER.isLoggingLevelEnabled(logLevel)) { |
| 122 | + return; |
| 123 | + } |
| 124 | + switch (format) { |
| 125 | + case PLAIN: |
| 126 | + LOGGER.log(logLevel, () -> "Metrics published: " + metrics); |
| 127 | + break; |
| 128 | + case PRETTY: |
| 129 | + String guid = Integer.toHexString(metrics.hashCode()); |
| 130 | + logPretty(guid, metrics, 0); |
| 131 | + break; |
| 132 | + default: |
| 133 | + throw new IllegalStateException("Unsupported format: " + format); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private void logPretty(String guid, MetricCollection metrics, int indent) { |
| 138 | + // Pre-determine metric key-value-pairs so that we can calculate the necessary padding |
| 139 | + List<String> metricValues = new ArrayList<>(); |
| 140 | + metrics.forEach(m -> { |
| 141 | + metricValues.add(String.format("%s=%s", m.metric().name(), m.value())); |
| 142 | + }); |
| 143 | + |
| 144 | + int maxLen = getMaxLen(metricValues); |
| 145 | + |
| 146 | + // MetricCollection name |
| 147 | + LOGGER.log(logLevel, () -> String.format("[%s]%s %s", |
| 148 | + guid, |
| 149 | + repeat(" ", indent), |
| 150 | + metrics.name())); |
| 151 | + |
| 152 | + // Open box |
| 153 | + LOGGER.log(logLevel, () -> String.format("[%s]%s ┌%s┐", |
| 154 | + guid, |
| 155 | + repeat(" ", indent), |
| 156 | + repeat("─", maxLen + 2))); |
| 157 | + |
| 158 | + // Metric key-value-pairs |
| 159 | + metricValues.forEach(metric -> LOGGER.log(logLevel, () -> { |
| 160 | + return String.format("[%s]%s │ %s │", |
| 161 | + guid, |
| 162 | + repeat(" ", indent), |
| 163 | + pad(metric, maxLen)); |
| 164 | + })); |
| 165 | + |
| 166 | + // Close box |
| 167 | + LOGGER.log(logLevel, () -> String.format("[%s]%s └%s┘", |
| 168 | + guid, |
| 169 | + repeat(" ", indent), |
| 170 | + repeat("─", maxLen + 2))); |
| 171 | + |
| 172 | + // Recursively repeat for any children |
| 173 | + metrics.children().forEach(child -> logPretty(guid, child, indent + PRETTY_INDENT_SIZE)); |
| 174 | + } |
| 175 | + |
| 176 | + private static int getMaxLen(List<String> strings) { |
| 177 | + int maxLen = 0; |
| 178 | + for (String str : strings) { |
| 179 | + maxLen = Math.max(maxLen, str.length()); |
| 180 | + } |
| 181 | + return maxLen; |
| 182 | + } |
| 183 | + |
| 184 | + private static String pad(String str, int length) { |
| 185 | + return str + repeat(" ", length - str.length()); |
39 | 186 | }
|
40 | 187 |
|
41 | 188 | @Override
|
|
0 commit comments