Skip to content

Commit

Permalink
Refactor tracing helper to be reusable (#6469)
Browse files Browse the repository at this point in the history
* Remove the Setup function from tracing package so that it
can be vendored. Vendoring the Setup function might cause
errors with reconciler-test framework because some flags from
knative/pkg are "redefined".
* Split WithHTTPHostAndPath into WithHTTPHostAndPath and WithHTTPURL.
The first one adds http.host and http.path. The second one adds
http.url. This separation allows using one or the other regardless of
Kind (Client/Server/etc.)
* MatchHTTPSpanNoReply now passes the options to the underlying
MatchHTTPSpanWithCode in the same was as MatchHTTPSpanWithReply
* Created more generic functions WithCode and MatchSpan

* Fix lint S1001: should use copy() instead of a loop
  • Loading branch information
mgencur authored Aug 2, 2022
1 parent 297b8af commit 359f0e2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 57 deletions.
4 changes: 1 addition & 3 deletions pkg/adapter/v2/test/test_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ func (c *TestCloudEventsClient) Sent() []cloudevents.Event {
c.lock.Lock()
defer c.lock.Unlock()
r := make([]cloudevents.Event, len(c.sent))
for i := range c.sent {
r[i] = c.sent[i]
}
copy(r, c.sent)
return r
}

Expand Down
4 changes: 1 addition & 3 deletions pkg/channel/fanout/fanout_message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ func NewFanoutMessageHandler(logger *zap.Logger, messageDispatcher channel.Messa
asyncHandler: config.AsyncHandler,
}
handler.subscriptions = make([]Subscription, len(config.Subscriptions))
for i := range config.Subscriptions {
handler.subscriptions[i] = config.Subscriptions[i]
}
copy(handler.subscriptions, config.Subscriptions)
// The receiver function needs to point back at the handler itself, so set it up after
// initialization.
receiver, err := channel.NewMessageReceiver(createMessageReceiverFunction(handler), logger, reporter)
Expand Down
8 changes: 4 additions & 4 deletions test/conformance/helpers/channel_tracing_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func setupChannelTracingWithReply(
// 4. Channel sends event to Mutator pod.
Span: tracinghelper.MatchHTTPSpanWithReply(
model.Client,
tracinghelper.WithHTTPHostAndPath(
tracinghelper.WithHTTPURL(
fmt.Sprintf("%s.%s.svc", mutatingPod.Name, client.Namespace),
"",
),
Expand All @@ -181,7 +181,7 @@ func setupChannelTracingWithReply(
// 7. Channel sends reply from Mutator Pod to the reply Channel.
Span: tracinghelper.MatchHTTPSpanNoReply(
model.Client,
tracinghelper.WithHTTPHostAndPath(
tracinghelper.WithHTTPURL(
fmt.Sprintf("%s-kn-channel.%s.svc", replyChannelName, client.Namespace),
"",
),
Expand All @@ -205,7 +205,7 @@ func setupChannelTracingWithReply(
// 10. Reply Channel sends event to the logging Pod.
Span: tracinghelper.MatchHTTPSpanNoReply(
model.Client,
tracinghelper.WithHTTPHostAndPath(
tracinghelper.WithHTTPURL(
fmt.Sprintf("%s.%s.svc", recordEventsPod.Name, client.Namespace),
"",
),
Expand Down Expand Up @@ -242,7 +242,7 @@ func setupChannelTracingWithReply(
// 1. Sending pod sends event to Channel (only if the sending pod generates a span).
Span: tracinghelper.MatchHTTPSpanNoReply(
model.Client,
tracinghelper.WithHTTPHostAndPath(
tracinghelper.WithHTTPURL(
fmt.Sprintf("%s-kn-channel.%s.svc", channelName, client.Namespace),
"",
),
Expand Down
36 changes: 22 additions & 14 deletions test/conformance/helpers/tracing/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,15 @@ func WithLocalEndpointServiceName(s string) SpanMatcherOption {
}

func WithHTTPHostAndPath(host, path string) SpanMatcherOption {
return func(m *SpanMatcher) {
m.Tags["http.host"] = regexp.MustCompile("^" + regexp.QuoteMeta(host) + HostSuffix + "$")
m.Tags["http.path"] = regexp.MustCompile("^" + regexp.QuoteMeta(path) + "$")
}
}

func WithHTTPURL(host, path string) SpanMatcherOption {
return func(m *SpanMatcher) {
if m.Kind != nil {
if *m.Kind == model.Client {
m.Tags["http.url"] = regexp.MustCompile("^http://" + regexp.QuoteMeta(host) + HostSuffix + regexp.QuoteMeta(path) + "$")
} else if *m.Kind == model.Server {
m.Tags["http.host"] = regexp.MustCompile("^" + regexp.QuoteMeta(host) + HostSuffix + "$")
m.Tags["http.path"] = regexp.MustCompile("^" + regexp.QuoteMeta(path) + "$")
}
}
m.Tags["http.url"] = regexp.MustCompile("^http://" + regexp.QuoteMeta(host) + HostSuffix + regexp.QuoteMeta(path) + "$")
}
}

Expand Down Expand Up @@ -110,22 +109,31 @@ func (m *SpanMatcher) MatchesSpan(span *model.SpanModel) error {
return nil
}

func MatchHTTPSpanWithCode(kind model.Kind, statusCode int, opts ...SpanMatcherOption) *SpanMatcher {
func MatchSpan(kind model.Kind, opts ...SpanMatcherOption) *SpanMatcher {
m := &SpanMatcher{
Kind: &kind,
Tags: map[string]*regexp.Regexp{
"http.method": regexp.MustCompile("^" + http.MethodPost + "$"),
"http.status_code": regexp.MustCompile("^" + strconv.Itoa(statusCode) + "$"),
},
}
for _, opt := range opts {
opt(m)
}
return m
}

func MatchHTTPSpanWithCode(kind model.Kind, statusCode int, opts ...SpanMatcherOption) *SpanMatcher {
return MatchSpan(kind, WithCode(statusCode))
}

func WithCode(statusCode int) SpanMatcherOption {
return func(m *SpanMatcher) {
m.Tags = map[string]*regexp.Regexp{
"http.method": regexp.MustCompile("^" + http.MethodPost + "$"),
"http.status_code": regexp.MustCompile("^" + strconv.Itoa(statusCode) + "$"),
}
}
}

func MatchHTTPSpanNoReply(kind model.Kind, opts ...SpanMatcherOption) *SpanMatcher {
return MatchHTTPSpanWithCode(kind, 202)
return MatchHTTPSpanWithCode(kind, 202, opts...)
}

func MatchHTTPSpanWithReply(kind model.Kind, opts ...SpanMatcherOption) *SpanMatcher {
Expand Down
32 changes: 0 additions & 32 deletions test/conformance/helpers/tracing/zipkin.go

This file was deleted.

3 changes: 2 additions & 1 deletion test/conformance/helpers/tracing_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/system"
"knative.dev/pkg/test/zipkin"

tracinghelper "knative.dev/eventing/test/conformance/helpers/tracing"
Expand Down Expand Up @@ -61,7 +62,7 @@ func tracingTest(

// Do NOT call zipkin.CleanupZipkinTracingSetup. That will be called exactly once in
// TestMain.
tracinghelper.Setup(t, client)
zipkin.SetupZipkinTracingFromConfigTracingOrFail(context.Background(), t, client.Kube, system.Namespace())

// Start the event info store. Note this is done _before_ we setup the infrastructure, which
// sends the event.
Expand Down

0 comments on commit 359f0e2

Please sign in to comment.