Skip to content

Commit

Permalink
Use user URL if validation returns empty endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanielRN committed Jul 28, 2021
1 parent fd2410c commit b7bc7df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
15 changes: 10 additions & 5 deletions packages/opentelemetry-exporter-collector-grpc/src/util.ts
Expand Up @@ -108,11 +108,15 @@ export function send<ExportItem, ServiceRequest>(
}

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.'
Expand All @@ -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
}
16 changes: 9 additions & 7 deletions packages/opentelemetry-exporter-collector-grpc/test/util.test.ts
Expand Up @@ -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',
Expand Down

0 comments on commit b7bc7df

Please sign in to comment.