-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetector.py
More file actions
366 lines (299 loc) · 11.8 KB
/
detector.py
File metadata and controls
366 lines (299 loc) · 11.8 KB
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
363
364
365
366
# SPDX-FileCopyrightText: 2026 The Botanu Authors
# SPDX-License-Identifier: Apache-2.0
"""Resource Detector — auto-detect execution environment for cost attribution.
Detects attributes from:
- Kubernetes (``k8s.*``)
- Cloud providers (``cloud.*``, ``aws.*``, ``gcp.*``, ``azure.*``)
- Host / VM (``host.*``, ``os.*``)
- Container (``container.*``)
- Serverless / FaaS (``faas.*``)
- Process (``process.*``)
"""
from __future__ import annotations
import os
import platform
import socket
import sys
from functools import lru_cache
from typing import Any, Dict, Optional
# =========================================================================
# Environment Variable Mappings
# =========================================================================
K8S_ENV_MAPPINGS: Dict[str, Optional[str]] = {
"KUBERNETES_SERVICE_HOST": None,
"HOSTNAME": "k8s.pod.name",
"K8S_POD_NAME": "k8s.pod.name",
"K8S_POD_UID": "k8s.pod.uid",
"K8S_NAMESPACE": "k8s.namespace.name",
"K8S_NODE_NAME": "k8s.node.name",
"K8S_CLUSTER_NAME": "k8s.cluster.name",
"K8S_DEPLOYMENT_NAME": "k8s.deployment.name",
"K8S_STATEFULSET_NAME": "k8s.statefulset.name",
"K8S_CONTAINER_NAME": "k8s.container.name",
}
AWS_ENV_MAPPINGS: Dict[str, Optional[str]] = {
"AWS_REGION": "cloud.region",
"AWS_DEFAULT_REGION": "cloud.region",
"AWS_ACCOUNT_ID": "cloud.account.id",
"ECS_CONTAINER_METADATA_URI": None,
"ECS_CLUSTER": "aws.ecs.cluster.name",
"ECS_TASK_ARN": "aws.ecs.task.arn",
"ECS_TASK_DEFINITION_FAMILY": "aws.ecs.task.family",
"AWS_LAMBDA_FUNCTION_NAME": "faas.name",
"AWS_LAMBDA_FUNCTION_VERSION": "faas.version",
"AWS_LAMBDA_LOG_GROUP_NAME": "aws.lambda.log_group",
"AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "faas.max_memory",
}
GCP_ENV_MAPPINGS: Dict[str, Optional[str]] = {
"GOOGLE_CLOUD_PROJECT": "cloud.account.id",
"GCLOUD_PROJECT": "cloud.account.id",
"GCP_PROJECT": "cloud.account.id",
"GOOGLE_CLOUD_REGION": "cloud.region",
"K_SERVICE": "faas.name",
"K_REVISION": "faas.version",
"K_CONFIGURATION": "gcp.cloud_run.configuration",
"FUNCTION_NAME": "faas.name",
"FUNCTION_TARGET": "faas.trigger",
"FUNCTION_SIGNATURE_TYPE": "gcp.function.signature_type",
}
AZURE_ENV_MAPPINGS: Dict[str, Optional[str]] = {
"AZURE_SUBSCRIPTION_ID": "cloud.account.id",
"AZURE_RESOURCE_GROUP": "azure.resource_group",
"WEBSITE_SITE_NAME": "faas.name",
"FUNCTIONS_EXTENSION_VERSION": "azure.functions.version",
"WEBSITE_INSTANCE_ID": "faas.instance",
"REGION_NAME": "cloud.region",
}
# =========================================================================
# Detection Functions
# =========================================================================
def detect_kubernetes() -> Dict[str, Any]:
attrs: Dict[str, Any] = {}
if not os.environ.get("KUBERNETES_SERVICE_HOST"):
return attrs
for env_var, attr_name in K8S_ENV_MAPPINGS.items():
value = os.environ.get(env_var)
if attr_name and value:
attrs[attr_name] = value
if "k8s.pod.name" not in attrs:
hostname = os.environ.get("HOSTNAME", socket.gethostname())
if hostname:
attrs["k8s.pod.name"] = hostname
namespace_file = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
if "k8s.namespace.name" not in attrs and os.path.exists(namespace_file):
try:
with open(namespace_file) as fh:
attrs["k8s.namespace.name"] = fh.read().strip()
except OSError:
pass
return attrs
def detect_cloud_provider() -> Dict[str, Any]:
attrs: Dict[str, Any] = {}
if _is_aws():
attrs["cloud.provider"] = "aws"
for env_var, attr_name in AWS_ENV_MAPPINGS.items():
value = os.environ.get(env_var)
if attr_name and value:
attrs[attr_name] = value
if os.environ.get("AWS_LAMBDA_FUNCTION_NAME"):
attrs["faas.id"] = (
f"arn:aws:lambda:{attrs.get('cloud.region', 'unknown')}:"
f"{attrs.get('cloud.account.id', 'unknown')}:"
f"function:{os.environ['AWS_LAMBDA_FUNCTION_NAME']}"
)
az = _get_aws_availability_zone()
if az:
attrs["cloud.availability_zone"] = az
if "cloud.region" not in attrs:
attrs["cloud.region"] = az[:-1]
elif _is_gcp():
attrs["cloud.provider"] = "gcp"
for env_var, attr_name in GCP_ENV_MAPPINGS.items():
value = os.environ.get(env_var)
if attr_name and value:
attrs[attr_name] = value
if os.environ.get("K_SERVICE"):
attrs["faas.trigger"] = "http"
elif os.environ.get("FUNCTION_NAME"):
attrs["faas.trigger"] = os.environ.get("FUNCTION_TRIGGER_TYPE", "unknown")
elif _is_azure():
attrs["cloud.provider"] = "azure"
for env_var, attr_name in AZURE_ENV_MAPPINGS.items():
value = os.environ.get(env_var)
if attr_name and value:
attrs[attr_name] = value
return attrs
def _is_aws() -> bool:
indicators = [
"AWS_REGION",
"AWS_DEFAULT_REGION",
"AWS_LAMBDA_FUNCTION_NAME",
"ECS_CONTAINER_METADATA_URI",
"AWS_EXECUTION_ENV",
]
return any(os.environ.get(var) for var in indicators)
def _is_gcp() -> bool:
indicators = [
"GOOGLE_CLOUD_PROJECT",
"GCLOUD_PROJECT",
"GCP_PROJECT",
"K_SERVICE",
"FUNCTION_NAME",
]
return any(os.environ.get(var) for var in indicators)
def _is_azure() -> bool:
indicators = [
"WEBSITE_SITE_NAME",
"AZURE_FUNCTIONS_ENVIRONMENT",
"AZURE_SUBSCRIPTION_ID",
]
return any(os.environ.get(var) for var in indicators)
def _get_aws_availability_zone() -> Optional[str]:
"""Get AWS availability zone from EC2 instance metadata.
Uses IMDS (Instance Metadata Service) which is only accessible from within EC2.
Configure via environment variables:
- AWS_EC2_METADATA_SERVICE_ENDPOINT: Override the metadata endpoint
- AWS_EC2_METADATA_DISABLED: Set to 'true' to disable metadata calls
"""
if os.environ.get("AWS_LAMBDA_FUNCTION_NAME"):
return None
# Respect AWS SDK standard env vars for disabling/configuring metadata
if os.environ.get("AWS_EC2_METADATA_DISABLED", "").lower() == "true":
return None
# Use AWS SDK standard endpoint override, or default to standard IMDS address
endpoint = os.environ.get("AWS_EC2_METADATA_SERVICE_ENDPOINT", "http://169.254.169.254")
if not endpoint or not endpoint.startswith(("http://", "https://")):
return None
try:
import urllib.request
url = f"{endpoint}/latest/meta-data/placement/availability-zone"
req = urllib.request.Request(url, headers={"Accept": "text/plain"}) # noqa: S310
with urllib.request.urlopen(req, timeout=0.5) as resp: # noqa: S310
return resp.read().decode("utf-8").strip()
except Exception:
return None
def detect_host() -> Dict[str, Any]:
attrs: Dict[str, Any] = {}
try:
hostname = socket.gethostname()
if hostname:
attrs["host.name"] = hostname
except Exception:
pass
host_id = os.environ.get("HOST_ID") or os.environ.get("INSTANCE_ID")
if host_id:
attrs["host.id"] = host_id
elif "host.name" in attrs:
attrs["host.id"] = attrs["host.name"]
attrs["os.type"] = sys.platform
attrs["host.arch"] = platform.machine()
return attrs
def detect_container() -> Dict[str, Any]:
attrs: Dict[str, Any] = {}
container_id = _get_container_id()
if container_id:
attrs["container.id"] = container_id
if os.path.exists("/.dockerenv"):
attrs["container.runtime"] = "docker"
elif os.environ.get("KUBERNETES_SERVICE_HOST"):
attrs["container.runtime"] = "containerd"
return attrs
def _get_container_id() -> Optional[str]:
container_id = os.environ.get("CONTAINER_ID") or os.environ.get("HOSTNAME")
cgroup_path = "/proc/self/cgroup"
if os.path.exists(cgroup_path):
try:
with open(cgroup_path) as fh:
for line in fh:
if "docker" in line or "kubepods" in line:
parts = line.strip().split("/")
if parts:
last = parts[-1]
if last.startswith("cri-containerd-"):
last = last[15:]
if len(last) >= 12:
return last[:64]
except OSError:
pass
return container_id if container_id and len(container_id) >= 12 else None
def detect_process() -> Dict[str, Any]:
attrs: Dict[str, Any] = {}
attrs["process.pid"] = os.getpid()
attrs["process.runtime.name"] = "python"
attrs["process.runtime.version"] = sys.version.split()[0]
if sys.argv:
attrs["process.command"] = sys.argv[0][:200]
return attrs
def detect_serverless() -> Dict[str, Any]:
attrs: Dict[str, Any] = {}
if os.environ.get("AWS_LAMBDA_FUNCTION_NAME"):
attrs["faas.name"] = os.environ["AWS_LAMBDA_FUNCTION_NAME"]
version = os.environ.get("AWS_LAMBDA_FUNCTION_VERSION")
if version:
attrs["faas.version"] = version
memory = os.environ.get("AWS_LAMBDA_FUNCTION_MEMORY_SIZE")
if memory:
attrs["faas.max_memory"] = int(memory) * 1024 * 1024
elif os.environ.get("K_SERVICE"):
attrs["faas.name"] = os.environ["K_SERVICE"]
revision = os.environ.get("K_REVISION")
if revision:
attrs["faas.version"] = revision
elif os.environ.get("FUNCTION_NAME"):
attrs["faas.name"] = os.environ["FUNCTION_NAME"]
target = os.environ.get("FUNCTION_TARGET")
if target:
attrs["faas.trigger"] = target
elif os.environ.get("WEBSITE_SITE_NAME"):
attrs["faas.name"] = os.environ["WEBSITE_SITE_NAME"]
instance = os.environ.get("WEBSITE_INSTANCE_ID")
if instance:
attrs["faas.instance"] = instance
return attrs
# =========================================================================
# Main Detection
# =========================================================================
@lru_cache(maxsize=1)
def detect_all_resources() -> Dict[str, Any]:
"""Detect all environment resource attributes.
Results are cached (environment doesn't change during runtime).
"""
attrs: Dict[str, Any] = {}
attrs.update(detect_host())
attrs.update(detect_process())
attrs.update(detect_container())
attrs.update(detect_cloud_provider())
attrs.update(detect_kubernetes())
attrs.update(detect_serverless())
if "service.instance.id" not in attrs:
container_id = attrs.get("container.id")
if container_id:
attrs["service.instance.id"] = container_id[:12]
elif pod_name := attrs.get("k8s.pod.name"):
attrs["service.instance.id"] = pod_name
elif host_id := attrs.get("host.id"):
attrs["service.instance.id"] = host_id
return attrs
def get_resource_attributes(
include_host: bool = True,
include_process: bool = True,
include_container: bool = True,
include_cloud: bool = True,
include_k8s: bool = True,
include_faas: bool = True,
) -> Dict[str, Any]:
"""Get resource attributes with selective detection."""
attrs: Dict[str, Any] = {}
if include_host:
attrs.update(detect_host())
if include_process:
attrs.update(detect_process())
if include_container:
attrs.update(detect_container())
if include_cloud:
attrs.update(detect_cloud_provider())
if include_k8s:
attrs.update(detect_kubernetes())
if include_faas:
attrs.update(detect_serverless())
return attrs