Particle, Cloud Functions & Prometheus
Following up on my earlier story “Particle, OpenCensus & MicroK8s” I was wondering whether Prometheus PushGateway may be a good solution for delivering Particle device measurement.
It works well.
Kubernetes
Here’s the Kubernetes config to create a Deployment for the Pushgateway and expose this as a Service (using a TCP Load-Balancer):
You can get the service’s endpoint:
GATEWAY=$(kubectl get services/pushgateway \
--output=jsonpath="{.status.loadBalancer.ingress[0].ip}") && \
echo ${GATEWAY}
Cloud Functions
Here’s a Cloud Function that transforms an incoming Particle Webhook POST into a Prometheus Gauge and pushes it to a Pushgateway:
This can be deployed using:
gcloud functions deploy topushgateway \
--region=us-central1 \
--project=${PROJECT} \
--runtime=go112 \
--trigger-http \
--entry-point=ToPushGateway \
--set-env-vars=GATEWAY=${GATEWAY}
Particle Webhook
Create a Particle Integration Webhook that ships the Particle “percent” events to the Cloud Function:
Testing
After ≥1 minute, the Webhook should report events:
The Cloud Function should report events:
Or:
gcloud logging read 'resource.type="cloud_function" resource.labels.function_name="topushgateway"' \
--project=${PROJECT} \
--freshness=5m \
--format="json" \
| jq -r .[].textPayloadFunction execution took 43 ms, finished with status code: 200
2019/06/28 17:29:45 value: 22.881563
2019/06/28 17:29:45 function
Function execution started
Function execution took 40 ms, finished with status code: 200
2019/06/28 17:28:44 value: 22.710623
2019/06/28 17:28:44 function
Function execution started
Function execution took 43 ms, finished with status code: 200
2019/06/28 17:27:44 value: 22.442003
2019/06/28 17:27:44 function
Function execution started
Function execution took 42 ms, finished with status code: 200
2019/06/28 17:26:44 value: 22.710623
2019/06/28 17:26:44 function
Function execution started
Function execution took 41 ms, finished with status code: 200
2019/06/28 17:25:44 value: 22.637363
2019/06/28 17:25:44 function
Function execution started
The Pushgateway should report events:
google-chrome http://${GATEWAY}:9091/metrics
And you should be able to CTRL-F the “percent” measurement in the page.
Prometheus
If you want to connect a Prometheus server to the Pushgateway, create a prometheus.yml
and replace the value of ${GATEWAY}
:
global:
scrape_interval: 5s
external_labels:
monitor: "local-monitor"scrape_configs:
- job_name: "pushgateway"
honor_labels: true
static_configs:
- targets: ["${GATEWAY}:9091"]
Then:
docker run \
--publish=9090:9090 \
--volume=${PWD}/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
And browse http://localhost:9090
And, find percent
using PromQL:
NB In the PromQL, I’m filtering by
job="pushgateway"
andcore_id=${COREID}
where${COREID}
is the identifier of the Particle device pulled from the Webhook data.
Conclusion
I’ve been meaning to explore the Prometheus Pushgateway. Because the Particle publish events are available ephemerally, it makes sense to push these to a Pushgateway using something like Cloud Functions.
Hopefully this helps you envisage other scenarios!
That’s all.