Example #1
0
def push_stats_to_prometheus(maas_name, push_gateway):
    metrics = create_metrics(STATS_DEFINITIONS,
                             registry=prom_cli.CollectorRegistry())
    update_prometheus_stats(metrics)
    prom_cli.push_to_gateway(push_gateway,
                             job="stats_for_%s" % maas_name,
                             registry=metrics.registry)
Example #2
0
 def generate_latest(self):
     """Generate a bytestring with metric values."""
     if self.registry is not None:
         registry = self.registry
         if registry is prom_cli.REGISTRY:
             # when using the global registry, setup up multiprocess
             # support. In this case, a separate registry needs to be used
             # for generating the samples.
             registry = prom_cli.CollectorRegistry()
             from prometheus_client import multiprocess
             multiprocess.MultiProcessCollector(registry)
         return prom_cli.generate_latest(registry)
Example #3
0
def prometheus_stats_handler(request):
    have_prometheus = (
        PROMETHEUS_SUPPORTED and
        Config.objects.get_config('prometheus_enabled'))
    if not have_prometheus:
        return HttpResponseNotFound()

    metrics = create_metrics(
        STATS_DEFINITIONS,
        registry=prom_cli.CollectorRegistry())
    update_prometheus_stats(metrics)
    return HttpResponse(
        content=metrics.generate_latest(), content_type="text/plain")
Example #4
0
def create_metrics(metric_definitions):
    """Return a PrometheusMetrics from the specified definitions."""
    if not PROMETHEUS_SUPPORTED:
        return PrometheusMetrics(registry=None, metrics=None)
    registry = prom_cli.CollectorRegistry()
    metrics = {}
    for metric in metric_definitions:
        cls = getattr(prom_cli, metric.type)
        metrics[metric.name] = cls(metric.name,
                                   metric.description,
                                   metric.labels,
                                   registry=registry)
    return PrometheusMetrics(registry=registry, metrics=metrics)
Example #5
0
def prometheus_stats_handler(request):
    configs = Config.objects.get_configs(['prometheus_enabled', 'uuid'])
    have_prometheus = PROMETHEUS_SUPPORTED and configs['prometheus_enabled']
    if not have_prometheus:
        return HttpResponseNotFound()

    metrics = create_metrics(
        STATS_DEFINITIONS,
        extra_labels={'maas_id': configs['uuid']},
        update_handlers=[update_prometheus_stats],
        registry=prom_cli.CollectorRegistry())
    return HttpResponse(
        content=metrics.generate_latest(), content_type="text/plain")
Example #6
0
def prometheus_stats_handler(request):
    configs = Config.objects.get_configs(["prometheus_enabled", "uuid"])
    have_prometheus = PROMETHEUS_SUPPORTED and configs["prometheus_enabled"]
    if not have_prometheus:
        return HttpResponseNotFound()

    global _METRICS
    if not _METRICS:
        _METRICS = create_metrics(
            STATS_DEFINITIONS,
            extra_labels={"maas_id": configs["uuid"]},
            update_handlers=[update_prometheus_stats],
            registry=prom_cli.CollectorRegistry(),
        )

    return HttpResponse(
        content=_METRICS.generate_latest(), content_type="text/plain"
    )