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

baggage: Improve performance #4743

Merged
merged 26 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Improve `go.opentelemetry.io/otel/trace.TraceState`'s performance. (#4722)
- Improve `go.opentelemetry.io/otel/propagation.TraceContext`'s performance. (#4721)
- Improve `go.opentelemetry.io/otel/baggage.NewMember`'s performance. (#4743)
pellared marked this conversation as resolved.
Show resolved Hide resolved

## [1.21.0/0.44.0] 2023-11-16

Expand Down
50 changes: 48 additions & 2 deletions baggage/baggage.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ const (
keyValueDelimiter = "="
propertyDelimiter = ";"

// if you update these 2 values you must update the static implementation below: keyReValidator and valReValidator.
cdvr1993 marked this conversation as resolved.
Show resolved Hide resolved
keyDef = `([\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5a\x5e-\x7a\x7c\x7e]+)`
valueDef = `([\x21\x23-\x2b\x2d-\x3a\x3c-\x5B\x5D-\x7e]*)`
keyValueDef = `\s*` + keyDef + `\s*` + keyValueDelimiter + `\s*` + valueDef + `\s*`
)

var (
keyRe = regexp.MustCompile(`^` + keyDef + `$`)
valueRe = regexp.MustCompile(`^` + valueDef + `$`)
keyRe = keyReValidator{}
valueRe = valReValidator{}
propertyRe = regexp.MustCompile(`^(?:\s*` + keyDef + `\s*|` + keyValueDef + `)$`)
cdvr1993 marked this conversation as resolved.
Show resolved Hide resolved
)

Expand Down Expand Up @@ -550,3 +551,48 @@ func (b Baggage) String() string {
}
return strings.Join(members, listDelimiter)
}

// They must follow the following rules (regex syntax):
// keyDef = `([\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5a\x5e-\x7a\x7c\x7e]+)`
// valueDef = `([\x21\x23-\x2b\x2d-\x3a\x3c-\x5B\x5D-\x7e]*)`.
type keyReValidator struct{}
pellared marked this conversation as resolved.
Show resolved Hide resolved

func (keyReValidator) MatchString(s string) bool {
if len(s) == 0 {
return false
}

for _, c := range s {
if !(c >= 0x23 && c <= 0x27) &&
!(c >= 0x30 && c <= 0x39) &&
!(c >= 0x41 && c <= 0x5a) &&
!(c >= 0x5e && c <= 0x7a) &&
c != 0x21 &&
c != 0x2a &&
c != 0x2b &&
c != 0x2d &&
c != 0x2e &&
c != 0x7c &&
c != 0x7e {
return false
}
}

return true
}

type valReValidator struct{}

func (valReValidator) MatchString(s string) bool {
for _, c := range s {
if !(c >= 0x23 && c <= 0x2b) &&
!(c >= 0x2d && c <= 0x3a) &&
!(c >= 0x3c && c <= 0x5b) &&
!(c >= 0x5d && c <= 0x7e) &&
c != 0x21 {
return false
}
}

return true
}
pellared marked this conversation as resolved.
Show resolved Hide resolved