Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config option to disable reporting #202

Merged
merged 11 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion charmcraft-22.04.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,11 @@ config:
aren't sent to tracing backend at all. Anything outside of 0-100 range will be normalised
to this range by Grafana Agent.
type: float
default: 100.0
default: 100.0
reporting_enabled:
description: |
Toggle reporting of usage info to grafana, such as enabled feature flags.

Ref: https://grafana.com/docs/agent/latest/static/configuration/flags/#report-information-usage
type: boolean
default: true
22 changes: 16 additions & 6 deletions charmcraft-24.04.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,32 @@ config:
default: false
tracing_sample_rate_charm:
description: >
This property defines the percentage of charm traces that are sent to the tracing backend.
This property defines the percentage of charm traces that are sent to tracing backend.
Setting it to 100 would mean all charm traces are kept, setting to 0 means charm traces
aren't sent to the tracing backend at all.
aren't sent to tracing backend at all. Anything outside of 0-100 range will be normalised
to this range by Grafana Agent.
type: float
default: 100.0
tracing_sample_rate_workload:
description: >
This property defines the percentage of workload traces that are sent to the tracing backend.
This property defines the percentage of workload traces that are sent to tracing backend.
Setting it to 100 would mean all workload traces are kept, setting to 0 means workload traces
aren't sent to the tracing backend at all.
aren't sent to tracing backend at all. Anything outside of 0-100 range will be normalised
to this range by Grafana Agent.
type: float
default: 1.0
tracing_sample_rate_error:
description: >
This property defines the percentage of error traces (from all sources) that are sent to the tracing backend.
This property defines the percentage of error traces (regardless of the type) that are sent to tracing backend.
Setting it to 100 would mean all error traces are kept, setting to 0 means error traces
aren't sent to the tracing backend at all.
aren't sent to tracing backend at all. Anything outside of 0-100 range will be normalised
to this range by Grafana Agent.
type: float
default: 100.0
reporting_enabled:
description: |
Toggle reporting of usage info to grafana, such as enabled feature flags.

Ref: https://grafana.com/docs/agent/latest/static/configuration/flags/#report-information-usage
type: boolean
default: true
12 changes: 6 additions & 6 deletions lib/charms/grafana_agent/v0/cos_agent.py
sed-i marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ def __init__(self, *args):
import pydantic
from cosl import GrafanaDashboard, JujuTopology
from cosl.rules import AlertRules
from ops import CharmBase
from ops.charm import RelationChangedEvent
from ops.framework import EventBase, EventSource, Object, ObjectEvents
from ops.model import ModelError, Relation
from ops.testing import CharmType

if TYPE_CHECKING:
try:
Expand Down Expand Up @@ -468,7 +468,7 @@ def dump(self, databag: Optional[MutableMapping] = None, clear: bool = True):
return databag


class CosAgentProviderUnitData(DatabagModel): # pyright: ignore [reportGeneralTypeIssues]
class CosAgentProviderUnitData(DatabagModel):
"""Unit databag model for `cos-agent` relation."""

# The following entries are the same for all units of the same principal.
Expand All @@ -495,7 +495,7 @@ class CosAgentProviderUnitData(DatabagModel): # pyright: ignore [reportGeneralT
KEY: ClassVar[str] = "config"


class CosAgentPeersUnitData(DatabagModel): # pyright: ignore [reportGeneralTypeIssues]
class CosAgentPeersUnitData(DatabagModel):
"""Unit databag model for `peers` cos-agent machine charm peer relation."""

# We need the principal unit name and relation metadata to be able to render identifiers
Expand Down Expand Up @@ -594,7 +594,7 @@ class Receiver(pydantic.BaseModel):
)


class CosAgentRequirerUnitData(DatabagModel): # pyright: ignore [reportGeneralTypeIssues] # noqa: D101
class CosAgentRequirerUnitData(DatabagModel): # noqa: D101
"""Application databag model for the COS-agent requirer."""

receivers: List[Receiver] = pydantic.Field(
Expand All @@ -608,7 +608,7 @@ class COSAgentProvider(Object):

def __init__(
self,
charm: CharmBase,
charm: CharmType,
relation_name: str = DEFAULT_RELATION_NAME,
metrics_endpoints: Optional[List["_MetricsEndpointDict"]] = None,
metrics_rules_dir: str = "./src/prometheus_alert_rules",
Expand Down Expand Up @@ -877,7 +877,7 @@ class COSAgentRequirer(Object):

def __init__(
self,
charm: CharmBase,
charm: CharmType,
*,
relation_name: str = DEFAULT_RELATION_NAME,
peer_relation_name: str = DEFAULT_PEER_RELATION_NAME,
Expand Down
10 changes: 8 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ def _verify_snap_track(self) -> None:
try:
# install_ga_snap calls snap.ensure so it should do the right thing whether the track
# changes or not.
install_ga_snap(classic=bool(self.config["classic_snap"]))
install_ga_snap(
classic=bool(self.config["classic_snap"]),
config={"reporting-enabled": "1" if self.config["reporting_enabled"] else "0"},
)
except (snap.SnapError, SnapSpecError) as e:
raise GrafanaAgentInstallError("Failed to refresh grafana-agent.") from e

Expand All @@ -245,7 +248,10 @@ def _install(self) -> None:
"""Install/refresh the Grafana Agent snap."""
self.unit.status = MaintenanceStatus("Installing grafana-agent snap")
try:
install_ga_snap(classic=bool(self.config["classic_snap"]))
install_ga_snap(
classic=bool(self.config["classic_snap"]),
config={"reporting-enabled": "1" if self.config["reporting_enabled"] else "0"},
)
except (snap.SnapError, SnapSpecError) as e:
raise GrafanaAgentInstallError("Failed to install grafana-agent.") from e

Expand Down
17 changes: 11 additions & 6 deletions src/snap_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import logging
import platform
import subprocess
from typing import Dict, Optional

import charms.operator_libs_linux.v2.snap as snap_lib

Expand All @@ -24,10 +25,10 @@
_grafana_agent_snap_name = "grafana-agent"
_grafana_agent_snaps = {
# (confinement, arch): revision
("strict", "amd64"): 51, # 0.40.4
("strict", "arm64"): 52, # 0.40.4
("classic", "amd64"): 82, # 0.40.4
("classic", "arm64"): 83, # 0.40.4
("strict", "amd64"): 94, # 0.40.4
("strict", "arm64"): 96, # 0.40.4
("classic", "amd64"): 95, # 0.40.4
("classic", "arm64"): 97, # 0.40.4
}
sed-i marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



Expand All @@ -37,7 +38,7 @@ class SnapSpecError(Exception):
pass


def install_ga_snap(classic: bool):
def install_ga_snap(classic: bool, config: Optional[Dict[str, str]] = None):
"""Looks up system details and installs the appropriate grafana-agent snap revision."""
arch = get_system_arch()
confinement = "classic" if classic else "strict"
Expand All @@ -47,13 +48,14 @@ def install_ga_snap(classic: bool):
raise SnapSpecError(
f"Snap spec not found for arch={arch} and confinement={confinement}"
) from e
_install_snap(name=_grafana_agent_snap_name, revision=revision, classic=classic)
_install_snap(name=_grafana_agent_snap_name, revision=revision, classic=classic, config=config)


def _install_snap(
name: str,
revision: str,
classic: bool = False,
config: Optional[Dict[str, str]] = None,
):
"""Install and pin the given snap revision.

Expand All @@ -79,6 +81,9 @@ def _install_snap(
else:
snap.ensure(state=snap_lib.SnapState.Present, revision=revision, classic=classic)

if config:
snap.set(config)

snap.hold()


Expand Down
Loading