@@ -283,24 +283,38 @@ func (f *Firecracker) do(ctx context.Context, method, path string, reqBody any,
283283 attribute .String ("http.method" , method ),
284284 attribute .String ("http.route" , path ),
285285 )
286- ctx , span := otel .Tracer ("hypeman/hypervisor/firecracker" ).Start (ctx , "hypervisor.http " + method + " " + path , trace .WithAttributes (attrs ... ))
287- defer span .End ()
286+ tracer := otel .Tracer ("hypeman/hypervisor/firecracker" )
287+ spanName := "hypervisor.http " + method + " " + path
288+ shouldTrace := hypervisor .ShouldTraceHypervisorHTTPSpan (method , path )
289+
290+ var span trace.Span
291+ if shouldTrace {
292+ var spanCtx context.Context
293+ spanCtx , span = tracer .Start (ctx , spanName , trace .WithAttributes (attrs ... ))
294+ ctx = spanCtx
295+ defer span .End ()
296+ }
297+
298+ recordError := func (err error ) {
299+ if shouldTrace {
300+ span .RecordError (err )
301+ span .SetStatus (codes .Error , err .Error ())
302+ }
303+ }
288304
289305 var bodyReader io.Reader
290306 if reqBody != nil {
291307 data , err := json .Marshal (reqBody )
292308 if err != nil {
293- span .RecordError (err )
294- span .SetStatus (codes .Error , err .Error ())
309+ recordError (err )
295310 return nil , fmt .Errorf ("marshal request body: %w" , err )
296311 }
297312 bodyReader = bytes .NewReader (data )
298313 }
299314
300315 req , err := http .NewRequestWithContext (ctx , method , "http://localhost" + path , bodyReader )
301316 if err != nil {
302- span .RecordError (err )
303- span .SetStatus (codes .Error , err .Error ())
317+ recordError (err )
304318 return nil , fmt .Errorf ("create request: %w" , err )
305319 }
306320 req .Header .Set ("Accept" , "application/json" )
@@ -310,35 +324,43 @@ func (f *Firecracker) do(ctx context.Context, method, path string, reqBody any,
310324
311325 resp , err := f .client .Do (req )
312326 if err != nil {
313- span .RecordError (err )
314- span .SetStatus (codes .Error , err .Error ())
327+ recordError (err )
315328 return nil , fmt .Errorf ("request %s %s: %w" , method , path , err )
316329 }
317330 defer resp .Body .Close ()
318- span .SetAttributes (attribute .Int ("http.status_code" , resp .StatusCode ))
331+ if shouldTrace {
332+ span .SetAttributes (attribute .Int ("http.status_code" , resp .StatusCode ))
333+ }
319334
320335 data , err := io .ReadAll (resp .Body )
321336 if err != nil {
322- span .RecordError (err )
323- span .SetStatus (codes .Error , err .Error ())
337+ recordError (err )
324338 return nil , fmt .Errorf ("read response body: %w" , err )
325339 }
326340
327341 for _ , status := range expectedStatus {
328342 if resp .StatusCode == status {
329- span .SetStatus (codes .Ok , "" )
343+ if shouldTrace {
344+ span .SetStatus (codes .Ok , "" )
345+ }
330346 return data , nil
331347 }
332348 }
333349
334350 if len (data ) > 0 {
335351 var apiErr apiError
336352 if err := json .Unmarshal (data , & apiErr ); err == nil && apiErr .FaultMessage != "" {
337- span .SetStatus (codes .Error , apiErr .FaultMessage )
353+ if shouldTrace {
354+ span .SetAttributes (attribute .Int ("http.status_code" , resp .StatusCode ))
355+ span .SetStatus (codes .Error , apiErr .FaultMessage )
356+ }
338357 return nil , fmt .Errorf ("status %d: %s" , resp .StatusCode , apiErr .FaultMessage )
339358 }
340359 }
341- span .SetStatus (codes .Error , resp .Status )
360+ if shouldTrace {
361+ span .SetAttributes (attribute .Int ("http.status_code" , resp .StatusCode ))
362+ span .SetStatus (codes .Error , resp .Status )
363+ }
342364 return nil , fmt .Errorf ("status %d: %s" , resp .StatusCode , string (data ))
343365}
344366
0 commit comments