Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Disconnected Spans exported for Remote-log-url #1280

Merged
merged 14 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/gofr/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Exporter struct {
logger logging.Logger // Logger for logging errors and other messages.
}

const SQLConnectTrace = "sql.connector.connect"

// NewExporter creates a new Exporter instance with a custom endpoint and logger.
func NewExporter(endpoint string, logger logging.Logger) *Exporter {
return &Exporter{
Expand Down Expand Up @@ -96,6 +98,10 @@ func convertSpans(spans []sdktrace.ReadOnlySpan) []Span {
convertedSpans := make([]Span, 0, len(spans))

for i, s := range spans {
if s.Name() == SQLConnectTrace {
continue
}

convertedSpan := Span{
TraceID: s.SpanContext().TraceID().String(),
ID: s.SpanContext().SpanID().String(),
Expand Down
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
Loading