Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gorm metric stats #2

Merged
merged 1 commit into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/davecgh/go-spew v1.1.1
github.com/gotomicro/ego v1.0.2
github.com/gotomicro/ego v1.0.3-0.20220422043740-dd25289ced38
github.com/json-iterator/go v1.1.12
github.com/spf13/cast v1.3.1
github.com/stretchr/testify v1.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ github.com/gotomicro/ego v1.0.1 h1:TId194gdnP43RNwh4xBU/MC8Tc6R8nJ5v3BZtf6movE=
github.com/gotomicro/ego v1.0.1/go.mod h1:Pjt+5kIVGMqzpKYMxeN4s/Dc2TrNO9R58yQwt9zoXck=
github.com/gotomicro/ego v1.0.2 h1:ScGEaP1MVT4w6kPa8AB8sqRdDiCWN4p1g7AwBCzrU1s=
github.com/gotomicro/ego v1.0.2/go.mod h1:Pjt+5kIVGMqzpKYMxeN4s/Dc2TrNO9R58yQwt9zoXck=
github.com/gotomicro/ego v1.0.3-0.20220422043740-dd25289ced38 h1:e0PVqRna/NjkjCJF2+Dj75HJ32wshSG2i6paJpeF+1k=
github.com/gotomicro/ego v1.0.3-0.20220422043740-dd25289ced38/go.mod h1:Pjt+5kIVGMqzpKYMxeN4s/Dc2TrNO9R58yQwt9zoXck=
github.com/gotomicro/logrotate v0.0.0-20211108024517-45d1f9a03ff5 h1:y9nw0S0zlla/SBt1GGaTNNCF+781epJ62MntVVbekqQ=
github.com/gotomicro/logrotate v0.0.0-20211108024517-45d1f9a03ff5/go.mod h1:jKlh8i9m79fE8HAO28kYLN70l87bb7olTLuX/Blex/U=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
Expand Down
48 changes: 0 additions & 48 deletions init.go

This file was deleted.

45 changes: 0 additions & 45 deletions instance.go

This file was deleted.

65 changes: 65 additions & 0 deletions stat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package egorm

import (
"net/http"
"sync"

"github.com/gotomicro/ego/core/elog"
"github.com/gotomicro/ego/core/emetric"
"github.com/gotomicro/ego/server/egovernor"
jsoniter "github.com/json-iterator/go"
)

var instances = sync.Map{}

func init() {
egovernor.HandleFunc("/debug/gorm/stats", func(w http.ResponseWriter, r *http.Request) {
_ = jsoniter.NewEncoder(w).Encode(stats())
})
go monitor()
}

func monitor() {
for {
instances.Range(func(key, val interface{}) bool {
name := key.(string)
db := val.(*Component)

sqlDB, err := db.DB()
if err != nil {
elog.EgoLogger.With(elog.FieldComponent(PackageName)).Panic("monitor db error", elog.FieldErr(err))
return false
}
stats := sqlDB.Stats()
emetric.ClientStatsGauge.Set(float64(stats.MaxOpenConnections), emetric.TypeGorm, name, "max_open_connections")
emetric.ClientStatsGauge.Set(float64(stats.OpenConnections), emetric.TypeGorm, name, "open_connections")
emetric.ClientStatsGauge.Set(float64(stats.InUse), emetric.TypeGorm, name, "in_use")
emetric.ClientStatsGauge.Set(float64(stats.Idle), emetric.TypeGorm, name, "idle")
emetric.ClientStatsGauge.Set(float64(stats.WaitCount), emetric.TypeGorm, name, "wait_count")
emetric.ClientStatsGauge.Set(float64(stats.WaitDuration), emetric.TypeGorm, name, "wait_duration")
emetric.ClientStatsGauge.Set(float64(stats.MaxIdleClosed), emetric.TypeGorm, name, "max_idle_closed")
emetric.ClientStatsGauge.Set(float64(stats.MaxIdleTimeClosed), emetric.TypeGorm, name, "max_idle_time_closed")
emetric.ClientStatsGauge.Set(float64(stats.MaxLifetimeClosed), emetric.TypeGorm, name, "max_lifetime_closed")
return true
})
}
}

// stats
func stats() (stats map[string]interface{}) {
stats = make(map[string]interface{})
instances.Range(func(key, val interface{}) bool {
name := key.(string)
db := val.(*Component)

sqlDB, err := db.DB()
if err != nil {
elog.EgoLogger.With(elog.FieldComponent(PackageName)).Panic("stats db error", elog.FieldErr(err))
return false
}
stats[name] = sqlDB.Stats()
return true
})

return
}