You are browsing a read-only backup copy of Wikitech. The live site can be found at

Prometheus: Difference between revisions

From Wikitech-static
Jump to navigation Jump to search
Line 324: Line 324:
* Add Prometheus java client ( to cassandra-metrics-collector ( alongside Graphite support.
* Add Prometheus java client ( to cassandra-metrics-collector ( alongside Graphite support.
* Add Prometheus java client to creole (
* Add Prometheus java client to creole (
== JMX ==
Prometheus [ jmx_exporter] can be used to collect metrics through JMX.
A few notes:
* Some standard JVM metrics are always collected as [ DefaultsExports], those cannot be ignored in the jmx_exporter configuration. The same metrics could be collected explicitly from their respective MBeans, but we chose to standardize on the default exports.
* Without whitelist / blacklist, jmx_exporter will iterate through all MBeans and read all their attributes. This can be expensive, or even dangerous depending on the MBeans exposed by the application.
* The whitelist / blacklist work as:
** load all mbeans corresponding to the whitelist query,
** load all mbeans corresponding to the black list query,
** remove all blacklisted mbeans from the list of whitelisted mbeans,
** iterate over the remaining mbeans, including reading all their attributes.
This implies that an overly broad blacklist query can still have a non trivial cost.

== Dashboards ==
== Dashboards ==

Revision as of 14:25, 9 January 2018

What is it?

Prometheus is a free software ecosystem for monitoring and alerting, with focus on reliability and simplicity. See also prometheus overview and prometheus FAQ.

There's a few interesting features that are missing from what we have now, among others:

multi-dimensional data model
Metrics have a name and several key=value pairs to better model what the metric is about. e.g. to measure varnish requests in the upload cache in eqiad we'd have a metric like http_requests_total{cache="upload",site="eqiad"}.
a powerful query language
Makes it able to ask complex questions, e.g. when debugging problems or drilling down for root cause during outages. From the example above, the query topk(3, sum(http_requests_total{status~="^5"}) by (cache)) would return the top 3 caches (text/upload/misc) with the most errors (status matches the regexp "^5")
pull metrics from targets
Prometheus is primarily based on a pull model, in which the prometheus server has a list of targets it should scrape metrics from. The pull protocol is HTTP based and simply put, the target returns a list of "<metric> <value>". Pushing metrics is supported too, see also

After the Prometheus POC (as per User:Filippo_Giunchedi/Prometheus_POC) has been running in Labs for some time, during FQ1 2016-2017 we'll be extending Prometheus deployment to production, as outlined in the Technical Operations goals .


Each prometheus server is configured to scrape a list of targets (i.e. HTTP endpoints) at a certain frequency, in our case starting at 60s. All metrics are stored on the local disk with a per-server retention period (minimum of 4 months for the initial goal).

All targets to be scraped are grouped into jobs, depending on the purpose that those targets serve. For example the job to scrape all host-level data for a given location using node-exporter will be called node and each target will be listed as hostname:9100. Similarly there could be jobs for varnish, mysql, etc.

Each prometheus server is meant to be stand-alone and polling targets in the same failure domain as the server itself as appropriate (e.g. the same datacenter, the same vlan and so on). For example this allows to keep the monitoring local to the datacenter and not have spotty metrics upon cross-datacenter connectivity blips. (See also Federation)

Prometheus single server.png


The endpoint being polled by the prometheus server and answering the GET requests is typically called exporter, e.g. the host-level metrics exporter is node-exporter.

Each exporter serves the current snapshot of metrics when polled by the prometheus server, there is no metric history kept by the exporter itself. Further, the exporter usually runs on the same host as the service or host it is monitoring.


Why just stand-alone prometheus servers with local storage and not clustered storage? The idea behind a single prometheus server is one of reliability: a monitoring system must be more reliabile than the systems it is monitoring. It is certainly easier to get local storage right and reliable than clustered storage, especially important when collecting operational metrics.

See also prometheus storage documentation for a more in-depth explanation and storage space requirements.

High availability

With local storage being the basic building block we can still achieve high-availability by running more than one server in parallel, each configured the same and polling the same set of targets. Queries for data can be routed via LVS in an active/standby fashion.

Prometheus HA server.png


For efficiency reasons, prometheus spools chunks of datapoints in memory for each metric before flushing them to disk. This makes it harder to perform backups online by simply copying the files on disk. The issue of having consistent backups is also discussed in prometheus #651.

Notwithstanding the above, it should be possible to backup the prometheus local storage files as-is by archiving its storage directory with tar before regular (bacula) backups. Since the backup is being done online it will result in some inconsistencies, upon restoring the backup Prometheus will crash-recovery its storage at startup.

To perform backups of consistent/clean state, at the moment prometheus needs to be shutdown gracefully, therefore when running an active/standby configuration backup can be taken on the standby prometheus to minimize its impact. Note that the shutdown will result in gaps in the standby prometheus server for the duration of the shutdown.

Failure recovery

In the event of a prometheus server having an unusable local storage (disk failed, FS failed, corruption, etc) failure recovery can take the form of:

  • start with empty storage: of course it is a complete loss of metric history for the local server and will obviously fully recover once the metric retention period has passed.
  • recover from backups: restore the storage directory to the last good backup
  • copy data from a similar server: when deployed in pairs it is possible to copy/rsync the storage directory onto the failed server, this will likely result in gaps in the recent history though (see also Backups)

Federation and multiple DCs

Each prometheus server is able to act as a target to another prometheus server by means of Prometheus federation. Our use case for this feature is primarily hierarchical federation, namely to have a 'global' prometheus that aggregates datacenter-level metrics from prometheus in each datacenter. Prometheus federation.png

The global instance is what we would normally use in grafana as the "datasource" for dashboards to get an overview of all sites and aggregated metrics. To drilldown further and get more details it is possible to use the datacenter-local datasource and dashboard.

Server location

In the diagram above the various Prometheus servers are logically separated, though physically they can share one/multiple machines. As of Nov 2016 Prometheus dc-local runs in two VMs for each of eqiad/codfw (instance named "ops") and we're in process of provisioning real hardware.

An open question at this time is where to host the dc-local Prometheus servers for caching centers, essentially two options:

  1. Local to the site
  2. Remote, e.g. codfw polling ulsfo and eqiad polling esams

The local option offers some advantages since all sites are logically the same and all polling for monitoring purposes is kept local to the site and reflects our current Ganglia deployment. Only the global instance would reach out to remote sites and thus could be affected by cross-DC network unavailability.

This is significant especially during outages: the global instance would show a drop in global aggregates while the dc-local instance can keep collecting high-resolution data from site-local machines.

Disadvantages of the local option include (as of Nov 2016) running Prometheus on the bastion for sites where we lack internal dedicated machines (e.g. ulsfo) alongside other services like tftp/installserver. Also the fact that running Prometheus on a single bastion would provide no redundancy when the bastion is down.

Service Discovery

Prometheus supports different kinds of discovery through its configuration. For example, in role::prometheus::labs_project implements auto-discovery of all instances for a given labs project. file_sd_config is used to continuously monitor a set of configuration files for changes and the script prometheus-labs-targets is run periodically to write the list of instances to the relative configuration file. The file_sd files are reloaded automatically by prometheus, so new instances will be auto-discovered and have their instance-level metrics collected.

While file-based service discovery works, Prometheus also supports higher-level discovery for example for Kubernetes (see also role::prometheus::tools).

Adding new metrics

Direct service instrumentation

The most benefits from service metrics are obtained when services are directly instrumented with one of Prometheus clients, e.g. Python client. Metrics are then exposed via HTTP, commonly at /metrics, on the service's HTTP port (in the common case) or a separate port if the service isn't HTTP to begin with.

Service exporters

For cases where services can't be directly instrumented (aka whitebox monitoring), a sidekick application exporter can be run alongside the service that will query the service using whatever mechanism and expose prometheus metrics via the client. This is the case for example for varnish_exporter parsing varnishstat -j or apache_exporter parsing apache's mod_status page.

Machine-level metrics

Another class of metrics is all those related to the machine itself rather than a particular service. Those involve calling a subprocess and parsing the result, often in a cronjob. In these cases the simplest thing to do is drop plaintext files on the machine's filesystem for node-exporter to pick up and expose the metrics on HTTP. This mechanism is named textfile and for example the python client has support for it, e.g. sample textfile collector usage. This is most likely the mechanism we could use to replace most of the custom collectors we have for Diamond.

Ephemeral jobs

Yet another case involves service-level ephemeral jobs that are not quite long-lived enough to be queried via HTTP. For those jobs there's a push mechanism to be used: metrics are pushed to Prometheus' pushgateway via HTTP and subsequently scraped by Prometheus from the gateway itself. This method appears similar to what statsd for its semplicity but it should be used with care, see also best practices on when to use the pushgateway. Good use cases could be MW's maintenance jobs: tracking how long the job took and when it last succeeded; if the job isn't tied to a machine in particular it is usually a good candidate.


Access Prometheus web interface

Prometheus web interface can be accessed on each prometheus server on port 80 (eg: prometheus2003.codfw.wmnet). SSH port forwarding can be used to access the web UI as follows:

ssh -L 8000:localhost:80 prometheus2003.codfw.wmnet

and then pointing your browser to http://localhost:8000/ops/. This method will eventually be replaced by ldap-authentication instead, see also bug T151009

List metrics with curl

One easy way to check what metrics are being collected by prometheus on a given machine is to request the metrics via HTTP like prometheus server does at scrape time, e.g. for node-exporter on port 9100:

 curl -s localhost:9100/metrics

Query cheatsheet

Filter for a specific instance

Given values such as


and a template variable called $server, containing the server hostname, one can filter for the selected instance as follows:


Filter by label using multi-values template variables

Given the following two metrics:

varnish_version{job="varnish-upload", ...}
node_uname_info{cluster="cache_upload", ...}

and a multi-value template variable called $cache_type, with the following values: text,upload,misc,canary, it is possible to write a prometheus query filtering the selected cache_types:


Dynamic, query-based template variables

Grafana's templating allows to define template variables based on Prometheus queries.

Given the following metric:

node_uname_info{release="4.9.0-0.bpo.4-amd64", ...}
node_uname_info{release="4.9.0-0.bpo.3-amd64", ...}

Choose Query as the variable Type, the desired Data Source, and specify a query such as the following to extract the values:

 label_values(node_uname_info, release)

Use cases


MySQL monitoring is performed by running prometheus-mysqld-exporter on the database machine to be monitored. Metrics are exported via http on port 9104 and fetched by prometheus server(s), to preview what metrics are being collected a fetch can be simulated with:

curl -s localhost:9104/metrics | grep -v '^#'


Per group / shard / role overview
Per server drilldown


One of the initial use cases for Prometheus is to provide at least as good service as Ganglia. For host-level metrics we're using prometheus-node-exporter and grouping hosts based on $cluster puppet variable.


Per cluster overview

Replacing Ganglia

As of Aug 2016 Prometheus is deployed in WMF's main locations: codfw and eqiad. To achieve feature-parity with Ganglia we'd need to expand Prometheus deployment to more locations, more machines and more metrics.

more locations
To fully replace Ganglia we'd need to deploy one (or two) prometheus servers in caching DCs too, similar to what we're doing with the ganglia aggregators. In practice this would mean running the server on ulsfo and esams bastions, as of Aug 2016 resources on both seem available (i.e. disk space and memory). To have aggregated stats available it is also possible to deploy one (in eqiad/codfw) "global" Prometheus servers that federates from each DC-local Prometheus.
more machines
Increase the number of machines from which we collect host metrics to 100% for each location Prometheus is deployed to, for jessie and trusty distributions.
more metrics
The current Ganglia deployment includes other metrics other than machine-level, namely the gmond plugins listed below and committed to puppet.git. Some of those can be replaced by existing exporters listed at while others will require some porting to prometheus' python client (packaged as python-prometheus-client). Each prometheus exporter will require some deployment/packaging work, namely creating packages (preferably using Debian native go packaging, or fpm as outlined at Prometheus/Exporters) plus puppet integration and instruct prometheus to poll the additional exporters.

Ganglia plugins
Parses apache's status page, similar to
Parses gdnsd JSON stats from localhost:3506/json, will require porting to prometheus python client
Parses varnish's JSON, similar to
Parses metrics from /tmp/vhtcpd.stats and will require porting
Already replaced by prometheus-mysqld-exporter
Parse metrics from localhost:9200, replacement could be based off something like or
Parse json from localhost:9002/memory.json, will require porting to prometheus python client
Ditto, for localhost:9002/check-health
Similar to
Parses stats from http://localhost:8000/?command=health, OCG is on its way out though
Parse stats from /srv/osmosis/state.txt, from OSM's
Similar to
Similar to
Counts sockets from udp2log, still used/useful?
Parse json from /var/cache/varnishkafka/varnishkafka.stats.json
Similar to, parses json stats from /var/cache/kafkatee/kafkatee.stats.json
Parse exim log files and state files and queue to extract messages stats

Replacing Graphite

Another use case imaginable for Prometheus is to replace the current Graphite deployment. This task is less "standalone" than replacing Ganglia and therefore more difficult: Graphite is more powerful and used by more people/services/dashboards. Nevertheless it should be possible to keep Prometheus and Graphite alongside each other and progressively put more data into Prometheus without affecting Graphite users. The top contributors to data that flows into Graphite as of Aug 2016 are Diamond, Statsd and Cassandra.


Diamond runs on each machine in the fleet, collecting local data and send the resulting metrics via TCP using carbon line-oriented protocol. See also add Prometheus support to Diamond though this might not be trivial as there needs to be a mapping from flat metric names to key => value pairs.

Similarly to Ganglia, there are custom collectors in use that would need an equivalent functionality using Prometheus clients/exporters. For some simple results (e.g. exit code / single output from commands) it is easier to write metrics in a text file for node_exporter to pick up and present it together with machine-level metrics.
Parse exim's paniclog and queue stats by calling exim -bpr
Report the number of users' crontabs, mainly used in tools
Report puppet stats from last_run_summary.yaml
Execute nagios commands locally and report the exit code
Collect nginx basic metrics from nginx's status page
Parse XML from localhost:9999
Report the number of cherry-pick patches in a given git repo
Parse localhost:9001 and report stats
Parse localhost:9002/dump-apc-info and report stats
Parse MOTD from local irc server
Parse libvirt local KVM stats and expose per-instance stats
See memcached in ganglia above
Report sysctl net.netfilter.nf_conntrack_count
Parse and report stats from /proc/net/rpc/nfsd and /proc/fs/nfsd/pool_stats
Emulate iostat for NFS mount points using /proc/self/mountstats
Parse json from nutcracker stats, though nutcracker might be on its way out and replaced by mcrouter
Parse openldap metrics from local ldap server /
Parse metrics from rec_control
Parse PyBal's pools info from localhost:9090
Parse RCStream stats from localhost:10080 (Rcstream will be deprecated in July 2017)
Collect rabbitmq queue stats, for openstack
Collect redis stats from multiple instances
Collect metrics from gridengine
Collect number of lines from who
Collect varnish stats from varnishtop, used in beta only ?
Collect jmx stats exported by jolokia at http://localhost:8778
Paired down collector for elasticsearch, exports basic stats and not per-index


Statsd traffic for the most part flows from machines to statsd.eqiad.wmnet over UDP on port 8125 for aggregation. There are some exceptions (e.g. swift) where statsd aggregation is performed on localhost and then pushed via graphite line-oriented protocol.

Prometheus provides statsd_exporter to receive statsd metrics and turn those into key => value prometheus metrics according to a user-supplied mapping. The resulting metrics are then exposed via HTTP for prometheus server to scrape.

One idea to integrate statsd_exporter into our statsd traffic is to put it "inline" between the application and statsd.eqiad.wmnet. In other words we would need to:

  1. Modify statsd_exporter to mirror received udp packets to statsd.eqiad.wmnet and install it on end hosts
  2. Opt-in applications by changing their statsd host from statsd.eqiad.wmnet to localhost
  3. Extend the statsd_exporter mapping file to include mappings for our statsd metrics.

This method works well for applications/languages that are request-scoped (e.g. php) since there isn't necessarily a server process to keep and aggregate metrics in. For services that qualify, the recommended way is to switch to Prometheus client for instrumentation.


Cassandra is hosted on separate Graphite machines due to the number and size of metrics it pushes, particularly in conjunction with Restbase. It should be evaluated separatedly too if e.g. a separate prometheus instance makes sense. WRT implementation there are two viable options:


Prometheus jmx_exporter can be used to collect metrics through JMX.

A few notes:

  • Some standard JVM metrics are always collected as DefaultsExports, those cannot be ignored in the jmx_exporter configuration. The same metrics could be collected explicitly from their respective MBeans, but we chose to standardize on the default exports.
  • Without whitelist / blacklist, jmx_exporter will iterate through all MBeans and read all their attributes. This can be expensive, or even dangerous depending on the MBeans exposed by the application.
  • The whitelist / blacklist work as:
    • load all mbeans corresponding to the whitelist query,
    • load all mbeans corresponding to the black list query,
    • remove all blacklisted mbeans from the list of whitelisted mbeans,
    • iterate over the remaining mbeans, including reading all their attributes.

This implies that an overly broad blacklist query can still have a non trivial cost.


Grafana dashboards will need porting from Graphite to Prometheus metrics; this is likely to be the most labor-intensive part since most (all?) dashboards are hand-curated. While it should be possible to programmatically change statsd metric names into prometheus metric names, the query language is different enough to make this impractical except for very basic cases.