Add basic resource information in about response

This commit is contained in:
Ingo Oppermann
2024-08-20 11:55:08 +02:00
parent 0b1601542d
commit 68607ed932
6 changed files with 58 additions and 24 deletions

View File

@@ -1500,6 +1500,7 @@ func (a *api) start(ctx context.Context) error {
return false
},
Resources: a.resources,
}
mainserverhandler, err := http.NewServer(serverConfig)

View File

@@ -8,11 +8,12 @@ type About struct {
ID string `json:"id"`
CreatedAt string `json:"created_at"` // RFC3339
Uptime uint64 `json:"uptime_seconds"`
Version Version `json:"version"`
Version AboutVersion `json:"version"`
Resources AboutResources `json:"resources"`
}
// Version is some information about the binary
type Version struct {
// AboutVersion is some information about the binary
type AboutVersion struct {
Number string `json:"number"`
Commit string `json:"repository_commit"`
Branch string `json:"repository_branch"`
@@ -21,13 +22,26 @@ type Version struct {
Compiler string `json:"compiler"`
}
// AboutResources holds information about the current resource usage
type AboutResources struct {
IsThrottling bool // Whether this core is currently throttling
NCPU float64 // Number of CPU on this node
CPU float64 // Current CPU load, 0-100*ncpu
CPULimit float64 // Defined CPU load limit, 0-100*ncpu
CPUCore float64 // Current CPU load of the core itself, 0-100*ncpu
Mem uint64 // Currently used memory in bytes
MemLimit uint64 // Defined memory limit in bytes
MemTotal uint64 // Total available memory in bytes
MemCore uint64 // Current used memory of the core itself in bytes
}
// MinimalAbout is the minimal information about the API
type MinimalAbout struct {
App string `json:"app"`
Auths []string `json:"auths"`
Version VersionMinimal `json:"version"`
Version AboutVersionMinimal `json:"version"`
}
type VersionMinimal struct {
type AboutVersionMinimal struct {
Number string `json:"number"`
}

View File

@@ -6,6 +6,7 @@ import (
"github.com/datarhei/core/v16/app"
"github.com/datarhei/core/v16/http/api"
"github.com/datarhei/core/v16/resources"
"github.com/datarhei/core/v16/restream"
"github.com/labstack/echo/v4"
@@ -15,13 +16,15 @@ import (
// about the API version and build infos.
type AboutHandler struct {
restream restream.Restreamer
resources resources.Resources
auths func() []string
}
// NewAbout returns a new About type
func NewAbout(restream restream.Restreamer, auths func() []string) *AboutHandler {
func NewAbout(restream restream.Restreamer, resources resources.Resources, auths func() []string) *AboutHandler {
return &AboutHandler{
restream: restream,
resources: resources,
auths: auths,
}
}
@@ -41,7 +44,7 @@ func (p *AboutHandler) About(c echo.Context) error {
return c.JSON(http.StatusOK, api.MinimalAbout{
App: app.Name,
Auths: p.auths(),
Version: api.VersionMinimal{
Version: api.AboutVersionMinimal{
Number: app.Version.MajorString(),
},
})
@@ -56,7 +59,7 @@ func (p *AboutHandler) About(c echo.Context) error {
ID: p.restream.ID(),
CreatedAt: createdAt.Format(time.RFC3339),
Uptime: uint64(time.Since(createdAt).Seconds()),
Version: api.Version{
Version: api.AboutVersion{
Number: app.Version.String(),
Commit: app.Commit,
Branch: app.Branch,
@@ -66,5 +69,18 @@ func (p *AboutHandler) About(c echo.Context) error {
},
}
if p.resources != nil {
res := p.resources.Info()
about.Resources.IsThrottling = res.CPU.Throttling
about.Resources.NCPU = res.CPU.NCPU
about.Resources.CPU = (100 - res.CPU.Idle) * res.CPU.NCPU
about.Resources.CPULimit = res.CPU.Limit * res.CPU.NCPU
about.Resources.CPUCore = res.CPU.Core * res.CPU.NCPU
about.Resources.Mem = res.Mem.Total - res.Mem.Available
about.Resources.MemLimit = res.Mem.Limit
about.Resources.MemTotal = res.Mem.Total
about.Resources.MemCore = res.Mem.Core
}
return c.JSON(http.StatusOK, about)
}

View File

@@ -19,7 +19,7 @@ func getDummyAboutRouter() (*echo.Echo, error) {
return nil, err
}
handler := NewAbout(rs, func() []string { return []string{} })
handler := NewAbout(rs, nil, func() []string { return []string{} })
router.Add("GET", "/", handler.About)

View File

@@ -100,7 +100,7 @@ func (h *ClusterHandler) NodeGetVersion(c echo.Context) error {
v := peer.CoreAbout()
version := api.Version{
version := api.AboutVersion{
Number: v.Version.Number,
Commit: v.Version.Commit,
Branch: v.Version.Branch,

View File

@@ -51,6 +51,7 @@ import (
"github.com/datarhei/core/v16/monitor"
"github.com/datarhei/core/v16/net"
"github.com/datarhei/core/v16/prometheus"
"github.com/datarhei/core/v16/resources"
"github.com/datarhei/core/v16/restream"
"github.com/datarhei/core/v16/rtmp"
"github.com/datarhei/core/v16/session"
@@ -100,6 +101,7 @@ type Config struct {
Cluster cluster.Cluster
IAM iam.IAM
IAMSkipper func(ip string) bool
Resources resources.Resources
}
type CorsConfig struct {
@@ -251,6 +253,7 @@ func NewServer(config Config) (serverhandler.Server, error) {
s.handler.about = api.NewAbout(
config.Restream,
config.Resources,
func() []string { return config.IAM.Validators() },
)