Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 6161d2e

Browse files
committed
Add support for Tag metadata (#1125)
* Add support for tag metadata. * update ocgrpc and ochttp to use new insert/update/upsert api. : * updated existing method optional metadata option. * make TTLNoPropagation and TTLUnlimitedPropagation a function. * changed ttl api. * add test case for multiple TTL metadata. * add test case and note for update/insert api.
1 parent ed3a3f0 commit 6161d2e

File tree

6 files changed

+293
-38
lines changed

6 files changed

+293
-38
lines changed

tag/map.go

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,15 @@ type Tag struct {
2828
Value string
2929
}
3030

31+
type tagContent struct {
32+
value string
33+
m metadatas
34+
}
35+
3136
// Map is a map of tags. Use New to create a context containing
3237
// a new Map.
3338
type Map struct {
34-
m map[Key]string
39+
m map[Key]tagContent
3540
}
3641

3742
// Value returns the value for the key if a value for the key exists.
@@ -40,7 +45,7 @@ func (m *Map) Value(k Key) (string, bool) {
4045
return "", false
4146
}
4247
v, ok := m.m[k]
43-
return v, ok
48+
return v.value, ok
4449
}
4550

4651
func (m *Map) String() string {
@@ -62,29 +67,29 @@ func (m *Map) String() string {
6267
return buffer.String()
6368
}
6469

65-
func (m *Map) insert(k Key, v string) {
70+
func (m *Map) insert(k Key, v string, md metadatas) {
6671
if _, ok := m.m[k]; ok {
6772
return
6873
}
69-
m.m[k] = v
74+
m.m[k] = tagContent{value: v, m: md}
7075
}
7176

72-
func (m *Map) update(k Key, v string) {
77+
func (m *Map) update(k Key, v string, md metadatas) {
7378
if _, ok := m.m[k]; ok {
74-
m.m[k] = v
79+
m.m[k] = tagContent{value: v, m: md}
7580
}
7681
}
7782

78-
func (m *Map) upsert(k Key, v string) {
79-
m.m[k] = v
83+
func (m *Map) upsert(k Key, v string, md metadatas) {
84+
m.m[k] = tagContent{value: v, m: md}
8085
}
8186

8287
func (m *Map) delete(k Key) {
8388
delete(m.m, k)
8489
}
8590

8691
func newMap() *Map {
87-
return &Map{m: make(map[Key]string)}
92+
return &Map{m: make(map[Key]tagContent)}
8893
}
8994

9095
// Mutator modifies a tag map.
@@ -95,13 +100,17 @@ type Mutator interface {
95100
// Insert returns a mutator that inserts a
96101
// value associated with k. If k already exists in the tag map,
97102
// mutator doesn't update the value.
98-
func Insert(k Key, v string) Mutator {
103+
// Metadata applies metadata to the tag. It is optional.
104+
// Metadatas are applied in the order in which it is provided.
105+
// If more than one metadata updates the same attribute then
106+
// the update from the last metadata prevails.
107+
func Insert(k Key, v string, mds ...Metadata) Mutator {
99108
return &mutator{
100109
fn: func(m *Map) (*Map, error) {
101110
if !checkValue(v) {
102111
return nil, errInvalidValue
103112
}
104-
m.insert(k, v)
113+
m.insert(k, v, createMetadatas(mds...))
105114
return m, nil
106115
},
107116
}
@@ -110,13 +119,17 @@ func Insert(k Key, v string) Mutator {
110119
// Update returns a mutator that updates the
111120
// value of the tag associated with k with v. If k doesn't
112121
// exists in the tag map, the mutator doesn't insert the value.
113-
func Update(k Key, v string) Mutator {
122+
// Metadata applies metadata to the tag. It is optional.
123+
// Metadatas are applied in the order in which it is provided.
124+
// If more than one metadata updates the same attribute then
125+
// the update from the last metadata prevails.
126+
func Update(k Key, v string, mds ...Metadata) Mutator {
114127
return &mutator{
115128
fn: func(m *Map) (*Map, error) {
116129
if !checkValue(v) {
117130
return nil, errInvalidValue
118131
}
119-
m.update(k, v)
132+
m.update(k, v, createMetadatas(mds...))
120133
return m, nil
121134
},
122135
}
@@ -126,18 +139,37 @@ func Update(k Key, v string) Mutator {
126139
// value of the tag associated with k with v. It inserts the
127140
// value if k doesn't exist already. It mutates the value
128141
// if k already exists.
129-
func Upsert(k Key, v string) Mutator {
142+
// Metadata applies metadata to the tag. It is optional.
143+
// Metadatas are applied in the order in which it is provided.
144+
// If more than one metadata updates the same attribute then
145+
// the update from the last metadata prevails.
146+
func Upsert(k Key, v string, mds ...Metadata) Mutator {
130147
return &mutator{
131148
fn: func(m *Map) (*Map, error) {
132149
if !checkValue(v) {
133150
return nil, errInvalidValue
134151
}
135-
m.upsert(k, v)
152+
m.upsert(k, v, createMetadatas(mds...))
136153
return m, nil
137154
},
138155
}
139156
}
140157

158+
func createMetadatas(mds ...Metadata) metadatas {
159+
var metas metadatas
160+
if len(mds) > 0 {
161+
for _, md := range mds {
162+
if md != nil {
163+
md(&metas)
164+
}
165+
}
166+
} else {
167+
WithTTL(TTLUnlimitedPropagation)(&metas)
168+
}
169+
return metas
170+
171+
}
172+
141173
// Delete returns a mutator that deletes
142174
// the value associated with k.
143175
func Delete(k Key) Mutator {
@@ -160,10 +192,10 @@ func New(ctx context.Context, mutator ...Mutator) (context.Context, error) {
160192
if !checkKeyName(k.Name()) {
161193
return ctx, fmt.Errorf("key:%q: %v", k, errInvalidKeyName)
162194
}
163-
if !checkValue(v) {
195+
if !checkValue(v.value) {
164196
return ctx, fmt.Errorf("key:%q value:%q: %v", k.Name(), v, errInvalidValue)
165197
}
166-
m.insert(k, v)
198+
m.insert(k, v.value, v.m)
167199
}
168200
}
169201
var err error

tag/map_codec.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,11 @@ func Encode(m *Map) []byte {
170170
}
171171
eg.writeByte(byte(tagsVersionID))
172172
for k, v := range m.m {
173-
eg.writeByte(byte(keyTypeString))
174-
eg.writeStringWithVarintLen(k.name)
175-
eg.writeBytesWithVarintLen([]byte(v))
173+
if v.m.ttl.ttl == valueTTLUnlimitedPropagation {
174+
eg.writeByte(byte(keyTypeString))
175+
eg.writeStringWithVarintLen(k.name)
176+
eg.writeBytesWithVarintLen([]byte(v.value))
177+
}
176178
}
177179
return eg.bytes()
178180
}
@@ -190,7 +192,7 @@ func Decode(bytes []byte) (*Map, error) {
190192

191193
// DecodeEach decodes the given serialized tag map, calling handler for each
192194
// tag key and value decoded.
193-
func DecodeEach(bytes []byte, fn func(key Key, val string)) error {
195+
func DecodeEach(bytes []byte, fn func(key Key, val string, md metadatas)) error {
194196
eg := &encoderGRPC{
195197
buf: bytes,
196198
}
@@ -228,7 +230,10 @@ func DecodeEach(bytes []byte, fn func(key Key, val string)) error {
228230
if !checkValue(val) {
229231
return errInvalidValue
230232
}
231-
fn(key, val)
233+
fn(key, val, createMetadatas(WithTTL(TTLUnlimitedPropagation)))
234+
if err != nil {
235+
return err
236+
}
232237
}
233238
return nil
234239
}

tag/map_codec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestEncodeDecode(t *testing.T) {
9191

9292
got := make([]keyValue, 0)
9393
for k, v := range decoded.m {
94-
got = append(got, keyValue{k, string(v)})
94+
got = append(got, keyValue{k, string(v.value)})
9595
}
9696
want := tc.pairs
9797

0 commit comments

Comments
 (0)