-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
endpoints.go
362 lines (327 loc) · 10.2 KB
/
endpoints.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package observer // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer"
import (
"errors"
"fmt"
"net"
"reflect"
)
type (
// EndpointID unique identifies an endpoint per-observer instance.
EndpointID string
// EndpointEnv is a map of endpoint attributes.
EndpointEnv map[string]any
// EndpointType is a type of an endpoint like a port or pod.
EndpointType string
)
const (
// PortType is a port endpoint.
PortType EndpointType = "port"
// PodType is a pod endpoint.
PodType EndpointType = "pod"
// K8sServiceType is a service endpoint.
K8sServiceType EndpointType = "k8s.service"
// K8sIngressType is a ingress endpoint.
K8sIngressType EndpointType = "k8s.ingress"
// K8sNodeType is a Kubernetes Node endpoint.
K8sNodeType EndpointType = "k8s.node"
// HostPortType is a hostport endpoint.
HostPortType EndpointType = "hostport"
// ContainerType is a container endpoint.
ContainerType EndpointType = "container"
)
var (
_ EndpointDetails = (*Pod)(nil)
_ EndpointDetails = (*Port)(nil)
_ EndpointDetails = (*K8sService)(nil)
_ EndpointDetails = (*K8sNode)(nil)
_ EndpointDetails = (*HostPort)(nil)
_ EndpointDetails = (*Container)(nil)
)
// EndpointDetails provides additional context about an endpoint such as a Pod or Port.
type EndpointDetails interface {
Env() EndpointEnv
Type() EndpointType
}
// Endpoint is a service that can be contacted remotely.
type Endpoint struct {
// ID uniquely identifies this endpoint.
ID EndpointID
// Target is an IP address or hostname of the endpoint. It can also be a hostname/ip:port pair.
Target string
// Details contains additional context about the endpoint such as a Pod or Port.
Details EndpointDetails
}
// Env converts an endpoint into a map suitable for expr evaluation.
func (e *Endpoint) Env() (EndpointEnv, error) {
if e.Details == nil {
return nil, errors.New("endpoint is missing details")
}
env := e.Details.Env()
env["endpoint"] = e.Target
env["type"] = string(e.Details.Type())
env["id"] = string(e.ID)
// Exposing the target as a split "host" and "port" enables the receiver creator
// to be able to discover receivers that require these options to be configured
// separately.
const hostKey = "host"
const portKey = "port"
host, port, err := net.SplitHostPort(e.Target)
// An error most likely means there was no port when splitting, so the host
// can simply be the target.
if err != nil {
host = e.Target
} else {
// Only try to set the port if a valid port was found when splitting the target
if _, keyExists := env[portKey]; !keyExists {
env[portKey] = port
}
}
if _, keyExists := env[hostKey]; !keyExists {
env[hostKey] = host
}
return env, nil
}
func (e *Endpoint) String() string {
return fmt.Sprintf("Endpoint{ID: %v, Target: %v, Details: %T%+v}", e.ID, e.Target, e.Details, e.Details)
}
func (e Endpoint) equals(other Endpoint) bool {
switch {
case e.ID != other.ID:
return false
case e.Target != other.Target:
return false
case e.Details == nil && other.Details != nil:
return false
case other.Details == nil && e.Details != nil:
return false
case e.Details == nil && other.Details == nil:
return true
case e.Details.Type() != other.Details.Type():
return false
default:
return reflect.DeepEqual(e.Details.Env(), other.Details.Env())
}
}
// K8sService is a discovered k8s service.
type K8sService struct {
// Name of the service.
Name string
// UID is the unique ID in the cluster for the service.
UID string
// Labels is a map of user-specified metadata.
Labels map[string]string
// Annotations is a map of user-specified metadata.
Annotations map[string]string
// Namespace must be unique for services with same name.
Namespace string
// ClusterIP is the IP under which the service is reachable within the cluster.
ClusterIP string
// ServiceType is the type of the service: ClusterIP, NodePort, LoadBalancer, ExternalName
ServiceType string
}
func (s *K8sService) Env() EndpointEnv {
return map[string]any{
"uid": s.UID,
"name": s.Name,
"labels": s.Labels,
"annotations": s.Annotations,
"namespace": s.Namespace,
"cluster_ip": s.ClusterIP,
"service_type": s.ServiceType,
}
}
func (s *K8sService) Type() EndpointType {
return K8sServiceType
}
// K8sIngress is a discovered k8s ingress.
type K8sIngress struct {
// Name of the ingress.
Name string
// UID is the unique ID in the cluster for the ingress.
UID string
// Labels is a map of user-specified metadata.
Labels map[string]string
// Annotations is a map of user-specified metadata.
Annotations map[string]string
// Namespace must be unique for ingress with same name.
Namespace string
// Scheme represents whether the ingress path is accessible via HTTPS or HTTP.
Scheme string
// Host is the fully qualified domain name of a network host
Host string
// Path that map requests to backends
Path string
}
func (s *K8sIngress) Env() EndpointEnv {
return map[string]any{
"uid": s.UID,
"name": s.Name,
"labels": s.Labels,
"annotations": s.Annotations,
"namespace": s.Namespace,
"scheme": s.Scheme,
"host": s.Host,
"path": s.Path,
}
}
func (s *K8sIngress) Type() EndpointType {
return K8sIngressType
}
// Pod is a discovered k8s pod.
type Pod struct {
// Name of the pod.
Name string
// UID is the unique ID in the cluster for the pod.
UID string
// Labels is a map of user-specified metadata.
Labels map[string]string
// Annotations is a map of user-specified metadata.
Annotations map[string]string
// Namespace must be unique for pods with same name.
Namespace string
}
func (p *Pod) Env() EndpointEnv {
return map[string]any{
"uid": p.UID,
"name": p.Name,
"labels": p.Labels,
"annotations": p.Annotations,
"namespace": p.Namespace,
}
}
func (p *Pod) Type() EndpointType {
return PodType
}
// Port is an endpoint that has a target as well as a port.
type Port struct {
// Name is the name of the container port.
Name string
// Pod is the k8s pod in which the container is running.
Pod Pod
// Port number of the endpoint.
Port uint16
// Transport is the transport protocol used by the Endpoint. (TCP or UDP).
Transport Transport
}
func (p *Port) Env() EndpointEnv {
return map[string]any{
"name": p.Name,
"port": p.Port,
"pod": p.Pod.Env(),
"transport": p.Transport,
}
}
func (p *Port) Type() EndpointType {
return PortType
}
// HostPort is an endpoint discovered on a host.
type HostPort struct {
// ProcessName of the process associated to Endpoint. If host_observer
// is unable to collect information about process using the
// Port, this value is an empty string.
ProcessName string
// Command used to invoke the process using the Endpoint.
Command string
// Port number of the endpoint.
Port uint16
// Transport is the transport protocol used by the Endpoint. (TCP or UDP).
Transport Transport
// IsIPv6 indicates whether or not the Endpoint is IPv6.
IsIPv6 bool
}
func (h *HostPort) Env() EndpointEnv {
return map[string]any{
"process_name": h.ProcessName,
"command": h.Command,
"is_ipv6": h.IsIPv6,
"port": h.Port,
"transport": h.Transport,
}
}
func (h *HostPort) Type() EndpointType {
return HostPortType
}
// Container is a discovered container
type Container struct {
// Name is the primary name of the container
Name string
// Image is the name of the container image
Image string
// Tag is the container image tag, e.g. '0.1'
Tag string
// Port is the exposed port of container
Port uint16
// AlternatePort is the exposed port accessed through some kind of redirection,
// such as Docker port redirection
AlternatePort uint16
// Command used to invoke the process using the Endpoint.
Command string
// ContainerID is the id of the container exposing the Endpoint.
ContainerID string
// Host is the hostname/ip address of the Endpoint.
Host string
// Transport is the transport protocol used by the Endpoint. (TCP or UDP).
Transport Transport
// Labels is a map of user-specified metadata on the container.
Labels map[string]string
}
func (c *Container) Env() EndpointEnv {
return map[string]any{
"name": c.Name,
"image": c.Image,
"tag": c.Tag,
"port": c.Port,
"alternate_port": c.AlternatePort,
"command": c.Command,
"container_id": c.ContainerID,
"host": c.Host,
"transport": c.Transport,
"labels": c.Labels,
}
}
func (c *Container) Type() EndpointType {
return ContainerType
}
// K8sNode represents a Kubernetes Node object:
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/k8s.md#node
type K8sNode struct {
// Name is the name of the Kubernetes Node.
Name string
// UID is the unique ID for the node
UID string
// Hostname is the node hostname as reported by the status object
Hostname string
// ExternalIP is the node's external IP address as reported by the Status object
ExternalIP string
// InternalIP is the node internal IP address as reported by the Status object
InternalIP string
// ExternalDNS is the node's external DNS record as reported by the Status object
ExternalDNS string
// InternalDNS is the node's internal DNS record as reported by the Status object
InternalDNS string
// Annotations is an arbitrary key-value map of non-identifying, user-specified node metadata
Annotations map[string]string
// Labels is the map of identifying, user-specified node metadata
Labels map[string]string
// KubeletEndpointPort is the node status object's DaemonEndpoints.KubeletEndpoint.Port value
KubeletEndpointPort uint16
}
func (n *K8sNode) Env() EndpointEnv {
return map[string]any{
"name": n.Name,
"uid": n.UID,
"annotations": n.Annotations,
"labels": n.Labels,
"hostname": n.Hostname,
"external_ip": n.ExternalIP,
"internal_ip": n.InternalIP,
"external_dns": n.ExternalDNS,
"internal_dns": n.InternalDNS,
"kubelet_endpoint_port": n.KubeletEndpointPort,
}
}
func (n *K8sNode) Type() EndpointType {
return K8sNodeType
}