Skip to content
Merged
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
16 changes: 13 additions & 3 deletions daemon/mgr/container_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,15 @@ func (mgr *ContainerManager) StreamStats(ctx context.Context, name string, confi
if err != nil {
return err
}
containerStat := toContainerStats(metrics.Timestamp, stats)

if err := enc.Encode(containerStat); err != nil {
return err
// metrics may be nil if the container is not running,
// so just ignore it and try get the metrics next time.
if metrics != nil {
containerStat := toContainerStats(metrics.Timestamp, stats)

if err := enc.Encode(containerStat); err != nil {
return err
}
}

time.Sleep(DefaultStatsInterval)
Expand All @@ -81,6 +86,11 @@ func (mgr *ContainerManager) Stats(ctx context.Context, name string) (*container
ID := c.ID
c.Unlock()

// only get metrics when the container is running
if !(c.IsRunning() || c.IsPaused()) {
return nil, nil, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return nil will panic if the StreamStat toContainerStats consumes the result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense, i have fix it.

}

metric, err := mgr.Client.ContainerStats(ctx, ID)
if err != nil {
return nil, nil, err
Expand Down