1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 // Msg is a key used to add message strings to tag lists.
16 Msg = NewStringKey("message", "a readable message")
17 // Name is used for things like traces that have a name.
18 Name = NewStringKey("name", "an entity name")
19 // Err is a key used to add error values to tag lists.
20 Err = NewErrorKey("error", "an error that occurred")
23 // Key is used as the identity of a Tag.
24 // Keys are intended to be compared by pointer only, the name should be unique
25 // for communicating with external systems, but it is not required or enforced.
27 // Name returns the key name.
29 // Description returns a string that can be used to describe the value.
32 // Format is used in formatting to append the value of the tag to the
34 // The formatter may use the supplied buf as a scratch area to avoid
36 Format(w io.Writer, buf []byte, tag Tag)
39 // ValueKey represents a key for untyped values.
40 type ValueKey struct {
45 // NewKey creates a new Key for untyped values.
46 func NewKey(name, description string) *ValueKey {
47 return &ValueKey{name: name, description: description}
50 func (k *ValueKey) Name() string { return k.name }
51 func (k *ValueKey) Description() string { return k.description }
53 func (k *ValueKey) Format(w io.Writer, buf []byte, tag Tag) {
54 fmt.Fprint(w, k.From(tag))
57 // Get can be used to get a tag for the key from a TagMap.
58 func (k *ValueKey) Get(tags TagMap) interface{} {
59 if t := tags.Find(k); t.Valid() {
65 // From can be used to get a value from a Tag.
66 func (k *ValueKey) From(t Tag) interface{} { return t.UnpackValue() }
68 // Of creates a new Tag with this key and the supplied value.
69 func (k *ValueKey) Of(value interface{}) Tag { return TagOfValue(k, value) }
71 // IntKey represents a key
77 // NewIntKey creates a new Key for int values.
78 func NewIntKey(name, description string) *IntKey {
79 return &IntKey{name: name, description: description}
82 func (k *IntKey) Name() string { return k.name }
83 func (k *IntKey) Description() string { return k.description }
85 func (k *IntKey) Format(w io.Writer, buf []byte, tag Tag) {
86 w.Write(strconv.AppendInt(buf, int64(k.From(tag)), 10))
89 // Of creates a new Tag with this key and the supplied value.
90 func (k *IntKey) Of(v int) Tag { return TagOf64(k, uint64(v)) }
92 // Get can be used to get a tag for the key from a TagMap.
93 func (k *IntKey) Get(tags TagMap) int {
94 if t := tags.Find(k); t.Valid() {
100 // From can be used to get a value from a Tag.
101 func (k *IntKey) From(t Tag) int { return int(t.Unpack64()) }
103 // Int8Key represents a key
104 type Int8Key struct {
109 // NewInt8Key creates a new Key for int8 values.
110 func NewInt8Key(name, description string) *Int8Key {
111 return &Int8Key{name: name, description: description}
114 func (k *Int8Key) Name() string { return k.name }
115 func (k *Int8Key) Description() string { return k.description }
117 func (k *Int8Key) Format(w io.Writer, buf []byte, tag Tag) {
118 w.Write(strconv.AppendInt(buf, int64(k.From(tag)), 10))
121 // Of creates a new Tag with this key and the supplied value.
122 func (k *Int8Key) Of(v int8) Tag { return TagOf64(k, uint64(v)) }
124 // Get can be used to get a tag for the key from a TagMap.
125 func (k *Int8Key) Get(tags TagMap) int8 {
126 if t := tags.Find(k); t.Valid() {
132 // From can be used to get a value from a Tag.
133 func (k *Int8Key) From(t Tag) int8 { return int8(t.Unpack64()) }
135 // Int16Key represents a key
136 type Int16Key struct {
141 // NewInt16Key creates a new Key for int16 values.
142 func NewInt16Key(name, description string) *Int16Key {
143 return &Int16Key{name: name, description: description}
146 func (k *Int16Key) Name() string { return k.name }
147 func (k *Int16Key) Description() string { return k.description }
149 func (k *Int16Key) Format(w io.Writer, buf []byte, tag Tag) {
150 w.Write(strconv.AppendInt(buf, int64(k.From(tag)), 10))
153 // Of creates a new Tag with this key and the supplied value.
154 func (k *Int16Key) Of(v int16) Tag { return TagOf64(k, uint64(v)) }
156 // Get can be used to get a tag for the key from a TagMap.
157 func (k *Int16Key) Get(tags TagMap) int16 {
158 if t := tags.Find(k); t.Valid() {
164 // From can be used to get a value from a Tag.
165 func (k *Int16Key) From(t Tag) int16 { return int16(t.Unpack64()) }
167 // Int32Key represents a key
168 type Int32Key struct {
173 // NewInt32Key creates a new Key for int32 values.
174 func NewInt32Key(name, description string) *Int32Key {
175 return &Int32Key{name: name, description: description}
178 func (k *Int32Key) Name() string { return k.name }
179 func (k *Int32Key) Description() string { return k.description }
181 func (k *Int32Key) Format(w io.Writer, buf []byte, tag Tag) {
182 w.Write(strconv.AppendInt(buf, int64(k.From(tag)), 10))
185 // Of creates a new Tag with this key and the supplied value.
186 func (k *Int32Key) Of(v int32) Tag { return TagOf64(k, uint64(v)) }
188 // Get can be used to get a tag for the key from a TagMap.
189 func (k *Int32Key) Get(tags TagMap) int32 {
190 if t := tags.Find(k); t.Valid() {
196 // From can be used to get a value from a Tag.
197 func (k *Int32Key) From(t Tag) int32 { return int32(t.Unpack64()) }
199 // Int64Key represents a key
200 type Int64Key struct {
205 // NewInt64Key creates a new Key for int64 values.
206 func NewInt64Key(name, description string) *Int64Key {
207 return &Int64Key{name: name, description: description}
210 func (k *Int64Key) Name() string { return k.name }
211 func (k *Int64Key) Description() string { return k.description }
213 func (k *Int64Key) Format(w io.Writer, buf []byte, tag Tag) {
214 w.Write(strconv.AppendInt(buf, k.From(tag), 10))
217 // Of creates a new Tag with this key and the supplied value.
218 func (k *Int64Key) Of(v int64) Tag { return TagOf64(k, uint64(v)) }
220 // Get can be used to get a tag for the key from a TagMap.
221 func (k *Int64Key) Get(tags TagMap) int64 {
222 if t := tags.Find(k); t.Valid() {
228 // From can be used to get a value from a Tag.
229 func (k *Int64Key) From(t Tag) int64 { return int64(t.Unpack64()) }
231 // UIntKey represents a key
232 type UIntKey struct {
237 // NewUIntKey creates a new Key for uint values.
238 func NewUIntKey(name, description string) *UIntKey {
239 return &UIntKey{name: name, description: description}
242 func (k *UIntKey) Name() string { return k.name }
243 func (k *UIntKey) Description() string { return k.description }
245 func (k *UIntKey) Format(w io.Writer, buf []byte, tag Tag) {
246 w.Write(strconv.AppendUint(buf, uint64(k.From(tag)), 10))
249 // Of creates a new Tag with this key and the supplied value.
250 func (k *UIntKey) Of(v uint) Tag { return TagOf64(k, uint64(v)) }
252 // Get can be used to get a tag for the key from a TagMap.
253 func (k *UIntKey) Get(tags TagMap) uint {
254 if t := tags.Find(k); t.Valid() {
260 // From can be used to get a value from a Tag.
261 func (k *UIntKey) From(t Tag) uint { return uint(t.Unpack64()) }
263 // UInt8Key represents a key
264 type UInt8Key struct {
269 // NewUInt8Key creates a new Key for uint8 values.
270 func NewUInt8Key(name, description string) *UInt8Key {
271 return &UInt8Key{name: name, description: description}
274 func (k *UInt8Key) Name() string { return k.name }
275 func (k *UInt8Key) Description() string { return k.description }
277 func (k *UInt8Key) Format(w io.Writer, buf []byte, tag Tag) {
278 w.Write(strconv.AppendUint(buf, uint64(k.From(tag)), 10))
281 // Of creates a new Tag with this key and the supplied value.
282 func (k *UInt8Key) Of(v uint8) Tag { return TagOf64(k, uint64(v)) }
284 // Get can be used to get a tag for the key from a TagMap.
285 func (k *UInt8Key) Get(tags TagMap) uint8 {
286 if t := tags.Find(k); t.Valid() {
292 // From can be used to get a value from a Tag.
293 func (k *UInt8Key) From(t Tag) uint8 { return uint8(t.Unpack64()) }
295 // UInt16Key represents a key
296 type UInt16Key struct {
301 // NewUInt16Key creates a new Key for uint16 values.
302 func NewUInt16Key(name, description string) *UInt16Key {
303 return &UInt16Key{name: name, description: description}
306 func (k *UInt16Key) Name() string { return k.name }
307 func (k *UInt16Key) Description() string { return k.description }
309 func (k *UInt16Key) Format(w io.Writer, buf []byte, tag Tag) {
310 w.Write(strconv.AppendUint(buf, uint64(k.From(tag)), 10))
313 // Of creates a new Tag with this key and the supplied value.
314 func (k *UInt16Key) Of(v uint16) Tag { return TagOf64(k, uint64(v)) }
316 // Get can be used to get a tag for the key from a TagMap.
317 func (k *UInt16Key) Get(tags TagMap) uint16 {
318 if t := tags.Find(k); t.Valid() {
324 // From can be used to get a value from a Tag.
325 func (k *UInt16Key) From(t Tag) uint16 { return uint16(t.Unpack64()) }
327 // UInt32Key represents a key
328 type UInt32Key struct {
333 // NewUInt32Key creates a new Key for uint32 values.
334 func NewUInt32Key(name, description string) *UInt32Key {
335 return &UInt32Key{name: name, description: description}
338 func (k *UInt32Key) Name() string { return k.name }
339 func (k *UInt32Key) Description() string { return k.description }
341 func (k *UInt32Key) Format(w io.Writer, buf []byte, tag Tag) {
342 w.Write(strconv.AppendUint(buf, uint64(k.From(tag)), 10))
345 // Of creates a new Tag with this key and the supplied value.
346 func (k *UInt32Key) Of(v uint32) Tag { return TagOf64(k, uint64(v)) }
348 // Get can be used to get a tag for the key from a TagMap.
349 func (k *UInt32Key) Get(tags TagMap) uint32 {
350 if t := tags.Find(k); t.Valid() {
356 // From can be used to get a value from a Tag.
357 func (k *UInt32Key) From(t Tag) uint32 { return uint32(t.Unpack64()) }
359 // UInt64Key represents a key
360 type UInt64Key struct {
365 // NewUInt64Key creates a new Key for uint64 values.
366 func NewUInt64Key(name, description string) *UInt64Key {
367 return &UInt64Key{name: name, description: description}
370 func (k *UInt64Key) Name() string { return k.name }
371 func (k *UInt64Key) Description() string { return k.description }
373 func (k *UInt64Key) Format(w io.Writer, buf []byte, tag Tag) {
374 w.Write(strconv.AppendUint(buf, k.From(tag), 10))
377 // Of creates a new Tag with this key and the supplied value.
378 func (k *UInt64Key) Of(v uint64) Tag { return TagOf64(k, v) }
380 // Get can be used to get a tag for the key from a TagMap.
381 func (k *UInt64Key) Get(tags TagMap) uint64 {
382 if t := tags.Find(k); t.Valid() {
388 // From can be used to get a value from a Tag.
389 func (k *UInt64Key) From(t Tag) uint64 { return t.Unpack64() }
391 // Float32Key represents a key
392 type Float32Key struct {
397 // NewFloat32Key creates a new Key for float32 values.
398 func NewFloat32Key(name, description string) *Float32Key {
399 return &Float32Key{name: name, description: description}
402 func (k *Float32Key) Name() string { return k.name }
403 func (k *Float32Key) Description() string { return k.description }
405 func (k *Float32Key) Format(w io.Writer, buf []byte, tag Tag) {
406 w.Write(strconv.AppendFloat(buf, float64(k.From(tag)), 'E', -1, 32))
409 // Of creates a new Tag with this key and the supplied value.
410 func (k *Float32Key) Of(v float32) Tag {
411 return TagOf64(k, uint64(math.Float32bits(v)))
414 // Get can be used to get a tag for the key from a TagMap.
415 func (k *Float32Key) Get(tags TagMap) float32 {
416 if t := tags.Find(k); t.Valid() {
422 // From can be used to get a value from a Tag.
423 func (k *Float32Key) From(t Tag) float32 {
424 return math.Float32frombits(uint32(t.Unpack64()))
427 // Float64Key represents a key
428 type Float64Key struct {
433 // NewFloat64Key creates a new Key for int64 values.
434 func NewFloat64Key(name, description string) *Float64Key {
435 return &Float64Key{name: name, description: description}
438 func (k *Float64Key) Name() string { return k.name }
439 func (k *Float64Key) Description() string { return k.description }
441 func (k *Float64Key) Format(w io.Writer, buf []byte, tag Tag) {
442 w.Write(strconv.AppendFloat(buf, k.From(tag), 'E', -1, 64))
445 // Of creates a new Tag with this key and the supplied value.
446 func (k *Float64Key) Of(v float64) Tag {
447 return TagOf64(k, math.Float64bits(v))
450 // Get can be used to get a tag for the key from a TagMap.
451 func (k *Float64Key) Get(tags TagMap) float64 {
452 if t := tags.Find(k); t.Valid() {
458 // From can be used to get a value from a Tag.
459 func (k *Float64Key) From(t Tag) float64 {
460 return math.Float64frombits(t.Unpack64())
463 // StringKey represents a key
464 type StringKey struct {
469 // NewStringKey creates a new Key for int64 values.
470 func NewStringKey(name, description string) *StringKey {
471 return &StringKey{name: name, description: description}
474 func (k *StringKey) Name() string { return k.name }
475 func (k *StringKey) Description() string { return k.description }
477 func (k *StringKey) Format(w io.Writer, buf []byte, tag Tag) {
478 w.Write(strconv.AppendQuote(buf, k.From(tag)))
481 // Of creates a new Tag with this key and the supplied value.
482 func (k *StringKey) Of(v string) Tag { return TagOfString(k, v) }
484 // Get can be used to get a tag for the key from a TagMap.
485 func (k *StringKey) Get(tags TagMap) string {
486 if t := tags.Find(k); t.Valid() {
492 // From can be used to get a value from a Tag.
493 func (k *StringKey) From(t Tag) string { return t.UnpackString() }
495 // BooleanKey represents a key
496 type BooleanKey struct {
501 // NewBooleanKey creates a new Key for bool values.
502 func NewBooleanKey(name, description string) *BooleanKey {
503 return &BooleanKey{name: name, description: description}
506 func (k *BooleanKey) Name() string { return k.name }
507 func (k *BooleanKey) Description() string { return k.description }
509 func (k *BooleanKey) Format(w io.Writer, buf []byte, tag Tag) {
510 w.Write(strconv.AppendBool(buf, k.From(tag)))
513 // Of creates a new Tag with this key and the supplied value.
514 func (k *BooleanKey) Of(v bool) Tag {
521 // Get can be used to get a tag for the key from a TagMap.
522 func (k *BooleanKey) Get(tags TagMap) bool {
523 if t := tags.Find(k); t.Valid() {
529 // From can be used to get a value from a Tag.
530 func (k *BooleanKey) From(t Tag) bool { return t.Unpack64() > 0 }
532 // ErrorKey represents a key
533 type ErrorKey struct {
538 // NewErrorKey creates a new Key for int64 values.
539 func NewErrorKey(name, description string) *ErrorKey {
540 return &ErrorKey{name: name, description: description}
543 func (k *ErrorKey) Name() string { return k.name }
544 func (k *ErrorKey) Description() string { return k.description }
546 func (k *ErrorKey) Format(w io.Writer, buf []byte, tag Tag) {
547 io.WriteString(w, k.From(tag).Error())
550 // Of creates a new Tag with this key and the supplied value.
551 func (k *ErrorKey) Of(v error) Tag { return TagOfValue(k, v) }
553 // Get can be used to get a tag for the key from a TagMap.
554 func (k *ErrorKey) Get(tags TagMap) error {
555 if t := tags.Find(k); t.Valid() {
561 // From can be used to get a value from a Tag.
562 func (k *ErrorKey) From(t Tag) error {
563 err, _ := t.UnpackValue().(error)