1 module hunt.trace.Client;
2 
3 import hunt.trace.Endpoint;
4 import hunt.trace.Annotation;
5 import hunt.trace.Span;
6 import hunt.trace.Constrants;
7 import zipkin.proto3.zipkin;
8 import hunt.event.timer;
9 import hunt.util.Timer;
10 import hunt.net;
11 import hunt.imf.clients.GatewayTcpClient;
12 import hunt.imf.protocol.protobuf.ProtobufProtocol;
13 
14 alias PSpan = zipkin.proto3.zipkin.Span;
15 alias CSpan = hunt.trace.Span.Span;
16 
17 __gshared GatewayTcpClient g_context = null;
18 __gshared Timer g_timer = null;
19 
20 import hunt.logging;
21 import hunt.util.Serialize;
22 import core.time;
23 
24 @property bool tracing()
25 {
26     return g_context !is null;
27 }
28 
29 
30 void initIMF(string host, ushort port)
31 {
32     ProtobufProtocol tcp = new ProtobufProtocol(host,port);
33     g_context = new GatewayTcpClient(tcp);
34     g_context.connect();
35 }
36 
37 
38 void uploadFromIMF(CSpan[] spans ...)
39 {
40     ListOfSpans pspans = new ListOfSpans();
41     foreach(s ; spans)
42     {
43         pspans.spans ~= toPSpan(s);
44     }
45     
46     if(g_context !is null)   
47     {
48         g_context.sendMsg(0 , pspans);
49     }
50 }
51 
52 PSpan toPSpan(CSpan cspan)
53 {
54     auto pspan = new PSpan();
55     
56     pspan.traceId = cast(ubyte[])cspan.traceId;
57     pspan.name = cspan.name;
58     pspan.parentId = cast(ubyte[])cspan.parentId;
59     pspan.id = cast(ubyte[])cspan.id;
60     switch(cspan.kind)
61     {
62         case KindOfServer:
63             pspan.kind = PSpan.Kind.SERVER;
64             break;
65         case KindOfClient:
66             pspan.kind = PSpan.Kind.CLIENT;
67             break;
68         case KindOfPRODUCER:
69             pspan.kind = PSpan.Kind.PRODUCER;
70             break;
71         case KindOfCONSUMER:
72             pspan.kind = PSpan.Kind.CONSUMER;
73             break;
74         default:
75             pspan.kind = PSpan.Kind.SPAN_KIND_UNSPECIFIED;
76     }
77 
78     pspan.debug_ = cspan.debug_;
79     pspan.timestamp = cspan.timestamp;
80     pspan.duration = cspan.duration;
81     pspan.debug_ = cspan.debug_;
82     pspan.shared_ = cspan.shared_;
83     
84     pspan.localEndpoint =  toObject!(zipkin.proto3.zipkin.Endpoint)(toJson(cspan.localEndpoint));
85     pspan.remoteEndpoint = toObject!(zipkin.proto3.zipkin.Endpoint)(toJson(cspan.remoteEndpoint));
86     pspan.annotations = toObject!(zipkin.proto3.zipkin.Annotation[])(toJson(cspan.annotations));
87     pspan.tags = cspan.tags;
88 
89     return pspan;
90 }
91 
92 CSpan toCSpan(PSpan pspan)
93 {
94     auto cspan = new CSpan();
95     
96     cspan.traceId = cast(string)pspan.traceId;
97     cspan.name = pspan.name;
98     cspan.parentId = cast(string)pspan.parentId;
99     cspan.id = cast(string)pspan.id;
100     switch(pspan.kind)
101     {
102         case PSpan.Kind.SERVER:
103             cspan.kind = KindOfServer;
104             break;
105         case PSpan.Kind.CLIENT:
106             cspan.kind = KindOfClient;
107             break;
108         case PSpan.Kind.PRODUCER:
109             cspan.kind = KindOfPRODUCER;
110             break;
111         case PSpan.Kind.CONSUMER:
112             cspan.kind = KindOfCONSUMER;
113             break;
114         default:
115             cspan.kind = "SPAN_KIND_UNSPECIFIED";
116     }
117 
118     cspan.debug_ = pspan.debug_;
119     cspan.timestamp = pspan.timestamp;
120     cspan.duration = pspan.duration;
121     cspan.debug_ = pspan.debug_;
122     cspan.shared_ = pspan.shared_;
123     
124     cspan.localEndpoint =  toObject!(hunt.trace.Endpoint.EndPoint)(toJson(pspan.localEndpoint));
125     cspan.remoteEndpoint = toObject!(hunt.trace.Endpoint.EndPoint)(toJson(pspan.remoteEndpoint));
126     cspan.annotations = toObject!(hunt.trace.Annotation.Annotation[])(toJson(pspan.annotations));
127     cspan.tags = pspan.tags;
128 
129     return cspan;
130 }