@@ -18,12 +18,18 @@ import * as coreTypes from '@opencensus/core';
1818import * as webCore from '@opencensus/web-core' ;
1919import * 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
9399function 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+
103123function adaptMessageEventType ( type : string ) : apiTypes . MessageEventType {
104124 switch ( type ) {
105125 case 'SENT' : {
@@ -114,13 +134,24 @@ function adaptMessageEventType(type: string): apiTypes.MessageEventType {
114134
115135function 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 {
0 commit comments