Ejemplo n.º 1
0
        /// <summary>
        /// 批量发送调用链到 追踪服务器
        /// </summary>
        internal static void TryDequeue()
        {
            if (TraceQueue.IsEmpty)
            {
                return;
            }

            List <sys_trace> traces = new List <sys_trace>();

ReTryDequeue:
            while (!TraceQueue.IsEmpty && traces.Count < 100)
            {
                TraceQueue.TryDequeue(out sys_trace trace);
                trace.Ip            = GetValueByKey(trace.InputDictionary, "X-Original-For");
                trace.TraceId       = GetValueByKey(trace.InputDictionary, "TraceId");
                trace.PreTraceId    = GetValueByKey(trace.InputDictionary, "PreTraceId");
                trace.AppName       = GetValueByKey(trace.InputDictionary, "AppName");
                trace.AppNameTarget = GetValueByKey(trace.InputDictionary, "AppNameTarget");
                trace.TTL           = RequestInt32(trace.InputDictionary, "TTL");
                trace.Target        = GetValueByKey(trace.InputDictionary, "Target");
                trace.Askchannel    = GetValueByKey(trace.InputDictionary, "channel");
                trace.Askrouter     = GetValueByKey(trace.InputDictionary, "router");
                trace.Askmethod     = GetValueByKey(trace.InputDictionary, "method");
                trace.GlobalTraceId = GetValueByKey(trace.InputDictionary, "GlobalTraceId");
                trace.Uname         = GetValueByKey(trace.InputDictionary, "uname");
                trace.Rlt           = trace.Response?.IndexOf("tatus\":true") > 0;

                /**
                 * 成功的请求清空链路追踪响应值
                 */
                if (trace.Rlt)
                {
                    trace.Response = null;
                }

                /**
                 * 请求内容默认只记录3000字符
                 */
                Dictionary <string, string> requestBody = new Dictionary <string, string>();
                var keys = trace.InputDictionary.Keys;
                foreach (var key in keys)
                {
                    if (key.Equals("channel") ||
                        key.Equals("router") ||
                        key.Equals("method") ||
                        key.Equals("X-Original-For") ||
                        key.Equals("TraceId") ||
                        key.Equals("PreTraceId") ||
                        key.Equals("AppName") ||
                        key.Equals("AppNameTarget") ||
                        key.Equals("TTL") ||
                        key.Equals("Target") ||
                        key.Equals("GlobalTraceId") ||
                        key.Equals("uname")
                        )
                    {
                        continue;
                    }
                    if (trace.InputDictionary.TryGetValue(key, out string value))
                    {
                        if (key.EndsWith("Secret") ||
                            key.EndsWith("pwd"))
                        {
                            value = "******";
                        }
                        if (!string.IsNullOrEmpty(value) && value.Length > TransmitTrace.CallChainCharLength)
                        {
                            value = value.Substring(0, TransmitTrace.CallChainCharLength);
                        }
                        requestBody.Add(key, value);
                    }
                }
                trace.Request = Newtonsoft.Json.JsonConvert.SerializeObject(requestBody);

                traces.Add(trace);
            }
            if (traces.Count <= 0)
            {
                return;
            }
            Dictionary <string, string> inputTrace = new Dictionary <string, string>
            {
                { Const.Enum.Eng.NAMESPACE, "Anno.Plugs.Trace" },
                { Const.Enum.Eng.CLASS, "Trace" },
                { Const.Enum.Eng.METHOD, "TraceBatch" },
                { "traces", Newtonsoft.Json.JsonConvert.SerializeObject(traces) }
            };

            Connector.BrokerDns(inputTrace);
            if (!TraceQueue.IsEmpty)
            {
                traces.Clear();
                goto ReTryDequeue;
            }
        }