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

Add tracecontextb3 HTTPFormats #1429

Merged
merged 6 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions test/spoof/spoof.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import (
"knative.dev/pkg/test/ingress"
"knative.dev/pkg/test/logging"
"knative.dev/pkg/test/zipkin"
"knative.dev/pkg/tracing/propagation/tracecontextb3"

"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/plugin/ochttp/propagation/b3"
"go.opencensus.io/trace"
)

Expand Down Expand Up @@ -135,7 +135,7 @@ func New(
// Enable Zipkin tracing
roundTripper := &ochttp.Transport{
Base: transport,
Propagation: &b3.HTTPFormat{},
Propagation: tracecontextb3.TraceContextEgress,
}

sc := SpoofingClient{
Expand Down
2 changes: 2 additions & 0 deletions tracing/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/trace"
"k8s.io/apimachinery/pkg/util/sets"
"knative.dev/pkg/tracing/propagation/tracecontextb3"
)

var (
Expand Down Expand Up @@ -50,6 +51,7 @@ func HTTPSpanIgnoringPaths(pathsToIgnore ...string) func(http.Handler) http.Hand
}
return underlyingSampling
},
Propagation: tracecontextb3.TraceContextEgress,
}
}
}
53 changes: 53 additions & 0 deletions tracing/propagation/http_format_sequence.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2020 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package propagation

import (
"net/http"

"go.opencensus.io/trace"
"go.opencensus.io/trace/propagation"
)

// HTTPFormatSequence is a propagation.HTTPFormat that applies multiple other propagation formats.
// For incoming requests, it will use the first SpanContext it can find, checked in the order of
// HTTPFormatSequence.Ingress.
// For outgoing requests, it will apply all the formats to the outgoing request, in the order of
// HTTPFormatSequence.Egress.
type HTTPFormatSequence struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The struct isn't necessary here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, just inline with
type HHFS []proo.HTTPFormat.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Ingress []propagation.HTTPFormat
Egress []propagation.HTTPFormat
}

var _ propagation.HTTPFormat = (*HTTPFormatSequence)(nil)

// SpanContextFromRequest satisfies the propagation.HTTPFormat interface.
func (h *HTTPFormatSequence) SpanContextFromRequest(req *http.Request) (trace.SpanContext, bool) {
for _, format := range h.Ingress {
if sc, ok := format.SpanContextFromRequest(req); ok {
return sc, true
}
}
return trace.SpanContext{}, false
}

// SpanContextToRequest satisfies the propagation.HTTPFormat interface.
func (h *HTTPFormatSequence) SpanContextToRequest(sc trace.SpanContext, req *http.Request) {
for _, format := range h.Egress {
format.SpanContextToRequest(sc, req)
}
}
150 changes: 150 additions & 0 deletions tracing/propagation/http_format_sequence_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
Copyright 2020 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package propagation

import (
"fmt"
"net/http"
"testing"

"github.com/google/go-cmp/cmp"
"go.opencensus.io/plugin/ochttp/propagation/b3"
"go.opencensus.io/plugin/ochttp/propagation/tracecontext"
"go.opencensus.io/trace"
"go.opencensus.io/trace/propagation"

_ "knative.dev/pkg/metrics/testing"
)

var (
sampled trace.TraceOptions = 1
notSampled trace.TraceOptions = 1

traceID = trace.TraceID{99, 108, 97, 101, 114, 115, 105, 103, 104, 116, 101, 100, 110, 101, 115, 115}
spanID = trace.SpanID{107, 110, 97, 116, 105, 118, 101, 0}

tracePropagators = []propagation.HTTPFormat{
&b3.HTTPFormat{},
&tracecontext.HTTPFormat{},
both,
}
)

var both = &HTTPFormatSequence{
Ingress: []propagation.HTTPFormat{
&tracecontext.HTTPFormat{},
&b3.HTTPFormat{},
},
Egress: []propagation.HTTPFormat{
&tracecontext.HTTPFormat{},
&b3.HTTPFormat{},
},
}

func TestSpanContextFromRequest(t *testing.T) {
testCases := map[string]struct {
sc *trace.SpanContext
}{
"no incoming trace": {},
"not sampled": {
sc: &trace.SpanContext{
TraceID: traceID,
SpanID: spanID,
TraceOptions: notSampled,
},
},
"sampled": {
sc: &trace.SpanContext{
TraceID: traceID,
SpanID: spanID,
TraceOptions: sampled,
},
},
}

for _, tracePropagator := range tracePropagators {
for n, tc := range testCases {
t.Run(fmt.Sprintf("%T-%s", tracePropagator, n), func(t *testing.T) {
r := &http.Request{}
r.Header = http.Header{}
if tc.sc != nil {
tracePropagator.SpanContextToRequest(*tc.sc, r)
}
// Check we extract the correct SpanContext with both the original and the
// 'both' propagators.
for _, extractFormat := range []propagation.HTTPFormat{tracePropagator, both} {
actual, ok := extractFormat.SpanContextFromRequest(r)
if tc.sc == nil {
if ok {
t.Errorf("Expected no span context using %T, found %v", extractFormat, actual)
}
continue
}
if diff := cmp.Diff(*tc.sc, actual); diff != "" {
t.Errorf("Unexpected span context using %T (-want +got): %s", extractFormat, diff)
}
}
})
}
}
}

func TestSpanContextToRequest(t *testing.T) {
testCases := map[string]struct {
sc *trace.SpanContext
}{
"no incoming trace": {},
"not sampled": {
sc: &trace.SpanContext{
TraceID: traceID,
SpanID: spanID,
TraceOptions: notSampled,
},
},
"sampled": {
sc: &trace.SpanContext{
TraceID: traceID,
SpanID: spanID,
TraceOptions: sampled,
},
},
}

for n, tc := range testCases {
t.Run(n, func(t *testing.T) {
r := &http.Request{}
r.Header = http.Header{}
if tc.sc != nil {
// Apply using the TraceContextB3 propagator.
both.SpanContextToRequest(*tc.sc, r)
}
// Verify that we extract the correct SpanContext with all three formats.
for _, tracePropagator := range tracePropagators {
actual, ok := tracePropagator.SpanContextFromRequest(r)
if tc.sc == nil {
if ok {
t.Errorf("Expected no span context using %T, found %v", tracePropagator, actual)
}
continue
}
if diff := cmp.Diff(*tc.sc, actual); diff != "" {
t.Errorf("Unexpected span context using %T (-want +got): %s", tracePropagator, diff)
}
}
})
}
}
61 changes: 61 additions & 0 deletions tracing/propagation/tracecontextb3/http_format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2020 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package tracecontextb3

import (
"go.opencensus.io/plugin/ochttp/propagation/b3"
"go.opencensus.io/plugin/ochttp/propagation/tracecontext"
ocpropagation "go.opencensus.io/trace/propagation"
"knative.dev/pkg/tracing/propagation"
)

// TraceContextB3Egress is a propagation.HTTPFormat that reads both TraceContext and B3 tracing
// formats, preferring TraceContext. It always writes both formats.
var TraceContextB3Egress = &propagation.HTTPFormatSequence{
Ingress: []ocpropagation.HTTPFormat{
&tracecontext.HTTPFormat{},
&b3.HTTPFormat{},
},
Egress: []ocpropagation.HTTPFormat{
&tracecontext.HTTPFormat{},
&b3.HTTPFormat{},
},
}

// TraceContextEgress is a propagation.HTTPFormat that reads both TraceContext and B3 tracing
// formats, preferring TraceContext. It always writes TraceContext format exclusively.
var TraceContextEgress = &propagation.HTTPFormatSequence{
Ingress: []ocpropagation.HTTPFormat{
&tracecontext.HTTPFormat{},
&b3.HTTPFormat{},
},
Egress: []ocpropagation.HTTPFormat{
&tracecontext.HTTPFormat{},
},
}

// B3Egress is a propagation.HTTPFormat that reads both TraceContext and B3 tracing formats,
// preferring TraceContext. It always writes B3 format exclusively.
var B3Egress = &propagation.HTTPFormatSequence{
Ingress: []ocpropagation.HTTPFormat{
&tracecontext.HTTPFormat{},
&b3.HTTPFormat{},
},
Egress: []ocpropagation.HTTPFormat{
&b3.HTTPFormat{},
},
}
Loading