Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CodeSource.work_dir #532

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/guides/advanced/link-to-code-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ logfire.configure(
repository='https://github.com/pydantic/logfire', #(1)!
revision='<hash of commit used on release>', #(2)!
root_path='.', #(3)!
work_dir='.' #(4)!
)
)
```

1. The URL of the repository e.g. `https://github.com/pydantic/logfire`.
2. The specific branch, tag, or commit hash to link to e.g. `main`.
3. The path to the root of the repository. If your code is in a subdirectory, you can specify it here.
4. The path to where the code is being executed. This is used to determine the relative path of the code file.

You can learn more in our [`logfire.CodeSource`][logfire.CodeSource] API reference.

Expand Down
12 changes: 11 additions & 1 deletion logfire/_internal/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
DEFAULT_FALLBACK_FILE_NAME,
OTLP_MAX_BODY_SIZE,
RESOURCE_ATTRIBUTES_CODE_ROOT_PATH,
RESOURCE_ATTRIBUTES_CODE_WORK_DIR,
RESOURCE_ATTRIBUTES_VCS_REPOSITORY_REF_REVISION,
RESOURCE_ATTRIBUTES_VCS_REPOSITORY_URL,
LevelName,
Expand Down Expand Up @@ -207,14 +208,22 @@ class CodeSource:
revision: str
"""The git revision of the code e.g. branch name, commit hash, tag name etc."""

root_path: str
root_path: str = field(default='.')
"""The root path for the source code in the repository.

Example:
If the `code.filename` is `/path/to/project/src/logfire/main.py` and the `root_path` is `src/`, the URL
for the source code will be `src/path/to/project/src/logfire/main.py`.
Comment on lines 214 to 216
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still needs updating

"""

work_dir: str = field(default='.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this setting at all? Isn't it just os.getcwd()?

"""This is the absolute path to the directory where the code is executed.

Example:
If you see the code in `/path/to/project/src/logfire/main.py`, and on your repository the code is in
`src/logfire/main.py`, then the `work_dir` should be `/path/to/project`.
"""


class DeprecatedKwargs(TypedDict):
# Empty so that passing any additional kwargs makes static type checkers complain.
Expand Down Expand Up @@ -672,6 +681,7 @@ def _initialize(self) -> None:
if self.code_source:
otel_resource_attributes.update(
{
RESOURCE_ATTRIBUTES_CODE_WORK_DIR: self.code_source.work_dir,
RESOURCE_ATTRIBUTES_CODE_ROOT_PATH: self.code_source.root_path,
RESOURCE_ATTRIBUTES_VCS_REPOSITORY_URL: self.code_source.repository,
RESOURCE_ATTRIBUTES_VCS_REPOSITORY_REF_REVISION: self.code_source.revision,
Expand Down
3 changes: 3 additions & 0 deletions logfire/_internal/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ def log_level_attributes(level: LevelName | int) -> dict[str, otel_types.Attribu
RESOURCE_ATTRIBUTES_CODE_ROOT_PATH = 'logfire.code.root_path'
"""The root path of the current repository."""

RESOURCE_ATTRIBUTES_CODE_WORK_DIR = 'logfire.code.work_dir'
"""The working directory of the application."""

OTLP_MAX_INT_SIZE = 2**63 - 1
"""OTLP only supports signed 64-bit integers, larger integers get sent as strings."""

Expand Down
1 change: 1 addition & 0 deletions tests/test_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,7 @@ def test_code_source(config_kwargs: dict[str, Any], exporter: TestExporter):
'service.name': 'unknown_service',
'process.pid': 1234,
'logfire.code.root_path': 'logfire',
'logfire.code.work_dir': '.',
'vcs.repository.url.full': 'https://github.com/pydantic/logfire',
'vcs.repository.ref.revision': 'main',
'service.version': '1.2.3',
Expand Down