mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-01 03:03:10 -06:00
Move WASM to its own file, add config and comments
This commit is contained in:
parent
8e5f913e0b
commit
a60c307be2
|
|
@ -30,7 +30,7 @@ func SyncAPI(base *basepkg.BaseDendrite, cfg *config.Dendrite) {
|
||||||
base.ProcessContext,
|
base.ProcessContext,
|
||||||
base.PublicClientAPIMux, userAPI, rsAPI,
|
base.PublicClientAPIMux, userAPI, rsAPI,
|
||||||
base.KeyServerHTTPClient(),
|
base.KeyServerHTTPClient(),
|
||||||
federation, &cfg.SyncAPI, cfg,
|
federation, cfg,
|
||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ func (m *Monolith) AddAllPublicRoutes(process *process.ProcessContext, csMux, ss
|
||||||
mediaapi.AddPublicRoutes(mediaMux, &m.Config.MediaAPI, &m.Config.ClientAPI.RateLimiting, m.UserAPI, m.Client)
|
mediaapi.AddPublicRoutes(mediaMux, &m.Config.MediaAPI, &m.Config.ClientAPI.RateLimiting, m.UserAPI, m.Client)
|
||||||
syncapi.AddPublicRoutes(
|
syncapi.AddPublicRoutes(
|
||||||
process, csMux, m.UserAPI, m.RoomserverAPI,
|
process, csMux, m.UserAPI, m.RoomserverAPI,
|
||||||
m.KeyAPI, m.FedClient, &m.Config.SyncAPI, m.Config,
|
m.KeyAPI, m.FedClient, m.Config,
|
||||||
true,
|
true,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
48
syncapi/stats.go
Normal file
48
syncapi/stats.go
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
//go:build !wasm
|
||||||
|
// +build !wasm
|
||||||
|
|
||||||
|
package syncapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getMemoryStats(p *phoneHomeStats) error {
|
||||||
|
oldUsage := p.prevData
|
||||||
|
newUsage := syscall.Rusage{}
|
||||||
|
if err := syscall.Getrusage(syscall.RUSAGE_SELF, &newUsage); err != nil {
|
||||||
|
logrus.WithError(err).Error("unable to get usage")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
newData := timestampToRUUsage{timestamp: time.Now().Unix(), usage: newUsage}
|
||||||
|
p.prevData = newData
|
||||||
|
|
||||||
|
usedCPUTime := (newUsage.Utime.Sec + newUsage.Stime.Sec) - (oldUsage.usage.Utime.Sec + oldUsage.usage.Stime.Sec)
|
||||||
|
|
||||||
|
if usedCPUTime == 0 || newData.timestamp == oldUsage.timestamp {
|
||||||
|
logrus.Info("setting cpu average to 0")
|
||||||
|
p.stats["cpu_average"] = 0
|
||||||
|
} else {
|
||||||
|
p.stats["cpu_average"] = math.Floor(float64(usedCPUTime / (newData.timestamp - oldUsage.timestamp) * 100))
|
||||||
|
}
|
||||||
|
p.stats["memory_rss"] = newUsage.Maxrss
|
||||||
|
return nil
|
||||||
|
}
|
||||||
20
syncapi/stats_wasm.go
Normal file
20
syncapi/stats_wasm.go
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package syncapi
|
||||||
|
|
||||||
|
// stub, since WASM doesn't support syscall.Getrusage
|
||||||
|
func getMemoryStats(p *phoneHomeStats) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -54,10 +54,10 @@ func AddPublicRoutes(
|
||||||
rsAPI api.RoomserverInternalAPI,
|
rsAPI api.RoomserverInternalAPI,
|
||||||
keyAPI keyapi.KeyInternalAPI,
|
keyAPI keyapi.KeyInternalAPI,
|
||||||
federation *gomatrixserverlib.FederationClient,
|
federation *gomatrixserverlib.FederationClient,
|
||||||
cfg *config.SyncAPI,
|
|
||||||
baseCfg *config.Dendrite,
|
baseCfg *config.Dendrite,
|
||||||
isMonolith bool,
|
isMonolith bool,
|
||||||
) {
|
) {
|
||||||
|
cfg := &baseCfg.SyncAPI
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
js := jetstream.Prepare(&cfg.Matrix.JetStream)
|
js := jetstream.Prepare(&cfg.Matrix.JetStream)
|
||||||
|
|
||||||
|
|
@ -120,8 +120,9 @@ func AddPublicRoutes(
|
||||||
logrus.WithError(err).Panicf("failed to start receipts consumer")
|
logrus.WithError(err).Panicf("failed to start receipts consumer")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: add config
|
if baseCfg.Global.ReportStats {
|
||||||
go startPhoneHomeCollector(startTime, baseCfg, syncDB, userAPI, isMonolith)
|
go startPhoneHomeCollector(startTime, baseCfg, syncDB, userAPI, isMonolith)
|
||||||
|
}
|
||||||
|
|
||||||
routing.Setup(router, requestPool, syncDB, userAPI, federation, rsAPI, cfg)
|
routing.Setup(router, requestPool, syncDB, userAPI, federation, rsAPI, cfg)
|
||||||
}
|
}
|
||||||
|
|
@ -145,7 +146,6 @@ type timestampToRUUsage struct {
|
||||||
func startPhoneHomeCollector(startTime time.Time, cfg *config.Dendrite, syncDB storage.Database, userAPI userapi.UserInternalAPI, isMonolith bool) {
|
func startPhoneHomeCollector(startTime time.Time, cfg *config.Dendrite, syncDB storage.Database, userAPI userapi.UserInternalAPI, isMonolith bool) {
|
||||||
|
|
||||||
p := phoneHomeStats{
|
p := phoneHomeStats{
|
||||||
stats: make(map[string]interface{}),
|
|
||||||
startTime: startTime,
|
startTime: startTime,
|
||||||
serverName: cfg.Global.ServerName,
|
serverName: cfg.Global.ServerName,
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
|
|
@ -172,41 +172,39 @@ func startPhoneHomeCollector(startTime time.Time, cfg *config.Dendrite, syncDB s
|
||||||
|
|
||||||
func (p *phoneHomeStats) collect() {
|
func (p *phoneHomeStats) collect() {
|
||||||
p.stats = make(map[string]interface{})
|
p.stats = make(map[string]interface{})
|
||||||
|
// general information
|
||||||
p.stats["servername"] = p.serverName
|
p.stats["servername"] = p.serverName
|
||||||
p.stats["monolith"] = p.isMonolith
|
p.stats["monolith"] = p.isMonolith
|
||||||
p.stats["version"] = internal.VersionString()
|
p.stats["version"] = internal.VersionString()
|
||||||
|
p.stats["timestamp"] = time.Now().Unix()
|
||||||
|
p.stats["go_version"] = runtime.Version()
|
||||||
|
p.stats["uptime_seconds"] = math.Floor(time.Now().Sub(p.startTime).Seconds())
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute*1)
|
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute*1)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
oldUsage := p.prevData
|
// cpu and memory usage information
|
||||||
newUsage := syscall.Rusage{}
|
err := getMemoryStats(p)
|
||||||
if err := syscall.Getrusage(syscall.RUSAGE_SELF, &newUsage); err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Error("unable to get usage")
|
logrus.WithError(err).Error("unable to get memory/cpu stats")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
newData := timestampToRUUsage{timestamp: time.Now().Unix(), usage: newUsage}
|
|
||||||
p.prevData = newData
|
|
||||||
|
|
||||||
usedCPUTime := (newUsage.Utime.Sec + newUsage.Stime.Sec) - (oldUsage.usage.Utime.Sec + oldUsage.usage.Stime.Sec)
|
// configuration information
|
||||||
|
p.stats["federation_disabled"] = p.cfg.Global.DisableFederation
|
||||||
if usedCPUTime == 0 || newData.timestamp == oldUsage.timestamp {
|
p.stats["nats_embedded"] = true
|
||||||
logrus.Info("setting cpu average to 0")
|
p.stats["nats_in_memory"] = p.cfg.Global.JetStream.InMemory
|
||||||
p.stats["cpu_average"] = 0
|
if len(p.cfg.Global.JetStream.Addresses) > 0 {
|
||||||
} else {
|
p.stats["nats_embedded"] = false
|
||||||
p.stats["cpu_average"] = math.Floor(float64(usedCPUTime / (newData.timestamp - oldUsage.timestamp) * 100))
|
p.stats["nats_in_memory"] = false // probably
|
||||||
}
|
}
|
||||||
|
|
||||||
p.stats["uptime_seconds"] = math.Floor(time.Now().Sub(p.startTime).Seconds())
|
|
||||||
p.stats["timestamp"] = time.Now().Unix()
|
|
||||||
p.stats["go_version"] = runtime.Version()
|
|
||||||
p.stats["memory_rss"] = newUsage.Maxrss
|
|
||||||
if len(p.cfg.Logging) > 0 {
|
if len(p.cfg.Logging) > 0 {
|
||||||
p.stats["log_level"] = p.cfg.Logging[0].Level
|
p.stats["log_level"] = p.cfg.Logging[0].Level
|
||||||
} else {
|
} else {
|
||||||
p.stats["log_level"] = "info"
|
p.stats["log_level"] = "info"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// database configuration
|
||||||
db, err := sqlutil.Open(&p.cfg.SyncAPI.Database)
|
db, err := sqlutil.Open(&p.cfg.SyncAPI.Database)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Error("unable to database")
|
logrus.WithError(err).Error("unable to database")
|
||||||
|
|
@ -235,6 +233,7 @@ func (p *phoneHomeStats) collect() {
|
||||||
p.stats["database_engine"] = dbEngine
|
p.stats["database_engine"] = dbEngine
|
||||||
p.stats["database_server_version"] = dbVersion
|
p.stats["database_server_version"] = dbVersion
|
||||||
|
|
||||||
|
// message and room stats
|
||||||
messages, err := p.db.DailyMessages(ctx, 0)
|
messages, err := p.db.DailyMessages(ctx, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Error("unable to query DailyMessages")
|
logrus.WithError(err).Error("unable to query DailyMessages")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue