-
Notifications
You must be signed in to change notification settings - Fork 106
/
outputprometheus.go
74 lines (62 loc) · 1.73 KB
/
outputprometheus.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
package outputprometheus
import (
"context"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/goglog"
"github.com/tsaikd/gogstash/config/logevent"
)
// ModuleName is the name used in config file
const ModuleName = "prometheus"
// OutputConfig holds the configuration json fields and internal objects
type OutputConfig struct {
config.OutputConfig
Address string `json:"address,omitempty"`
MsgCount prometheus.Counter `json:"-"`
}
// DefaultOutputConfig returns an OutputConfig struct with default values
func DefaultOutputConfig() OutputConfig {
return OutputConfig{
OutputConfig: config.OutputConfig{
CommonConfig: config.CommonConfig{
Type: ModuleName,
},
},
Address: ":8080",
MsgCount: prometheus.NewCounter(prometheus.CounterOpts{
Name: "processed_messages_total",
Help: "Number of processed messages",
}),
}
}
// InitHandler initialize the output plugin
func InitHandler(
ctx context.Context,
raw config.ConfigRaw,
control config.Control,
) (config.TypeOutputConfig, error) {
conf := DefaultOutputConfig()
if err := config.ReflectConfig(raw, &conf); err != nil {
return nil, err
}
if err := prometheus.Register(conf.MsgCount); err != nil {
return nil, err
}
go conf.serveHTTP()
return &conf, nil
}
// Output event
func (o *OutputConfig) Output(ctx context.Context, event logevent.LogEvent) (err error) {
o.MsgCount.Inc()
return
}
func (o *OutputConfig) serveHTTP() {
logger := goglog.Logger
http.Handle("/metrics", promhttp.Handler())
logger.Infof("Listen %s", o.Address)
if err := http.ListenAndServe(o.Address, nil); err != nil {
logger.Fatal(err)
}
}