-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcreation_log_parser.py
107 lines (90 loc) · 3.76 KB
/
creation_log_parser.py
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
from datetime import datetime, timedelta
import subprocess
import secrets
OTEL_COLLECTOR_URL_HTTP = "http://localhost:4318"
SERVICE_NAME = "codespace-tracking"
activity_list = []
#with open("creation.log", mode="r") as log:
with open("/workspaces/.codespaces/.persistedshare/creation.log", mode="r") as log:
loglines = log.readlines()
for line in loglines:
# All "starting activity" lines end with 3 dots
# Except the final line which is a one-off exception
# =================================================================================
if "..." in line or "Finished configuring codespace." in line:
activity_list.append(line.strip())
log_start_time = activity_list[0][:24]
#print(f"Aside from dumping generic info, log started at: {log_start_time}")
trace_id = secrets.token_hex(16)
main_span_id = secrets.token_hex(8)
trace_start_time = None
trace_end_time = None
position = 0
sub_spans_to_send = []
#print(f"Activity list len: {len(activity_list)}")
for activity in activity_list:
# We need to get two activities to create the span
# Start and end
# Imagine this:
# 2024-06-03 11:24:45.950Z Configuration starting...
# 2024-06-03 11:24:49.377Z Creating container...
#
# Here we need to know that "Configuration starting..." began at 11:24:45.950Z
# Ended at 11:24:49.377Z (so duration was 573ms)
#
activity_start_left, activity_start_right = activity[:24], activity[26:]
activity_start_left_dt = datetime.fromisoformat(activity_start_left)
# Set trace start time
if trace_start_time is None: trace_start_time = activity_start_left_dt
if trace_end_time is None and position == len(activity_list)-1: trace_end_time = activity_start_left_dt + timedelta(seconds=1)
try:
activity_end_left = activity_list[position+1][:24]
activity_end_left_dt = datetime.fromisoformat(activity_end_left)
diff = activity_end_left_dt - activity_start_left_dt
sub_spans_to_send.append({
"trace-id": trace_id,
"span-id": secrets.token_hex(8),
"parent-span-id": main_span_id,
"start-time": activity_start_left_dt,
"end-time": activity_end_left_dt,
"span-name": activity_start_right,
"duration-seconds": diff.total_seconds()
})
except:
main_span_length_seconds = (trace_end_time - trace_start_time).total_seconds()
start_time = str(int(trace_start_time.timestamp()*1000000000))
duration = str(int(main_span_length_seconds*1000))
args = [
"tracepusher",
"--endpoint", OTEL_COLLECTOR_URL_HTTP,
"--insecure", "true",
"--service-name", SERVICE_NAME,
"--span-name", "codespace-creation",
"--start-time", start_time,
"--duration", duration,
"--duration-type", "ms",
"--trace-id", trace_id,
"--span-id", secrets.token_hex(8)
]
output = subprocess.run(args, capture_output=True, text=True)
position += 1
# Now send sub spans
for span in sub_spans_to_send:
start_time = str(int(span['start-time'].timestamp()*1000000000))
span_name = span['span-name']
span_parent_id = span['parent-span-id']
args = [
"tracepusher",
"--endpoint", OTEL_COLLECTOR_URL_HTTP,
"--insecure", "true",
"--service-name", SERVICE_NAME,
"--span-name", span_name,
"--start-time", start_time,
"--duration", str(int(span['duration-seconds']*1000)),
"--duration-type", "ms",
"--trace-id", span['trace-id'],
"--span-id", span['span-id'],
"--parent-span-id", span_parent_id
]
# Send spans to OTEL collector
subprocess.run(args, capture_output=True, text=True)