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