0

I am trying to create a simple python exporter for Prometheus. The exporter will generate a random number and i want Prometheus to grab it, but i am getting the error "INVALID" is not a valid start token. Here is my code:

import prometheus_client
import random
import mimetypes
from prometheus_client import Gauge
import time

app = Flask (__name__)

randomizer = Gauge('python_randomizer', 'The random number')

@app.route("/")
def rand():
    randomizer = (random.randint(1, 100))
    time.sleep(1)   
    x = str (randomizer)
    return Response(x, mimetype="text/plain")

And here is my config file

- job_name: 'my_randomizer'
    metrics_path: /
    static_configs:
    - targets: ['0.0.0.0:5050']
F_Stain
  • 33
  • 4
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot, not link to external portal). There are other useful information. – furas May 13 '21 at 19:31
  • Please make sure the page display correctly before addign it to prometheus. See https://stackoverflow.com/questions/57823842/error-invalid-is-not-a-valid-start-token – Michael Doubez May 14 '21 at 12:23

2 Answers2

1

The string value of a Gauge is for human debugging, it is not valid exposition format.

https://github.com/prometheus/client_python#flask are the docs on how to expose with Flask.

brian-brazil
  • 31,678
  • 6
  • 93
  • 86
0

Thank you all for taking the time to answer my question but i figured it out. My code lack the CollectorRegistry. Also by making my graphs to array helped me do the trick.

import prometheus_client
import random
import mimetypes
import time
from prometheeus_client.core import CollectorRegistry
from prometheus_client import Gauge

app = Flask (__name__)

graphs = {}
graphs['r'] = Gauge('python_randomizer', 'The random number')

@app.route("/")
def creating_number():
    randomizer = (random.randint(1, 100))
    graphs['r'].set(randomizer)
    time.sleep(1)   
    return "This is a test"

@app.roote("/metric")
def exporting_number():
    res= []
    for k,v graphs.items():
        res.append(promtheus_client.generate_latest(v))
    return Response(res, mimetype="text/plain")
F_Stain
  • 33
  • 4