github.com/blend/go-sdk@v1.20220411.3/stats/printer.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package stats 9 10 import ( 11 "context" 12 "fmt" 13 "io" 14 "os" 15 "strings" 16 "time" 17 18 "github.com/blend/go-sdk/logger" 19 ) 20 21 var ( 22 _ Collector = (*Printer)(nil) 23 ) 24 25 // NewPrinter creates a printer from a given logger. 26 func NewPrinter(log logger.Log) *Printer { 27 p := new(Printer) 28 if log != nil { 29 p.Output = logger.ShimWriter{ 30 Context: context.Background(), 31 Log: log, 32 EventProvider: func(contents []byte) logger.Event { 33 return logger.NewMessageEvent("statsd", strings.TrimSpace(string(contents))) 34 }, 35 } 36 } else { 37 p.Output = os.Stdout 38 } 39 return p 40 } 41 42 // Printer is a collector that prints calls to a given writer. 43 type Printer struct { 44 Field struct { 45 Namespace string 46 DefaultTags []string 47 } 48 Output io.Writer 49 } 50 51 // AddDefaultTag adds a default tag. 52 func (p *Printer) AddDefaultTag(name, value string) { 53 p.Field.DefaultTags = append(p.Field.DefaultTags, Tag(name, value)) 54 } 55 56 // AddDefaultTags adds default tags. 57 func (p *Printer) AddDefaultTags(tags ...string) { 58 p.Field.DefaultTags = append(p.Field.DefaultTags, tags...) 59 } 60 61 // DefaultTags returns the default tags set. 62 func (p Printer) DefaultTags() []string { 63 return p.Field.DefaultTags 64 } 65 66 // Count implemenents stats.Collector. 67 func (p Printer) Count(name string, value int64, tags ...string) error { 68 return p.writeln("count", name, fmt.Sprint(value), tags...) 69 } 70 71 // Increment implemenents stats.Collector. 72 func (p Printer) Increment(name string, tags ...string) error { 73 return p.writeln("increment", name, "1", tags...) 74 } 75 76 // Gauge implemenents stats.Collector. 77 func (p Printer) Gauge(name string, value float64, tags ...string) error { 78 return p.writeln("gauge", name, fmt.Sprint(value), tags...) 79 } 80 81 // Histogram implemenents stats.Collector. 82 func (p Printer) Histogram(name string, value float64, tags ...string) error { 83 return p.writeln("histogram", name, fmt.Sprint(value), tags...) 84 } 85 86 // Distribution implemenents stats.Collector. 87 func (p Printer) Distribution(name string, value float64, tags ...string) error { 88 return p.writeln("distribution", name, fmt.Sprint(value), tags...) 89 } 90 91 // TimeInMilliseconds implemenents stats.Collector. 92 func (p Printer) TimeInMilliseconds(name string, value time.Duration, tags ...string) error { 93 return p.writeln("timeInMilliseconds", name, value.String(), tags...) 94 } 95 96 func (p Printer) writeln(metricType, name, value string, tags ...string) (err error) { 97 if p.Output == nil { 98 return 99 } 100 tags = append(p.Field.DefaultTags, tags...) 101 if p.Field.Namespace != "" { 102 _, err = fmt.Fprintf(p.Output, "%s %s.%s %s %s\n", metricType, p.Field.Namespace, name, value, strings.Join(tags, ",")) 103 } else { 104 _, err = fmt.Fprintf(p.Output, "%s %s %s %s\n", metricType, name, value, strings.Join(tags, ",")) 105 } 106 return 107 } 108 109 // Flush implemenents stats.Collector. 110 func (p Printer) Flush() error { 111 return nil 112 } 113 114 // Close implemenents stats.Collector. 115 func (p Printer) Close() error { 116 return nil 117 }