Skip to content

Commit

Permalink
Merge pull request #71 from arenadata/feature/ADBDEV-4581
Browse files Browse the repository at this point in the history
ADBDEV-4581: Implement monitoring metrics in PXF service
  • Loading branch information
xardazzzzzz authored Nov 29, 2023
2 parents 922d812 + 79d945b commit 242956a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void testServiceMetrics() throws Exception {
String expectedPxfRecordsSent = "4.0";
String actualPxfRecordsSent = "";
String json = IOUtils.toString(serviceMetricsUrl, StandardCharsets.UTF_8);
JSONArray metrics = new JSONArray(json);
JSONArray metrics = new JSONObject(json).getJSONArray("metrics");
for (int i = 0; i < metrics.length(); i++) {
if (metrics.getJSONObject(i).getString("name").equals("pxf.records.sent")) {
actualPxfRecordsSent = metrics.getJSONObject(i).getJSONArray("measurements").getJSONObject(0).getString("value");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.greenplum.pxf.service.rest.dto.ServiceMetricsDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.boot.actuate.metrics.MetricsEndpoint;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -29,7 +31,8 @@ public class ServiceMetricsRestController {
"pxf.bytes.sent",
"pxf.fragments.sent",
"pxf.records.received",
"pxf.records.sent"
"pxf.records.sent",
"process.uptime"
);
private static final Collection<String> CLUSTER_METRIC_NAMES = Arrays.asList(
"jvm.memory.committed",
Expand All @@ -43,21 +46,27 @@ public class ServiceMetricsRestController {
private final MetricsEndpoint metricsEndpoint;
private final String clusterName;
private final String hostName;
private final HealthEndpoint healthEndpoint;

public ServiceMetricsRestController(final MetricsEndpoint metricsEndpoint,
@Value("${cluster-name}") final String clusterName,
@Value("${eureka.instance.hostname}") final String hostName) {
@Value("${eureka.instance.hostname}") final String hostName,
final HealthEndpoint healthEndpoint) {
this.metricsEndpoint = metricsEndpoint;
this.clusterName = clusterName;
this.hostName = hostName;
this.healthEndpoint = healthEndpoint;
}

@GetMapping
public Collection<MetricsEndpoint.MetricResponse> get() {
return METRIC_NAMES.stream()
.map(name -> metricsEndpoint.metric(name, Collections.emptyList()))
.filter(Objects::nonNull)
.collect(Collectors.toList());
public ServiceMetricsDto get() {
return new ServiceMetricsDto(
healthEndpoint.health().getStatus().getCode(),
METRIC_NAMES.stream()
.map(name -> metricsEndpoint.metric(name, Collections.emptyList()))
.filter(Objects::nonNull)
.collect(Collectors.toList())
);
}

@GetMapping("/cluster-metrics")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.greenplum.pxf.service.rest.dto;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.actuate.metrics.MetricsEndpoint;

import java.util.Collection;

@Getter
@RequiredArgsConstructor
public class ServiceMetricsDto {
private final String status;
private final Collection<MetricsEndpoint.MetricResponse> metrics;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.greenplum.pxf.service.rest;

import org.greenplum.pxf.service.rest.dto.ServiceMetricsDto;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.health.HealthComponent;
import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.actuate.metrics.MetricsEndpoint;

import java.util.Collection;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
Expand All @@ -16,15 +18,23 @@ class ServiceMetricsRestControllerTest {
private static final String clusterName = "cluster name";
private static final String hostName = "host name";
private final MetricsEndpoint metricsEndpoint = mock(MetricsEndpoint.class);
private final ServiceMetricsRestController controller = new ServiceMetricsRestController(metricsEndpoint, clusterName, hostName);
private final HealthEndpoint healthEndpoint = mock(HealthEndpoint.class);
private final ServiceMetricsRestController controller = new ServiceMetricsRestController(metricsEndpoint, clusterName, hostName, healthEndpoint);

@Test
public void get() {
MetricsEndpoint.MetricResponse metricResponse = mock(MetricsEndpoint.MetricResponse.class);
when(metricsEndpoint.metric(anyString(), any())).thenReturn(metricResponse).thenReturn(null);
Collection<MetricsEndpoint.MetricResponse> result = controller.get();
assertEquals(1, result.size());
result.stream().findAny().filter(metricResponse::equals).orElseGet(Assertions::fail);
HealthComponent healthComponent = mock(HealthComponent.class);
Status status = mock(Status.class);
String code = "DOWN";
when(status.getCode()).thenReturn(code);
when(healthComponent.getStatus()).thenReturn(status);
when(healthEndpoint.health()).thenReturn(healthComponent);
ServiceMetricsDto result = controller.get();
assertEquals(code, result.getStatus());
assertEquals(1, result.getMetrics().size());
result.getMetrics().stream().findAny().filter(metricResponse::equals).orElseGet(Assertions::fail);
}

@Test
Expand Down

0 comments on commit 242956a

Please sign in to comment.