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
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ export const DEFAULT_COLLECTOR_URL = `http://localhost:4318/${DEFAULT_COLLECTOR_
* @returns url string
*/
export function getDefaultUrl(config: OTLPExporterConfigBase): string {
return typeof config.url === 'string'
? config.url
: getEnv().OTEL_EXPORTER_OTLP_LOGS_ENDPOINT.length > 0
? appendRootPathToUrlIfNeeded(getEnv().OTEL_EXPORTER_OTLP_LOGS_ENDPOINT)
: getEnv().OTEL_EXPORTER_OTLP_ENDPOINT.length > 0
? appendResourcePathToUrl(
getEnv().OTEL_EXPORTER_OTLP_ENDPOINT,
DEFAULT_COLLECTOR_RESOURCE_PATH
)
: DEFAULT_COLLECTOR_URL;
if (typeof config.url === 'string') {
return config.url;
}

const env = getEnv();
if (env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT.length > 0) {
return appendRootPathToUrlIfNeeded(env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT);
}

if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) {
return appendResourcePathToUrl(
env.OTEL_EXPORTER_OTLP_ENDPOINT,
DEFAULT_COLLECTOR_RESOURCE_PATH
);
}

return DEFAULT_COLLECTOR_URL;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,30 @@ export class OTLPLogExporter
{
constructor(config: OTLPExporterConfigBase = {}) {
super(config, ProtobufLogsSerializer, 'application/x-protobuf');
const env = getEnv();
this._headers = Object.assign(
this._headers,
baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_LOGS_HEADERS
)
baggageUtils.parseKeyPairsIntoRecord(env.OTEL_EXPORTER_OTLP_LOGS_HEADERS)
);
}

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

const env = getEnv();
if (env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT.length > 0) {
return appendRootPathToUrlIfNeeded(env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT);
}

if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) {
return appendResourcePathToUrl(
env.OTEL_EXPORTER_OTLP_ENDPOINT,
DEFAULT_COLLECTOR_RESOURCE_PATH
);
}

return DEFAULT_COLLECTOR_URL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,34 @@ export class OTLPLogExporter
{
constructor(config: OTLPExporterConfigBase = {}) {
super(config, ProtobufLogsSerializer, 'application/x-protobuf');
const env = getEnv();
this.headers = {
...this.headers,
...USER_AGENT,
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_LOGS_HEADERS
env.OTEL_EXPORTER_OTLP_LOGS_HEADERS
),
...parseHeaders(config?.headers),
};
}

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

const env = getEnv();
if (env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT.length > 0) {
return appendRootPathToUrlIfNeeded(env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT);
}

if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) {
return appendResourcePathToUrl(
env.OTEL_EXPORTER_OTLP_ENDPOINT,
DEFAULT_COLLECTOR_RESOURCE_PATH
);
}

return DEFAULT_COLLECTOR_URL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,34 @@ export class OTLPTraceExporter
{
constructor(config: OTLPExporterConfigBase = {}) {
super(config, JsonTraceSerializer, 'application/json');
const env = getEnv();
this._headers = Object.assign(
this._headers,
baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS
env.OTEL_EXPORTER_OTLP_TRACES_HEADERS
)
);
}

getDefaultUrl(config: OTLPExporterConfigBase): 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;
if (typeof config.url === 'string') {
return config.url;
}

const env = getEnv();
if (env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0) {
return appendRootPathToUrlIfNeeded(
env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
);
}

if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) {
return appendResourcePathToUrl(
env.OTEL_EXPORTER_OTLP_ENDPOINT,
DEFAULT_COLLECTOR_RESOURCE_PATH
);
}

return DEFAULT_COLLECTOR_URL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,36 @@ export class OTLPTraceExporter
{
constructor(config: OTLPExporterNodeConfigBase = {}) {
super(config, JsonTraceSerializer, 'application/json');
const env = getEnv();
this.headers = {
...this.headers,
...USER_AGENT,
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS
env.OTEL_EXPORTER_OTLP_TRACES_HEADERS
),
...parseHeaders(config?.headers),
};
}

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;
if (typeof config.url === 'string') {
return config.url;
}

const env = getEnv();
if (env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0) {
return appendRootPathToUrlIfNeeded(
env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
);
}

if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) {
return appendResourcePathToUrl(
env.OTEL_EXPORTER_OTLP_ENDPOINT,
DEFAULT_COLLECTOR_RESOURCE_PATH
);
}

return DEFAULT_COLLECTOR_URL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,34 @@ export class OTLPTraceExporter
{
constructor(config: OTLPExporterConfigBase = {}) {
super(config, ProtobufTraceSerializer, 'application/x-protobuf');
const env = getEnv();
this._headers = Object.assign(
this._headers,
baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS
env.OTEL_EXPORTER_OTLP_TRACES_HEADERS
)
);
}

getDefaultUrl(config: OTLPExporterConfigBase): 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;
if (typeof config.url === 'string') {
return config.url;
}

const env = getEnv();
if (env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0) {
return appendRootPathToUrlIfNeeded(
env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
);
}

if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) {
return appendResourcePathToUrl(
env.OTEL_EXPORTER_OTLP_ENDPOINT,
DEFAULT_COLLECTOR_RESOURCE_PATH
);
}

return DEFAULT_COLLECTOR_URL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,36 @@ export class OTLPTraceExporter
{
constructor(config: OTLPExporterNodeConfigBase = {}) {
super(config, ProtobufTraceSerializer, 'application/x-protobuf');
const env = getEnv();
this.headers = {
...this.headers,
...USER_AGENT,
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS
env.OTEL_EXPORTER_OTLP_TRACES_HEADERS
),
...parseHeaders(config?.headers),
};
}

getDefaultUrl(config: OTLPExporterNodeConfigBase) {
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;
if (typeof config.url === 'string') {
return config.url;
}

const env = getEnv();
if (env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0) {
return appendRootPathToUrlIfNeeded(
env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
);
}

if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) {
return appendResourcePathToUrl(
env.OTEL_EXPORTER_OTLP_ENDPOINT,
DEFAULT_COLLECTOR_RESOURCE_PATH
);
}

return DEFAULT_COLLECTOR_URL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,35 @@ class OTLPExporterBrowserProxy extends OTLPExporterBrowserBase<
> {
constructor(config?: OTLPMetricExporterOptions & OTLPExporterConfigBase) {
super(config, JsonMetricsSerializer, 'application/json');
const env = getEnv();
this._headers = Object.assign(
this._headers,
baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS
env.OTEL_EXPORTER_OTLP_METRICS_HEADERS
)
);
}

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

const env = getEnv();
if (env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT.length > 0) {
return appendRootPathToUrlIfNeeded(
env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT
);
}

if (env.OTEL_EXPORTER_OTLP_ENDPOINT.length > 0) {
return appendResourcePathToUrl(
env.OTEL_EXPORTER_OTLP_ENDPOINT,
DEFAULT_COLLECTOR_RESOURCE_PATH
);
}

return DEFAULT_COLLECTOR_URL;
}
}

Expand Down
Loading
Loading