forked from honeycombio/libhoney-go
/
libhoney.go
727 lines (653 loc) · 21.3 KB
/
libhoney.go
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
// Copyright 2016 Honeycomb, Hound Technology, Inc. All rights reserved.
// Use of this source code is governed by the Apache License 2.0
// license that can be found in the LICENSE file.
package libhoney
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"path"
"reflect"
"sort"
"strings"
"sync"
"time"
"gopkg.in/alexcesaro/statsd.v2"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
const (
defaultSampleRate = 1
defaultAPIHost = "https://api.honeycomb.io/"
version = "1.5.0"
// DefaultMaxBatchSize how many events to collect in a batch
DefaultMaxBatchSize = 50
// DefaultBatchTimeout how frequently to send unfilled batches
DefaultBatchTimeout = 100 * time.Millisecond
// DefaultMaxConcurrentBatches how many batches to maintain in parallel
DefaultMaxConcurrentBatches = 80
// DefaultPendingWorkCapacity how many events to queue up for busy batches
DefaultPendingWorkCapacity = 10000
)
var (
ptrKinds = []reflect.Kind{reflect.Ptr, reflect.Slice, reflect.Map}
)
// globals to support default/singleton-like behavior
var (
tx Output
txOnce sync.Once
blockOnResponses = false
sd, _ = statsd.New(statsd.Mute(true)) // init working default, to be overridden
responses = make(chan Response, 2*DefaultPendingWorkCapacity)
defaultBuilder = &Builder{
APIHost: defaultAPIHost,
SampleRate: defaultSampleRate,
dynFields: make([]dynamicField, 0, 0),
fieldHolder: fieldHolder{
data: make(map[string]interface{}),
},
}
)
// UserAgentAddition is a variable set at compile time via -ldflags to allow you
// to augment the "User-Agent" header that libhoney sends along with each event.
// The default User-Agent is "libhoney-go/<version>". If you set this variable, its
// contents will be appended to the User-Agent string, separated by a space. The
// expected format is product-name/version, eg "myapp/1.0"
var UserAgentAddition string
// Config specifies settings for initializing the library.
type Config struct {
// WriteKey is the Honeycomb authentication token. If it is specified during
// libhoney initialization, it will be used as the default write key for all
// events. If absent, write key must be explicitly set on a builder or
// event. Find your team write key at https://ui.honeycomb.io/account
WriteKey string
// Dataset is the name of the Honeycomb dataset to which to send these events.
// If it is specified during libhoney initialization, it will be used as the
// default dataset for all events. If absent, dataset must be explicitly set
// on a builder or event.
Dataset string
// SampleRate is the rate at which to sample this event. Default is 1,
// meaning no sampling. If you want to send one event out of every 250 times
// Send() is called, you would specify 250 here.
SampleRate uint
// APIHost is the hostname for the Honeycomb API server to which to send this
// event. default: https://api.honeycomb.io/
APIHost string
// TODO add logger in an agnostic way
// BlockOnSend determines if libhoney should block or drop packets that exceed
// the size of the send channel (set by PendingWorkCapacity). Defaults to
// False - events overflowing the send channel will be dropped.
BlockOnSend bool
// BlockOnResponse determines if libhoney should block trying to hand
// responses back to the caller. If this is true and there is nothing reading
// from the Responses channel, it will fill up and prevent events from being
// sent to Honeycomb. Defaults to False - if you don't read from the Responses
// channel it will be ok.
BlockOnResponse bool
// Output allows you to override what happens to events after you call
// Send() on them. By default, events are asynchronously sent to the
// Honeycomb API. You can use the MockOutput included in this package in
// unit tests, or use the WriterOutput to write events to STDOUT or to a
// file when developing locally.
Output Output
// Configuration for the underlying sender. It is safe (and recommended) to
// leave these values at their defaults. You cannot change these values
// after calling Init()
MaxBatchSize uint // how many events to collect into a batch before sending. Overrides DefaultMaxBatchSize.
SendFrequency time.Duration // how often to send off batches. Overrides DefaultBatchTimeout.
MaxConcurrentBatches uint // how many batches can be inflight simultaneously. Overrides DefaultMaxConcurrentBatches.
PendingWorkCapacity uint // how many events to allow to pile up. Overrides DefaultPendingWorkCapacity
// Transport can be provided to the http.Client attempting to talk to
// Honeycomb servers. Intended for use in tests in order to assert on
// expected behavior.
Transport http.RoundTripper
}
// VerifyWriteKey calls out to the Honeycomb API to validate the write key, so
// we can exit immediately if desired instead of happily sending events that
// are all rejected.
func VerifyWriteKey(config Config) (string, error) {
if config.WriteKey == "" {
return "", errors.New("Write key is empty")
}
if config.APIHost == "" {
config.APIHost = defaultAPIHost
}
u, err := url.Parse(config.APIHost)
if err != nil {
return "", fmt.Errorf("Error parsing API URL: %s", err)
}
u.Path = path.Join(u.Path, "1", "team_slug")
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return "", err
}
req.Header.Set("User-Agent", UserAgentAddition)
req.Header.Add("X-Honeycomb-Team", config.WriteKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusUnauthorized {
return "", errors.New("Write key provided is invalid")
}
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf(`Abnormal non-200 response verifying Honeycomb write key: %d
Response body: %s`, resp.StatusCode, string(body))
}
ret := map[string]string{}
if err := json.Unmarshal(body, &ret); err != nil {
return "", err
}
return ret["team_slug"], nil
}
// Event is used to hold data that can be sent to Honeycomb. It can also
// specify overrides of the config settings.
type Event struct {
// WriteKey, if set, overrides whatever is found in Config
WriteKey string
// Dataset, if set, overrides whatever is found in Config
Dataset string
// SampleRate, if set, overrides whatever is found in Config
SampleRate uint
// APIHost, if set, overrides whatever is found in Config
APIHost string
// Timestamp, if set, specifies the time for this event. If unset, defaults
// to Now()
Timestamp time.Time
// Metadata is a field for you to add in data that will be handed back to you
// on the Response object read off the Responses channel. It is not sent to
// Honeycomb with the event.
Metadata interface{}
// fieldHolder contains fields (and methods) common to both events and builders
fieldHolder
}
// Marshaling an Event for batching up to the Honeycomb servers. Omits fields
// that aren't specific to this particular event, and allows for behavior like
// omitempty'ing a zero'ed out time.Time.
func (e *Event) MarshalJSON() ([]byte, error) {
e.lock.RLock()
defer e.lock.RUnlock()
tPointer := &(e.Timestamp)
if e.Timestamp.IsZero() {
tPointer = nil
}
// don't include sample rate if it's 1; this is the default
sampleRate := e.SampleRate
if sampleRate == 1 {
sampleRate = 0
}
return json.Marshal(struct {
Data marshallableMap `json:"data"`
SampleRate uint `json:"samplerate,omitempty"`
Timestamp *time.Time `json:"time,omitempty"`
}{e.data, sampleRate, tPointer})
}
// Builder is used to create templates for new events, specifying default fields
// and override settings.
type Builder struct {
// WriteKey, if set, overrides whatever is found in Config
WriteKey string
// Dataset, if set, overrides whatever is found in Config
Dataset string
// SampleRate, if set, overrides whatever is found in Config
SampleRate uint
// APIHost, if set, overrides whatever is found in Config
APIHost string
// fieldHolder contains fields (and methods) common to both events and builders
fieldHolder
// any dynamic fields to apply to each generated event
dynFields []dynamicField
dynFieldsLock sync.RWMutex
}
type fieldHolder struct {
data marshallableMap
lock sync.RWMutex
}
// Wrapper type for custom JSON serialization: individual values that can't be
// marshalled (or are null pointers) will be skipped, instead of causing
// marshalling to raise an error.
type marshallableMap map[string]interface{}
func (m marshallableMap) MarshalJSON() ([]byte, error) {
keys := make([]string, len(m))
i := 0
for k := range m {
keys[i] = k
i++
}
sort.Strings(keys)
out := bytes.NewBufferString("{")
first := true
for _, k := range keys {
b, ok := maybeMarshalValue(m[k])
if ok {
if first {
first = false
} else {
out.WriteByte(',')
}
out.WriteByte('"')
out.Write([]byte(k))
out.WriteByte('"')
out.WriteByte(':')
out.Write(b)
}
}
out.WriteByte('}')
return out.Bytes(), nil
}
func maybeMarshalValue(v interface{}) ([]byte, bool) {
if v == nil {
return nil, false
}
val := reflect.ValueOf(v)
kind := val.Type().Kind()
for _, ptrKind := range ptrKinds {
if kind == ptrKind && val.IsNil() {
return nil, false
}
}
b, err := json.Marshal(v)
if err != nil {
return nil, false
}
return b, true
}
type dynamicField struct {
name string
fn func() interface{}
}
// Init is called on app initialization and passed a Config struct, which
// configures default behavior. Use of package-level functions (e.g. SendNow())
// require that WriteKey and Dataset are defined.
//
// Otherwise, if WriteKey and DataSet are absent or a Config is not provided,
// they may be specified later, either on a Builder or an Event. WriteKey,
// Dataset, SampleRate, and APIHost can all be overridden on a per-Builder or
// per-Event basis.
//
// Make sure to call Close() to flush buffers.
func Init(config Config) error {
// Default sample rate should be 1. 0 is invalid.
if config.SampleRate == 0 {
config.SampleRate = defaultSampleRate
}
if config.APIHost == "" {
config.APIHost = defaultAPIHost
}
if config.MaxBatchSize == 0 {
config.MaxBatchSize = DefaultMaxBatchSize
}
if config.SendFrequency == 0 {
config.SendFrequency = DefaultBatchTimeout
}
if config.MaxConcurrentBatches == 0 {
config.MaxConcurrentBatches = DefaultMaxConcurrentBatches
}
if config.PendingWorkCapacity == 0 {
config.PendingWorkCapacity = DefaultPendingWorkCapacity
}
blockOnResponses = config.BlockOnResponse
if config.Output == nil {
// reset the global transmission
tx = &txDefaultClient{
maxBatchSize: config.MaxBatchSize,
batchTimeout: config.SendFrequency,
maxConcurrentBatches: config.MaxConcurrentBatches,
pendingWorkCapacity: config.PendingWorkCapacity,
blockOnSend: config.BlockOnSend,
blockOnResponses: config.BlockOnResponse,
transport: config.Transport,
}
} else {
tx = config.Output
}
if err := tx.Start(); err != nil {
return err
}
sd, _ = statsd.New(statsd.Prefix("libhoney"))
responses = make(chan Response, config.PendingWorkCapacity*2)
defaultBuilder = &Builder{
WriteKey: config.WriteKey,
Dataset: config.Dataset,
SampleRate: config.SampleRate,
APIHost: config.APIHost,
dynFields: make([]dynamicField, 0, 0),
fieldHolder: fieldHolder{
data: make(map[string]interface{}),
},
}
return nil
}
// Close waits for all in-flight messages to be sent. You should
// call Close() before app termination.
func Close() {
tx.Stop()
close(responses)
}
// SendNow is a shortcut to create an event, add data, and send the event.
func SendNow(data interface{}) error {
ev := NewEvent()
if err := ev.Add(data); err != nil {
return err
}
if err := ev.Send(); err != nil {
return err
}
return nil
}
// Responses returns the channel from which the caller can read the responses
// to sent events.
func Responses() chan Response {
return responses
}
// AddDynamicField takes a field name and a function that will generate values
// for that metric. The function is called once every time a NewEvent() is
// created and added as a field (with name as the key) to the newly created
// event.
func AddDynamicField(name string, fn func() interface{}) error {
return defaultBuilder.AddDynamicField(name, fn)
}
// AddField adds a Field to the global scope. This metric will be inherited by
// all builders and events.
func AddField(name string, val interface{}) {
defaultBuilder.AddField(name, val)
}
// Add adds its data to the global scope. It adds all fields in a struct or all
// keys in a map as individual Fields. These metrics will be inherited by all
// builders and events.
func Add(data interface{}) error {
return defaultBuilder.Add(data)
}
// NewEvent creates a new event prepopulated with any Fields present in the
// global scope.
func NewEvent() *Event {
return defaultBuilder.NewEvent()
}
// AddField adds an individual metric to the event or builder on which it is
// called. Note that if you add a value that cannot be serialized to JSON (eg a
// function or channel), the event will fail to send.
func (f *fieldHolder) AddField(key string, val interface{}) {
f.lock.Lock()
defer f.lock.Unlock()
f.data[key] = val
}
// Add adds a complex data type to the event or builder on which it's called.
// For structs, it adds each exported field. For maps, it adds each key/value.
// Add will error on all other types.
func (f *fieldHolder) Add(data interface{}) error {
switch reflect.TypeOf(data).Kind() {
case reflect.Struct:
return f.addStruct(data)
case reflect.Map:
return f.addMap(data)
case reflect.Ptr:
return f.Add(reflect.ValueOf(data).Elem().Interface())
}
return fmt.Errorf(
"Couldn't add type %s content %+v",
reflect.TypeOf(data).Kind(), data,
)
}
func (f *fieldHolder) addStruct(s interface{}) error {
f.lock.Lock()
defer f.lock.Unlock()
// TODO should we handle embedded structs differently from other deep structs?
sType := reflect.TypeOf(s)
sVal := reflect.ValueOf(s)
// Iterate through the fields, adding each.
for i := 0; i < sType.NumField(); i++ {
fieldInfo := sType.Field(i)
if fieldInfo.PkgPath != "" {
// skipping unexported field in the struct
continue
}
var fName string
fTag := fieldInfo.Tag.Get("json")
if fTag != "" {
if fTag == "-" {
// skip this field
continue
}
// slice off options
if idx := strings.Index(fTag, ","); idx != -1 {
options := fTag[idx:]
fTag = fTag[:idx]
if strings.Contains(options, "omitempty") && isEmptyValue(sVal.Field(i)) {
// skip empty values if omitempty option is set
continue
}
}
fName = fTag
} else {
fName = fieldInfo.Name
}
f.data[fName] = sVal.Field(i).Interface()
}
return nil
}
func (f *fieldHolder) addMap(m interface{}) error {
f.lock.Lock()
defer f.lock.Unlock()
mVal := reflect.ValueOf(m)
mKeys := mVal.MapKeys()
for _, key := range mKeys {
// get a string representation of key
var keyStr string
switch key.Type().Kind() {
case reflect.String:
keyStr = key.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
reflect.Uint64, reflect.Float32, reflect.Float64, reflect.Complex64,
reflect.Complex128:
keyStr = fmt.Sprintf("%v", key.Interface())
default:
return fmt.Errorf("failed to add map: key type %s unaccepted", key.Type().Kind())
}
f.data[keyStr] = mVal.MapIndex(key).Interface()
}
return nil
}
// AddFunc takes a function and runs it repeatedly, adding the return values
// as fields.
// The function should return error when it has exhausted its values
func (f *fieldHolder) AddFunc(fn func() (string, interface{}, error)) error {
for {
key, rawVal, err := fn()
if err != nil {
// fn is done giving us data
break
}
f.AddField(key, rawVal)
}
return nil
}
// Fields returns a reference to the map of fields that have been added to an
// event. Caution: it is not safe to manipulate the returned map concurrently
// with calls to AddField, Add or AddFunc.
func (f *fieldHolder) Fields() map[string]interface{} {
return f.data
}
// Send dispatches the event to be sent to Honeycomb, sampling if necessary.
//
// If you have sampling enabled
// (i.e. SampleRate >1), Send will only actually transmit data with a
// probability of 1/SampleRate. No error is returned whether or not traffic
// is sampled, however, the Response sent down the response channel will
// indicate the event was sampled in the errors Err field.
//
// Send inherits the values of required fields from Config. If any required
// fields are specified in neither Config nor the Event, Send will return an
// error. Required fields are APIHost, WriteKey, and Dataset. Values specified
// in an Event override Config.
func (e *Event) Send() error {
if shouldDrop(e.SampleRate) {
sd.Increment("sampled")
sendDroppedResponse(e, "event dropped due to sampling")
return nil
}
return e.SendPresampled()
}
// SendPresampled dispatches the event to be sent to Honeycomb.
//
// Sampling is assumed to have already happened. SendPresampled will dispatch
// every event handed to it, and pass along the sample rate. Use this instead of
// Send() when the calling function handles the logic around which events to
// drop when sampling.
//
// SendPresampled inherits the values of required fields from Config. If any
// required fields are specified in neither Config nor the Event, Send will
// return an error. Required fields are APIHost, WriteKey, and Dataset. Values
// specified in an Event override Config.
func (e *Event) SendPresampled() error {
e.lock.RLock()
defer e.lock.RUnlock()
if len(e.data) == 0 {
return errors.New("No metrics added to event. Won't send empty event.")
}
if e.APIHost == "" {
return errors.New("No APIHost for Honeycomb. Can't send to the Great Unknown.")
}
if e.WriteKey == "" {
return errors.New("No WriteKey specified. Can't send event.")
}
if e.Dataset == "" {
return errors.New("No Dataset for Honeycomb. Can't send datasetless.")
}
txOnce.Do(func() {
if tx == nil {
tx = &txDefaultClient{
maxBatchSize: DefaultMaxBatchSize,
batchTimeout: DefaultBatchTimeout,
maxConcurrentBatches: DefaultMaxConcurrentBatches,
pendingWorkCapacity: DefaultPendingWorkCapacity,
}
tx.Start()
}
})
tx.Add(e)
return nil
}
// sendResponse sends a dropped event response down the response channel
func sendDroppedResponse(e *Event, message string) {
r := Response{
Err: errors.New(message),
Metadata: e.Metadata,
}
if blockOnResponses {
responses <- r
} else {
select {
case responses <- r:
default:
}
}
}
// returns true if the sample should be dropped
func shouldDrop(rate uint) bool {
return rand.Intn(int(rate)) != 0
}
// NewBuilder creates a new event builder. The builder inherits any
// Dynamic or Static Fields present in the global scope.
func NewBuilder() *Builder {
return defaultBuilder.Clone()
}
// AddDynamicField adds a dynamic field to the builder. Any events
// created from this builder will get this metric added.
func (b *Builder) AddDynamicField(name string, fn func() interface{}) error {
b.dynFieldsLock.Lock()
defer b.dynFieldsLock.Unlock()
dynFn := dynamicField{
name: name,
fn: fn,
}
b.dynFields = append(b.dynFields, dynFn)
return nil
}
// SendNow is a shortcut to create an event from this builder, add data, and
// send the event.
func (b *Builder) SendNow(data interface{}) error {
ev := b.NewEvent()
if err := ev.Add(data); err != nil {
return err
}
if err := ev.Send(); err != nil {
return err
}
return nil
}
// NewEvent creates a new Event prepopulated with fields, dynamic
// field values, and configuration inherited from the builder.
func (b *Builder) NewEvent() *Event {
e := &Event{
WriteKey: b.WriteKey,
Dataset: b.Dataset,
SampleRate: b.SampleRate,
APIHost: b.APIHost,
Timestamp: time.Now(),
}
e.data = make(map[string]interface{})
b.lock.RLock()
defer b.lock.RUnlock()
for k, v := range b.data {
e.data[k] = v
}
// create dynamic metrics
b.dynFieldsLock.RLock()
defer b.dynFieldsLock.RUnlock()
for _, dynField := range b.dynFields {
e.AddField(dynField.name, dynField.fn())
}
return e
}
// Clone creates a new builder that inherits all traits of this builder and
// creates its own scope in which to add additional static and dynamic fields.
func (b *Builder) Clone() *Builder {
newB := &Builder{
WriteKey: b.WriteKey,
Dataset: b.Dataset,
SampleRate: b.SampleRate,
APIHost: b.APIHost,
dynFields: make([]dynamicField, 0, len(b.dynFields)),
}
newB.data = make(map[string]interface{})
b.lock.RLock()
defer b.lock.RUnlock()
for k, v := range b.data {
newB.data[k] = v
}
// copy dynamic metric generators
b.dynFieldsLock.RLock()
defer b.dynFieldsLock.RUnlock()
for _, dynFd := range b.dynFields {
newB.dynFields = append(newB.dynFields, dynFd)
}
return newB
}
// Helper lifted from Go stdlib encoding/json
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Ptr:
return v.IsNil()
}
return false
}