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

fix(exporters): do not append trailing '/' when URL contains path #3274

Merged
Show file tree
Hide file tree
Changes from 3 commits
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
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ All notable changes to experimental packages in this project will be documented

### :bug: (Bug Fix)

* fix(exporters): do not append trailing '/' when URL contains path [#3274](https://github.com/open-telemetry/opentelemetry-js/pull/3274) @pichlermarc

### :books: (Refine Doc)

### :house: (Internal)
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)
vmarchaud marked this conversation as resolved.
Show resolved Hide resolved
? 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
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