From b7bc7df0471b92dfa569e8d3fad0e5eecb842559 Mon Sep 17 00:00:00 2001 From: "(Eliseo) Nathaniel Ruiz Nowell" Date: Wed, 28 Jul 2021 11:26:55 -0700 Subject: [PATCH] Use user URL if validation returns empty endpoint --- .../src/util.ts | 15 ++++++++++----- .../test/util.test.ts | 16 +++++++++------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/packages/opentelemetry-exporter-collector-grpc/src/util.ts b/packages/opentelemetry-exporter-collector-grpc/src/util.ts index 299a6e7e81..e48add5e08 100644 --- a/packages/opentelemetry-exporter-collector-grpc/src/util.ts +++ b/packages/opentelemetry-exporter-collector-grpc/src/util.ts @@ -108,11 +108,15 @@ export function send( } export function validateAndNormalizeUrl(url: string): string { - const hasProtocol = url.match(/^([\w]{1,8}):\/\//); - if (!hasProtocol) { - 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 +127,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 ba08009cd5..18a68d4888 100644 --- a/packages/opentelemetry-exporter-collector-grpc/test/util.test.ts +++ b/packages/opentelemetry-exporter-collector-grpc/test/util.test.ts @@ -24,19 +24,21 @@ import { validateAndNormalizeUrl } from '../src/util'; describe('validateAndNormalizeUrl()', () => { const tests = [ { - name: 'bare hostname should return same value', + name: 'grpc://host:port should trim off protocol', + input: 'grpc://api.datacat.io:1234', + expected: 'api.datacat.io:1234', + }, + { + name: 'bare hostname should warn but return same value', input: 'api.datacat.io', expected: 'api.datacat.io', + warn: 'Parsing URL failed to return a valid host. Returning raw url.', }, { - name: 'host:port should return same value', + name: 'host:port should warn but return same value', input: 'api.datacat.io:1234', expected: 'api.datacat.io:1234', - }, - { - name: 'grpc://host:port should trim off protocol', - input: 'grpc://api.datacat.io:1234', - expected: 'api.datacat.io:1234', + warn: 'Parsing URL failed to return a valid host. Returning raw url.', }, { name: 'bad protocol should warn but return host:port',