forked from topfreegames/pitaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statsd_reporter.go
149 lines (124 loc) · 4.24 KB
/
statsd_reporter.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
// Copyright (c) TFG Co. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package metrics
import (
"fmt"
"os"
"strconv"
"github.com/DataDog/datadog-go/statsd"
"github.com/topfreegames/pitaya/config"
"github.com/topfreegames/pitaya/logger"
)
// Client is the interface to required dogstatsd functions
type Client interface {
Count(name string, value int64, tags []string, rate float64) error
Gauge(name string, value float64, tags []string, rate float64) error
TimeInMilliseconds(name string, value float64, tags []string, rate float64) error
}
// StatsdReporter sends application metrics to statsd
type StatsdReporter struct {
client Client
rate float64
serverType string
hostname string
defaultTags []string
}
// NewStatsdReporter returns an instance of statsd reportar and an error if something fails
func NewStatsdReporter(
config *config.Config,
serverType string,
tagsMap map[string]string,
clientOrNil ...Client,
) (*StatsdReporter, error) {
host := config.GetString("pitaya.metrics.statsd.host")
prefix := config.GetString("pitaya.metrics.statsd.prefix")
rate, err := strconv.ParseFloat(config.GetString("pitaya.metrics.statsd.rate"), 64)
if err != nil {
return nil, err
}
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
sr := &StatsdReporter{
rate: rate,
serverType: serverType,
hostname: hostname,
}
sr.buildDefaultTags(tagsMap)
if len(clientOrNil) > 0 {
sr.client = clientOrNil[0]
} else {
c, err := statsd.New(host)
if err != nil {
return nil, err
}
c.Namespace = prefix
sr.client = c
}
return sr, nil
}
func (s *StatsdReporter) buildDefaultTags(tagsMap map[string]string) {
defaultTags := make([]string, len(tagsMap)+2)
defaultTags[0] = fmt.Sprintf("serverType:%s", s.serverType)
defaultTags[1] = fmt.Sprintf("hostname:%s", s.hostname)
idx := 2
for k, v := range tagsMap {
defaultTags[idx] = fmt.Sprintf("%s:%s", k, v)
idx++
}
s.defaultTags = defaultTags
}
// ReportCount sends count reports to statsd
func (s *StatsdReporter) ReportCount(metric string, tagsMap map[string]string, count float64) error {
fullTags := s.defaultTags
for k, v := range tagsMap {
fullTags = append(fullTags, fmt.Sprintf("%s:%s", k, v))
}
err := s.client.Count(metric, int64(count), fullTags, s.rate)
if err != nil {
logger.Log.Errorf("failed to report count: %q", err)
}
return err
}
// ReportGauge sents the gauge value and reports to statsd
func (s *StatsdReporter) ReportGauge(metric string, tagsMap map[string]string, value float64) error {
fullTags := s.defaultTags
for k, v := range tagsMap {
fullTags = append(fullTags, fmt.Sprintf("%s:%s", k, v))
}
err := s.client.Gauge(metric, value, fullTags, s.rate)
if err != nil {
logger.Log.Errorf("failed to report gauge: %q", err)
}
return err
}
// ReportSummary observes the summary value and reports to statsd
func (s *StatsdReporter) ReportSummary(metric string, tagsMap map[string]string, value float64) error {
fullTags := s.defaultTags
for k, v := range tagsMap {
fullTags = append(fullTags, fmt.Sprintf("%s:%s", k, v))
}
err := s.client.TimeInMilliseconds(metric, float64(value), fullTags, s.rate)
if err != nil {
logger.Log.Errorf("failed to report summary: %q", err)
}
return err
}