This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
/
riemann.go
77 lines (59 loc) · 1.95 KB
/
riemann.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
package emitter
import (
"fmt"
"net"
"code.cloudfoundry.org/lager"
"github.com/The-Cloud-Source/goryman"
"github.com/concourse/atc/metric"
)
type RiemannEmitter struct {
client *goryman.GorymanClient
connected bool
servicePrefix string
tags []string
}
type RiemannConfig struct {
Host string `long:"riemann-host" description:"Riemann server address to emit metrics to."`
Port uint16 `long:"riemann-port" default:"5555" description:"Port of the Riemann server to emit metrics to."`
ServicePrefix string `long:"riemann-service-prefix" default:"" description:"An optional prefix for emitted Riemann services"`
Tags []string `long:"riemann-tag" description:"Tag to attach to emitted metrics. Can be specified multiple times." value-name:"TAG"`
}
func init() {
metric.RegisterEmitter(&RiemannConfig{})
}
func (config *RiemannConfig) Description() string { return "Riemann" }
func (config *RiemannConfig) IsConfigured() bool { return config.Host != "" }
func (config *RiemannConfig) NewEmitter() (metric.Emitter, error) {
return &RiemannEmitter{
client: goryman.NewGorymanClient(net.JoinHostPort(config.Host, fmt.Sprintf("%d", config.Port))),
connected: false,
servicePrefix: config.ServicePrefix,
tags: config.Tags,
}, nil
}
func (emitter *RiemannEmitter) Emit(logger lager.Logger, event metric.Event) {
if !emitter.connected {
err := emitter.client.Connect()
if err != nil {
logger.Error("connection-failed", err)
return
}
emitter.connected = true
}
err := emitter.client.SendEvent(&goryman.Event{
Service: emitter.servicePrefix + event.Name,
Metric: event.Value,
State: string(event.State),
Attributes: event.Attributes,
Host: event.Host,
Time: event.Time.Unix(),
Tags: emitter.tags,
})
if err != nil {
logger.Error("failed-to-emit", err)
if err := emitter.client.Close(); err != nil {
logger.Error("failed-to-close", err)
}
emitter.connected = false
}
}