-
Notifications
You must be signed in to change notification settings - Fork 244
feat: add X-Logfire-Telemetry header #1905
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
Open
adriangb
wants to merge
2
commits into
main
Choose a base branch
from
add-telemetry-header
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+238
−13
Open
Changes from all commits
Commits
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,70 @@ | ||
| """Build the `X-Logfire-Telemetry` request header. | ||
|
|
||
| The header carries non-sensitive, config-derived signals about how this SDK | ||
| instance is configured, encoded as a compact JSON object. SDK/runtime identity | ||
| (version, language, Python version, OS, etc.) lives on the standard | ||
| `User-Agent` header instead — see `UA_HEADER` in `_internal/client.py`. | ||
| Secrets (`token`, `api_key`, `service_name`, etc.) are never included. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .config import LogfireConfig | ||
|
|
||
|
|
||
| TELEMETRY_HEADER_NAME = 'X-Logfire-Telemetry' | ||
|
|
||
|
|
||
| def _config_telemetry_pairs(config: LogfireConfig) -> dict[str, Any]: | ||
| """Pick fields of `LogfireConfig` that are useful for product analytics. | ||
|
|
||
| Each field below has an explicit rationale; do not add a field unless you have | ||
| one. Everything else either duplicates information the server already knows, | ||
| isn't actionable, or risks leaking sensitive data (token, api_key, | ||
| service_name, environment, etc.). | ||
| """ | ||
| # Multi-project usage: how many users configure more than one write token in | ||
| # a single SDK instance. Drives auth/routing roadmap decisions. | ||
| token = config.token | ||
| if isinstance(token, list): | ||
| token_count = len(token) | ||
| elif token: | ||
| token_count = 1 | ||
| else: | ||
| token_count = 0 | ||
|
|
||
| pairs: dict[str, Any] = { | ||
| # Adoption signal for the `code_source=` option (newer feature): tells us | ||
| # whether the integration with the source-code link UI is worth investing in. | ||
| 'code_source_set': config.code_source is not None, | ||
| # Adoption signal for the variables / feature-flag feature (newer feature): | ||
| # informs whether to keep building on it. | ||
| 'variables_set': config.variables is not None, | ||
| 'token_count': token_count, | ||
| } | ||
|
|
||
| if config._service_instance_id: # pyright: ignore[reportPrivateUsage] | ||
| # Mirrors the OTLP resource attribute of the same name | ||
| # (https://opentelemetry.io/docs/specs/semconv/registry/attributes/service/#service-instance-id). | ||
| # High-cardinality per-process identifier — kept here rather than in | ||
| # User-Agent because user-agent strings are typically aggregated and a | ||
| # per-instance id would explode the cardinality of any UA-based analytics. | ||
| pairs['service_instance_id'] = config._service_instance_id # pyright: ignore[reportPrivateUsage] | ||
|
|
||
| return pairs | ||
|
|
||
|
|
||
| def build_telemetry_header(config: LogfireConfig | None = None) -> str | None: | ||
| """Return the JSON-encoded `X-Logfire-Telemetry` value, or None if no config. | ||
|
|
||
| Without a `LogfireConfig` there is nothing config-specific to report — the | ||
| SDK/runtime identity is already in `User-Agent`, so callers should simply | ||
| omit the header in that case. | ||
| """ | ||
| if config is None: | ||
| return None | ||
| return json.dumps(_config_telemetry_pairs(config), separators=(',', ':')) |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove the user agent? Why not do the opposite and put everything in there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy to keep the user agent. But does token count (a proxy for project count) which @samuelcolvin requested really belong in the user agent header? I also fear that things (proxies, etc.) expect it to be ~ low cardinality and putting something like the service instance id (which I think is important to be able to correlate what features are used together and segment users) in there might break something that makes that assumption.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about using the user agent everywhere for _base_telemetry_pairs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use the user agent for things that have some semblence of belonging there. But high cardinality things (service instance id) or feature flags / feature usage IMO do not belong there. I spoke with @Viicos today about this and we're on the same page in that regard.
I am pushing a commit to restore User-Agent