Skip to content

refactor(otlp-grpc-exporter-base): use getStringFromEnv instead of process.env #5595

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2

### :house: Internal

* 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
* 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

## 0.200.0

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