Source File
tracestate.go
Belonging Package
go.opencensus.io/trace/tracestate
package tracestate
import (
)
const (
keyMaxSize = 256
valueMaxSize = 256
maxKeyValuePairs = 32
)
const (
keyWithoutVendorFormat = `[a-z][_0-9a-z\-\*\/]{0,255}`
keyWithVendorFormat = `[a-z][_0-9a-z\-\*\/]{0,240}@[a-z][_0-9a-z\-\*\/]{0,13}`
keyFormat = `(` + keyWithoutVendorFormat + `)|(` + keyWithVendorFormat + `)`
valueFormat = `[\x20-\x2b\x2d-\x3c\x3e-\x7e]{0,255}[\x21-\x2b\x2d-\x3c\x3e-\x7e]`
)
var keyValidationRegExp = regexp.MustCompile(`^(` + keyFormat + `)$`)
var valueValidationRegExp = regexp.MustCompile(`^(` + valueFormat + `)$`)
type Tracestate struct {
entries []Entry
}
func ( *Tracestate) () []Entry {
if == nil {
return nil
}
return .entries
}
func ( *Tracestate) ( string) *Entry {
for , := range .entries {
if .Key == {
.entries = append(.entries[:], .entries[+1:]...)
return &
}
}
return nil
}
func ( *Tracestate) ( []Entry) error {
for , := range {
.remove(.Key)
}
if len(.entries)+len() > maxKeyValuePairs {
return fmt.Errorf("adding %d key-value pairs to current %d pairs exceeds the limit of %d",
len(), len(.entries), maxKeyValuePairs)
}
.entries = append(, .entries...)
return nil
}
func ( Entry) bool {
return keyValidationRegExp.MatchString(.Key) &&
valueValidationRegExp.MatchString(.Value)
}
func ( ...Entry) (string, bool) {
:= make(map[string]int)
for , := range {
if , := [.Key]; {
return .Key, true
}
[.Key] = 1
}
return "", false
}
func ( ...Entry) (*Entry, bool) {
for , := range {
if !isValid() {
return &, false
}
}
return nil, true
}
func ( *Tracestate, ...Entry) (*Tracestate, error) {
if == nil && len() == 0 {
return nil, nil
}
if , := areEntriesValid(...); ! {
return nil, fmt.Errorf("key-value pair {%s, %s} is invalid", .Key, .Value)
}
if , := containsDuplicateKey(...); {
return nil, fmt.Errorf("contains duplicate keys (%s)", )
}
:= Tracestate{}
if != nil && len(.entries) > 0 {
.entries = append([]Entry{}, .entries...)
}
:= .add()
if != nil {
return nil,
}
return &, nil
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |