improve logging, add redis check
This commit is contained in:
1 parent
94c9f20dfd
commit
ab6a8b406e
4 files changed
+53
-11
No files matched your search
@@ -1 +1,2 @@
|
||||
bin/
|
||||
config.yml
|
||||
@@ -28,3 +28,16 @@ Usage of latency-exporter:
|
||||
-p string
|
||||
port to run on (default "9062")
|
||||
```
|
||||
|
||||
## Configuration Example
|
||||
|
||||
```yml
|
||||
services:
|
||||
- name: pytest
|
||||
host: 127.0.0.1
|
||||
port: 8000
|
||||
- name: redis-test
|
||||
host: 127.0.0.1
|
||||
port: 6379
|
||||
type: redis
|
||||
```
|
||||
@@ -1,4 +0,0 @@
|
||||
services:
|
||||
- name: pytest
|
||||
host: 127.0.0.1
|
||||
port: 8000
|
||||
@@ -1,22 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"time"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"fmt"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
Name string `yaml:"name"`
|
||||
Host string `yaml:"host"`
|
||||
Port string `yaml:"port"`
|
||||
Type string `yaml:"type"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
@@ -35,6 +39,7 @@ var (
|
||||
port = flag.String("p", "9062", "port to run on")
|
||||
conf = flag.String("c", "config.yml", "configuration file to use")
|
||||
inter = flag.Int("i", 30, "time interval in seconds between two checks")
|
||||
debug = flag.Bool("d", false, "enable debug logs")
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -54,7 +59,7 @@ func main() {
|
||||
time.Sleep(time.Duration(*inter) * time.Second)
|
||||
}
|
||||
}()
|
||||
log.Println("Starting server on address:", *addr, "port:", *port)
|
||||
log.Println("INFO: Starting server on address:", *addr, "port:", *port)
|
||||
err := http.ListenAndServe(net.JoinHostPort(*addr, *port), nil)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to start server, error: ", err)
|
||||
@@ -67,21 +72,48 @@ func checkLatency() {
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read configuration file, error: %v", err)
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(source, &config)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to unmarshal configuration, error: %v", err)
|
||||
}
|
||||
|
||||
for _, service := range config.Services {
|
||||
if service.Type == "" {
|
||||
service.Type = "tcp"
|
||||
}
|
||||
if *debug == true {
|
||||
log.Printf("DEBUG: Dialing %s: host: %s, port: %s, type: %s",
|
||||
service.Name, service.Host,service.Port,service.Type)
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
conn, err := net.Dial("tcp", net.JoinHostPort(service.Host, service.Port))
|
||||
if err != nil {
|
||||
log.Printf("Failed to check service: %s, error: %s", service.Name, err)
|
||||
log.Printf("ERROR: Failed to check service: %s, error: %s", service.Name, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Performs a Redis check if needed
|
||||
if strings.ToLower(service.Type) == "redis" {
|
||||
_, err := conn.Write([]byte("PING\r\n"))
|
||||
if err != nil {
|
||||
log.Printf("ERROR: Failed to check service: %s, error: %s", service.Name, err)
|
||||
continue
|
||||
}
|
||||
reply, _ := bufio.NewReader(conn).ReadString('\n')
|
||||
if reply != "+PONG\r\n" {
|
||||
log.Printf("ERROR: Bad answer from %s (type redis): %s", service.Name, reply)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
conn.Close()
|
||||
|
||||
duration := time.Since(start)
|
||||
latency.With(prometheus.Labels{"service": service.Name}).Set(duration.Seconds())
|
||||
log.Printf("Service: %s, Latency: %v seconds", service.Name, duration.Seconds())
|
||||
if *debug == true {
|
||||
log.Printf("DEBUG: Latency %s: %v seconds", service.Name, duration.Seconds())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user