github.com/blend/go-sdk@v1.20220411.3/stats/multicollector.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 "sort" 12 "strings" 13 "time" 14 ) 15 16 var ( 17 _ Collector = (*MultiCollector)(nil) 18 ) 19 20 // MultiCollector is a class that wraps a set of statsd collectors 21 type MultiCollector []Collector 22 23 // AddDefaultTag implements Taggable. 24 func (collectors MultiCollector) AddDefaultTag(name, value string) { 25 for _, collector := range collectors { 26 collector.AddDefaultTag(name, value) 27 } 28 } 29 30 // AddDefaultTags implements Taggable. 31 func (collectors MultiCollector) AddDefaultTags(tags ...string) { 32 for _, collector := range collectors { 33 collector.AddDefaultTags(tags...) 34 } 35 } 36 37 // DefaultTags returns the unique default tags for the collectors. 38 func (collectors MultiCollector) DefaultTags() (output []string) { 39 values := map[string]bool{} 40 for _, collector := range collectors { 41 for _, tag := range collector.DefaultTags() { 42 values[tag] = true 43 } 44 } 45 for key := range values { 46 output = append(output, key) 47 } 48 sort.Strings(output) 49 return 50 } 51 52 // HasTagKey returns if the collector has a given tag key 53 // in *any* collector's default tags. 54 func (collectors MultiCollector) HasTagKey(tagKey string) bool { 55 var key, value string 56 for _, collector := range collectors { 57 for _, tag := range collector.DefaultTags() { 58 key, value = SplitTag(tag) 59 if strings.EqualFold(tagKey, key) && value != "" { 60 return true 61 } 62 } 63 } 64 return false 65 } 66 67 // Count increments a counter by a value and writes to the collectors. 68 func (collectors MultiCollector) Count(name string, value int64, tags ...string) (err error) { 69 for _, collector := range collectors { 70 err = collector.Count(name, value, tags...) 71 if err != nil { 72 return 73 } 74 } 75 return 76 } 77 78 // Increment increments a counter by 1 and writes to the collectors. 79 func (collectors MultiCollector) Increment(name string, tags ...string) (err error) { 80 for _, collector := range collectors { 81 err = collector.Increment(name, tags...) 82 if err != nil { 83 return 84 } 85 } 86 return 87 } 88 89 // Gauge sets a gauge value and writes to the collectors. 90 func (collectors MultiCollector) Gauge(name string, value float64, tags ...string) (err error) { 91 for _, collector := range collectors { 92 err = collector.Gauge(name, value, tags...) 93 if err != nil { 94 return 95 } 96 } 97 return 98 } 99 100 // Histogram sets a histogram value and writes to the collectors. 101 func (collectors MultiCollector) Histogram(name string, value float64, tags ...string) (err error) { 102 for _, collector := range collectors { 103 err = collector.Histogram(name, value, tags...) 104 if err != nil { 105 return 106 } 107 } 108 return 109 } 110 111 // Distribution sets a distribution value and writes to the collectors. 112 func (collectors MultiCollector) Distribution(name string, value float64, tags ...string) (err error) { 113 for _, collector := range collectors { 114 err = collector.Distribution(name, value, tags...) 115 if err != nil { 116 return 117 } 118 } 119 return 120 } 121 122 // TimeInMilliseconds sets a timing value and writes to the different hosts 123 func (collectors MultiCollector) TimeInMilliseconds(name string, value time.Duration, tags ...string) (err error) { 124 for _, collector := range collectors { 125 err = collector.TimeInMilliseconds(name, value, tags...) 126 if err != nil { 127 return 128 } 129 } 130 return 131 } 132 133 // Flush forces a flush on all collectors. 134 func (collectors MultiCollector) Flush() (err error) { 135 for _, collector := range collectors { 136 err = collector.Flush() 137 if err != nil { 138 return 139 } 140 } 141 return 142 } 143 144 // Close closes all collectors. 145 func (collectors MultiCollector) Close() (err error) { 146 for _, collector := range collectors { 147 err = collector.Close() 148 if err != nil { 149 return 150 } 151 } 152 return 153 }