|
| 1 | +package tally |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + smirastatsd "github.com/smira/go-statsd" |
| 8 | + "github.com/uber-go/tally/v4" |
| 9 | + "go.uber.org/fx" |
| 10 | + "go.uber.org/zap" |
| 11 | + |
| 12 | + "github.com/coinbase/chainstorage/internal/config" |
| 13 | +) |
| 14 | + |
| 15 | +type ( |
| 16 | + StatsReporterParams struct { |
| 17 | + fx.In |
| 18 | + Lifecycle fx.Lifecycle |
| 19 | + Logger *zap.Logger |
| 20 | + Config *config.Config |
| 21 | + } |
| 22 | + |
| 23 | + reporter struct { |
| 24 | + client *smirastatsd.Client |
| 25 | + } |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + reportingInterval = time.Second |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + // hardcoding this to be datadog format |
| 34 | + // we need think about whats the best way to set it up in config such that |
| 35 | + // when we switch reporter impl, config will still be backward compatible |
| 36 | + tagFormat = smirastatsd.TagFormatDatadog |
| 37 | +) |
| 38 | + |
| 39 | +func NewStatsReporter(params StatsReporterParams) tally.StatsReporter { |
| 40 | + if params.Config.StatsD == nil { |
| 41 | + return tally.NullStatsReporter |
| 42 | + } |
| 43 | + cfg := params.Config.StatsD |
| 44 | + client := smirastatsd.NewClient( |
| 45 | + cfg.Address, |
| 46 | + smirastatsd.MetricPrefix(cfg.Prefix), |
| 47 | + smirastatsd.TagStyle(tagFormat), |
| 48 | + smirastatsd.ReportInterval(reportingInterval), |
| 49 | + ) |
| 50 | + params.Logger.Info("initialized statsd client") |
| 51 | + params.Lifecycle.Append(fx.Hook{ |
| 52 | + OnStop: func(ctx context.Context) error { |
| 53 | + return client.Close() |
| 54 | + }, |
| 55 | + }) |
| 56 | + return &reporter{ |
| 57 | + client: client, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func convertTags(tagsMap map[string]string) []smirastatsd.Tag { |
| 62 | + tags := make([]smirastatsd.Tag, 0, len(tagsMap)) |
| 63 | + for key, value := range tagsMap { |
| 64 | + tags = append(tags, smirastatsd.StringTag(key, value)) |
| 65 | + } |
| 66 | + return tags |
| 67 | +} |
| 68 | + |
| 69 | +func (r *reporter) ReportCounter(name string, tags map[string]string, value int64) { |
| 70 | + r.client.Incr(name, value, convertTags(tags)...) |
| 71 | +} |
| 72 | + |
| 73 | +func (r *reporter) ReportGauge(name string, tags map[string]string, value float64) { |
| 74 | + r.client.FGauge(name, value, convertTags(tags)...) |
| 75 | +} |
| 76 | + |
| 77 | +func (r *reporter) ReportTimer(name string, tags map[string]string, value time.Duration) { |
| 78 | + r.client.PrecisionTiming(name, value, convertTags(tags)...) |
| 79 | +} |
| 80 | + |
| 81 | +func (r *reporter) ReportHistogramValueSamples( |
| 82 | + name string, |
| 83 | + tags map[string]string, |
| 84 | + buckets tally.Buckets, |
| 85 | + bucketLowerBound, |
| 86 | + bucketUpperBound float64, |
| 87 | + samples int64) { |
| 88 | + panic("no implemented") |
| 89 | +} |
| 90 | + |
| 91 | +func (r *reporter) ReportHistogramDurationSamples( |
| 92 | + name string, |
| 93 | + tags map[string]string, |
| 94 | + buckets tally.Buckets, |
| 95 | + bucketLowerBound, |
| 96 | + bucketUpperBound time.Duration, |
| 97 | + samples int64) { |
| 98 | + panic("no implemented") |
| 99 | +} |
| 100 | + |
| 101 | +func (r *reporter) Capabilities() tally.Capabilities { |
| 102 | + return r |
| 103 | +} |
| 104 | + |
| 105 | +func (r *reporter) Reporting() bool { |
| 106 | + return true |
| 107 | +} |
| 108 | + |
| 109 | +func (r *reporter) Tagging() bool { |
| 110 | + return true |
| 111 | +} |
| 112 | + |
| 113 | +func (r *reporter) Flush() { |
| 114 | + // no-op |
| 115 | +} |
0 commit comments