Skip to content

Commit

Permalink
Merge branch 'main' into rauno
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud authored Jul 22, 2021
2 parents 3165e8b + 39c6ea5 commit 2f04748
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/basic-tracer-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"url": "https://github.com/open-telemetry/opentelemetry-js/issues"
},
"dependencies": {
"@opentelemetry/api": "^1.0.0",
"@opentelemetry/api": "^1.0.1",
"@opentelemetry/exporter-jaeger": "0.23.0",
"@opentelemetry/tracing": "0.23.0"
},
Expand Down
4 changes: 2 additions & 2 deletions examples/collector-exporter-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ npm install

```shell script
# from this directory
npm start:tracing
npm run start:tracing
```

3. Run metrics app

```shell script
# from this directory
npm start:metrics
npm run start:metrics
```

4. Open page at <http://localhost:9411/zipkin/> - you should be able to see the spans in zipkin
Expand Down
2 changes: 1 addition & 1 deletion examples/collector-exporter-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"url": "https://github.com/open-telemetry/opentelemetry-js/issues"
},
"dependencies": {
"@opentelemetry/api": "^1.0.0",
"@opentelemetry/api": "^1.0.1",
"@opentelemetry/core": "0.23.0",
"@opentelemetry/exporter-collector": "0.23.0",
"@opentelemetry/exporter-collector-grpc": "0.23.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/grpc-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"dependencies": {
"@grpc/grpc-js": "^1.2.12",
"@opentelemetry/api": "^1.0.0",
"@opentelemetry/api": "^1.0.1",
"@opentelemetry/exporter-jaeger": "0.23.0",
"@opentelemetry/exporter-zipkin": "0.23.0",
"@opentelemetry/instrumentation": "0.23.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/grpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"url": "https://github.com/open-telemetry/opentelemetry-js/issues"
},
"dependencies": {
"@opentelemetry/api": "^1.0.0",
"@opentelemetry/api": "^1.0.1",
"@opentelemetry/exporter-jaeger": "0.23.0",
"@opentelemetry/exporter-zipkin": "0.23.0",
"@opentelemetry/instrumentation": "0.23.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"url": "https://github.com/open-telemetry/opentelemetry-js/issues"
},
"dependencies": {
"@opentelemetry/api": "^1.0.0",
"@opentelemetry/api": "^1.0.1",
"@opentelemetry/exporter-jaeger": "0.23.0",
"@opentelemetry/exporter-zipkin": "0.23.0",
"@opentelemetry/instrumentation": "0.23.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/https/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"url": "https://github.com/open-telemetry/opentelemetry-js/issues"
},
"dependencies": {
"@opentelemetry/api": "^1.0.0",
"@opentelemetry/api": "^1.0.1",
"@opentelemetry/exporter-jaeger": "0.23.0",
"@opentelemetry/exporter-zipkin": "0.23.0",
"@opentelemetry/instrumentation": "0.23.0",
Expand Down
1 change: 1 addition & 0 deletions examples/metrics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"url": "https://github.com/open-telemetry/opentelemetry-js/issues"
},
"dependencies": {
"@opentelemetry/api": "^1.0.1",
"@opentelemetry/core": "0.23.0",
"@opentelemetry/exporter-prometheus": "0.23.0",
"@opentelemetry/metrics": "0.23.0"
Expand Down
1 change: 1 addition & 0 deletions examples/opentracing-shim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"url": "https://github.com/open-telemetry/opentelemetry-js/issues"
},
"dependencies": {
"@opentelemetry/api": "^1.0.1",
"@opentelemetry/exporter-jaeger": "0.23.0",
"@opentelemetry/exporter-zipkin": "0.23.0",
"@opentelemetry/instrumentation": "0.23.0",
Expand Down
1 change: 1 addition & 0 deletions examples/prometheus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"author": "OpenTelemetry Authors",
"license": "Apache-2.0",
"dependencies": {
"@opentelemetry/api": "^1.0.1",
"@opentelemetry/exporter-prometheus": "0.23.0",
"@opentelemetry/metrics": "0.23.0"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/tracer-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"webpack-merge": "^4.2.2"
},
"dependencies": {
"@opentelemetry/api": "^1.0.0",
"@opentelemetry/api": "^1.0.1",
"@opentelemetry/context-zone": "0.23.0",
"@opentelemetry/core": "0.23.0",
"@opentelemetry/exporter-collector": "0.23.0",
Expand Down
6 changes: 5 additions & 1 deletion packages/opentelemetry-exporter-collector-grpc/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,12 @@ 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.pathname !== '/') {
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 Down
81 changes: 81 additions & 0 deletions packages/opentelemetry-exporter-collector-grpc/test/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as sinon from 'sinon';
import * as assert from 'assert';

import { diag } from '@opentelemetry/api';
import { validateAndNormalizeUrl } from '../src/util';

// Tests added to detect breakage released in #2130
describe('validateAndNormalizeUrl()', () => {
const tests = [
{
name: 'bare hostname should return same value',
input: 'api.datacat.io',
expected: 'api.datacat.io',
},
{
name: 'host:port should 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',
},
{
name: 'bad protocol should warn but return host:port',
input: 'badproto://api.datacat.io:1234',
expected: 'api.datacat.io:1234',
warn: 'URL protocol should be http(s):// or grpc(s)://. Using grpc://.',
},
{
name: 'path on end of url should warn but return host:port',
input: 'grpc://api.datacat.io:1234/a/b/c',
expected: 'api.datacat.io:1234',
warn: 'URL path should not be set when using grpc, the path part of the URL will be ignored.',
},
{
name: ':// in path should not be used for protocol even if protocol not specified',
input: 'api.datacat.io/a/b://c',
expected: 'api.datacat.io',
warn: 'URL path should not be set when using grpc, the path part of the URL will be ignored.',
},
{
name: ':// in path is valid when a protocol is specified',
input: 'grpc://api.datacat.io/a/b://c',
expected: 'api.datacat.io',
warn: 'URL path should not be set when using grpc, the path part of the URL will be ignored.',
},
];
tests.forEach(test => {
it(test.name, () => {
const diagWarn = sinon.stub(diag, 'warn');
try {
assert.strictEqual(validateAndNormalizeUrl(test.input), (test.expected));
if (test.warn) {
sinon.assert.calledWith(diagWarn, test.warn);
} else {
sinon.assert.notCalled(diagWarn);
}
} finally {
diagWarn.restore();
}
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,16 @@ export function sendWithXhr(
) {
const xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('Content-Type', 'application/json');
Object.entries(headers).forEach(([k, v]) => {

const defaultHeaders = {
'Accept': 'application/json',
'Content-Type': 'application/json',
};

Object.entries({
...defaultHeaders,
...headers,
}).forEach(([k, v]) => {
xhr.setRequestHeader(k, v);
});

Expand Down
128 changes: 128 additions & 0 deletions packages/opentelemetry-exporter-collector/test/browser/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as sinon from 'sinon';
import { sendWithXhr } from '../../src/platform/browser/util';
import { ensureHeadersContain } from '../helper';

describe('util - browser', () => {
let server: any;
const body = '';
const url = '';

let onSuccessStub: sinon.SinonStub;
let onErrorStub: sinon.SinonStub;

beforeEach(() => {
onSuccessStub = sinon.stub();
onErrorStub = sinon.stub();
server = sinon.fakeServer.create();
});

afterEach(() => {
server.restore();
sinon.restore();
});

describe('when XMLHTTPRequest is used', () => {
let expectedHeaders: Record<string,string>;
beforeEach(()=>{
expectedHeaders = {
// ;charset=utf-8 is applied by sinon.fakeServer
'Content-Type': 'application/json;charset=utf-8',
'Accept': 'application/json',
}
});
describe('and Content-Type header is set', () => {
beforeEach(()=>{
const explicitContentType = {
'Content-Type': 'application/json',
};
sendWithXhr(body, url, explicitContentType, onSuccessStub, onErrorStub);
});
it('Request Headers should contain "Content-Type" header', done => {

setTimeout(() => {
const { requestHeaders } = server.requests[0];
ensureHeadersContain(requestHeaders, expectedHeaders);
done();
});
});
it('Request Headers should contain "Accept" header', done => {

setTimeout(() => {
const { requestHeaders } = server.requests[0];
ensureHeadersContain(requestHeaders, expectedHeaders);
done();
});
});
});

describe('and empty headers are set', () => {
beforeEach(()=>{
const emptyHeaders = {};
sendWithXhr(body, url, emptyHeaders, onSuccessStub, onErrorStub);
});
it('Request Headers should contain "Content-Type" header', done => {

setTimeout(() => {
const { requestHeaders } = server.requests[0];
ensureHeadersContain(requestHeaders, expectedHeaders);
done();
});
});
it('Request Headers should contain "Accept" header', done => {

setTimeout(() => {
const { requestHeaders } = server.requests[0];
ensureHeadersContain(requestHeaders, expectedHeaders);
done();
});
});
});
describe('and custom headers are set', () => {
let customHeaders: Record<string,string>;
beforeEach(()=>{
customHeaders = { aHeader: 'aValue', bHeader: 'bValue' };
sendWithXhr(body, url, customHeaders, onSuccessStub, onErrorStub);
});
it('Request Headers should contain "Content-Type" header', done => {

setTimeout(() => {
const { requestHeaders } = server.requests[0];
ensureHeadersContain(requestHeaders, expectedHeaders);
done();
});
});
it('Request Headers should contain "Accept" header', done => {

setTimeout(() => {
const { requestHeaders } = server.requests[0];
ensureHeadersContain(requestHeaders, expectedHeaders);
done();
});
});
it('Request Headers should contain custom headers', done => {

setTimeout(() => {
const { requestHeaders } = server.requests[0];
ensureHeadersContain(requestHeaders, customHeaders);
done();
});
});
});
});
});

0 comments on commit 2f04748

Please sign in to comment.