Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 93d6a7a

Browse files
Export browser performance timestamps for time events (#11)
1 parent 43b127a commit 93d6a7a

7 files changed

Lines changed: 123 additions & 20 deletions

File tree

packages/opencensus-web-core/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ export {Span} from './trace/model/span';
2020
export {Tracer} from './trace/model/tracer';
2121
export {Tracing} from './trace/model/tracing';
2222
export * from './trace/model/types';
23+
export * from './trace/model/attribute-keys';
2324

2425
// Re-export types this uses from @opencensus/core.
25-
export {Annotation, Attributes, Link, MessageEvent, SpanContext, SpanEventListener, TraceState, Propagation, Exporter, TracerConfig, Config} from '@opencensus/core';
26+
export {Annotation, Attributes, Link, SpanContext, SpanEventListener, TraceState, Propagation, Exporter, TracerConfig, Config} from '@opencensus/core';
2627

2728
export * from './common/time-util';
29+
export * from './common/url-util';
30+
31+
// Export global tracing instance.
32+
import {Tracing} from './trace/model/tracing';
33+
const tracing = new Tracing();
34+
export {tracing};

packages/opencensus-web-core/src/trace/model/root-span.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@
1515
*/
1616

1717
import * as coreTypes from '@opencensus/core';
18-
1918
import {randomTraceId} from '../../internal/util';
20-
2119
import {Span} from './span';
2220
import {SpanKind} from './types';
2321

2422
/** Simple mock root span for use in use tests. */
2523
export class RootSpan extends Span implements coreTypes.RootSpan {
2624
/** A list of child spans. */
27-
spans: coreTypes.Span[] = [];
25+
spans: Span[] = [];
2826

2927
constructor(
3028
/** Trace associated with this root span. */
@@ -54,6 +52,7 @@ export class RootSpan extends Span implements coreTypes.RootSpan {
5452
child.traceState = this.traceState;
5553
if (name) child.name = name;
5654
if (kind) child.kind = kind;
55+
child.start();
5756
this.spans.push(child);
5857
return child;
5958
}

packages/opencensus-web-core/src/trace/model/span.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
*/
1616

1717
import * as coreTypes from '@opencensus/core';
18+
1819
import {LOGGER} from '../../common/console-logger';
1920
import {getDateForPerfTime} from '../../common/time-util';
2021
import {randomSpanId} from '../../internal/util';
21-
import {SpanKind} from './types';
22+
23+
import {MessageEvent, SpanKind} from './types';
2224

2325
/** Default span name if none is specified. */
2426
const DEFAULT_SPAN_NAME = 'unnamed';
@@ -66,7 +68,7 @@ export class Span implements coreTypes.Span {
6668
annotations: coreTypes.Annotation[] = [];
6769

6870
/** Event describing messages sent/received between Spans. */
69-
messageEvents: coreTypes.MessageEvent[] = [];
71+
messageEvents: MessageEvent[] = [];
7072

7173
/** Pointers from the current span to another span */
7274
links: coreTypes.Link[] = [];

packages/opencensus-web-core/src/trace/model/types.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17+
// TODO(draffensperger): Remove these once the new @opencensus/core version is
18+
// pushed that has these enums/fields natively.
19+
1720
/**
1821
* Type of span. Can be used to specify additional relationships between spans
1922
* in addition to a parent/child relationship.
@@ -58,3 +61,22 @@ export enum MessageEventType {
5861
/** Indicates a received message. */
5962
RECEIVED = 'RECEIVED',
6063
}
64+
65+
/**
66+
* An event describing a message sent/received between Spans.
67+
*/
68+
export interface MessageEvent {
69+
/** A timestamp for the event. */
70+
timestamp: number;
71+
/** Indicates whether the message was sent or received. */
72+
type: string;
73+
/** An identifier for the MessageEvent's message. */
74+
id: string;
75+
/** The number of uncompressed bytes sent or received. */
76+
uncompressedSize?: number;
77+
/**
78+
* The number of compressed bytes sent or received. If zero or
79+
* undefined, assumed to be the same size as uncompressed.
80+
*/
81+
compressedSize?: number;
82+
}

packages/opencensus-web-exporter-ocagent/src/adapters.ts

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ import * as coreTypes from '@opencensus/core';
1818
import * as webCore from '@opencensus/web-core';
1919
import * as apiTypes from './api-types';
2020

21+
/**
22+
* This is a recent time in epoch milliseconds. Timestamps before this are
23+
* assumed to be in browser performance clock millseconds, and timestamps after
24+
* it are assumed to be in epoch milliseconds.
25+
*/
26+
const RECENT_EPOCH_MS = 1500000000000; // July 13, 2017.
27+
2128
/**
2229
* Converts a RootSpan type from @opencensus/core to the Span JSON structure
2330
* expected by the OpenCensus Agent's HTTP/JSON (grpc-gateway) API.
2431
*/
25-
export function adaptRootSpan(rootSpan: coreTypes.RootSpan|
26-
webCore.RootSpan): apiTypes.Span[] {
32+
export function adaptRootSpan(rootSpan: coreTypes.RootSpan): apiTypes.Span[] {
2733
const adaptedSpans: apiTypes.Span[] = rootSpan.spans.map(adaptSpan);
2834
adaptedSpans.unshift(adaptSpan(rootSpan));
2935
return adaptedSpans;
@@ -92,14 +98,28 @@ function adaptAttributes(attributes: coreTypes.Attributes):
9298

9399
function adaptAnnotation(annotation: coreTypes.Annotation): apiTypes.TimeEvent {
94100
return {
95-
time: new Date(annotation.timestamp).toISOString(),
101+
time: adaptTimestampNumber(annotation.timestamp),
96102
annotation: {
97103
description: adaptString(annotation.description),
98104
attributes: adaptAttributes(annotation.attributes),
99105
},
100106
};
101107
}
102108

109+
/**
110+
* Adapts a timestamp number for an annotation or message event. For
111+
* opencensus-web, those timestamps are allowed to be either in epoch
112+
* milliseconds or in browser performance clock milliseconds. This determines
113+
* which one it likely is based on the size of the number.
114+
* @return ISO string for timestamp
115+
*/
116+
function adaptTimestampNumber(timestamp: number): string {
117+
if (timestamp > RECENT_EPOCH_MS) {
118+
return new Date(timestamp).toISOString();
119+
}
120+
return webCore.getIsoDateStrForPerfTime(timestamp);
121+
}
122+
103123
function adaptMessageEventType(type: string): apiTypes.MessageEventType {
104124
switch (type) {
105125
case 'SENT': {
@@ -114,13 +134,24 @@ function adaptMessageEventType(type: string): apiTypes.MessageEventType {
114134

115135
function adaptMessageEvent(messageEvent: coreTypes.MessageEvent):
116136
apiTypes.TimeEvent {
137+
const apiMessageEvent: apiTypes.MessageEvent = {
138+
// tslint:disable-next-line:ban Needed to parse hexadecimal.
139+
id: String(parseInt(messageEvent.id, 16)),
140+
type: adaptMessageEventType(messageEvent.type),
141+
};
142+
// TODO(draffensperger): Remove this extra logic once there is a new
143+
// @opencensus/core release with message event size types
144+
if ((messageEvent as webCore.MessageEvent).uncompressedSize) {
145+
apiMessageEvent.uncompressedSize =
146+
(messageEvent as webCore.MessageEvent).uncompressedSize;
147+
}
148+
if ((messageEvent as webCore.MessageEvent).compressedSize) {
149+
apiMessageEvent.compressedSize =
150+
(messageEvent as webCore.MessageEvent).compressedSize;
151+
}
117152
return {
118-
time: new Date(messageEvent.timestamp).toISOString(),
119-
messageEvent: {
120-
// tslint:disable-next-line:ban Needed to parse hexadecimal.
121-
id: String(parseInt(messageEvent.id, 16)),
122-
type: adaptMessageEventType(messageEvent.type),
123-
},
153+
time: adaptTimestampNumber(messageEvent.timestamp),
154+
messageEvent: apiMessageEvent,
124155
};
125156
}
126157

@@ -176,7 +207,7 @@ interface MaybeWebSpan {
176207
endTime: Date;
177208
}
178209

179-
function adaptSpan(span: coreTypes.Span|webCore.Span): apiTypes.Span {
210+
function adaptSpan(span: coreTypes.Span): apiTypes.Span {
180211
// The stackTrace and childSpanCount attributes are not currently supported by
181212
// opencensus-web.
182213
return {

packages/opencensus-web-exporter-ocagent/src/api-types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,12 @@ export interface MessageEvent {
420420
/**
421421
* The number of uncompressed bytes sent or received.
422422
*/
423-
uncompressedSize?: string;
423+
uncompressedSize?: string|number;
424424
/**
425425
* The number of compressed bytes sent or received. If zero, assumed to be the
426426
* same size as uncompressed.
427427
*/
428-
compressedSize?: string;
428+
compressedSize?: string|number;
429429
}
430430

431431
/**

packages/opencensus-web-exporter-ocagent/test/test-adapters.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,26 @@ describe('Core to API Span adapters', () => {
175175
webSpan1.traceId = '00000000000000000000000000000001';
176176
webSpan1.startPerfTime = 10.1;
177177
webSpan1.endPerfTime = 20.113;
178+
webSpan1.messageEvents = [{
179+
id: '1',
180+
timestamp: 19.002,
181+
type: webTypes.MessageEventType.SENT,
182+
uncompressedSize: 22,
183+
compressedSize: 15,
184+
// TODO(draffensperger): remove the `as coreTypes.MessageEvent` once core
185+
// interface has new fields.
186+
} as coreTypes.MessageEvent];
178187
const webRootSpan = new webTypes.RootSpan(tracer);
179188
webRootSpan.spans = [webSpan1];
180189
webRootSpan.startPerfTime = 5.001;
181190
webRootSpan.endPerfTime = 30.000001;
182191
webRootSpan.id = '000000000000000b';
183192
webRootSpan.traceId = '00000000000000000000000000000001';
193+
webRootSpan.annotations = [{
194+
timestamp: 41.001,
195+
description: 'annotation with perf time',
196+
attributes: {attr1: true},
197+
}];
184198

185199
const apiSpans = adaptRootSpan(webRootSpan);
186200

@@ -195,7 +209,25 @@ describe('Core to API Span adapters', () => {
195209
startTime: '2019-01-20T16:00:00.005001000Z',
196210
endTime: '2019-01-20T16:00:00.030000001Z',
197211
attributes: {attributeMap: {}},
198-
timeEvents: {timeEvent: []},
212+
timeEvents: {
213+
timeEvent: [
214+
{
215+
time: '2019-01-20T16:00:00.041001000Z',
216+
annotation: {
217+
description: {
218+
value: 'annotation with perf time',
219+
},
220+
attributes: {
221+
attributeMap: {
222+
attr1: {
223+
boolValue: true,
224+
},
225+
},
226+
},
227+
},
228+
},
229+
],
230+
},
199231
links: {link: []},
200232
status: {},
201233
sameProcessAsParentSpan: true,
@@ -210,7 +242,17 @@ describe('Core to API Span adapters', () => {
210242
startTime: '2019-01-20T16:00:00.010100000Z',
211243
endTime: '2019-01-20T16:00:00.020113000Z',
212244
attributes: {attributeMap: {}},
213-
timeEvents: {timeEvent: []},
245+
timeEvents: {
246+
timeEvent: [{
247+
time: '2019-01-20T16:00:00.019002000Z',
248+
messageEvent: {
249+
id: '1',
250+
type: apiTypes.MessageEventType.SENT,
251+
uncompressedSize: 22,
252+
compressedSize: 15,
253+
},
254+
}],
255+
},
214256
links: {link: []},
215257
status: {},
216258
sameProcessAsParentSpan: true,

0 commit comments

Comments
 (0)