-
Notifications
You must be signed in to change notification settings - Fork 884
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
1216ea4
d4ee7ce
f73fa53
3e1c9b8
9510279
e8a966e
abfb0d0
150fe77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -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() | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -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 ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
fallbackIfNullishOrBlank( | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, |
||
signalSpecificPath, | ||
|
There was a problem hiding this comment.
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):i.e.
.trim()
andfallbackIfNullishOrBlank
should no longer be necessary.