|
7 | 7 |
|
8 | 8 | package com.orange.lo.sample.lo2iothub;
|
9 | 9 |
|
10 |
| -import java.lang.invoke.MethodHandles; |
11 |
| -import java.time.Duration; |
12 |
| -import java.util.concurrent.Executors; |
13 |
| -import java.util.concurrent.ThreadFactory; |
14 |
| -import java.util.concurrent.TimeUnit; |
15 |
| - |
| 10 | +import com.orange.lo.sample.lo2iothub.utils.MetricsProperties; |
| 11 | +import io.micrometer.cloudwatch2.CloudWatchConfig; |
| 12 | +import io.micrometer.cloudwatch2.CloudWatchMeterRegistry; |
| 13 | +import io.micrometer.core.instrument.Clock; |
| 14 | +import io.micrometer.core.instrument.MeterRegistry; |
| 15 | +import io.micrometer.core.instrument.config.MeterFilter; |
16 | 16 | import org.slf4j.Logger;
|
17 | 17 | import org.slf4j.LoggerFactory;
|
18 | 18 | import org.springframework.beans.factory.annotation.Qualifier;
|
19 | 19 | import org.springframework.boot.SpringApplication;
|
20 | 20 | import org.springframework.boot.autoconfigure.SpringBootApplication;
|
21 | 21 | import org.springframework.context.annotation.Bean;
|
| 22 | +import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient; |
22 | 23 |
|
23 |
| -import io.micrometer.core.instrument.Clock; |
24 |
| -import io.micrometer.core.instrument.Counter; |
25 |
| -import io.micrometer.core.instrument.step.StepMeterRegistry; |
26 |
| -import io.micrometer.core.instrument.step.StepRegistryConfig; |
| 24 | +import java.lang.invoke.MethodHandles; |
27 | 25 |
|
28 | 26 | @SpringBootApplication
|
29 | 27 | public class ConnectorApplication {
|
30 | 28 |
|
31 | 29 | private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
|
| 30 | + private final MetricsProperties metricsProperties; |
| 31 | + |
| 32 | + ConnectorApplication(MetricsProperties metricsProperties) { |
| 33 | + this.metricsProperties = metricsProperties; |
| 34 | + } |
32 | 35 |
|
33 | 36 | public static void main(String[] args) {
|
34 | 37 | SpringApplication.run(ConnectorApplication.class, args);
|
35 | 38 | }
|
36 | 39 |
|
37 | 40 | @Bean
|
38 |
| - public StepRegistryConfig stepRegistryConfig() { |
39 |
| - return new StepRegistryConfig() { |
40 |
| - |
41 |
| - @Override |
42 |
| - public Duration step() { |
43 |
| - return Duration.ofMinutes(1); |
44 |
| - } |
| 41 | + @Qualifier("counters") |
| 42 | + public MeterRegistry meterRegistry() { |
| 43 | + CloudWatchMeterRegistry cloudWatchMeterRegistry = new CloudWatchMeterRegistry(cloudWatchConfig(), Clock.SYSTEM, CloudWatchAsyncClient.create()); |
| 44 | + cloudWatchMeterRegistry.config() |
| 45 | + .meterFilter(MeterFilter.deny(id -> !id.getName().startsWith("message"))) |
| 46 | + .commonTags(metricsProperties.getDimensionName(), metricsProperties.getDimensionValue()); |
| 47 | + return cloudWatchMeterRegistry; |
| 48 | + } |
45 | 49 |
|
46 |
| - @Override |
47 |
| - public String prefix() { |
48 |
| - return ""; |
49 |
| - } |
| 50 | + private CloudWatchConfig cloudWatchConfig() { |
| 51 | + return new CloudWatchConfig() { |
50 | 52 |
|
51 | 53 | @Override
|
52 | 54 | public String get(String key) {
|
53 | 55 | return null;
|
54 | 56 | }
|
55 |
| - }; |
56 |
| - } |
57 |
| - |
58 |
| - @Bean |
59 |
| - @Qualifier("counters") |
60 |
| - public StepMeterRegistry stepMeterRegistry() { |
61 |
| - return new StepMeterRegistry(stepRegistryConfig(), Clock.SYSTEM) { |
62 | 57 |
|
63 | 58 | @Override
|
64 |
| - protected TimeUnit getBaseTimeUnit() { |
65 |
| - return TimeUnit.MILLISECONDS; |
66 |
| - } |
67 |
| - |
68 |
| - @Override |
69 |
| - protected void publish() { |
70 |
| - getMeters().stream().filter(m -> m.getId().getName().startsWith("message")).map(m -> get(m.getId().getName()).counter()).forEach(c -> LOG.info(c.getId().getName() + " = " + val(c))); |
71 |
| - } |
72 |
| - |
73 |
| - @Override |
74 |
| - public void start(ThreadFactory threadFactory) { |
75 |
| - super.start(Executors.defaultThreadFactory()); |
| 59 | + public String namespace() { |
| 60 | + return metricsProperties.getNamespace(); |
76 | 61 | }
|
77 | 62 | };
|
78 | 63 | }
|
79 |
| - |
80 |
| - private long val(Counter cnt) { |
81 |
| - return Math.round(cnt.count()); |
82 |
| - } |
83 | 64 | }
|
0 commit comments