15
15
package com .google .api .client .util ;
16
16
17
17
import com .google .api .client .http .HttpHeaders ;
18
- import com .google .api .client .http .HttpRequest ;
18
+ import com .google .api .client .http .HttpResponse ;
19
19
import com .google .api .client .http .HttpStatusCodes ;
20
- import com .google .common .annotations .VisibleForTesting ;
21
20
22
21
import io .opencensus .contrib .http .util .HttpPropagationUtil ;
23
22
import io .opencensus .trace .BlankSpan ;
24
23
import io .opencensus .trace .EndSpanOptions ;
25
- import io .opencensus .trace .NetworkEvent ;
26
- import io .opencensus .trace .NetworkEvent .Type ;
27
24
import io .opencensus .trace .Span ;
28
25
import io .opencensus .trace .Status ;
29
26
import io .opencensus .trace .Tracer ;
30
27
import io .opencensus .trace .Tracing ;
31
28
import io .opencensus .trace .propagation .TextFormat ;
32
29
33
- import java .util . Collections ;
30
+ import java .io . IOException ;
34
31
import java .util .logging .Level ;
35
32
import java .util .logging .Logger ;
36
33
import javax .annotation .Nullable ;
37
34
38
35
/**
39
36
* Utilities for Census monitoring and tracing.
40
37
*
41
- * @author Hailong Wen
42
38
* @since 1.24
39
+ * @author Hailong Wen
43
40
*/
44
41
public class OpenCensusUtils {
45
42
46
- private static final Logger logger = Logger .getLogger (OpenCensusUtils .class .getName ());
47
-
48
- /**
49
- * Span name for tracing {@link HttpRequest#execute()}.
50
- */
51
- public static final String SPAN_NAME_HTTP_REQUEST_EXECUTE =
52
- "Sent." + HttpRequest .class .getName () + ".execute" ;
53
-
54
- /**
55
- * OpenCensus tracing component. When no OpenCensus implementation is provided, it will return a
56
- * no-op tracer.
57
- */
58
- private static Tracer tracer = Tracing .getTracer ();
59
-
60
- /**
61
- * Sequence id generator for message event.
62
- */
63
- private static AtomicLong idGenerator = new AtomicLong ();
43
+ private static final Logger LOGGER = Logger .getLogger (OpenCensusUtils .class .getName ());
64
44
65
45
/**
66
- * Whether spans should be recorded locally. Defaults to true.
46
+ * OpenCensus tracing component.
47
+ * When no OpenCensus implementation is provided, it will return a no-op tracer.
67
48
*/
68
- private static volatile boolean isRecordEvent = true ;
49
+ static Tracer tracer = Tracing . getTracer () ;
69
50
70
51
/**
71
52
* {@link TextFormat} used in tracing context propagation.
72
53
*/
73
54
@ Nullable
74
- @ VisibleForTesting
75
- static volatile TextFormat propagationTextFormat = null ;
55
+ static TextFormat propagationTextFormat = null ;
76
56
77
57
/**
78
- * {@link TextFormat.Setter} for {@link #propagationTextFormat }.
58
+ * {@link TextFormat.Setter} for {@link activeTextFormat }.
79
59
*/
80
60
@ Nullable
81
- @ VisibleForTesting
82
- static volatile TextFormat .Setter propagationTextFormatSetter = null ;
61
+ static TextFormat .Setter propagationTextFormatSetter = null ;
83
62
84
63
/**
85
64
* Sets the {@link TextFormat} used in context propagation.
86
- *
87
- * <p>This API allows users of google-http-client to specify other text format, or disable context
88
- * propagation by setting it to {@code null}. It should be used along with {@link
89
- * #setPropagationTextFormatSetter} for setting purpose. </p>
90
- *
91
65
* @param textFormat the text format.
92
66
*/
93
67
public static void setPropagationTextFormat (@ Nullable TextFormat textFormat ) {
@@ -96,28 +70,12 @@ public static void setPropagationTextFormat(@Nullable TextFormat textFormat) {
96
70
97
71
/**
98
72
* Sets the {@link TextFormat.Setter} used in context propagation.
99
- *
100
- * <p>This API allows users of google-http-client to specify other text format setter, or disable
101
- * context propagation by setting it to {@code null}. It should be used along with {@link
102
- * #setPropagationTextFormat} for setting purpose. </p>
103
- *
104
73
* @param textFormatSetter the {@code TextFormat.Setter} for the text format.
105
74
*/
106
75
public static void setPropagationTextFormatSetter (@ Nullable TextFormat .Setter textFormatSetter ) {
107
76
propagationTextFormatSetter = textFormatSetter ;
108
77
}
109
78
110
- /**
111
- * Sets whether spans should be recorded locally.
112
- *
113
- * <p> This API allows users of google-http-client to turn on/off local span collection. </p>
114
- *
115
- * @param recordEvent record span locally if true.
116
- */
117
- public static void setIsRecordEvent (boolean recordEvent ) {
118
- isRecordEvent = recordEvent ;
119
- }
120
-
121
79
/**
122
80
* Returns the tracing component of OpenCensus.
123
81
*
@@ -127,27 +85,15 @@ public static Tracer getTracer() {
127
85
return tracer ;
128
86
}
129
87
130
- /**
131
- * Returns whether spans should be recorded locally.
132
- *
133
- * @return whether spans should be recorded locally.
134
- */
135
- public static boolean isRecordEvent () {
136
- return isRecordEvent ;
137
- }
138
-
139
88
/**
140
89
* Propagate information of current tracing context. This information will be injected into HTTP
141
90
* header.
142
- *
143
- * @param span the span to be propagated.
144
- * @param headers the headers used in propagation.
145
91
*/
146
- public static void propagateTracingContext (Span span , HttpHeaders headers ) {
147
- Preconditions .checkArgument (span != null , "span should not be null." );
148
- Preconditions .checkArgument (headers != null , "headers should not be null." );
92
+ public static void propagateTracingContext (HttpHeaders headers ) {
93
+ Preconditions .checkNotNull (headers );
149
94
if (propagationTextFormat != null && propagationTextFormatSetter != null ) {
150
- if (!span .equals (BlankSpan .INSTANCE )) {
95
+ Span span = tracer .getCurrentSpan ();
96
+ if (span != null && !span .equals (BlankSpan .INSTANCE )) {
151
97
propagationTextFormat .inject (span .getContext (), headers , propagationTextFormatSetter );
152
98
}
153
99
}
@@ -161,7 +107,7 @@ public static void propagateTracingContext(Span span, HttpHeaders headers) {
161
107
*/
162
108
public static EndSpanOptions getEndSpanOptions (@ Nullable Integer statusCode ) {
163
109
// Always sample the span, but optionally export it.
164
- EndSpanOptions .Builder builder = EndSpanOptions .builder ();
110
+ EndSpanOptions .Builder builder = EndSpanOptions .builder (). setSampleToLocalSpanStore ( true ) ;
165
111
if (statusCode == null ) {
166
112
builder .setStatus (Status .UNKNOWN );
167
113
} else if (!HttpStatusCodes .isSuccess (statusCode )) {
@@ -193,49 +139,6 @@ public static EndSpanOptions getEndSpanOptions(@Nullable Integer statusCode) {
193
139
return builder .build ();
194
140
}
195
141
196
- /**
197
- * Records a new message event which contains the size of the request content. Note that the size
198
- * represents the message size in application layer, i.e., content-length.
199
- *
200
- * @param span The {@code span} in which the send event occurs.
201
- * @param size Size of the request.
202
- */
203
- public static void recordSentMessageEvent (Span span , long size ) {
204
- recordMessageEvent (span , size , Type .SENT );
205
- }
206
-
207
- /**
208
- * Records a new message event which contains the size of the response content. Note that the size
209
- * represents the message size in application layer, i.e., content-length.
210
- *
211
- * @param span The {@code span} in which the receive event occurs.
212
- * @param size Size of the response.
213
- */
214
- public static void recordReceivedMessageEvent (Span span , long size ) {
215
- recordMessageEvent (span , size , Type .RECV );
216
- }
217
-
218
- /**
219
- * Records a message event of a certain {@link NetowrkEvent.Type}. This method is package
220
- * protected since {@link NetworkEvent} might be deprecated in future releases.
221
- *
222
- * @param span The {@code span} in which the event occurs.
223
- * @param size Size of the message.
224
- * @param eventType The {@code NetworkEvent.Type} of the message event.
225
- */
226
- @ VisibleForTesting
227
- static void recordMessageEvent (Span span , long size , Type eventType ) {
228
- Preconditions .checkArgument (span != null , "span should not be null." );
229
- if (size < 0 ) {
230
- size = 0 ;
231
- }
232
- NetworkEvent event = NetworkEvent
233
- .builder (eventType , idGenerator .getAndIncrement ())
234
- .setUncompressedMessageSize (size )
235
- .build ();
236
- span .addNetworkEvent (event );
237
- }
238
-
239
142
static {
240
143
try {
241
144
propagationTextFormat = HttpPropagationUtil .getCloudTraceFormat ();
@@ -246,16 +149,7 @@ public void put(HttpHeaders carrier, String key, String value) {
246
149
}
247
150
};
248
151
} catch (Exception e ) {
249
- logger .log (
250
- Level .WARNING , "Cannot initialize default OpenCensus HTTP propagation text format." , e );
251
- }
252
-
253
- try {
254
- Tracing .getExportComponent ().getSampledSpanStore ().registerSpanNamesForCollection (
255
- Collections .<String >singletonList (SPAN_NAME_HTTP_REQUEST_EXECUTE ));
256
- } catch (Exception e ) {
257
- logger .log (
258
- Level .WARNING , "Cannot register default OpenCensus span names for collection." , e );
152
+ LOGGER .log (Level .WARNING , "Cannot initiate OpenCensus modules, tracing disabled" , e );
259
153
}
260
154
}
261
155
0 commit comments