Skip to content
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

refactor: reduce and simplify usage of getEnv() #4799

Merged
merged 4 commits into from
Jun 25, 2024

Conversation

danstarns
Copy link
Contributor

@danstarns danstarns commented Jun 15, 2024

In most of the exporters, there is a function getEnv() that is called an unnecessary amount of times inside nested ternary operators.

It looks as getEnv() calls parseEnvironment() that happens to iterate over a large static object parsing its values and performing other assertions.

See extract from source:

  getDefaultUrl(config: OTLPExporterNodeConfigBase): string {
    return typeof config.url === 'string'
      ? config.url
      : getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0
      ? appendRootPathToUrlIfNeeded(getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT)
      : getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0
      ? appendResourcePathToUrl(
          getEnv().OTEL_EXPORTER_OTLP_ENDPOINT,
          DEFAULT_COLLECTOR_RESOURCE_PATH
        )
      : DEFAULT_COLLECTOR_URL;
  }

This PR reduces the total invocations of getEnv along the exporters in the hope of easing performance and readability.

@danstarns danstarns requested a review from a team June 15, 2024 21:17
Copy link
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

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

Thanks. This is more readable now 👍

FYI performance-wise this would not change a lot since all the changed functions are not called on a hot path (only once on start-up).

@pichlermarc
Copy link
Member

Please also add a changelog entry to the unrelased section, subsection enhancement in CHANGELOG.md and experimental/CHANGELOG.md then we can merge this. 🙂

@danstarns
Copy link
Contributor Author

Please also add a changelog entry to the unrelased section, subsection enhancement in CHANGELOG.md and experimental/CHANGELOG.md then we can merge this. 🙂

Updated @pichlermarc 👍

@pichlermarc pichlermarc added this pull request to the merge queue Jun 25, 2024
Merged via the queue into open-telemetry:main with commit 3fdadd5 Jun 25, 2024
18 checks passed
Zirak pushed a commit to Zirak/opentelemetry-js that referenced this pull request Sep 14, 2024
* refactor: reduce and simplify usage of getEnv()

* docs: update CHANGELOG

* fixup! docs: update CHANGELOG

---------

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants