Skip to content

Use APPLICATIONINSIGHTS_PYTHON_ATTACHTYPE env var in _is_attach_enabled#46955

Merged
jeremydvoss merged 6 commits into
Azure:mainfrom
jeremydvoss:attach-type
May 28, 2026
Merged

Use APPLICATIONINSIGHTS_PYTHON_ATTACHTYPE env var in _is_attach_enabled#46955
jeremydvoss merged 6 commits into
Azure:mainfrom
jeremydvoss:attach-type

Conversation

@jeremydvoss
Copy link
Copy Markdown
Member

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:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

@github-actions github-actions Bot added the Monitor - Exporter Monitor OpenTelemetry Exporter label May 18, 2026
@jeremydvoss jeremydvoss force-pushed the attach-type branch 2 times, most recently from 269acd9 to 84a7386 Compare May 27, 2026 18:24
@jeremydvoss jeremydvoss marked this pull request as ready for review May 27, 2026 19:01
Copilot AI review requested due to automatic review settings May 27, 2026 19:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_ATTACHTYPE constant 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_enabled short-circuits to True when APPLICATIONINSIGHTS_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 Functions PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY, App Service agent path, and AKS AKS_ARM_NAMESPACE_ID, which can reintroduce false positives. Either apply the per-RP logic before returning True (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

Comment thread sdk/monitor/azure-monitor-opentelemetry-exporter/tests/test_utils.py Outdated
Comment thread sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md
jeremydvoss and others added 5 commits May 28, 2026 15:09
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>
Copy link
Copy Markdown
Member

@rads-1996 rads-1996 left a comment

Choose a reason for hiding this comment

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

LGTM

@jeremydvoss jeremydvoss enabled auto-merge (squash) May 28, 2026 22:33
@jeremydvoss jeremydvoss merged commit 805cfa4 into Azure:main May 28, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Monitor - Exporter Monitor OpenTelemetry Exporter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants