【问题标题】:How to solve "INVALID" is not a valid start token in Prometheus如何解决“INVALID”不是 Prometheus 中的有效起始令牌
【发布时间】:2021-08-03 22:58:09
【问题描述】:

我正在尝试为 Prometheus 创建一个简单的 python 导出器。导出器将生成一个随机数,我希望 Prometheus 抓取它,但我收到错误“INVALID”不是有效的开始令牌。这是我的代码:

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")

这是我的配置文件

- job_name: 'my_randomizer'
    metrics_path: /
    static_configs:
    - targets: ['0.0.0.0:5050']

【问题讨论】:

  • 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是截图,不是链接到外部门户)有问题(不是评论)。还有其他有用的信息。
  • 添加到prometheus前请确保页面显示正确。见stackoverflow.com/questions/57823842/…

标签: python docker prometheus


【解决方案1】:

仪表的字符串值用于人工调试,它不是有效的说明格式。

https://github.com/prometheus/client_python#flask 是有关如何使用 Flask 公开的文档。

【讨论】:

    【解决方案2】:

    感谢大家花时间回答我的问题,但我想通了。我的代码缺少 CollectorRegistry。此外,通过将我的图表制作成数组帮助我做到了这一点。

    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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 2020-11-08
      • 2021-10-02
      相关资源
      最近更新 更多