-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
76 lines (65 loc) · 1.96 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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/containous/flaeg"
"github.com/lawrencegripper/traefik-appinsights-watchdog/health"
"github.com/lawrencegripper/traefik-appinsights-watchdog/routing"
"github.com/lawrencegripper/traefik-appinsights-watchdog/types"
)
func main() {
hostName, err := os.Hostname()
if err != nil {
fmt.Println("Unable to automatically set instanceid to hostname")
}
config := &types.Configuration{
Debug: false,
PollIntervalSec: 120,
InstanceID: hostName,
WatchdogTestServerPort: 40001,
TraefikHealthEndpoint: "http://localhost:8080/health",
TraefikBackendName: "fabric:/TraefikType/Watchdog",
WatchdogTraefikURL: "http://localhost:80/TraefikType/Watchdog",
}
rootCmd := &flaeg.Command{
Name: "start",
Description: `Starts the watchdog, checking both the /health endpoint and request routing`,
Config: config,
DefaultPointersConfig: config,
Run: func() error {
if config.AppInsightsKey == "" {
fmt.Println("Application insights key is required use '--appinsightskey=key' use '-h' to see help")
os.Exit(1)
}
fmt.Printf("Running watchdog with config :\n %+v\n", prettyPrintStruct(config))
startWatchdog(*config)
return nil
},
}
//init flaeg
flaeg := flaeg.New(rootCmd, os.Args[1:])
//run test
if err := flaeg.Run(); err != nil {
fmt.Printf("Error %s \n", err.Error())
}
}
func prettyPrintStruct(item interface{}) string {
b, _ := json.MarshalIndent(item, "", " ")
return string(b)
}
func startWatchdog(config types.Configuration) {
healthChan := make(chan types.StatsEvent)
client := NewTelemetryClient(config)
ctx := context.Background()
go routing.StartCheck(ctx, config, healthChan)
go health.StartCheck(ctx, config, healthChan)
for {
event := <-healthChan
PublishToAppInsights(client, event, config)
if config.Debug {
fmt.Println(prettyPrintStruct(event))
}
}
}