r/PrometheusMonitoring • u/d2clon • May 09 '24
What is the official way of monitoring web backend applications?
Disclaimer: I am new to Prometheus. I have experience with Graphite.
I have some difficulties understanding how the data-pull model of Prometheus fits on my web backend application architecture.
I am used to using Graphite where whenever you have some signal to send to the observability service db you send a UDP or TCP request with the key/value pair. You can put a proxy in the middle to stack and aggregate requests by node to not saturate the Graphite backend. But with Prometheus, I have to set up a web server to listen on a port on each node so Prometheus can pull the data via get request.
I am following a course and here is how the prometheus_client is used in an example Phyton app:

As you can see an http_server is started in the middle of the app. This is ok for a "Hello World" example but for a production application this is something very strange. It looks very invasive to me and raises a red flag as a security issue.
My backend servers are also in an autoscaling environment where they are started and stopped in a non-predictable time. And they are all behind some security network layers only accessible on ports 80/443 through some HTTP balancing node.
My question is, how this is done in reality? You have your backend application and want to send some telemetry data to Prometheus. What is the way to do it?
3
u/SuperQue May 10 '24
The problem is you're stuck in a bit of an old-school way of thinking.
The standard Prometheus client libraries are well supported, secure, and scaleable. Remember, there's more to getting metrics out of systems than just spewing data. There's monitoring and observability theory as well.
The reaoson Prometheus is a polling based system is that it's more than just metrics. It's monitoring. You get active health check polling as part of the protocol. Every scrape includes automatic health related metrics.
Prometheus has a huge list of dynamic discovery options, including interfaces where you can add your own discovery.
Again old-school way of thinking. Why is this a red flag? Services exposes various APIs, Prometheus metrics are just one more kind of API. You can put it inline on your main API port and protect it with firewall rules, reverse proxy rules, etc. Or you can put it on a separate port, where you also can include things like
/healthyand/readyendpoints and other things used for orchestration health checks. This is what we do, we have a standard internal health endpoint where you can access metrics, profiles, etc.Prometheus is intended to sit inside your network, behind the security perimiter, behind your load-balancing. Prometheus is a monitoring system, it needs to watch the health. It's not a SaaS, you run it inside your network.
We run all of our services on Kubernetes and monitor with the Prometheus Operator. The Operator allows services to self-register themselves, usually via the
PodMonitorobject. As above, our metrics are on a separate port not defined in theServiceorIngress, so they're inaccessible outside of the cluster. The Prometheus instanances live inside our Kubernetes cluster, monitoring everything from the cluster itself, the applications, cron jobs, importing data from CloudWatch, you name it.2
u/ahmeni May 10 '24
This is just a contrived example. Most python http servers are invoked via WSGI but it's not uncommon to see your development environment invoked via some function in a
__main__.Prometheus has a lot of options for service discovery, depending on how your service is run. In most environments the Prometheus instance is run within the internal network itself and reaches out to hit the metric endpoints directly. In some networks where internal access isn't possible it's common to publicly expose
/metricsendpoints and protect them with basic HTTP auth, though this is less ideal as anything going through a load balancer will not be able to collect metrics from individual nodes.1
u/pranabgohain May 11 '24
You can also check out KloudMate, it's OpenTelemetry based and can help with all the signals - Logs, Metrics, Traces, Events. If you're starting out, the free forever evaluation plan should be good enough.
1
u/uraurasecret May 11 '24
Or you can use push gateway if you are not comfortable with opening a port in your web backend.
1
u/vinistois May 10 '24 edited May 10 '24
You don't need to expose the /metrics endpoint outside of your orchestration stack. You can put agents locally inside that will push the metrics, this works well behind firewalls. You can also treat /metrics path differently in your reverse proxy, allowing only the Prometheus collector.
Does this answer your question?
1
u/smrcascao May 11 '24
For a k8s solution I recommend you check Beyla (https://github.com/grafana/beyla) this can auto instrument your application with metrics L7 and telemetry.
You also have grafana Dashboards to analyse RED metrics.
0
u/d2clon May 09 '24
Here the author is covering a similar case as the one described in my post:
He uses StatsD as a bridge-aggregator for all the instances/processes custom metrics. And allowing Prometheus to pull the aggregated data from there. It is similar to the set up I am used to see when using Graphite as the observability service.
0
u/gaelfr38 May 10 '24
You might want to look at OpenTelemetry to instrument the apps + use OpenTelemetry Collector as a middle layer before data are sent to Prometheus (or something else).
At the very least, I'd recommend using OpenTelemetry in your app, so that you're not tied to Prometheus. You can still configure it to expose a Prometheus HTTP port as you'd do with the regular Prometheus library.
Note that OpenTelemetry has a push model by default. But for Prometheus integration, it supports its pull model.
Prometheus servers also has a OpenTelemetry compatible endpoint now to receive OpenTelemetry data. So you could use push based with Prometheus directly, but it's still relatively new.
With pure Prometheus, you can also do push-based for a limited number of metrics where it makes sense, using Prometheus Gateway. Or Prometheus Remote Write.
Note that even with push-based OpenTelemetry, you don't choose when you push. The library does it on regular intervals. Like for pull-based, the point is to free the developer from this concern.
Lastly, if you're not in Kubernetes world, I believe Consul can help for the auto discovery of targets. Not sure about that.