Skip to content

Commit

Permalink
Fix bugs with actuator data over HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Nov 9, 2022
1 parent 22b3733 commit a411663
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.springframework.ide.vscode.boot.java.livehover.v2;

import java.io.IOException;
import java.net.URI;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
Expand All @@ -28,8 +29,10 @@ public class HttpActuatorConnection implements ActuatorConnection {

private Gson gson;
private RestTemplate restTemplate;
private String actuatorUrl;

public HttpActuatorConnection(String actuatorUrl) {
this.actuatorUrl = actuatorUrl;
this.restTemplate = new RestTemplateBuilder().rootUri(actuatorUrl).build();
this.gson = new Gson();
}
Expand Down Expand Up @@ -80,11 +83,12 @@ public String getBeans() throws IOException {
@Override
public String getLiveMetrics(String metricName, String tags) throws IOException {
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromPath("/metrics/"+metricName);
if (tags != null) {
if (tags != null && !tags.isBlank()) {
uriBuilder.queryParam("tag", tags);
}
String url = uriBuilder.encode().toUriString();
return restTemplate.getForObject(url, String.class);
// /{ownerId}/new cases make REST Template URI template handler to think that {ownerId} is a URI parameter which it is not.
String url = actuatorUrl + uriBuilder.toUriString();
return restTemplate.getForObject(URI.create(url), String.class);
}

@Override
Expand All @@ -95,8 +99,9 @@ public String getMetrics(String metric, Map<String, String> tags) throws IOExcep
uriBuilder.queryParam("tag", e.getKey() + ":" + e.getValue());
}
}
String url = uriBuilder.encode().toUriString();
return restTemplate.getForObject(url, String.class);
// /{ownerId}/new cases make REST Template URI template handler to think that {ownerId} is a URI parameter which it is not.
String url = actuatorUrl + uriBuilder.toUriString();
return restTemplate.getForObject(URI.create(url), String.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
/*******************************************************************************
* Copyright (c) 2022 VMware, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* VMware, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.livehover.v2;

public class Measurements {

private String statistic;
private Long value;
private Double value;

public String getStatistic() {
return statistic;
}


public Long getValue() {
public Double getValue() {
return value;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*******************************************************************************
* Copyright (c) 2022 VMware, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* VMware, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.livehover.v2;

public enum ProcessType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,11 @@ private CompletableFuture<Object> handleLiveMetricsProcessRequest(ExecuteCommand
String metricName = getArgumentByKey(params, "metricName");
if (processKey != null) {
switch(metricName) {
case "gcPauses": {
case SpringProcessConnectorService.GC_PAUSES: {
SpringProcessGcPausesMetricsLiveData data = connectorService.getGcPausesMetricsLiveData(processKey);
return CompletableFuture.completedFuture(data.getGcPausesMetrics());
}
case "memory": {
case SpringProcessConnectorService.MEMORY: {
SpringProcessMemoryMetricsLiveData data = connectorService.getMemoryMetricsLiveData(processKey);
return CompletableFuture.completedFuture(data.getMemoryMetrics());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
*/
public class SpringProcessConnectorService {

private static final String METRICS = "metrics";
public static final String GC_PAUSES = "gcPauses";
public static final String MEMORY = "memory";

private static final Logger log = LoggerFactory.getLogger(SpringProcessConnectorService.class);

private final SpringProcessLiveDataProvider liveDataProvider;
Expand Down Expand Up @@ -171,8 +175,8 @@ private void scheduleConnect(ProgressTask progressTask, String processKey, Sprin
progressTask.progressDone();

refreshProcess(new SpringProcessParams(processKey, "", "", ""));
refreshProcess(new SpringProcessParams(processKey, "metrics", "memory", "area:heap"));
refreshProcess(new SpringProcessParams(processKey, "metrics", "gcPauses", ""));
refreshProcess(new SpringProcessParams(processKey, METRICS, MEMORY, "area:heap"));
refreshProcess(new SpringProcessParams(processKey, METRICS, GC_PAUSES, ""));
}
catch (Exception e) {
log.info("problem occured during process connect", e);
Expand Down Expand Up @@ -232,7 +236,7 @@ private void scheduleRefresh(ProgressTask progressTask, SpringProcessParams spri

try {
progressTask.progressEvent(progressMessage);
if(endpoint.equals("metrics") && metricName.equals("memory")) {
if(METRICS.equals(endpoint) && MEMORY.equals(metricName)) {
SpringProcessMemoryMetricsLiveData newMetricsLiveData = connector.refreshMemoryMetrics(this.liveDataProvider.getCurrent(processKey), metricName, springProcessParams.getTags());

if (newMetricsLiveData != null) {
Expand All @@ -243,7 +247,7 @@ private void scheduleRefresh(ProgressTask progressTask, SpringProcessParams spri
this.connectedSuccess.put(processKey, true);
}

} else if(endpoint.equals("metrics") && metricName.equals("gcPauses")) {
} else if(METRICS.equals(endpoint) && GC_PAUSES.equals(metricName)) {
SpringProcessGcPausesMetricsLiveData newMetricsLiveData = connector.refreshGcPausesMetrics(this.liveDataProvider.getCurrent(processKey), metricName, springProcessParams.getTags());

if (newMetricsLiveData != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.HttpClientErrorException;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -198,7 +200,7 @@ public SpringProcessGcPausesMetricsLiveData retrieveLiveGcPausesMetricsData(Proc
}
}

LiveMemoryMetricsModel metrics = getLiveMetrics(connection, metricName, tags);
LiveMemoryMetricsModel metrics = getLiveMetrics(connection, "jvm.gc.pause", tags);
if(metrics != null) {
memoryMetricsList.add(metrics);
}
Expand Down Expand Up @@ -240,7 +242,12 @@ public RequestMappingMetrics getRequestMappingMetrics(String[] paths, String[] r

return RequestMappingMetrics.parse(metricsData);
} catch (IOException e) {
// ignore
} catch (HttpClientErrorException e) {
if (e.getStatusCode() != null && e.getStatusCode() == HttpStatus.NOT_FOUND) {
// not found 404 - don't log it - means metric is unavailable
} else {
log.error("", e);
}
} catch (Exception e) {
log.error("", e);
}
Expand Down

0 comments on commit a411663

Please sign in to comment.