Renecto commited on
Commit
942388a
·
verified ·
1 Parent(s): 3d35409

Update util/trace.py

Browse files
Files changed (1) hide show
  1. util/trace.py +46 -28
util/trace.py CHANGED
@@ -1,37 +1,55 @@
1
  import os
2
  from google.cloud import trace_v2
3
  from google.oauth2 import service_account
 
 
4
  import uuid
5
 
6
- with open(os.getenv("GOOGLE_APPLICATION_CREDENTIALS"), "w") as f:
7
- f.write(os.getenv("gcp_service_json"))
8
- f.close()
 
9
 
10
- def initialize_trace_client():
11
- credentials = service_account.Credentials.from_service_account_file(os.getenv("GOOGLE_APPLICATION_CREDENTIALS"))
12
- client = trace_v2.TraceServiceClient(credentials=credentials)
13
- return client, f"projects/{os.getenv("PROJECT")}"
 
 
14
 
 
 
 
 
 
 
15
 
16
- from google.protobuf.timestamp_pb2 import Timestamp
17
- from datetime import datetime
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- def create_span(client, parent_span_id=None, span_display_name="Default Span"):
20
- trace_id = uuid.uuid4().hex
21
- span_id = uuid.uuid4().hex[:16]
22
- span_name = f"{os.getenv("PROJECT")}/traces/{trace_id}/spans/{span_id}"
23
- now = datetime.utcnow()
24
- timestamp = Timestamp()
25
- timestamp.FromDatetime(now)
26
-
27
- span = trace_v2.Span(
28
- name=span_name,
29
- span_id=span_id,
30
- parent_span_id=parent_span_id,
31
- display_name=trace_v2.types.TruncatableString(value=span_display_name),
32
- start_time=timestamp,
33
- end_time=timestamp
34
- )
35
-
36
- client.batch_write_spans(name=os.getenv("PROJECT"), spans=[span])
37
- return span_name # 返り値としてスパンの名前を返す
 
1
  import os
2
  from google.cloud import trace_v2
3
  from google.oauth2 import service_account
4
+ from google.protobuf.timestamp_pb2 import Timestamp
5
+ from datetime import datetime
6
  import uuid
7
 
8
+ class TraceManager:
9
+ def __init__(self):
10
+ self.client = self._initialize_client()
11
+ self.project_name = f"projects/{os.getenv('PROJECT')}"
12
 
13
+ def _initialize_client(self):
14
+ with open(os.getenv("GOOGLE_APPLICATION_CREDENTIALS"), "w") as f:
15
+ f.write(os.getenv("gcp_service_json"))
16
+ f.close()
17
+ credentials = service_account.Credentials.from_service_account_file(os.getenv("GOOGLE_APPLICATION_CREDENTIALS"))
18
+ return trace_v2.TraceServiceClient(credentials=credentials)
19
 
20
+ def start_span(self, span_display_name="Default Span"):
21
+ span_id = uuid.uuid4().hex[:16]
22
+ trace_id = uuid.uuid4().hex
23
+ span_name = f"{self.project_name}/traces/{trace_id}/spans/{span_id}"
24
+ start_timestamp = self._get_current_timestamp()
25
+ return trace_id, span_id, span_name, start_timestamp
26
 
27
+ def close_span(self, trace_id, span_id, start_timestamp, span_display_name="Default Span", status="OK", error_message=None):
28
+ span_name = f"{self.project_name}/traces/{trace_id}/spans/{span_id}"
29
+ end_timestamp = self._get_current_timestamp()
30
+ attributes = self._create_attributes(status, error_message)
31
+
32
+ span = trace_v2.Span(
33
+ name=span_name,
34
+ span_id=span_id,
35
+ display_name=trace_v2.types.TruncatableString(value=span_display_name),
36
+ start_time=start_timestamp,
37
+ end_time=end_timestamp,
38
+ attributes=attributes
39
+ )
40
+ self.client.batch_write_spans(name=self.project_name, spans=[span])
41
+ return span_name
42
+
43
+ def _get_current_timestamp(self):
44
+ now = datetime.utcnow()
45
+ timestamp = Timestamp()
46
+ timestamp.FromDatetime(now)
47
+ return timestamp
48
 
49
+ def _create_attributes(self, status, error_message):
50
+ attribute_map = {
51
+ "status": trace_v2.types.AttributeValue(string_value=trace_v2.types.TruncatableString(value=status))
52
+ }
53
+ if error_message:
54
+ attribute_map["error.message"] = trace_v2.types.AttributeValue(string_value=trace_v2.types.TruncatableString(value=error_message))
55
+ return trace_v2.types.Attributes(attribute_map=attribute_map)