Skip to content

Commit 6f7908e

Browse files
committed
send reject messages to logshuttle
1 parent ea164cb commit 6f7908e

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*env*
2+
*.git*

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ Metrics Syslog Collector has two major requirements - a Postgres database and an
2525

2626
## Environment Variables
2727

28-
| Name | Description | Required |
29-
| --------------------- | ----------------------------------------------------------------- | --------- |
30-
| DATABASE_URL | URL connection string for a Postgres database | Required |
31-
| DEBUG | Set to "true" to print out additional degugging info | Optional |
32-
| OPENTSDB_IP | IP address of the OpenTSDB compatible server to send metrics to | Required |
33-
| PORT | Network port to listen on | Required |
34-
| UNIQUE_METRIC_LIMIT | How many metrics should be allowed per app - default is 100 | Optional |
28+
| Name | Description | Required |
29+
| --------------------- | ------------------------------------------------------------------------- | --------- |
30+
| DATABASE_URL | URL connection string for a Postgres database | Required |
31+
| DEBUG | Set to "true" to print out additional degugging info | Optional |
32+
| OPENTSDB_IP | IP address of the OpenTSDB compatible server to send metrics to | Required |
33+
| PORT | Network port to listen on | Required |
34+
| UNIQUE_METRIC_LIMIT | How many metrics should be allowed per app - default is 100 | Optional |
35+
| LOGSHUTTLE_URL | URL of logshuttle (for reject messages) (defaults to in-cluster hostname) | Optional |
3536

3637
## Running
3738

main.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package main
22

33
import (
4+
"bytes"
45
"database/sql"
6+
"encoding/json"
57
"fmt"
68
"io/ioutil"
79
"log"
810
"net"
11+
"net/http"
912
"os"
1013
"regexp"
1114
"strconv"
@@ -132,6 +135,58 @@ func sendMetric(v []string, t map[string]string, message string, tags [][]string
132135
fmt.Fprintf(conn, put)
133136
}
134137

138+
func rejectMetric(app string, metric string, metricType string) {
139+
appParts := strings.SplitN(app, "-", 2)
140+
appName := appParts[0]
141+
appSpace := appParts[1]
142+
143+
rejectMessage := struct {
144+
Log string `json:"log"`
145+
Stream string `json:"stream"`
146+
Time time.Time `json:"time"`
147+
Kubernetes interface{} `json:"kubernetes"`
148+
Topic string `json:"topic"`
149+
}{
150+
"Unique metrics limit exceeded. Metric discarded: [" + metricType + "] " + metric,
151+
"stdout",
152+
time.Now(),
153+
struct {
154+
PodName string `json:"pod_name"`
155+
ContainerName string `json:"container_name"`
156+
}{
157+
"akkeris-warning",
158+
appName,
159+
},
160+
appSpace,
161+
}
162+
163+
var logshuttleURL string
164+
if os.Getenv("LOGSHUTTLE_URL") != "" {
165+
logshuttleURL = os.Getenv("LOGSHUTTLE_URL")
166+
} else {
167+
logshuttleURL = "http://logshuttle.akkeris-system.svc.cluster.local"
168+
}
169+
170+
jsonb, err := json.Marshal(rejectMessage)
171+
if err != nil {
172+
fmt.Println("Unable to send reject message to logshuttle")
173+
fmt.Println(err)
174+
return
175+
}
176+
177+
resp, err := http.Post(logshuttleURL+"/log-events", "application/json", bytes.NewBuffer(jsonb))
178+
if err != nil {
179+
fmt.Println("Unable to send reject message to logshuttle")
180+
fmt.Println(err)
181+
return
182+
}
183+
defer resp.Body.Close()
184+
185+
if resp.StatusCode < 200 || resp.StatusCode > 299 {
186+
fmt.Println("Error: " + strconv.Itoa(resp.StatusCode) + " response returned from logshuttle")
187+
}
188+
}
189+
135190
func main() {
136191
var err error
137192
conn, err = net.Dial("tcp", os.Getenv("OPENTSDB_IP"))
@@ -186,6 +241,8 @@ func main() {
186241
ok := checkMetric(t["app"], t["metric"])
187242
if ok {
188243
sendMetric(v, t, message, tags)
244+
} else {
245+
rejectMetric(t["app"], t["metric"], v[1])
189246
}
190247
}
191248
}

0 commit comments

Comments
 (0)