Skip to content

No input validation on MetricFamily classes resulting in confusing errors #362

@wolph

Description

@wolph

Using the code from the readme with the small alteration of an incorrect value, an export results in a useless stacktrace.

The code used:

from prometheus_client.core import GaugeMetricFamily, CounterMetricFamily, REGISTRY

class CustomCollector(object):
    def collect(self):
        yield GaugeMetricFamily('my_gauge', 'Help text', value=7)
        c = CounterMetricFamily('my_counter_total', 'Help text', labels=['foo'])
        c.add_metric(['bar'], None)
        c.add_metric(['baz'], 'baz')
        yield c

REGISTRY.register(CustomCollector())

The resulting trace:

Exception happened during processing of request from ('127.0.0.1', 12345)
Traceback (most recent call last):
  File "/usr/lib/python3.6/socketserver.py", line 651, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python3.6/socketserver.py", line 361, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python3.6/socketserver.py", line 721, in __init__
    self.handle()
  File "/usr/lib/python3.6/http/server.py", line 418, in handle
    self.handle_one_request()
  File "/usr/lib/python3.6/http/server.py", line 406, in handle_one_request
    method()
  File "prometheus_client/exposition.py", line 146, in do_GET
    output = encoder(registry)
  File "prometheus_client/exposition.py", line 119, in generate_latest
    output.append(sample_line(s))
  File "/opt/lg5commander/.venv/lib/python3.6/site-packages/prometheus_client/exposition.py", line 86, in sample_line
    s.name, labelstr, floatToGoString(s.value), timestamp)
  File "test.py", line 8, in floatToGoString
    d = float(d)
TypeError: float() argument must be a string or a number, not 'NoneType'

I am not too familiar with the code but I wonder if it would somehow be possible to merge the metrics_core.py code with metrics.py. Or is there a reason that, for example, a Gauge does not use the GaugeMetricFamily internally (or vice versa)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions