<p>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. </p>

<p>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. </p>

<p>I did find the command <code>kubectl logs deployment/queue-worker -n openfaas</code> from <a href="https://github.com/openfaas/workshop/blob/master/lab7.md" rel="nofollow noreferrer">here</a> 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).</p>

<p>You can enable a <a href="https://docs.nats.io/running-a-nats-service/nats_admin/monitoring" rel="nofollow noreferrer">monitoring endpoint</a> in NATS to get a number of general <a href="https://docs.nats.io/running-a-nats-service/nats_admin/monitoring#monitoring-endpoints" rel="nofollow noreferrer">endpoints</a> to query which can go down to a specific channel.</p>
<p>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.</p>
<p>Have a look at the templates in a <a href="https://github.com/canhnt/k8s-nats-streaming/blob/master/charts/nats-streaming-ft/templates/service.yaml" rel="nofollow noreferrer">nats-streaming-ft</a> helm chart.</p>
<p>Add the monitoring port to your container <code>spec</code></p>
<pre><code>spec:
  containers:
  - name: nats-streaming
    args:
      - /opt/nats-streaming-server
      - --http_port=8222
</code></pre>
<p>And the chosen monitoring port to your <code>ports</code> list in the <code>Service</code>.</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: nats-monitoring
  labels:
    app:  nats
spec:
  selector:
    app:  nats
  ports:
  - name: monitoring
    protocol: TCP
    port: 8222
</code></pre>
