Skip to content

Commit 91dd2cf

Browse files
Aws metrics v2
1 parent d55adc1 commit 91dd2cf

File tree

5 files changed

+177
-61
lines changed

5 files changed

+177
-61
lines changed
Lines changed: 6 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,19 @@
1-
/**
2-
* Copyright (c) Orange. All Rights Reserved.
3-
*
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-
*/
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+
*/
77

88
package com.orange.lo.sample.lo2iothub;
99

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-
import org.slf4j.Logger;
17-
import org.slf4j.LoggerFactory;
18-
import org.springframework.beans.factory.annotation.Qualifier;
1910
import org.springframework.boot.SpringApplication;
2011
import org.springframework.boot.autoconfigure.SpringBootApplication;
21-
import org.springframework.context.annotation.Bean;
22-
23-
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
24-
import software.amazon.awssdk.regions.providers.AwsProfileRegionProvider;
25-
import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient;
26-
27-
import java.lang.invoke.MethodHandles;
2812

2913
@SpringBootApplication
3014
public class ConnectorApplication {
3115

32-
private static final String SERVICE_PROFILE_NAME = "service-profile";
33-
private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
34-
private final MetricsProperties metricsProperties;
35-
36-
ConnectorApplication(MetricsProperties metricsProperties) {
37-
this.metricsProperties = metricsProperties;
38-
}
39-
4016
public static void main(String[] args) {
4117
SpringApplication.run(ConnectorApplication.class, args);
4218
}
43-
44-
@Bean
45-
@Qualifier("counters")
46-
public MeterRegistry meterRegistry() {
47-
CloudWatchAsyncClient cloudWatchAsyncClient = CloudWatchAsyncClient.builder()
48-
.credentialsProvider(ProfileCredentialsProvider.create(SERVICE_PROFILE_NAME))
49-
.region(new AwsProfileRegionProvider(null, SERVICE_PROFILE_NAME).getRegion())
50-
.build();
51-
52-
CloudWatchMeterRegistry cloudWatchMeterRegistry = new CloudWatchMeterRegistry(cloudWatchConfig(), Clock.SYSTEM, cloudWatchAsyncClient);
53-
54-
cloudWatchMeterRegistry.config()
55-
.meterFilter(MeterFilter.deny(id -> !id.getName().startsWith("message")))
56-
.commonTags(metricsProperties.getDimensionName(), metricsProperties.getDimensionValue());
57-
return cloudWatchMeterRegistry;
58-
}
59-
60-
private CloudWatchConfig cloudWatchConfig() {
61-
return new CloudWatchConfig() {
62-
63-
@Override
64-
public String get(String key) {
65-
return null;
66-
}
67-
68-
@Override
69-
public String namespace() {
70-
return metricsProperties.getNamespace();
71-
}
72-
};
73-
}
7419
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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.lo2iothub.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.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
import org.springframework.beans.factory.annotation.Qualifier;
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+
@Qualifier("counters")
50+
public MeterRegistry meterRegistry() {
51+
MeterRegistry meterRegistry;
52+
if (metricsProperties.isSendToCloudwatch()) {
53+
meterRegistry = getCloudWatchMeterRegistry();
54+
} else {
55+
meterRegistry = stepMeterRegistry();
56+
}
57+
return meterRegistry;
58+
}
59+
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(String key) {
91+
return null;
92+
}
93+
94+
@Override
95+
public String namespace() {
96+
return metricsProperties.getNamespace();
97+
}
98+
};
99+
}
100+
101+
private StepMeterRegistry stepMeterRegistry() {
102+
StepMeterRegistry stepMeterRegistry = new StepMeterRegistry(stepRegistryConfig(), Clock.SYSTEM) {
103+
104+
@Override
105+
protected TimeUnit getBaseTimeUnit() {
106+
return TimeUnit.MILLISECONDS;
107+
}
108+
109+
@Override
110+
protected void publish() {
111+
getMeters().stream()
112+
.filter(m -> m.getId().getName().startsWith("message"))
113+
.map(m -> get(m.getId().getName()).counter())
114+
.forEach(c -> LOG.info("{} = {}", c.getId().getName(), val(c)));
115+
}
116+
117+
@Override
118+
public void start(ThreadFactory threadFactory) {
119+
super.start(Executors.defaultThreadFactory());
120+
}
121+
};
122+
stepMeterRegistry.start(Executors.defaultThreadFactory());
123+
return stepMeterRegistry;
124+
}
125+
126+
private StepRegistryConfig stepRegistryConfig() {
127+
return new StepRegistryConfig() {
128+
129+
@Override
130+
public Duration step() {
131+
return Duration.ofMinutes(1);
132+
}
133+
134+
@Override
135+
public String prefix() {
136+
return "";
137+
}
138+
139+
@Override
140+
public String get(String key) {
141+
return null;
142+
}
143+
};
144+
}
145+
146+
private long val(Counter cnt) {
147+
return Math.round(cnt.count());
148+
}
149+
}

src/main/java/com/orange/lo/sample/lo2iothub/utils/MetricsProperties.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public class MetricsProperties {
99
private String namespace;
1010
private String dimensionName;
1111
private String dimensionValue;
12+
private boolean sendToCloudwatch;
13+
private boolean useServiceProfile;
1214

1315
public String getNamespace() {
1416
return namespace;
@@ -33,4 +35,20 @@ public String getDimensionValue() {
3335
public void setDimensionValue(String dimensionValue) {
3436
this.dimensionValue = dimensionValue;
3537
}
38+
39+
public boolean isSendToCloudwatch() {
40+
return sendToCloudwatch;
41+
}
42+
43+
public void setSendToCloudwatch(boolean sendToCloudwatch) {
44+
this.sendToCloudwatch = sendToCloudwatch;
45+
}
46+
47+
public boolean isUseServiceProfile() {
48+
return useServiceProfile;
49+
}
50+
51+
public void setUseServiceProfile(boolean useServiceProfile) {
52+
this.useServiceProfile = useServiceProfile;
53+
}
3654
}

src/main/resources/application.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ azure:
5353
instrumentation-key: YOUR_INSTMENTATION_KEY
5454

5555
metrics:
56+
send-to-cloudwatch: false
57+
use-service-profile: false
5658
namespace: CCS
5759
dimension-name: connector-id
5860
dimension-value:

src/main/resources/logback.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
<logger name="com.microsoft.azure" level="ERROR" />
4141
<logger name="org.apache.qpid" level="FINE"/>
4242
<logger name="proton.trace" level="FINE" />
43+
<Logger name="org.apache.catalina.core" level="WARN" />
44+
<Logger name="org.springframework.boot.web.embedded.tomcat" level="WARN" />
4345

4446
<root level="INFO">
4547
<appender-ref ref="STDOUT" />

0 commit comments

Comments
 (0)