SIENTIAPDE-1083: add unit tests for metrics.py.

This commit is contained in:
Bruno Domingues
2025-05-28 15:42:58 -03:00
parent 14c71580e0
commit 12e9151b74
2 changed files with 297 additions and 0 deletions

0
tests/unit/__init__.py Normal file
View File

297
tests/unit/test_metrics.py Normal file
View File

@@ -0,0 +1,297 @@
# tests/unit/test_metrics.py
import pytest
from prometheus_client import Counter, Gauge, Histogram
import ingestor.metrics as metrics
# --- Test Functions for Each Metric (Corrected for v0.22.0 _name behavior) ---
def test_app_loop_count():
"""Verify the definition of APP_LOOP_COUNT."""
assert metrics.APP_LOOP_COUNT is not None
assert isinstance(metrics.APP_LOOP_COUNT, Counter)
assert metrics.APP_LOOP_COUNT._name == "app_main_loop" # REMOVED _total
assert set(metrics.APP_LOOP_COUNT._labelnames) == {"pod_id"}
def test_app_loop_duration():
"""Verify the definition of APP_LOOP_DURATION."""
assert metrics.APP_LOOP_DURATION is not None
assert isinstance(metrics.APP_LOOP_DURATION, Histogram)
assert (
metrics.APP_LOOP_DURATION._name == "app_main_loop_duration_seconds"
) # Histograms don't have _total
assert set(metrics.APP_LOOP_DURATION._labelnames) == {"pod_id"}
def test_app_errors_total():
"""Verify the definition of APP_ERRORS_TOTAL."""
assert metrics.APP_ERRORS_TOTAL is not None
assert isinstance(metrics.APP_ERRORS_TOTAL, Counter)
assert metrics.APP_ERRORS_TOTAL._name == "app_errors" # REMOVED _total
assert set(metrics.APP_ERRORS_TOTAL._labelnames) == {"pod_id"}
def test_app_up():
"""Verify the definition of APP_UP."""
assert metrics.APP_UP is not None
assert isinstance(metrics.APP_UP, Gauge)
assert metrics.APP_UP._name == "app_up" # Gauges don't have _total
assert set(metrics.APP_UP._labelnames) == {"pod_id"}
def test_ingestors_ativos():
"""Verify the definition of INGESTORS_ATIVOS."""
assert metrics.INGESTORS_ATIVOS is not None
assert isinstance(metrics.INGESTORS_ATIVOS, Gauge)
assert metrics.INGESTORS_ATIVOS._name == "ingestor_active_total"
assert set(metrics.INGESTORS_ATIVOS._labelnames) == set()
def test_slots_totais():
"""Verify the definition of SLOTS_TOTAIS."""
assert metrics.SLOTS_TOTAIS is not None
assert isinstance(metrics.SLOTS_TOTAIS, Gauge)
assert metrics.SLOTS_TOTAIS._name == "ingestor_slots_total"
assert set(metrics.SLOTS_TOTAIS._labelnames) == set()
def test_leases_totais():
"""Verify the definition of LEASES_TOTAIS."""
assert metrics.LEASES_TOTAIS is not None
assert isinstance(metrics.LEASES_TOTAIS, Gauge)
assert metrics.LEASES_TOTAIS._name == "ingestor_leases_total"
assert set(metrics.LEASES_TOTAIS._labelnames) == set()
def test_slots_managed():
"""Verify the definition of SLOTS_MANAGED."""
assert metrics.SLOTS_MANAGED is not None
assert isinstance(metrics.SLOTS_MANAGED, Gauge)
assert metrics.SLOTS_MANAGED._name == "ingestor_slots_managed_current"
assert set(metrics.SLOTS_MANAGED._labelnames) == {"pod_id"}
def test_slots_acquired():
"""Verify the definition of SLOTS_ACQUIRED."""
assert metrics.SLOTS_ACQUIRED is not None
assert isinstance(metrics.SLOTS_ACQUIRED, Counter)
assert metrics.SLOTS_ACQUIRED._name == "ingestor_slots_acquired" # REMOVED _total
assert set(metrics.SLOTS_ACQUIRED._labelnames) == {"pod_id"}
def test_slots_released():
"""Verify the definition of SLOTS_RELEASED."""
assert metrics.SLOTS_RELEASED is not None
assert isinstance(metrics.SLOTS_RELEASED, Counter)
assert metrics.SLOTS_RELEASED._name == "ingestor_slots_released" # REMOVED _total
assert set(metrics.SLOTS_RELEASED._labelnames) == {"pod_id"}
def test_opc_managers_active():
"""Verify the definition of OPC_MANAGERS_ACTIVE."""
assert metrics.OPC_MANAGERS_ACTIVE is not None
assert isinstance(metrics.OPC_MANAGERS_ACTIVE, Gauge)
assert metrics.OPC_MANAGERS_ACTIVE._name == "ingestor_opc_managers_active"
assert set(metrics.OPC_MANAGERS_ACTIVE._labelnames) == {"pod_id"}
def test_opc_subscription_errors():
"""Verify the definition of OPC_SUBSCRIPTION_ERRORS."""
assert metrics.OPC_SUBSCRIPTION_ERRORS is not None
assert isinstance(metrics.OPC_SUBSCRIPTION_ERRORS, Counter)
assert (
metrics.OPC_SUBSCRIPTION_ERRORS._name == "ingestor_opc_subscription_errors"
) # REMOVED _total
assert set(metrics.OPC_SUBSCRIPTION_ERRORS._labelnames) == {
"pod_id",
"server",
"slot",
}
def test_opc_connections_total():
"""Verify the definition of OPC_CONNECTIONS_TOTAL."""
assert metrics.OPC_CONNECTIONS_TOTAL is not None
assert isinstance(metrics.OPC_CONNECTIONS_TOTAL, Counter)
assert (
metrics.OPC_CONNECTIONS_TOTAL._name == "opc_connections_initiated"
) # REMOVED _total
assert set(metrics.OPC_CONNECTIONS_TOTAL._labelnames) == {"pod_id", "server_name"}
def test_opc_connections_failed():
"""Verify the definition of OPC_CONNECTIONS_FAILED."""
assert metrics.OPC_CONNECTIONS_FAILED is not None
assert isinstance(metrics.OPC_CONNECTIONS_FAILED, Counter)
assert (
metrics.OPC_CONNECTIONS_FAILED._name == "opc_connections_failed"
) # REMOVED _total
assert set(metrics.OPC_CONNECTIONS_FAILED._labelnames) == {"pod_id", "server_name"}
def test_opc_connection_status():
"""Verify the definition of OPC_CONNECTION_STATUS."""
assert metrics.OPC_CONNECTION_STATUS is not None
assert isinstance(metrics.OPC_CONNECTION_STATUS, Gauge)
assert metrics.OPC_CONNECTION_STATUS._name == "opc_connection_status"
assert set(metrics.OPC_CONNECTION_STATUS._labelnames) == {
"pod_id",
"server_name",
"server_url",
}
def test_opc_subscriptions_created():
"""Verify the definition of OPC_SUBSCRIPTIONS_CREATED."""
assert metrics.OPC_SUBSCRIPTIONS_CREATED is not None
assert isinstance(metrics.OPC_SUBSCRIPTIONS_CREATED, Counter)
assert (
metrics.OPC_SUBSCRIPTIONS_CREATED._name == "opc_subscriptions_created"
) # REMOVED _total
assert set(metrics.OPC_SUBSCRIPTIONS_CREATED._labelnames) == {
"pod_id",
"server_name",
"slot_name",
}
def test_opc_tags_subscribed():
"""Verify the definition of OPC_TAGS_SUBSCRIBED."""
assert metrics.OPC_TAGS_SUBSCRIBED is not None
assert isinstance(metrics.OPC_TAGS_SUBSCRIBED, Gauge)
assert metrics.OPC_TAGS_SUBSCRIBED._name == "opc_tags_subscribed_current"
assert set(metrics.OPC_TAGS_SUBSCRIBED._labelnames) == {"pod_id", "server_name"}
def test_opc_datachange_notifications():
"""Verify the definition of OPC_DATACHANGE_NOTIFICATIONS."""
assert metrics.OPC_DATACHANGE_NOTIFICATIONS is not None
assert isinstance(metrics.OPC_DATACHANGE_NOTIFICATIONS, Counter)
assert (
metrics.OPC_DATACHANGE_NOTIFICATIONS._name == "opc_datachange_notifications"
) # REMOVED _total
assert set(metrics.OPC_DATACHANGE_NOTIFICATIONS._labelnames) == {
"pod_id",
"server_name",
"tag_id",
"tag_name",
}
def test_opc_tag_last_value():
"""Verify the definition of OPC_TAG_LAST_VALUE."""
assert metrics.OPC_TAG_LAST_VALUE is not None
assert isinstance(metrics.OPC_TAG_LAST_VALUE, Gauge)
assert metrics.OPC_TAG_LAST_VALUE._name == "opc_tag_last_value"
assert set(metrics.OPC_TAG_LAST_VALUE._labelnames) == {
"pod_id",
"server_name",
"tag_id",
"tag_name",
}
def test_opc_tag_last_read_timestamp():
"""Verify the definition of OPC_TAG_LAST_READ_TIMESTAMP."""
assert metrics.OPC_TAG_LAST_READ_TIMESTAMP is not None
assert isinstance(metrics.OPC_TAG_LAST_READ_TIMESTAMP, Gauge)
assert (
metrics.OPC_TAG_LAST_READ_TIMESTAMP._name
== "opc_tag_last_read_timestamp_seconds"
)
assert set(metrics.OPC_TAG_LAST_READ_TIMESTAMP._labelnames) == {
"pod_id",
"server_name",
"tag_id",
"tag_name",
}
def test_opc_cycles_without_data():
"""Verify the definition of OPC_CYCLES_WITHOUT_DATA."""
assert metrics.OPC_CYCLES_WITHOUT_DATA is not None
assert isinstance(metrics.OPC_CYCLES_WITHOUT_DATA, Gauge)
assert metrics.OPC_CYCLES_WITHOUT_DATA._name == "opc_cycles_without_data"
assert set(metrics.OPC_CYCLES_WITHOUT_DATA._labelnames) == {"pod_id", "server_name"}
def test_opc_reconnections_total():
"""Verify the definition of OPC_RECONNECTIONS_TOTAL."""
assert metrics.OPC_RECONNECTIONS_TOTAL is not None
assert isinstance(metrics.OPC_RECONNECTIONS_TOTAL, Counter)
assert (
metrics.OPC_RECONNECTIONS_TOTAL._name == "opc_reconnections_tried"
) # REMOVED _total
assert set(metrics.OPC_RECONNECTIONS_TOTAL._labelnames) == {"pod_id", "server_name"}
def test_kafka_messages_sent():
"""Verify the definition of KAFKA_MESSAGES_SENT."""
assert metrics.KAFKA_MESSAGES_SENT is not None
assert isinstance(metrics.KAFKA_MESSAGES_SENT, Counter)
assert metrics.KAFKA_MESSAGES_SENT._name == "kafka_messages_sent" # REMOVED _total
assert set(metrics.KAFKA_MESSAGES_SENT._labelnames) == {"pod_id", "topic"}
def test_kafka_messages_errors():
"""Verify the definition of KAFKA_MESSAGES_ERRORS."""
assert metrics.KAFKA_MESSAGES_ERRORS is not None
assert isinstance(metrics.KAFKA_MESSAGES_ERRORS, Counter)
assert (
metrics.KAFKA_MESSAGES_ERRORS._name == "kafka_messages_errors"
) # REMOVED _total
assert set(metrics.KAFKA_MESSAGES_ERRORS._labelnames) == {"pod_id", "topic"}
def test_kafka_connection_status():
"""Verify the definition of KAFKA_CONNECTION_STATUS."""
assert metrics.KAFKA_CONNECTION_STATUS is not None
assert isinstance(metrics.KAFKA_CONNECTION_STATUS, Gauge)
assert metrics.KAFKA_CONNECTION_STATUS._name == "kafka_connection_status"
assert set(metrics.KAFKA_CONNECTION_STATUS._labelnames) == {"pod_id"}
def test_redis_operations_total():
"""Verify the definition of REDIS_OPERATIONS_TOTAL."""
assert metrics.REDIS_OPERATIONS_TOTAL is not None
assert isinstance(metrics.REDIS_OPERATIONS_TOTAL, Counter)
assert metrics.REDIS_OPERATIONS_TOTAL._name == "redis_operations" # REMOVED _total
assert set(metrics.REDIS_OPERATIONS_TOTAL._labelnames) == {"pod_id", "operation"}
def test_redis_operations_errors():
"""Verify the definition of REDIS_OPERATIONS_ERRORS."""
assert metrics.REDIS_OPERATIONS_ERRORS is not None
assert isinstance(metrics.REDIS_OPERATIONS_ERRORS, Counter)
assert (
metrics.REDIS_OPERATIONS_ERRORS._name == "redis_operations_errors"
) # REMOVED _total
assert set(metrics.REDIS_OPERATIONS_ERRORS._labelnames) == {"pod_id", "operation"}
def test_redis_operations_duration():
"""Verify the definition of REDIS_OPERATIONS_DURATION."""
assert metrics.REDIS_OPERATIONS_DURATION is not None
assert isinstance(metrics.REDIS_OPERATIONS_DURATION, Histogram)
assert (
metrics.REDIS_OPERATIONS_DURATION._name == "redis_operations_duration_seconds"
)
assert set(metrics.REDIS_OPERATIONS_DURATION._labelnames) == {"pod_id", "operation"}
def test_redis_connection_status():
"""Verify the definition of REDIS_CONNECTION_STATUS."""
assert metrics.REDIS_CONNECTION_STATUS is not None
assert isinstance(metrics.REDIS_CONNECTION_STATUS, Gauge)
assert metrics.REDIS_CONNECTION_STATUS._name == "redis_connection_status"
assert set(metrics.REDIS_CONNECTION_STATUS._labelnames) == {"pod_id"}
def test_notifications_sent():
"""Verify the definition of NOTIFICATIONS_SENT."""
assert metrics.NOTIFICATIONS_SENT is not None
assert isinstance(metrics.NOTIFICATIONS_SENT, Counter)
assert metrics.NOTIFICATIONS_SENT._name == "notifications_sent" # REMOVED _total
assert set(metrics.NOTIFICATIONS_SENT._labelnames) == {"pod_id", "level", "block"}