Python prometheus_client.Counter() Examples

The following are 30 code examples for showing how to use prometheus_client.Counter(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module prometheus_client , or try the search function .

Example 1
Project: CrawlerMonitor   Author: adrianyoung   File: monitor.py    License: MIT License 7 votes vote down vote up
def create_metric(self):
        # record app conf
        self.conf_info = Info('celery_conf_info','APP_CONF')
        self.conf_info_c = CollectorRegistry()

        # monitor worker info
        self.workers_info = Info('celery_workers_info', 'WORKER_INFO')
        self.workers_info_c = CollectorRegistry()

        # monitor worker info real-time
        self.workers_state = Gauge('celery_workers_state', 'WORKER_STATE', ['worker'])
        self.workers_state_c = CollectorRegistry()
        self.workers_processed = Gauge('celery_processed_tasks_total', 'WORKER_TASKS_PROCESSED', ['worker'])
        self.workers_processed_c = CollectorRegistry()
        self.workers_active = Gauge('celery_active_tasks_total', 'WORKER_TASKS_ACTIVE', ['worker'])
        self.workers_active_c = CollectorRegistry()

        # monitor tasks info
        self.tasks_counter = Counter('celery_tasks_total', 'TASK_COUNT_INFO', ['worker','task','result'])
        self.tasks_counter_c = CollectorRegistry()
        self.tasks_runtime = Summary('celery_tasks_seconds', 'TASK_RUNTIME', ['worker', 'task'])
        self.tasks_runtime_c = CollectorRegistry()
        self.tasks_info = Info('celery_tasks_info', 'TASK_INFO')
        self.tasks_info_c = CollectorRegistry() 
Example 2
Project: prometheus-pve-exporter   Author: prometheus-pve   File: http.py    License: Apache License 2.0 6 votes vote down vote up
def start_http_server(config, port, address=''):
    """
    Start a HTTP API server for Proxmox VE prometheus collector.
    """

    duration = Summary(
        'pve_collection_duration_seconds',
        'Duration of collections by the PVE exporter',
        ['module'],
    )
    errors = Counter(
        'pve_request_errors_total',
        'Errors in requests to PVE exporter',
        ['module'],
    )

    # Initialize metrics.
    for module in config.keys():
        # pylint: disable=no-member
        errors.labels(module)
        # pylint: disable=no-member
        duration.labels(module)

    app = PveExporterApplication(config, duration, errors)
    run_simple(address, port, app, threaded=True) 
Example 3
Project: ouroboros   Author: pyouroboros   File: dataexporters.py    License: MIT License 6 votes vote down vote up
def __init__(self, data_manager, config):
        self.config = config
        self.data_manager = data_manager
        self.http_server = prometheus_client.start_http_server(
            self.config.prometheus_port,
            addr=self.config.prometheus_addr
        )
        self.updated_containers_counter = prometheus_client.Counter(
            'containers_updated',
            'Count of containers updated',
            ['socket', 'container']
        )
        self.monitored_containers_gauge = prometheus_client.Gauge(
            'containers_being_monitored',
            'Gauge of containers being monitored',
            ['socket']
        )
        self.updated_all_containers_gauge = prometheus_client.Gauge(
            'all_containers_updated',
            'Count of total updated',
            ['socket']
        )
        self.logger = getLogger() 
Example 4
Project: prometheus_flask_exporter   Author: rycus86   File: __init__.py    License: MIT License 6 votes vote down vote up
def counter(self, name, description, labels=None, **kwargs):
        """
        Use a Counter to track the total number of invocations 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 Counter
        """

        return self._track(
            Counter,
            lambda metric, time: metric.inc(),
            kwargs, name, description, labels,
            registry=self.registry
        ) 
Example 5
Project: naz   Author: komuw   File: my_hook.py    License: MIT License 6 votes vote down vote up
def __init__(self) -> None:
        self.registry = prometheus_client.CollectorRegistry()
        _labels = ["project", "smpp_command", "state", "response_code"]
        self.counter = prometheus_client.Counter(
            name="number_of_messages",
            documentation="number of messages processed by naz.",
            labelnames=_labels,
            registry=self.registry,
        )
        self._LOOP: typing.Union[None, asyncio.events.AbstractEventLoop] = None
        self.thread_name_prefix = "naz_benchmarks_hook_pool"

        # go to prometheus dashboard(http://localhost:9000/) & you can run queries like:
        # 1. container_memory_rss{name="naz_cli", container_label_com_docker_compose_service="naz_cli"}
        # 2. container_memory_rss{name=~"naz_cli|message_producer"}
        # 3. number_of_messages_total{project="naz_benchmarks"}
        # 4. rate(number_of_messages_total{smpp_command="submit_sm",state="request"}[30s])  # msg sending over the past 30seconds 
Example 6
Project: InfraBox   Author: SAP   File: checker.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, conn, args):
        self.conn = conn
        self.args = args
        self.ha_enabled = get_env("INFRABOX_HA_ENABLED")
        self.monitoring_enabled = get_env("INFRABOX_MONITORING_ENABLED")
        self.namespace = get_env("INFRABOX_GENERAL_SYSTEM_NAMESPACE")
        self.check_interval = int(get_env('INFRABOX_HA_CHECK_INTERVAL'))
        self.active_timeout = get_env('INFRABOX_HA_ACTIVE_TIMEOUT')
        self.cluster_name = get_env('INFRABOX_CLUSTER_NAME')
        self.logger = get_logger("checker")
        self.root_url = get_env("INFRABOX_ROOT_URL")
        self.is_cluster_healthy = True
        self.retry_times = 0
        self.max_retry_times = 3
        self.infrabox_api_call_errors = Counter(
            'infrabox_api_errors_total', 
            'Errors in requests to InfraBox API')
        self.storage_checker_errors = Counter(
            'storage_checker_errors_total', 
            'Errors uploding/downloading files to/from storage')
        self.infrabox_dashboard_access_errors = Counter(
            'infrabox_dashboard_access_errors_total', 
            'Errors acessing dashboard')
        self.slack = CheckerSlackHook.get_from_env(self.logger) 
Example 7
Project: dagster   Author: dagster-io   File: test_resources.py    License: Apache License 2.0 6 votes vote down vote up
def test_prometheus_counter():
    @solid(required_resource_keys={'prometheus'})
    def prometheus_solid(context):
        c = Counter(
            'some_counter_seconds',
            'Description of this counter',
            registry=context.resources.prometheus.registry,
        )
        c.inc()
        c.inc(1.6)
        recorded = context.resources.prometheus.registry.get_sample_value(
            'some_counter_seconds_total'
        )
        assert abs(2.6 - recorded) < EPS

    assert execute_solid(prometheus_solid, run_config=ENV, mode_def=MODE).success 
Example 8
Project: openstacksdk   Author: openstack   File: cloud_region.py    License: Apache License 2.0 6 votes vote down vote up
def get_prometheus_counter(self):
        registry = self.get_prometheus_registry()
        if not registry or not prometheus_client:
            return
        counter = getattr(registry, '_openstacksdk_counter', None)
        if not counter:
            counter = prometheus_client.Counter(
                'openstack_http_requests',
                'Number of HTTP requests made to an OpenStack service',
                labelnames=[
                    'method', 'endpoint', 'service_type', 'status_code'
                ],
                registry=registry,
            )
            registry._openstacksdk_counter = counter
        return counter 
Example 9
Project: client_python   Author: prometheus   File: test_wsgi.py    License: Apache License 2.0 6 votes vote down vote up
def validate_metrics(self, metric_name, help_text, increments):
        """
        WSGI app serves the metrics from the provided registry.
        """
        c = Counter(metric_name, help_text, registry=self.registry)
        for _ in range(increments):
            c.inc()
        # Create and run WSGI app
        app = make_wsgi_app(self.registry)
        outputs = app(self.environ, self.capture)
        # Assert outputs
        self.assertEqual(len(outputs), 1)
        output = outputs[0].decode('utf8')
        # Status code
        self.assertEqual(self.captured_status, "200 OK")
        # Headers
        self.assertEqual(len(self.captured_headers), 1)
        self.assertEqual(self.captured_headers[0], ("Content-Type", CONTENT_TYPE_LATEST))
        # Body
        self.assertIn("# HELP " + metric_name + "_total " + help_text + "\n", output)
        self.assertIn("# TYPE " + metric_name + "_total counter\n", output)
        self.assertIn(metric_name + "_total " + str(increments) + ".0\n", output) 
Example 10
Project: client_python   Author: prometheus   File: test_twisted.py    License: Apache License 2.0 6 votes vote down vote up
def test_reports_metrics(self):
        """
        ``MetricsResource`` serves the metrics from the provided registry.
        """
        c = Counter('cc', 'A counter', registry=self.registry)
        c.inc()

        root = Resource()
        root.putChild(b'metrics', MetricsResource(registry=self.registry))
        server = reactor.listenTCP(0, Site(root))
        self.addCleanup(server.stopListening)

        agent = Agent(reactor)
        port = server.getHost().port
        url = "http://localhost:{port}/metrics".format(port=port)
        d = agent.request(b"GET", url.encode("ascii"))

        d.addCallback(readBody)
        d.addCallback(self.assertEqual, generate_latest(self.registry))

        return d 
Example 11
Project: biggraphite   Author: criteo   File: cassandra.py    License: Apache License 2.0 6 votes vote down vote up
def expose_metrics(metrics, cluster_name=""):
    """Adaptor to notify prometheus of Cassandra's metrics change."""
    metrics_adp = {}

    def counter_adaptor(cpt, fn):
        def inner(*args, **kwargs):
            cpt.inc()
            fn(*args, **kwargs)

        return inner

    for attr in dir(metrics):
        if attr.startswith("on_"):
            metric_name = "bg_cassandra_" + cluster_name + "_" + attr[3:]
            cpt = prometheus_client.Counter(metric_name, "")
            metrics_adp[metric_name] = cpt
            setattr(metrics, attr, counter_adaptor(cpt, attr))

    return metrics_adp 
Example 12
Project: anchore-engine   Author: anchore   File: metrics.py    License: Apache License 2.0 6 votes vote down vote up
def counter_inc(name, step=1, description="", **kwargs):
    global metrics

    if not enabled:
        return True

    try:
        if name not in metrics:
            metrics[name] = Counter(name, description, list(kwargs.keys()))

        if kwargs:
            metrics[name].labels(**kwargs).inc(step)
        else:
            metrics[name].inc(step)

    except Exception as err:
        logger.warn("adding metric failed - exception: " + str(err))

    return True 
Example 13
Project: PROTON   Author: PruthviKumarBK   File: proton_prometheus.py    License: 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 14
Project: integrations-core   Author: DataDog   File: conftest.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def poll_mock():
    registry = CollectorRegistry()
    g1 = Gauge('metric1', 'processor usage', ['matched_label', 'node', 'flavor'], registry=registry)
    g1.labels(matched_label="foobar", node="host1", flavor="test").set(99.9)
    g2 = Gauge('metric2', 'memory usage', ['matched_label', 'node', 'timestamp'], registry=registry)
    g2.labels(matched_label="foobar", node="host2", timestamp="123").set(12.2)
    c1 = Counter('counter1', 'hits', ['node'], registry=registry)
    c1.labels(node="host2").inc(42)
    g3 = Gauge('metric3', 'memory usage', ['matched_label', 'node', 'timestamp'], registry=registry)
    g3.labels(matched_label="foobar", node="host2", timestamp="456").set(float('inf'))

    poll_mock_patch = mock.patch(
        'requests.get',
        return_value=mock.MagicMock(
            status_code=200,
            iter_lines=lambda **kwargs: ensure_unicode(generate_latest(registry)).split("\n"),
            headers={'Content-Type': "text/plain"},
        ),
    )
    with poll_mock_patch:
        yield 
Example 15
Project: sanic-prometheus   Author: dkruchinin   File: metrics.py    License: 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 16
Project: desec-stack   Author: desec-io   File: metrics.py    License: MIT License 5 votes vote down vote up
def set_counter(name, *args, **kwargs):
    metrics[name] = Counter(name, *args, **kwargs) 
Example 17
Project: armada   Author: airshipit   File: metrics.py    License: 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 18
Project: royal-chaos   Author: KTH   File: health_metric.py    License: MIT License 5 votes vote down vote up
def main(args):
    dataset = extract_messages(args.dataset)
    c_labels = ['hostname', 'result']
    basic_health_checking = Counter('health_checking', 'A health checking metric for hedwig', c_labels)
    start_http_server(args.port)

    while True:
        result = health_checking(dataset)
        logging.info("health_checking finished, result: %s"%result)
        # export metrics
        basic_health_checking.labels(
            hostname="production",
            result=result
        ).inc()
        time.sleep(15) 
Example 19
Project: zentral   Author: zentralopensource   File: kombu.py    License: Apache License 2.0 5 votes vote down vote up
def setup_prometheus_metrics(self):
        self.preprocessed_events_counter = Counter(
            "preprocessed_events",
            "Preprocessed events",
            ["routing_key"]
        )
        self.produced_events_counter = Counter(
            "produced_events",
            "Produced events",
            ["event_type"]
        ) 
Example 20
Project: zentral   Author: zentralopensource   File: kombu.py    License: Apache License 2.0 5 votes vote down vote up
def setup_prometheus_metrics(self):
        self.enriched_events_counter = Counter(
            "enriched_events",
            "Enriched events",
            ["event_type"]
        )
        self.produced_events_counter = Counter(
            "produced_events",
            "Produced events",
            ["event_type"]
        ) 
Example 21
Project: zentral   Author: zentralopensource   File: kombu.py    License: Apache License 2.0 5 votes vote down vote up
def setup_prometheus_metrics(self):
        self.processed_events_counter = Counter(
            "processed_events",
            "Processed events",
            ["event_type"]
        ) 
Example 22
Project: zentral   Author: zentralopensource   File: kombu.py    License: Apache License 2.0 5 votes vote down vote up
def setup_prometheus_metrics(self):
        self.stored_events_counter = Counter(
            "stored_events",
            "Stored events",
            ["event_type"]
        ) 
Example 23
Project: BentoML   Author: bentoml   File: instruments.py    License: 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 24
Project: quay   Author: quay   File: progress.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, prom_pushgateway_addr, prom_job, labels, total_steps_num=None):
        self._total_steps_num = total_steps_num
        self._completed_steps = 0.0

        registry = CollectorRegistry()

        self._migration_completion_percent = Gauge(
            "migration_completion_percent",
            "Estimate of the completion percentage of the job",
            registry=registry,
        )
        self._migration_complete_total = Counter(
            "migration_complete_total",
            "Binary value of whether or not the job is complete",
            registry=registry,
        )
        self._migration_failed_total = Counter(
            "migration_failed_total",
            "Binary value of whether or not the job has failed",
            registry=registry,
        )
        self._migration_items_completed_total = Counter(
            "migration_items_completed_total",
            "Number of items this migration has completed",
            registry=registry,
        )

        self._push = partial(
            push_to_gateway,
            prom_pushgateway_addr,
            job=prom_job,
            registry=registry,
            grouping_key=labels,
        ) 
Example 25
Project: client_python   Author: prometheus   File: test_asgi.py    License: Apache License 2.0 5 votes vote down vote up
def validate_metrics(self, metric_name, help_text, increments):
        """
        ASGI app serves the metrics from the provided registry.
        """
        c = Counter(metric_name, help_text, registry=self.registry)
        for _ in range(increments):
            c.inc()
        # Create and run ASGI app
        app = make_asgi_app(self.registry)
        self.seed_app(app)
        self.send_default_request()
        # Assert outputs
        outputs = self.get_all_output()
        # Assert outputs
        self.assertEqual(len(outputs), 2)
        response_start = outputs[0]
        self.assertEqual(response_start['type'], 'http.response.start')
        response_body = outputs[1]
        self.assertEqual(response_body['type'], 'http.response.body')
        # Status code
        self.assertEqual(response_start['status'], 200)
        # Headers
        self.assertEqual(len(response_start['headers']), 1)
        self.assertEqual(response_start['headers'][0], (b"Content-Type", CONTENT_TYPE_LATEST.encode('utf8')))
        # Body
        output = response_body['body'].decode('utf8')
        self.assertIn("# HELP " + metric_name + "_total " + help_text + "\n", output)
        self.assertIn("# TYPE " + metric_name + "_total counter\n", output)
        self.assertIn(metric_name + "_total " + str(increments) + ".0\n", output) 
Example 26
Project: fiaas-deploy-daemon   Author: fiaas   File: __init__.py    License: Apache License 2.0 5 votes vote down vote up
def _connect_signals():
    rs_counter = Counter("web_request_started", "HTTP requests received")
    request_started.connect(lambda s, *a, **e: rs_counter.inc(), weak=False)
    rf_counter = Counter("web_request_finished", "HTTP requests successfully handled")
    request_finished.connect(lambda s, *a, **e: rf_counter.inc(), weak=False)
    re_counter = Counter("web_request_exception", "Failed HTTP requests")
    got_request_exception.connect(lambda s, *a, **e: re_counter.inc(), weak=False) 
Example 27
Project: fiaas-deploy-daemon   Author: fiaas   File: factory.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, factory, transformers, config):
        self._factory = factory
        self._transformers = transformers
        self._config = config
        self._supported_versions = [factory.version] + transformers.keys()
        self._fiaas_counter = Counter("fiaas_yml_version", "The version of fiaas.yml used", ["version", "app_name"]) 
Example 28
Project: faucet   Author: faucetsdn   File: faucet_metrics.py    License: Apache License 2.0 5 votes vote down vote up
def _counter(self, var, var_help, labels):
        return Counter(var, var_help, labels, registry=self._reg) # pylint: disable=unexpected-keyword-arg 
Example 29
Project: jaeger-client-python   Author: jaegertracing   File: prometheus.py    License: Apache License 2.0 5 votes vote down vote up
def create_counter(self, name, tags=None):
        counter = self._get_metric(Counter, name, tags)

        def increment(value):
            counter.inc(value)
        return increment 
Example 30
Project: python-prometheus-demo   Author: amitsaha   File: middleware.py    License: 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)