Skip to content

Commit

Permalink
Merge branch 'main' into http-attr-sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud authored Jul 17, 2021
2 parents 9c9ae40 + 5aabcc7 commit 967a152
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as types from '../../types';
import * as path from 'path';
import * as RequireInTheMiddle from 'require-in-the-middle';
import * as semver from 'semver';
import { satisfies } from 'semver';
import { InstrumentationAbstract } from '../../instrumentation';
import { InstrumentationModuleDefinition } from './types';
import { diag } from '@opentelemetry/api';
Expand Down Expand Up @@ -50,7 +50,7 @@ export abstract class InstrumentationBase<T = any>
if (this._modules.length === 0) {
diag.warn(
'No modules instrumentation has been defined,' +
' nothing will be patched'
' nothing will be patched'
);
}

Expand Down Expand Up @@ -80,7 +80,7 @@ export abstract class InstrumentationBase<T = any>
// main module
if (
typeof version === 'string' &&
isSupported(module.supportedVersions, version)
isSupported(module.supportedVersions, version, module.includePrerelease)
) {
if (typeof module.patch === 'function') {
module.moduleExports = exports;
Expand All @@ -93,7 +93,7 @@ export abstract class InstrumentationBase<T = any>
// internal file
const files = module.files ?? [];
const file = files.find(file => file.name === name);
if (file && isSupported(file.supportedVersions, version)) {
if (file && isSupported(file.supportedVersions, version, module.includePrerelease)) {
file.moduleExports = exports;
if (this._enabled) {
return file.patch(exports, module.moduleVersion);
Expand Down Expand Up @@ -167,8 +167,8 @@ export abstract class InstrumentationBase<T = any>
}
}

function isSupported(supportedVersions: string[], version: string): boolean {
function isSupported(supportedVersions: string[], version: string, includePrerelease?: boolean): boolean {
return supportedVersions.some(supportedVersion => {
return semver.satisfies(version, supportedVersion);
return satisfies(version, supportedVersion, { includePrerelease });
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export interface InstrumentationModuleDefinition<T> {
/** Module internal files to be patched */
files: InstrumentationModuleFile<any>[];

/** If set to true, the includePrerelease check will be included when calling semver.satisfies */
includePrerelease?: boolean;

/** Method to patch the instrumentation */
patch?: (moduleExports: T, moduleVersion?: string) => T;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
export enum PerformanceTimingNames {
CONNECT_END = 'connectEnd',
CONNECT_START = 'connectStart',
DECODED_BODY_SIZE = 'decodedBodySize',
DOM_COMPLETE = 'domComplete',
DOM_CONTENT_LOADED_EVENT_END = 'domContentLoadedEventEnd',
DOM_CONTENT_LOADED_EVENT_START = 'domContentLoadedEventStart',
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-web/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { PerformanceTimingNames } from './enums/PerformanceTimingNames';
export type PerformanceEntries = {
[PerformanceTimingNames.CONNECT_END]?: number;
[PerformanceTimingNames.CONNECT_START]?: number;
[PerformanceTimingNames.DECODED_BODY_SIZE]?: number;
[PerformanceTimingNames.DOM_COMPLETE]?: number;
[PerformanceTimingNames.DOM_CONTENT_LOADED_EVENT_END]?: number;
[PerformanceTimingNames.DOM_CONTENT_LOADED_EVENT_START]?: number;
Expand Down
14 changes: 11 additions & 3 deletions packages/opentelemetry-web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,19 @@ export function addSpanNetworkEvents(
addSpanNetworkEvent(span, PTN.REQUEST_START, resource);
addSpanNetworkEvent(span, PTN.RESPONSE_START, resource);
addSpanNetworkEvent(span, PTN.RESPONSE_END, resource);
const contentLength = resource[PTN.ENCODED_BODY_SIZE];
if (contentLength !== undefined) {
const encodedLength = resource[PTN.ENCODED_BODY_SIZE];
if (encodedLength !== undefined) {
span.setAttribute(
SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH,
contentLength
encodedLength
);
}
const decodedLength = resource[PTN.DECODED_BODY_SIZE];
// Spec: Not set if transport encoding not used (in which case encoded and decoded sizes match)
if (decodedLength !== undefined && encodedLength !== decodedLength) {
span.setAttribute(
SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED,
decodedLength
);
}
}
Expand Down
22 changes: 21 additions & 1 deletion packages/opentelemetry-web/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,34 @@ describe('utils', () => {
[PTN.REQUEST_START]: 123,
[PTN.RESPONSE_START]: 123,
[PTN.RESPONSE_END]: 123,
[PTN.ENCODED_BODY_SIZE]: 123,
[PTN.DECODED_BODY_SIZE]: 123,
[PTN.ENCODED_BODY_SIZE]: 61,
} as PerformanceEntries;

assert.strictEqual(addEventSpy.callCount, 0);

addSpanNetworkEvents(span, entries);

assert.strictEqual(addEventSpy.callCount, 9);
assert.strictEqual(setAttributeSpy.callCount, 2);
});
it('should only include encoded size when content encoding is being used', () => {
const addEventSpy = sinon.spy();
const setAttributeSpy = sinon.spy();
const span = ({
addEvent: addEventSpy,
setAttribute: setAttributeSpy,
} as unknown) as tracing.Span;
const entries = {
[PTN.DECODED_BODY_SIZE]: 123,
[PTN.ENCODED_BODY_SIZE]: 123,
} as PerformanceEntries;

assert.strictEqual(setAttributeSpy.callCount, 0);

addSpanNetworkEvents(span, entries);

assert.strictEqual(addEventSpy.callCount, 0);
assert.strictEqual(setAttributeSpy.callCount, 1);
});
});
Expand Down
4 changes: 2 additions & 2 deletions renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"rangeStrategy": "bump"
},
{
"matchPaths": ["backwards-compatibility/**"],
"ignoreDeps": ["@types/node"],
"matchPaths": ["backwards-compatibility"],
"matchPackageNames": ["@types/node"],
"enabled": false
}
],
Expand Down

0 comments on commit 967a152

Please sign in to comment.