| 1 | // Copyright 2022 The OpenZipkin Authors |
| 2 | // |
| 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 |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 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. |
| 14 | |
| 15 | // Package baggage holds a Baggage propagation implementation based on |
| 16 | // explicit allowList semantics. |
| 17 | package baggagescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/ |
| 18 | |
| 19 | import ( |
| 20 | "stringsscip-go gomod github.com/golang/go/src go1.20 strings/" |
| 21 | |
| 22 | "github.com/openzipkin/zipkin-go/middlewarescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/middleware`/" |
| 23 | "github.com/openzipkin/zipkin-go/modelscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/model`/" |
| 24 | ) |
| 25 | |
| 26 | var ( |
| 27 | _ middlewarescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/middleware`/.BaggageHandlerscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/middleware`/BaggageHandler# = (*baggagescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#)(nil) |
| 28 | _ modelscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/model`/.BaggageFieldsscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/model`/BaggageFields# = (*baggagescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#)(nil) |
| 29 | ) |
| 30 | |
| 31 | type baggagescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage# struct { |
| 32 | // registry holds our registry of allowed fields to propagate |
| 33 | registryscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#registry. map[string]struct{} |
| 34 | // fields holds the retrieved key-values pairs to propagate |
| 35 | fieldsscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#fields. map[string][]string |
| 36 | } |
| 37 | |
| 38 | // New returns a new Baggage interface which is configured to propagate the |
| 39 | // registered fields. |
| 40 | func Newscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/New().(keys ...string) middlewarescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/middleware`/.BaggageHandlerscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/middleware`/BaggageHandler# { |
| 41 | b := &baggagescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#{ |
| 42 | registryscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#registry.: make(map[string]struct{}), |
| 43 | } |
| 44 | for _, key := range keys { |
| 45 | b.registryscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#registry.[stringsscip-go gomod github.com/golang/go/src go1.20 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.20 strings/ToLower().(key)] = struct{}{} |
| 46 | } |
| 47 | return b |
| 48 | } |
| 49 | |
| 50 | // New is called by server middlewares and returns a fresh initialized |
| 51 | // baggage implementation. |
| 52 | func (b *baggagescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#) Newscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#New().() modelscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/model`/.BaggageFieldsscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/model`/BaggageFields# { |
| 53 | return &baggagescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#{ |
| 54 | registryscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#registry.: b.registryscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#registry., |
| 55 | fieldsscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#fields.: make(map[string][]string), |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func (b *baggagescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#) Getscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#Get().(key string) []string { |
| 60 | return b.fieldsscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#fields.[stringsscip-go gomod github.com/golang/go/src go1.20 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.20 strings/ToLower().(key)] |
| 61 | } |
| 62 | |
| 63 | func (b *baggagescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#) Addscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#Add().(key string, values ...string) bool { |
| 64 | if len(values) == 0 { |
| 65 | return false |
| 66 | } |
| 67 | key = stringsscip-go gomod github.com/golang/go/src go1.20 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.20 strings/ToLower().(key) |
| 68 | if _, ok := b.registryscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#registry.[key]; !ok { |
| 69 | return false |
| 70 | } |
| 71 | // multiple values for a header is allowed |
| 72 | b.fieldsscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#fields.[key] = append(b.fieldsscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#fields.[key], values...) |
| 73 | |
| 74 | return true |
| 75 | } |
| 76 | |
| 77 | func (b *baggagescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#) Setscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#Set().(key string, values ...string) bool { |
| 78 | if len(values) == 0 { |
| 79 | return false |
| 80 | } |
| 81 | key = stringsscip-go gomod github.com/golang/go/src go1.20 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.20 strings/ToLower().(key) |
| 82 | if _, ok := b.registryscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#registry.[key]; !ok { |
| 83 | return false |
| 84 | } |
| 85 | b.fieldsscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#fields.[key] = values |
| 86 | |
| 87 | return true |
| 88 | } |
| 89 | |
| 90 | func (b *baggagescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#) Deletescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#Delete().(key string) bool { |
| 91 | key = stringsscip-go gomod github.com/golang/go/src go1.20 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.20 strings/ToLower().(key) |
| 92 | if _, ok := b.registryscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#registry.[key]; !ok { |
| 93 | return false |
| 94 | } |
| 95 | for k := range b.fieldsscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#fields. { |
| 96 | if key == k { |
| 97 | delete(b.fieldsscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#fields., k) |
| 98 | } |
| 99 | } |
| 100 | return true |
| 101 | } |
| 102 | |
| 103 | func (b *baggagescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#) Iteratescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#Iterate().(f func(key string, values []string)) { |
| 104 | for key, v := range b.fieldsscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#fields. { |
| 105 | values := make([]string, len(v)) |
| 106 | copy(values, v) |
| 107 | f(key, values) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func (b *baggagescip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#) IterateKeysscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#IterateKeys().(f func(key string)) { |
| 112 | for key := range b.registryscip-go gomod github.com/openzipkin/zipkin-go . `github.com/openzipkin/zipkin-go/propagation/baggage`/baggage#registry. { |
| 113 | f(key) |
| 114 | } |
| 115 | } |