/
task_stats_handler.go
69 lines (61 loc) · 2.59 KB
/
task_stats_handler.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
// 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 v4
import (
"encoding/json"
"fmt"
"net/http"
"github.com/aws/amazon-ecs-agent/agent/engine/dockerstate"
"github.com/aws/amazon-ecs-agent/agent/handlers/utils"
v3 "github.com/aws/amazon-ecs-agent/agent/handlers/v3"
"github.com/aws/amazon-ecs-agent/agent/stats"
"github.com/cihub/seelog"
)
var TaskStatsPath = "/v4/" + utils.ConstructMuxVar(v3.V3EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/task/stats"
func TaskStatsHandler(state dockerstate.TaskEngineState, statsEngine stats.Engine) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
taskArn, err := v3.GetTaskARNByRequest(r, state)
if err != nil {
errResponseJSON, err := json.Marshal(fmt.Sprintf("V4 task stats handler: unable to get task arn from request: %s", err.Error()))
if e := utils.WriteResponseIfMarshalError(w, err); e != nil {
return
}
utils.WriteJSONToResponse(w, http.StatusBadRequest, errResponseJSON, utils.RequestTypeTaskStats)
return
}
seelog.Infof("V4 tasks stats handler: writing response for task '%s'", taskArn)
WriteV4TaskStatsResponse(w, taskArn, state, statsEngine)
}
}
// WriteV4TaskStatsResponse writes the task stats to response writer.
func WriteV4TaskStatsResponse(w http.ResponseWriter,
taskARN string,
state dockerstate.TaskEngineState,
statsEngine stats.Engine) {
taskStatsResponse, err := NewV4TaskStatsResponse(taskARN, state, statsEngine)
if err != nil {
seelog.Warnf("Unable to get task stats for task '%s': %v", taskARN, err)
errResponseJSON, err := json.Marshal("Unable to get task stats for: " + taskARN)
if e := utils.WriteResponseIfMarshalError(w, err); e != nil {
return
}
utils.WriteJSONToResponse(w, http.StatusBadRequest, errResponseJSON, utils.RequestTypeTaskStats)
return
}
responseJSON, err := json.Marshal(taskStatsResponse)
if e := utils.WriteResponseIfMarshalError(w, err); e != nil {
return
}
seelog.Infof("V4 Stats response json is %v", responseJSON)
utils.WriteJSONToResponse(w, http.StatusOK, responseJSON, utils.RequestTypeTaskStats)
}