Skip to content

Commit

Permalink
Remove warnings raised on sonar analyses
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciogeneroso committed Jun 24, 2024
1 parent ffe1871 commit 96b162b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package com.generoso.ft.sd.client;

import org.hawkular.agent.prometheus.PrometheusMetricsProcessor;
import org.hawkular.agent.prometheus.text.TextPrometheusMetricsProcessor;
import org.hawkular.agent.prometheus.types.MetricFamily;
import org.hawkular.agent.prometheus.walkers.CollectorPrometheusMetricsWalker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.List;

Expand All @@ -24,19 +20,15 @@ public class MetricsClientRequest {
private Client client;

public List<MetricFamily> collectMetrics() {
try {
HttpResponse<String> response = client.execute(privateMetricsRequestTemplate);
return parsePrometheusMetrics(response.body());
} catch (IOException e) {
throw new RuntimeException("Unable to gather metrics", e);
}
var response = client.execute(privateMetricsRequestTemplate);
return parsePrometheusMetrics(response.body());
}

private List<MetricFamily> parsePrometheusMetrics(String metricEndpointResponseBody) throws IOException {
InputStream inputStream = new ByteArrayInputStream(metricEndpointResponseBody.getBytes(StandardCharsets.UTF_8));
private List<MetricFamily> parsePrometheusMetrics(String metricEndpointResponseBody) {
var inputStream = new ByteArrayInputStream(metricEndpointResponseBody.getBytes(StandardCharsets.UTF_8));

CollectorPrometheusMetricsWalker collector = new CollectorPrometheusMetricsWalker();
PrometheusMetricsProcessor<?> processor = new TextPrometheusMetricsProcessor(inputStream, collector);
var collector = new CollectorPrometheusMetricsWalker();
var processor = new TextPrometheusMetricsProcessor(inputStream, collector);
processor.walk();

return collector.getAllMetricFamilies();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.generoso.ft.sd.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.generoso.ft.sd.client.model.Endpoint;
import com.generoso.ft.sd.client.RequestTemplate;
import com.generoso.ft.sd.client.model.Endpoint;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.net.http.HttpClient;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

Expand All @@ -29,16 +29,18 @@ public HttpClient httpClient() {

@Bean
public Map<Endpoint, RequestTemplate> privateRequestTemplates(
@Qualifier("private") List<RequestTemplate> templates) {
var map = new HashMap<Endpoint, RequestTemplate>(templates.size());
templates.forEach(t -> map.put(t.getEndpoint(), t));
return map;
@Qualifier("private") List<RequestTemplate> templates) {
return buildMap(templates);
}

@Bean
public Map<Endpoint, RequestTemplate> requestTemplates(
@Qualifier("service-request") List<RequestTemplate> templates) {
var map = new HashMap<Endpoint, RequestTemplate>(templates.size());
@Qualifier("service-request") List<RequestTemplate> templates) {
return buildMap(templates);
}

private Map<Endpoint, RequestTemplate> buildMap(@Qualifier("service-request") List<RequestTemplate> templates) {
var map = new EnumMap<Endpoint, RequestTemplate>(Endpoint.class);
templates.forEach(t -> map.put(t.getEndpoint(), t));
return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,52 +56,54 @@ private RequestTemplate getRequestTemplate(Endpoint endpoint) {

static {
bodyMap = new HashMap<>();
bodyMap.put("APP_NAME", "{\n" +
" \"instance\": {\n" +
" \"instanceId\": \"5784ed89d3cf:gateway:8080\",\n" +
" \"app\": \"APP_NAME\",\n" +
" \"appGroupName\": null,\n" +
" \"ipAddr\": \"172.20.0.3\",\n" +
" \"sid\": \"na\",\n" +
" \"homePageUrl\": \"http://172.20.0.3:8080/\",\n" +
" \"statusPageUrl\": \"http://172.20.0.3:8080/actuator/info\",\n" +
" \"healthCheckUrl\": \"http://172.20.0.3:8080/actuator/health\",\n" +
" \"secureHealthCheckUrl\": null,\n" +
" \"vipAddress\": \"app_name\",\n" +
" \"secureVipAddress\": \"app_name\",\n" +
" \"countryId\": 1,\n" +
" \"dataCenterInfo\": {\n" +
" \"@class\": \"com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo\",\n" +
" \"name\": \"MyOwn\"\n" +
" },\n" +
" \"hostName\": \"172.20.0.3\",\n" +
" \"status\": \"UP\",\n" +
" \"overriddenStatus\": \"UNKNOWN\",\n" +
" \"leaseInfo\": {\n" +
" \"renewalIntervalInSecs\": 30,\n" +
" \"durationInSecs\": 90,\n" +
" \"registrationTimestamp\": 0,\n" +
" \"lastRenewalTimestamp\": 0,\n" +
" \"evictionTimestamp\": 0,\n" +
" \"serviceUpTimestamp\": 0\n" +
" },\n" +
" \"isCoordinatingDiscoveryServer\": false,\n" +
" \"lastUpdatedTimestamp\": 1658954363496,\n" +
" \"lastDirtyTimestamp\": 1658954365828,\n" +
" \"actionType\": null,\n" +
" \"asgName\": null,\n" +
" \"port\": {\n" +
" \"$\": 8080,\n" +
" \"@enabled\": \"true\"\n" +
" },\n" +
" \"securePort\": {\n" +
" \"$\": 443,\n" +
" \"@enabled\": \"false\"\n" +
" },\n" +
" \"metadata\": {\n" +
" \"management.port\": \"8080\"\n" +
" }\n" +
" }\n" +
"}");
bodyMap.put("APP_NAME", """
{
"instance": {
"instanceId": "5784ed89d3cf:gateway:8080",
"app": "APP_NAME",
"appGroupName": null,
"ipAddr": "172.20.0.3",
"sid": "na",
"homePageUrl": "http://172.20.0.3:8080/",
"statusPageUrl": "http://172.20.0.3:8080/actuator/info",
"healthCheckUrl": "http://172.20.0.3:8080/actuator/health",
"secureHealthCheckUrl": null,
"vipAddress": "app_name",
"secureVipAddress": "app_name",
"countryId": 1,
"dataCenterInfo": {
"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
"name": "MyOwn"
},
"hostName": "172.20.0.3",
"status": "UP",
"overriddenStatus": "UNKNOWN",
"leaseInfo": {
"renewalIntervalInSecs": 30,
"durationInSecs": 90,
"registrationTimestamp": 0,
"lastRenewalTimestamp": 0,
"evictionTimestamp": 0,
"serviceUpTimestamp": 0
},
"isCoordinatingDiscoveryServer": false,
"lastUpdatedTimestamp": 1658954363496,
"lastDirtyTimestamp": 1658954365828,
"actionType": null,
"asgName": null,
"port": {
"$": 8080,
"@enabled": "true"
},
"securePort": {
"$": 443,
"@enabled": "false"
},
"metadata": {
"management.port": "8080"
}
}
}
""");
}
}
2 changes: 1 addition & 1 deletion service-discovery-ft/src/test/resources/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spring:
jmx:
enable: false
enabled: false
application:
name: sd-functional-tests
profiles:
Expand Down

0 comments on commit 96b162b

Please sign in to comment.