-
Notifications
You must be signed in to change notification settings - Fork 331
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b80ad66
Add a b3tracecontext.HTTPFormat.
Harwayne d99f4a6
hack/update-deps.sh
Harwayne 6982f09
PR comments.
Harwayne ba1f6d9
Move to HTTPFormatSequence.
Harwayne cb68763
Remove the struct.
Harwayne 8a25d03
Allow distinct ingress and egress formats.
Harwayne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}, | ||
}, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.