Skip to content

Commit

Permalink
Fix: Disconnected Spans exported for Remote-log-url (#1280)
Browse files Browse the repository at this point in the history
  • Loading branch information
coolwednesday authored Dec 13, 2024
1 parent b056e50 commit 2ebf567
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions pkg/gofr/service/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,14 @@ func (h *httpService) createAndSendRequest(ctx context.Context, method string, p
uri := h.url + "/" + path
uri = strings.TrimRight(uri, "/")

spanContext, span := h.Tracer.Start(ctx, uri)
ctx, span := h.Tracer.Start(ctx, uri)
defer span.End()

spanContext = httptrace.WithClientTrace(spanContext, otelhttptrace.NewClientTrace(ctx))
// Attach client-side trace handling for HTTP request.
clientTraceCtx := httptrace.WithClientTrace(ctx, otelhttptrace.NewClientTrace(ctx))

req, err := http.NewRequestWithContext(spanContext, method, uri, bytes.NewBuffer(body))
// Create the HTTP request with the tracing context.
req, err := http.NewRequestWithContext(clientTraceCtx, method, uri, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
Expand All @@ -161,25 +163,15 @@ func (h *httpService) createAndSendRequest(ctx context.Context, method string, p
req.Header.Set("Content-Type", "application/json")
}

// encode the query parameters on the request
encodeQueryParameters(req, queryParams)

if !trace.SpanFromContext(ctx).SpanContext().HasTraceID() {
// Start context and Tracing
ctx = req.Context()
// Inject tracing information into the request headers.
otel.GetTextMapPropagator().Inject(clientTraceCtx, propagation.HeaderCarrier(req.Header))

// extract the traceID and spanID from the headers and create a new context for the same
// this context will make a new span using the traceID and link the incoming SpanID as
// its parentID, thus connecting two spans
ctx = otel.GetTextMapPropagator().Extract(ctx, propagation.HeaderCarrier(req.Header))
}

// inject the TraceParent header manually in the request headers
otel.GetTextMapPropagator().Inject(spanContext, propagation.HeaderCarrier(req.Header))
// encode the query parameters on the request.
encodeQueryParameters(req, queryParams)

log := &Log{
Timestamp: time.Now(),
CorrelationID: trace.SpanFromContext(ctx).SpanContext().TraceID().String(),
CorrelationID: trace.SpanFromContext(clientTraceCtx).SpanContext().TraceID().String(),
HTTPMethod: method,
URI: uri,
}
Expand All @@ -196,12 +188,12 @@ func (h *httpService) createAndSendRequest(ctx context.Context, method string, p
log.ResponseCode = http.StatusInternalServerError
h.Log(&ErrorLog{Log: log, ErrorMessage: err.Error()})

h.updateMetrics(ctx, method, respTime.Seconds(), http.StatusInternalServerError)
h.updateMetrics(clientTraceCtx, method, respTime.Seconds(), http.StatusInternalServerError)

return resp, err
}

h.updateMetrics(ctx, method, respTime.Seconds(), resp.StatusCode)
h.updateMetrics(clientTraceCtx, method, respTime.Seconds(), resp.StatusCode)
log.ResponseCode = resp.StatusCode

h.Log(log)
Expand Down

0 comments on commit 2ebf567

Please sign in to comment.