This repository was archived by the owner on Sep 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy path__init__.py
More file actions
64 lines (50 loc) · 1.76 KB
/
__init__.py
File metadata and controls
64 lines (50 loc) · 1.76 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
import datetime
import os
import socket
from google.protobuf.timestamp_pb2 import Timestamp
from opencensus.common.version import __version__ as opencensus_version
from opencensus.proto.agent.common.v1 import common_pb2
# Default agent endpoint
DEFAULT_ENDPOINT = 'localhost:55678'
# OCAgent exporter version
EXPORTER_VERSION = '0.0.1'
def get_node(service_name, host_name):
"""Generates Node message from params and system information.
"""
return common_pb2.Node(
identifier=common_pb2.ProcessIdentifier(
host_name=socket.gethostname() if host_name is None
else host_name,
pid=os.getpid(),
start_timestamp=proto_ts_from_datetime(
datetime.datetime.now(datetime.UTC))),
library_info=common_pb2.LibraryInfo(
language=common_pb2.LibraryInfo.Language.Value('PYTHON'),
exporter_version=EXPORTER_VERSION,
core_library_version=opencensus_version),
service_info=common_pb2.ServiceInfo(name=service_name))
def proto_ts_from_datetime(dt):
"""Converts datetime to protobuf timestamp.
:type dt: datetime
:param dt: date and time
:rtype: :class:`~google.protobuf.timestamp_pb2.Timestamp`
:returns: protobuf timestamp
"""
ts = Timestamp()
if (dt is not None):
ts.FromDatetime(dt)
return ts
def proto_ts_from_datetime_str(dt):
"""Converts string datetime in ISO format to protobuf timestamp.
:type dt: str
:param dt: string with datetime in ISO format
:rtype: :class:`~google.protobuf.timestamp_pb2.Timestamp`
:returns: protobuf timestamp
"""
ts = Timestamp()
if (dt is not None):
try:
ts.FromJsonString(dt)
except ValueError:
pass
return ts