Python prometheus_client.Histogram() Examples

The following are 16 code examples of prometheus_client.Histogram(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module prometheus_client , or try the search function .
Example #1
Source File: metrics.py    From anchore-engine with Apache License 2.0 8 votes vote down vote up
def histogram_observe(name, observation, description="", buckets=None, **kwargs):
    global metrics, enabled

    if not enabled:
        return True

    try:
        if name not in metrics:
            if buckets:
                buckets.append(float("inf"))
                metrics[name] = Histogram(name, description, list(kwargs.keys()), buckets=buckets)
            else:
                metrics[name] = Histogram(name, description, list(kwargs.keys()))

        if kwargs:
            metrics[name].labels(**kwargs).observe(observation)
        else:
            metrics[name].observe(observation)
    except Exception as err:
        logger.warn("adding metric failed - exception: " + str(err))

    return True 
Example #2
Source File: __init__.py    From prometheus_flask_exporter with MIT License 7 votes vote down vote up
def histogram(self, name, description, labels=None, **kwargs):
        """
        Use a Histogram to track the execution time and invocation count
        of the method.

        :param name: the name of the metric
        :param description: the description of the metric
        :param labels: a dictionary of `{labelname: callable_or_value}` for labels
        :param kwargs: additional keyword arguments for creating the Histogram
        """

        return self._track(
            Histogram,
            lambda metric, time: metric.observe(time),
            kwargs, name, description, labels,
            registry=self.registry
        ) 
Example #3
Source File: cloud_region.py    From openstacksdk with Apache License 2.0 6 votes vote down vote up
def get_prometheus_histogram(self):
        registry = self.get_prometheus_registry()
        if not registry or not prometheus_client:
            return
        # We have to hide a reference to the histogram on the registry
        # object, because it's collectors must be singletons for a given
        # registry but register at creation time.
        hist = getattr(registry, '_openstacksdk_histogram', None)
        if not hist:
            hist = prometheus_client.Histogram(
                'openstack_http_response_time',
                'Time taken for an http response to an OpenStack service',
                labelnames=[
                    'method', 'endpoint', 'service_type', 'status_code'
                ],
                registry=registry,
            )
            registry._openstacksdk_histogram = hist
        return hist 
Example #4
Source File: metrics.py    From client_python with Apache License 2.0 6 votes vote down vote up
def __init__(self,
                 name,
                 documentation,
                 labelnames=(),
                 namespace='',
                 subsystem='',
                 unit='',
                 registry=REGISTRY,
                 labelvalues=None,
                 buckets=DEFAULT_BUCKETS,
                 ):
        self._prepare_buckets(buckets)
        super(Histogram, self).__init__(
            name=name,
            documentation=documentation,
            labelnames=labelnames,
            namespace=namespace,
            subsystem=subsystem,
            unit=unit,
            registry=registry,
            labelvalues=labelvalues,
        )
        self._kwargs['buckets'] = buckets 
Example #5
Source File: metrics.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self,
        name,
        documentation,
        labelnames=(),
        namespace='',
        subsystem='',
        unit='',
        registry=REGISTRY,
        labelvalues=None,
        buckets=DEFAULT_BUCKETS,
    ):
        self._prepare_buckets(buckets)
        super(Histogram, self).__init__(
            name=name,
            documentation=documentation,
            labelnames=labelnames,
            namespace=namespace,
            subsystem=subsystem,
            unit=unit,
            registry=registry,
            labelvalues=labelvalues,
        )
        self._kwargs['buckets'] = buckets 
Example #6
Source File: proton_prometheus.py    From PROTON with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self):
        super(ProtonPrometheus, self).__init__()
        self.registry = CollectorRegistry()
        self.requests = Counter(
            'http_total_request',
            'Counter of total HTTP requests',
            ['method', 'path', 'status'],
            registry=self.registry)

        self.request_historygram = Histogram(
            'request_latency_seconds',
            'Histogram of request latency',
            ['method', 'path', 'status'],
            registry=self.registry) 
Example #7
Source File: metrics.py    From sanic-prometheus with MIT License 5 votes vote down vote up
def init(app, latency_buckets=None, multiprocess_mode='all',
         memcollect_enabled=True, metrics_list=None):
    app.metrics['RQS_COUNT'] = Counter(
        'sanic_request_count',
        'Sanic Request Count',
        ['method', 'endpoint', 'http_status']
    )

    hist_kwargs = {}
    if latency_buckets is not None:
        hist_kwargs = {'buckets': latency_buckets}
    app.metrics['RQS_LATENCY'] = Histogram(
        'sanic_request_latency_sec',
        'Sanic Request Latency Histogram',
        ['method', 'endpoint', 'http_status'],
        **hist_kwargs
    )

    if memcollect_enabled:
        app.metrics['PROC_RSS_MEM_BYTES'] = Gauge(
            'sanic_mem_rss_bytes',
            'Resident memory used by process running Sanic',
            multiprocess_mode=multiprocess_mode
        )
        app.metrics['PROC_RSS_MEM_PERC'] = Gauge(
            'sanic_mem_rss_perc',
            'A per cent of total physical memory used by ' +
            'the process running Sanic',
            multiprocess_mode=multiprocess_mode
        )

    if metrics_list:
        for name, pm_metric in metrics_list:
            app.metrics[name] = pm_metric 
Example #8
Source File: metrics.py    From desec-stack with MIT License 5 votes vote down vote up
def set_histogram(name, *args, **kwargs):
    metrics[name] = Histogram(name, *args, **kwargs)


#models.py metrics 
Example #9
Source File: metrics.py    From armada with Apache License 2.0 5 votes vote down vote up
def __init__(self, prefix, description, labels):
        """
        :param prefix: prefix to use for each metric name
        :param description: description of action to use in metric description
        :param labels: label names to define for each metric
        """
        self.full_prefix = '{}_{}'.format(self.__class__._PREFIX, prefix)
        self.progress = prometheus_client.Gauge(
            '{}_attempt_inprogress'.format(self.full_prefix),
            'In progress attempts to {}'.format(description),
            labels,
            registry=REGISTRY,
            multiprocess_mode='livesum')
        self.attempt_total = prometheus_client.Counter(
            '{}_attempt_total'.format(self.full_prefix),
            'Total attempts to {}'.format(description),
            labels,
            registry=REGISTRY)
        self.failure_total = prometheus_client.Counter(
            '{}_failure_total'.format(self.full_prefix),
            'Total failures to {}'.format(description),
            labels,
            registry=REGISTRY)
        self.duration = prometheus_client.Histogram(
            '{}_duration_seconds'.format(self.full_prefix),
            'Seconds to {}'.format(description),
            labels,
            registry=REGISTRY) 
Example #10
Source File: metrics.py    From armada with Apache License 2.0 5 votes vote down vote up
def __init__(self, prefix, description, labels):
        super().__init__(prefix, description, labels)
        self.concurrency = prometheus_client.Histogram(
            '{}_concurrency_count'.format(self.full_prefix),
            'Count of charts being handled concurrently for chart',
            labels,
            registry=REGISTRY) 
Example #11
Source File: metrics.py    From armada with Apache License 2.0 5 votes vote down vote up
def __init__(self, prefix, description, labels):
        super().__init__(prefix, description, labels)
        self.timeout = prometheus_client.Histogram(
            '{}_timeout_duration_seconds'.format(self.full_prefix),
            'Configured timeout (in seconds) to {}'.format(description),
            labels,
            registry=REGISTRY)
        self.timeout_usage = prometheus_client.Histogram(
            '{}_timeout_usage_ratio'.format(self.full_prefix),
            'Ratio of duration to timeout to {}'.format(description),
            labels,
            registry=REGISTRY) 
Example #12
Source File: instruments.py    From BentoML with Apache License 2.0 5 votes vote down vote up
def __init__(self, app, bento_service):
        self.app = app
        self.bento_service = bento_service

        from prometheus_client import Histogram, Counter, Gauge, CollectorRegistry

        service_name = self.bento_service.name
        namespace = config('instrument').get('default_namespace')
        # Use local registry instead of the global one to avoid duplicated metrics
        # register
        self.collector_registry = CollectorRegistry()

        self.metrics_request_duration = Histogram(
            name=service_name + '_request_duration_seconds',
            documentation=service_name + " API HTTP request duration in seconds",
            namespace=namespace,
            labelnames=['endpoint', 'service_version', 'http_response_code'],
            registry=self.collector_registry,
        )
        self.metrics_request_total = Counter(
            name=service_name + "_request_total",
            documentation='Totoal number of HTTP requests',
            namespace=namespace,
            labelnames=['endpoint', 'service_version', 'http_response_code'],
            registry=self.collector_registry,
        )
        self.metrics_request_in_progress = Gauge(
            name=service_name + "_request_in_progress",
            documentation='Totoal number of HTTP requests in progress now',
            namespace=namespace,
            labelnames=['endpoint', 'service_version'],
            registry=self.collector_registry,
        ) 
Example #13
Source File: test_resources.py    From dagster with Apache License 2.0 5 votes vote down vote up
def test_prometheus_histogram():
    @solid(required_resource_keys={'prometheus'})
    def prometheus_solid(context):
        h = Histogram(
            'pipeline_runtime_seconds',
            'Description of histogram',
            registry=context.resources.prometheus.registry,
        )
        h.observe(4.7)
        recorded = context.resources.prometheus.registry.get_sample_value(
            'pipeline_runtime_seconds_sum'
        )
        assert abs(4.7 - recorded) < EPS

    assert execute_solid(prometheus_solid, run_config=ENV, mode_def=MODE).success 
Example #14
Source File: celery_prometheus_exporter.py    From celery-prometheus-exporter with MIT License 5 votes vote down vote up
def get_histogram_buckets_from_evn(env_name):
    if env_name in os.environ:
        buckets = decode_buckets(os.environ.get(env_name))
    else:
        if hasattr(prometheus_client.Histogram, 'DEFAULT_BUCKETS'): # pragma: no cover
            buckets = prometheus_client.Histogram.DEFAULT_BUCKETS
        else: # pragma: no cover
            # For prometheus-client < 0.3.0 we cannot easily access
            # the default buckets:
            buckets = (.005, .01, .025, .05, .075, .1, .25, .5, .75, 1.0, 2.5, 5.0, 7.5, 10.0, float('inf'))
    return buckets 
Example #15
Source File: faucet_metrics.py    From faucet with Apache License 2.0 5 votes vote down vote up
def _histogram(self, var, var_help, labels, buckets):
        return Histogram(var, var_help, labels, buckets=buckets, registry=self._reg) # pylint: disable=unexpected-keyword-arg 
Example #16
Source File: middleware.py    From python-prometheus-demo with MIT License 5 votes vote down vote up
def setup_metrics(app, app_name):
    app['REQUEST_COUNT'] = Counter(
      'requests_total', 'Total Request Count',
      ['app_name', 'method', 'endpoint', 'http_status']
    )
    app['REQUEST_LATENCY'] = Histogram(
        'request_latency_seconds', 'Request latency',
        ['app_name', 'endpoint']
    )

    app['REQUEST_IN_PROGRESS'] = Gauge(
        'requests_in_progress_total', 'Requests in progress',
        ['app_name', 'endpoint', 'method']
    )

    app.middlewares.insert(0, prom_middleware(app_name))
    app.router.add_get("/metrics", metrics)