imbi-plugin-grafana 2.63.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- imbi_plugin_grafana-2.63.0/.gitignore +30 -0
- imbi_plugin_grafana-2.63.0/PKG-INFO +116 -0
- imbi_plugin_grafana-2.63.0/README.md +98 -0
- imbi_plugin_grafana-2.63.0/moon.yml +22 -0
- imbi_plugin_grafana-2.63.0/pyproject.toml +45 -0
- imbi_plugin_grafana-2.63.0/src/imbi/plugins/grafana/__init__.py +12 -0
- imbi_plugin_grafana-2.63.0/src/imbi/plugins/grafana/doctor.py +178 -0
- imbi_plugin_grafana-2.63.0/src/imbi/plugins/grafana/lifecycle.py +79 -0
- imbi_plugin_grafana-2.63.0/src/imbi/plugins/grafana/links.py +122 -0
- imbi_plugin_grafana-2.63.0/src/imbi/plugins/grafana/plugin.py +157 -0
- imbi_plugin_grafana-2.63.0/src/imbi/plugins/grafana/py.typed +0 -0
- imbi_plugin_grafana-2.63.0/src/imbi/plugins/grafana/steps.py +370 -0
- imbi_plugin_grafana-2.63.0/tests/__init__.py +0 -0
- imbi_plugin_grafana-2.63.0/tests/test_plugin.py +327 -0
- imbi_plugin_grafana-2.63.0/tests/test_steps.py +299 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
__pycache__
|
|
2
|
+
*.egg-info
|
|
3
|
+
*.pyc
|
|
4
|
+
*.env
|
|
5
|
+
.env
|
|
6
|
+
.env.test
|
|
7
|
+
build/
|
|
8
|
+
dist/
|
|
9
|
+
docs/site/
|
|
10
|
+
helm/imbi/charts/
|
|
11
|
+
runtime/state/
|
|
12
|
+
.venv/
|
|
13
|
+
.coverage
|
|
14
|
+
.mypy_cache/
|
|
15
|
+
.pytest_cache/
|
|
16
|
+
.ruff_cache/
|
|
17
|
+
coverage.xml
|
|
18
|
+
site/
|
|
19
|
+
|
|
20
|
+
# moon
|
|
21
|
+
.moon/cache
|
|
22
|
+
.moon/docker
|
|
23
|
+
|
|
24
|
+
# design-sync
|
|
25
|
+
.design-sync/sb-reference/
|
|
26
|
+
.design-sync/learnings/
|
|
27
|
+
.design-sync/.cache/
|
|
28
|
+
.design-sync/node_modules
|
|
29
|
+
.ds-sync/
|
|
30
|
+
ds-bundle/
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: imbi-plugin-grafana
|
|
3
|
+
Version: 2.63.0
|
|
4
|
+
Summary: Grafana Alerting webhook plugin for Imbi
|
|
5
|
+
Author-email: Ryan McLean <ryanm@aweber.com>
|
|
6
|
+
License-Expression: BSD-3-Clause
|
|
7
|
+
Classifier: Development Status :: 3 - Alpha
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: Natural Language :: English
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
13
|
+
Requires-Python: >=3.14
|
|
14
|
+
Requires-Dist: httpx>=0.27
|
|
15
|
+
Requires-Dist: imbi-common==2.63.0
|
|
16
|
+
Requires-Dist: pydantic>=2
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# imbi-plugin-grafana
|
|
20
|
+
|
|
21
|
+
The Grafana plugin for the [Imbi](https://github.com/AWeber-Imbi)
|
|
22
|
+
platform, distributed as a single Python package (`imbi.plugins.grafana`)
|
|
23
|
+
exposing one Integration (`grafana`) with three capabilities:
|
|
24
|
+
|
|
25
|
+
- **`lifecycle`** — writes the project's `EXISTS_IN` edge, whose
|
|
26
|
+
identifier (`{slug}-{id}` by default) the project's alert rules
|
|
27
|
+
carry in a label (`service` by default), and a project link to the
|
|
28
|
+
alert rules list searched by that label. The link key is the
|
|
29
|
+
integration slug. Removes both on delete.
|
|
30
|
+
- **`webhook-actions`** — receives deliveries from a Grafana Alerting
|
|
31
|
+
webhook contact point. imbi-gateway finds the project by the label
|
|
32
|
+
value and records each delivery as an event. It verifies the
|
|
33
|
+
signature only when the webhook has a secret.
|
|
34
|
+
The capability advertises no custom actions.
|
|
35
|
+
- **`analysis`** — Project Doctor checks that the edge and link exist
|
|
36
|
+
and match the integration options, with a one-click repair.
|
|
37
|
+
|
|
38
|
+
Options: `service_url` (the Grafana URL, required) and `label` (the
|
|
39
|
+
alert rule label that names the project, default `service`).
|
|
40
|
+
|
|
41
|
+
The Imbi host discovers the package by scanning `imbi.plugins.*` and
|
|
42
|
+
reads the module-level `PLUGIN` attribute; there are no entry points.
|
|
43
|
+
All plugin base classes come from `imbi.common.plugins`.
|
|
44
|
+
|
|
45
|
+
## The delivery
|
|
46
|
+
|
|
47
|
+
A Grafana webhook contact point sends its default JSON payload, one
|
|
48
|
+
request for each notification group:
|
|
49
|
+
|
|
50
|
+
```json
|
|
51
|
+
{
|
|
52
|
+
"receiver": "imbi",
|
|
53
|
+
"status": "firing",
|
|
54
|
+
"alerts": [
|
|
55
|
+
{
|
|
56
|
+
"status": "firing",
|
|
57
|
+
"labels": {"alertname": "5xx rate high", "service": "webform"},
|
|
58
|
+
"annotations": {"runbook_url": "https://imbi.example.com/..."},
|
|
59
|
+
"startsAt": "...",
|
|
60
|
+
"endsAt": "0001-01-01T00:00:00Z",
|
|
61
|
+
"values": {"B": 12.5},
|
|
62
|
+
"generatorURL": "...",
|
|
63
|
+
"fingerprint": "...",
|
|
64
|
+
"silenceURL": "..."
|
|
65
|
+
}
|
|
66
|
+
],
|
|
67
|
+
"groupLabels": {},
|
|
68
|
+
"commonLabels": {},
|
|
69
|
+
"commonAnnotations": {},
|
|
70
|
+
"externalURL": "https://grafana.example.com/",
|
|
71
|
+
"groupKey": "...",
|
|
72
|
+
"title": "...",
|
|
73
|
+
"state": "alerting",
|
|
74
|
+
"message": "..."
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Signature
|
|
79
|
+
|
|
80
|
+
Turn on **HMAC signature** on the contact point. Grafana then sends the
|
|
81
|
+
hex HMAC-SHA256 of the raw body, keyed by the shared secret, in the
|
|
82
|
+
`X-Grafana-Alerting-Signature` header. Put the same secret in the
|
|
83
|
+
**Secret** field of the Imbi webhook. Leave the contact point's
|
|
84
|
+
**Timestamp Header** empty: with it set, Grafana signs
|
|
85
|
+
`<timestamp>:<body>` instead, which the gateway does not compute. When
|
|
86
|
+
the webhook has a secret, the gateway drops a delivery whose signature
|
|
87
|
+
is missing or does not match. When it has no secret, the gateway
|
|
88
|
+
processes each delivery unverified and logs a warning one time for
|
|
89
|
+
each webhook in each process.
|
|
90
|
+
The header name and prefix are the `SIGNATURE_HEADER` and
|
|
91
|
+
`SIGNATURE_PREFIX` constants in `imbi.plugins.grafana.plugin`.
|
|
92
|
+
|
|
93
|
+
## Development
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
moon run root:setup # uv sync + pre-commit hooks
|
|
97
|
+
moon run grafana:test # coverage (fails under 85%)
|
|
98
|
+
moon run grafana:lint grafana:typecheck grafana:format # ruff + basedpyright + format check
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Only the workflow steps call Grafana. They need the optional
|
|
102
|
+
`api_token` credential (a service account token with the `Viewer`
|
|
103
|
+
role) and send requests only to `service_url`. They use these Grafana
|
|
104
|
+
APIs:
|
|
105
|
+
|
|
106
|
+
- `GET /api/datasources`, `GET /api/datasources/uid/:uid`, and
|
|
107
|
+
`POST /api/ds/query`:
|
|
108
|
+
<https://grafana.com/docs/grafana/latest/developers/http_api/data_source/>
|
|
109
|
+
- The Prometheus query fields (`expr`, `range`, `instant`):
|
|
110
|
+
<https://grafana.com/docs/grafana/latest/datasources/prometheus/query-editor/>
|
|
111
|
+
and
|
|
112
|
+
<https://grafana.github.io/grafana-foundation-sdk/typescript/Reference/prometheus/object-dataquery>
|
|
113
|
+
|
|
114
|
+
## License
|
|
115
|
+
|
|
116
|
+
BSD-3-Clause.
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# imbi-plugin-grafana
|
|
2
|
+
|
|
3
|
+
The Grafana plugin for the [Imbi](https://github.com/AWeber-Imbi)
|
|
4
|
+
platform, distributed as a single Python package (`imbi.plugins.grafana`)
|
|
5
|
+
exposing one Integration (`grafana`) with three capabilities:
|
|
6
|
+
|
|
7
|
+
- **`lifecycle`** — writes the project's `EXISTS_IN` edge, whose
|
|
8
|
+
identifier (`{slug}-{id}` by default) the project's alert rules
|
|
9
|
+
carry in a label (`service` by default), and a project link to the
|
|
10
|
+
alert rules list searched by that label. The link key is the
|
|
11
|
+
integration slug. Removes both on delete.
|
|
12
|
+
- **`webhook-actions`** — receives deliveries from a Grafana Alerting
|
|
13
|
+
webhook contact point. imbi-gateway finds the project by the label
|
|
14
|
+
value and records each delivery as an event. It verifies the
|
|
15
|
+
signature only when the webhook has a secret.
|
|
16
|
+
The capability advertises no custom actions.
|
|
17
|
+
- **`analysis`** — Project Doctor checks that the edge and link exist
|
|
18
|
+
and match the integration options, with a one-click repair.
|
|
19
|
+
|
|
20
|
+
Options: `service_url` (the Grafana URL, required) and `label` (the
|
|
21
|
+
alert rule label that names the project, default `service`).
|
|
22
|
+
|
|
23
|
+
The Imbi host discovers the package by scanning `imbi.plugins.*` and
|
|
24
|
+
reads the module-level `PLUGIN` attribute; there are no entry points.
|
|
25
|
+
All plugin base classes come from `imbi.common.plugins`.
|
|
26
|
+
|
|
27
|
+
## The delivery
|
|
28
|
+
|
|
29
|
+
A Grafana webhook contact point sends its default JSON payload, one
|
|
30
|
+
request for each notification group:
|
|
31
|
+
|
|
32
|
+
```json
|
|
33
|
+
{
|
|
34
|
+
"receiver": "imbi",
|
|
35
|
+
"status": "firing",
|
|
36
|
+
"alerts": [
|
|
37
|
+
{
|
|
38
|
+
"status": "firing",
|
|
39
|
+
"labels": {"alertname": "5xx rate high", "service": "webform"},
|
|
40
|
+
"annotations": {"runbook_url": "https://imbi.example.com/..."},
|
|
41
|
+
"startsAt": "...",
|
|
42
|
+
"endsAt": "0001-01-01T00:00:00Z",
|
|
43
|
+
"values": {"B": 12.5},
|
|
44
|
+
"generatorURL": "...",
|
|
45
|
+
"fingerprint": "...",
|
|
46
|
+
"silenceURL": "..."
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
"groupLabels": {},
|
|
50
|
+
"commonLabels": {},
|
|
51
|
+
"commonAnnotations": {},
|
|
52
|
+
"externalURL": "https://grafana.example.com/",
|
|
53
|
+
"groupKey": "...",
|
|
54
|
+
"title": "...",
|
|
55
|
+
"state": "alerting",
|
|
56
|
+
"message": "..."
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Signature
|
|
61
|
+
|
|
62
|
+
Turn on **HMAC signature** on the contact point. Grafana then sends the
|
|
63
|
+
hex HMAC-SHA256 of the raw body, keyed by the shared secret, in the
|
|
64
|
+
`X-Grafana-Alerting-Signature` header. Put the same secret in the
|
|
65
|
+
**Secret** field of the Imbi webhook. Leave the contact point's
|
|
66
|
+
**Timestamp Header** empty: with it set, Grafana signs
|
|
67
|
+
`<timestamp>:<body>` instead, which the gateway does not compute. When
|
|
68
|
+
the webhook has a secret, the gateway drops a delivery whose signature
|
|
69
|
+
is missing or does not match. When it has no secret, the gateway
|
|
70
|
+
processes each delivery unverified and logs a warning one time for
|
|
71
|
+
each webhook in each process.
|
|
72
|
+
The header name and prefix are the `SIGNATURE_HEADER` and
|
|
73
|
+
`SIGNATURE_PREFIX` constants in `imbi.plugins.grafana.plugin`.
|
|
74
|
+
|
|
75
|
+
## Development
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
moon run root:setup # uv sync + pre-commit hooks
|
|
79
|
+
moon run grafana:test # coverage (fails under 85%)
|
|
80
|
+
moon run grafana:lint grafana:typecheck grafana:format # ruff + basedpyright + format check
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Only the workflow steps call Grafana. They need the optional
|
|
84
|
+
`api_token` credential (a service account token with the `Viewer`
|
|
85
|
+
role) and send requests only to `service_url`. They use these Grafana
|
|
86
|
+
APIs:
|
|
87
|
+
|
|
88
|
+
- `GET /api/datasources`, `GET /api/datasources/uid/:uid`, and
|
|
89
|
+
`POST /api/ds/query`:
|
|
90
|
+
<https://grafana.com/docs/grafana/latest/developers/http_api/data_source/>
|
|
91
|
+
- The Prometheus query fields (`expr`, `range`, `instant`):
|
|
92
|
+
<https://grafana.com/docs/grafana/latest/datasources/prometheus/query-editor/>
|
|
93
|
+
and
|
|
94
|
+
<https://grafana.github.io/grafana-foundation-sdk/typescript/Reference/prometheus/object-dataquery>
|
|
95
|
+
|
|
96
|
+
## License
|
|
97
|
+
|
|
98
|
+
BSD-3-Clause.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# yaml-language-server: $schema=https://moonrepo.dev/schemas/project.json
|
|
2
|
+
layer: 'library'
|
|
3
|
+
stack: 'backend'
|
|
4
|
+
language: 'python'
|
|
5
|
+
tags:
|
|
6
|
+
- 'python'
|
|
7
|
+
|
|
8
|
+
project:
|
|
9
|
+
name: 'imbi-plugin-grafana'
|
|
10
|
+
description: 'Grafana Alerting webhook plugin for Imbi'
|
|
11
|
+
|
|
12
|
+
dependsOn:
|
|
13
|
+
- 'common'
|
|
14
|
+
|
|
15
|
+
# Inherits lint / format / typecheck / test from .moon/tasks/python.yml. The
|
|
16
|
+
# shared test task points --cov-config at this member's pyproject, where
|
|
17
|
+
# [tool.coverage] scopes source + threshold to the member.
|
|
18
|
+
tasks:
|
|
19
|
+
test:
|
|
20
|
+
args:
|
|
21
|
+
- '--cov'
|
|
22
|
+
- '--cov-report=term-missing'
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "imbi-plugin-grafana"
|
|
3
|
+
version = "2.63.0"
|
|
4
|
+
description = "Grafana Alerting webhook plugin for Imbi"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.14"
|
|
7
|
+
license = "BSD-3-Clause"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "Ryan McLean", email = "ryanm@aweber.com" },
|
|
10
|
+
]
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 3 - Alpha",
|
|
13
|
+
"Intended Audience :: Developers",
|
|
14
|
+
"Natural Language :: English",
|
|
15
|
+
"Operating System :: OS Independent",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.14",
|
|
18
|
+
]
|
|
19
|
+
dependencies = [
|
|
20
|
+
"httpx>=0.27",
|
|
21
|
+
"imbi-common==2.63.0",
|
|
22
|
+
"pydantic>=2",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[build-system]
|
|
26
|
+
requires = ["hatchling"]
|
|
27
|
+
build-backend = "hatchling.build"
|
|
28
|
+
|
|
29
|
+
# Coverage for the per-member `moon run grafana:test` run. Scoped to this
|
|
30
|
+
# member so the report/threshold reflect only imbi.plugins.grafana; the aggregate
|
|
31
|
+
# `root:coverage` run uses the root pyproject config instead.
|
|
32
|
+
[tool.coverage.run]
|
|
33
|
+
branch = true
|
|
34
|
+
source_pkgs = ["imbi.plugins.grafana"]
|
|
35
|
+
|
|
36
|
+
[tool.coverage.report]
|
|
37
|
+
exclude_also = ["typing.TYPE_CHECKING"]
|
|
38
|
+
fail_under = 85
|
|
39
|
+
show_missing = true
|
|
40
|
+
|
|
41
|
+
[tool.hatch.build.targets.wheel]
|
|
42
|
+
packages = ["src/imbi"]
|
|
43
|
+
include = [
|
|
44
|
+
"src/imbi/plugins/grafana/py.typed",
|
|
45
|
+
]
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Imbi Grafana plugin: link projects to Grafana alert rules and
|
|
2
|
+
receive Grafana Alerting webhook deliveries."""
|
|
3
|
+
|
|
4
|
+
from importlib import metadata
|
|
5
|
+
|
|
6
|
+
from imbi.plugins.grafana.plugin import GrafanaPlugin, GrafanaWebhookActions
|
|
7
|
+
|
|
8
|
+
#: Discovered by the imbi-common registry's convention scan.
|
|
9
|
+
PLUGIN = GrafanaPlugin
|
|
10
|
+
|
|
11
|
+
version = metadata.version('imbi-plugin-grafana')
|
|
12
|
+
__all__ = ['PLUGIN', 'GrafanaPlugin', 'GrafanaWebhookActions', 'version']
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"""Grafana project doctor.
|
|
2
|
+
|
|
3
|
+
Checks that the project has an ``EXISTS_IN`` edge to the Grafana
|
|
4
|
+
integration, that the plugin verified it, and that the edge and the
|
|
5
|
+
``grafana`` link match the integration options. Warns when the edge
|
|
6
|
+
identifier differs from ``{slug}-{id}`` (for example after a rename),
|
|
7
|
+
because the alert rules must carry that value in the label.
|
|
8
|
+
|
|
9
|
+
The plugin has no Grafana credentials, so the doctor cannot confirm
|
|
10
|
+
that alert rules with the label exist.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from imbi.common.plugins.base import (
|
|
16
|
+
AnalysisCapability,
|
|
17
|
+
AnalysisResultItem,
|
|
18
|
+
AnalysisResultStatus,
|
|
19
|
+
PluginContext,
|
|
20
|
+
RemediationOffer,
|
|
21
|
+
RemediationResult,
|
|
22
|
+
ServiceWriteback,
|
|
23
|
+
)
|
|
24
|
+
from imbi.plugins.grafana import links
|
|
25
|
+
|
|
26
|
+
#: Write the edge and link for the current identifier.
|
|
27
|
+
_LINK = 'link-project'
|
|
28
|
+
#: Rewrite the edge and link with ``{slug}-{id}`` as the identifier.
|
|
29
|
+
_USE_DEFAULT = 'use-project-value'
|
|
30
|
+
|
|
31
|
+
_LINK_OFFER = RemediationOffer(
|
|
32
|
+
id=_LINK,
|
|
33
|
+
label='Link the project to Grafana',
|
|
34
|
+
confirm=(
|
|
35
|
+
'This writes the Grafana link of this project. The alert rules '
|
|
36
|
+
'must carry the identifier in the configured label.'
|
|
37
|
+
),
|
|
38
|
+
destructive=True,
|
|
39
|
+
)
|
|
40
|
+
_USE_DEFAULT_OFFER = RemediationOffer(
|
|
41
|
+
id=_USE_DEFAULT,
|
|
42
|
+
label='Use the current project value as the identifier',
|
|
43
|
+
confirm=(
|
|
44
|
+
'This changes the identifier that Grafana deliveries must carry. '
|
|
45
|
+
'Alert rules labelled with the old value stop matching this '
|
|
46
|
+
'project.'
|
|
47
|
+
),
|
|
48
|
+
destructive=True,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _item(
|
|
53
|
+
slug: str,
|
|
54
|
+
title: str,
|
|
55
|
+
status: AnalysisResultStatus,
|
|
56
|
+
description: str,
|
|
57
|
+
remediation: RemediationOffer | None = None,
|
|
58
|
+
) -> AnalysisResultItem:
|
|
59
|
+
return AnalysisResultItem(
|
|
60
|
+
slug=slug,
|
|
61
|
+
title=title,
|
|
62
|
+
status=status,
|
|
63
|
+
description=description,
|
|
64
|
+
remediation=remediation,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class GrafanaDoctor(AnalysisCapability):
|
|
69
|
+
"""Validate and repair the project's Grafana edge and link."""
|
|
70
|
+
|
|
71
|
+
async def analyze(
|
|
72
|
+
self, ctx: PluginContext, credentials: dict[str, str]
|
|
73
|
+
) -> list[AnalysisResultItem]:
|
|
74
|
+
del credentials
|
|
75
|
+
if not ctx.integration_slug:
|
|
76
|
+
return [
|
|
77
|
+
_item(
|
|
78
|
+
'exists-in',
|
|
79
|
+
'Grafana link',
|
|
80
|
+
'warn',
|
|
81
|
+
'This capability is not bound to an Integration.',
|
|
82
|
+
)
|
|
83
|
+
]
|
|
84
|
+
value = links.identifier(ctx)
|
|
85
|
+
try:
|
|
86
|
+
expected = links.writeback(ctx, value)
|
|
87
|
+
except links.GrafanaConfigError as exc:
|
|
88
|
+
return [_item('exists-in', 'Grafana link', 'warn', str(exc))]
|
|
89
|
+
return [
|
|
90
|
+
self._check_link(ctx, expected),
|
|
91
|
+
self._check_identifier(ctx, value),
|
|
92
|
+
]
|
|
93
|
+
|
|
94
|
+
@staticmethod
|
|
95
|
+
def _check_link(
|
|
96
|
+
ctx: PluginContext, expected: ServiceWriteback
|
|
97
|
+
) -> AnalysisResultItem:
|
|
98
|
+
name = links.label(ctx)
|
|
99
|
+
conn = links.connection(ctx)
|
|
100
|
+
if links.matches(ctx, expected):
|
|
101
|
+
return _item(
|
|
102
|
+
'exists-in',
|
|
103
|
+
'Grafana link',
|
|
104
|
+
'pass',
|
|
105
|
+
f'Linked to Grafana alert rules with '
|
|
106
|
+
f'`{name}={expected.identifier}`.',
|
|
107
|
+
)
|
|
108
|
+
if conn is None:
|
|
109
|
+
return _item(
|
|
110
|
+
'exists-in',
|
|
111
|
+
'Grafana link',
|
|
112
|
+
'fail',
|
|
113
|
+
'The project has no Grafana link, so the gateway cannot '
|
|
114
|
+
'route Grafana deliveries to it. Use the Fix action to link '
|
|
115
|
+
f'it as `{name}={expected.identifier}`.',
|
|
116
|
+
_LINK_OFFER,
|
|
117
|
+
)
|
|
118
|
+
return _item(
|
|
119
|
+
'exists-in',
|
|
120
|
+
'Grafana link',
|
|
121
|
+
'warn',
|
|
122
|
+
f'The Grafana link `{name}={expected.identifier}` exists, but '
|
|
123
|
+
'the edge or the alert rules link does not match the '
|
|
124
|
+
'integration options. Use the Fix action to refresh it.',
|
|
125
|
+
_LINK_OFFER,
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
@staticmethod
|
|
129
|
+
def _check_identifier(
|
|
130
|
+
ctx: PluginContext, value: str
|
|
131
|
+
) -> AnalysisResultItem:
|
|
132
|
+
name = links.label(ctx)
|
|
133
|
+
default = links.default_identifier(ctx)
|
|
134
|
+
if value == default:
|
|
135
|
+
return _item(
|
|
136
|
+
'identifier',
|
|
137
|
+
'Grafana identifier',
|
|
138
|
+
'pass',
|
|
139
|
+
f'Alert rules and dashboards must carry `{name}={value}`.',
|
|
140
|
+
)
|
|
141
|
+
return _item(
|
|
142
|
+
'identifier',
|
|
143
|
+
'Grafana identifier',
|
|
144
|
+
'warn',
|
|
145
|
+
f'The Grafana identifier `{value}` differs from the current '
|
|
146
|
+
f'project value `{default}`. Alert rules must carry '
|
|
147
|
+
f'`{name}={value}`. Update the labels in Grafana, then use the '
|
|
148
|
+
f'Fix action to switch to `{default}`.',
|
|
149
|
+
_USE_DEFAULT_OFFER,
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
async def remediate(
|
|
153
|
+
self,
|
|
154
|
+
ctx: PluginContext,
|
|
155
|
+
credentials: dict[str, str],
|
|
156
|
+
remediation_id: str,
|
|
157
|
+
) -> RemediationResult:
|
|
158
|
+
if remediation_id not in (_LINK, _USE_DEFAULT):
|
|
159
|
+
return await super().remediate(ctx, credentials, remediation_id)
|
|
160
|
+
value = (
|
|
161
|
+
links.default_identifier(ctx)
|
|
162
|
+
if remediation_id == _USE_DEFAULT
|
|
163
|
+
else links.identifier(ctx)
|
|
164
|
+
)
|
|
165
|
+
try:
|
|
166
|
+
expected = links.writeback(ctx, value)
|
|
167
|
+
except links.GrafanaConfigError as exc:
|
|
168
|
+
return RemediationResult(status='failed', message=str(exc))
|
|
169
|
+
if links.matches(ctx, expected):
|
|
170
|
+
return RemediationResult(
|
|
171
|
+
status='noop',
|
|
172
|
+
message=f'Already linked as {links.label(ctx)}={value!r}.',
|
|
173
|
+
)
|
|
174
|
+
ctx.service_writeback = expected
|
|
175
|
+
return RemediationResult(
|
|
176
|
+
status='fixed',
|
|
177
|
+
message=f'Linked the project as {links.label(ctx)}={value!r}.',
|
|
178
|
+
)
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""Grafana lifecycle capability: link each project to its alert rules.
|
|
2
|
+
|
|
3
|
+
Grafana has no project resource to create. The hooks write the
|
|
4
|
+
``EXISTS_IN`` edge whose identifier the gateway matches against the
|
|
5
|
+
label in each alert delivery, and the ``grafana`` link to the alert
|
|
6
|
+
rules that carry that label. Archive is a no-op; delete removes the
|
|
7
|
+
edge and the link.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from imbi.common.plugins.base import (
|
|
13
|
+
LifecycleCapability,
|
|
14
|
+
LifecycleResult,
|
|
15
|
+
PluginContext,
|
|
16
|
+
)
|
|
17
|
+
from imbi.plugins.grafana import links
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class GrafanaLifecycle(LifecycleCapability):
|
|
21
|
+
"""Write (or refresh) the project's edge to the Grafana integration."""
|
|
22
|
+
|
|
23
|
+
async def on_project_archived(
|
|
24
|
+
self, ctx: PluginContext, credentials: dict[str, str]
|
|
25
|
+
) -> LifecycleResult:
|
|
26
|
+
del ctx, credentials
|
|
27
|
+
return LifecycleResult(
|
|
28
|
+
status='skipped', message='Grafana has no archive concept'
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
async def on_project_created(
|
|
32
|
+
self, ctx: PluginContext, credentials: dict[str, str]
|
|
33
|
+
) -> LifecycleResult:
|
|
34
|
+
del credentials
|
|
35
|
+
return self._link(ctx)
|
|
36
|
+
|
|
37
|
+
async def on_project_updated(
|
|
38
|
+
self, ctx: PluginContext, credentials: dict[str, str]
|
|
39
|
+
) -> LifecycleResult:
|
|
40
|
+
del credentials
|
|
41
|
+
return self._link(ctx)
|
|
42
|
+
|
|
43
|
+
async def on_project_deleted(
|
|
44
|
+
self, ctx: PluginContext, credentials: dict[str, str]
|
|
45
|
+
) -> LifecycleResult:
|
|
46
|
+
del credentials
|
|
47
|
+
if links.connection(ctx) is None:
|
|
48
|
+
return LifecycleResult(
|
|
49
|
+
status='skipped', message='No Grafana link to remove'
|
|
50
|
+
)
|
|
51
|
+
try:
|
|
52
|
+
expected = links.writeback(ctx, links.identifier(ctx))
|
|
53
|
+
except links.GrafanaConfigError as exc:
|
|
54
|
+
return LifecycleResult(status='failed', message=str(exc))
|
|
55
|
+
ctx.service_writeback = expected.model_copy(update={'remove': True})
|
|
56
|
+
return LifecycleResult(
|
|
57
|
+
status='ok',
|
|
58
|
+
message=f'Removed the Grafana link {expected.identifier!r}',
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
@staticmethod
|
|
62
|
+
def _link(ctx: PluginContext) -> LifecycleResult:
|
|
63
|
+
value = links.identifier(ctx)
|
|
64
|
+
try:
|
|
65
|
+
expected = links.writeback(ctx, value)
|
|
66
|
+
except links.GrafanaConfigError as exc:
|
|
67
|
+
return LifecycleResult(status='failed', message=str(exc))
|
|
68
|
+
if links.matches(ctx, expected):
|
|
69
|
+
return LifecycleResult(
|
|
70
|
+
status='skipped',
|
|
71
|
+
message=f'Already linked as {links.label(ctx)}={value!r}',
|
|
72
|
+
)
|
|
73
|
+
ctx.service_writeback = expected
|
|
74
|
+
verb = 'Refreshed' if links.connection(ctx) else 'Linked'
|
|
75
|
+
return LifecycleResult(
|
|
76
|
+
status='ok',
|
|
77
|
+
message=f'{verb} the Grafana link {links.label(ctx)}={value!r}',
|
|
78
|
+
artifacts={'identifier': value, 'label': links.label(ctx)},
|
|
79
|
+
)
|