66
77'use strict'
88
9+ const spanMessages = require ( '../data/v1/Span_pb' )
910class Span {
1011 constructor ( traceRoot ) {
1112 this . traceRoot = traceRoot
@@ -14,6 +15,57 @@ class Span {
1415 isSpan ( ) {
1516 return true
1617 }
18+
19+ toProtocolBuffer ( ) {
20+ const pSpanMessage = new spanMessages . PSpanMessage ( )
21+ const pSpan = new spanMessages . PSpan ( )
22+ pSpan . setVersion ( 1 )
23+
24+ const pTransactionId = this . traceRoot . getTraceId ( ) . toProtocolBuffer ( )
25+ pSpan . setTransactionid ( pTransactionId )
26+
27+ pSpan . setSpanid ( this . traceRoot . getTraceId ( ) . getSpanId ( ) )
28+ pSpan . setParentspanid ( this . traceRoot . getTraceId ( ) . getParentSpanId ( ) )
29+ pSpan . setStarttime ( this . startTime )
30+ pSpan . setElapsed ( this . elapsedTime )
31+ pSpan . setApiid ( this . apiId )
32+ pSpan . setServicetype ( this . serviceType )
33+
34+ const pAcceptEvent = new spanMessages . PAcceptEvent ( )
35+ pAcceptEvent . setRpc ( this . rpc )
36+ pAcceptEvent . setEndpoint ( this . endPoint ?? 'UNKNOWN' )
37+ pAcceptEvent . setRemoteaddr ( this . remoteAddress ?? 'UNKNOWN' )
38+
39+ const pParentInfo = new spanMessages . PParentInfo ( )
40+ if ( this . parentApplicationType ) {
41+ pParentInfo . setParentapplicationtype ( this . parentApplicationType )
42+ }
43+ if ( this . parentApplicationName ) {
44+ pParentInfo . setParentapplicationname ( this . parentApplicationName )
45+ }
46+ if ( this . acceptorHost ) {
47+ pParentInfo . setAcceptorhost ( this . acceptorHost )
48+ }
49+ pAcceptEvent . setParentinfo ( pParentInfo )
50+ pSpan . setAcceptevent ( pAcceptEvent )
51+
52+ pSpan . setFlag ( this . traceRoot . getTraceId ( ) . getFlags ( ) )
53+
54+ if ( this . traceRoot . hasErrorCode ( ) ) {
55+ pSpan . setErr ( this . traceRoot . getShared ( ) . getErrorCode ( ) )
56+ }
57+
58+ if ( this . applicationServiceType ) {
59+ pSpan . setApplicationservicetype ( this . applicationServiceType )
60+ }
61+ // TODO: SpanMessageMapperImpl.java: spanTraceRootSharedLoggingInfo loggingTransactionInfo
62+
63+ this . spanEventList . forEach ( spanEvent => pSpan . addSpanevent ( spanEvent . toProtocolBuffer ( ) ) )
64+ this . annotations . forEach ( annotation => pSpan . addAnnotation ( annotation . pAnnotation ( ) ) )
65+
66+ pSpanMessage . setSpan ( pSpan )
67+ return pSpanMessage
68+ }
1769}
1870
1971class SpanBuilder {
@@ -23,6 +75,15 @@ class SpanBuilder {
2375 this . startTime = traceRoot . getTraceStartTime ( )
2476 }
2577
78+ setServiceType ( serviceType ) {
79+ this . serviceType = serviceType
80+ return this
81+ }
82+
83+ getStartTime ( ) {
84+ return this . startTime
85+ }
86+
2687 setApiId ( apiId ) {
2788 this . apiId = apiId
2889 return this
@@ -72,9 +133,26 @@ class SpanBuilder {
72133 return this . traceRoot
73134 }
74135
136+ setSpanEventList ( spanEventList ) {
137+ this . spanEventList = spanEventList
138+ return this
139+ }
140+
141+ setApplicationServiceType ( applicationServiceType ) {
142+ this . applicationServiceType = applicationServiceType
143+ return this
144+ }
145+
146+ markAfterTime ( ) {
147+ this . elapsedTime = Date . now ( ) - this . startTime
148+ return this
149+ }
150+
75151 build ( ) {
76152 const span = new Span ( this . traceRoot )
77- this . startTime = this . startTime || Date . now ( )
153+ span . serviceType = this . serviceType
154+ span . startTime = this . startTime || Date . now ( )
155+ span . elapsedTime = this . elapsedTime
78156 span . apiId = this . apiId
79157 span . rpc = this . rpc
80158 span . endPoint = this . endPoint
@@ -84,6 +162,8 @@ class SpanBuilder {
84162 span . acceptorHost = this . acceptorHost
85163 span . parentApplicationName = this . parentApplicationName
86164 span . parentApplicationType = this . parentApplicationType
165+ span . spanEventList = this . spanEventList ?? [ ]
166+ span . applicationServiceType = this . applicationServiceType
87167 return span
88168 }
89169}
0 commit comments