Skip to content

feat: Add ability to selectively enable exporting of SDK internal metrics with the OTEL_PYTHON_SDK_INTERNAL_METRICS_ENABLED environment variable#5151

Open
herin049 wants to merge 11 commits intoopen-telemetry:mainfrom
herin049:feat/skd-metrics-opt-in
Open

feat: Add ability to selectively enable exporting of SDK internal metrics with the OTEL_PYTHON_SDK_INTERNAL_METRICS_ENABLED environment variable#5151
herin049 wants to merge 11 commits intoopen-telemetry:mainfrom
herin049:feat/skd-metrics-opt-in

Conversation

@herin049
Copy link
Copy Markdown
Contributor

@herin049 herin049 commented Apr 27, 2026

Description

Adds ability to selectively enable/disable exporting of SDK internal metric collection with the introduction of the OTEL_PYTHON_SDK_METRIC_ENABLED environment variable.

Motivation

While the "Stable by Default" OTEP has not been finalized yet, it is still preferrable to not emit unstable metric by default.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

uv run tox -e py314-test-opentelemetry-sdk

Does This PR Require a Contrib Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@herin049 herin049 requested a review from a team as a code owner April 27, 2026 03:26
@herin049 herin049 moved this to Reviewed PRs that need fixes in Python PR digest Apr 27, 2026
@herin049 herin049 moved this from Reviewed PRs that need fixes to Ready for review in Python PR digest Apr 27, 2026
@herin049 herin049 added the Approve Public API check This label shows that the public symbols added or changed in a PR are strictly necessary label Apr 27, 2026
Copy link
Copy Markdown
Member

@MikeGoldsmith MikeGoldsmith left a comment

Choose a reason for hiding this comment

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

Looks great, thanks @herin049.

It would be good to replace existing env var parsing with the new parse_boolean_environment_variable helper you've added in a follow-up PR.

@github-project-automation github-project-automation Bot moved this from Ready for review to Approved PRs in Python PR digest Apr 27, 2026
Comment thread CHANGELOG.md Outdated
@herin049 herin049 changed the title feat: Add ability to selectively enable exporting of SDK internal metrics with the OTEL_PYTHON_SDK_METRIC_ENABLED environment variable feat: Add ability to selectively enable exporting of SDK internal metrics with the OTEL_PYTHON_SDK_INTERNAL_METRICS_ENABLED environment variable Apr 28, 2026
@herin049 herin049 requested a review from DylanRussell April 28, 2026 02:00
@herin049 herin049 force-pushed the feat/skd-metrics-opt-in branch from c224202 to e528cde Compare April 29, 2026 21:24
Copy link
Copy Markdown
Member

@emdneto emdneto left a comment

Choose a reason for hiding this comment

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

Cool. I was implementing this and just saw your PR today.
@herin049 wdyt to instead use the concept of a NoOpExporterMetrics? This way, we also avoid creating the instruments.

Something like:

class NoOpExporterMetrics:
    @contextmanager
    def export_operation(self, num_items: int) -> Iterator[ExportResult]:
        yield ExportResult()

And from a factory, you check the env var

def create_exporter_metrics(signal, endpoint, meter_provider):
    if not parse_boolean_environment_variable(
        OTEL_PYTHON_SDK_INTERNAL_METRICS_ENABLED,
        default=False,
    ):
        return NoOpExporterMetrics()

    return ExporterMetrics(signal, endpoint, meter_provider)

I'm ok with the env var name 👍🏻

@herin049
Copy link
Copy Markdown
Contributor Author

herin049 commented May 3, 2026

emdneto

Cool. I was implementing this and just saw your PR today. @herin049 wdyt to instead use the concept of a NoOpExporterMetrics? This way, we also avoid creating the instruments.

Something like:

class NoOpExporterMetrics:
    @contextmanager
    def export_operation(self, num_items: int) -> Iterator[ExportResult]:
        yield ExportResult()

And from a factory, you check the env var

def create_exporter_metrics(signal, endpoint, meter_provider):
    if not parse_boolean_environment_variable(
        OTEL_PYTHON_SDK_INTERNAL_METRICS_ENABLED,
        default=False,
    ):
        return NoOpExporterMetrics()

    return ExporterMetrics(signal, endpoint, meter_provider)

I'm ok with the env var name 👍🏻

Updated the PR based on the suggestions. I opted to keep environment variable parsing outside of the factory method to keep configuration separate from construction (and to prevent having to add opentelemetry-sdk to the proto package).

@herin049 herin049 requested a review from emdneto May 3, 2026 04:58
OTEL_EXPORTER_OTLP_TIMEOUT,
OTEL_PYTHON_SDK_INTERNAL_METRICS_ENABLED,
)
from opentelemetry.sdk.environment_variables._internal import (
Copy link
Copy Markdown
Contributor

@xrmx xrmx May 4, 2026

Choose a reason for hiding this comment

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

The exporters does not depend on latest sdk version so either we bump the baseline or we add an helper in otlp.proto.common and live with the duplication.
We should probably also write this relationship somewhere in text or in tests against the baseline and not only latest sdk, not in this PR but just thinking out of loud.

Copy link
Copy Markdown
Contributor

@xrmx xrmx May 4, 2026

Choose a reason for hiding this comment

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

Actually it's old exporter with new sdk so something more hard to test. But yeah the point is that if the parse_boolean_environment_variable symbol go away in a future sdk we break the contract.

Copy link
Copy Markdown
Contributor Author

@herin049 herin049 May 4, 2026

Choose a reason for hiding this comment

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

The same issue applies to all of the other imports from the SDK that the exporters use. For example, we use the experimental _OTEL_PYTHON_EXPORTER_OTLP_GRPC_CREDENTIAL_PROVIDER from opentelemetry.sdk.environment_variables, so technically we can never remove _OTEL_PYTHON_EXPORTER_OTLP_GRPC_CREDENTIAL_PROVIDER anymore from the SDK without breaking older versions of the HTTP exporter. While not ideal, I'd be more in favor of pinning the version of the SDK that exporters depend on to avoid potential scenarios like these in the future. Perhaps something to bring up in the SIG to get some other opinions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approve Public API check This label shows that the public symbols added or changed in a PR are strictly necessary

Projects

Status: Approved PRs

Development

Successfully merging this pull request may close these issues.

5 participants