canonical / canonical/cos-proxy-operator

`event.app` can be None on relation-broken, putting the charm in error state

Open
#158 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
3
Forks
12
PR merge metrics
No merged PRs in 30d

Description

Via @pedrofragola.
When the prometheus relation is removed,

```mermaid
graph LR
cos-proxy --- prometheus
```

the charm goes into error state because `event.app` is None.

https://github.com/canonical/cos-proxy-operator/blob/49eb7f03ee641d6944ae63b2a8ae6de7b8fb7b8a/src/charm.py#L622-L624

```
App Version Status Scale Charm Channel Rev Exposed Message
cos-proxy n/a error 1 cos-proxy latest/stable 73 no hook failed: "dashboards-relation-broken"
```

## Grafana
```
2024-09-02 14:34:50 INFO juju.worker.uniter resolver.go:180 awaiting error resolution for "relation-broken" hook
2024-09-02 14:34:53 WARNING unit.cos-proxy/7.juju-log server.go:325 dashboards:699: 'app' expected but not received.
2024-09-02 14:34:53 WARNING unit.cos-proxy/7.juju-log server.go:325 dashboards:699: 'app_name' expected in snapshot but not found.
2024-09-02 14:34:53 ERROR unit.cos-proxy/7.juju-log server.go:325 dashboards:699: Uncaught exception while in charm code:
Traceback (most recent call last):
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/./src/charm.py", line 643, in
main(COSProxyCharm)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 544, in main
manager.run()
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 520, in run
self._emit()
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 509, in _emit
_emit_charm_event(self.charm, self.dispatcher.event_name)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 143, in _emit_charm_event
event_to_emit.emit(*args, **kwargs)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/framework.py", line 352, in emit
framework._emit(event)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/framework.py", line 851, in _emit
self._reemit(event_path)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/framework.py", line 941, in _reemit
custom_handler(event)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/lib/charms/grafana_k8s/v0/grafana_dashboard.py", line 1685, in remove_dashboards
app_ids = _type_convert_stored(self._stored.id_mappings.get(event.app.name, "")) # type: ignore
AttributeError: 'NoneType' object has no attribute 'name'
```

## Prometheus
```
2024-09-12 09:56:50 ERROR unit.cos-proxy/7.juju-log server.go:325 prometheus-target:700: Uncaught exception while in charm code:
Traceback (most recent call last):
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/./src/charm.py", line 643, in
main(COSProxyCharm)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 544, in main
manager.run()
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 520, in run
self._emit()
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 509, in _emit
_emit_charm_event(self.charm, self.dispatcher.event_name)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 143, in _emit_charm_event
event_to_emit.emit(*args, **kwargs)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/framework.py", line 352, in emit
framework._emit(event)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/framework.py", line 851, in _emit
self._reemit(event_path)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/framework.py", line 941, in _reemit
custom_handler(event)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/./src/charm.py", line 560, in _prometheus_target_relation_broken
self._handle_prometheus_alert_rule_files(RULES_DIR, event.app.name)
AttributeError: 'NoneType' object has no attribute 'name'
2024-09-12 09:56:50 ERROR juju.worker.uniter.operation runhook.go:180 hook "prometheus-target-relation-broken" (via hook dispatching script: dispatch) failed: exit status 1
2024-09-12 09:56:50 INFO juju.worker.uniter resolver.go:180 awaiting error resolution for "relation-broken" hook
2024-09-12 10:00:46 INFO juju.worker.uniter resolver.go:180 awaiting error resolution for "relation-broken" hook
2024-09-12 10:01:50 INFO juju.worker.uniter resolver.go:180 awaiting error resolution for "relation-broken" hook
2024-09-12 10:01:53 WARNING unit.cos-proxy/7.juju-log server.go:325 prometheus-target:700: 'app' expected but not received.
2024-09-12 10:01:53 WARNING unit.cos-proxy/7.juju-log server.go:325 prometheus-target:700: 'app_name' expected in snapshot but not found.
2024-09-12 10:01:53 WARNING unit.cos-proxy/7.juju-log server.go:325 prometheus-target:700: 'app_name' expected in snapshot but not found.
2024-09-12 10:01:53 ERROR unit.cos-proxy/7.juju-log server.go:325 prometheus-target:700: Uncaught exception while in charm code:
Traceback (most recent call last):
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/./src/charm.py", line 643, in
main(COSProxyCharm)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 544, in main
manager.run()
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 520, in run
self._emit()
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 509, in _emit
_emit_charm_event(self.charm, self.dispatcher.event_name)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 143, in _emit_charm_event
event_to_emit.emit(*args, **kwargs)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/framework.py", line 352, in emit
framework._emit(event)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/framework.py", line 851, in _emit
self._reemit(event_path)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/framework.py", line 941, in _reemit
custom_handler(event)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/./src/charm.py", line 560, in _prometheus_target_relation_broken
self._handle_prometheus_alert_rule_files(RULES_DIR, event.app.name)
AttributeError: 'NoneType' object has no attribute 'name'
2024-09-12 10:01:54 ERROR juju.worker.uniter.operation runhook.go:180 hook "prometheus-target-relation-broken" (via hook dispatching script: dispatch) failed: exit status 1
2024-09-12 10:01:54 INFO juju.worker.uniter resolver.go:180 awaiting error resolution for "relation-broken" hook
2024-09-12 10:05:44 INFO juju.worker.uniter resolver.go:180 awaiting error resolution for "relation-broken" hook
2024-09-12 10:06:54 INFO juju.worker.uniter resolver.go:180 awaiting error resolution for "relation-broken" hook
2024-09-12 10:06:57 WARNING unit.cos-proxy/7.juju-log server.go:325 prometheus-target:700: 'app' expected but not received.
2024-09-12 10:06:57 WARNING unit.cos-proxy/7.juju-log server.go:325 prometheus-target:700: 'app_name' expected in snapshot but not found.
2024-09-12 10:06:57 WARNING unit.cos-proxy/7.juju-log server.go:325 prometheus-target:700: 'app_name' expected in snapshot but not found.
2024-09-12 10:06:57 ERROR unit.cos-proxy/7.juju-log server.go:325 prometheus-target:700: Uncaught exception while in charm code:
Traceback (most recent call last):
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/./src/charm.py", line 643, in
main(COSProxyCharm)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 544, in main
manager.run()
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 520, in run
self._emit()
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 509, in _emit
_emit_charm_event(self.charm, self.dispatcher.event_name)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/main.py", line 143, in _emit_charm_event
event_to_emit.emit(*args, **kwargs)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/framework.py", line 352, in emit
framework._emit(event)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/framework.py", line 851, in _emit
self._reemit(event_path)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/venv/ops/framework.py", line 941, in _reemit
custom_handler(event)
File "/var/lib/juju/agents/unit-cos-proxy-7/charm/./src/charm.py", line 560, in _prometheus_target_relation_broken
self._handle_prometheus_alert_rule_files(RULES_DIR, event.app.name)
AttributeError: 'NoneType' object has no attribute 'name'
2024-09-12 10:06:57 ERROR juju.worker.uniter.operation runhook.go:180 hook "prometheus-target-relation-broken" (via hook dispatching script: dispatch) failed: exit status 1
2024-09-12 10:06:57 INFO juju.worker.uniter resolver.go:180 awaiting error resolution for "relation-broken" hook
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in src/charm.py at _prometheus_target_relation_broken and the linked relation-broken handling around lines 622-624, then inspect the Grafana dashboard handler in lib/charms/grafana_k8s/v0/grafana_dashboard.py. Reproduce relation removal if possible and verify that dashboards and prometheus-target relation-broken hooks complete without an AttributeError when event.app is absent.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devops, infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.