forked from cloudflare/privacy-gateway-server-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statsd_metrics.go
54 lines (44 loc) · 1.35 KB
/
statsd_metrics.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
package main
import (
"fmt"
"log"
"time"
"github.com/DataDog/datadog-go/v5/statsd"
)
type StatsDMetrics struct {
serviceName string
metricsName string
eventName string
startedAt time.Time
client statsd.ClientInterface
}
func (s *StatsDMetrics) Fire(result string) {
tags := []string{fmt.Sprintf("event_name:%s", s.eventName), fmt.Sprintf("result:%s", result), fmt.Sprintf("service:%s", s.serviceName)}
err := s.client.TimeInMilliseconds(s.metricsName, float64(time.Since(s.startedAt).Milliseconds()), tags, 1)
if err != nil {
log.Printf("Cannot send metrics to statsd: %s", err)
}
}
func (s *StatsDMetrics) ResponseStatus(prefix string, status int) {
s.Fire(fmt.Sprintf("%s_response_status_%d", prefix, status))
}
func createStatsDClient(host, port string, timeout int) (statsd.ClientInterface, error) {
if host == "" || port == "" {
return &statsd.NoOpClient{}, nil
}
return statsd.New(host+":"+port, statsd.WithWriteTimeout(time.Duration(timeout)*time.Millisecond), statsd.WithoutTelemetry())
}
type StatsDMetricsFactory struct {
serviceName string
metricsName string
client statsd.ClientInterface
}
func (f StatsDMetricsFactory) Create(eventName string) Metrics {
return &StatsDMetrics{
serviceName: f.serviceName,
metricsName: f.metricsName,
eventName: eventName,
startedAt: time.Now(),
client: f.client,
}
}