1 // Copyright 2018, OpenCensus Authors
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // Package tracestate implements support for the Tracestate header of the
16 // W3C TraceContext propagation format.
31 keyWithoutVendorFormat = `[a-z][_0-9a-z\-\*\/]{0,255}`
32 keyWithVendorFormat = `[a-z][_0-9a-z\-\*\/]{0,240}@[a-z][_0-9a-z\-\*\/]{0,13}`
33 keyFormat = `(` + keyWithoutVendorFormat + `)|(` + keyWithVendorFormat + `)`
34 valueFormat = `[\x20-\x2b\x2d-\x3c\x3e-\x7e]{0,255}[\x21-\x2b\x2d-\x3c\x3e-\x7e]`
37 var keyValidationRegExp = regexp.MustCompile(`^(` + keyFormat + `)$`)
38 var valueValidationRegExp = regexp.MustCompile(`^(` + valueFormat + `)$`)
40 // Tracestate represents tracing-system specific context in a list of key-value pairs. Tracestate allows different
41 // vendors propagate additional information and inter-operate with their legacy Id formats.
42 type Tracestate struct {
46 // Entry represents one key-value pair in a list of key-value pair of Tracestate.
48 // Key is an opaque string up to 256 characters printable. It MUST begin with a lowercase letter,
49 // and can only contain lowercase letters a-z, digits 0-9, underscores _, dashes -, asterisks *, and
53 // Value is an opaque string up to 256 characters printable ASCII RFC0020 characters (i.e., the
54 // range 0x20 to 0x7E) except comma , and =.
58 // Entries returns a slice of Entry.
59 func (ts *Tracestate) Entries() []Entry {
66 func (ts *Tracestate) remove(key string) *Entry {
67 for index, entry := range ts.entries {
69 ts.entries = append(ts.entries[:index], ts.entries[index+1:]...)
76 func (ts *Tracestate) add(entries []Entry) error {
77 for _, entry := range entries {
80 if len(ts.entries)+len(entries) > maxKeyValuePairs {
81 return fmt.Errorf("adding %d key-value pairs to current %d pairs exceeds the limit of %d",
82 len(entries), len(ts.entries), maxKeyValuePairs)
84 ts.entries = append(entries, ts.entries...)
88 func isValid(entry Entry) bool {
89 return keyValidationRegExp.MatchString(entry.Key) &&
90 valueValidationRegExp.MatchString(entry.Value)
93 func containsDuplicateKey(entries ...Entry) (string, bool) {
94 keyMap := make(map[string]int)
95 for _, entry := range entries {
96 if _, ok := keyMap[entry.Key]; ok {
97 return entry.Key, true
104 func areEntriesValid(entries ...Entry) (*Entry, bool) {
105 for _, entry := range entries {
113 // New creates a Tracestate object from a parent and/or entries (key-value pair).
114 // Entries from the parent are copied if present. The entries passed to this function
115 // are inserted in front of those copied from the parent. If an entry copied from the
116 // parent contains the same key as one of the entry in entries then the entry copied
117 // from the parent is removed. See add func.
119 // An error is returned with nil Tracestate if
120 // 1. one or more entry in entries is invalid.
121 // 2. two or more entries in the input entries have the same key.
122 // 3. the number of entries combined from the parent and the input entries exceeds maxKeyValuePairs.
123 // (duplicate entry is counted only once).
124 func New(parent *Tracestate, entries ...Entry) (*Tracestate, error) {
125 if parent == nil && len(entries) == 0 {
128 if entry, ok := areEntriesValid(entries...); !ok {
129 return nil, fmt.Errorf("key-value pair {%s, %s} is invalid", entry.Key, entry.Value)
132 if key, duplicate := containsDuplicateKey(entries...); duplicate {
133 return nil, fmt.Errorf("contains duplicate keys (%s)", key)
136 tracestate := Tracestate{}
138 if parent != nil && len(parent.entries) > 0 {
139 tracestate.entries = append([]Entry{}, parent.entries...)
142 err := tracestate.add(entries)
146 return &tracestate, nil