-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathmetadata_ecs.go
103 lines (84 loc) · 2.59 KB
/
metadata_ecs.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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License 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 ecs
import (
"encoding/json"
"log"
"net/http"
)
type TaskMetaData struct {
Cluster string
LaunchType string // TODO: Change to enum when defined in otel collector convent
TaskARN string
Family string
AvailabilityZone string
Containers []Container
}
type Container struct {
DockerID string `json:"DockerId"`
ContainerARN string
Type string
KnownStatus string
LogDriver string
LogOptions LogData
}
type LogData struct {
LogGroup string `json:"awslogs-group"`
Region string `json:"awslogs-region"`
Stream string `json:"awslogs-stream"`
}
type ecsMetadataProviderImpl struct {
client HTTPClient
}
var _ ecsMetadataProvider = &ecsMetadataProviderImpl{}
// Retrieves the metadata for a task running on Amazon ECS
func (md *ecsMetadataProviderImpl) fetchTaskMetaData(tmde string) (*TaskMetaData, error) {
ret, err := fetch(tmde+"/task", md, true)
if ret == nil {
return nil, err
}
return ret.(*TaskMetaData), err
}
// Retrieves the metadata for the Amazon ECS Container the collector is running on
func (md *ecsMetadataProviderImpl) fetchContainerMetaData(tmde string) (*Container, error) {
ret, err := fetch(tmde, md, false)
if ret == nil {
return nil, err
}
return ret.(*Container), err
}
func fetch(tmde string, md *ecsMetadataProviderImpl, task bool) (tmdeResp interface{}, err error) {
req, err := http.NewRequest(http.MethodGet, tmde, nil)
if err != nil {
log.Printf("Received error constructing request to ECS Task Metadata Endpoint: %v", err)
return nil, err
}
resp, err := md.client.Do(req)
if err != nil {
log.Printf("Received error from ECS Task Metadata Endpoint: %v", err)
return nil, err
}
if task {
tmdeResp = &TaskMetaData{}
} else {
tmdeResp = &Container{}
}
err = json.NewDecoder(resp.Body).Decode(tmdeResp)
defer resp.Body.Close()
if err != nil {
log.Printf("Encountered unexpected error reading response from ECS Task Metadata Endpoint: %v", err)
return nil, err
}
return tmdeResp, nil
}