-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(trace): remove unused code and simplify carrier interface
feat(trace): add support for binary trace context in carrier interface to enable cross-language tracing fix(trace): fix header names to conform to W3C Trace Context specification feat(trace/w3c.go): add functions to convert SpanContext and TraceState to and from W3C format to enable cross-language tracing
- Loading branch information
1 parent
665df36
commit 0707149
Showing
7 changed files
with
175 additions
and
102 deletions.
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
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,48 @@ | ||
package trace | ||
|
||
import "go.opentelemetry.io/otel/trace" | ||
|
||
// BinaryFromSpanContext returns the binary format representation of a SpanContext. | ||
// | ||
// If sc is the zero value, Binary returns nil. | ||
func BinaryFromSpanContext(sc trace.SpanContext) []byte { | ||
traceID := sc.TraceID() | ||
spanID := sc.SpanID() | ||
traceFlags := sc.TraceFlags() | ||
if sc.Equal(emptySpanContext) { | ||
return nil | ||
} | ||
var b [29]byte | ||
copy(b[2:18], traceID[:]) | ||
b[18] = 1 | ||
copy(b[19:27], spanID[:]) | ||
b[27] = 2 | ||
b[28] = uint8(traceFlags) | ||
return b[:] | ||
} | ||
|
||
// SpanContextFromBinary returns the SpanContext represented by b. | ||
// | ||
// If b has an unsupported version ID or contains no TraceID, SpanContextFromBinary returns with ok==false. | ||
func SpanContextFromBinary(b []byte) (sc trace.SpanContext, ok bool) { | ||
var scConfig trace.SpanContextConfig | ||
if len(b) == 0 || b[0] != 0 { | ||
return trace.SpanContext{}, false | ||
} | ||
b = b[1:] | ||
if len(b) >= 17 && b[0] == 0 { | ||
copy(scConfig.TraceID[:], b[1:17]) | ||
b = b[17:] | ||
} else { | ||
return trace.SpanContext{}, false | ||
} | ||
if len(b) >= 9 && b[0] == 1 { | ||
copy(scConfig.SpanID[:], b[1:9]) | ||
b = b[9:] | ||
} | ||
if len(b) >= 2 && b[0] == 2 { | ||
scConfig.TraceFlags = trace.TraceFlags(b[1]) | ||
} | ||
sc = trace.NewSpanContext(scConfig) | ||
return sc, true | ||
} |
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,16 @@ | ||
package trace | ||
|
||
type Carrier interface { | ||
Get(key string) string | ||
Set(key, value string) | ||
} | ||
|
||
type MapCarrier map[string]string | ||
|
||
func (c MapCarrier) Get(key string) string { | ||
return c[key] | ||
} | ||
|
||
func (c MapCarrier) Set(key, value string) { | ||
c[key] = value | ||
} |
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