Skip to content

Commit

Permalink
Cleanup no-untyped-def in agent_prometheus
Browse files Browse the repository at this point in the history
Change-Id: I539d8db2c66912f08d04118c2fd125af499fa8fb
  • Loading branch information
LarsMichelsen committed Jun 17, 2024
1 parent 8acc622 commit 55be22c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
8 changes: 6 additions & 2 deletions cmk/plugins/lib/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

from cmk.utils import password_store

from cmk.special_agents.v0_unstable.request_helper import create_api_connect_session, parse_api_url
from cmk.special_agents.v0_unstable.request_helper import (
ApiSession,
create_api_connect_session,
parse_api_url,
)


class ConnectionConfig(TypedDict):
Expand Down Expand Up @@ -49,7 +53,7 @@ def extract_connection_args(config):
return connection_args


def generate_api_session(connection_options):
def generate_api_session(connection_options: dict) -> ApiSession:
return create_api_connect_session(
connection_options["api_url"],
auth=connection_options.get("auth"),
Expand Down
18 changes: 11 additions & 7 deletions cmk/plugins/prometheus/special_agents/agent_prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
extract_connection_args,
generate_api_session,
)
from cmk.special_agents.v0_unstable.request_helper import ApiSession

LOGGER = logging.getLogger() # root logger for now

Expand Down Expand Up @@ -80,7 +81,7 @@ def parse_pod_name(labels: dict[str, str], prepend_namespace: bool = False) -> s


class CAdvisorExporter:
def __init__(self, api_client, options) -> None: # type: ignore[no-untyped-def]
def __init__(self, api_client: "PrometheusAPI", options: dict) -> None:
self.api_client = api_client
self.container_name_option = options.get("container_id", "short")
self.pod_containers: dict = {}
Expand Down Expand Up @@ -192,7 +193,7 @@ def memory_pod_summary(self, _group_element: str) -> list[dict[str, dict[str, An
def _retrieve_pods_memory_summary(
self, memory_info: list[tuple[str, str]]
) -> tuple[dict[str, dict[str, Any]], dict[str, str]]:
result: dict[str, dict[str, str | dict[str, str]]] = {}
result: dict[str, dict[str, str | dict[str, Any]]] = {}
associations = {}
for memory_stat, promql_query in memory_info:
promql_query = promql_query.replace("{namespace_filter}", self._namespace_query_part())
Expand Down Expand Up @@ -400,8 +401,8 @@ def __init__(self, raw_response: dict[str, Any]) -> None:
self.labels = raw_response["metric"]
self.internal_values = raw_response["value"]

def label_value(self, key: str, default=None) -> str: # type: ignore[no-untyped-def]
return self.labels.get(key, default)
def label_value(self, key: str) -> str:
return self.labels.get(key)

def value(self, default_value: float | int | None = None, as_string: bool = False) -> float:
try:
Expand Down Expand Up @@ -563,7 +564,7 @@ class PrometheusAPI:
Realizes communication with the Prometheus API
"""

def __init__(self, session) -> None: # type: ignore[no-untyped-def]
def __init__(self, session: ApiSession) -> None:
self.session = session

def perform_specified_promql_queries(
Expand Down Expand Up @@ -737,7 +738,7 @@ class ApiData:
Server & the Prometheus Exporters
"""

def __init__(self, api_client, exporter_options) -> None: # type: ignore[no-untyped-def]
def __init__(self, api_client: "PrometheusAPI", exporter_options: dict) -> None:
self.api_client = api_client
self.prometheus_server = PrometheusServer(api_client)
if "cadvisor" in exporter_options:
Expand All @@ -746,7 +747,10 @@ def __init__(self, api_client, exporter_options) -> None: # type: ignore[no-unt
if "node_exporter" in exporter_options:

def get_promql(promql_expression: str) -> list[PromQLMetric]:
return api_client.perform_multi_result_promql(promql_expression).promql_metrics
result = api_client.perform_multi_result_promql(promql_expression)
if result is None:
raise ApiError("Missing PromQL result for %s" % promql_expression)
return result.promql_metrics

self.node_exporter = NodeExporter(get_promql)

Expand Down

0 comments on commit 55be22c

Please sign in to comment.