-
Notifications
You must be signed in to change notification settings - Fork 621
/
Copy pathtask.go
173 lines (154 loc) · 4.85 KB
/
task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package stats
import (
"context"
"fmt"
"time"
"github.com/aws/amazon-ecs-agent/agent/logger"
"github.com/aws/amazon-ecs-agent/agent/logger/field"
apitaskstatus "github.com/aws/amazon-ecs-agent/agent/api/task/status"
"github.com/aws/amazon-ecs-agent/agent/config"
"github.com/aws/amazon-ecs-agent/agent/stats/resolver"
"github.com/aws/amazon-ecs-agent/agent/utils/retry"
"github.com/docker/docker/api/types"
dockerstats "github.com/docker/docker/api/types"
)
// statsTaskCommon contains the common fields in StatsTask for both Linux and Windows.
// StatsTask abstracts methods to gather and aggregate network data for a task. Used only for AWSVPC mode.
type statsTaskCommon struct {
StatsQueue *Queue
TaskMetadata *TaskMetadata
Ctx context.Context
Cancel context.CancelFunc
Resolver resolver.ContainerMetadataResolver
metricPublishInterval time.Duration
}
func (taskStat *StatsTask) StartStatsCollection() {
queueSize := int(config.DefaultContainerMetricsPublishInterval.Seconds() * 4)
taskStat.StatsQueue = NewQueue(queueSize)
taskStat.StatsQueue.Reset()
go taskStat.collect()
}
func (taskStat *StatsTask) StopStatsCollection() {
taskStat.Cancel()
}
func (taskStat *StatsTask) collect() {
taskId := taskStat.TaskMetadata.TaskId
backoff := retry.NewExponentialBackoff(time.Second*1, time.Second*10, 0.5, 2)
for {
err := taskStat.processStatsStream()
select {
case <-taskStat.Ctx.Done():
logger.Debug("Stopping stats collection for taskStat", logger.Fields{
field.TaskID: taskId,
})
return
default:
if err != nil {
d := backoff.Duration()
time.Sleep(d)
logger.Debug("Error querying stats for task", logger.Fields{
field.TaskID: taskId,
field.Error: err,
})
}
// We were disconnected from the stats stream.
// Check if the task is terminal. If it is, stop collecting metrics.
terminal, err := taskStat.terminal()
if err != nil {
// Error determining if the task is terminal. clean-up anyway.
logger.Warn("Error determining if the task is terminal, stopping stats collection", logger.Fields{
field.TaskID: taskId,
field.Error: err,
})
taskStat.StopStatsCollection()
} else if terminal {
logger.Warn("Task is terminal, stopping stats collection", logger.Fields{
field.TaskID: taskId,
})
taskStat.StopStatsCollection()
}
}
}
}
func (taskStat *StatsTask) processStatsStream() error {
taskId := taskStat.TaskMetadata.TaskId
awsvpcNetworkStats, errC := taskStat.getAWSVPCNetworkStats()
returnError := false
for {
select {
case <-taskStat.Ctx.Done():
logger.Info("Task context is done", logger.Fields{
field.TaskID: taskId,
})
return nil
case err := <-errC:
logger.Warn("Error encountered processing metrics stream from host, this may affect cloudwatch metric accuracy", logger.Fields{
field.TaskID: taskId,
field.Error: err,
})
returnError = true
case rawStat, ok := <-awsvpcNetworkStats:
if !ok {
if returnError {
return fmt.Errorf("error encountered processing metrics stream from host")
}
return nil
}
if err := taskStat.StatsQueue.Add(rawStat); err != nil {
logger.Warn("Error converting task stats", logger.Fields{
field.TaskID: taskId,
field.Error: err,
})
}
}
}
}
func (taskStat *StatsTask) terminal() (bool, error) {
resolvedTask, err := taskStat.Resolver.ResolveTaskByARN(taskStat.TaskMetadata.TaskArn)
if err != nil {
return false, err
}
return resolvedTask.GetKnownStatus() == apitaskstatus.TaskStopped, nil
}
func (taskStat *StatsTask) getAWSVPCNetworkStats() (<-chan *types.StatsJSON, <-chan error) {
errC := make(chan error, 1)
statsC := make(chan *dockerstats.StatsJSON)
if taskStat.TaskMetadata.NumberContainers > 0 {
go func() {
defer close(statsC)
statPollTicker := time.NewTicker(taskStat.metricPublishInterval)
defer statPollTicker.Stop()
for range statPollTicker.C {
networkStats, err := taskStat.retrieveNetworkStatistics()
if err != nil {
errC <- err
return
}
dockerStats := &types.StatsJSON{
Networks: networkStats,
Stats: types.Stats{
Read: time.Now(),
},
}
select {
case <-taskStat.Ctx.Done():
return
case statsC <- dockerStats:
}
}
}()
}
return statsC, errC
}