Use APPLICATIONINSIGHTS_PYTHON_ATTACHTYPE env var in _is_attach_enabled#46955
Merged
Conversation
269acd9 to
84a7386
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates attach detection in azure-monitor-opentelemetry-exporter by introducing support for the APPLICATIONINSIGHTS_PYTHON_ATTACHTYPE environment variable, aiming to reduce false positives from legacy attach heuristics.
Changes:
- Added
_APPLICATIONINSIGHTS_PYTHON_ATTACHTYPEconstant and updated_is_attach_enabled()to consult it before legacy detection. - Added unit tests covering attach-type scenarios across App Service, Functions, and AKS.
- Documented the change in the package changelog.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_utils.py | Adds attach-type env-var check to _is_attach_enabled(), before falling back to legacy heuristics. |
| sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_constants.py | Introduces constant for APPLICATIONINSIGHTS_PYTHON_ATTACHTYPE. |
| sdk/monitor/azure-monitor-opentelemetry-exporter/tests/test_utils.py | Adds tests for attach enablement behavior when APPLICATIONINSIGHTS_PYTHON_ATTACHTYPE is set and when it is absent. |
| sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md | Adds changelog entry describing the new attach detection behavior. |
Comments suppressed due to low confidence (1)
sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_utils.py:86
_is_attach_enabledshort-circuits toTruewhenAPPLICATIONINSIGHTS_PYTHON_ATTACHTYPE == "IntegratedAuto", but the in-code comment (and PR description) says it should also satisfy the existing per-RP attach checks (Functions/App Service/AKS). As written, it bypasses checks like FunctionsPYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY, App Service agent path, and AKSAKS_ARM_NAMESPACE_ID, which can reintroduce false positives. Either apply the per-RP logic before returningTrue(when attach_type is IntegratedAuto) or update the comment/contract to match the intended authoritative behavior.
def _is_attach_enabled():
attach_type = environ.get(_APPLICATIONINSIGHTS_PYTHON_ATTACHTYPE)
if attach_type is not None:
# If the env var is set, attach is only enabled if the value is
# "IntegratedAuto" AND the existing per-RP logic is satisfied.
if attach_type == "IntegratedAuto":
return True
return False
# Fallback to legacy logic when the env var is not set
if _is_on_functions():
return environ.get(_PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY) == "true"
if _is_on_app_service():
return isdir("/agents/python/")
if _is_on_aks():
return _AKS_ARM_NAMESPACE_ID in environ
return False
rads-1996
reviewed
May 27, 2026
Update _is_attach_enabled() to check the new APPLICATIONINSIGHTS_PYTHON_ATTACHTYPE environment variable set by the RP integration agent (sitecustomize.py). When the env var is present, attach is only considered enabled if it is set to IntegratedAuto AND the existing per-RP logic (Functions, App Service, AKS) is satisfied. When the env var is absent, the function falls back to legacy detection (path-based for App Service, env-var-based for Functions/AKS). This resolves false positives where the agent path exists but auto-instrumentation backed off or failed to attach. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Update _is_attach_enabled() to check the new
APPLICATIONINSIGHTS_PYTHON_ATTACHTYPE environment variable set by the RP integration agent (sitecustomize.py). When the env var is present, attach is only considered enabled if it is set to IntegratedAuto AND the existing per-RP logic (Functions, App Service, AKS) is satisfied. When the env var is absent, the function falls back to legacy detection (path-based for App Service, env-var-based for Functions/AKS).
This resolves false positives where the agent path exists but auto-instrumentation backed off or failed to attach.
Description
Please add an informative description that covers that changes made by the pull request and link all relevant issues.
If an SDK is being regenerated based on a new API spec, a link to the pull request containing these API spec changes should be included above.
All SDK Contribution checklist:
General Guidelines and Best Practices
Testing Guidelines