即使 prometheus 源显示变量,也无法在 grafana 中获取数据

Posted

技术标签:

【中文标题】即使 prometheus 源显示变量,也无法在 grafana 中获取数据【英文标题】:Unable to fetch data in grafana even though prometheus source is showing the variable 【发布时间】:2020-02-22 14:45:40 【问题描述】:

我有一个 Python 脚本,它在 localhost:8093 上抛出数据。 Prometheus 配置为从此源获取数据,Grafana 配置为将 Prometheus 作为源。现在,我可以看到端口 8093 上的变量(我的脚本在其中抛出数据),但我无法在 Grafana 中看到任何值 - 当我在 Grafana 中键入查询时,我可以看到变量的建议,但它们有 null(N /A) 值。

我试图检查数据是否来自 8093 到 prometheus,但无法检查。然而,在 8 个变量中,有一个会好起来的。其余都缺席(null)。

我的代码抛出数据:

import json
import time
from statistics import median

import requests
from prometheus_client import (Enum, Gauge, Histogram, Summary,
                               start_http_server)

from exchanges_config import *
from self_health_check.utils import send_slack_notification
start_http_server(8093)


def notify_driver_health_check_issue(exception):
    send_slack_notification(message="Some issue in driver_health_check!")
    send_slack_notification(message=str(exception))


def notify_exchange_error(exchange, exception):
    send_slack_notification(
        message="Something wrong or mismatching in 's response -> ".format(
            exchange, exception
        )
    )


slack_notification_counter = 0
while True:
    # get the response for all exchanges asap, all at once to avoid time difference
    exmo_response = bitlish_response = bittrex_spot_price_zec_usd_response = \
        bittrex_spot_price_zec_btc_response = bittrex_transaction_volume_response = gemini_zec_usd_response = \
        gemini_zec_btc_response = bitfinex_zec_usd_response = bitfinex_zec_btc_response = \
        binance_zec_btc_response = coinbase_zec_usd_response = \
        kraken_zec_usd_response = coinjar_zec_btc_response = None
    try:
        exmo_response = requests.get(url=EXMO_URL, timeout=5)
    except Exception as e:
        notify_driver_health_check_issue(e)
        pass
    try:
        bitlish_response = requests.get(url=BITLISH_URL, timeout=5)
    except Exception as e:
        notify_driver_health_check_issue(e)
        pass
    try:
        bittrex_spot_price_zec_usd_response = requests.get(
            url=BITTREX_ZEC_USD_SPOT_PRICE_URL, timeout=5)
    except Exception as e:
        notify_driver_health_check_issue(e)
        pass
    try:
        bittrex_spot_price_zec_btc_response = requests.get(
            url=BITTREX_ZEC_BTC_SPOT_PRICE_URL, timeout=5)
    except Exception as e:
        notify_driver_health_check_issue(e)
        pass
    try:
        bittrex_transaction_volume_response = requests.get(
            url=BITTREX_TRANSACTION_VOLUME_URL, timeout=5)
    except Exception as e:
        notify_driver_health_check_issue(e)
        pass
    try:
        gemini_zec_usd_response = requests.get(
            url=GEMINI_ZEC_USD_URL, timeout=5)
    except Exception as e:
        notify_driver_health_check_issue(e)
        pass
    try:
        gemini_zec_btc_response = requests.get(
            url=GEMINI_ZEC_BTC_URL, timeout=5)
    except Exception as e:
        notify_driver_health_check_issue(e)
        pass
    try:
        bitfinex_zec_usd_response = requests.get(
            url=BITFINEX_ZEC_USD_URL, timeout=5)
    except Exception as e:
        notify_driver_health_check_issue(e)
        pass
    try:
        bitfinex_zec_btc_response = requests.get(
            url=BITFINEX_ZEC_BTC_URL, timeout=5)
    except Exception as e:
        notify_driver_health_check_issue(e)
        pass
    try:
        binance_zec_btc_response = requests.get(
            url=BINANCE_ZEC_BTC_URL, timeout=5)
    except Exception as e:
        notify_driver_health_check_issue(e)
        pass
    try:
        coinbase_zec_usd_response = requests.get(url=COINBASE_ZEC_USD_URI, timeout=5, headers=
            'AUTHORIZATION': 'Bearer abd90df5f27a7b170cd775abf89d632b350b7c1c9d53e08b340cd9832ce52c2c')
    except Exception as e:
        notify_driver_health_check_issue(e)
        pass
    try:
        kraken_zec_usd_response = requests.get(
            url=KRAKEN_ZEC_USD_URL, timeout=5)
    except Exception as e:
        notify_driver_health_check_issue(e)
        pass
    try:
        coinjar_zec_btc_response = requests.get(
            url=COINJAR_ZEC_BTC_URL, timeout=5)
    except Exception as e:
        notify_driver_health_check_issue(e)
        pass

    # Exmo
    try:
        exmo_data = exmo_response.json()
        # setting transaction volume in USD
        exmo_usd_transaction_volume = exmo_data['ZEC_USD']['vol_curr']
        # setting zec_usd spot price
        exmo_usd_spot_price = float(exmo_data['ZEC_USD']['last_trade'])
        # setting transaction volume in BTC
        exmo_btc_transaction_volume = exmo_data['ZEC_BTC']['vol_curr']
        # setting zec_btc spot price
        exmo_btc_spot_price = float(exmo_data['ZEC_BTC']['last_trade'])
        # plotting usd and btc transaction volume and spot price
        if exmo_usd_transaction_volume == 0:
            set_state = '0'
        else:
            set_state = '1'
        EXMO_TRANSACTION_VOLUME_USD_PORT.state(set_state)
        if exmo_btc_transaction_volume == 0:
            set_state = '0'
        else:
            set_state = '1'
        EXMO_TRANSACTION_VOLUME_BTC_PORT.state(set_state)
        if exmo_usd_spot_price == 0:
            set_state = '0'
        else:
            set_state = '1'
        EXMO_SPOT_PRICE_USD_PORT.state(set_state)
        if exmo_btc_spot_price == 0:
            set_state = '0'
        else:
            set_state = '1'
        EXMO_SPOT_PRICE_BTC_PORT.state(set_state)
    except Exception as e:
        if exmo_response is None:
            send_slack_notification(message="Exmo response is None!")
        else:
            notify_exchange_error("Exmo", str(e))

    # Bitlish
    try:
        bitlish_data = bitlish_response.json()
        # setting transaction volume in USD
        bitlish_usd_transaction_volume = bitlish_data['zecusd']['updated']
        # setting zec_usd spot price
        bitlish_usd_spot_price = float(bitlish_data['zecusd']['last'])
        # setting transaction volume in BTC
        bitlish_btc_transaction_volume = bitlish_data['zecbtc']['updated']
        # setting zec_btc spot price
        bitlish_btc_spot_price = float(bitlish_data['zecbtc']['last'])
        # plotting usd and btc transaction volume and spot price
        if bitlish_usd_transaction_volume == 0:
            set_state = '0'
        else:
            set_state = '1'
        BITLISH_TRANSACTION_VOLUME_USD_PORT.state(set_state)
        if bitlish_btc_transaction_volume == 0:
            set_state = '0'
        else:
            set_state = '1'
        BITLISH_TRANSACTION_VOLUME_BTC_PORT.state(set_state)
        if bitlish_usd_spot_price == 0:
            set_state = '0'
        else:
            set_state = '1'
        BITLISH_SPOT_PRICE_USD_PORT.state(set_state)
        if bitlish_btc_spot_price == 0:
            set_state = '0'
        else:
            set_state = '1'
        BITLISH_SPOT_PRICE_BTC_PORT.state(set_state)
    except Exception as e:
        notify_exchange_error("Bitlish", str(e))

    # Bittrex
    try:
        bittrex_spot_price_zec_btc_data = bittrex_spot_price_zec_btc_response.json()
        bittrex_btc_spot_price = float(
            bittrex_spot_price_zec_btc_data['result']['Last'])
        bittrex_spot_price_zec_usd_data = bittrex_spot_price_zec_usd_response.json()
        bittrex_usd_spot_price = float(
            bittrex_spot_price_zec_usd_data['result']['Last'])
        bittrex_transaction_volume_data = bittrex_transaction_volume_response.json()
        for obj in bittrex_transaction_volume_data:
            if obj["symbol"] == "ZEC-USD":
                bittrex_usd_transaction_volume = obj['volume']
                if bittrex_usd_transaction_volume == 0:
                    set_state = '0'
                else:
                    set_state = '1'
                BITTREX_TRANSACTION_VOLUME_USD_PORT.state(set_state)
            if obj["symbol"] == "ZEC-BTC":
                bittrex_btc_transaction_volume = obj['volume']
                if bittrex_btc_transaction_volume == 0:
                    set_state = '0'
                else:
                    set_state = '1'
                BITTREX_TRANSACTION_VOLUME_BTC_PORT.state(set_state)
        if bittrex_usd_spot_price == 0:
            set_state = '0'
        else:
            set_state = '1'
        BITTREX_SPOT_PRICE_USD_PORT.state(set_state)
        if bittrex_btc_spot_price == 0:
            set_state = '0'
        else:
            set_state = '1'
        BITTREX_SPOT_PRICE_BTC_PORT.state(set_state)
    except Exception as e:
        notify_exchange_error("Bitrex", str(e))

    # Gemini
    try:
        gemini_zec_usd_data = gemini_zec_usd_response.json()
        gemini_zec_btc_data = gemini_zec_btc_response.json()
        gemini_usd_spot_price = float(gemini_zec_usd_data['last'])
        gemini_btc_spot_price = float(gemini_zec_btc_data['last'])
        gemini_usd_transaction_volume = gemini_zec_usd_data['volume']['ZEC']
        gemini_btc_transaction_volume = gemini_zec_btc_data['volume']['ZEC']
        if gemini_usd_spot_price == 0:
            set_state = '0'
        else:
            set_state = '1'
        GEMINI_SPOT_PRICE_USD_PORT.state(set_state)
        if gemini_btc_spot_price == 0:
            set_state = '0'
        else:
            set_state = '1'
        GEMINI_SPOT_PRICE_BTC_PORT.state(set_state)
        if gemini_usd_transaction_volume == 0:
            set_state = '0'
        else:
            set_state = '1'
        GEMINI_TRANSACTION_VOLUME_USD_PORT.state(set_state)
        if gemini_btc_transaction_volume == 0:
            set_state = '0'
        else:
            set_state = '1'
        GEMINI_TRANSACTION_VOLUME_BTC_PORT.state(set_state)
    except Exception as e:
        notify_exchange_error("Gemini", str(e))

    # Bitfinex
    try:
        bitfinex_zec_usd_data = bitfinex_zec_usd_response.json()
        bitfinex_zec_btc_data = bitfinex_zec_btc_response.json()
        bitfinex_usd_spot_price = float(bitfinex_zec_usd_data[0][-4])
        bitfinex_btc_spot_price = float(bitfinex_zec_btc_data[0][-4])
        bitfinex_usd_transaction_volume = bitfinex_zec_usd_data[0][-3]
        bitfinex_btc_transaction_volume = bitfinex_zec_btc_data[0][-3]
        if bitfinex_usd_spot_price == 0:
            set_state = '0'
        else:
            set_state = '1'
        BITFINEX_SPOT_PRICE_USD_PORT.state(set_state)
        if bitfinex_btc_spot_price == 0:
            set_state = '0'
        else:
            set_state = '1'
        BITFINEX_SPOT_PRICE_BTC_PORT.state(set_state)
        if bitfinex_usd_transaction_volume == 0:
            set_state = '0'
        else:
            set_state = '1'
        BITFINEX_TRANSACTION_VOLUME_USD_PORT.state(set_state)
        if bitfinex_btc_transaction_volume == 0:
            set_state = '0'
        else:
            set_state = '1'
        BITFINEX_TRANSACTION_VOLUME_BTC_PORT.state(set_state)
    except Exception as e:
        notify_exchange_error("Bitfinex", str(e))

    # Binance
    try:
        binance_zec_btc_data = binance_zec_btc_response.json()
        binance_btc_spot_price = float(binance_zec_btc_data['lastPrice'])
        binance_btc_transaction_volume = float(binance_zec_btc_data['volume'])
        if binance_btc_spot_price == 0:
            set_state = '0'
        else:
            set_state = '1'
        BINANCE_SPOT_PRICE_BTC_PORT.state(set_state)
        if binance_btc_transaction_volume == 0:
            set_state = '0'
        else:
            set_state = '1'
        BINANCE_TRANSACTION_VOLUME_BTC_PORT.state(set_state)
    except Exception as e:
        notify_exchange_error("Binance", str(e))

    # Coinbase
    try:
        coinbase_zec_usd_data = coinbase_zec_usd_response.json()['data']
        coinbase_usd_spot_price = float(coinbase_zec_usd_data['amount'])
        if coinbase_usd_spot_price == 0:
            set_state = '0'
        else:
            set_state = '1'
        COINBASE_SPOT_PRICE_USD_PORT.state(set_state)
    except Exception as e:
        notify_exchange_error("Coinbase", str(e))

    # Kraken
    try:
        kraken_zec_usd_data = kraken_zec_usd_response.json()[
            'result']['XZECZUSD']
        kraken_usd_spot_price = float(kraken_zec_usd_data['c'][0])
        kraken_usd_transaction_volume = float(kraken_zec_usd_data['c'][1])
        if kraken_usd_spot_price == 0:
            set_state = '0'
        else:
            set_state = '1'
        KRAKEN_SPOT_PRICE_USD_PORT.state(set_state)
        if kraken_usd_transaction_volume == 0:
            set_state = '0'
        else:
            set_state = '1'
        KRAKEN_TRANSACTION_VOLUME_USD_PORT.state(set_state)
    except Exception as e:
        notify_exchange_error("Kraken", str(e))

    # Coinjar
    try:
        coinjar_zec_btc_data = coinjar_zec_btc_response.json()
        coinjar_btc_spot_price = float(coinjar_zec_btc_data['last'])
        coinjar_btc_transaction_volume = float(coinjar_zec_btc_data['volume'])
        if coinjar_btc_spot_price == 0:
            set_state = '0'
        else:
            set_state = '1'
        COINJAR_SPOT_PRICE_BTC_PORT.state(set_state)
        if coinjar_btc_transaction_volume == 0:
            set_state = '0'
        else:
            set_state = '1'
        COINJAR_TRANSACTION_VOLUME_BTC_PORT.state(set_state)
    except Exception as e:
        notify_exchange_error("Coinjar", str(e))

    spot_price_usd = [exmo_usd_spot_price, bitlish_usd_spot_price,
                      bittrex_usd_spot_price, gemini_usd_spot_price, bitfinex_usd_spot_price, kraken_usd_spot_price]
    spot_price_btc = [exmo_btc_spot_price, bitlish_btc_spot_price,
                      bittrex_btc_spot_price, gemini_btc_spot_price, bitfinex_btc_spot_price, binance_btc_spot_price]
    spot_price_median_usd = median(spot_price_usd)
    spot_price_median_btc = median(spot_price_btc)
    # Create a config of exchange: price for many use-cases
    spot_price_usd_all_exchanges = 
        "Exmo": exmo_usd_spot_price,
        "Bitlish": bitlish_usd_spot_price,
        "Bittrex": bittrex_usd_spot_price,
        "Gemini": gemini_usd_spot_price,
        "Bitfinex": bitfinex_usd_spot_price,
        "Coinbase": coinbase_usd_spot_price,
        "Kraken": kraken_usd_spot_price
    
    spot_price_btc_all_exchanges = 
        "Exmo": exmo_btc_spot_price,
        "Bitlish": bitlish_btc_spot_price,
        "Bittrex": bittrex_btc_spot_price,
        "Gemini": gemini_btc_spot_price,
        "Bitfinex": bitfinex_btc_spot_price,
        "Binance": binance_btc_spot_price,
        "Coinjar": coinjar_btc_spot_price
    
    for exchange_usd_price in spot_price_usd_all_exchanges.keys():
        change_percentage = abs(
            spot_price_usd_all_exchanges[exchange_usd_price] - spot_price_median_usd) / spot_price_median_usd * 100.0
        USD_EXCHANGE[exchange_usd_price].set(change_percentage)
        ABSOLUTE_PRICE_USD_EXCHANGE[exchange_usd_price].set(
            spot_price_usd_all_exchanges[exchange_usd_price])
    for exchange_btc_price in spot_price_btc_all_exchanges.keys():
        change_percentage = abs(
            spot_price_btc_all_exchanges[exchange_btc_price] - spot_price_median_btc) / spot_price_median_btc * 100.0
        BTC_EXCHANGE[exchange_btc_price].set(change_percentage)
        ABSOLUTE_PRICE_BTC_EXCHANGE[exchange_btc_price].set(
            spot_price_btc_all_exchanges[exchange_btc_price])

    # transaction volume
    transaction_volume_usd_all_exchanges = 
        "Exmo": exmo_usd_transaction_volume,
        "Bitlish": bitlish_usd_transaction_volume,
        "Bittrex": bittrex_usd_transaction_volume,
        "Gemini": gemini_usd_transaction_volume,
        "Bitfinex": bitfinex_usd_transaction_volume,
        "Kraken": kraken_usd_transaction_volume
    
    transaction_volume_btc_all_exchanges = 
        "Exmo": exmo_btc_transaction_volume,
        "Bitlish": bitlish_btc_transaction_volume,
        "Bittrex": bittrex_btc_transaction_volume,
        "Gemini": gemini_btc_transaction_volume,
        "Bitfinex": bitfinex_btc_transaction_volume,
        "Binance": binance_btc_transaction_volume,
        "Coinjar": coinjar_btc_spot_price
    
    for transaction_volume_usd in transaction_volume_usd_all_exchanges.keys():
        ABSOLUTE_TRANSACTION_VOLUME_USD_EXCHANGE[transaction_volume_usd].set(
            transaction_volume_usd_all_exchanges[transaction_volume_usd])

    for transaction_volume_btc in transaction_volume_btc_all_exchanges.keys():
        ABSOLUTE_TRANSACTION_VOLUME_BTC_EXCHANGE[transaction_volume_btc].set(
            transaction_volume_btc_all_exchanges[transaction_volume_btc]
        )
    print(slack_notification_counter)
    slack_notification_counter += 1
    if slack_notification_counter % 100 == 0:
        send_slack_notification(
            message=" iterations of exchanges health checks done!".format(slack_notification_counter))
    time.sleep(150)

Exmo 的数据一切正常。其余的不是。 这是我在 Grafana 中的体验:

如您所见,kraken_usd_spot_price_absolute 可用,但当我选择它时,它为空。 但是,如果我对exmo_usd_spot_price_absolute 执行相同的操作,它会起作用。

我的投掷数据配置如下:

# Kraken Configs
KRAKEN_ZEC_USD_URL = "https://api.kraken.com/0/public/Ticker?pair=ZECUSD"
KRAKEN_SPOT_PRICE_USD_PORT = Enum('kraken_spot_price_usd_check',
                                  'Kraken Spot Price USD Check', states=['1', '0'])
KRAKEN_TRANSACTION_VOLUME_USD_PORT = Enum(
    'kraken_transaction_volume_usd_check', 'Kraken Transaction Volume USD Check', states=['1', '0'])
KRAKEN_USD_SPOT_PRICE_DEVIATION_GAUGE = Gauge('kraken_usd_spot_price_deviation_gauge',
                                              'kraken_usd_spot_price_deviation_gauge gauge')                                # Gauge Configs for Spot Price Deviation
KRAKEN_USD_SPOT_PRICE_ABSOLUTE = Gauge(
    'kraken_usd_spot_price_absolute', 'kraken_usd_spot_price_absolute_gauge')
KRAKEN_USD_TRANSACTION_VOLUME_ABSOLUTE = Gauge(
    'kraken_usd_transaction_volume_absolute', 'kraken_usd_transaction_volume_absolute_gauge')

# Exmo Configs
EXMO_URL = "https://api.exmo.com/v1/ticker/"
EXMO_SPOT_PRICE_USD_PORT = Enum('exmo_spot_price_usd_check',
                                'Exmo Spot Price USD Check', states=['1', '0'])
EXMO_SPOT_PRICE_BTC_PORT = Enum('exmo_spot_price_btc_check',
                                'Exmo Spot Price BTC Check', states=['1', '0'])
EXMO_TRANSACTION_VOLUME_USD_PORT = Enum(
    'exmo_transaction_volume_usd_check', 'Exmo Transaction Volume USD Check', states=['1', '0'])
EXMO_TRANSACTION_VOLUME_BTC_PORT = Enum(
    'exmo_transaction_volume_btc_check', 'Exmo Transaction Volume BTC Check', states=['1', '0'])
EXMO_SPOT_PRICE_MEDIAN_DEVIATION_PORT = Histogram(
    'exmo_spot_price_deviation_check', 'Exmo Spot Price Deviation Check')
EXMO_SUMMARY = Summary('request_latency_seconds', 'Description of summary')
EXMO_USD_SPOT_PRICE_DEVIATION_GAUGE = Gauge('exmo_usd_spot_price_deviation_gauge',
                                            'exmo_usd_spot_price_deviation_gauge gauge')
EXMO_BTC_SPOT_PRICE_DEVIATION_GAUGE = Gauge('exmo_btc_spot_price_deviation_gauge',
                                            'exmo_btc_spot_price_deviation_gauge gauge')
EXMO_USD_SPOT_PRICE_ABSOLUTE = Gauge(
    'exmo_usd_spot_price_absolute', 'exmo_usd_spot_price_absolute_gauge')
EXMO_BTC_SPOT_PRICE_ABSOLUTE = Gauge(
    'exmo_btc_spot_price_absolute', 'exmo_btc_spot_price_absolute_gauge')
EXMO_USD_TRANSACTION_VOLUME_ABSOLUTE = Gauge(
    'exmo_usd_transaction_volume_absolute', 'exmo_usd_transaction_volume_absolute_gauge')
EXMO_BTC_TRANSACTION_VOLUME_ABSOLUTE = Gauge(
    'exmo_btc_transaction_volume_absolute', 'exmo_btc_transaction_volume_absolute_gauge')

我已经处理好时间段。我已经检查了最后几分钟,即使那样我也只能看到 Exmo。一点上下文,exmo 是第一个添加的,然后是其余的。但是,我也重新启动了脚本和 Prometheus。不过,我可以看到 8093 中的变量。

我希望在我的 grafana 仪表板中看到所有这些变量。

【问题讨论】:

【参考方案1】:

我进一步调试,Prometheus 也有数据,但 Grafana 没有。事实证明,时间范围是错误的。即使选择了“最后 5 分钟”,范围查询也不知何故搞砸了,并且在那段时间内没有数据。取消选择/选择自定义范围,然后选择“最后 5 分钟”解决了问题。

【讨论】:

以上是关于即使 prometheus 源显示变量,也无法在 grafana 中获取数据的主要内容,如果未能解决你的问题,请参考以下文章

即使更新了各自的变量,也无法在 Flutter 中更新上下文

Prometheus监控运维实战十一:Pushgateway

即使已经定义了变量,所需的代码也无法找到它们

命令问题 - 即使在 PATH 环境变量中,“ng”命令也无法识别为内部或外部 [重复]

为啥即使在有状态小部件中使用 setstate 也无法获取更新的变量。因为我想在新的 TabBar 选项上更新我的 Container

为啥即使使用 setlocal enabledelayedexpansion 也无法访问 for 循环(批处理文件)中的局部变量? [复制]