|
| 1 | +/** |
| 2 | + * Copyright (c) Orange. All Rights Reserved. |
| 3 | + * <p> |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.orange.lo.sample.mqtt2eventhub.utils; |
| 9 | + |
| 10 | +import io.micrometer.cloudwatch2.CloudWatchConfig; |
| 11 | +import io.micrometer.cloudwatch2.CloudWatchMeterRegistry; |
| 12 | +import io.micrometer.core.instrument.Clock; |
| 13 | +import io.micrometer.core.instrument.Counter; |
| 14 | +import io.micrometer.core.instrument.MeterRegistry; |
| 15 | +import io.micrometer.core.instrument.config.MeterFilter; |
| 16 | +import io.micrometer.core.instrument.step.StepMeterRegistry; |
| 17 | +import io.micrometer.core.instrument.step.StepRegistryConfig; |
| 18 | +import org.jetbrains.annotations.NotNull; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | +import org.springframework.context.annotation.Bean; |
| 22 | +import org.springframework.context.annotation.Configuration; |
| 23 | +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; |
| 24 | +import software.amazon.awssdk.auth.credentials.InstanceProfileCredentialsProvider; |
| 25 | +import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; |
| 26 | +import software.amazon.awssdk.regions.Region; |
| 27 | +import software.amazon.awssdk.regions.providers.AwsProfileRegionProvider; |
| 28 | +import software.amazon.awssdk.regions.providers.InstanceProfileRegionProvider; |
| 29 | +import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient; |
| 30 | + |
| 31 | +import java.lang.invoke.MethodHandles; |
| 32 | +import java.time.Duration; |
| 33 | +import java.util.concurrent.Executors; |
| 34 | +import java.util.concurrent.ThreadFactory; |
| 35 | +import java.util.concurrent.TimeUnit; |
| 36 | + |
| 37 | +@Configuration |
| 38 | +public class MeterRegistryConfig { |
| 39 | + |
| 40 | + private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); |
| 41 | + private static final String AWS_SERVICE_PROFILE_NAME = "service-profile"; |
| 42 | + private final MetricsProperties metricsProperties; |
| 43 | + |
| 44 | + public MeterRegistryConfig(MetricsProperties metricsProperties) { |
| 45 | + this.metricsProperties = metricsProperties; |
| 46 | + } |
| 47 | + |
| 48 | + @Bean |
| 49 | + public MeterRegistry meterRegistry() { |
| 50 | + MeterRegistry meterRegistry; |
| 51 | + if (metricsProperties.isSendToCloudwatch()) { |
| 52 | + meterRegistry = getCloudWatchMeterRegistry(); |
| 53 | + } else { |
| 54 | + meterRegistry = stepMeterRegistry(); |
| 55 | + } |
| 56 | + return meterRegistry; |
| 57 | + } |
| 58 | + |
| 59 | + @NotNull |
| 60 | + private CloudWatchMeterRegistry getCloudWatchMeterRegistry() { |
| 61 | + CloudWatchAsyncClient cloudWatchAsyncClient = CloudWatchAsyncClient.builder() |
| 62 | + .credentialsProvider(getAwsCredentialsProvider()) |
| 63 | + .region(getRegion()) |
| 64 | + .build(); |
| 65 | + |
| 66 | + CloudWatchMeterRegistry cloudWatchMeterRegistry = new CloudWatchMeterRegistry(cloudWatchConfig(), Clock.SYSTEM, cloudWatchAsyncClient); |
| 67 | + |
| 68 | + cloudWatchMeterRegistry.config() |
| 69 | + .meterFilter(MeterFilter.deny(id -> !id.getName().startsWith("message"))) |
| 70 | + .commonTags(metricsProperties.getDimensionName(), metricsProperties.getDimensionValue()); |
| 71 | + return cloudWatchMeterRegistry; |
| 72 | + } |
| 73 | + |
| 74 | + private AwsCredentialsProvider getAwsCredentialsProvider() { |
| 75 | + return metricsProperties.isUseServiceProfile() |
| 76 | + ? ProfileCredentialsProvider.create(AWS_SERVICE_PROFILE_NAME) |
| 77 | + : InstanceProfileCredentialsProvider.create(); |
| 78 | + } |
| 79 | + |
| 80 | + private Region getRegion() { |
| 81 | + return metricsProperties.isUseServiceProfile() |
| 82 | + ? new AwsProfileRegionProvider(null, AWS_SERVICE_PROFILE_NAME).getRegion() |
| 83 | + : new InstanceProfileRegionProvider().getRegion(); |
| 84 | + } |
| 85 | + |
| 86 | + private CloudWatchConfig cloudWatchConfig() { |
| 87 | + return new CloudWatchConfig() { |
| 88 | + |
| 89 | + @Override |
| 90 | + public String get(@NotNull String key) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + @NotNull |
| 95 | + @Override |
| 96 | + public String namespace() { |
| 97 | + return metricsProperties.getNamespace(); |
| 98 | + } |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + private StepMeterRegistry stepMeterRegistry() { |
| 103 | + StepMeterRegistry stepMeterRegistry = new StepMeterRegistry(stepRegistryConfig(), Clock.SYSTEM) { |
| 104 | + |
| 105 | + @NotNull |
| 106 | + @Override |
| 107 | + protected TimeUnit getBaseTimeUnit() { |
| 108 | + return TimeUnit.MILLISECONDS; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + protected void publish() { |
| 113 | + getMeters().stream() |
| 114 | + .filter(m -> m.getId().getName().startsWith("message") ) |
| 115 | + .map(m -> get(m.getId().getName()).counter()) |
| 116 | + .forEach(c -> LOG.info(c.getId().getName() + " = " + val(c))); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void start(@NotNull ThreadFactory threadFactory) { |
| 121 | + super.start(Executors.defaultThreadFactory()); |
| 122 | + } |
| 123 | + }; |
| 124 | + stepMeterRegistry.start(Executors.defaultThreadFactory()); |
| 125 | + return stepMeterRegistry; |
| 126 | + } |
| 127 | + |
| 128 | + private StepRegistryConfig stepRegistryConfig() { |
| 129 | + return new StepRegistryConfig() { |
| 130 | + |
| 131 | + @NotNull |
| 132 | + @Override |
| 133 | + public Duration step() { |
| 134 | + return Duration.ofMinutes(1); |
| 135 | + } |
| 136 | + |
| 137 | + @NotNull |
| 138 | + @Override |
| 139 | + public String prefix() { |
| 140 | + return ""; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public String get(@NotNull String key) { |
| 145 | + return null; |
| 146 | + } |
| 147 | + }; |
| 148 | + } |
| 149 | + |
| 150 | + private long val(Counter cnt) { |
| 151 | + return Math.round(cnt.count()); |
| 152 | + } |
| 153 | +} |
0 commit comments