Skip to content

Commit

Permalink
fix(exporters): do not append trailing '/' when URL contains path (op…
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc authored Oct 5, 2022
1 parent e91cac5 commit 1864a9a
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 21 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to experimental packages in this project will be documented
### :bug: (Bug Fix)

* fix(node-sdk): move `@opentelemetry/semantic-conventions` to `dependencies` [#3283](https://github.com/open-telemetry/opentelemetry-js/pull/3283) @mhassan1
* fix(exporters): do not append trailing '/' when URL contains path [#3274](https://github.com/open-telemetry/opentelemetry-js/pull/3274) @pichlermarc

### :books: (Refine Doc)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class OTLPTraceExporter
return typeof config.url === 'string'
? config.url
: getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0
? appendRootPathToUrlIfNeeded(getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, DEFAULT_COLLECTOR_RESOURCE_PATH)
? 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class OTLPTraceExporter
return typeof config.url === 'string'
? config.url
: getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0
? appendRootPathToUrlIfNeeded(getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, DEFAULT_COLLECTOR_RESOURCE_PATH)
? 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class OTLPTraceExporter
return typeof config.url === 'string'
? config.url
: getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT.length > 0
? appendRootPathToUrlIfNeeded(getEnv().OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, DEFAULT_COLLECTOR_RESOURCE_PATH)
? 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class OTLPExporterBrowserProxy extends OTLPExporterBrowserBase<ResourceMetrics,
return typeof config.url === 'string'
? config.url
: getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT.length > 0
? appendRootPathToUrlIfNeeded(getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT, DEFAULT_COLLECTOR_RESOURCE_PATH)
? 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class OTLPExporterNodeProxy extends OTLPExporterNodeBase<ResourceMetrics, IExpor
return typeof config.url === 'string'
? config.url
: getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT.length > 0
? appendRootPathToUrlIfNeeded(getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT, DEFAULT_COLLECTOR_RESOURCE_PATH)
? 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class OTLPMetricExporterNodeProxy extends OTLPProtoExporterNodeBase<ResourceMetr
return typeof config.url === 'string'
? config.url
: getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT.length > 0
? appendRootPathToUrlIfNeeded(getEnv().OTEL_EXPORTER_OTLP_METRICS_ENDPOINT, DEFAULT_COLLECTOR_RESOURCE_PATH)
? 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;
Expand Down
17 changes: 11 additions & 6 deletions experimental/packages/otlp-exporter-base/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,19 @@ export function appendResourcePathToUrl(url: string, path: string): string {
/**
* Adds root path to signal specific endpoint when endpoint contains no path part and no root path
* @param url
* @param path
* @returns url
*/
export function appendRootPathToUrlIfNeeded(url: string, path: string): string {
if (!url.includes(path) && !url.endsWith('/')) {
url = url + '/';
export function appendRootPathToUrlIfNeeded(url: string): string {
try {
const parsedUrl = new URL(url);
if (parsedUrl.pathname === '') {
parsedUrl.pathname = parsedUrl.pathname + '/';
}
return parsedUrl.toString();
} catch {
diag.warn(`Could not parse export URL: '${url}'`);
return url;
}
return url;
}

/**
Expand All @@ -83,7 +88,7 @@ export function configureExporterTimeout(timeoutMillis: number | undefined): num
function getExporterTimeoutFromEnv(): number {
const definedTimeout =
Number(getEnv().OTEL_EXPORTER_OTLP_TRACES_TIMEOUT ??
getEnv().OTEL_EXPORTER_OTLP_TIMEOUT);
getEnv().OTEL_EXPORTER_OTLP_TIMEOUT);

if (definedTimeout <= 0) {
// OTLP exporter configured timeout - using default value of 10000ms
Expand Down
30 changes: 21 additions & 9 deletions experimental/packages/otlp-exporter-base/test/common/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { diag } from '@opentelemetry/api';
import {
parseHeaders,
appendResourcePathToUrl,
appendRootPathToUrlIfNeeded
appendRootPathToUrlIfNeeded,
} from '../../src/util';

describe('utils', () => {
Expand Down Expand Up @@ -84,23 +84,35 @@ describe('utils', () => {
describe('appendRootPathToUrlIfNeeded - specifc signal http endpoint', () => {
it('should append root path when missing', () => {
const url = 'http://foo.bar';
const resourcePath = 'v1/traces';

const finalUrl = appendRootPathToUrlIfNeeded(url, resourcePath);
const finalUrl = appendRootPathToUrlIfNeeded(url);
assert.strictEqual(finalUrl, url + '/');
});
it('should not append root path and return same url', () => {
const url = 'http://foo.bar/';
const resourcePath = 'v1/traces';

const finalUrl = appendRootPathToUrlIfNeeded(url, resourcePath);
const finalUrl = appendRootPathToUrlIfNeeded(url);
assert.strictEqual(finalUrl, url);
});
it('should append root path when url contains resource path', () => {
const url = 'http://foo.bar/v1/traces';
const resourcePath = 'v1/traces';
it('should not append root path when url contains resource path', () => {
{
const url = 'http://foo.bar/v1/traces';

const finalUrl = appendRootPathToUrlIfNeeded(url);
assert.strictEqual(finalUrl, url);
}
{
const url = 'https://endpoint/something';

const finalUrl = appendRootPathToUrlIfNeeded(url);
assert.strictEqual(finalUrl, url);
}
});

it('should not change string when url is not parseable', () => {
const url = 'this is not a URL';

const finalUrl = appendRootPathToUrlIfNeeded(url, resourcePath);
const finalUrl = appendRootPathToUrlIfNeeded(url);
assert.strictEqual(finalUrl, url);
});
});
Expand Down

0 comments on commit 1864a9a

Please sign in to comment.