aerospike / aerospike-client-java

Aerospike Java Client Library
Other
237 stars 214 forks source link

Add listener for node addition / deletion #334

Open anuragagarwal561994 opened 3 months ago

anuragagarwal561994 commented 3 months ago

I am trying to record metrics of aerospike via micrometer client.

So for example if I need to record the connections stats as a gauge I would need to declare it like

Gauge.builder(MetricName.AEROSPIKE_SYNC_CONNECTION_STATS, node, c -> c.sync.closed)
                .tags("state", CLOSED)
                .description("Aerospike sync connection closed stats")
                .register(meterRegistry);

Node I can touch the node instance here, once the guage is configured it is done, it will keep monitoring the node.

I can set this up at the start of the client, but when new nodes get added or removed, this is not changed.

If I would have had a listener of when the node is added or removed, I can create a new gauge with the new instance and remove the old one respectively from recording via micrometer.

There exists a MetricListener with the help of which I can somewhat achieve this, in a way:

public class AerospikeMetricListener implements MetricsListener {
    private final Map<String, Node> nodeMap = new ConcurrentHashMap<>();

    @Override
    public void onEnable(Cluster cluster, MetricsPolicy policy) {
        for (var node : cluster.getNodes()) {
            nodeMap.put(node.getName(), node);
        }
    }

    @Override
    public void onSnapshot(Cluster cluster) {
        for (var node : cluster.getNodes()) {
            nodeMap.putIfAbsent(node.getName(), node);
        }
    }

    @Override
    public void onNodeClose(Node node) {
        nodeMap.remove(node.getName());
    }

    @Override
    public void onDisable(Cluster cluster) {
        nodeMap.clear();
    }
}

Now when I know there is a new entry, I can start tracking the gauge. But this metric listener also starts recording the aerospike internal latency metrics as well on which I don't have much control and managing this listener is like an added responsibility for the client.

I think aerospike can provide listeners on addition and deletion of new nodes and moreover if possible I would say do provide the micromerter implementation itself, because that will help larger audience to consistently track client metrics.

BrianNichols commented 2 months ago

The deletion of a node is already covered by onNodeClose(). If a node is added and logging is enabled (https://aerospike.com/developer/client/logging?client=java), the client sends a log message starting with "Add node".

If this is not sufficient, an extra MetricsListener method called "onNodeOpen(Node node)" could be added. We are currently busy with other tasks, so it needs to wait for now.

anuragagarwal561994 commented 2 months ago

I think reading the log message is not the right way, moreover it won't give me the instance of the new node.

I think I can contribute for this simple functionality, also what are your thoughts about using micrometer or any instrumentation in the client?

BrianNichols commented 2 months ago

The goal is to minimize dependencies on 3rd party libraries, so adding an extra instrumentation library dependency is not likely.

anuragagarwal561994 commented 2 months ago

May be if possible a separate module can be provided which adds the support or help with OpenTelemetry auto instrumentation.

We tried it ourselves but it will be helpful if done from the side of aerospike I feel.