Пример #1
0
def get_metrics_reporting_registry(
    process_registry: CollectorRegistry,
) -> CollectorRegistry:
    """
    Get the metrics registry for reporting metrics.

    If we're running under gunicorn, then each worker has its own process and
    its own process collector. For reporting, we need a fresh registry with a
    multiprocess collector that points to the metrics folder (created empty at
    startup, with a database file for each process and metric type). It will
    use the databases in this folder to generate combined metrics across the
    processes, and will not double-count the reporting process's metrics.

    If we're not running under gunicorn, then return the passed per-process
    registry, which is the only metrics registry. In the single-process case,
    We could use the default prometheus_client.REGISTRY, but it makes tests
    easier to write if it is possible to replace the registry with a fresh one.
    """
    try:
        settings = config.Settings()
        prometheus_multiproc_dir = settings.prometheus_multiproc_dir
    except ValidationError:
        prometheus_multiproc_dir = None

    if prometheus_multiproc_dir:
        registry = CollectorRegistry()
        MultiProcessCollector(registry, path=prometheus_multiproc_dir)
        return registry
    return process_registry
Пример #2
0
    def __init__(self, app=None, export_defaults=True,
                 defaults_prefix='flask', group_by='path',
                 buckets=None, static_labels=None, registry=None):
        """
        Create a new multiprocess-aware Prometheus metrics export configuration.

        :param app: the Flask application (can be `None`)
        :param export_defaults: expose all HTTP request latencies
            and number of HTTP requests
        :param defaults_prefix: string to prefix the default exported
            metrics name with (when either `export_defaults=True` or
            `export_defaults(..)` is called)
        :param group_by: group default HTTP metrics by
            this request property, like `path`, `endpoint`, `url_rule`, etc.
            (defaults to `path`)
        :param buckets: the time buckets for request latencies
            (will use the default when `None`)
        :param static_labels: static labels to attach to each of the
            metrics exposed by this metrics instance
        :param registry: the Prometheus Registry to use (can be `None` and it
            will be registered with `prometheus_client.multiprocess.MultiProcessCollector`)
        """

        _check_multiproc_env_var()

        registry = registry or CollectorRegistry()
        MultiProcessCollector(registry)

        super(MultiprocessPrometheusMetrics, self).__init__(
            app=app, path=None, export_defaults=export_defaults,
            defaults_prefix=defaults_prefix, group_by=group_by,
            buckets=buckets, static_labels=static_labels,
            registry=registry
        )
Пример #3
0
def get_metrics() -> str:
    """Get collected metrics."""
    registry = REGISTRY
    if 'PROMETHEUS_MULTIPROC_DIR' in os.environ:
        registry = CollectorRegistry()
        MultiProcessCollector(registry)

    return generate_latest(registry)
Пример #4
0
def get_registry(name):
    if name not in collector_registries.keys():
        collector_registries[name] = CollectorRegistry()

        if prometheus_multiproc_dir is not None:
            MultiProcessCollector(collector_registries[name])

    return collector_registries[name]
Пример #5
0
def metrics(request: Request) -> Response:
    if "prometheus_multiproc_dir" in os.environ:
        registry = CollectorRegistry()
        MultiProcessCollector(registry)
    else:
        registry = REGISTRY

    return Response(generate_latest(registry), headers={"Content-Type": CONTENT_TYPE_LATEST})
Пример #6
0
 def get_metrics():
     registry = CollectorRegistry()
     MultiProcessCollector(registry)
     data = generate_latest(registry)
     response_headers = [
         ('Content-type', CONTENT_TYPE_LATEST),
         ('Content-Length', str(len(data))),
     ]
     return data, 200, response_headers
Пример #7
0
def collect_metrics():
    registry = CollectorRegistry()
    MultiProcessCollector(registry)
    PlatformCollector(registry)
    ExternalMetrics(registry)

    data = generate_latest(registry)

    return Response(data, mimetype=CONTENT_TYPE_LATEST)
Пример #8
0
    def __init__(self, app, endpoint="/metrics"):
        self.app = None
        self.endpoint = endpoint
        self.registry = CollectorRegistry()
        MultiProcessCollector(self.registry)
        self._metrics = {}

        if app is not None:
            self.init_app(app)
Пример #9
0
    def test_deprecation_warning(self):
        os.environ['prometheus_multiproc_dir'] = self.tempdir
        with warnings.catch_warnings(record=True) as w:
            values.ValueClass = get_value_class()
            registry = CollectorRegistry()
            collector = MultiProcessCollector(registry)
            Counter('c', 'help', registry=None)

            assert os.environ['PROMETHEUS_MULTIPROC_DIR'] == self.tempdir
            assert len(w) == 1
            assert issubclass(w[-1].category, DeprecationWarning)
            assert "PROMETHEUS_MULTIPROC_DIR" in str(w[-1].message)
Пример #10
0
def metrics(request: Request) -> Response:
    if "prometheus_multiproc_dir" in os.environ:
        registry = CollectorRegistry()
        MultiProcessCollector(registry)
        logger.info(
            f"Metrics multiprocess :[{os.environ['prometheus_multiproc_dir']}]"
        )
    else:
        registry = REGISTRY
        logger.info(f"Metrics no multiprocess")

    return Response(generate_latest(registry), media_type=CONTENT_TYPE_LATEST)
Пример #11
0
def get_metrics(request):
    """Pyramid view that return the metrics"""

    if prom.IS_MULTIPROC:
        registry = CollectorRegistry()
        MultiProcessCollector(registry)
    else:
        registry = REGISTRY

    request.response.content_type = CONTENT_TYPE_LATEST
    resp = Response(content_type=CONTENT_TYPE_LATEST, )
    resp.body = generate_latest(registry)
    return resp
Пример #12
0
def prometheus_registry():
    """
    Configure prometheus_client.

    This is run the first time the /metrics endpoint is used.
    """
    # The multiprocess configuration makes it compatible with gunicorn.
    # https://github.com/prometheus/client_python/#multiprocess-mode-eg-gunicorn
    from prometheus_client import CollectorRegistry
    from prometheus_client.multiprocess import MultiProcessCollector

    registry = CollectorRegistry()
    MultiProcessCollector(registry)  # This has a side effect, apparently.
    return registry
    def __init__(self, app=None, **kwargs):
        """
        Create a new multiprocess-aware Prometheus metrics export configuration.

        :param registry: the Prometheus Registry to use (can be `None` and it
            will be registered with `prometheus_client.multiprocess.MultiProcessCollector`)
        """

        _check_multiproc_env_var()

        registry = kwargs.get('registry') or CollectorRegistry()
        MultiProcessCollector(registry)

        super(MultiprocessPrometheusMetrics, self).__init__(app=app,
                                                            path=None,
                                                            registry=registry,
                                                            **kwargs)
Пример #14
0
    def set_config(self, config: ValueExtractor) -> None:
        super().set_config(config)
        registry = get_registry(self.config.get('registry', REGISTRY))
        if MULTIPROC_DIR:
            registry = MultiProcessCollector(registry)
        self._registry = registry

        port: int = self.config.get_int('port', default=0)
        if port:
            start_http_server(port=port, registry=registry)

        graphite: Optional[ValueExtractor] = self.config.get('graphite')
        if graphite:
            address = graphite.get('address')
            if isinstance(address, str):
                addr = address.split(':')
                address = (addr[0], int(addr[-1]))
            gb = GraphiteBridge(address, registry=registry)
            interval: float = graphite.get_duration('interval', 60)
            prefix: str = graphite.get('prefix', '')
            gb.start(interval, prefix=prefix)
Пример #15
0
    def __init__(self, collectors: Iterable[Any]):
        """Builds a metric encoder using the given list of collectors. A collector is broadly
        defined here as any type that implements the `collect` method.

        :param collectors: The collectors to fetch metrics from
        :type collectors: Iterable[Any]
        """
        self._registry = CollectorRegistry(auto_describe=True)

        # Always use a new registry for collecting mmapped files under multiprocess mode
        if "PROMETHEUS_MULTIPROC_DIR" in os.environ:
            MultiProcessCollector(self._registry)
            # Do not double register metrics that implement MultiProcessValue
            # See: prometheus_client/values.py#L31
            collectors = filter(
                lambda c: not isinstance(c, LiveMetricRegistry) and
                not isinstance(c, MetricWrapperBase) and hasattr(c, "collect"),
                collectors,
            )

        for c in collectors:
            self._registry.register(c)
Пример #16
0
def prometheus_cleanup_worker(pid):
    """Aggregate dead worker's metrics into a single archive file."""
    mark_process_dead(pid)
    prom_dir = os.environ['prometheus_multiproc_dir']
    worker_files = [
        'histogram_{}.db'.format(pid),
        'counter_{}.db'.format(pid),
    ]
    paths = _filter_exists(os.path.join(prom_dir, f) for f in worker_files)

    # check at least one worker file exists
    if not paths:
        return

    histogram_path = os.path.join(prom_dir, histogram_archive)
    counter_path = os.path.join(prom_dir, counter_archive)
    archive_paths = _filter_exists([histogram_path, counter_path])

    collect_paths = paths + archive_paths
    collector = MultiProcessCollector(None)

    try:
        metrics = collector.merge(collect_paths, accumulate=False)
    except AttributeError:
        metrics = legacy_collect(collect_paths)

    tmp_histogram = tempfile.NamedTemporaryFile(delete=False)
    tmp_counter = tempfile.NamedTemporaryFile(delete=False)
    write_metrics(metrics, tmp_histogram.name, tmp_counter.name)

    # ensure reader does get partial state
    with prometheus_lock:
        os.rename(tmp_histogram.name, histogram_path)
        os.rename(tmp_counter.name, counter_path)

        for path in paths:
            os.unlink(path)
Пример #17
0
def metrics():
    return Response(
        generate_latest(MultiProcessCollector(CollectorRegistry())),
        mimetype=CONTENT_TYPE_LATEST,
    )
Пример #18
0
def display_metrics():
    registry = prometheus_client.CollectorRegistry()
    MultiProcessCollector(registry)
    data = prometheus_client.generate_latest(registry=registry)
    return data.decode()
Пример #19
0
def test_prometheus_cleanup(registry):
    pid = 1

    def getpid():
        return pid

    # override use of os.getpid. _ValueClass is recreated after every test,
    # so we don't need to clean up
    from prometheus_client import core
    core._ValueClass = core._MultiProcessValue(getpid)

    histogram = metrics.Histogram(
        name='histogram',
        documentation='test histogram',
        labelnames=['foo', 'bar', 'baz'],
        statsd='{name}.{label}',
        registry=registry,
    )
    counter = metrics.Counter(
        name='counter',
        documentation='test counter',
        labelnames=['foo', 'bar', 'baz'],
        statsd='{name}.{label}',
        registry=registry,
    )

    from prometheus_client.multiprocess import MultiProcessCollector
    collector = MultiProcessCollector(registry)
    labels = {'foo': 'foo', 'bar': 'bar', 'baz': 'baz'}

    def collect():
        return {m.name: m for m in collector.collect()}

    def files():
        return list(sorted(os.listdir(os.environ['prometheus_multiproc_dir'])))

    counter.inc(1, **labels)
    histogram.observe(0.5, **labels)
    histogram.observe(2.5, **labels)

    assert files() == [
        'counter_1.db',
        'histogram_1.db',
    ]

    before = collect()
    metrics.prometheus_cleanup_worker(pid)
    after = collect()
    assert files() == [
        'counter_archive.db',
        'histogram_archive.db',
    ]
    assert before == after

    # magic!
    pid += 1

    # new worker, create some new metrics, check they are all combined
    counter.inc(2, **labels)
    histogram.observe(0.5, **labels)
    histogram.observe(2.5, **labels)

    later = collect()
    assert files() == [
        'counter_2.db',
        'counter_archive.db',
        'histogram_2.db',
        'histogram_archive.db',
    ]

    # check counter is correct

    assert later['counter'].samples == [
        Sample(counter_name('counter_total'), labels, 3.0),
    ]

    expected_histogram = [
        Sample('histogram_bucket', dict(le='0.005', **labels), 0.0),
        Sample('histogram_bucket', dict(le='0.01', **labels), 0.0),
        Sample('histogram_bucket', dict(le='0.025', **labels), 0.0),
        Sample('histogram_bucket', dict(le='0.05', **labels), 0.0),
        Sample('histogram_bucket', dict(le='0.075', **labels), 0.0),
        Sample('histogram_bucket', dict(le='0.1', **labels), 0.0),
        Sample('histogram_bucket', dict(le='0.25', **labels), 0.0),
        Sample('histogram_bucket', dict(le='0.5', **labels), 2.0),
        Sample('histogram_bucket', dict(le='0.75', **labels), 2.0),
        Sample('histogram_bucket', dict(le='1.0', **labels), 2.0),
        Sample('histogram_bucket', dict(le='2.5', **labels), 4.0),
        Sample('histogram_bucket', dict(le='5.0', **labels), 4.0),
        Sample('histogram_bucket', dict(le='7.5', **labels), 4.0),
        Sample('histogram_bucket', dict(le='10.0', **labels), 4.0),
        Sample('histogram_bucket', dict(le='+Inf', **labels), 4.0),
        Sample('histogram_count', labels, 4.0),
        Sample('histogram_sum', labels, 6.0),
    ]

    # check histogram is correct
    later['histogram'].samples.sort(key=metrics.histogram_sorter)
    assert later['histogram'].samples == expected_histogram

    # check the final files produce the correct numbers
    metrics.prometheus_cleanup_worker(pid)
    final = collect()
    assert files() == [
        'counter_archive.db',
        'histogram_archive.db',
    ]
    final['histogram'].samples.sort(key=metrics.histogram_sorter)
    assert later == final
Пример #20
0
@contact: [email protected]
@time: 2018/5/8 上午10:18
"""

import os

import prometheus_client
from flask import Flask, Response, request
from flask import jsonify
from prometheus_client import CollectorRegistry, multiprocess
from prometheus_client import Counter
from prometheus_client.multiprocess import MultiProcessCollector

# REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
REGISTRY = CollectorRegistry(auto_describe=False)
MultiProcessCollector(REGISTRY)

requests_total = Counter("app:requests_total",
                         "Total count of requests", ["method", "url_rule"],
                         registry=REGISTRY)

app = Flask(__name__)


def record(func):
    """
    func 即为调用该函数的方法
    """
    def req(*args, **kwargs):
        requests_total.labels(method=request.method,
                              url_rule=request.path.lower()).inc()
Пример #21
0
 def setUp(self):
     self.tempdir = tempfile.mkdtemp()
     os.environ['PROMETHEUS_MULTIPROC_DIR'] = self.tempdir
     values.ValueClass = MultiProcessValue(lambda: 123)
     self.registry = CollectorRegistry()
     self.collector = MultiProcessCollector(self.registry)
Пример #22
0
def metrics(request):
    log.info('Serving metrics')
    registry = CollectorRegistry()
    MultiProcessCollector(registry)
    data = generate_latest(registry)
    return Response(data, content_type='text/plain')
Пример #23
0
 def setUp(self):
     self.tempdir = tempfile.mkdtemp()
     os.environ['prometheus_multiproc_dir'] = self.tempdir
     core._ValueClass = core._MultiProcessValue(lambda: 123)
     self.registry = CollectorRegistry()
     MultiProcessCollector(self.registry, self.tempdir)
Пример #24
0
def create_registry():
    registry = CollectorRegistry()
    MultiProcessCollector(registry)
    return registry