Skip to content

Commit 00b01ef

Browse files
committed
fix: add more logs to help troubleshooting propagation of OpenTelemetry context from clients
1 parent d924ca4 commit 00b01ef

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

fdw.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"net"
2020
"net/http"
2121
"os"
22+
"strings"
2223
"time"
2324
"unsafe"
2425

@@ -177,11 +178,23 @@ func goFdwGetRelSize(state *C.FdwPlanState, root *C.PlannerInfo, rows *C.double,
177178
if state.trace_context_string != nil {
178179
traceContext = C.GoString(state.trace_context_string)
179180
log.Printf("[TRACE] Extracted trace context from session: %s", traceContext)
181+
182+
if len(traceContext) > 0 {
183+
log.Printf("[DEBUG] Trace context length: %d characters", len(traceContext))
184+
if strings.Contains(traceContext, "traceparent=") {
185+
log.Printf("[DEBUG] Trace context contains traceparent field")
186+
} else {
187+
log.Printf("[WARN] Trace context missing traceparent field - may be malformed")
188+
}
189+
}
190+
} else {
191+
log.Printf("[DEBUG] No trace context found in session variables")
180192
}
181193

182194
// Add trace context to options for hub layer
183195
if traceContext != "" {
184196
tableOpts["trace_context"] = traceContext
197+
log.Printf("[DEBUG] Added trace context to table options")
185198
}
186199

187200
// build columns

hub/hub_base.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,15 @@ func (h *hubBase) traceContextForScan(table string, columns []string, limit int6
379379

380380
// Check if we have trace context from session variables
381381
if traceContextStr, exists := opts["trace_context"]; exists && traceContextStr != "" {
382+
log.Printf("[DEBUG] traceContextForScan received trace context: %s", traceContextStr)
382383
if parentCtx := h.parseTraceContext(traceContextStr); parentCtx != nil {
383384
baseCtx = parentCtx
384385
log.Printf("[TRACE] Using parent trace context for scan of table: %s", table)
386+
} else {
387+
log.Printf("[WARN] Failed to parse trace context for table: %s", table)
385388
}
389+
} else {
390+
log.Printf("[DEBUG] No trace context found in options for table: %s", table)
386391
}
387392

388393
// Create span with potentially propagated context
@@ -396,28 +401,45 @@ func (h *hubBase) traceContextForScan(table string, columns []string, limit int6
396401
if limit != -1 {
397402
span.SetAttributes(attribute.Int64("limit", limit))
398403
}
404+
405+
spanCtx := span.SpanContext()
406+
if spanCtx.IsValid() {
407+
log.Printf("[DEBUG] Created span for table %s - TraceID: %s, SpanID: %s",
408+
table, spanCtx.TraceID().String(), spanCtx.SpanID().String())
409+
}
410+
399411
return &telemetry.TraceCtx{Ctx: ctx, Span: span}
400412
}
401413

402414
// parseTraceContext parses trace context string from session variables
403415
// Format: "traceparent=00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01;tracestate=rojo=00f067aa0ba902b7"
404416
func (h *hubBase) parseTraceContext(traceContextString string) context.Context {
417+
log.Printf("[DEBUG] parseTraceContext called with: %s", traceContextString)
418+
405419
if traceContextString == "" {
420+
log.Printf("[DEBUG] Empty trace context string")
406421
return nil
407422
}
408423

409424
carrier := propagation.MapCarrier{}
410425

411426
// Parse the trace context string format: "traceparent=..;tracestate=.."
412427
parts := strings.Split(traceContextString, ";")
428+
log.Printf("[DEBUG] Split trace context into %d parts: %v", len(parts), parts)
429+
413430
for _, part := range parts {
414431
if kv := strings.SplitN(part, "=", 2); len(kv) == 2 {
415432
key := strings.TrimSpace(kv[0])
416433
value := strings.TrimSpace(kv[1])
417434
carrier[key] = value
435+
log.Printf("[DEBUG] Added to carrier: %s = %s", key, value)
436+
} else {
437+
log.Printf("[DEBUG] Skipping invalid part: %s", part)
418438
}
419439
}
420440

441+
log.Printf("[DEBUG] Final carrier contents: %v", carrier)
442+
421443
if len(carrier) == 0 {
422444
log.Printf("[WARN] No valid trace context found in: %s", traceContextString)
423445
return nil
@@ -438,7 +460,7 @@ func (h *hubBase) parseTraceContext(traceContextString string) context.Context {
438460
return extractedCtx
439461
}
440462

441-
log.Printf("[WARN] Extracted trace context is not valid")
463+
log.Printf("[WARN] Extracted trace context is not valid - carrier was: %v", carrier)
442464
return nil
443465
}
444466

hub/scan_iterator_base.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/turbot/steampipe-plugin-sdk/v5/telemetry"
1717
"github.com/turbot/steampipe-postgres-fdw/v2/types"
1818
"github.com/turbot/steampipe/v2/pkg/query/queryresult"
19+
"go.opentelemetry.io/otel/trace"
1920
"google.golang.org/protobuf/reflect/protoreflect"
2021
)
2122

@@ -199,13 +200,25 @@ func (i *scanIteratorBase) GetScanMetadata() []queryresult.ScanMetadataRow {
199200
}
200201

201202
func (i *scanIteratorBase) newExecuteRequest() *proto.ExecuteRequest {
203+
traceCarrier := grpc.CreateCarrierFromContext(i.traceCtx.Ctx)
204+
log.Printf("[DEBUG] newExecuteRequest creating trace carrier for table %s: %v", i.table, traceCarrier)
205+
206+
// Validate span context from the trace context
207+
spanCtx := trace.SpanContextFromContext(i.traceCtx.Ctx)
208+
if spanCtx.IsValid() {
209+
log.Printf("[DEBUG] newExecuteRequest has valid span context - TraceID: %s, SpanID: %s",
210+
spanCtx.TraceID().String(), spanCtx.SpanID().String())
211+
} else {
212+
log.Printf("[WARN] newExecuteRequest has invalid span context for table %s", i.table)
213+
}
214+
202215
req := &proto.ExecuteRequest{
203216
Table: i.table,
204217
QueryContext: i.queryContext,
205218
CallId: i.callId,
206219
// pass connection name - used for aggregators
207220
Connection: i.connectionName,
208-
TraceContext: grpc.CreateCarrierFromContext(i.traceCtx.Ctx),
221+
TraceContext: traceCarrier,
209222
ExecuteConnectionData: make(map[string]*proto.ExecuteConnectionData),
210223
}
211224

0 commit comments

Comments
 (0)