|
| 1 | +package adapter |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info" |
| 9 | + testtracing "github.com/openshift-pipelines/pipelines-as-code/pkg/test/tracing" |
| 10 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/tracing" |
| 11 | + "go.opentelemetry.io/otel" |
| 12 | + "go.opentelemetry.io/otel/propagation" |
| 13 | + sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 14 | + semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 15 | + "go.opentelemetry.io/otel/trace" |
| 16 | + "gotest.tools/v3/assert" |
| 17 | +) |
| 18 | + |
| 19 | +func TestSetVCSSpanAttributes(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + eventTypeKey := string(tracing.PACEventTypeKey) |
| 23 | + repoURLKey := string(semconv.VCSRepositoryURLFullKey) |
| 24 | + headRevKey := string(semconv.VCSRefHeadRevisionKey) |
| 25 | + |
| 26 | + tests := []struct { |
| 27 | + name string |
| 28 | + event *info.Event |
| 29 | + want map[string]string |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "full event", |
| 33 | + event: &info.Event{ |
| 34 | + EventType: "pull_request", |
| 35 | + URL: "https://github.com/test/repo", |
| 36 | + SHA: "abc123", |
| 37 | + }, |
| 38 | + want: map[string]string{ |
| 39 | + eventTypeKey: "pull_request", |
| 40 | + repoURLKey: "https://github.com/test/repo", |
| 41 | + headRevKey: "abc123", |
| 42 | + }, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "event type only", |
| 46 | + event: &info.Event{ |
| 47 | + EventType: "push", |
| 48 | + }, |
| 49 | + want: map[string]string{ |
| 50 | + eventTypeKey: "push", |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "url without sha", |
| 55 | + event: &info.Event{ |
| 56 | + EventType: "issue_comment", |
| 57 | + URL: "https://github.com/test/repo", |
| 58 | + }, |
| 59 | + want: map[string]string{ |
| 60 | + eventTypeKey: "issue_comment", |
| 61 | + repoURLKey: "https://github.com/test/repo", |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + t.Parallel() |
| 69 | + |
| 70 | + exporter := &testtracing.RecordingExporter{} |
| 71 | + tp := sdktrace.NewTracerProvider( |
| 72 | + sdktrace.WithSampler(sdktrace.AlwaysSample()), |
| 73 | + sdktrace.WithSyncer(exporter), |
| 74 | + ) |
| 75 | + defer func() { _ = tp.Shutdown(context.Background()) }() |
| 76 | + |
| 77 | + ctx, span := tp.Tracer("test").Start(context.Background(), "test-span") |
| 78 | + setVCSSpanAttributes(ctx, tt.event) |
| 79 | + span.End() |
| 80 | + |
| 81 | + spans := exporter.GetSpans() |
| 82 | + assert.Equal(t, len(spans), 1) |
| 83 | + got := map[string]string{} |
| 84 | + for _, a := range spans[0].Attributes() { |
| 85 | + got[string(a.Key)] = a.Value.AsString() |
| 86 | + } |
| 87 | + assert.DeepEqual(t, got, tt.want) |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestProcessEventSpanHonorsIncomingTraceContext(t *testing.T) { |
| 93 | + exporter := testtracing.SetupTracer(t) |
| 94 | + |
| 95 | + // Simulate an external system sending a webhook with a traceparent header. |
| 96 | + // Create a parent span to generate a valid trace context. |
| 97 | + parentCtx, parentSpan := otel.Tracer("external-system").Start(context.Background(), "external-root") |
| 98 | + expectedTraceID := parentSpan.SpanContext().TraceID() |
| 99 | + parentSpan.End() |
| 100 | + |
| 101 | + // Inject the parent context into HTTP headers (what the webhook sender would do). |
| 102 | + req, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, "http://localhost", nil) |
| 103 | + otel.GetTextMapPropagator().Inject(parentCtx, propagation.HeaderCarrier(req.Header)) |
| 104 | + |
| 105 | + // This is the exact extract → start sequence from handleEvent. |
| 106 | + tracedCtx := otel.GetTextMapPropagator().Extract(context.Background(), propagation.HeaderCarrier(req.Header)) |
| 107 | + _, span := otel.Tracer(tracing.TracerName).Start(tracedCtx, "PipelinesAsCode:ProcessEvent", |
| 108 | + trace.WithSpanKind(trace.SpanKindServer), |
| 109 | + ) |
| 110 | + span.End() |
| 111 | + |
| 112 | + spans := exporter.GetSpans() |
| 113 | + var processSpan sdktrace.ReadOnlySpan |
| 114 | + for _, s := range spans { |
| 115 | + if s.Name() == "PipelinesAsCode:ProcessEvent" { |
| 116 | + processSpan = s |
| 117 | + } |
| 118 | + } |
| 119 | + assert.Assert(t, processSpan != nil, "ProcessEvent span not found") |
| 120 | + assert.Equal(t, processSpan.Parent().TraceID(), expectedTraceID, |
| 121 | + "ProcessEvent span should be parented under the incoming trace context, not a new root") |
| 122 | + assert.Assert(t, processSpan.Parent().IsValid(), |
| 123 | + "ProcessEvent span should have a valid remote parent") |
| 124 | +} |
| 125 | + |
| 126 | +func TestProcessEventSpanCreatesRootWithoutIncomingContext(t *testing.T) { |
| 127 | + exporter := testtracing.SetupTracer(t) |
| 128 | + |
| 129 | + // Webhook with no traceparent header. |
| 130 | + req, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, "http://localhost", nil) |
| 131 | + |
| 132 | + tracedCtx := otel.GetTextMapPropagator().Extract(context.Background(), propagation.HeaderCarrier(req.Header)) |
| 133 | + _, span := otel.Tracer(tracing.TracerName).Start(tracedCtx, "PipelinesAsCode:ProcessEvent", |
| 134 | + trace.WithSpanKind(trace.SpanKindServer), |
| 135 | + ) |
| 136 | + span.End() |
| 137 | + |
| 138 | + spans := exporter.GetSpans() |
| 139 | + processSpan := testtracing.FindSpan(spans, "PipelinesAsCode:ProcessEvent") |
| 140 | + assert.Assert(t, processSpan != nil) |
| 141 | + assert.Assert(t, !processSpan.Parent().IsValid(), |
| 142 | + "ProcessEvent span should be a root when no incoming trace context is present") |
| 143 | +} |
0 commit comments