-
Notifications
You must be signed in to change notification settings - Fork 14
Add span origin provenance #570
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
Merged
Abhijeet Prasad (AbhiPrasad)
merged 5 commits into
main
from
agent/span-origin-provenance
Jul 15, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bb908fa
Add span origin provenance
Qard 9e12ee8
Format Python OTel imports
Qard 7c7ad6a
Preserve span origin at export
Qard ccc6039
fix: keep span origin environment in context
Qard b5f572e
fix: preserve span origin environment name
Qard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import importlib.metadata | ||
| import os | ||
| from typing import Any, TypedDict | ||
|
|
||
| from .env import BraintrustEnv | ||
|
|
||
|
|
||
| class SpanOriginEnvironment(TypedDict, total=False): | ||
| type: str | ||
| name: str | ||
|
|
||
|
|
||
| def detect_environment( | ||
| explicit: SpanOriginEnvironment | None = None, | ||
| ) -> SpanOriginEnvironment | None: | ||
| if explicit: | ||
| return explicit | ||
|
|
||
| env_type = BraintrustEnv.ENVIRONMENT_TYPE.get(None, use_dotenv=True) | ||
| env_name = BraintrustEnv.ENVIRONMENT_NAME.get(None, use_dotenv=True) | ||
| if env_type or env_name: | ||
| return { | ||
| **({"type": env_type} if env_type else {}), | ||
| **({"name": env_name} if env_name else {}), | ||
| } | ||
|
|
||
| ci = _first_present( | ||
| { | ||
| "GITHUB_ACTIONS": "github_actions", | ||
| "GITLAB_CI": "gitlab_ci", | ||
| "CIRCLECI": "circleci", | ||
| "BUILDKITE": "buildkite", | ||
| "JENKINS_URL": "jenkins", | ||
| "JENKINS_HOME": "jenkins", | ||
| "TF_BUILD": "azure_pipelines", | ||
| "TEAMCITY_VERSION": "teamcity", | ||
| "TRAVIS": "travis", | ||
| "BITBUCKET_BUILD_NUMBER": "bitbucket", | ||
| } | ||
| ) | ||
| if ci: | ||
| return {"type": "ci", "name": ci} | ||
| if os.environ.get("CI"): | ||
| return {"type": "ci", "name": "ci"} | ||
|
|
||
| server = _first_present( | ||
| { | ||
| "VERCEL": "vercel", | ||
| "NETLIFY": "netlify", | ||
| } | ||
| ) | ||
| if server: | ||
| return {"type": "server", "name": server} | ||
|
|
||
| if os.environ.get("ECS_CONTAINER_METADATA_URI") or os.environ.get("ECS_CONTAINER_METADATA_URI_V4"): | ||
| return {"type": "server", "name": "ecs"} | ||
| aws_execution_env = os.environ.get("AWS_EXECUTION_ENV") | ||
| if aws_execution_env: | ||
| if aws_execution_env.startswith("AWS_ECS_"): | ||
| return {"type": "server", "name": "ecs"} | ||
| if aws_execution_env.startswith("AWS_Lambda_"): | ||
| return {"type": "server", "name": "aws_lambda"} | ||
| if os.environ.get("AWS_LAMBDA_FUNCTION_NAME"): | ||
| return {"type": "server", "name": "aws_lambda"} | ||
|
|
||
| server = _first_present( | ||
| { | ||
| "K_SERVICE": "cloud_run", | ||
| "FUNCTION_TARGET": "gcp_functions", | ||
| "KUBERNETES_SERVICE_HOST": "kubernetes", | ||
| "DYNO": "heroku", | ||
| "FLY_APP_NAME": "fly", | ||
| "RAILWAY_ENVIRONMENT": "railway", | ||
| "RENDER_SERVICE_NAME": "render", | ||
| } | ||
| ) | ||
| if server: | ||
| return {"type": "server", "name": server} | ||
|
|
||
| return _deployment_mode(os.environ.get("PYTHON_ENV")) | ||
|
|
||
|
|
||
| def merge_span_origin_context( | ||
| context: dict[str, Any] | None, | ||
| instrumentation_name: str, | ||
| environment: SpanOriginEnvironment | None, | ||
| ) -> dict[str, Any]: | ||
| merged = dict(context or {}) | ||
| span_origin = dict(merged.get("span_origin") or {}) | ||
| span_origin = { | ||
| "name": "braintrust.sdk.python", | ||
| "version": _sdk_version(), | ||
| "instrumentation": {"name": instrumentation_name}, | ||
| **({"environment": environment} if environment else {}), | ||
| **span_origin, | ||
| } | ||
| merged["span_origin"] = span_origin | ||
| return merged | ||
|
|
||
|
|
||
| def _sdk_version() -> str: | ||
| try: | ||
| return importlib.metadata.version("braintrust") | ||
| except importlib.metadata.PackageNotFoundError: | ||
| return "unknown" | ||
|
|
||
|
|
||
| def _first_present(mapping: dict[str, str]) -> str | None: | ||
| for key, name in mapping.items(): | ||
| if os.environ.get(key): | ||
| return name | ||
| return None | ||
|
|
||
|
|
||
| def _deployment_mode(value: str | None) -> SpanOriginEnvironment | None: | ||
| if not value: | ||
| return None | ||
| normalized = value.lower() | ||
| if normalized in ("production", "staging"): | ||
| return {"type": "server", "name": normalized} | ||
| if normalized in ("development", "local"): | ||
| return {"type": "local", "name": normalized} | ||
| return None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.