Skip to content

Commit

Permalink
test(plugin-http): increase coverage (open-telemetry#371)
Browse files Browse the repository at this point in the history
closes open-telemetry#370

Signed-off-by: Olivier Albertini <[email protected]>
  • Loading branch information
OlivierAlbertini authored and danielkhan committed Sep 29, 2019
1 parent 0c34096 commit 7005026
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ describe('HttpPlugin', () => {
nock.enableNetConnect();

plugin.enable(http, tracer, tracer.logger);
// Ensure that http module is patched.
assert.strictEqual(http.Server.prototype.emit.__wrapped, true);
server = http.createServer((request, response) => {
response.end('Test Server Response');
});
Expand Down Expand Up @@ -76,6 +78,8 @@ describe('HttpPlugin', () => {
(tracer.startSpan as sinon.SinonSpy).called,
false
);

assert.strictEqual(http.Server.prototype.emit.__wrapped, undefined);
assert.strictEqual((tracer.withSpan as sinon.SinonSpy).called, false);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/basic-tracer';
import { HttpPluginConfig } from '../../src';
import { Utils } from '../../src/utils';
import { HttpPluginConfig, Http } from '../../src/types';

let server: http.Server;
const serverPort = 22345;
Expand Down Expand Up @@ -82,18 +82,20 @@ describe('HttpPlugin', () => {
});

before(() => {
const ignoreConfig = [
`http://${hostname}/ignored/string`,
/\/ignored\/regexp$/i,
(url: string) => url.endsWith(`/ignored/function`),
];
const config: HttpPluginConfig = {
ignoreIncomingPaths: ignoreConfig,
ignoreOutgoingUrls: ignoreConfig,
ignoreIncomingPaths: [
`/ignored/string`,
/\/ignored\/regexp$/i,
(url: string) => url.endsWith(`/ignored/function`),
],
ignoreOutgoingUrls: [
`http://${hostname}:${serverPort}/ignored/string`,
/\/ignored\/regexp$/i,
(url: string) => url.endsWith(`/ignored/function`),
],
applyCustomAttributesOnSpan: customAttributeFunction,
};
plugin.enable(http, tracer, tracer.logger, config);

server = http.createServer((request, response) => {
response.end('Test Server Response');
});
Expand All @@ -106,6 +108,18 @@ describe('HttpPlugin', () => {
plugin.disable();
});

it('http module should be patched', () => {
assert.strictEqual(http.Server.prototype.emit.__wrapped, true);
});

it("should not patch if it's not a http module", () => {
const httpNotPatched = new HttpPlugin(
HttpPlugin.component,
process.versions.node
).enable({} as Http, tracer, tracer.logger, {});
assert.strictEqual(Object.keys(httpNotPatched).length, 0);
});

it('should generate valid spans (client side and server side)', async () => {
const result = await httpRequest.get(
`http://${hostname}:${serverPort}${pathname}`
Expand Down Expand Up @@ -142,7 +156,7 @@ describe('HttpPlugin', () => {
assert.strictEqual(spans.length, 0);
});

const httpErrorCodes = [400, 401, 403, 404, 429, 501, 503, 504, 500];
const httpErrorCodes = [400, 401, 403, 404, 429, 501, 503, 504, 500, 505];

for (let i = 0; i < httpErrorCodes.length; i++) {
it(`should test span for GET requests with http error ${httpErrorCodes[i]}`, async () => {
Expand Down Expand Up @@ -283,14 +297,14 @@ describe('HttpPlugin', () => {
});

for (const ignored of ['string', 'function', 'regexp']) {
it(`should not trace ignored requests with type ${ignored}`, async () => {
it(`should not trace ignored requests (client and server side) with type ${ignored}`, async () => {
const testPath = `/ignored/${ignored}`;
doNock(hostname, testPath, 200, 'Ok');

await httpRequest.get(
`${protocol}://${hostname}:${serverPort}${testPath}`
);
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 0);
await httpRequest.get(`${protocol}://${hostname}${testPath}`);
assert.strictEqual(spans.length, 0);
});
}

Expand Down
51 changes: 31 additions & 20 deletions packages/opentelemetry-plugin-http/test/functionals/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,19 @@ describe('Utils', () => {

describe('getRequestInfo()', () => {
it('should get options object', () => {
const result = Utils.getRequestInfo('http://google.fr/');
assert.strictEqual(result.optionsParsed.hostname, 'google.fr');
assert.strictEqual(result.optionsParsed.protocol, 'http:');
assert.strictEqual(result.optionsParsed.path, '/');
assert.strictEqual(result.pathname, '/');
const webUrl = 'http://google.fr/';
const urlParsed = url.parse(webUrl);
const urlParsedWithoutPathname = {
...urlParsed,
pathname: undefined,
};
for (const param of [webUrl, urlParsed, urlParsedWithoutPathname]) {
const result = Utils.getRequestInfo(param);
assert.strictEqual(result.optionsParsed.hostname, 'google.fr');
assert.strictEqual(result.optionsParsed.protocol, 'http:');
assert.strictEqual(result.optionsParsed.path, '/');
assert.strictEqual(result.pathname, '/');
}
});
});

Expand Down Expand Up @@ -212,22 +220,25 @@ describe('Utils', () => {

describe('setSpanWithError()', () => {
it('should have error attributes', () => {
const span = new Span(
new BasicTracer({
scopeManager: new NoopScopeManager(),
}),
'test',
{ spanId: '', traceId: '' },
SpanKind.INTERNAL
);
const errorMessage = 'test error';
Utils.setSpanWithError(span, new Error(errorMessage));
const attributes = span.toReadableSpan().attributes;
assert.strictEqual(
attributes[AttributeNames.HTTP_ERROR_MESSAGE],
errorMessage
);
assert.ok(attributes[AttributeNames.HTTP_ERROR_NAME]);
for (const obj of [undefined, { statusCode: 400 }]) {
const span = new Span(
new BasicTracer({
scopeManager: new NoopScopeManager(),
}),
'test',
{ spanId: '', traceId: '' },
SpanKind.INTERNAL
);
/* tslint:disable-next-line:no-any */
Utils.setSpanWithError(span, new Error(errorMessage), obj as any);
const attributes = span.toReadableSpan().attributes;
assert.strictEqual(
attributes[AttributeNames.HTTP_ERROR_MESSAGE],
errorMessage
);
assert.ok(attributes[AttributeNames.HTTP_ERROR_NAME]);
}
});
});
describe('isOpenTelemetryRequest()', () => {
Expand Down

0 comments on commit 7005026

Please sign in to comment.