File: traceid.py

package info (click to toggle)
python-aws-xray-sdk 0.95-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 792 kB
  • sloc: python: 3,006; makefile: 20
file content (28 lines) | stat: -rw-r--r-- 774 bytes parent folder | download | duplicates (4)
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
import os
import time
import binascii


class TraceId:
    """
    A trace ID tracks the path of a request through your application.
    A trace collects all the segments generated by a single request.
    A trace ID is required for a segment.
    """
    VERSION = '1'
    DELIMITER = '-'

    def __init__(self):
        """
        Generate a random trace id.
        """
        self.start_time = int(time.time())
        self.__number = binascii.b2a_hex(os.urandom(12)).decode('utf-8')

    def to_id(self):
        """
        Convert TraceId object to a string.
        """
        return "%s%s%s%s%s" % (TraceId.VERSION, TraceId.DELIMITER,
                               format(self.start_time, 'x'),
                               TraceId.DELIMITER, self.__number)