DiamondLightSource / DiamondLightSource/observability-utils

Consider making boilerplate implicit

Open
#19 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

I am seeing the following flavour of boilerplate a lot:

```python
OTLP_EXPORT_ENABLED = environ.get("OTLP_EXPORT_ENABLED", "false").lower() == "true"

setup_tracing("", with_otlp_export=OTLP_EXPORT_ENABLED)
TRACER = get_tracer("")

# ...later on

@start_as_current_span(TRACER, "bar")
def foo(bar: int) -> None: ...
```

It feels like we could make it implicit, and have this boilerplate automatically run on the first use of the decorator, something like

```python
@functools.cache # Means this function will execute lazily once per value of package_name and cache the return value
def default_tracer(package_name: str) -> Tracer:
setup_tracing(package_name, with_otlp_export=OTLP_EXPORT_ENABLED)
return = get_tracer(package_name)

def start_as_current_span(tracer: Tracer | None = None, ...) -> ...:
package_name = ... # Presuming there is a way to determine programatically
tracer = tracer or default_tracer(package_name)
...
```

## Acceptance Criteria
- The library works as-advertised without having to write the above boilerplate

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.