Search in sources :

Example 1 with ProxyTopicStat

use of org.apache.pulsar.websocket.stats.ProxyTopicStat in project incubator-pulsar by apache.

the class WebSocketProxyStats method getStat.

public ProxyTopicStat getStat(String topicName) {
    if (!service().getProducers().containsKey(topicName) && !service().getConsumers().containsKey(topicName) && !service().getReaders().containsKey(topicName)) {
        LOG.warn("topic doesn't exist {}", topicName);
        throw new RestException(Status.NOT_FOUND, "Topic does not exist");
    }
    ProxyTopicStat topicStat = new ProxyTopicStat();
    if (service().getProducers().containsKey(topicName)) {
        service().getProducers().get(topicName).forEach(handler -> {
            ProducerStats stat = new ProducerStats(handler);
            topicStat.producerStats.add(stat);
        });
    }
    if (service().getConsumers().containsKey(topicName)) {
        service().getConsumers().get(topicName).forEach(handler -> {
            topicStat.consumerStats.add(new ConsumerStats(handler));
        });
    }
    if (service().getReaders().containsKey(topicName)) {
        service().getReaders().get(topicName).forEach(handler -> {
            topicStat.consumerStats.add(new ConsumerStats(handler));
        });
    }
    return topicStat;
}
Also used : ProxyTopicStat(org.apache.pulsar.websocket.stats.ProxyTopicStat) ProducerStats(org.apache.pulsar.websocket.stats.ProxyTopicStat.ProducerStats) ConsumerStats(org.apache.pulsar.websocket.stats.ProxyTopicStat.ConsumerStats)

Example 2 with ProxyTopicStat

use of org.apache.pulsar.websocket.stats.ProxyTopicStat in project incubator-pulsar by apache.

the class WebSocketProxyStats method getStats.

@GET
@Path("/{property}/{cluster}/{namespace}/{topic}/stats")
@ApiOperation(value = "Get the stats for the topic.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Topic does not exist") })
public ProxyTopicStat getStats(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, @PathParam("topic") @Encoded String topic) {
    topic = decode(topic);
    TopicName topicName = TopicName.get("persistent", property, cluster, namespace, topic);
    validateUserAccess(topicName);
    ProxyTopicStat stats = getStat(topicName.toString());
    if (stats == null) {
        throw new RestException(Status.NOT_FOUND, "Topic does not exist");
    }
    return stats;
}
Also used : ProxyTopicStat(org.apache.pulsar.websocket.stats.ProxyTopicStat) TopicName(org.apache.pulsar.common.naming.TopicName) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with ProxyTopicStat

use of org.apache.pulsar.websocket.stats.ProxyTopicStat in project incubator-pulsar by apache.

the class WebSocketProxyStats method getStat.

public Map<String, ProxyTopicStat> getStat() {
    Map<String, ProxyTopicStat> statMap = Maps.newHashMap();
    service().getProducers().forEach((topicName, handlers) -> {
        ProxyTopicStat topicStat = statMap.computeIfAbsent(topicName, t -> new ProxyTopicStat());
        handlers.forEach(handler -> topicStat.producerStats.add(new ProducerStats(handler)));
        statMap.put(topicName, topicStat);
    });
    service().getConsumers().forEach((topicName, handlers) -> {
        ProxyTopicStat topicStat = statMap.computeIfAbsent(topicName, t -> new ProxyTopicStat());
        handlers.forEach(handler -> topicStat.consumerStats.add(new ConsumerStats(handler)));
        statMap.put(topicName, topicStat);
    });
    service().getReaders().forEach((topicName, handlers) -> {
        ProxyTopicStat topicStat = statMap.computeIfAbsent(topicName, t -> new ProxyTopicStat());
        handlers.forEach(handler -> topicStat.consumerStats.add(new ConsumerStats(handler)));
        statMap.put(topicName, topicStat);
    });
    return statMap;
}
Also used : ProxyTopicStat(org.apache.pulsar.websocket.stats.ProxyTopicStat) ProducerStats(org.apache.pulsar.websocket.stats.ProxyTopicStat.ProducerStats) ConsumerStats(org.apache.pulsar.websocket.stats.ProxyTopicStat.ConsumerStats)

Example 4 with ProxyTopicStat

use of org.apache.pulsar.websocket.stats.ProxyTopicStat in project incubator-pulsar by apache.

the class ProxyPublishConsumeTest method verifyProxyStats.

private void verifyProxyStats(Client client, String baseUrl, String topic) {
    String statUrl = baseUrl + "stats";
    WebTarget webTarget = client.target(statUrl);
    Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
    Response response = (Response) invocationBuilder.get();
    String responseStr = response.readEntity(String.class);
    final Gson gson = new Gson();
    final Map<String, ProxyTopicStat> data = gson.fromJson(responseStr, new TypeToken<Map<String, ProxyTopicStat>>() {
    }.getType());
    // number of topic is loaded = 1
    Assert.assertEquals(data.size(), 1);
    Entry<String, ProxyTopicStat> entry = data.entrySet().iterator().next();
    Assert.assertEquals(entry.getKey(), "persistent://" + topic);
    ProxyTopicStat stats = entry.getValue();
    // number of consumers are connected = 2 (one is reader)
    Assert.assertEquals(stats.consumerStats.size(), 2);
    ConsumerStats consumerStats = stats.consumerStats.iterator().next();
    // Assert.assertTrue(consumerStats.numberOfMsgDelivered > 0);
    Assert.assertNotNull(consumerStats.remoteConnection);
    // number of producers are connected = 1
    Assert.assertEquals(stats.producerStats.size(), 1);
    ProducerStats producerStats = stats.producerStats.iterator().next();
    // Assert.assertTrue(producerStats.numberOfMsgPublished > 0);
    Assert.assertNotNull(producerStats.remoteConnection);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) Invocation(javax.ws.rs.client.Invocation) ProxyTopicStat(org.apache.pulsar.websocket.stats.ProxyTopicStat) ProducerStats(org.apache.pulsar.websocket.stats.ProxyTopicStat.ProducerStats) TypeToken(com.google.gson.reflect.TypeToken) Gson(com.google.gson.Gson) ConsumerStats(org.apache.pulsar.websocket.stats.ProxyTopicStat.ConsumerStats) WebTarget(javax.ws.rs.client.WebTarget)

Example 5 with ProxyTopicStat

use of org.apache.pulsar.websocket.stats.ProxyTopicStat in project incubator-pulsar by apache.

the class ProxyPublishConsumeTest method verifyTopicStat.

private void verifyTopicStat(Client client, String baseUrl, String topic) {
    String statUrl = baseUrl + topic + "/stats";
    WebTarget webTarget = client.target(statUrl);
    Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
    Response response = (Response) invocationBuilder.get();
    String responseStr = response.readEntity(String.class);
    final Gson gson = new Gson();
    final ProxyTopicStat data = gson.fromJson(responseStr, ProxyTopicStat.class);
    Assert.assertFalse(data.producerStats.isEmpty());
    Assert.assertFalse(data.consumerStats.isEmpty());
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) Invocation(javax.ws.rs.client.Invocation) ProxyTopicStat(org.apache.pulsar.websocket.stats.ProxyTopicStat) Gson(com.google.gson.Gson) WebTarget(javax.ws.rs.client.WebTarget)

Aggregations

ProxyTopicStat (org.apache.pulsar.websocket.stats.ProxyTopicStat)5 ConsumerStats (org.apache.pulsar.websocket.stats.ProxyTopicStat.ConsumerStats)3 ProducerStats (org.apache.pulsar.websocket.stats.ProxyTopicStat.ProducerStats)3 Gson (com.google.gson.Gson)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Invocation (javax.ws.rs.client.Invocation)2 WebTarget (javax.ws.rs.client.WebTarget)2 Response (javax.ws.rs.core.Response)2 TypeToken (com.google.gson.reflect.TypeToken)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 TopicName (org.apache.pulsar.common.naming.TopicName)1