-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
extension.go
247 lines (210 loc) · 6.99 KB
/
extension.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package dockerobserver // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer/dockerobserver"
import (
"context"
"fmt"
"strings"
"sync"
"time"
dtypes "github.com/docker/docker/api/types"
"github.com/docker/go-connections/nat"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/extension"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer"
dcommon "github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/docker"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/docker"
)
const (
defaultDockerAPIVersion = 1.22
minimalRequiredDockerAPIVersion = 1.22
)
var _ extension.Extension = (*dockerObserver)(nil)
var _ observer.EndpointsLister = (*dockerObserver)(nil)
var _ observer.Observable = (*dockerObserver)(nil)
type dockerObserver struct {
*observer.EndpointsWatcher
logger *zap.Logger
config *Config
cancel func()
once *sync.Once
ctx context.Context
dClient *docker.Client
}
// newObserver creates a new docker observer extension.
func newObserver(logger *zap.Logger, config *Config) (extension.Extension, error) {
d := &dockerObserver{
logger: logger, config: config,
once: &sync.Once{},
cancel: func() {
// Safe value provided on initialisation
},
}
d.EndpointsWatcher = observer.NewEndpointsWatcher(d, time.Second, logger)
return d, nil
}
// Start will instantiate required components needed by the Docker observer
func (d *dockerObserver) Start(ctx context.Context, _ component.Host) error {
dCtx, cancel := context.WithCancel(context.Background())
d.cancel = cancel
d.ctx = dCtx
// Create new Docker client
dConfig, err := docker.NewConfig(d.config.Endpoint, d.config.Timeout, d.config.ExcludedImages, d.config.DockerAPIVersion)
if err != nil {
return err
}
d.dClient, err = docker.NewDockerClient(dConfig, d.logger)
if err != nil {
return fmt.Errorf("could not create docker client: %w", err)
}
if err = d.dClient.LoadContainerList(ctx); err != nil {
return err
}
d.once.Do(
func() {
go func() {
cacheRefreshTicker := time.NewTicker(d.config.CacheSyncInterval)
defer cacheRefreshTicker.Stop()
clientCtx, clientCancel := context.WithCancel(d.ctx)
go d.dClient.ContainerEventLoop(clientCtx)
for {
select {
case <-d.ctx.Done():
clientCancel()
return
case <-cacheRefreshTicker.C:
err = d.dClient.LoadContainerList(clientCtx)
if err != nil {
d.logger.Error("Could not sync container cache", zap.Error(err))
}
}
}
}()
},
)
return nil
}
func (d *dockerObserver) Shutdown(_ context.Context) error {
d.cancel()
return nil
}
func (d *dockerObserver) ListEndpoints() []observer.Endpoint {
var endpoints []observer.Endpoint
for _, container := range d.dClient.Containers() {
endpoints = append(endpoints, d.containerEndpoints(container.ContainerJSON)...)
}
return endpoints
}
// containerEndpoints generates a list of observer.Endpoint given a Docker ContainerJSON.
// This function will only generate endpoints if a container is in the Running state and not Paused.
func (d *dockerObserver) containerEndpoints(c *dtypes.ContainerJSON) []observer.Endpoint {
var endpoints []observer.Endpoint
if !c.State.Running || c.State.Running && c.State.Paused {
return endpoints
}
knownPorts := map[nat.Port]bool{}
for k := range c.Config.ExposedPorts {
knownPorts[k] = true
}
// iterate over exposed ports and try to create endpoints
for portObj := range knownPorts {
endpoint := d.endpointForPort(portObj, c)
// the endpoint was not set, so we'll drop it
if endpoint == nil {
continue
}
endpoints = append(endpoints, *endpoint)
}
return endpoints
}
// endpointForPort creates an observer.Endpoint for a given port that is exposed in a Docker container.
// Each endpoint has a unique ID generated by the combination of the container.ID, container.Name,
// underlying host name, and the port number.
// Uses the user provided config settings to override certain fields.
func (d *dockerObserver) endpointForPort(portObj nat.Port, c *dtypes.ContainerJSON) *observer.Endpoint {
endpoint := observer.Endpoint{}
port := uint16(portObj.Int())
proto := portObj.Proto()
mappedPort, mappedIP := findHostMappedPort(c, portObj)
if d.config.IgnoreNonHostBindings && mappedPort == 0 && mappedIP == "" {
return nil
}
// unique ID per containerID:port
var id observer.EndpointID
if mappedPort != 0 {
id = observer.EndpointID(fmt.Sprintf("%s:%d", c.ID, mappedPort))
} else {
id = observer.EndpointID(fmt.Sprintf("%s:%d", c.ID, port))
}
imageRef, err := dcommon.ParseImageName(c.Config.Image)
if err != nil {
d.logger.Error("could not parse container image name", zap.Error(err))
}
details := &observer.Container{
Name: strings.TrimPrefix(c.Name, "/"),
Image: imageRef.Repository,
Tag: imageRef.Tag,
Command: strings.Join(c.Config.Cmd, " "),
ContainerID: c.ID,
Transport: portProtoToTransport(proto),
Labels: c.Config.Labels,
}
// Set our hostname based on config settings
if d.config.UseHostnameIfPresent && c.Config.Hostname != "" {
details.Host = c.Config.Hostname
} else {
// Use the IP Address of the first network we iterate over.
// This can be made configurable if so desired.
for _, n := range c.NetworkSettings.Networks {
details.Host = n.IPAddress
break
}
// If we still haven't gotten a host at this point and we are using
// host bindings, just make it localhost.
if details.Host == "" && d.config.UseHostBindings {
details.Host = "127.0.0.1"
}
}
// If we are using HostBindings & port and IP are set, use those
if d.config.UseHostBindings && mappedPort != 0 && mappedIP != "" {
details.Host = mappedIP
details.Port = mappedPort
details.AlternatePort = port
if details.Host == "0.0.0.0" {
details.Host = "127.0.0.1"
}
} else {
details.Port = port
details.AlternatePort = mappedPort
}
endpoint = observer.Endpoint{
ID: id,
Target: fmt.Sprintf("%s:%d", details.Host, details.Port),
Details: details,
}
return &endpoint
}
// FindHostMappedPort returns the port number of the docker port binding to the
// underlying host, or 0 if none exists. It also returns the mapped ip that the
// port is bound to on the underlying host, or "" if none exists.
func findHostMappedPort(c *dtypes.ContainerJSON, exposedPort nat.Port) (uint16, string) {
bindings := c.NetworkSettings.Ports[exposedPort]
for _, binding := range bindings {
if port, err := nat.ParsePort(binding.HostPort); err == nil {
return uint16(port), binding.HostIP
}
}
return 0, ""
}
// Valid proto for docker containers should be tcp, udp, sctp
// https://github.com/docker/go-connections/blob/v0.4.0/nat/nat.go#L116
func portProtoToTransport(proto string) observer.Transport {
switch proto {
case "tcp":
return observer.ProtocolTCP
case "udp":
return observer.ProtocolUDP
}
return observer.ProtocolUnknown
}