Skip to content

Commit d255d9a

Browse files
Switch Custom Metrics Shipment (#109)
* Switch Custom Metrics Shipment SAP BTP Application Logging has been changed to accept custom metrics within the log message stream. For this the messages need to be in JSON format and contain a field "type":"metrics" and an array of metrics in field "metrics". The prior approach using an Http endpoint is deprecated for removal. This commit changes cf-java-logging-support to provide custom metrics according to the new format by writing the correct JSON to `System.out`. * Run Formatter and Cleanup over changed files This commit contains changes by applying the formatter and cleanup configuration across all files of the PR.
1 parent 913caac commit d255d9a

File tree

16 files changed

+222
-692
lines changed

16 files changed

+222
-692
lines changed

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-core/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
<name>cf-java-monitoring-custom-metrics-clients-core</name>
1313

1414
<dependencies>
15-
<dependency>
16-
<groupId>org.apache.httpcomponents</groupId>
17-
<artifactId>httpclient</artifactId>
18-
</dependency>
1915
<dependency>
2016
<groupId>com.google.code.gson</groupId>
2117
<artifactId>gson</artifactId>

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-core/src/main/java/com/sap/cloud/cf/monitoring/client/MonitoringClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ public interface MonitoringClient {
99

1010
/**
1111
* Send single metric to the Monitoring service
12-
*
12+
*
1313
* @param metric
1414
* @throws MonitoringClientException
1515
*/
1616
void send(Metric metric) throws MonitoringClientException;
1717

1818
/**
1919
* Send list of metrics to the Monitoring service
20-
*
20+
*
2121
* @param metrics
2222
* @throws MonitoringClientException
2323
*/
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,19 @@
11
package com.sap.cloud.cf.monitoring.client;
22

33
import static com.sap.cloud.cf.monitoring.client.utils.Utils.checkNotNull;
4-
import static java.lang.String.format;
54

6-
import java.io.IOException;
7-
import java.io.UnsupportedEncodingException;
85
import java.util.Arrays;
96
import java.util.List;
107

11-
import org.apache.http.Header;
12-
import org.apache.http.HttpEntity;
13-
import org.apache.http.HttpHeaders;
14-
import org.apache.http.HttpStatus;
15-
import org.apache.http.auth.AuthenticationException;
16-
import org.apache.http.auth.UsernamePasswordCredentials;
17-
import org.apache.http.client.methods.CloseableHttpResponse;
18-
import org.apache.http.client.methods.HttpPost;
19-
import org.apache.http.entity.StringEntity;
20-
import org.apache.http.impl.auth.BasicScheme;
21-
import org.apache.http.impl.client.CloseableHttpClient;
22-
import org.apache.http.impl.client.HttpClients;
23-
import org.apache.http.util.EntityUtils;
24-
import org.slf4j.Logger;
25-
import org.slf4j.LoggerFactory;
26-
278
import com.google.gson.Gson;
28-
import com.sap.cloud.cf.monitoring.client.configuration.ConfigurationProvider;
299
import com.sap.cloud.cf.monitoring.client.exceptions.MonitoringClientException;
3010
import com.sap.cloud.cf.monitoring.client.model.Metric;
31-
import com.sap.cloud.cf.monitoring.client.model.api.MetricRequest;
11+
import com.sap.cloud.cf.monitoring.client.model.MetricEnvelope;
3212

3313
public class MonitoringClientBuilder {
34-
private static final Logger LOG = LoggerFactory.getLogger(MonitoringClientBuilder.class);
35-
36-
private ConfigurationProvider provider;
37-
private CloseableHttpClient client = null;
38-
39-
public MonitoringClientBuilder setConfigurationProvider(ConfigurationProvider provider) {
40-
checkNotNull(provider, "configuration provider");
41-
this.provider = provider;
42-
return this;
43-
}
44-
45-
public MonitoringClientBuilder setHttpClient(CloseableHttpClient client) {
46-
checkNotNull(client, "http client");
47-
this.client = client;
48-
return this;
49-
}
5014

5115
public MonitoringClient create() {
52-
if (this.client == null) {
53-
return new MonitoringClientImpl(this.provider);
54-
}
55-
return new MonitoringClientImpl(this.provider, this.client);
16+
return new MonitoringClientImpl();
5617
}
5718

5819
static class MonitoringClientImpl implements MonitoringClient {
@@ -61,25 +22,6 @@ static class MonitoringClientImpl implements MonitoringClient {
6122

6223
private static final Gson gson = new Gson();
6324

64-
private ConfigurationProvider configurationProvider;
65-
private CloseableHttpClient client;
66-
private UsernamePasswordCredentials credentials;
67-
68-
private static CloseableHttpClient createHttpClient(ConfigurationProvider provider) {
69-
return HttpClients.custom().useSystemProperties().build();
70-
}
71-
72-
MonitoringClientImpl(ConfigurationProvider provider) {
73-
this(provider, createHttpClient(provider));
74-
}
75-
76-
MonitoringClientImpl(ConfigurationProvider provider, CloseableHttpClient client) {
77-
checkNotNull(provider, "configuration provider");
78-
checkNotNull(client, "client");
79-
this.configurationProvider = provider;
80-
this.client = client;
81-
}
82-
8325
@Override
8426
public void send(Metric metric) throws MonitoringClientException {
8527
checkNotNull(metric, "metric");
@@ -92,77 +34,9 @@ public void send(List<Metric> metrics) throws MonitoringClientException {
9234
if (metrics.isEmpty()) {
9335
throw new IllegalArgumentException("The list of metrics cannot be empty");
9436
}
95-
MetricRequest metricRequest = createMetricRequest(metrics);
96-
String requestURL = createRequestURL();
97-
processRequest(requestURL, metricRequest);
98-
}
99-
100-
private void processRequest(String requestURL, MetricRequest metricRequest) {
101-
HttpPost request = new HttpPost(requestURL);
102-
request.setEntity(createEntity(metricRequest));
103-
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
104-
request.addHeader(basicSchema(request));
105-
106-
try (CloseableHttpResponse response = client.execute(request)) {
107-
checkResponseCode(response);
108-
EntityUtils.consumeQuietly(response.getEntity());
109-
} catch (IOException e) {
110-
throw new MonitoringClientException(
111-
"Error occured while trying to send metrics to the monitoring service", e);
112-
}
113-
}
114-
115-
private Header basicSchema(HttpPost request) {
116-
try {
117-
return new BasicScheme().authenticate(getCredentials(configurationProvider), request, null);
118-
} catch (AuthenticationException e) {
119-
LOG.error("Could not initialize BasicSchema");
120-
}
121-
return null;
122-
}
123-
124-
private UsernamePasswordCredentials getCredentials(ConfigurationProvider provider) {
125-
if (credentials == null) {
126-
credentials =
127-
new UsernamePasswordCredentials(provider.getClientId(), new String(provider.getClientSecret()));
128-
}
129-
return credentials;
130-
}
131-
132-
private void checkResponseCode(CloseableHttpResponse response) {
133-
int statusCode = response.getStatusLine().getStatusCode();
134-
if (HttpStatus.SC_CREATED != statusCode) {
135-
String message = extractErrorMessage(response);
136-
throw new MonitoringClientException(format(
137-
"Unexpected response code from monitoring service: %d, message: %s", statusCode, message));
138-
}
139-
}
140-
141-
private String extractErrorMessage(CloseableHttpResponse response) {
142-
try {
143-
return EntityUtils.toString(response.getEntity());
144-
} catch (Exception e) { //NOSONAR
145-
LOG.warn("Cannot extract the error message from the response", e);
146-
return "";
147-
}
148-
}
149-
150-
private HttpEntity createEntity(MetricRequest metricRequest) {
151-
try {
152-
return new StringEntity(gson.toJson(metricRequest));
153-
} catch (UnsupportedEncodingException e) {
154-
throw new MonitoringClientException("Unable to create request entity", e);
155-
}
156-
}
157-
158-
private String createRequestURL() {
159-
return String.format(REQUEST_URL_TEMPLATE, configurationProvider.getUrl(),
160-
configurationProvider.getApplicationGUID(), configurationProvider.getInstanceGUID());
161-
}
162-
163-
private MetricRequest createMetricRequest(List<Metric> metrics) {
164-
return new MetricRequest(configurationProvider.getApplicationGUID(),
165-
configurationProvider.getInstanceGUID(), configurationProvider.getInstanceIndex(), metrics);
37+
MetricEnvelope envelope = new MetricEnvelope(metrics);
38+
String message = gson.toJson(envelope);
39+
System.out.println(message);
16640
}
16741
}
16842
}

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-core/src/main/java/com/sap/cloud/cf/monitoring/client/configuration/CFConfigurationProvider.java

Lines changed: 0 additions & 140 deletions
This file was deleted.

cf-java-monitoring-custom-metrics-clients/cf-custom-metrics-clients-core/src/main/java/com/sap/cloud/cf/monitoring/client/configuration/ConfigurationProvider.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)