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

Refactor to reflect modern ways of charming #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
130 changes: 50 additions & 80 deletions observed/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,62 @@

## Description

This charm demonstrates how to integrate with [Canonical Observability Stack (COS)](https://charmhub.io/cos-lite).
This charm demonstrates how to integrate with COS observability stack.

It ships a dashboard, a prometheus alert rule and a loki alert rule.
The general steps for a clean start are:

## Usage

Deploy the [Canonical Observability Stack (COS)](https://charmhub.io/cos-lite) and offer the three needed integrations. This guide does not cover setting up COS lite but leaves that up to the reader.
1. Initialize your charm you wish to integrate.

```charmcraft init --profile machine```

2. Fetch the [COS agent libs] with charmcraft:

```charmcraft fetch-lib charms.grafana_agent.v0.cos_agent```

3. Create directories for grafana dashboards, prometheus alert rules & loki rules. We will reference those in the code.
```mkdir -p ./src/alert_rules/loki ./src/alert_rules/prometheus ./src/grafana_dashboards```

4. Add to metadata.yaml

```
provides:
cos-agent:
interface: cos_agent
```

5. Then in your charm.py

```
def __init__(self, *args):
super().__init__(*args)

# Define the data to send to grafana-agent
self._grafana_agent = COSAgentProvider(
self, metrics_endpoints=[
{"path": "/metrics", "port": self.config.get('port')},
],
metrics_rules_dir="./src/alert_rules/prometheus",
logs_rules_dir="./src/alert_rules/loki"
```

Full guide here: https://charmhub.io/topics/canonical-observability-stack/tutorials/instrumenting-machine-charms

![Alt text](overview.png)
## Usage

Lets say your url:s from the COS stack are available as:
First, make sure you have a COS stack deployed and operational which offers the three needed integrations. Lets say your url:s are available as:

admin/cos.cos-grafana-dashboard
admin/admin/cos.prometheus-remote
admin/admin/cos.cos-loki-logging

Then deploy this charm with the grafana-agent as a subordinate in your own test model like this:

Then deploy this charm with the grafana-agent as a subordinate like this:

juju depoy ./observed.charm
juju deploy grafana-agent
juju deploy grafana-agent # You might need to add --channel edge.

Now, relate with the COS offers you have in your test model.
Now, relate with the COS offers you have in your model

juju relate grafana-agent cos.cos-grafana-dashboard

Expand All @@ -33,80 +67,16 @@ Now, relate with the COS offers you have in your test model.

Juju will now take care of sending dashboards, alert-rules & loki rules to COS and you can access them in COS grafana dashboard.

![The deployed model](deployed-and-related.png)

![The dashboard](grafana-dashboard-view.png)

## Dashboard with juju topoligy.

When Juju ships the supplied [dashboard](src/grafana_dashboards/microsample_dashboard.json), variables from the juju topolgy are injected by the grafana-agent library such that they can be used directly in the dashboard json.

For example in the loki query:

```"expr": "{juju_application=\"$juju_application\", juju_unit=~\"$juju_unit\"} |~ \".* 404 .*\""```

Read about [Juju topology](https://discourse.charmhub.io/t/juju-topology-labels/8874) for how to extend this to your own charms.

## Alert rules examples
## Customize dashboards etc.

The charm ships a [prometheus alert rule](src/alert_rules/prometheus/microsample_prometheus.rule) that is triggered once you have called the microsample API more than 3 times like below:
Read here: https://discourse.charmhub.io/t/juju-topology-labels/8874

curl http://microsample.ip:8080/api/info
## Relations

The charm shipps a Loki alert rule, that is triggered if API is called incorrecty which causes Loki to see a 404 code in the logs)
Relate this to https://charmhub.io/grafana-agent

curl http://microsample.ip:8080/api/XXX
## Contributing

## Alertmanager examples
Place a PR

There is 4 different examples of [alertmanager configurations](src/alertmanager_configs/) that shows how to intergrate pagerduty and slack with alertmanager.
If other providers or configuration is needed, more information can be found at [prometheus documentation](https://prometheus.io/docs/alerting/latest/configuration/) site.

Inject configuration file with juju using following command:

juju config alertmanager config_file=@/path/to/file

To show or check current configuration:

juju run-action alertmanager/0 show-config
juju run-action alertmanager/0 check-config

### Development process

This charm is developed following this guide: https://charmhub.io/topics/canonical-observability-stack/tutorials/instrumenting-machine-charms

The general steps are:

1. Initialize your charm you wish to integrate.

```charmcraft init --profile machine```

2. Fetch the grafana-agent libs with charmcraft:

```charmcraft fetch-lib charms.grafana_agent.v0.cos_agent```

3. Create directories for grafana dashboards, prometheus alert rules & loki rules. We will reference those in the code.
```mkdir -p ./src/alert_rules/loki ./src/alert_rules/prometheus ./src/grafana_dashboards```

4. Add to metadata.yaml

```
provides:
cos-agent:
interface: cos_agent
```

5. Then in your charm.py

```
def __init__(self, *args):
super().__init__(*args)

# Define the data to send to grafana-agent
self._grafana_agent = COSAgentProvider(
self, metrics_endpoints=[
{"path": "/metrics", "port": self.config.get('port')},
],
metrics_rules_dir="./src/alert_rules/prometheus",
logs_rules_dir="./src/alert_rules/loki"
```
[COS agent libs]: https://canonical.github.io/charm-relation-interfaces/interfaces/cos_agent/v0/
Empty file removed observed/actions.yaml
Empty file.
31 changes: 25 additions & 6 deletions observed/charmcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
# Learn more about charmcraft.yaml configuration at:
# https://juju.is/docs/sdk/charmcraft-config
type: "charm"
name: observed
type: charm
title: The observed charm
summary: An educational charm that can be observed with the COS stack.
description: |
A charm that allows a user to observe the microsample service with the COS stack.

bases:
- build-on:
- name: "ubuntu"
- name: ubuntu
channel: "22.04"
run-on:
- name: "ubuntu"
channel: "22.04"
- name: ubuntu
channel: "22.04"

config:
options:
port:
type: string
default: "8080"
description: "Listen port (-p) passed to flask run."
channel:
type: string
default: "edge"
description: "Channel for microsample snap."

provides:
cos-agent:
interface: cos_agent
9 changes: 0 additions & 9 deletions observed/config.yaml

This file was deleted.

Loading