### Motivation

The call to `serverStats().getNumAliveClientConnections()` inside ZK server is 
throwing NPE in some cases where the `ServerCnxnFactory` is null. 

```
22:07:04.269 [qtp33419717-17] WARN  org.eclipse.jetty.servlet.ServletHandler - 
/metrics
java.lang.NullPointerException: null
        at 
org.apache.zookeeper.server.ZooKeeperServer.getNumAliveConnections(ZooKeeperServer.java:883)
 ~[org.apache.pulsar-pulsar-broker-2.2.0-streamlio-4.jar:2.2.0-streamlio-4]
        at 
org.apache.zookeeper.server.ServerStats.getNumAliveClientConnections(ServerStats.java:90)
 ~[org.apache.pulsar-pulsar-broker-2.2.0-streamlio-4.jar:2.2.0-streamlio-4]
        at 
org.apache.pulsar.zookeeper.ZooKeeperServerAspect$3.get(ZooKeeperServerAspect.java:74)
 ~[org.apache.pulsar-pulsar-zookeeper-2.2.0-streamlio-4.jar:2.2.0-streamlio-4]
        at io.prometheus.client.Gauge.collect(Gauge.java:295) 
~[io.prometheus-simpleclient-0.0.23.jar:?]
        at 
io.prometheus.client.CollectorRegistry$MetricFamilySamplesEnumeration.findNextElement(CollectorRegistry.java:180)
 ~[io.prometheus-simpleclient-0.0.23.jar:?]
        at 
io.prometheus.client.CollectorRegistry$MetricFamilySamplesEnumeration.nextElement(CollectorRegistry.java:213)
 ~[io.prometheus-simpleclient-0.0.23.jar:?]
        at 
io.prometheus.client.CollectorRegistry$MetricFamilySamplesEnumeration.nextElement(CollectorRegistry.java:134)
 ~[io.prometheus-simpleclient-0.0.23.jar:?]
        at 
io.prometheus.client.exporter.common.TextFormat.write004(TextFormat.java:22) 
~[io.prometheus-simpleclient_common-0.0.23.jar:?]
        at 
io.prometheus.client.exporter.MetricsServlet.doGet(MetricsServlet.java:43) 
~[io.prometheus-simpleclient_servlet-0.0.23.jar:?]
```

This also break the metrics output since it won't print any more metrics after 
this exception.

### Modifications

Bypass the `ServerStats` object and directly get the info on the 
`ServerCnxnFactory`, validating that it's not null.

[ Full content available at: https://github.com/apache/pulsar/pull/2710 ]
This message was relayed via gitbox.apache.org for [email protected]

Reply via email to