Skip to content
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 APIs for tracestate. #81

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/core/span_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type SpanContext struct {
TraceID TraceID
SpanID uint64
TraceOptions byte
Tracestate *Tracestate
}

var (
Expand Down
140 changes: 140 additions & 0 deletions api/core/tracestate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright 2019, OpenTelemetry 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 tracestate implements support for the Tracestate header of the
// W3C TraceContext propagation format.
package core

import (
"fmt"
"regexp"
)

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 + `)$`)

// Tracestate represents tracing-system specific context in a list of key-value pairs. Tracestate allows different
// vendors propagate additional information and inter-operate with their legacy Id formats.
type Tracestate struct {
// Key is an opaque string up to 256 characters printable. It MUST begin with a lowercase letter,
// and can only contain lowercase letters a-z, digits 0-9, underscores _, dashes -, asterisks *, and
// forward slashes /.
// Value is an opaque string up to 256 characters printable ASCII RFC0020 characters (i.e., the
// range 0x20 to 0x7E) except comma , and =.
entries []KeyValue
}

// Entries returns a slice of core.KeyValues.
func (ts *Tracestate) Entries() []KeyValue {
if ts == nil {
return nil
}
return ts.entries
}

func (ts *Tracestate) remove(key string) *KeyValue {
for index, entry := range ts.entries {
if entry.Key.Variable.Name == key {
ts.entries = append(ts.entries[:index], ts.entries[index+1:]...)
return &entry
}
}
return nil
}

func (ts *Tracestate) add(entries []KeyValue) error {
for _, entry := range entries {
ts.remove(entry.Key.Variable.Name)
}
if len(ts.entries)+len(entries) > maxKeyValuePairs {
return fmt.Errorf("adding %d key-value pairs to current %d pairs exceeds the limit of %d",
len(entries), len(ts.entries), maxKeyValuePairs)
}
ts.entries = append(entries, ts.entries...)
return nil
}

func isValid(entry KeyValue) bool {
return keyValidationRegExp.MatchString(entry.Key.Variable.Name) &&
valueValidationRegExp.MatchString(entry.Value.String)
}

func containsDuplicateKey(entries ...KeyValue) (string, bool) {
keyMap := make(map[string]int)
for _, entry := range entries {
if _, ok := keyMap[entry.Key.Variable.Name]; ok {
return entry.Key.Variable.Name, true
}
keyMap[entry.Key.Variable.Name] = 1
}
return "", false
}

func areEntriesValid(entries ...KeyValue) (*KeyValue, bool) {
for _, entry := range entries {
if !isValid(entry) {
return &entry, false
}
}
return nil, true
}

// New creates a Tracestate object from a parent and/or entries (key-value pair).
// Entries from the parent are copied if present. The entries passed to this function
// are inserted in front of those copied from the parent. If an entry copied from the
// parent contains the same key as one of the entry in entries then the entry copied
// from the parent is removed. See add func.
//
// An error is returned with nil Tracestate if
// 1. one or more entry in entries is invalid.
// 2. two or more entries in the input entries have the same key.
// 3. the number of entries combined from the parent and the input entries exceeds maxKeyValuePairs.
// (duplicate entry is counted only once).
func New(parent *Tracestate, entries ...KeyValue) (*Tracestate, error) {
if parent == nil && len(entries) == 0 {
return nil, nil
}
if entry, ok := areEntriesValid(entries...); !ok {
return nil, fmt.Errorf("key-value pair {%s, %s} is invalid", entry.Key.Variable.Name, entry.Value.String)
}

if key, duplicate := containsDuplicateKey(entries...); duplicate {
return nil, fmt.Errorf("contains duplicate keys (%s)", key)
}

tracestate := Tracestate{}

if parent != nil && len(parent.entries) > 0 {
tracestate.entries = append([]KeyValue{}, parent.entries...)
}

err := tracestate.add(entries)
if err != nil {
return nil, err
}
return &tracestate, nil
}
Loading