Skip to content

Commit

Permalink
test(opentelemetry-fetch): assert in own describe block
Browse files Browse the repository at this point in the history
  • Loading branch information
niekert committed Apr 11, 2021
1 parent e7e5c15 commit f050709
Showing 1 changed file with 72 additions and 21 deletions.
93 changes: 72 additions & 21 deletions packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ import {
} from '@opentelemetry/web';
import * as assert from 'assert';
import * as sinon from 'sinon';
import { FetchInstrumentation, FetchInstrumentationConfig } from '../src';
import {
FetchInstrumentation,
FetchInstrumentationConfig,
FetchCustomAttributeFunction,
} from '../src';
import { AttributeNames } from '../src/enums/AttributeNames';
import { HttpAttribute } from '@opentelemetry/semantic-conventions';

Expand All @@ -58,11 +62,6 @@ const getData = (url: string, method?: string) =>
});

const CUSTOM_ATTRIBUTE_KEY = 'span kind';

const customAttributeFunction = (span: api.Span): void => {
span.setAttribute(CUSTOM_ATTRIBUTE_KEY, api.SpanKind.CLIENT);
};

const defaultResource = {
connectEnd: 15,
connectStart: 13,
Expand Down Expand Up @@ -293,7 +292,6 @@ describe('fetch', () => {
const propagateTraceHeaderCorsUrls = [url];
prepareData(done, url, {
propagateTraceHeaderCorsUrls,
applyCustomAttributesOnSpan: customAttributeFunction,
});
});

Expand Down Expand Up @@ -358,10 +356,9 @@ describe('fetch', () => {
[HttpAttribute.HTTP_STATUS_TEXT]: 'OK',
[HttpAttribute.HTTP_SCHEME]: 'http',
[HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH]: 30,
[CUSTOM_ATTRIBUTE_KEY]: api.SpanKind.CLIENT,
});

assert.strictEqual(keys.length, 10, 'number of attributes is wrong');
assert.strictEqual(keys.length, 9, 'number of attributes is wrong');
});

it('span should have correct events', () => {
Expand Down Expand Up @@ -554,6 +551,72 @@ describe('fetch', () => {
});
});

describe('applyCustomAttributesOnSpan option', () => {
const noop = () => {};
const prepare = (
url: string,
applyCustomAttributesOnSpan: FetchCustomAttributeFunction,
cb: VoidFunction = noop
) => {
const propagateTraceHeaderCorsUrls = [url];

prepareData(cb, url, {
propagateTraceHeaderCorsUrls,
applyCustomAttributesOnSpan,
});
};

afterEach(() => {
clearData();
});

it('applies attributes when the request is succesful', done => {
prepare(
url,
span => {
span.setAttribute(CUSTOM_ATTRIBUTE_KEY, 'custom value');
},
() => {
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
const attributes = span.attributes;

assert.ok(attributes[CUSTOM_ATTRIBUTE_KEY] === 'custom value');
done();
}
);
});

it('applies custom attributes when the request fails', done => {
prepare(
badUrl,
span => {
span.setAttribute(CUSTOM_ATTRIBUTE_KEY, 'custom value');
},
() => {
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
const attributes = span.attributes;

assert.ok(attributes[CUSTOM_ATTRIBUTE_KEY] === 'custom value');
done();
}
);
});

it('has request and response objects in callback arguments', done => {
const applyCustomAttributes: FetchCustomAttributeFunction = (
span,
request,
response
) => {
assert.ok(request.method === 'GET');
assert.ok(response.status === 200);

done();
};

prepare(url, applyCustomAttributes);
});
});
describe('when url is ignored', () => {
beforeEach(done => {
const propagateTraceHeaderCorsUrls = url;
Expand Down Expand Up @@ -595,7 +658,6 @@ describe('fetch', () => {
const propagateTraceHeaderCorsUrls = badUrl;
prepareData(done, badUrl, {
propagateTraceHeaderCorsUrls,
applyCustomAttributesOnSpan: customAttributeFunction,
});
});
afterEach(() => {
Expand All @@ -610,17 +672,6 @@ describe('fetch', () => {
'parent span is not root span'
);
});

it('should apply custom attributes', () => {
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
const attributes = span.attributes;

assert.strictEqual(
attributes[CUSTOM_ATTRIBUTE_KEY],
api.SpanKind.CLIENT,
'Custom attribute was not applied'
);
});
});

describe('when request is NOT successful (405)', () => {
Expand Down

0 comments on commit f050709

Please sign in to comment.