dashboards-relation-changed failed, if remove_unit is None

Bug #1946006 reported by Robert Gildein
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Grafana Charm
Fix Released
Medium
Alvaro Uria

Bug Description

When `dashboards-relation-{joined,changed}` hook is triggered and the
remote_unit is None, this hook fails and the unit enters an error state.

The solution would be to move this condition [1] to this line [2].

---
[1]: https://git.launchpad.net/charm-grafana/tree/src/reactive/grafana.py#n1282
[2]: https://git.launchpad.net/charm-grafana/tree/src/reactive/grafana.py#n1277

Related branches

description: updated
Revision history for this message
Robert Gildein (rgildein) wrote :

This error was noticed when I tried to update the relation data via
`event.relation.data[self.app].update(...)` in the charm based on the
operator framework.

I think it makes more sense when data is part of an application
relation data, which is not supported now. Solution for that will
be using `relation_id=None` if remote_unit is None.

Revision history for this message
Alvaro Uria (aluria) wrote :

The charm runs:
"""
remote_unit = os.environ.get("JUJU_REMOTE_UNIT", None)
relation_id = hookenv.relation_id("dashboards", remote_unit)
"""

However, an envvar exists with the relation_id [1]. Since this handler is only run when the dashboards-relation-{joined,changed} are triggered, I assume the relation ids are of type "dashboards:<ID>", which is the value that would be returned by "hookenv.relation_id".

It is still unclear to me what are the envvars available when "event.relation.data[self.app].update" is run at the other side of the relation, and how charm-grafana should react to it. Can "hookenv.relation_get" be run when data is shared at the application level rather than at the unit level? Please clarify.

1. https://juju.is/docs/olm/environment-variables#heading--juju_relation_id

Revision history for this message
Robert Gildein (rgildein) wrote :
Download full text (3.4 KiB)

I will describe this with an example that is able to accept the use of
`self.unit` as well as` self.app`.

Here is how I modified the `dashboards_joined` [1] function:
```python
@hook("dashboards-relation-joined", "dashboards-relation-changed")
def dashboards_joined(dashboards):
    """Process dashboards from related charms."""
    hookenv.log("hook %s triggered" % hookenv.hook_name(), level="CRITICAL")

    remote_unit = os.environ.get("JUJU_REMOTE_UNIT", None)
    hookenv.log("remote_unit: `%s`" % remote_unit, level="CRITICAL")
    hookenv.log("environ: %s" % {key: value for key, value in os.environ.items() if key.startswith("JUJU_REMOTE")}, level="CRITICAL")

    if not remote_unit:
        hookenv.log("remote_unit is None, return", "WARNING")
        relation_id = None
    else:
        relation_id = hookenv.relation_id("dashboards", remote_unit)
        hookenv.log(
            "remote_unit {}, relation_id {}".format(remote_unit, relation_id), "INFO"
        )
```

And here's an example when I used it
`event.relation.data[self.unit].update(...)`:

```bash
unit-grafana-0: 13:09:11 CRITICAL unit.grafana/0.juju-log dashboards:67: hook dashboards-relation-joined triggered
unit-grafana-0: 13:09:11 CRITICAL unit.grafana/0.juju-log dashboards:67: remote_unit: `prometheus-bind-exporter/1`
unit-grafana-0: 13:09:11 CRITICAL unit.grafana/0.juju-log dashboards:67: environ: {'JUJU_REMOTE_UNIT': 'prometheus-bind-exporter/1', 'JUJU_REMOTE_APP': 'prometheus-bind-exporter'}
unit-grafana-0: 13:09:13 CRITICAL unit.grafana/0.juju-log dashboards:67: hook dashboards-relation-changed triggered
unit-grafana-0: 13:09:13 CRITICAL unit.grafana/0.juju-log dashboards:67: remote_unit: `prometheus-bind-exporter/1`
unit-grafana-0: 13:09:13 CRITICAL unit.grafana/0.juju-log dashboards:67: environ: {'JUJU_REMOTE_UNIT': 'prometheus-bind-exporter/1', 'JUJU_REMOTE_APP': 'prometheus-bind-exporter'}
```

And here's an example when I used it
`event.relation.data[self.app].update(...)`:

```bash
unit-grafana-0: 13:13:15 CRITICAL unit.grafana/0.juju-log dashboards:68: hook dashboards-relation-joined triggered
unit-grafana-0: 13:13:15 CRITICAL unit.grafana/0.juju-log dashboards:68: remote_unit: `prometheus-bind-exporter/1`
unit-grafana-0: 13:13:15 CRITICAL unit.grafana/0.juju-log dashboards:68: environ: {'JUJU_REMOTE_UNIT': 'prometheus-bind-exporter/1', 'JUJU_REMOTE_APP': 'prometheus-bind-exporter'}
unit-grafana-0: 13:13:17 CRITICAL unit.grafana/0.juju-log dashboards:68: hook dashboards-relation-changed triggered
unit-grafana-0: 13:13:17 CRITICAL unit.grafana/0.juju-log dashboards:68: remote_unit: `prometheus-bind-exporter/1`
unit-grafana-0: 13:13:17 CRITICAL unit.grafana/0.juju-log dashboards:68: environ: {'JUJU_REMOTE_UNIT': 'prometheus-bind-exporter/1', 'JUJU_REMOTE_APP': 'prometheus-bind-exporter'}
unit-grafana-0: 13:13:19 CRITICAL unit.grafana/0.juju-log dashboards:68: hook dashboards-relation-changed triggered
unit-grafana-0: 13:13:19 CRITICAL unit.grafana/0.juju-log dashboards:68: remote_unit: ``
unit-grafana-0: 13:13:19 CRITICAL unit.grafana/0.juju-log dashboards:68: environ: {'JUJU_REMOTE_UNIT': '', 'JUJU_REMOTE_APP': 'prometheus-bind-exporter'}
```

As you can see, whe...

Read more...

Alvaro Uria (aluria)
Changed in charm-grafana:
status: New → In Progress
assignee: nobody → Alvaro Uria (aluria)
importance: Undecided → Medium
Alvaro Uria (aluria)
Changed in charm-grafana:
status: In Progress → Fix Committed
milestone: none → 21.10
Celia Wang (ziyiwang)
Changed in charm-grafana:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.