Browse Source

Merge branch 'metrics-clean' of dan/cwtch into master

pull/75/head
Sarah Jamie Lewis 4 years ago
committed by Gogs
parent
commit
bdd8f9a5fd
  1. 14
      server/app/main.go
  2. 7
      server/app/serverConfig.json.sample
  3. 21
      server/metrics/monitors.go
  4. 2
      server/server.go
  5. 38
      server/serverConfig.go

14
server/app/main.go

@ -2,28 +2,14 @@ package main
import (
cwtchserver "cwtch.im/cwtch/server"
"io/ioutil"
"log"
"os"
)
const (
serverConfigFile = "serverConfig.json"
)
func confirmOrCopySampleConfig() {
// if no config file, attempt to copy sample
if _, err := os.Stat(serverConfigFile); os.IsNotExist(err) {
raw, err := ioutil.ReadFile(serverConfigFile + ".sample")
if err != nil {
log.Fatal("Could not read sample config to copy: ", err)
}
ioutil.WriteFile(serverConfigFile, raw, 0600)
}
}
func main() {
confirmOrCopySampleConfig()
serverConfig := cwtchserver.LoadConfig(serverConfigFile)
server := new(cwtchserver.Server)

7
server/app/serverConfig.json.sample

@ -1,7 +0,0 @@
{
"maxBufferLines": 100000,
"serverReporting": {
"reportingGroupId": "",
"reportingServerAddr": ""
}
}

21
server/metrics/monitors.go

@ -23,10 +23,12 @@ type Monitors struct {
ClientConns MonitorHistory
starttime time.Time
breakChannel chan bool
log bool
}
// Start initializes a Monitors's monitors
func (mp *Monitors) Start(ra *application.RicochetApplication) {
func (mp *Monitors) Start(ra *application.RicochetApplication, log bool) {
mp.log = log
mp.starttime = time.Now()
mp.breakChannel = make(chan bool)
mp.MessageCounter = NewCounter()
@ -35,19 +37,15 @@ func (mp *Monitors) Start(ra *application.RicochetApplication) {
mp.Memory = NewMonitorHistory(MegaBytes, func() float64 { sysInfo, _ := pidusage.GetStat(os.Getpid()); return float64(sysInfo.Memory) })
mp.ClientConns = NewMonitorHistory(Count, func() float64 { return float64(ra.ConnectionCount()) })
// Todo: replace with proper reporting
go mp.log()
if mp.log {
go mp.run()
}
}
func (mp *Monitors) log() {
func (mp *Monitors) run() {
for {
select {
case <-time.After(time.Minute):
messageMinutes := mp.Messages.Minutes()
cpuMinutes := mp.CPU.Minutes()
memoryMinutes := mp.Memory.Minutes()
listenConnsMinutes := mp.ClientConns.Minutes()
log.Printf("METRICS: Messages: %.0f ClientConns: %.0f CPU: %.2f Memory: %.0fMBs\n", messageMinutes[0], listenConnsMinutes[0], cpuMinutes[0], memoryMinutes[0]/1024/1024)
mp.report()
case <-mp.breakChannel:
return
@ -80,12 +78,13 @@ func (mp *Monitors) report() {
mp.Memory.Report(w)
w.Flush()
}
// Stop stops all the monitors in a Monitors
func (mp *Monitors) Stop() {
mp.breakChannel <- true
if mp.log {
mp.breakChannel <- true
}
mp.Messages.Stop()
mp.CPU.Stop()
mp.Memory.Stop()

2
server/server.go

@ -23,7 +23,7 @@ type Server struct {
func (s *Server) Run(serverConfig *Config) {
s.config = serverConfig
cwtchserver := new(application.RicochetApplication)
s.metricsPack.Start(cwtchserver)
s.metricsPack.Start(cwtchserver, s.config.ServerReporting.LogMetricsToFile)
l, err := application.SetupOnion("127.0.0.1:9051", "tcp4", "", s.config.PrivateKey(), 9878)

38
server/serverConfig.go

@ -11,6 +11,7 @@ import (
// Reporting is a struct for storing a the config a server needs to be a peer, and connect to a group to report
type Reporting struct {
LogMetricsToFile bool `json:"logMetricsToFile"`
PeerPrivateKey string `json:"privateKey"`
ReportingGroupID string `json:"reportingGroupId"`
ReportingServerAddr string `json:"reportingServerAddr"`
@ -41,30 +42,43 @@ func (config *Config) Save(filename string) {
ioutil.WriteFile(filename, bytes, 0600)
}
// newConfig generates a simple config with defaults. Unmarshal will return them if they aren't specified
func newConfig() *Config {
config := Config{}
config.MaxBufferLines = 100000
config.ServerReporting.LogMetricsToFile = false
return &config
}
// LoadConfig loads a Config from a json file specified by filename
func LoadConfig(filename string) *Config {
raw, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal("Could not read config: ", err)
}
config := newConfig()
config := Config{}
err = json.Unmarshal(raw, &config)
raw, err := ioutil.ReadFile(filename)
if err == nil {
err = json.Unmarshal(raw, &config)
if err != nil {
log.Fatal("Error reading config: ", err)
if err != nil {
log.Println("Error reading config: ", err)
}
}
configAutoPopulate(config)
// Always save (first time generation, new version with new variables populated)
config.Save(filename)
return config
}
// Auto populate required values if missing and save
func configAutoPopulate(config *Config) {
if config.PrivateKeyBytes == "" {
config.generatePrivateKey()
config.Save(filename)
}
if config.ServerReporting.PeerPrivateKey == "" {
config.generatePeerPrivateKey()
config.Save(filename)
}
return &config
}
func (config *Config) generatePrivateKey() {

Loading…
Cancel
Save