-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
170 lines (137 loc) · 4.33 KB
/
main.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
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
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"net/http"
"sort"
"strings"
"time"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/textparse"
)
func enrichMetrics(input []byte, clusterID string) ([]byte, error) {
symbolTable := labels.NewSymbolTable()
parser := textparse.NewPromParser(input, symbolTable)
var builder bytes.Buffer
metricsCount := 0
for {
entry, err := parser.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("parsing error: %v", err)
}
switch entry {
case textparse.EntryType:
name, typ := parser.Type()
fmt.Fprintf(&builder, "# TYPE %s %s\n", name, typ)
case textparse.EntryHelp:
name, help := parser.Help()
fmt.Fprintf(&builder, "# HELP %s %s\n", name, help)
case textparse.EntrySeries:
_, _, value := parser.Series()
var lbls labels.Labels
parser.Metric(&lbls)
// Add cluster_id to the labels
lbls = append(lbls,
labels.Label{
Name: "cluster_id",
Value: clusterID,
},
)
sort.Sort(lbls)
metricName := ""
for _, lbl := range lbls {
if lbl.Name == "__name__" {
metricName = lbl.Value
break
}
}
var labelStrings []string
for _, lbl := range lbls {
if lbl.Name != "__name__" {
labelStrings = append(labelStrings, fmt.Sprintf("%s=\"%s\"", lbl.Name, lbl.Value))
}
}
labelsStr := ""
if len(labelStrings) > 0 {
labelsStr = fmt.Sprintf("{%s}", strings.Join(labelStrings, ","))
}
fmt.Fprintf(&builder, "%s%s %g\n", metricName, labelsStr, value)
metricsCount++
}
}
log.Printf("Processed %d metrics for cluster %s", metricsCount, clusterID)
return builder.Bytes(), nil
}
func forwardToPrometheus(metrics []byte, forwardURL string) error {
startTime := time.Now()
resp, err := http.Post(forwardURL, "text/plain", bytes.NewBuffer(metrics))
if err != nil {
return fmt.Errorf("error forwarding to Prometheus: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("unexpected status code %d: %s", resp.StatusCode, string(body))
}
log.Printf("Successfully forwarded metrics to Prometheus in %v", time.Since(startTime))
return nil
}
func handleTelemetry(w http.ResponseWriter, r *http.Request, forwardURL string) {
startTime := time.Now()
if r.Method != http.MethodPost {
log.Printf("Method not allowed: %s", r.Method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
clusterID := r.Header.Get("X-Cluster-ID")
if clusterID == "" {
log.Printf("Request rejected: missing X-Cluster-ID header")
http.Error(w, "X-Cluster-ID header is required", http.StatusBadRequest)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("Error reading request body: %v", err)
http.Error(w, fmt.Sprintf("Error reading request: %v", err), http.StatusBadRequest)
return
}
log.Printf("Received metrics for cluster %s", clusterID)
enrichedMetrics, err := enrichMetrics(body, clusterID)
if err != nil {
log.Printf("Error processing metrics for cluster %s: %v", clusterID, err)
http.Error(w, fmt.Sprintf("Error processing metrics: %v", err), http.StatusBadRequest)
return
}
if err := forwardToPrometheus(enrichedMetrics, forwardURL); err != nil {
log.Printf("Error forwarding metrics for cluster %s: %v", clusterID, err)
http.Error(w, fmt.Sprintf("Error forwarding metrics: %v", err), http.StatusInternalServerError)
return
}
log.Printf("Request from cluster %s processed in %v", clusterID, time.Since(startTime))
w.WriteHeader(http.StatusOK)
}
func main() {
// Define flags
forwardURL := flag.String("forward-url", "http://vminsert-cozy-telemetry:8480/insert/0/prometheus/api/v1/import/prometheus", "URL to forward the metrics to")
listenAddr := flag.String("listen-addr", ":8081", "Address to listen on for incoming metrics")
flag.Parse()
server := &http.Server{
Addr: *listenAddr,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
handleTelemetry(w, r, *forwardURL)
})
log.Printf("Starting server on %s", *listenAddr)
log.Printf("Forwarding metrics to %s", *forwardURL)
if err := server.ListenAndServe(); err != nil {
log.Fatalf("Server error: %v", err)
}
}