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

Use user URL if validation returns empty endpoint #2382

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}
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