Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

8.3 KB
Raw Blame
from dataclasses import dataclass, field
from datetime import timedelta
from typing import Literal
from urllib.parse import urljoin

import clickhouse_connect
import clickhouse_connect.driver
import clickhouse_connect.driver.client
import py
from sqlalchemy import Engine

LegacyPath = py.path.local


@dataclass
class TestContainerUrlConfig:
    __test__ = False
    scheme: str
    address: str
    port: int

    def base(self) -> str:
        return f"{self.scheme}://{self.address}:{self.port}"

    def get(self, path: str) -> str:
        return urljoin(self.base(), path)

    def __cache__(self) -> dict:
        return {
            "scheme": self.scheme,
            "address": self.address,
            "port": self.port,
        }

    def __log__(self) -> str:
        return f"TestContainerUrlConfig(scheme={self.scheme}, address={self.address}, port={self.port})"


@dataclass
class TestContainerDocker:
    __test__ = False
    id: str
    host_configs: dict[str, TestContainerUrlConfig]
    container_configs: dict[str, TestContainerUrlConfig]

    @staticmethod
    def from_cache(cache: dict) -> "TestContainerDocker":
        return TestContainerDocker(
            id=cache["id"],
            host_configs={port: TestContainerUrlConfig(**config) for port, config in cache["host_configs"].items()},
            container_configs={port: TestContainerUrlConfig(**config) for port, config in cache["container_configs"].items()},
        )

    def __cache__(self) -> dict:
        return {
            "id": self.id,
            "host_configs": {port: config.__cache__() for port, config in self.host_configs.items()},
            "container_configs": {port: config.__cache__() for port, config in self.container_configs.items()},
        }

    def __log__(self) -> str:
        return f"TestContainerDocker(id={self.id}, host_configs={', '.join(host_config.__log__() for host_config in self.host_configs.values())}, container_configs={', '.join(container_config.__log__() for container_config in self.container_configs.values())})"


@dataclass
class TestContainerSQL:
    __test__ = False
    container: TestContainerDocker
    conn: Engine
    env: dict[str, str]

    def __cache__(self) -> dict:
        return {
            "container": self.container.__cache__(),
            "env": self.env,
        }

    def __log__(self) -> str:
        return f"TestContainerSQL(container={self.container.__log__()}, env={self.env})"


@dataclass
class TestContainerClickhouse:
    __test__ = False
    container: TestContainerDocker
    conn: clickhouse_connect.driver.client.Client
    env: dict[str, str]
    # Per-node containers when running a multi-node cluster. Empty for the
    # default single-node setup; nodes[0] is the node `conn`/`env` point at
    # (the initiator every query goes through).
    nodes: list[TestContainerDocker] = field(default_factory=list)

    def __cache__(self) -> dict:
        return {
            "container": self.container.__cache__(),
            "env": self.env,
            "nodes": [node.__cache__() for node in self.nodes],
        }

    def __log__(self) -> str:
        return f"TestContainerClickhouse(container={self.container.__log__()}, env={self.env})"


@dataclass
class TestContainerIDP:
    __test__ = False
    container: TestContainerDocker

    def __cache__(self) -> dict:
        return {
            "container": self.container.__cache__(),
        }

    def __log__(self) -> str:
        return f"TestContainerIDP(container={self.container.__log__()})"


@dataclass
class SigNoz:
    __test__ = False
    self: TestContainerDocker
    sqlstore: TestContainerSQL
    telemetrystore: TestContainerClickhouse
    zeus: TestContainerDocker
    gateway: TestContainerDocker

    def __cache__(self) -> dict:
        return self.self.__cache__()

    def __log__(self) -> str:
        return f"SigNoz(self={self.self.__log__()}, sqlstore={self.sqlstore.__log__()}, telemetrystore={self.telemetrystore.__log__()}, zeus={self.zeus.__log__()}, gateway={self.gateway.__log__()})"


@dataclass
class Operation:
    __test__ = False
    name: str

    def __cache__(self) -> dict:
        return {"name": self.name}

    def __log__(self) -> str:
        return f"Operation(name={self.name})"


@dataclass
class Network:
    __test__ = False
    id: str
    name: str

    def __cache__(self) -> dict:
        return {
            "id": self.id,
            "name": self.name,
        }

    def __log__(self) -> str:
        return f"Network(id={self.id}, name={self.name})"


@dataclass
class TLS:
    __test__ = False
    ca_cert_path: str
    ca_key_path: str

    @staticmethod
    def from_cache(cache: dict) -> "TLS":
        return TLS(ca_cert_path=cache["ca_cert_path"], ca_key_path=cache["ca_key_path"])

    def __cache__(self) -> dict:
        return {
            "ca_cert_path": self.ca_cert_path,
            "ca_key_path": self.ca_key_path,
        }

    def __log__(self) -> str:
        return f"TLS(ca_cert_path={self.ca_cert_path}, ca_key_path={self.ca_key_path})"


# Alerts related types


@dataclass(frozen=True)
class AlertData:
    # type of the alert data, one of 'metrics', 'logs', 'traces'
    type: Literal["metrics", "logs", "traces"]
    # path to the data file in testdata directory
    data_path: str


@dataclass(frozen=True)
class FiringAlert:
    # labels of the alert that is firing
    labels: dict[str, str]
    # annotations and other fields can be added later as per need


@dataclass(frozen=True)
class AlertExpectation:
    # whether we expect any alerts to be fired
    should_alert: bool
    # alerts that we expect to be fired
    expected_alerts: list[FiringAlert]
    # seconds to wait for the alerts to be fired, if no
    # alerts are fired in the expected time, the test will fail
    wait_time_seconds: int


@dataclass(frozen=True)
class AlertTestCase:
    # name of the test case
    name: str
    # path to the rule file in testdata directory
    rule_path: str
    # list of alert data that will be inserted into the database
    alert_data: list[AlertData]
    # list of alert expectations for the test case
    alert_expectation: AlertExpectation


@dataclass(frozen=True)
class NotificationValidation:
    # destination type of the notification, either webhook or email
    # slack, msteams, pagerduty, opsgenie, webhook channels send notifications through webhook
    # email channels send notifications through email
    destination_type: Literal["webhook", "email"]
    # validation data for validating the received notification payload
    validation_data: dict[str, any]


@dataclass(frozen=True)
class AMNotificationExpectation:
    # whether we expect any notifications to be fired or not, false when testing downtime scenarios
    # or don't expect any notifications to be fired in given time period
    should_notify: bool
    # seconds to wait for the notifications to be fired, if no
    # notifications are fired in the expected time, the test will fail
    wait_time_seconds: int
    # list of notifications to expect, as a single rule can trigger multiple notifications
    # spanning across different notifiers
    notification_validations: list[NotificationValidation]


@dataclass(frozen=True)
class AlertManagerNotificationTestCase:
    # name of the test case
    name: str
    # path to the rule file in testdata directory
    rule_path: str
    # list of alert data that will be inserted into the database for the rule to be triggered
    alert_data: list[AlertData]
    # configuration for the notification channel
    channel_config: dict[str, any]
    # notification expectations for the test case
    notification_expectation: AMNotificationExpectation


@dataclass(frozen=True)
class RoutePolicyExpectation:
    # expression of the route policy, each policy routes to its own channel
    expression: str
    # alerts expected on the policy's channel, empty when none should arrive
    expected_alerts: list[FiringAlert]


@dataclass(frozen=True)
class RoutingPolicyTestCase:
    # name of the test case
    name: str
    policies: list[RoutePolicyExpectation]


@dataclass(frozen=True)
class PlannedDowntimeTestCase:
    # name of the test case
    name: str
    # scope expression of the downtime, empty to mute every alert of the rule
    scope: str
    # offset of the downtime start from now
    start_offset: timedelta
    # alerts expected to get through the downtime
    expected_alerts: list[FiringAlert]