MCPcopy Index your code
hub / github.com/docker/cli / collect

Function collect

cli/command/container/stats_helpers.go:68–181  ·  view source on GitHub ↗
(ctx context.Context, s *Stats, cli client.ContainerAPIClient, streamStats bool, waitFirst *sync.WaitGroup)

Source from the content-addressed store, hash-verified

66}
67
68func collect(ctx context.Context, s *Stats, cli client.ContainerAPIClient, streamStats bool, waitFirst *sync.WaitGroup) { //nolint:gocyclo
69 var getFirst bool
70
71 defer func() {
72 // if error happens, and we get nothing of stats, release wait group whatever
73 if !getFirst {
74 getFirst = true
75 waitFirst.Done()
76 }
77 }()
78
79 response, err := cli.ContainerStats(ctx, s.Container, client.ContainerStatsOptions{
80 Stream: streamStats,
81 IncludePreviousSample: !streamStats, // collect previous CPU value for the first result when not streaming.
82 })
83 if err != nil {
84 s.SetError(err)
85 return
86 }
87
88 u := make(chan error, 1)
89 go func() {
90 defer response.Body.Close()
91 dec := json.NewDecoder(response.Body)
92 for {
93 if ctx.Err() != nil {
94 return
95 }
96 var v container.StatsResponse
97 if err := dec.Decode(&v); err != nil {
98 dec = json.NewDecoder(io.MultiReader(dec.Buffered(), response.Body))
99 u <- err
100 if err == io.EOF {
101 break
102 }
103 time.Sleep(100 * time.Millisecond)
104 continue
105 }
106
107 // Daemon versions before v29 did not return per-stats OSType;
108 // fall back to using the daemon's OSType.
109 if v.OSType == "" {
110 v.OSType = daemonOSType
111 }
112
113 if daemonOSType == "windows" {
114 netRx, netTx := calculateNetwork(v.Networks)
115 s.SetStatistics(StatsEntry{
116 Name: v.Name,
117 ID: v.ID,
118 CPUPercentage: calculateCPUPercentWindows(&v),
119 Memory: float64(v.MemoryStats.PrivateWorkingSet),
120 NetworkRx: netRx,
121 NetworkTx: netTx,
122 BlockRead: float64(v.StorageStats.ReadSizeBytes),
123 BlockWrite: float64(v.StorageStats.WriteSizeBytes),
124 })
125 } else {

Callers 1

RunStatsFunction · 0.85

Calls 11

calculateNetworkFunction · 0.85
calculateBlockIOFunction · 0.85
calculateCPUPercentUnixFunction · 0.85
SetErrorMethod · 0.80
SetStatisticsMethod · 0.80
SetErrorAndResetMethod · 0.80
ErrMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…