Skip to content

Commit

Permalink
chore: update charm libraries (#213)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: arturo-seijas <[email protected]>
  • Loading branch information
github-actions[bot] and arturo-seijas authored Jan 31, 2024
1 parent 4f3bddb commit 5cdc03e
Showing 1 changed file with 7 additions and 30 deletions.
37 changes: 7 additions & 30 deletions lib/charms/grafana_agent/v0/cos_agent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2024 Canonical Ltd.
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.

r"""## Overview.
Expand Down Expand Up @@ -206,17 +206,15 @@ def __init__(self, *args):
```
"""

import base64
import json
import logging
import lzma
from collections import namedtuple
from itertools import chain
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Dict, List, Optional, Set, Union

import pydantic
from cosl import JujuTopology
from cosl import GrafanaDashboard, JujuTopology
from cosl.rules import AlertRules
from ops.charm import RelationChangedEvent
from ops.framework import EventBase, EventSource, Object, ObjectEvents
Expand All @@ -236,7 +234,7 @@ class _MetricsEndpointDict(TypedDict):

LIBID = "dc15fa84cef84ce58155fb84f6c6213a"
LIBAPI = 0
LIBPATCH = 6
LIBPATCH = 7

PYDEPS = ["cosl", "pydantic < 2"]

Expand All @@ -251,31 +249,6 @@ class _MetricsEndpointDict(TypedDict):
SnapEndpoint = namedtuple("SnapEndpoint", "owner, name")


class GrafanaDashboard(str):
"""Grafana Dashboard encoded json; lzma-compressed."""

# TODO Replace this with a custom type when pydantic v2 released (end of 2023 Q1?)
# https://github.com/pydantic/pydantic/issues/4887
@staticmethod
def _serialize(raw_json: Union[str, bytes]) -> "GrafanaDashboard":
if not isinstance(raw_json, bytes):
raw_json = raw_json.encode("utf-8")
encoded = base64.b64encode(lzma.compress(raw_json)).decode("utf-8")
return GrafanaDashboard(encoded)

def _deserialize(self) -> Dict:
try:
raw = lzma.decompress(base64.b64decode(self.encode("utf-8"))).decode()
return json.loads(raw)
except json.decoder.JSONDecodeError as e:
logger.error("Invalid Dashboard format: %s", e)
return {}

def __repr__(self):
"""Return string representation of self."""
return "<GrafanaDashboard>"


class CosAgentProviderUnitData(pydantic.BaseModel):
"""Unit databag model for `cos-agent` relation."""

Expand Down Expand Up @@ -748,6 +721,10 @@ def metrics_jobs(self) -> List[Dict]:
"job_name": job["job_name"],
"metrics_path": job["path"],
"static_configs": [{"targets": [f"localhost:{job['port']}"]}],
# We include insecure_skip_verify because we are always scraping localhost.
# Even if we have the certs for the scrape targets, we'd rather specify the scrape
# jobs with localhost rather than the SAN DNS the cert was issued for.
"tls_config": {"insecure_skip_verify": True},
}

scrape_jobs.append(job)
Expand Down

0 comments on commit 5cdc03e

Please sign in to comment.