forked from StephenGoodall/OTLP-GitHubAction-Exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
111 lines (93 loc) · 2.95 KB
/
__init__.py
File metadata and controls
111 lines (93 loc) · 2.95 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
import time
from pyrfc3339 import parse
import os
import json
from fastcore.xtras import obj2dict
def do_fastcore_decode(obj):
newobj = obj2dict(obj)
return json.dumps(newobj)
def do_time(string):
return (int(round(time.mktime(parse(string).timetuple())) * 1000000000))
def do_time_ms(string):
return (int(round(time.mktime(parse(string).timetuple())) * 1000))
def do_string(string):
return str(string).lower().replace(" ", "")
def do_parse(string):
return string != "" and string is not None and string != "None"
def check_env_vars():
keys = ("ACTION_TOKEN","OTEL_EXPORTER_OTLP_ENDPOINT","WORKFLOW_RUN_ID","WORKFLOW_RUN_NAME")
keys_not_set = []
for key in keys:
if key not in os.environ:
keys_not_set.append(key)
else:
pass
if len(keys_not_set) > 0:
for key in keys_not_set:
print(key + " not set")
exit(1)
def _retrieve_env(var_name, default):
raw = os.getenv(var_name)
if raw is None:
return set(default)
return {part.strip() for part in raw.lower().split(",") if part.strip()}
# Current needed attributes for workflow span are injected in the exported
WORKFLOW_ALLOWED = _retrieve_env("GITHUB_ATTRS_ALLOW_WORKFLOW", set())
JOB_ALLOWED = _retrieve_env("GITHUB_ATTRS_ALLOW_JOB", {
"status",
"conclusion",
"started_at",
"completed_at",
"created_at",
"head_branch",
"head_sha",
"run_attempt",
"runner_group_id",
"runner_group_name",
"runner_id",
"runner_name",
})
STEP_ALLOWED = _retrieve_env("GITHUB_ATTRS_ALLOW_STEP", {
"status",
"conclusion",
"number",
"started_at",
"completed_at",
})
ALLOWED_BY_TYPE = {
"workflow": WORKFLOW_ALLOWED,
"job": JOB_ALLOWED,
"step": STEP_ALLOWED,
}
# Flatten nested dicts/lists into dotted keys.
# For lists of dicts, fields collapse onto `prefix.att`
def _flatten_object(obj, prefix=""):
flat = {}
if isinstance(obj, dict):
for k, v in obj.items():
key = do_string(k)
new_prefix = f"{prefix}.{key}" if prefix else key
if isinstance(v, (dict, list)):
flat.update(_flatten_object(v, new_prefix))
else:
flat[new_prefix] = v
elif isinstance(obj, list):
for item in obj:
if isinstance(item, dict):
flat.update(_flatten_object(item, prefix))
elif prefix:
flat[prefix] = item
return flat
def parse_attributes(obj, otype):
allowed = ALLOWED_BY_TYPE.get(str(otype).lower(), set())
conclusion = obj.get("conclusion") if isinstance(obj, dict) else None
out = {}
for name, value in _flatten_object(obj).items():
if name not in allowed:
continue
if not do_parse(value):
continue
out[name] = str(value)
if name.endswith("_at") and conclusion not in ("skipped", "cancelled"):
out[name + "_ms"] = do_time_ms(value)
return out