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

refactor(http-plugin): use SpanProcessor for tests #244

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,26 @@

import { NoopLogger } from '@opentelemetry/core';
import { AsyncHooksScopeManager } from '@opentelemetry/scope-async-hooks';
import { SpanKind, Span } from '@opentelemetry/types';
import { SpanKind, Span as ISpan } from '@opentelemetry/types';
import * as assert from 'assert';
import * as http from 'http';
import * as nock from 'nock';
import { HttpPlugin, plugin } from '../../src/http';
import { assertSpan } from '../utils/assertSpan';
import { DummyPropagation } from '../utils/DummyPropagation';
import { httpRequest } from '../utils/httpRequest';
import { TracerTest } from '../utils/TracerTest';
import { SpanAuditProcessor } from '../utils/SpanAuditProcessor';
import { NodeTracer } from '@opentelemetry/node-tracer';
import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/basic-tracer';

let server: http.Server;
const serverPort = 12345;
const protocol = 'http';
const hostname = 'localhost';
const pathname = '/test';
const audit = new SpanAuditProcessor();
const memoryExporter = new InMemorySpanExporter();

function doNock(
hostname: string,
Expand All @@ -48,7 +51,7 @@ function doNock(
.reply(httpCode, respBody);
}

export const customAttributeFunction = (span: Span): void => {
export const customAttributeFunction = (span: ISpan): void => {
mayurkale22 marked this conversation as resolved.
Show resolved Hide resolved
span.setAttribute('span kind', SpanKind.CLIENT);
};

Expand All @@ -69,22 +72,20 @@ describe('HttpPlugin', () => {
const scopeManager = new AsyncHooksScopeManager();
const httpTextFormat = new DummyPropagation();
const logger = new NoopLogger();
const tracer = new TracerTest(
{
scopeManager,
logger,
httpTextFormat,
},
audit
);
const tracer = new NodeTracer({
scopeManager,
logger,
httpTextFormat,
});
tracer.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
beforeEach(() => {
audit.reset();
memoryExporter.reset();
});

before(() => {
plugin.enable(http, tracer, tracer.logger);
const ignoreConfig = [
`http://${hostname}:${serverPort}/ignored/string`,
`http://${hostname}/ignored/string`,
/\/ignored\/regexp$/i,
(url: string) => url.endsWith(`/ignored/function`),
];
Expand All @@ -110,10 +111,8 @@ describe('HttpPlugin', () => {
httpRequest
.get(`http://${hostname}:${serverPort}${pathname}`)
.then(result => {
const spans = audit.processSpans();
const outgoingSpan = spans[0];
const incomingSpan = spans[1];

const spans = memoryExporter.getFinishedSpans();
const [incomingSpan, outgoingSpan] = spans;
const validations = {
hostname,
httpStatusCode: result.statusCode!,
Expand Down Expand Up @@ -143,12 +142,14 @@ describe('HttpPlugin', () => {
httpErrorCodes[i].toString()
);

const isReset = audit.processSpans().length === 0;
const isReset = memoryExporter.getFinishedSpans().length === 0;
assert.ok(isReset);
await httpRequest
.get(`${protocol}://${hostname}${testPath}`)
.then(result => {
const spans = audit.processSpans();
const spans = memoryExporter.getFinishedSpans();
const reqSpan = spans[0];

assert.strictEqual(result.data, httpErrorCodes[i].toString());
assert.strictEqual(spans.length, 1);

Expand All @@ -161,7 +162,7 @@ describe('HttpPlugin', () => {
reqHeaders: result.reqHeaders,
};

assertSpan(spans[0], SpanKind.CLIENT, validations);
assertSpan(reqSpan, SpanKind.CLIENT, validations);
});
});
}
Expand All @@ -171,31 +172,37 @@ describe('HttpPlugin', () => {
doNock(hostname, testPath, 200, 'Ok');
const name = 'TestRootSpan';
const span = tracer.startSpan(name);
tracer.withSpan(span, () => {
httpRequest.get(`${protocol}://${hostname}${testPath}`).then(result => {
const spans = audit.processSpans();
assert.ok(spans[0].name.indexOf('TestRootSpan') >= 0);
assert.strictEqual(spans.length, 2);
assert.ok(spans[1].name.indexOf(testPath) >= 0);
assert.strictEqual(
spans[1].spanContext.traceId,
spans[0].spanContext.traceId
);
const validations = {
hostname,
httpStatusCode: result.statusCode!,
httpMethod: 'GET',
pathname: testPath,
resHeaders: result.resHeaders,
reqHeaders: result.reqHeaders,
};
assertSpan(spans[1], SpanKind.CLIENT, validations);
assert.notStrictEqual(
spans[1].spanContext.spanId,
spans[0].spanContext.spanId
);
done();
});
return tracer.withSpan(span, () => {
httpRequest
.get(`${protocol}://${hostname}${testPath}`)
.then(result => {
span.end();
const spans = memoryExporter.getFinishedSpans();
const [reqSpan, localSpan] = spans;
const validations = {
hostname,
httpStatusCode: result.statusCode!,
httpMethod: 'GET',
pathname: testPath,
resHeaders: result.resHeaders,
reqHeaders: result.reqHeaders,
};

assert.ok(localSpan.name.indexOf('TestRootSpan') >= 0);
assert.strictEqual(spans.length, 2);
assert.ok(reqSpan.name.indexOf(testPath) >= 0);
assert.strictEqual(
localSpan.spanContext.traceId,
reqSpan.spanContext.traceId
);
assertSpan(reqSpan, SpanKind.CLIENT, validations);
assert.notStrictEqual(
localSpan.spanContext.spanId,
reqSpan.spanContext.spanId
);
done();
})
.catch(done);
});
});

Expand All @@ -214,14 +221,9 @@ describe('HttpPlugin', () => {
httpRequest
.get(`${protocol}://${hostname}${testPath}`)
.then(result => {
const spans = audit.processSpans();
assert.ok(spans[0].name.indexOf('TestRootSpan') >= 0);
assert.strictEqual(spans.length, 2);
assert.ok(spans[1].name.indexOf(testPath) >= 0);
assert.strictEqual(
spans[1].spanContext.traceId,
spans[0].spanContext.traceId
);
span.end();
const spans = memoryExporter.getFinishedSpans();
const [reqSpan, localSpan] = spans;
const validations = {
hostname,
httpStatusCode: result.statusCode!,
Expand All @@ -230,49 +232,52 @@ describe('HttpPlugin', () => {
resHeaders: result.resHeaders,
reqHeaders: result.reqHeaders,
};
assertSpan(spans[1], SpanKind.CLIENT, validations);

assert.ok(localSpan.name.indexOf('TestRootSpan') >= 0);
assert.strictEqual(spans.length, 2);
assert.ok(reqSpan.name.indexOf(testPath) >= 0);
assert.strictEqual(
localSpan.spanContext.traceId,
reqSpan.spanContext.traceId
);
assertSpan(reqSpan, SpanKind.CLIENT, validations);
assert.notStrictEqual(
spans[1].spanContext.spanId,
spans[0].spanContext.spanId
localSpan.spanContext.spanId,
reqSpan.spanContext.spanId
);
done();
});
});
});
}
// TODO: uncomment once https://github.com/open-telemetry/opentelemetry-js/pull/146 is merged
// it('should create multiple child spans for GET requests', (done) => {
// it.only('should create multiple child spans for GET requests', async () => {
// const testPath = '/outgoing/rootSpan/childs';
// const num = 5;
// doNock(hostname, testPath, 200, 'Ok', num);
// const name = 'TestRootSpan';
// const span = tracer.startSpan(name);
// const auditedSpan = audit.processSpans()[0];
// assert.ok(auditedSpan.name.indexOf('TestRootSpan') >= 0);
// tracer.withSpan(span, async () => {
// await tracer.withSpan(span, async () => {
// for (let i = 0; i < num; i++) {
// await httpRequest.get(`${ protocol }://${ hostname }${ testPath }`).then(result => {
// const spans = audit.processSpans();
// const startChildIndex = i + 1;
// assert.strictEqual(spans.length, startChildIndex + 1);
// assert.ok(spans[startChildIndex].name.indexOf(testPath) >= 0);
// assert.strictEqual(auditedSpan.spanContext.traceId, spans[startChildIndex].spanContext.traceId);
// });
// await httpRequest.get(`${ protocol }://${ hostname }${ testPath }`);
// const spans = memoryExporter.getFinishedSpans();
// assert.ok(spans[i].name.indexOf(testPath) >= 0);
// assert.strictEqual((span as Span).toReadableSpan().spanContext.traceId, spans[i].spanContext.traceId);
// }
// const spans = audit.processSpans();
// span.end();
// const spans = memoryExporter.getFinishedSpans();
// // 5 child spans ended + 1 span (root)
// assert.strictEqual(spans.length, 6);
// span.end();
// done();
// });
// console.log('end');
// });

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

const spans = audit.processSpans();
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 0);
await httpRequest.get(`${protocol}://${hostname}${testPath}`);
assert.strictEqual(spans.length, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ import * as nock from 'nock';
import { plugin } from '../../src/http';
import { assertSpan } from '../utils/assertSpan';
import { DummyPropagation } from '../utils/DummyPropagation';
import { TracerTest } from '../utils/TracerTest';
import { SpanAuditProcessor } from '../utils/SpanAuditProcessor';
import * as url from 'url';
import axios, { AxiosResponse } from 'axios';
import * as superagent from 'superagent';
import * as got from 'got';
import * as request from 'request-promise-native';
import * as path from 'path';
import { NodeTracer } from '@opentelemetry/node-tracer';
import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/basic-tracer';

const audit = new SpanAuditProcessor();
const memoryExporter = new InMemorySpanExporter();

export const customAttributeFunction = (span: Span): void => {
span.setAttribute('span kind', SpanKind.CLIENT);
Expand All @@ -44,16 +47,14 @@ describe('Packages', () => {
const httpTextFormat = new DummyPropagation();
const logger = new NoopLogger();

const tracer = new TracerTest(
{
scopeManager,
logger,
httpTextFormat,
},
audit
);
const tracer = new NodeTracer({
scopeManager,
logger,
httpTextFormat,
});
tracer.addSpanProcessor(new SimpleSpanProcessor(memoryExporter));
beforeEach(() => {
audit.reset();
memoryExporter.reset();
});

before(() => {
Expand Down Expand Up @@ -101,10 +102,7 @@ describe('Packages', () => {
const res = result as AxiosResponse<{}>;
resHeaders = res.headers;
}
const spans = audit.processSpans();
assert.strictEqual(spans.length, 1);
assert.ok(spans[0].name.indexOf(`GET ${urlparsed.pathname}`) >= 0);

const spans = memoryExporter.getFinishedSpans();
const span = spans[0];
const validations = {
hostname: urlparsed.hostname!,
Expand All @@ -115,6 +113,9 @@ describe('Packages', () => {
resHeaders,
};

assert.strictEqual(spans.length, 1);
assert.ok(span.name.indexOf(`GET ${urlparsed.pathname}`) >= 0);

switch (name) {
case 'axios':
assert.ok(
Expand Down
Loading