-
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.
feat(interceptor.go): add logging of span context trace ID and span ID
refactor(connect_service.go): rename GetBalance method to Call to improve semantics feat(utils.go): add utility functions to convert span context to and from binary format for tracing purposes
- Loading branch information
1 parent
c993336
commit f8136b1
Showing
3 changed files
with
55 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package trace | ||
|
||
import "go.opentelemetry.io/otel/trace" | ||
|
||
var emptySpanContext trace.SpanContext | ||
|
||
// 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 | ||
} |