How to get size of NATS Streaming Queue?

7/29/2019

Background: I'm going to be doing some experiments with OpenFaaS (running in Kubernetes) where I will be invoking several asynchronous execution requests. OpenFaaS uses NATS Streaming to queue these requests for asynchronous function execution.

What I need is a way to determine the size of this NATS Streaming queue so I can know how many items are in the queue. Is there a command to get the size or number of items in a NATS Streaming Queue? I searched Google and the NATS documentation and found nothing of use.

I did find the command kubectl logs deployment/queue-worker -n openfaas from here which displays the logs of the queue; however, this is not quite what I want (I want the number of items left in the queue, not the queue's full logs).

-- kd2amc
kubernetes
nats-streaming-server
openfaas

1 Answer

7/30/2019

You can enable a monitoring endpoint in NATS to get a number of general endpoints to query which can go down to a specific channel.

You would then need to expose a Service for that endpoint in Kubernetes for external access, probably via an Ingress if you want more control over which endpoints and how they are exposed.

Have a look at the templates in a nats-streaming-ft helm chart.

Add the monitoring port to your container spec

spec:
  containers:
  - name: nats-streaming
    args:
      - /opt/nats-streaming-server
      - --http_port=8222

And the chosen monitoring port to your ports list in the Service.

apiVersion: v1
kind: Service
metadata:
  name: nats-monitoring
  labels:
    app:  nats
spec:
  selector:
    app:  nats
  ports:
  - name: monitoring
    protocol: TCP
    port: 8222
-- Matt
Source: StackOverflow