aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/opentracing/opentracing-go/log/field.go
blob: 50feea341a732865402478bdd1f741bf3df8a4b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package log

import (
    "fmt"
    "math"
)

type fieldType int

const (
    stringType fieldType = iota
    boolType
    intType
    int32Type
    uint32Type
    int64Type
    uint64Type
    float32Type
    float64Type
    errorType
    objectType
    lazyLoggerType
    noopType
)

// Field instances are constructed via LogBool, LogString, and so on.
// Tracing implementations may then handle them via the Field.Marshal
// method.
//
// "heavily influenced by" (i.e., partially stolen from)
// https://github.com/uber-go/zap
type Field struct {
    key          string
    fieldType    fieldType
    numericVal   int64
    stringVal    string
    interfaceVal interface{}
}

// String adds a string-valued key:value pair to a Span.LogFields() record
func String(key, val string) Field {
    return Field{
        key:       key,
        fieldType: stringType,
        stringVal: val,
    }
}

// Bool adds a bool-valued key:value pair to a Span.LogFields() record
func Bool(key string, val bool) Field {
    var numericVal int64
    if val {
        numericVal = 1
    }
    return Field{
        key:        key,
        fieldType:  boolType,
        numericVal: numericVal,
    }
}

// Int adds an int-valued key:value pair to a Span.LogFields() record
func Int(key string, val int) Field {
    return Field{
        key:        key,
        fieldType:  intType,
        numericVal: int64(val),
    }
}

// Int32 adds an int32-valued key:value pair to a Span.LogFields() record
func Int32(key string, val int32) Field {
    return Field{
        key:        key,
        fieldType:  int32Type,
        numericVal: int64(val),
    }
}

// Int64 adds an int64-valued key:value pair to a Span.LogFields() record
func Int64(key string, val int64) Field {
    return Field{
        key:        key,
        fieldType:  int64Type,
        numericVal: val,
    }
}

// Uint32 adds a uint32-valued key:value pair to a Span.LogFields() record
func Uint32(key string, val uint32) Field {
    return Field{
        key:        key,
        fieldType:  uint32Type,
        numericVal: int64(val),
    }
}

// Uint64 adds a uint64-valued key:value pair to a Span.LogFields() record
func Uint64(key string, val uint64) Field {
    return Field{
        key:        key,
        fieldType:  uint64Type,
        numericVal: int64(val),
    }
}

// Float32 adds a float32-valued key:value pair to a Span.LogFields() record
func Float32(key string, val float32) Field {
    return Field{
        key:        key,
        fieldType:  float32Type,
        numericVal: int64(math.Float32bits(val)),
    }
}

// Float64 adds a float64-valued key:value pair to a Span.LogFields() record
func Float64(key string, val float64) Field {
    return Field{
        key:        key,
        fieldType:  float64Type,
        numericVal: int64(math.Float64bits(val)),
    }
}

// Error adds an error with the key "error" to a Span.LogFields() record
func Error(err error) Field {
    return Field{
        key:          "error",
        fieldType:    errorType,
        interfaceVal: err,
    }
}

// Object adds an object-valued key:value pair to a Span.LogFields() record
func Object(key string, obj interface{}) Field {
    return Field{
        key:          key,
        fieldType:    objectType,
        interfaceVal: obj,
    }
}

// LazyLogger allows for user-defined, late-bound logging of arbitrary data
type LazyLogger func(fv Encoder)

// Lazy adds a LazyLogger to a Span.LogFields() record; the tracing
// implementation will call the LazyLogger function at an indefinite time in
// the future (after Lazy() returns).
func Lazy(ll LazyLogger) Field {
    return Field{
        fieldType:    lazyLoggerType,
        interfaceVal: ll,
    }
}

// Noop creates a no-op log field that should be ignored by the tracer.
// It can be used to capture optional fields, for example those that should
// only be logged in non-production environment:
//
//     func customerField(order *Order) log.Field {
//          if os.Getenv("ENVIRONMENT") == "dev" {
//              return log.String("customer", order.Customer.ID)
//          }
//          return log.Noop()
//     }
//
//     span.LogFields(log.String("event", "purchase"), customerField(order))
//
func Noop() Field {
    return Field{
        fieldType: noopType,
    }
}

// Encoder allows access to the contents of a Field (via a call to
// Field.Marshal).
//
// Tracer implementations typically provide an implementation of Encoder;
// OpenTracing callers typically do not need to concern themselves with it.
type Encoder interface {
    EmitString(key, value string)
    EmitBool(key string, value bool)
    EmitInt(key string, value int)
    EmitInt32(key string, value int32)
    EmitInt64(key string, value int64)
    EmitUint32(key string, value uint32)
    EmitUint64(key string, value uint64)
    EmitFloat32(key string, value float32)
    EmitFloat64(key string, value float64)
    EmitObject(key string, value interface{})
    EmitLazyLogger(value LazyLogger)
}

// Marshal passes a Field instance through to the appropriate
// field-type-specific method of an Encoder.
func (lf Field) Marshal(visitor Encoder) {
    switch lf.fieldType {
    case stringType:
        visitor.EmitString(lf.key, lf.stringVal)
    case boolType:
        visitor.EmitBool(lf.key, lf.numericVal != 0)
    case intType:
        visitor.EmitInt(lf.key, int(lf.numericVal))
    case int32Type:
        visitor.EmitInt32(lf.key, int32(lf.numericVal))
    case int64Type:
        visitor.EmitInt64(lf.key, int64(lf.numericVal))
    case uint32Type:
        visitor.EmitUint32(lf.key, uint32(lf.numericVal))
    case uint64Type:
        visitor.EmitUint64(lf.key, uint64(lf.numericVal))
    case float32Type:
        visitor.EmitFloat32(lf.key, math.Float32frombits(uint32(lf.numericVal)))
    case float64Type:
        visitor.EmitFloat64(lf.key, math.Float64frombits(uint64(lf.numericVal)))
    case errorType:
        if err, ok := lf.interfaceVal.(error); ok {
            visitor.EmitString(lf.key, err.Error())
        } else {
            visitor.EmitString(lf.key, "<nil>")
        }
    case objectType:
        visitor.EmitObject(lf.key, lf.interfaceVal)
    case lazyLoggerType:
        visitor.EmitLazyLogger(lf.interfaceVal.(LazyLogger))
    case noopType:
        // intentionally left blank
    }
}

// Key returns the field's key.
func (lf Field) Key() string {
    return lf.key
}

// Value returns the field's value as interface{}.
func (lf Field) Value() interface{} {
    switch lf.fieldType {
    case stringType:
        return lf.stringVal
    case boolType:
        return lf.numericVal != 0
    case intType:
        return int(lf.numericVal)
    case int32Type:
        return int32(lf.numericVal)
    case int64Type:
        return int64(lf.numericVal)
    case uint32Type:
        return uint32(lf.numericVal)
    case uint64Type:
        return uint64(lf.numericVal)
    case float32Type:
        return math.Float32frombits(uint32(lf.numericVal))
    case float64Type:
        return math.Float64frombits(uint64(lf.numericVal))
    case errorType, objectType, lazyLoggerType:
        return lf.interfaceVal
    case noopType:
        return nil
    default:
        return nil
    }
}

// String returns a string representation of the key and value.
func (lf Field) String() string {
    return fmt.Sprint(lf.key, ":", lf.Value())
}