forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add snapshot tests for prometheus text format
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...er/opentelemetry-exporter-prometheus/tests/__snapshots__/test_prometheus_text_format.ambr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# serializer version: 1 | ||
# name: test_counter2 | ||
''' | ||
# HELP test_with_collision_total My description | ||
# TYPE test_with_collision_total counter | ||
test_with_collision_total{hello_world="hello_world"} 1.0 | ||
''' | ||
# --- | ||
# name: test_counter[my.counter.name-s] | ||
''' | ||
# HELP my_counter_name_seconds_total My description | ||
# TYPE my_counter_name_seconds_total counter | ||
my_counter_name_seconds_total{foo1_bar_baz="hello world"} 10.0 | ||
''' | ||
# --- | ||
# name: test_counter[request.count-{requests}] | ||
''' | ||
# HELP request_count_total My description | ||
# TYPE request_count_total counter | ||
request_count_total{foo1_bar_baz="hello world"} 10.0 | ||
''' | ||
# --- |
79 changes: 79 additions & 0 deletions
79
exporter/opentelemetry-exporter-prometheus/tests/test_prometheus_text_format.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Copyright The OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Callable, Iterable | ||
from syrupy import SnapshotAssertion | ||
|
||
import pytest | ||
from prometheus_client import generate_latest | ||
|
||
from opentelemetry.exporter.prometheus import PrometheusMetricReader | ||
from opentelemetry.sdk.metrics import MeterProvider | ||
|
||
|
||
@pytest.fixture | ||
def prometheus_metric_reader() -> PrometheusMetricReader: | ||
return PrometheusMetricReader(disable_target_info=True) | ||
|
||
|
||
@pytest.fixture | ||
def meter_provider( | ||
prometheus_metric_reader: PrometheusMetricReader, | ||
) -> Iterable[MeterProvider]: | ||
mp = MeterProvider(metric_readers=[prometheus_metric_reader]) | ||
yield mp | ||
mp.shutdown(timeout_millis=100) | ||
|
||
|
||
@pytest.fixture | ||
def get_prometheus_text( | ||
prometheus_metric_reader: PrometheusMetricReader, | ||
) -> Callable[[], str]: | ||
def result() -> str: | ||
result_bytes = generate_latest(prometheus_metric_reader._collector) | ||
return result_bytes.decode("utf-8").strip() | ||
|
||
return result | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"counter_name,unit", | ||
[("my.counter.name", "s"), ("request.count", "{requests}")], | ||
) | ||
def test_counter( | ||
counter_name: str, | ||
unit: str, | ||
meter_provider: MeterProvider, | ||
get_prometheus_text: Callable[[], str], | ||
snapshot: SnapshotAssertion, | ||
) -> None: | ||
counter = meter_provider.get_meter("scope").create_counter( | ||
counter_name, unit=unit, description="My description" | ||
) | ||
counter.add(10, {"foo1.bar:baz": "hello world"}) | ||
assert get_prometheus_text() == snapshot | ||
|
||
|
||
def test_counter2( | ||
meter_provider: MeterProvider, | ||
get_prometheus_text: Callable[[], str], | ||
snapshot: SnapshotAssertion, | ||
) -> None: | ||
counter = meter_provider.get_meter("scope").create_counter( | ||
"test.with.collision", unit="1", description="My description" | ||
) | ||
counter.add( | ||
1, {"hello_world": "hello_world", "hello.world": "hello.world"} | ||
) | ||
assert get_prometheus_text() == snapshot |