cleanup; fix environment stats not reporting network TX correctly
This commit is contained in:
parent
963a906c30
commit
1937d0366d
|
@ -1,20 +0,0 @@
|
||||||
package docker
|
|
||||||
|
|
||||||
import "io"
|
|
||||||
|
|
||||||
type Console struct {
|
|
||||||
HandlerFunc *func(string)
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ io.Writer = Console{}
|
|
||||||
|
|
||||||
func (c Console) Write(b []byte) (int, error) {
|
|
||||||
if c.HandlerFunc != nil {
|
|
||||||
l := make([]byte, len(b))
|
|
||||||
copy(l, b)
|
|
||||||
|
|
||||||
(*c.HandlerFunc)(string(l))
|
|
||||||
}
|
|
||||||
|
|
||||||
return len(b), nil
|
|
||||||
}
|
|
|
@ -76,6 +76,10 @@ func (e *Environment) Attach() error {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// Block the completion of this routine until the container is no longer running. This allows
|
||||||
|
// the pollResources function to run until it needs to be stopped. Because the container
|
||||||
|
// can be polled for resource usage, even when sropped, we need to have this logic present
|
||||||
|
// in order to cancel the context and therefore stop the routine that is spawned.
|
||||||
ok, err := e.client.ContainerWait(ctx, e.Id, container.WaitConditionNotRunning)
|
ok, err := e.client.ContainerWait(ctx, e.Id, container.WaitConditionNotRunning)
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"github.com/pterodactyl/wings/environment"
|
"github.com/pterodactyl/wings/environment"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
"sync/atomic"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Attach to the instance and then automatically emit an event whenever the resource usage for the
|
// Attach to the instance and then automatically emit an event whenever the resource usage for the
|
||||||
|
@ -28,21 +27,18 @@ func (e *Environment) pollResources(ctx context.Context) error {
|
||||||
defer stats.Body.Close()
|
defer stats.Body.Close()
|
||||||
|
|
||||||
dec := json.NewDecoder(stats.Body)
|
dec := json.NewDecoder(stats.Body)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
default:
|
default:
|
||||||
var v *types.StatsJSON
|
var v types.StatsJSON
|
||||||
|
|
||||||
if err := dec.Decode(&v); err != nil {
|
if err := dec.Decode(&v); err != nil {
|
||||||
if err != io.EOF && !errors.Is(err, context.Canceled) {
|
if err != io.EOF && !errors.Is(err, context.Canceled) {
|
||||||
e.log().WithField("error", err).Warn("error while processing Docker stats output for container")
|
e.log().WithField("error", err).Warn("error while processing Docker stats output for container")
|
||||||
} else {
|
} else {
|
||||||
e.log().Debug("io.EOF encountered during stats decode, stopping polling...")
|
e.log().Debug("io.EOF encountered during stats decode, stopping polling...")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,24 +48,16 @@ func (e *Environment) pollResources(ctx context.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var rx uint64
|
|
||||||
var tx uint64
|
|
||||||
for _, nw := range v.Networks {
|
|
||||||
atomic.AddUint64(&rx, nw.RxBytes)
|
|
||||||
atomic.AddUint64(&tx, nw.RxBytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
st := environment.Stats{
|
st := environment.Stats{
|
||||||
Memory: calculateDockerMemory(v.MemoryStats),
|
Memory: calculateDockerMemory(v.MemoryStats),
|
||||||
MemoryLimit: v.MemoryStats.Limit,
|
MemoryLimit: v.MemoryStats.Limit,
|
||||||
CpuAbsolute: calculateDockerAbsoluteCpu(&v.PreCPUStats, &v.CPUStats),
|
CpuAbsolute: calculateDockerAbsoluteCpu(v.PreCPUStats, v.CPUStats),
|
||||||
Network: struct {
|
Network: environment.NetworkStats{},
|
||||||
RxBytes uint64 `json:"rx_bytes"`
|
}
|
||||||
TxBytes uint64 `json:"tx_bytes"`
|
|
||||||
}{
|
for _, nw := range v.Networks {
|
||||||
RxBytes: rx,
|
st.Network.RxBytes += nw.RxBytes
|
||||||
TxBytes: tx,
|
st.Network.TxBytes += nw.TxBytes
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if b, err := json.Marshal(st); err != nil {
|
if b, err := json.Marshal(st); err != nil {
|
||||||
|
@ -106,7 +94,7 @@ func calculateDockerMemory(stats types.MemoryStats) uint64 {
|
||||||
// by the defined CPU limits on the container.
|
// by the defined CPU limits on the container.
|
||||||
//
|
//
|
||||||
// @see https://github.com/docker/cli/blob/aa097cf1aa19099da70930460250797c8920b709/cli/command/container/stats_helpers.go#L166
|
// @see https://github.com/docker/cli/blob/aa097cf1aa19099da70930460250797c8920b709/cli/command/container/stats_helpers.go#L166
|
||||||
func calculateDockerAbsoluteCpu(pStats *types.CPUStats, stats *types.CPUStats) float64 {
|
func calculateDockerAbsoluteCpu(pStats types.CPUStats, stats types.CPUStats) float64 {
|
||||||
// Calculate the change in CPU usage between the current and previous reading.
|
// Calculate the change in CPU usage between the current and previous reading.
|
||||||
cpuDelta := float64(stats.CPUUsage.TotalUsage) - float64(pStats.CPUUsage.TotalUsage)
|
cpuDelta := float64(stats.CPUUsage.TotalUsage) - float64(pStats.CPUUsage.TotalUsage)
|
||||||
|
|
||||||
|
|
|
@ -24,8 +24,10 @@ type Stats struct {
|
||||||
// Disk int64 `json:"disk_bytes"`
|
// Disk int64 `json:"disk_bytes"`
|
||||||
|
|
||||||
// Current network transmit in & out for a container.
|
// Current network transmit in & out for a container.
|
||||||
Network struct {
|
Network NetworkStats `json:"network"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type NetworkStats struct {
|
||||||
RxBytes uint64 `json:"rx_bytes"`
|
RxBytes uint64 `json:"rx_bytes"`
|
||||||
TxBytes uint64 `json:"tx_bytes"`
|
TxBytes uint64 `json:"tx_bytes"`
|
||||||
} `json:"network"`
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user