-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
137 lines (113 loc) · 3.49 KB
/
main.go
File metadata and controls
137 lines (113 loc) · 3.49 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
package main
import (
"flag"
"os"
"os/signal"
"runtime"
"time"
"net/http"
"net/http/pprof"
log "github.com/ReviveNetwork/GoRevive/Log"
"github.com/ReviveNetwork/GoRevive/core"
)
var (
// BuildTime of the build provided by the build command
BuildTime = "Not provided"
// GitHash of build provided by the build command
GitHash = "Not provided"
// GitBranch of the build provided by the build command
GitBranch = "Not provided"
// compileVersion we are receiving by the build command
CompileVersion = "0"
// Version of the Application
Version = "0.0.2"
// MyConfig Default configuration
MyConfig = Config{
MysqlServer: "localhost:3306",
MysqlUser: "loginserver",
MysqlDb: "loginserver",
MysqlPw: "",
}
mem runtime.MemStats
)
func collectGlobalMetrics(iDB *core.InfluxDB) {
runtime.ReadMemStats(&mem)
tags := map[string]string{"metric": "server_metrics", "server": "GoLoginServer", "version": Version}
fields := map[string]interface{}{
"memAlloc": int(mem.Alloc),
"memTotalAlloc": int(mem.TotalAlloc),
"memHeapAlloc": int(mem.HeapAlloc),
"memHeapSys": int(mem.HeapSys),
}
err := iDB.AddMetric("server_metrics", tags, fields)
if err != nil {
log.Errorln("Error adding Metric:", err)
}
}
func reconnectInflux(iDB *core.InfluxDB) {
err := iDB.Reconnect()
if err != nil {
log.Errorln("Error reconnecting:", err)
}
}
func main() {
var (
configPath = flag.String("config", "config.yml", "Path to yml configuration file")
logLevel = flag.String("logLevel", "error", "LogLevel [error|warning|note|debug]")
)
flag.Parse()
if CompileVersion != "0" {
Version = Version + "." + CompileVersion
}
log.SetLevel(*logLevel)
log.Notef("Starting up v%s - %s %s %s", Version, BuildTime, GitBranch, GitHash)
MyConfig.Load(*configPath)
r := http.NewServeMux()
// Register pprof handlers
r.HandleFunc("/debug/pprof/", pprof.Index)
r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
r.HandleFunc("/debug/pprof/profile", pprof.Profile)
r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
r.HandleFunc("/debug/pprof/trace", pprof.Trace)
go func() {
log.Noteln(http.ListenAndServe("0.0.0.0:6060", r))
}()
// Startup done
metricConnection := new(core.InfluxDB)
err := metricConnection.New(MyConfig.InfluxDBHost, MyConfig.InfluxDBDatabase, MyConfig.InfluxDBUser, MyConfig.InfluxDBPassword)
if err != nil {
log.Fatalln("Error connecting to MetricsDB:", err)
}
dbConnection := new(core.DB)
dbSQL, err := dbConnection.New(MyConfig.MysqlServer, MyConfig.MysqlDb, MyConfig.MysqlUser, MyConfig.MysqlPw)
if err != nil {
log.Fatalln("Error connecting to DB:", err)
}
loggingDBConnection := new(core.DB)
loggingDBSQL, err := loggingDBConnection.New(MyConfig.MysqlLoggingServer, MyConfig.MysqlLoggingDb, MyConfig.MysqlLoggingUser, MyConfig.MysqlLoggingPw)
if err != nil {
log.Fatalln("Error connecting to logging DB:", err)
}
globalMetrics := time.NewTicker(time.Second * 10)
go func() {
for range globalMetrics.C {
collectGlobalMetrics(metricConnection)
}
}()
influxReconnecter := time.NewTicker(time.Minute * 5)
go func() {
for range influxReconnecter.C {
reconnectInflux(metricConnection)
}
}()
searchProvider := new(SearchProvider)
searchProvider.New("SP", dbSQL, metricConnection)
clientManager := new(ClientManager)
clientManager.New("CM", dbSQL, loggingDBSQL, metricConnection)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
for sig := range c {
log.Noteln("Captured" + sig.String() + ". Shutting down.")
os.Exit(0)
}
}