|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2025 Canonical Ltd. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""FIXME dummy_load docstring.""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import time |
| 20 | + |
| 21 | +import opentelemetry.trace |
| 22 | + |
| 23 | +import ops |
| 24 | + |
| 25 | +tracer = opentelemetry.trace.get_tracer(__name__) |
| 26 | + |
| 27 | + |
| 28 | +class DatabaseReadyEvent(ops.charm.EventBase): |
| 29 | + """Event representing that the database is ready.""" |
| 30 | + |
| 31 | + |
| 32 | +class DatabaseRequirerEvents(ops.framework.ObjectEvents): |
| 33 | + """Container for Database Requirer events.""" |
| 34 | + |
| 35 | + ready = ops.charm.EventSource(DatabaseReadyEvent) |
| 36 | + |
| 37 | + |
| 38 | +class DatabaseRequirer(ops.framework.Object): |
| 39 | + """Dummy docstring.""" |
| 40 | + |
| 41 | + on = DatabaseRequirerEvents() # type: ignore |
| 42 | + |
| 43 | + def __init__(self, charm: ops.CharmBase): |
| 44 | + """Dummy docstring.""" |
| 45 | + super().__init__(charm, 'foo') |
| 46 | + self.framework.observe(charm.on.start, self._on_db_changed) |
| 47 | + |
| 48 | + def _on_db_changed(self, event: ops.StartEvent) -> None: |
| 49 | + """Dummy docstring.""" |
| 50 | + self.on.ready.emit() |
| 51 | + |
| 52 | + |
| 53 | +class FakeCharm(ops.CharmBase): |
| 54 | + """Dummy docstring.""" |
| 55 | + |
| 56 | + def __init__(self, framework: ops.Framework): |
| 57 | + """Dummy docstring.""" |
| 58 | + super().__init__(framework) |
| 59 | + self.framework.observe(self.on.setup_tracing, self._on_setup_tracing) |
| 60 | + self.framework.observe(self.on.start, self._on_start) |
| 61 | + self.framework.observe(self.on.collect_app_status, self._on_collect_app_status) |
| 62 | + self.db_requirer = DatabaseRequirer(self) |
| 63 | + self.framework.observe(self.db_requirer.on.ready, self._on_db_ready) |
| 64 | + |
| 65 | + def _on_setup_tracing(self, event: ops.SetupTracingEvent) -> None: |
| 66 | + """Dummy docstring.""" |
| 67 | + self.dummy_load(event) |
| 68 | + event.set_destination(url='http://localhost:4318/v1/traces') |
| 69 | + |
| 70 | + def _on_start(self, event: ops.StartEvent) -> None: |
| 71 | + """Dummy docstring.""" |
| 72 | + self.dummy_load(event) |
| 73 | + event.defer() |
| 74 | + |
| 75 | + def _on_db_ready(self, event: DatabaseReadyEvent) -> None: |
| 76 | + self.dummy_load(event) |
| 77 | + |
| 78 | + def _on_collect_app_status(self, event: ops.CollectStatusEvent) -> None: |
| 79 | + """Dummy docstring.""" |
| 80 | + self.dummy_load(event) |
| 81 | + event.add_status(ops.ActiveStatus('app seems ready')) |
| 82 | + |
| 83 | + def _on_collect_unit_status(self, event: ops.CollectStatusEvent) -> None: |
| 84 | + """Dummy docstring.""" |
| 85 | + self.dummy_load(event) |
| 86 | + event.add_status(ops.ActiveStatus('unit ready')) |
| 87 | + |
| 88 | + @tracer.start_as_current_span('FakeCharm.dummy_load') |
| 89 | + def dummy_load(self, event: ops.EventBase, duration: float = 0.0001) -> None: |
| 90 | + """Dummy docstring.""" |
| 91 | + print(event) |
| 92 | + time.sleep(duration) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + ops.main(FakeCharm) |
0 commit comments