1 module hunt.trace.Span;
2 
3 import hunt.trace.Endpoint;
4 import hunt.trace.Annotation;
5 import hunt.trace.Utils;
6 import std.json;
7 import hunt.util.Serialize;
8 
9 
10 
11 
12 
13 
14 class Span
15 {
16     /// 16bytes
17     string traceId;
18     string name;
19     @IGNORE
20     string parentId;
21     /// 8bytes
22     string id;
23     string kind;
24     long timestamp;
25     long duration;
26     @IGNORE
27     bool debug_;
28     @IGNORE
29     bool shared_;
30 
31     EndPoint localEndpoint;
32     EndPoint remoteEndpoint;
33     Annotation[] annotations;
34     string[string]  tags;
35 
36     string defaultId() {
37         return traceId ~ "-" ~ parentId ~ "-" ~ "1" ~ "-" ~ id;
38     }
39 
40     void addTag(string key , string value)
41     {
42         tags[key] = value;
43     }
44 
45     void addAnnotation(string value , long timestamp = 0)
46     {
47         auto anno = new Annotation();
48         anno.value = value;
49         if(timestamp == 0)
50             timestamp = usecs;
51         anno.timestamp = timestamp ;
52         annotations ~= anno;
53     }
54 
55     void start(long timestamp = 0)
56     {
57         if(timestamp != 0)
58             this.timestamp = timestamp;
59         else
60             this.timestamp = usecs;
61     }
62 
63     void finish(long timestamp = 0)
64     {
65         if(timestamp != 0)
66             this.duration = timestamp - this.timestamp;
67         else
68             this.duration = usecs - this.timestamp;
69 
70     }
71 
72     override string toString()
73     {
74         auto json = toJson(this);       
75         json["debug"] = (debug_);
76         json["shared"] = (shared_);
77         if(parentId.length != 0)
78             json["parentId"] = parentId;
79         return json.toString;
80     }
81 }