1 module hunt.trace.Tracer;
2 
3 import hunt.trace.Constrants;
4 import hunt.trace.Endpoint;
5 import hunt.trace.Helpers;
6 import hunt.trace.Span;
7 
8 import std.string;
9 
10 // private static Tracer g_tracer;
11 
12 // Tracer getTracer() {
13 //     return g_tracer;
14 // }
15 
16 // void setTracer(Tracer tracer) {
17 //     g_tracer = tracer;
18 // }
19 
20 __gshared isTraceEnabled = true;
21 
22 
23 // https://github.com/openzipkin/b3-propagation
24 
25 class Tracer {
26     __gshared EndPoint localEndpoint;
27     __gshared bool upload;
28     Span root;
29     Span[] children;
30 
31     this(string spanName, string spanKind = KindOfClient, string b3Header = null) {
32         root = new Span();
33         root.id = ID;
34 
35         if (!b3Header.empty()) {
36             // b3={TraceId}-{SpanId}-{SamplingState}-{ParentSpanId}
37             string[] args = b3Header.split("-");
38             if (args.length >= 3) {
39                 root.traceId = args[0];
40                 root.parentId = args[1];
41                 root.samplingState = args[2];
42             }
43         } else {
44             root.traceId = LID;
45         }
46 
47         root.name = spanName;
48         root.kind = spanKind;
49         root.localEndpoint = localEndpoint;
50     }
51 
52     Span addSpan(string spanName) {
53         auto span = new Span();
54         span.traceId = root.traceId;
55         span.name = spanName;
56         span.id = ID;
57         span.parentId = root.id;
58         span.kind = KindOfClient;
59         span.localEndpoint = localEndpoint;
60         children ~= span;
61         return span;
62     }
63 }