21
21
import com .google .common .annotations .VisibleForTesting ;
22
22
23
23
import io .opencensus .contrib .http .util .HttpPropagationUtil ;
24
- import io .opencensus .trace .BlankSpan ;
25
24
import io .opencensus .trace .EndSpanOptions ;
26
25
import io .opencensus .trace .NetworkEvent ;
27
26
import io .opencensus .trace .NetworkEvent .Type ;
28
27
import io .opencensus .trace .Span ;
28
+ import io .opencensus .trace .SpanContext ;
29
29
import io .opencensus .trace .Status ;
30
30
import io .opencensus .trace .Tracer ;
31
31
import io .opencensus .trace .Tracing ;
@@ -60,11 +60,6 @@ public class OpenCensusUtils {
60
60
*/
61
61
private static Tracer tracer = Tracing .getTracer ();
62
62
63
- /**
64
- * Sequence id generator for message event.
65
- */
66
- private static AtomicLong idGenerator = new AtomicLong ();
67
-
68
63
/**
69
64
* Whether spans should be recorded locally. Defaults to true.
70
65
*/
@@ -140,18 +135,18 @@ public static boolean isRecordEvent() {
140
135
}
141
136
142
137
/**
143
- * Propagate information of current tracing context. This information will be injected into HTTP
138
+ * Propagate information of a given tracing context. This information will be injected into HTTP
144
139
* header.
145
140
*
146
- * @param span the span to be propagated.
141
+ * @param spanContext the spanContext to be propagated.
147
142
* @param headers the headers used in propagation.
148
143
*/
149
- public static void propagateTracingContext (Span span , HttpHeaders headers ) {
150
- Preconditions .checkArgument (span != null , "span should not be null." );
144
+ public static void propagateTracingContext (SpanContext spanContext , HttpHeaders headers ) {
145
+ Preconditions .checkArgument (spanContext != null , "spanContext should not be null." );
151
146
Preconditions .checkArgument (headers != null , "headers should not be null." );
152
147
if (propagationTextFormat != null && propagationTextFormatSetter != null ) {
153
- if (!span .equals (BlankSpan . INSTANCE )) {
154
- propagationTextFormat .inject (span . getContext () , headers , propagationTextFormatSetter );
148
+ if (!spanContext .equals (SpanContext . INVALID )) {
149
+ propagationTextFormat .inject (spanContext , headers , propagationTextFormatSetter );
155
150
}
156
151
}
157
152
}
@@ -201,21 +196,23 @@ public static EndSpanOptions getEndSpanOptions(@Nullable Integer statusCode) {
201
196
* Note that the size represents the message size in application layer, i.e., content-length.
202
197
*
203
198
* @param span The {@code span} in which the send event occurs.
199
+ * @param id The id for the message, It is unique within an {@link HttpRequest}.
204
200
* @param size Size of the request.
205
201
*/
206
- public static void recordSentMessageEvent (Span span , long size ) {
207
- recordMessageEvent (span , size , Type .SENT );
202
+ public static void recordSentMessageEvent (Span span , long id , long size ) {
203
+ recordMessageEvent (span , id , size , Type .SENT );
208
204
}
209
205
210
206
/**
211
207
* Records a new message event which contains the size of the response content.
212
208
* Note that the size represents the message size in application layer, i.e., content-length.
213
209
*
214
210
* @param span The {@code span} in which the receive event occurs.
211
+ * @param id The id for the message. It is unique within an {@link HttpRequest}.
215
212
* @param size Size of the response.
216
213
*/
217
- public static void recordReceivedMessageEvent (Span span , long size ) {
218
- recordMessageEvent (span , size , Type .RECV );
214
+ public static void recordReceivedMessageEvent (Span span , long id , long size ) {
215
+ recordMessageEvent (span , id , size , Type .RECV );
219
216
}
220
217
221
218
/**
@@ -224,15 +221,16 @@ public static void recordReceivedMessageEvent(Span span, long size) {
224
221
* releases.
225
222
*
226
223
* @param span The {@code span} in which the event occurs.
224
+ * @param id The id for the message.
227
225
* @param size Size of the message.
228
226
* @param eventType The {@code NetworkEvent.Type} of the message event.
229
227
*/
230
228
@ VisibleForTesting
231
- static void recordMessageEvent (Span span , long size , Type eventType ) {
229
+ static void recordMessageEvent (Span span , long id , long size , Type eventType ) {
232
230
Preconditions .checkArgument (span != null , "span should not be null." );
233
231
if (size < 0 ) size = 0 ;
234
232
NetworkEvent event = NetworkEvent
235
- .builder (eventType , idGenerator . getAndIncrement () )
233
+ .builder (eventType , id )
236
234
.setUncompressedMessageSize (size )
237
235
.build ();
238
236
span .addNetworkEvent (event );
0 commit comments