Skip to main content

Exporting scrapy stats as prometheus metrics through pushgateway service

Project description

https://img.shields.io/pypi/status/scrapy_prometheus.svg https://travis-ci.org/sashgorokhov/scrapy_prometheus.svg?branch=master https://img.shields.io/github/license/sashgorokhov/scrapy_prometheus.svg https://img.shields.io/pypi/pyversions/scrapy_prometheus.svg https://badge.fury.io/py/scrapy_prometheus.svg

Scrapy stats collector that exports scrapy stats as prometheus metrics through pushgateway service.

Installation

Via pip:

pip install scrapy_prometheus

Usage

To start using, add scrapy_prometheus.PrometheusStatsCollector to STATS_CLASS setting:

STATS_CLASS = 'scrapy_prometheus.PrometheusStatsCollector'

This stats collector works exactly like the vanilla one (because it subclasses it), but also creates prometheus metrics and pushes them to pushgateway service on spider close signal.

It supports two metric types: Counter and Gauge. Stat metric type is determined by operation used on the stat: stats.inc_value will create a Counter metric, while other methods, stats.set_value, stats.max_value, stats.min_value, will create Gauge.

Stat value must be either int or float.

Note, that trying to perform action on a metric, that is not supposed to be used with this action (set_value on Counter or inc_value on Gauge) will produce scrapy_prometheus.InvalidMetricType error. To suppress it, set PROMETHEUS_SUPPRESS_TYPE_CHECK to True.

If you want to create other custom metrics, there are good news for you: scrapy_prometheus.PrometheusStatsCollector is also a prometheus_client.CollectorRegistry. Multi-inheritance rules. Just specify it as a registry, when creating a metric.

Available settings

# Prometheus pushgateway host
PROMETHEUS_PUSHGATEWAY = 'localhost:9091'  # default

# Metric name prefix
PROMETHEUS_METRIC_PREFIX = 'scrapy_prometheus'  # default

# Timeout for pushing metrics to pushgateway
PROMETHEUS_PUSH_TIMEOUT = 5  # default

# Method to use when pushing metrics
# Read https://github.com/prometheus/pushgateway#put-method
PROMETHEUS_PUSH_METHOD = 'POST'  # default

# Do not raise scrapy_prometheus.InvalidMetricType when stat is accessed as different type metric.
# For example, doing stats.inc_value('foo', 1) and then stats.set_value('foo', 2) will raise an error,
# Because metric of type Counter was already created for stat foo.
PROMETHEUS_SUPPRESS_TYPE_CHECK = False

# job label value, applied to all metrics.
PROMETHEUS_JOB = 'scrapy'  # default

# grouping label dict, applied to all metrics.
# by default it uses spider name.
PROMETHEUS_GROUPING_KEY = {'spider': <spider name>}

How metrics are created

Metric name is build from PROMETHEUS_METRIC_PREFIX and stat name, where all / are replaced with _.

For example:

  • stat foo: 67 whill produce metric scrapy_prometheus_foo{instance="...",job="scrapy",spider="..."} 67

  • stat foo/bar: 67 whill produce metric scrapy_prometheus_foo_bar{instance="...",job="scrapy",spider="..."} 67

  • stat foo/bar/baz: 67 whill produce metric scrapy_prometheus_foo_bar_baz{instance="...",job="scrapy",spider="..."} 67

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scrapy_prometheus-0.3.1.tar.gz (3.5 kB view details)

Uploaded Source

File details

Details for the file scrapy_prometheus-0.3.1.tar.gz.

File metadata

File hashes

Hashes for scrapy_prometheus-0.3.1.tar.gz
Algorithm Hash digest
SHA256 2c36f672c772cd4333343321c03a9d7999b32634fbff2a9f73aa2fc9171a54e5
MD5 9ec791888348c240affe95ed1af4feec
BLAKE2b-256 82580d24808e9200294b08b4dafaa5cd408b17b780a477617298b4af17ec9d45

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page