簡體   English   中英

普羅米修斯計量指標的單元測試

[英]Unittests for prometheus gauge metrics

這是我的指標代碼:

from prometheus_client import Gauge

probe = Gauge('probe_success', '1 - probe success, 0 - probe failure'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
          ['probe_type', 'target', 'timeout', 'service', 'namespace', 'reason'])

如果對地址的請求成功,則最后的值為1,如果發生錯誤,則值為0。

def probe_success(probe_type, target, timeout, service, namespace, reason, value):
    probe.labels(probe_type, target, timeout, service, namespace, reason).set(value)

這就是我的指標的樣子:

  probe_success{namespace="test",probe_type="http",reason="MissingSchema",service="servicename",target="ddress-test1",timeout="5"} 0.0
probe_success{namespace="test",probe_type="http",reason="ConnectionError",service="servicename",target="http://address-test2",timeout="10"} 0.0
probe_success{namespace="test-2",probe_type="http",reason="",service="servicename",target="https://www.google.com",timeout="5"} 1.0

那么我該如何測試這些指標。 我閱讀了有關 REGISTRY.get_sample_value 的信息,但我沒有看到如何在量規設置方法上使用它。 一般來說,我不知道如何構建適當的測試用例。 如果有人有建議,我會很高興閱讀它們。

這是我的解決方案:

from prometheus_client import REGISTRY
from utils.metrics import probe_success



def test_probe_success_metric_when_the_probe_fails():
   
    # calling the metrics function, passing the needed parameters
    # setting the gauges value to be equal to 0 (which means 'False', by default it is 1 'True')
    probe_success(
        'http',
        'http://127.0.0.1:8000/',
        1,
        'test_service',
        'test_namespace',
        'Timeout',
        0,
    )

    # searching in prometheus_client REGISTRY by metrics function name, and certain arguments (passed above)

    after = REGISTRY.get_sample_value(
        'probe_success',
        {
            'probe_type': 'http',
            'target': 'http://127.0.0.1:8000/',
            'timeout': '1',
            'service': 'test_service',
            'namespace': 'test_namespace',
            'reason': 'Timeout',
        },
    )
    
    # assert whether found gauge value of after is equal to 0.0 (the value which we passed above)
    assert 0.0 == after


 # Here the procedure is the same as above, but with the difference that here the gauge value is set to 1 'True'.
def test_probe_success_metric_when_the_probe_success():

    probe_success(
        'http', 'http://127.0.0.1:8000/', 1, 'test_service',
        'test_namespace', '', 1
    )

   after = REGISTRY.get_sample_value(
        'probe_success',
        {
            'probe_type': 'http',
            'target': 'http://127.0.0.1:8000/',
            'timeout': '1',
            'service': 'test_service',
            'namespace': 'test_namespace',
            'reason': '',
        },
    )

assert 1.0 == after

這是一個帶有解釋但帶有計數器指標的示例: https ://www.robustperception.io/how-to-unit-test-prometheus-instrumentation

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM