Skip to content

Commit

Permalink
Define appsignal.start() initialization method
Browse files Browse the repository at this point in the history
This allows for a more ergonomic replacement to the previous:

```python
from __appsignal__ import appsignal
appsignal.start()
```

Into a simpler:

```python
import appsignal
appsignal.start()
```

But, more importantly, it allows users to keep the AppSignal
configuration file at the project root (where commands such as
`appsignal diagnose` expect to find it) and refer it from other
parts of their application (such as `project_name/wsgi.py` in a
Django project) without imposing the demand for the project root
to itself be a Python package.
  • Loading branch information
unflxw committed Mar 15, 2024
1 parent 0a1eb25 commit 74febe3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 42 deletions.
59 changes: 59 additions & 0 deletions src/appsignal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from __future__ import annotations

import os
from runpy import run_path

from .client import Client as Appsignal
from .metrics import add_distribution_value, increment_counter, set_gauge
from .tracing import (
Expand Down Expand Up @@ -37,9 +42,63 @@
"increment_counter",
"set_gauge",
"add_distribution_value",
"start",
]


# Load the AppSignal client from the app specific `__appsignal__.py` client
# file. This loads the user config, rather than our default config.
# If no client file is found it return `None`.
# If there's a problem with the client file it will raise an
# `InvalidClientFileError` with a message containing more details.
def _client_from_config_file() -> Appsignal | None:
cwd = os.getcwd()
app_config_path = os.path.join(cwd, "__appsignal__.py")
if os.path.exists(app_config_path):
try:
client = run_path(app_config_path)["appsignal"]
if not isinstance(client, Appsignal):
raise InvalidClientFileError(
"The `appsignal` variable in `__appsignal__.py` does not "
"contain an AppSignal client. "
"Please define the configuration file as described in "
"our documentation: "
"https://docs.appsignal.com/python/configuration.html"
)

return client
except KeyError as error:
raise InvalidClientFileError(
"No `appsignal` variable found in `__appsignal__.py`. "
"Please define the configuration file as described in "
"our documentation: "
"https://docs.appsignal.com/python/configuration.html"
) from error

return None


def _must_client_from_config_file() -> Appsignal:
client = _client_from_config_file()
if client is None:
raise InvalidClientFileError(
"No `__appsignal__.py` file found in the current directory. "
"Please define the configuration file as described in "
"our documentation: "
"https://docs.appsignal.com/python/configuration.html"
)

return client


def start() -> None:
_must_client_from_config_file().start()


class InvalidClientFileError(Exception):
pass


# Try and load the appsignal-beta package. If it's present and imported, it
# will print a message about switching to the `appsignal` package.
try:
Expand Down
2 changes: 1 addition & 1 deletion src/appsignal/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from argparse import ArgumentParser, Namespace
from dataclasses import dataclass

from .. import _client_from_config_file
from ..client import Client
from ..config import Config, Options
from ..push_api_key_validator import PushApiKeyValidator
from .config import _client_from_config_file
from .exit_error import ExitError


Expand Down
41 changes: 0 additions & 41 deletions src/appsignal/cli/config.py

This file was deleted.

0 comments on commit 74febe3

Please sign in to comment.