Skip to content

Commit

Permalink
Clean up and add thorough tests
Browse files Browse the repository at this point in the history
  • Loading branch information
k-fish committed Jul 14, 2023
1 parent 5823ee7 commit f0cdc05
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
19 changes: 15 additions & 4 deletions packages/tracing-internal/src/browser/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,23 @@ function addHTTPTimings(span: Span): void {
});
}

/**
* Converts ALPN protocol ids to name and version.
*
* (https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids)
* @param nextHopProtocol PerformanceResourceTiming.nextHopProtocol
*/
export function extractNetworkProtocol(nextHopProtocol: string): { name: string; version: string } {
const name = nextHopProtocol.split('/')[0].toLowerCase() || (nextHopProtocol.startsWith('h') && 'http') || 'unknown';
const version = nextHopProtocol.split('/')[1] || nextHopProtocol.split('h')[1] || 'unknown';
return { name, version };
}

function resourceTimingEntryToSpanData(resourceTiming: PerformanceResourceTiming): [string, string | number][] {
const name = resourceTiming.nextHopProtocol.split('/')[0].toLowerCase() || (resourceTiming.nextHopProtocol.startsWith('h') && 'http') || 'unknown';
const version = resourceTiming.nextHopProtocol.split('/')[1] || resourceTiming.nextHopProtocol.split('h')[1] || 'unknown';

const { name, version } = extractNetworkProtocol(resourceTiming.nextHopProtocol);

const timingSpanData: [string, string | number][] = [];

timingSpanData.push(['network.protocol.version', version], ['network.protocol.name', name]);

if (!browserPerformanceTimeOrigin) {
Expand Down
53 changes: 52 additions & 1 deletion packages/tracing-internal/test/browser/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import type { Transaction } from '../../../tracing/src';
import { addExtensionMethods, Span, spanStatusfromHttpCode } from '../../../tracing/src';
import { getDefaultBrowserClientOptions } from '../../../tracing/test/testutils';
import type { FetchData, XHRData } from '../../src/browser/request';
import { fetchCallback, instrumentOutgoingRequests, shouldAttachHeaders, xhrCallback } from '../../src/browser/request';
import {
extractNetworkProtocol,
fetchCallback,
instrumentOutgoingRequests,
shouldAttachHeaders,
xhrCallback,
} from '../../src/browser/request';
import { TestClient } from '../utils/TestClient';

beforeAll(() => {
Expand Down Expand Up @@ -388,6 +394,51 @@ describe('callbacks', () => {
});
});

describe('HTTPTimings', () => {
describe('Extracting version from ALPN protocol', () => {
const nextHopToNetworkVersion = {
'http/0.9': { name: 'http', version: '0.9' },
'http/1.0': { name: 'http', version: '1.0' },
'http/1.1': { name: 'http', version: '1.1' },
'spdy/1': { name: 'spdy', version: '1' },
'spdy/2': { name: 'spdy', version: '2' },
'spdy/3': { name: 'spdy', version: '3' },
'stun.turn': { name: 'stun.turn', version: 'none' },
'stun.nat-discovery': { name: 'stun.nat-discovery', version: 'none' },
h2: { name: 'http', version: '2' },
h2c: { name: 'http', version: '2c' },
webrtc: { name: 'webrtc', version: 'none' },
'c-webrtc': { name: 'c-webrtc', version: 'none' },
ftp: { name: 'ftp', version: 'none' },
imap: { name: 'imap', version: 'none' },
pop3: { name: 'pop', version: '3' },
managesieve: { name: 'managesieve', version: 'none' },
coap: { name: 'coap', version: 'none' },
'xmpp-client': { name: 'xmpp-client', version: 'none' },
'xmpp-server': { name: 'xmpp-server', version: 'none' },
'acme-tls/1': { name: 'acme-tls', version: '1' },
mqtt: { name: 'mqtt', version: 'none' },
dot: { name: 'dot', version: 'none' },
'ntske/1': { name: 'ntske', version: '1' },
sunrpc: { name: 'sunrpc', version: 'none' },
h3: { name: 'http', version: '3' },
smb: { name: 'smb', version: 'none' },
irc: { name: 'irc', version: 'none' },
nntp: { name: 'nntp', version: 'none' },
nnsp: { name: 'nnsp', version: 'none' },
doq: { name: 'doq', version: 'none' },
'sip/2': { name: 'sip', version: '2' },
'tds/8.0': { name: 'tds', version: '8.0' },
dicom: { name: 'dicom', version: 'none' },
};

const protocols = Object.keys(nextHopToNetworkVersion);
for (const protocol of protocols) {
expect(extractNetworkProtocol(protocol)).toMatchObject(nextHopToNetworkVersion[protocol]);
}
});
});

describe('shouldAttachHeaders', () => {
describe('should prefer `tracePropagationTargets` over defaults', () => {
it('should return `true` if the url matches the new tracePropagationTargets', () => {
Expand Down

0 comments on commit f0cdc05

Please sign in to comment.