forked from atongen/prom_multi_proc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprom_multi_proc.go
More file actions
158 lines (137 loc) · 3.07 KB
/
prom_multi_proc.go
File metadata and controls
158 lines (137 loc) · 3.07 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"github.com/prometheus/client_golang/prometheus"
)
var (
logCloser io.WriteCloser
logger *log.Logger
metricsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "pmp_metrics_total",
Help: "Total count of metrics processed by status",
},
[]string{"status"},
)
)
type MetricSpec struct {
Type string `json:"type"`
Name string `json:"name"`
Help string `json:"help"`
Labels []string `json:"labels"`
Buckets []float64 `json:"buckets"`
Objectives map[string]float64 `json:"objectives"`
}
type Metric struct {
Name string `json:"name"`
LabelValues []string `json:"label_values"`
Method string `json:"method"`
Value float64 `json:"value"`
}
type nopCloser struct {
io.Writer
}
func (nopCloser) Close() error {
return nil
}
func CountMetric(status string) {
metricsTotal.WithLabelValues(status).Inc()
}
func SetLogger(file string) error {
if logCloser != nil {
logCloser.Close()
}
var err error
if file == "" {
var b bytes.Buffer
logCloser = nopCloser{&b}
logger = log.New(os.Stdout, "", log.LstdFlags)
} else {
logCloser, err = os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return fmt.Errorf("Error opening log file (%s): %s", file, err)
}
logger = log.New(logCloser, "", log.LstdFlags)
}
return nil
}
func LoadSpecs(file string) ([]*MetricSpec, error) {
var (
specs []*MetricSpec
err error
)
specsFile, err := os.OpenFile(file, os.O_RDONLY, 0644)
if err != nil {
return specs, err
}
defer specsFile.Close()
return ReadSpecs(specsFile)
}
func ReadSpecs(r io.Reader) ([]*MetricSpec, error) {
var result []*MetricSpec
jsonBlob, err := ioutil.ReadAll(r)
if err != nil {
return result, err
}
err = json.Unmarshal(jsonBlob, &result)
if err != nil {
return result, err
}
return result, nil
}
func DataReader(ln net.Listener, dataCh chan<- []byte) {
logger.Println("Starting listening on socket")
for {
// accept a connection
c, err := ln.Accept()
if err != nil {
CountMetric("error")
logger.Printf("ERROR (DataReader): %s", err)
continue
}
var buf bytes.Buffer
io.Copy(&buf, c)
dataCh <- buf.Bytes()
c.Close()
}
logger.Println("Ending listening on socket")
}
func DataParser(dataCh <-chan []byte, metricCh chan<- Metric) {
for {
var metrics []Metric
data := <-dataCh
err := json.Unmarshal(data, &metrics)
if err != nil {
CountMetric("error")
logger.Printf("ERROR (DataParser): %s", err)
continue
}
for i := 0; i < len(metrics); i++ {
metricCh <- metrics[i]
}
}
}
func DataProcessor(registry Registry, metricCh <-chan Metric, doneCh <-chan bool) {
logger.Println("Starting processing data")
for {
select {
case metric := <-metricCh:
err := registry.Handle(&metric)
if err != nil {
CountMetric("error")
logger.Printf("ERROR (DataProcessor): %s %+v", err, metric)
continue
}
CountMetric("ok")
case <-doneCh:
return
}
}
}