Skip to content

Commit c55142f

Browse files
lizthegreynaseemkullah
andauthoredJul 21, 2021
fix(@opentelemetry/exporter-collector-grpc) regression from #2130 when host specified without protocol (#2322)
* fix regression from #2130 when host specified without protocol * Update util.test.ts * Apply suggestions from code review Co-authored-by: Naseem <naseemkullah@gmail.com> * revert package.json changes * Update util.ts * add tests as per @MSNev request * Update packages/opentelemetry-exporter-collector-grpc/src/util.ts Co-authored-by: Naseem <naseemkullah@gmail.com> Co-authored-by: Naseem <naseemkullah@gmail.com>
1 parent 11719ed commit c55142f

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed
 

‎packages/opentelemetry-exporter-collector-grpc/src/util.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,12 @@ export function send<ExportItem, ServiceRequest>(
108108
}
109109

110110
export function validateAndNormalizeUrl(url: string): string {
111+
const hasProtocol = url.match(/^([\w]{1,8}):\/\//);
112+
if (!hasProtocol) {
113+
url = `https://${url}`;
114+
}
111115
const target = new URL(url);
112-
if (target.pathname !== '/') {
116+
if (target.pathname && target.pathname !== '/') {
113117
diag.warn(
114118
'URL path should not be set when using grpc, the path part of the URL will be ignored.'
115119
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as sinon from 'sinon';
18+
import * as assert from 'assert';
19+
20+
import { diag } from '@opentelemetry/api';
21+
import { validateAndNormalizeUrl } from '../src/util';
22+
23+
// Tests added to detect breakage released in #2130
24+
describe('validateAndNormalizeUrl()', () => {
25+
const tests = [
26+
{
27+
name: 'bare hostname should return same value',
28+
input: 'api.datacat.io',
29+
expected: 'api.datacat.io',
30+
},
31+
{
32+
name: 'host:port should return same value',
33+
input: 'api.datacat.io:1234',
34+
expected: 'api.datacat.io:1234',
35+
},
36+
{
37+
name: 'grpc://host:port should trim off protocol',
38+
input: 'grpc://api.datacat.io:1234',
39+
expected: 'api.datacat.io:1234',
40+
},
41+
{
42+
name: 'bad protocol should warn but return host:port',
43+
input: 'badproto://api.datacat.io:1234',
44+
expected: 'api.datacat.io:1234',
45+
warn: 'URL protocol should be http(s):// or grpc(s)://. Using grpc://.',
46+
},
47+
{
48+
name: 'path on end of url should warn but return host:port',
49+
input: 'grpc://api.datacat.io:1234/a/b/c',
50+
expected: 'api.datacat.io:1234',
51+
warn: 'URL path should not be set when using grpc, the path part of the URL will be ignored.',
52+
},
53+
{
54+
name: ':// in path should not be used for protocol even if protocol not specified',
55+
input: 'api.datacat.io/a/b://c',
56+
expected: 'api.datacat.io',
57+
warn: 'URL path should not be set when using grpc, the path part of the URL will be ignored.',
58+
},
59+
{
60+
name: ':// in path is valid when a protocol is specified',
61+
input: 'grpc://api.datacat.io/a/b://c',
62+
expected: 'api.datacat.io',
63+
warn: 'URL path should not be set when using grpc, the path part of the URL will be ignored.',
64+
},
65+
];
66+
tests.forEach(test => {
67+
it(test.name, () => {
68+
const diagWarn = sinon.stub(diag, 'warn');
69+
try {
70+
assert.strictEqual(validateAndNormalizeUrl(test.input), (test.expected));
71+
if (test.warn) {
72+
sinon.assert.calledWith(diagWarn, test.warn);
73+
} else {
74+
sinon.assert.notCalled(diagWarn);
75+
}
76+
} finally {
77+
diagWarn.restore();
78+
}
79+
});
80+
});
81+
});

0 commit comments

Comments
 (0)
Please sign in to comment.