@@ -186,6 +186,9 @@ func NewClientTrace(ctx context.Context, opts ...ClientTraceOption) *httptrace.C
186
186
}
187
187
188
188
func (ct * clientTracer ) start (hook , spanName string , attrs ... attribute.KeyValue ) {
189
+ ct .mtx .Lock ()
190
+ defer ct .mtx .Unlock ()
191
+
189
192
if ! ct .useSpans {
190
193
if ct .root == nil {
191
194
ct .root = trace .SpanFromContext (ct .Context )
@@ -194,9 +197,6 @@ func (ct *clientTracer) start(hook, spanName string, attrs ...attribute.KeyValue
194
197
return
195
198
}
196
199
197
- ct .mtx .Lock ()
198
- defer ct .mtx .Unlock ()
199
-
200
200
if hookCtx , found := ct .activeHooks [hook ]; ! found {
201
201
var sp trace.Span
202
202
ct .activeHooks [hook ], sp = ct .tr .Start (ct .getParentContext (hook ), spanName , trace .WithAttributes (attrs ... ), trace .WithSpanKind (trace .SpanKindClient ))
@@ -214,6 +214,13 @@ func (ct *clientTracer) start(hook, spanName string, attrs ...attribute.KeyValue
214
214
}
215
215
216
216
func (ct * clientTracer ) end (hook string , err error , attrs ... attribute.KeyValue ) {
217
+ ct .mtx .Lock ()
218
+ defer ct .mtx .Unlock ()
219
+
220
+ if ct .root == nil {
221
+ return
222
+ }
223
+
217
224
if ! ct .useSpans {
218
225
if err != nil {
219
226
attrs = append (attrs , attribute .String (hook + ".error" , err .Error ()))
@@ -222,8 +229,6 @@ func (ct *clientTracer) end(hook string, err error, attrs ...attribute.KeyValue)
222
229
return
223
230
}
224
231
225
- ct .mtx .Lock ()
226
- defer ct .mtx .Unlock ()
227
232
if ctx , ok := ct .activeHooks [hook ]; ok {
228
233
span := trace .SpanFromContext (ctx )
229
234
if err != nil {
@@ -321,6 +326,9 @@ func (ct *clientTracer) tlsHandshakeDone(_ tls.ConnectionState, err error) {
321
326
}
322
327
323
328
func (ct * clientTracer ) wroteHeaderField (k string , v []string ) {
329
+ if ct .root == nil {
330
+ return
331
+ }
324
332
if ct .useSpans && ct .span ("http.headers" ) == nil {
325
333
ct .start ("http.headers" , "http.headers" )
326
334
}
0 commit comments