Python prometheus_client.generate_latest() Examples

The following are 30 code examples of prometheus_client.generate_latest(). 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: nova_services.py    From prometheus-openstack-exporter with Apache License 2.0 6 votes vote down vote up
def get_stats(self):
        registry = CollectorRegistry()
        labels = ['region', 'host', 'service', 'state']
        services_stats_cache = self.get_cache_data()
        for services_stat in services_stats_cache:
            stat_gauge = Gauge(
                self.gauge_name_sanitize(
                    services_stat['stat_name']),
                'Openstack Nova Service statistic',
                labels,
                registry=registry)
            label_values = [self.osclient.region,
                            services_stat.get('host', ''),
                            services_stat.get('service', ''),
                            services_stat.get('state', '')]
            stat_gauge.labels(*label_values).set(services_stat['stat_value'])
        return generate_latest(registry) 
Example #2
Source File: cinder_services.py    From prometheus-openstack-exporter with Apache License 2.0 6 votes vote down vote up
def get_stats(self):
        registry = CollectorRegistry()
        labels = ['region', 'host', 'service', 'state']
        cinder_services_stats_cache = self.get_cache_data()
        for cinder_services_stat in cinder_services_stats_cache:
            stat_gauge = Gauge(
                self.gauge_name_sanitize(
                    cinder_services_stat['stat_name']),
                'Openstack Cinder Service statistic',
                labels,
                registry=registry)
            label_values = [self.osclient.region,
                            cinder_services_stat.get('host', ''),
                            cinder_services_stat.get('service', ''),
                            cinder_services_stat.get('state', '')]
            stat_gauge.labels(
                *
                label_values).set(
                cinder_services_stat['stat_value'])
        return generate_latest(registry) 
Example #3
Source File: conftest.py    From integrations-core with 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 #4
Source File: check_os_api.py    From prometheus-openstack-exporter with Apache License 2.0 6 votes vote down vote up
def get_stats(self):
        registry = CollectorRegistry()
        labels = ['region', 'url', 'service']
        check_api_data_cache = self.get_cache_data()
        for check_api_data in check_api_data_cache:
            label_values = [
                check_api_data['region'],
                check_api_data['url'],
                check_api_data['service']]
            gague_name = self.gauge_name_sanitize(
                "check_{}_api".format(check_api_data['service']))
            check_gauge = Gauge(
                gague_name,
                'Openstack API check. fail = 0, ok = 1 and unknown = 2',
                labels,
                registry=registry)
            check_gauge.labels(*label_values).set(check_api_data['status'])
        return generate_latest(registry) 
Example #5
Source File: neutron_agents.py    From prometheus-openstack-exporter with Apache License 2.0 6 votes vote down vote up
def get_stats(self):
        registry = CollectorRegistry()
        labels = ['region', 'host', 'service', 'state']
        neutron_agent_stats_cache = self.get_cache_data()
        for neutron_agent_stat in neutron_agent_stats_cache:
            stat_gauge = Gauge(
                self.gauge_name_sanitize(
                    neutron_agent_stat['stat_name']),
                'Openstack Neutron agent statistic',
                labels,
                registry=registry)
            label_values = [self.osclient.region,
                            neutron_agent_stat.get('host', ''),
                            neutron_agent_stat.get('service', ''),
                            neutron_agent_stat.get('state', '')]
            stat_gauge.labels(
                *
                label_values).set(
                neutron_agent_stat['stat_value'])
        return generate_latest(registry) 
Example #6
Source File: test_allocated_vm_collector.py    From azure-cost-mon with MIT License 6 votes vote down vote up
def test_single_machine(enable_login):
    responses.add(
        method='GET',
        url='https://management.azure.com/subscriptions/SUBSCRIPTION_ID/providers/Microsoft.Compute/virtualMachines?api-version=2017-03-30',
        match_querystring=True,
        json={'value': [
            {
                'id': '/subscriptions/SUBSCRIPTION_ID/resourceGroups/RESOURCE_GROUP/providers/Microsoft.Compute/virtualMachines/NAME',
                'location': 'WESTEUROPE',
                'properties': {'hardwareProfile': {'vmSize': 'SIZE'}}
            }
        ]})

    registry = CollectorRegistry()
    c = AzureAllocatedVMCollector('app_id', 'app_secret', 'tenant_id', ['SUBSCRIPTION_ID'], 'SERIES_NAME')
    registry.register(c)

    result = generate_latest(registry).decode('utf8').split('\n')
    assert 'SERIES_NAME{location="WESTEUROPE",resource_group="RESOURCE_GROUP",subscription="SUBSCRIPTION_ID",vm_size="SIZE"} 1.0' in result 
Example #7
Source File: test_enterprise_billing_collector.py    From azure-cost-mon with MIT License 6 votes vote down vote up
def test_extract_metrics(api_url, enrollment):

    responses.add(
        method='GET',
        url=api_url,
        match_querystring=True,
        json=sample_data
    )

    registry = CollectorRegistry()
    c = AzureEABillingCollector('costs', enrollment, 'ab123xy', 10)
    registry.register(c)

    result = generate_latest(registry).decode('utf8').split('\n')
    assert len(result) == 5

    expected_0 = 'costs{AccountName="platform",DepartmentName="engineering",MeterCategory="virtual network",MeterName="hours",MeterSubCategory="gateway hour",ResourceGroup="",SubscriptionName="production"} 0.0'
    expected_1 = 'costs{AccountName="platform",DepartmentName="engineering",MeterCategory="windows azure storage",MeterName="standard io - page blob/disk (gb)",MeterSubCategory="locally redundant",ResourceGroup="my-group",SubscriptionName="production"} 1.0'

    assert result[2] == expected_0
    assert result[3] == expected_1 
Example #8
Source File: test_twisted.py    From client_python with 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 #9
Source File: views.py    From kqueen with MIT License 6 votes vote down vote up
def root():
    # check access
    ip_whitelist = ip_network(current_app.config.get('PROMETHEUS_WHITELIST'))
    if ip_address(request.remote_addr) not in ip_whitelist:
        _jwt_required(current_app.config['JWT_DEFAULT_REALM'])

    MU = MetricUpdater()
    registry = CollectorRegistry()
    multiprocess.MultiProcessCollector(registry)

    data = generate_latest(registry)

    response = make_response(data)
    response.headers['Content-Type'] = CONTENT_TYPE_LATEST
    response.headers['Content-Length'] = str(len(data))
    logger.info('Kqueen metrics updating')
    MU.update_metrics()

    return response 
Example #10
Source File: test_helpers.py    From kqueen with MIT License 5 votes vote down vote up
def latest(self):
        user = UserFixture(namespace='demoorg')
        m = MetricUpdater()
        m.update_metrics()

        result = generate_latest().decode('utf-8')
        user.destroy()
        return result 
Example #11
Source File: flask_app.py    From python-prometheus-demo with MIT License 5 votes vote down vote up
def metrics():
    return Response(prometheus_client.generate_latest(), mimetype=CONTENT_TYPE_LATEST) 
Example #12
Source File: middleware.py    From python-prometheus-demo with MIT License 5 votes vote down vote up
def setup_metrics(app):
    app.before_request(start_timer)
    # The order here matters since we want stop_timer
    # to be executed first
    app.after_request(record_request_data)
    app.after_request(stop_timer)

    @app.route('/metrics')
    def metrics():
        return Response(prometheus_client.generate_latest(registry), mimetype=CONTENT_TYPE_LATEST) 
Example #13
Source File: proton_prometheus.py    From PROTON with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def on_get(self, req, resp):
        data = generate_latest(self.registry)
        resp.content_type = 'text/plain; version=0.0.4; charset=utf-8'
        resp.body = str(data.decode('utf-8')) 
Example #14
Source File: prometheus.py    From pyproxy-async with Apache License 2.0 5 votes vote down vote up
def get_data() -> str:
        return generate_latest(registry).decode() 
Example #15
Source File: aiohttp_resources.py    From async-worker with MIT License 5 votes vote down vote up
def metrics_route_handler(request: web.Request) -> web.Response:
    response = web.Response(
        body=generate_latest(registry=REGISTRY), content_type="text/plain"
    )
    return response 
Example #16
Source File: metrics_handler.py    From trinity with MIT License 5 votes vote down vote up
def handle(
        chain: BaseBeaconChain,
        event_bus: EndpointAPI,
        request: web.Request
    ) -> web.Response:
        logger = logging.getLogger('trinity.http.handlers.metrics_handler')
        try:
            if request.method == 'GET':
                logger.debug('Receiving GET request: %s', request.path)
                if request.path == '/metrics':
                    await process_metrics(chain, event_bus)
                    data = generate_latest(registry)
                    return web.Response(
                        body=data,
                        content_type='text/plain',
                    )

                return web.json_response({
                    'status': 'ok'
                })
            else:
                return response_error(f"Metrics Server doesn't support {request.method} request")
        except Exception as e:
            msg = f"[metrics_handler] Error: {e}"
            logger.error(msg)
            return response_error(msg) 
Example #17
Source File: test_reserved_vm_collector.py    From azure-cost-mon with MIT License 5 votes vote down vote up
def test_base_reservation(enable_login, single_order, reservation):
    responses.add(
        method='GET',
        url='https://management.azure.com/providers/Microsoft.Capacity/reservationOrders/SINGLE_ORDER/reservations?api-version=2017-11-01',
        match_querystring=True,
        json={'value': [reservation]})

    registry = CollectorRegistry()
    c = AzureReservedVMCollector('app_id', 'app_secret', 'tenant_id', 'SERIES_NAME')
    registry.register(c)

    result = generate_latest(registry).decode('utf8').split('\n')
    assert 'SERIES_NAME{duration="1-year",location="WESTEUROPE",subscription="SOME_SUBSCRIPTION",vm_size="SIZE"} 42.0' in result
    assert 'SERIES_NAME_next_expiration{duration="1-year",location="WESTEUROPE",subscription="SOME_SUBSCRIPTION",vm_size="SIZE"} 1546300800.0' in result 
Example #18
Source File: test_metrics_endpoint.py    From async-worker with MIT License 5 votes vote down vote up
def test_metric_cant_override_registry(self):
        registry = CollectorRegistry()
        Counter("counter", "Doc", registry=registry)
        Gauge("gauge", "Doc", registry=registry)
        Histogram("histogram", "Doc", registry=registry)
        self.assertEqual(b"", generate_latest(registry)) 
Example #19
Source File: test_reserved_vm_collector.py    From azure-cost-mon with MIT License 5 votes vote down vote up
def test_shared_reservation(enable_login, single_order, reservation):
    reservation['properties']['appliedScopeType'] = 'Shared'
    responses.add(
        method='GET',
        url='https://management.azure.com/providers/Microsoft.Capacity/reservationOrders/SINGLE_ORDER/reservations?api-version=2017-11-01',
        match_querystring=True,
        json={'value': [reservation]})

    registry = CollectorRegistry()
    c = AzureReservedVMCollector('app_id', 'app_secret', 'tenant_id', 'SERIES_NAME')
    registry.register(c)

    result = generate_latest(registry).decode('utf8').split('\n')
    assert 'SERIES_NAME{duration="1-year",location="WESTEUROPE",subscription="shared",vm_size="SIZE"} 42.0' in result
    assert 'SERIES_NAME_next_expiration{duration="1-year",location="WESTEUROPE",subscription="shared",vm_size="SIZE"} 1546300800.0' in result 
Example #20
Source File: flask_exporter.py    From Prometheus with GNU General Public License v3.0 5 votes vote down vote up
def hahah():
    mem.labels(memtype='total').set(psutil.virtual_memory().total)
    mem.labels(memtype='available').set(psutil.virtual_memory().available)
    mem.labels(memtype='used').set(psutil.virtual_memory().used)
    i.info({'version': '1.2.3', 'buildhost': 'foo@bar'})
    return Response(prometheus_client.generate_latest(REGISTRY),mimetype="text/plain") 
Example #21
Source File: middleware.py    From python-prometheus-demo with MIT License 5 votes vote down vote up
def metrics(request):
    resp = web.Response(body=prometheus_client.generate_latest())
    resp.content_type = CONTENT_TYPE_LATEST
    return resp 
Example #22
Source File: flask_app.py    From python-prometheus-demo with MIT License 5 votes vote down vote up
def metrics():
    return Response(prometheus_client.generate_latest(), mimetype=CONTENT_TYPE_LATEST) 
Example #23
Source File: vmware_exporter.py    From vmware_exporter with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def generate_latest_metrics(self, request):
        """ gets the latest metrics """
        section = request.args.get(b'section', [b'default'])[0].decode('utf-8')
        if section not in self.config.keys():
            logging.info("{} is not a valid section, using default".format(section))
            section = 'default'

        if self.config[section].get('vsphere_host') and self.config[section].get('vsphere_host') != "None":
            vsphere_host = self.config[section].get('vsphere_host')
        elif request.args.get(b'target', [None])[0]:
            vsphere_host = request.args.get(b'target', [None])[0].decode('utf-8')
        elif request.args.get(b'vsphere_host', [None])[0]:
            vsphere_host = request.args.get(b'vsphere_host')[0].decode('utf-8')
        else:
            request.setResponseCode(500)
            logging.info("No vsphere_host or target defined")
            request.write(b'No vsphere_host or target defined!\n')
            request.finish()
            return

        collector = VmwareCollector(
            vsphere_host,
            self.config[section]['vsphere_user'],
            self.config[section]['vsphere_password'],
            self.config[section]['collect_only'],
            self.config[section]['specs_size'],
            self.config[section]['fetch_custom_attributes'],
            self.config[section]['ignore_ssl'],
            self.config[section]['fetch_tags'],
            self.config[section]['fetch_alarms'],
        )
        metrics = yield collector.collect()

        registry = CollectorRegistry()
        registry.register(ListCollector(metrics))
        output = generate_latest(registry)

        request.setHeader("Content-Type", "text/plain; charset=UTF-8")
        request.setResponseCode(200)
        request.write(output)
        request.finish() 
Example #24
Source File: test_reserved_vm_collector.py    From azure-cost-mon with MIT License 5 votes vote down vote up
def test_three_year_reservation(enable_login, single_order, reservation):
    reservation['properties']['effectiveDateTime'] = '2016-01-01T00:00:00.000000Z'
    responses.add(
        method='GET',
        url='https://management.azure.com/providers/Microsoft.Capacity/reservationOrders/SINGLE_ORDER/reservations?api-version=2017-11-01',
        match_querystring=True,
        json={'value': [reservation]})

    registry = CollectorRegistry()
    c = AzureReservedVMCollector('app_id', 'app_secret', 'tenant_id', 'SERIES_NAME')
    registry.register(c)

    result = generate_latest(registry).decode('utf8').split('\n')
    assert 'SERIES_NAME{duration="3-year",location="WESTEUROPE",subscription="SOME_SUBSCRIPTION",vm_size="SIZE"} 42.0' in result
    assert 'SERIES_NAME_next_expiration{duration="3-year",location="WESTEUROPE",subscription="SOME_SUBSCRIPTION",vm_size="SIZE"} 1546300800.0' in result 
Example #25
Source File: test_reserved_vm_collector.py    From azure-cost-mon with MIT License 5 votes vote down vote up
def test_expired_reservation(enable_login, single_order, reservation):
    reservation['properties']['provisioningState'] = 'Canceled'
    responses.add(
        method='GET',
        url='https://management.azure.com/providers/Microsoft.Capacity/reservationOrders/SINGLE_ORDER/reservations?api-version=2017-11-01',
        match_querystring=True,
        json={'value': [reservation]})

    registry = CollectorRegistry()
    c = AzureReservedVMCollector('app_id', 'app_secret', 'tenant_id', 'SERIES_NAME')
    registry.register(c)

    result = generate_latest(registry).decode('utf8').split('\n')
    assert 'SERIES_NAME{duration="1-year",location="WESTEUROPE",subscription="shared",vm_size="SIZE"} 42.0' not in result
    assert 'SERIES_NAME_next_expiration{duration="1-year",location="WESTEUROPE",subscription="shared",vm_size="SIZE"} 1546300800.0' not in result 
Example #26
Source File: test_enterprise_billing_collector.py    From azure-cost-mon with MIT License 5 votes vote down vote up
def test_failing_requests(api_url, enrollment, status):
    registry = CollectorRegistry()
    c = AzureEABillingCollector('cloud_costs', enrollment, 'abc123xyz', 42.3)
    registry.register(c)

    responses.add(
        method='GET',
        url=api_url,
        match_querystring=True,
        status=status
    )

    with pytest.raises(requests.HTTPError):
        generate_latest(registry) 
Example #27
Source File: test_allocated_vm_collector.py    From azure-cost-mon with MIT License 5 votes vote down vote up
def test_multiple_machines_in_multiple_subscriptions(enable_login):
    responses.add(
        method='GET',
        url='https://management.azure.com/subscriptions/SUBSCRIPTION_A/providers/Microsoft.Compute/virtualMachines?api-version=2017-03-30',
        match_querystring=True,
        json={'value': [
            {
                'id': '/subscriptions/SUBSCRIPTION_A/resourceGroups/RESOURCE_GROUP/providers/Microsoft.Compute/virtualMachines/NAME',
                'location': 'WESTEUROPE',
                'properties': {'hardwareProfile': {'vmSize': 'SIZE'}}
            }
        ]})
    responses.add(
        method='GET',
        url='https://management.azure.com/subscriptions/SUBSCRIPTION_B/providers/Microsoft.Compute/virtualMachines?api-version=2017-03-30',
        match_querystring=True,
        json={'value': [
            {
                'id': '/subscriptions/SUBSCRIPTION_B/resourceGroups/RESOURCE_GROUP/providers/Microsoft.Compute/virtualMachines/NAME',
                'location': 'WESTEUROPE',
                'properties': {'hardwareProfile': {'vmSize': 'SIZE'}}
            }
        ]})

    registry = CollectorRegistry()
    c = AzureAllocatedVMCollector('app_id', 'app_secret', 'tenant_id', ['SUBSCRIPTION_A', 'SUBSCRIPTION_B'], 'SERIES_NAME')
    registry.register(c)

    result = generate_latest(registry).decode('utf8').split('\n')
    assert 'SERIES_NAME{location="WESTEUROPE",resource_group="RESOURCE_GROUP",subscription="SUBSCRIPTION_A",vm_size="SIZE"} 1.0' in result
    assert 'SERIES_NAME{location="WESTEUROPE",resource_group="RESOURCE_GROUP",subscription="SUBSCRIPTION_B",vm_size="SIZE"} 1.0' in result 
Example #28
Source File: views.py    From azure-cost-mon with MIT License 5 votes vote down vote up
def metrics():
    registry = CollectorRegistry()
    _register_collectors(registry)

    try:
        content = generate_latest(registry)
        return content, 200, {'Content-Type': CONTENT_TYPE_LATEST}
    except Exception as e:
        abort(Response("Scrape failed: {}".format(e), status=502)) 
Example #29
Source File: oscache.py    From prometheus-openstack-exporter with Apache License 2.0 5 votes vote down vote up
def get_stats(self):
        registry = CollectorRegistry()
        labels = ['region']
        label_values = [self.region]
        duration = Gauge('openstack_exporter_cache_refresh_duration_seconds',
                         'Cache refresh duration in seconds.',
                         labels, registry=registry)
        duration.labels(*label_values).set(self.duration)
        return generate_latest(registry) 
Example #30
Source File: prometheus_app.py    From postgraas_server with Apache License 2.0 5 votes vote down vote up
def metrics():
    try:
        content = generate_latest(REGISTRY)
        return content, 200, {'Content-Type': CONTENT_TYPE_LATEST}
    except Exception as error:
        logging.exception("Any exception occured during scraping")
        abort(Response("Scrape failed: {}".format(error), status=502))