diff --git a/packages/opentelemetry-exporter-collector-grpc/src/util.ts b/packages/opentelemetry-exporter-collector-grpc/src/util.ts index 299a6e7e819..282028cb444 100644 --- a/packages/opentelemetry-exporter-collector-grpc/src/util.ts +++ b/packages/opentelemetry-exporter-collector-grpc/src/util.ts @@ -113,6 +113,14 @@ export function validateAndNormalizeUrl(url: string): string { url = `https://${url}`; } const target = new URL(url); + + if (target.host === '') { + diag.warn( + 'Parsing URL failed to return a valid host. Returning raw url.' + ); + return url + } + if (target.pathname && target.pathname !== '/') { diag.warn( 'URL path should not be set when using grpc, the path part of the URL will be ignored.' @@ -123,5 +131,6 @@ export function validateAndNormalizeUrl(url: string): string { 'URL protocol should be http(s):// or grpc(s)://. Using grpc://.' ); } - return target.host; + + return target.host } diff --git a/packages/opentelemetry-exporter-collector-grpc/test/util.test.ts b/packages/opentelemetry-exporter-collector-grpc/test/util.test.ts index ba08009cd55..a3fbb3a6a21 100644 --- a/packages/opentelemetry-exporter-collector-grpc/test/util.test.ts +++ b/packages/opentelemetry-exporter-collector-grpc/test/util.test.ts @@ -62,6 +62,12 @@ describe('validateAndNormalizeUrl()', () => { expected: 'api.datacat.io', warn: 'URL path should not be set when using grpc, the path part of the URL will be ignored.', }, + { + name: 'missing scheme should fallback to provided url', + input: 'example-localhost:1234', + expected: 'example-localhost:1234', + warn: 'Parsing URL failed to return a valid host. Returning raw url.', + }, ]; tests.forEach(test => { it(test.name, () => {