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 return false
}, },
Resources: a.resources,
} }
mainserverhandler, err := http.NewServer(serverConfig) mainserverhandler, err := http.NewServer(serverConfig)

View File

@@ -2,17 +2,18 @@ package api
// About is some general information about the API // About is some general information about the API
type About struct { type About struct {
App string `json:"app"` App string `json:"app"`
Auths []string `json:"auths"` Auths []string `json:"auths"`
Name string `json:"name"` Name string `json:"name"`
ID string `json:"id"` ID string `json:"id"`
CreatedAt string `json:"created_at"` // RFC3339 CreatedAt string `json:"created_at"` // RFC3339
Uptime uint64 `json:"uptime_seconds"` Uptime uint64 `json:"uptime_seconds"`
Version Version `json:"version"` Version AboutVersion `json:"version"`
Resources AboutResources `json:"resources"`
} }
// Version is some information about the binary // AboutVersion is some information about the binary
type Version struct { type AboutVersion struct {
Number string `json:"number"` Number string `json:"number"`
Commit string `json:"repository_commit"` Commit string `json:"repository_commit"`
Branch string `json:"repository_branch"` Branch string `json:"repository_branch"`
@@ -21,13 +22,26 @@ type Version struct {
Compiler string `json:"compiler"` Compiler string `json:"compiler"`
} }
// MinimalAbout is the minimal information about the API // AboutResources holds information about the current resource usage
type MinimalAbout struct { type AboutResources struct {
App string `json:"app"` IsThrottling bool // Whether this core is currently throttling
Auths []string `json:"auths"` NCPU float64 // Number of CPU on this node
Version VersionMinimal `json:"version"` 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
} }
type VersionMinimal struct { // MinimalAbout is the minimal information about the API
type MinimalAbout struct {
App string `json:"app"`
Auths []string `json:"auths"`
Version AboutVersionMinimal `json:"version"`
}
type AboutVersionMinimal struct {
Number string `json:"number"` Number string `json:"number"`
} }

View File

@@ -6,6 +6,7 @@ import (
"github.com/datarhei/core/v16/app" "github.com/datarhei/core/v16/app"
"github.com/datarhei/core/v16/http/api" "github.com/datarhei/core/v16/http/api"
"github.com/datarhei/core/v16/resources"
"github.com/datarhei/core/v16/restream" "github.com/datarhei/core/v16/restream"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@@ -14,15 +15,17 @@ import (
// The AboutHandler type provides handler functions for retrieving details // The AboutHandler type provides handler functions for retrieving details
// about the API version and build infos. // about the API version and build infos.
type AboutHandler struct { type AboutHandler struct {
restream restream.Restreamer restream restream.Restreamer
auths func() []string resources resources.Resources
auths func() []string
} }
// NewAbout returns a new About type // 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{ return &AboutHandler{
restream: restream, restream: restream,
auths: auths, resources: resources,
auths: auths,
} }
} }
@@ -41,7 +44,7 @@ func (p *AboutHandler) About(c echo.Context) error {
return c.JSON(http.StatusOK, api.MinimalAbout{ return c.JSON(http.StatusOK, api.MinimalAbout{
App: app.Name, App: app.Name,
Auths: p.auths(), Auths: p.auths(),
Version: api.VersionMinimal{ Version: api.AboutVersionMinimal{
Number: app.Version.MajorString(), Number: app.Version.MajorString(),
}, },
}) })
@@ -56,7 +59,7 @@ func (p *AboutHandler) About(c echo.Context) error {
ID: p.restream.ID(), ID: p.restream.ID(),
CreatedAt: createdAt.Format(time.RFC3339), CreatedAt: createdAt.Format(time.RFC3339),
Uptime: uint64(time.Since(createdAt).Seconds()), Uptime: uint64(time.Since(createdAt).Seconds()),
Version: api.Version{ Version: api.AboutVersion{
Number: app.Version.String(), Number: app.Version.String(),
Commit: app.Commit, Commit: app.Commit,
Branch: app.Branch, 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) return c.JSON(http.StatusOK, about)
} }

View File

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

View File

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

View File

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