Skip to content

Commit

Permalink
refactor: reduce and simplify usage of getEnv() (open-telemetry#4799)
Browse files Browse the repository at this point in the history
* refactor: reduce and simplify usage of getEnv()

* docs: update CHANGELOG

* fixup! docs: update CHANGELOG

---------

Co-authored-by: Marc Pichler <[email protected]>
  • Loading branch information
2 people authored and Zirak committed Sep 14, 2024
1 parent 45bc51a commit d54d3d1
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 128 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/

### :house: (Internal)

* refactor: Simplify the code for the `getEnv` function [#4799](https://github.com/open-telemetry/opentelemetry-js/pull/4799) @danstarns

## 1.25.1

### :books: (Refine Doc)
Expand Down
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ All notable changes to experimental packages in this project will be documented

### :house: (Internal)

* refactor: Simplify the code for the `getEnv` function [#4799](https://github.com/open-telemetry/opentelemetry-js/pull/4799) @danstarns

## 0.52.1

### :rocket: (Enhancement)
Expand Down
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

0 comments on commit d54d3d1

Please sign in to comment.