Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2

### :house: Internal

* refactor(exporter-otlp-grpc): use getStringFromEnv instead of process.env [#5595](https://github.com/open-telemetry/opentelemetry-js/pull/5595) @weyert
* refactor(sdk-node): Refactored using `get*FromEnv` utility function instead of `process.env` for NodeSDK's logging setup. [#5563](https://github.com/open-telemetry/opentelemetry-js/issues/5563) @weyert
* chore(sdk-node): Refactored using `get*FromEnv` utility function instead of `process.env` for NodeSDK's resource detector setup. [#5582](https://github.com/open-telemetry/opentelemetry-js/pull/5582) @beeme1mr
* chore(sdk-node): Refactored using `get*FromEnv` utility function instead of `process.env` for NodeSDK's logging setup. [#5563](https://github.com/open-telemetry/opentelemetry-js/issues/5563) @weyert

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import { UnresolvedOtlpGrpcConfiguration } from './otlp-grpc-configuration';
import type { ChannelCredentials, Metadata } from '@grpc/grpc-js';
import { parseKeyPairsIntoRecord } from '@opentelemetry/core';
import { getStringFromEnv, parseKeyPairsIntoRecord } from '@opentelemetry/core';
import {
createEmptyMetadata,
createInsecureCredentials,
Expand All @@ -42,10 +42,12 @@ function fallbackIfNullishOrBlank(
}

function getMetadataFromEnv(signalIdentifier: string): Metadata | undefined {
const signalSpecificRawHeaders =
process.env[`OTEL_EXPORTER_OTLP_${signalIdentifier}_HEADERS`]?.trim();
const nonSignalSpecificRawHeaders =
process.env['OTEL_EXPORTER_OTLP_HEADERS']?.trim();
const signalSpecificRawHeaders = getStringFromEnv(
`OTEL_EXPORTER_OTLP_${signalIdentifier}_HEADERS`
);
const nonSignalSpecificRawHeaders = getStringFromEnv(
'OTEL_EXPORTER_OTLP_HEADERS'
);

const signalSpecificHeaders = parseKeyPairsIntoRecord(
signalSpecificRawHeaders
Expand Down Expand Up @@ -99,12 +101,15 @@ function getUrlFromEnv(signalIdentifier: string) {
// - http://example.test:4317 -> use insecure credentials if nothing else is provided
// - https://example.test:4317 -> use secure credentials from environment (or provided via code)

const specificEndpoint =
process.env[`OTEL_EXPORTER_OTLP_${signalIdentifier}_ENDPOINT`]?.trim();
const nonSpecificEndpoint =
process.env[`OTEL_EXPORTER_OTLP_ENDPOINT`]?.trim();
const specificEndpoint = getStringFromEnv(
`OTEL_EXPORTER_OTLP_${signalIdentifier}_ENDPOINT`
);
const nonSpecificEndpoint = getStringFromEnv(`OTEL_EXPORTER_OTLP_ENDPOINT`);

return fallbackIfNullishOrBlank(specificEndpoint, nonSpecificEndpoint);
return fallbackIfNullishOrBlank(
specificEndpoint?.trim(),
nonSpecificEndpoint?.trim()
);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this whole block could be simplified to (untested, and needs npm run lint:fix for formatting):

return getStringFromEnv(`OTEL_EXPORTER_OTLP_${signalIdentifier}_ENDPOINT`) ?? getStringFromEnv('OTEL_EXPORTER_OTLP_ENDPOINT');

i.e. .trim() and fallbackIfNullishOrBlank should no longer be necessary.

}

/**
Expand All @@ -128,16 +133,12 @@ function getUrlFromEnv(signalIdentifier: string) {
* @param signalIdentifier
*/
function getInsecureSettingFromEnv(signalIdentifier: string): boolean {
const signalSpecificInsecureValue = process.env[
const signalSpecificInsecureValue = getStringFromEnv(
`OTEL_EXPORTER_OTLP_${signalIdentifier}_INSECURE`
]
?.toLowerCase()
.trim();
const nonSignalSpecificInsecureValue = process.env[
)?.toLowerCase();
const nonSignalSpecificInsecureValue = getStringFromEnv(
`OTEL_EXPORTER_OTLP_INSECURE`
]
?.toLowerCase()
.trim();
)?.toLowerCase();

return (
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this whole function could be simplified to (untested):

// Use `getStringFromEnv` because any value, even if invalid, in the signal-specific envvar should win.
return (getStringFromEnv(`OTEL_EXPORTER_OTLP_${signalIdentifier}_INSECURE`)
  ? getBooleanFromEnv(`OTEL_EXPORTER_OTLP_${signalIdentifier}_INSECURE`)
  : getBooleanFromEnv('OTEL_EXPORTER_OTLP_INSECURE')
);

And the long comment on block starting with It will allow the following values as {@code true} can be removed.

fallbackIfNullishOrBlank(
Expand All @@ -152,8 +153,8 @@ function readFileFromEnv(
nonSignalSpecificEnvVar: string,
warningMessage: string
): Buffer | undefined {
const signalSpecificPath = process.env[signalSpecificEnvVar]?.trim();
const nonSignalSpecificPath = process.env[nonSignalSpecificEnvVar]?.trim();
const signalSpecificPath = getStringFromEnv(signalSpecificEnvVar);
const nonSignalSpecificPath = getStringFromEnv(nonSignalSpecificEnvVar);

const filePath = fallbackIfNullishOrBlank(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this could be simplified to (untested):

const filePath = getStringFromEnv(signalSpecificEnvVar) ?? getStringFromEnv(nonSignalSpecificEnvVar);

Then, with this suggestion and those above, fallbackIfNullishOrBlank is now unused in this file and can be removed.

signalSpecificPath,
Expand Down