This repository was archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathpublish.go
67 lines (56 loc) · 1.4 KB
/
publish.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
package publish
import (
"strconv"
schema "github.com/grafana/metrictank/schema"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
log "github.com/sirupsen/logrus"
)
var (
ingestedMetrics = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "gateway",
Name: "samples_ingested_total",
Help: "Number of samples ingested",
}, []string{"org"})
)
type Publisher interface {
Publish(metrics []*schema.MetricData) error
Type() string
}
var (
publisher Publisher
)
func Init(p Publisher) {
if p == nil {
publisher = &nullPublisher{}
} else {
publisher = p
}
log.Infof("using %s publisher", publisher.Type())
}
func Publish(metrics []*schema.MetricData) error {
if len(metrics) == 0 {
return nil
}
if err := publisher.Publish(metrics); err != nil {
return err
}
// capture accounting data.
orgCounts := make(map[int]int32)
for _, m := range metrics {
orgCounts[m.OrgId]++
}
for org, count := range orgCounts {
ingestedMetrics.WithLabelValues(strconv.Itoa(org)).Add(float64(count))
}
return nil
}
// nullPublisher drops all metrics passed through the publish interface
type nullPublisher struct{}
func (*nullPublisher) Publish(metrics []*schema.MetricData) error {
log.Debugf("publishing not enabled, dropping %d metrics", len(metrics))
return nil
}
func (*nullPublisher) Type() string {
return "nullPublisher"
}