Search in sources :

Example 1 with ClientConfigMetricResponse

use of com.alibaba.nacos.api.config.remote.response.ClientConfigMetricResponse in project nacos by alibaba.

the class ClientMetricsControllerTest method testGetCurrentMetric.

@Test
public void testGetCurrentMetric() throws Exception {
    ClientConfigMetricResponse response = new ClientConfigMetricResponse();
    response.putMetric("test", "test");
    Connection connection = Mockito.mock(Connection.class);
    when(connection.request(any(), anyLong())).thenReturn(response);
    List<Connection> connections = new ArrayList<>();
    connections.add(connection);
    when(connectionManager.getConnectionByIp(eq("127.0.0.1"))).thenReturn(connections);
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.METRICS_CONTROLLER_PATH + "/current").param("ip", "127.0.0.1").param("tenant", "test").param("dataId", "test").param("group", "test");
    String actualValue = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
    Assert.assertEquals("{\"test\":\"test\"}", actualValue);
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ClientConfigMetricResponse(com.alibaba.nacos.api.config.remote.response.ClientConfigMetricResponse) Connection(com.alibaba.nacos.core.remote.Connection) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with ClientConfigMetricResponse

use of com.alibaba.nacos.api.config.remote.response.ClientConfigMetricResponse in project nacos by alibaba.

the class ClientMetricsController method getClientMetrics.

/**
 * Get client config listener lists of subscriber in local machine.
 */
@GetMapping("/current")
public Map<String, Object> getClientMetrics(@RequestParam("ip") String ip, @RequestParam(value = "dataId", required = false) String dataId, @RequestParam(value = "group", required = false) String group, @RequestParam(value = "tenant", required = false) String tenant) {
    Map<String, Object> metrics = new HashMap<>(16);
    List<Connection> connectionsByIp = connectionManager.getConnectionByIp(ip);
    for (Connection connectionByIp : connectionsByIp) {
        try {
            ClientConfigMetricRequest clientMetrics = new ClientConfigMetricRequest();
            if (StringUtils.isNotBlank(dataId)) {
                clientMetrics.getMetricsKeys().add(ClientConfigMetricRequest.MetricsKey.build(CACHE_DATA, GroupKey2.getKey(dataId, group, tenant)));
                clientMetrics.getMetricsKeys().add(ClientConfigMetricRequest.MetricsKey.build(SNAPSHOT_DATA, GroupKey2.getKey(dataId, group, tenant)));
            }
            ClientConfigMetricResponse request1 = (ClientConfigMetricResponse) connectionByIp.request(clientMetrics, 1000L);
            metrics.putAll(request1.getMetrics());
        } catch (Exception e) {
            Loggers.CORE.error("Get config metrics error from client ip={},dataId={},group={},tenant={},error={}", ip, dataId, group, tenant, e);
        }
    }
    return metrics;
}
Also used : ClientConfigMetricRequest(com.alibaba.nacos.api.config.remote.request.ClientConfigMetricRequest) HashMap(java.util.HashMap) Connection(com.alibaba.nacos.core.remote.Connection) ClientConfigMetricResponse(com.alibaba.nacos.api.config.remote.response.ClientConfigMetricResponse) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

ClientConfigMetricResponse (com.alibaba.nacos.api.config.remote.response.ClientConfigMetricResponse)2 Connection (com.alibaba.nacos.core.remote.Connection)2 ClientConfigMetricRequest (com.alibaba.nacos.api.config.remote.request.ClientConfigMetricRequest)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1