Direct proxy works

This commit is contained in:
9seconds
2019-09-04 10:19:01 +03:00
parent 07985cf418
commit 2492a47d0a
87 changed files with 1883 additions and 1447 deletions
-76
View File
@@ -1,76 +0,0 @@
package stats
import (
"net"
"github.com/9seconds/mtg/mtproto"
)
const (
connectionsChanLength = 10
trafficChanLength = 10
)
var (
crashesChan = make(chan struct{})
statsChan = make(chan chan<- Stats)
connectionsChan = make(chan connectionData, connectionsChanLength)
trafficChan = make(chan trafficData, trafficChanLength)
)
type connectionData struct {
connectionType mtproto.ConnectionType
connected bool
addr *net.TCPAddr
}
type trafficData struct {
traffic int
ingress bool
}
// NewCrash indicates new crash.
func NewCrash() {
crashesChan <- struct{}{}
}
// ClientConnected indicates that new client was connected.
func ClientConnected(connectionType mtproto.ConnectionType, addr *net.TCPAddr) {
connectionsChan <- connectionData{
connectionType: connectionType,
addr: addr,
connected: true,
}
}
// ClientDisconnected indicates that client was disconnected.
func ClientDisconnected(connectionType mtproto.ConnectionType, addr *net.TCPAddr) {
connectionsChan <- connectionData{
connectionType: connectionType,
addr: addr,
connected: false,
}
}
// IngressTraffic accounts new ingress traffic.
func IngressTraffic(traffic int) {
trafficChan <- trafficData{
traffic: traffic,
ingress: true,
}
}
// EgressTraffic accounts new ingress traffic.
func EgressTraffic(traffic int) {
trafficChan <- trafficData{
traffic: traffic,
ingress: false,
}
}
// GetStats returns a snapshot of Stats instance.
func GetStats() Stats {
rpcChan := make(chan Stats)
statsChan <- rpcChan
return <-rpcChan
}
-28
View File
@@ -1,28 +0,0 @@
package stats
import (
"github.com/juju/errors"
"github.com/9seconds/mtg/config"
)
// Init initializes stats subsystem.
func Init(conf *config.Config) error {
if conf.StatsD.Enabled {
client, err := newStatsd(conf)
if err != nil {
return errors.Annotate(err, "Cannot initialize statsd client")
}
go client.run()
}
prometheus, err := newPrometheus(conf)
if err != nil {
return errors.Annotate(err, "Cannot initialize prometheus client")
}
go prometheus.run()
go NewStats(conf).start()
go startServer(conf, prometheus.getHTTPHandler())
return nil
}
-91
View File
@@ -1,91 +0,0 @@
package stats
import (
"net/http"
"time"
"github.com/juju/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/9seconds/mtg/config"
)
const prometheusPollTime = time.Second
type prometheusExporter struct {
registry prometheus.Gatherer
connections *prometheus.GaugeVec
traffic *prometheus.GaugeVec
speed *prometheus.GaugeVec
crashes prometheus.Gauge
}
func (p *prometheusExporter) run() {
for range time.Tick(prometheusPollTime) {
instance := GetStats()
p.connections.WithLabelValues("abridged", "v4").Set(float64(instance.Connections.Abridged.IPv4))
p.connections.WithLabelValues("abridged", "v6").Set(float64(instance.Connections.Abridged.IPv6))
p.connections.WithLabelValues("intermediate", "v4").Set(float64(instance.Connections.Intermediate.IPv4))
p.connections.WithLabelValues("intermediate", "v6").Set(float64(instance.Connections.Intermediate.IPv6))
p.connections.WithLabelValues("secure", "v4").Set(float64(instance.Connections.Secure.IPv4))
p.connections.WithLabelValues("secure", "v6").Set(float64(instance.Connections.Secure.IPv6))
p.traffic.WithLabelValues("ingress").Set(float64(instance.Traffic.ingress))
p.traffic.WithLabelValues("egress").Set(float64(instance.Traffic.egress))
p.speed.WithLabelValues("ingress").Set(float64(instance.Speed.ingress))
p.speed.WithLabelValues("egress").Set(float64(instance.Speed.egress))
p.crashes.Set(float64(instance.Crashes))
}
}
func (p *prometheusExporter) getHTTPHandler() http.Handler {
return promhttp.HandlerFor(p.registry, promhttp.HandlerOpts{})
}
func newPrometheus(conf *config.Config) (*prometheusExporter, error) {
registry := prometheus.NewRegistry()
connections := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: conf.Prometheus.Prefix,
Name: "connections",
Help: "Current number of connections to the proxy.",
}, []string{"type", "protocol"})
traffic := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: conf.Prometheus.Prefix,
Name: "traffic",
Help: "Traffic passed through the proxy in bytes.",
}, []string{"direction"})
speed := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: conf.Prometheus.Prefix,
Name: "speed",
Help: "Current throughput in bytes per second.",
}, []string{"direction"})
crashes := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: conf.Prometheus.Prefix,
Name: "crashes",
Help: "How many crashes happened.",
})
if err := registry.Register(connections); err != nil {
return nil, errors.Annotate(err, "Cannot register connections collector")
}
if err := registry.Register(traffic); err != nil {
return nil, errors.Annotate(err, "cannot register traffic collector")
}
if err := registry.Register(speed); err != nil {
return nil, errors.Annotate(err, "cannot register speed collector")
}
if err := registry.Register(crashes); err != nil {
return nil, errors.Annotate(err, "cannot register crashes collector")
}
return &prometheusExporter{
registry: registry,
connections: connections,
traffic: traffic,
speed: speed,
crashes: crashes,
}, nil
}
-40
View File
@@ -1,40 +0,0 @@
package stats
import (
"encoding/json"
"net/http"
"go.uber.org/zap"
"github.com/9seconds/mtg/config"
)
func startServer(conf *config.Config, prometheusHandler http.Handler) {
log := zap.S().Named("stats")
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
first, err := json.Marshal(GetStats())
if err != nil {
log.Errorw("Cannot encode json", "error", err)
http.Error(w, "Internal server error", 500)
return
}
interim := map[string]interface{}{}
json.Unmarshal(first, &interim) // nolint: errcheck, gosec
encoder := json.NewEncoder(w)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
if err = encoder.Encode(interim); err != nil {
log.Errorw("Cannot encode json", "error", err)
}
})
http.Handle("/prometheus/", prometheusHandler)
if err := http.ListenAndServe(conf.StatAddr(), nil); err != nil {
log.Fatalw("Stats server has been stopped", "error", err)
}
}
+65 -147
View File
@@ -1,175 +1,93 @@
package stats
import (
"encoding/json"
"fmt"
"strconv"
"time"
"net"
"net/http"
humanize "github.com/dustin/go-humanize"
"github.com/juju/errors"
"github.com/9seconds/mtg/config"
"github.com/9seconds/mtg/mtproto"
"github.com/9seconds/mtg/conntypes"
)
type uptime time.Time
func (u uptime) MarshalJSON() ([]byte, error) {
duration := time.Since(time.Time(u))
value := map[string]string{
"seconds": strconv.Itoa(int(duration.Seconds())),
"human": humanize.Time(time.Time(u)),
}
return json.Marshal(value)
type Stats interface {
IngressTraffic(int)
EgressTraffic(int)
ClientConnected(conntypes.ConnectionType, *net.TCPAddr)
ClientDisconnected(conntypes.ConnectionType, *net.TCPAddr)
Crash()
AntiReplayDetected()
}
type connectionType struct {
IPv6 uint32 `json:"ipv6"`
IPv4 uint32 `json:"ipv4"`
}
type multiStats []Stats
type baseConnections struct {
All connectionType `json:"all"`
Abridged connectionType `json:"abridged"`
Intermediate connectionType `json:"intermediate"`
Secure connectionType `json:"secure"`
}
type connections struct {
baseConnections
}
func (c connections) MarshalJSON() ([]byte, error) {
c.All.IPv4 = c.Abridged.IPv4 + c.Intermediate.IPv4 + c.Secure.IPv4
c.All.IPv6 = c.Abridged.IPv6 + c.Intermediate.IPv6 + c.Secure.IPv6
return json.Marshal(c.baseConnections)
}
type traffic struct {
ingress uint64
egress uint64
}
func (t *traffic) dumpValue(value uint64) map[string]interface{} {
return map[string]interface{}{
"bytes": value,
"human": humanize.Bytes(value),
func (m multiStats) IngressTraffic(traffic int) {
for i := range m {
go m[i].IngressTraffic(traffic)
}
}
func (t traffic) MarshalJSON() ([]byte, error) {
value := map[string]map[string]interface{}{
"ingress": t.dumpValue(t.ingress),
"egress": t.dumpValue(t.egress),
}
return json.Marshal(value)
}
type speed struct {
ingress uint64
egress uint64
}
func (s *speed) dumpValue(value uint64) map[string]interface{} {
return map[string]interface{}{
"bytes/s": value,
"human": fmt.Sprintf("%s/s", humanize.Bytes(value)),
func (m multiStats) EgressTraffic(traffic int) {
for i := range m {
go m[i].EgressTraffic(traffic)
}
}
func (s speed) MarshalJSON() ([]byte, error) {
value := map[string]map[string]interface{}{
"ingress": s.dumpValue(s.ingress),
"egress": s.dumpValue(s.egress),
func (m multiStats) ClientConnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
for i := range m {
go m[i].ClientConnected(connectionType, addr)
}
}
func (m multiStats) ClientDisconnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
for i := range m {
go m[i].ClientDisconnected(connectionType, addr)
}
}
func (m multiStats) Crash() {
for i := range m {
go m[i].Crash()
}
}
func (m multiStats) AntiReplayDetected() {
for i := range m {
go m[i].AntiReplayDetected()
}
}
var S Stats
func Init() error {
mux := http.NewServeMux()
instanceJSON := newStatsJSON(mux)
instancePrometheus, err := newStatsPrometheus(mux)
if err != nil {
return errors.Annotate(err, "Cannot initialize Prometheus")
}
return json.Marshal(value)
}
// Stats represents a statistics of the proxy.
type Stats struct {
URLs config.IPURLs `json:"urls"`
Connections connections `json:"connections"`
Traffic traffic `json:"traffic"`
Speed speed `json:"speed"`
Uptime uptime `json:"uptime"`
Crashes uint32 `json:"crashes"`
previousTraffic traffic
}
func (s *Stats) start() {
speedChan := time.Tick(time.Second)
for {
select {
case <-speedChan:
s.handleSpeed()
case event := <-trafficChan:
s.handleTraffic(event)
case event := <-connectionsChan:
s.handleConnection(event)
case getStatsChan := <-statsChan:
s.handleGetStats(getStatsChan)
case <-crashesChan:
s.handleCrash()
stats := []Stats{instanceJSON, instancePrometheus}
if config.C.StatsdStats.Addr.IP != nil {
instanceStatsd, err := newStatsStatsd()
if err != nil {
return errors.Annotate(err, "Cannot initialize StatsD")
}
}
}
func (s *Stats) handleTraffic(evt trafficData) {
if evt.ingress {
s.Traffic.ingress += uint64(evt.traffic)
} else {
s.Traffic.egress += uint64(evt.traffic)
}
}
func (s *Stats) handleSpeed() {
s.Speed.ingress = s.Traffic.ingress - s.previousTraffic.ingress
s.Speed.egress = s.Traffic.egress - s.previousTraffic.egress
s.previousTraffic.ingress = s.Traffic.ingress
s.previousTraffic.egress = s.Traffic.egress
}
func (s *Stats) handleConnection(evt connectionData) {
var inc uint32 = 1
if !evt.connected {
inc = ^uint32(0)
stats = append(stats, instanceStatsd)
}
var conn *connectionType
switch evt.connectionType {
case mtproto.ConnectionTypeAbridged:
conn = &s.Connections.Abridged
case mtproto.ConnectionTypeSecure:
conn = &s.Connections.Secure
default:
conn = &s.Connections.Intermediate
listener, err := net.Listen("tcp", config.C.StatsAddr.String())
if err != nil {
return errors.Annotate(err, "Cannot initialize stats server")
}
if evt.addr.IP.To4() != nil {
conn.IPv4 += inc
} else {
conn.IPv6 += inc
srv := http.Server{
Handler: mux,
}
}
go srv.Serve(listener) // nolint: errcheck
func (s *Stats) handleGetStats(getStatsChan chan<- Stats) {
getStatsChan <- *s
}
S = multiStats(stats)
func (s *Stats) handleCrash() {
s.Crashes++
}
// NewStats creates a new instance of Stats structure.
func NewStats(conf *config.Config) *Stats {
return &Stats{
URLs: conf.GetURLs(),
Uptime: uptime(time.Now()),
}
return nil
}
+131
View File
@@ -0,0 +1,131 @@
package stats
import (
"encoding/json"
"net"
"net/http"
"strconv"
"sync/atomic"
"time"
"go.uber.org/zap"
"github.com/9seconds/mtg/conntypes"
)
type statsJSON struct {
Connections statsJSONConnections `json:"connections"`
Traffic statsJSONTraffic `json:"traffic"`
Uptime statsJSONUptime `json:"uptime"`
Crashes uint32 `json:"crashes"`
AntiReplays uint32 `json:"anti_replay_detected"`
}
type statsBaseJSONConnections struct {
All statsJSONConnectionType `json:"all"`
Abridged statsJSONConnectionType `json:"abridged"`
Intermediate statsJSONConnectionType `json:"intermediate"`
Secured statsJSONConnectionType `json:"secured"`
}
type statsJSONConnections struct {
statsBaseJSONConnections
}
type statsJSONConnectionType struct {
IPv4 uint32 `json:"ipv4"`
IPv6 uint32 `json:"ipv6"`
}
func (c statsJSONConnections) MarshalJSON() ([]byte, error) {
c.All.IPv4 = c.Abridged.IPv4 + c.Intermediate.IPv4 + c.Secured.IPv4
c.All.IPv6 = c.Abridged.IPv6 + c.Intermediate.IPv6 + c.Secured.IPv6
return json.Marshal(c.statsBaseJSONConnections)
}
type statsJSONTraffic struct {
Ingress uint64 `json:"ingress"`
Egress uint64 `json:"egress"`
}
type statsJSONUptime time.Time
func (s statsJSONUptime) MarshalJSON() ([]byte, error) {
seconds := strconv.Itoa(int(time.Since(time.Time(s)).Seconds()))
return []byte(seconds), nil
}
func (s *statsJSON) IngressTraffic(traffic int) {
atomic.AddUint64(&s.Traffic.Ingress, uint64(traffic))
}
func (s *statsJSON) EgressTraffic(traffic int) {
atomic.AddUint64(&s.Traffic.Egress, uint64(traffic))
}
func (s *statsJSON) ClientConnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
s.changeConnections(connectionType, addr, 1)
}
func (s *statsJSON) ClientDisconnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
s.changeConnections(connectionType, addr, ^uint32(0))
}
func (s *statsJSON) changeConnections(connectionType conntypes.ConnectionType, addr *net.TCPAddr, value uint32) {
var connections *statsJSONConnectionType
switch connectionType {
case conntypes.ConnectionTypeAbridged:
connections = &s.Connections.Abridged
case conntypes.ConnectionTypeSecure:
connections = &s.Connections.Secured
default:
connections = &s.Connections.Intermediate
}
if addr.IP.To4() != nil {
atomic.AddUint32(&connections.IPv4, value)
} else {
atomic.AddUint32(&connections.IPv6, value)
}
}
func (s *statsJSON) Crash() {
atomic.AddUint32(&s.Crashes, 1)
}
func (s *statsJSON) AntiReplayDetected() {
atomic.AddUint32(&s.AntiReplays, 1)
}
func newStatsJSON(mux *http.ServeMux) Stats {
instance := &statsJSON{
Uptime: statsJSONUptime(time.Now()),
}
logger := zap.S().Named("stats")
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
first, err := json.Marshal(instance)
if err != nil {
logger.Errorw("Cannot encode json", "error", err)
http.Error(w, "Internal server error", http.StatusServiceUnavailable)
return
}
interim := map[string]interface{}{}
if err := json.Unmarshal(first, &interim); err != nil {
panic(err)
}
encoder := json.NewEncoder(w)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
if err := encoder.Encode(interim); err != nil {
logger.Errorw("Cannot encode json", "error", err)
}
})
return instance
}
+110
View File
@@ -0,0 +1,110 @@
package stats
import (
"net"
"net/http"
"github.com/juju/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/9seconds/mtg/config"
"github.com/9seconds/mtg/conntypes"
)
type statsPrometheus struct {
connections *prometheus.GaugeVec
traffic *prometheus.GaugeVec
crashes prometheus.Gauge
antiReplays prometheus.Gauge
}
func (s *statsPrometheus) IngressTraffic(traffic int) {
s.traffic.WithLabelValues("ingress").Add(float64(traffic))
}
func (s *statsPrometheus) EgressTraffic(traffic int) {
s.traffic.WithLabelValues("egress").Add(float64(traffic))
}
func (s *statsPrometheus) ClientConnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
s.changeConnections(connectionType, addr, 1.0)
}
func (s *statsPrometheus) ClientDisconnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
s.changeConnections(connectionType, addr, -1.0)
}
func (s *statsPrometheus) changeConnections(connectionType conntypes.ConnectionType,
addr *net.TCPAddr,
increment float64) {
var labels [2]string
switch connectionType {
case conntypes.ConnectionTypeAbridged:
labels[0] = "abridged"
case conntypes.ConnectionTypeSecure:
labels[0] = "secured"
default:
labels[0] = "intermediate"
}
labels[1] = "ipv4"
if addr.IP.To4() == nil {
labels[1] = "ipv6"
}
s.connections.WithLabelValues(labels[:]...).Add(increment)
}
func (s *statsPrometheus) Crash() {
s.crashes.Inc()
}
func (s *statsPrometheus) AntiReplayDetected() {
s.antiReplays.Inc()
}
func newStatsPrometheus(mux *http.ServeMux) (Stats, error) {
registry := prometheus.NewRegistry()
instance := &statsPrometheus{
connections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: config.C.PrometheusStats.Prefix,
Name: "connections",
Help: "Current number of connections to the proxy.",
}, []string{"type", "protocol"}),
traffic: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: config.C.PrometheusStats.Prefix,
Name: "traffic",
Help: "Traffic passed through the proxy in bytes.",
}, []string{"direction"}),
crashes: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: config.C.PrometheusStats.Prefix,
Name: "crashes",
Help: "How many crashes happened.",
}),
antiReplays: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: config.C.PrometheusStats.Prefix,
Name: "anti_replays",
Help: "How many anti replay attacks were prevented.",
}),
}
if err := registry.Register(instance.connections); err != nil {
return nil, errors.Annotate(err, "Cannot register metrics for connections")
}
if err := registry.Register(instance.traffic); err != nil {
return nil, errors.Annotate(err, "Cannot register metrics for traffic")
}
if err := registry.Register(instance.crashes); err != nil {
return nil, errors.Annotate(err, "Cannot register metrics for crashes")
}
if err := registry.Register(instance.antiReplays); err != nil {
return nil, errors.Annotate(err, "Cannot register metrics for anti replays")
}
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
mux.Handle("/prometheus", handler)
return instance, nil
}
+87
View File
@@ -0,0 +1,87 @@
package stats
import (
"net"
"strings"
"github.com/juju/errors"
"gopkg.in/alexcesaro/statsd.v2"
"github.com/9seconds/mtg/config"
"github.com/9seconds/mtg/conntypes"
)
type statsStatsd struct {
client *statsd.Client
}
func (s *statsStatsd) IngressTraffic(traffic int) {
s.client.Count("traffic.ingress", traffic)
}
func (s *statsStatsd) EgressTraffic(traffic int) {
s.client.Count("traffic.egress", traffic)
}
func (s *statsStatsd) ClientConnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
s.changeConnections(connectionType, addr, 1)
}
func (s *statsStatsd) ClientDisconnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
s.changeConnections(connectionType, addr, -1)
}
func (s *statsStatsd) changeConnections(connectionType conntypes.ConnectionType, addr *net.TCPAddr, value int) {
var labels [3]string
labels[0] = "connections"
switch connectionType {
case conntypes.ConnectionTypeAbridged:
labels[1] = "abridged"
case conntypes.ConnectionTypeSecure:
labels[1] = "secured"
default:
labels[1] = "intermediate"
}
labels[2] = "ipv4"
if addr.IP.To4() == nil {
labels[2] = "ipv6"
}
s.client.Count(strings.Join(labels[:], "."), value)
}
func (s *statsStatsd) Crash() {
s.client.Increment("crashes")
}
func (s *statsStatsd) AntiReplayDetected() {
s.client.Increment("anti_replays")
}
func newStatsStatsd() (Stats, error) {
options := []statsd.Option{
statsd.Prefix(config.C.StatsdStats.Prefix),
statsd.Network(config.C.StatsdStats.Addr.Network()),
statsd.Address(config.C.StatsdStats.Addr.String()),
statsd.TagsFormat(config.C.StatsdStats.TagsFormat),
}
if len(config.C.StatsdStats.Tags) > 0 {
tags := make([]string, len(config.C.StatsdStats.Tags)*2)
for k, v := range config.C.StatsdStats.Tags {
tags = append(tags, k, v)
}
options = append(options, statsd.Tags(tags...))
}
client, err := statsd.New(options...)
if err != nil {
return nil, errors.Annotate(err, "Cannot initialize a client")
}
return &statsStatsd{
client: client,
}, nil
}
-77
View File
@@ -1,77 +0,0 @@
package stats
import (
"time"
"github.com/juju/errors"
statsd "gopkg.in/alexcesaro/statsd.v2"
"github.com/9seconds/mtg/config"
)
const (
statsdConnectionsAbridgedV4 = "connections.abridged.ipv4"
statsdConnectionsAbridgedV6 = "connections.abridged.ipv6"
statsdConnectionsIntermediateV4 = "connections.intermediate.ipv4"
statsdConnectionsIntermediateV6 = "connections.intermediate.ipv6"
statsdConnectionsSecureV4 = "connections.secure.ipv4"
statsdConnectionsSecureV6 = "connections.secure.ipv6"
statsdTrafficIngress = "traffic.ingress"
statsdTrafficEgress = "traffic.egress"
statsdSpeedIngress = "speed.ingress"
statsdSpeedEgress = "speed.egress"
statsdCrashes = "crashes"
)
const statsdPollTime = time.Second
type statsdExporter struct {
client *statsd.Client
}
func (s *statsdExporter) run() {
for range time.Tick(statsdPollTime) {
instance := GetStats()
s.client.Gauge(statsdConnectionsAbridgedV4, instance.Connections.Abridged.IPv4)
s.client.Gauge(statsdConnectionsAbridgedV6, instance.Connections.Abridged.IPv6)
s.client.Gauge(statsdConnectionsIntermediateV4, instance.Connections.Intermediate.IPv4)
s.client.Gauge(statsdConnectionsIntermediateV6, instance.Connections.Intermediate.IPv6)
s.client.Gauge(statsdConnectionsSecureV4, instance.Connections.Secure.IPv4)
s.client.Gauge(statsdConnectionsSecureV6, instance.Connections.Secure.IPv6)
s.client.Gauge(statsdTrafficIngress, instance.Traffic.ingress)
s.client.Gauge(statsdTrafficEgress, instance.Traffic.egress)
s.client.Gauge(statsdSpeedIngress, instance.Speed.ingress)
s.client.Gauge(statsdSpeedEgress, instance.Speed.egress)
s.client.Gauge(statsdCrashes, instance.Crashes)
}
}
func newStatsd(conf *config.Config) (*statsdExporter, error) {
options := []statsd.Option{
statsd.Network(conf.StatsD.Addr.Network()),
statsd.Address(conf.StatsD.Addr.String()),
statsd.Prefix(conf.StatsD.Prefix),
}
if conf.StatsD.TagsFormat > 0 {
options = append(options, statsd.TagsFormat(conf.StatsD.TagsFormat))
tags := make([]string, len(conf.StatsD.Tags)*2)
for k, v := range conf.StatsD.Tags {
tags = append(tags, k, v)
}
options = append(options, statsd.Tags(tags...))
}
client, err := statsd.New(options...)
if err != nil {
return nil, errors.Annotate(err, "Cannot create statsd client")
}
return &statsdExporter{client: client}, nil
}