예제 #1
0
        /// <summary>
        /// 设置调用链 TraceId
        /// </summary>
        /// <param name="input"></param>
        /// <param name="micro">目标服务</param>
        public static sys_trace SetTraceId(Dictionary <string, string> input, Micro micro = null)
        {
            if (!SettingService.TraceOnOff)
            {
                return(null);
            }
            #region 分布式追踪不记录自己,防止调用链循环调用 +TraceRecord

            if (input[Const.Enum.Eng.NAMESPACE] == Namespace && input[Const.Enum.Eng.CLASS] == Class)
            {
                return(null);
            }
            if (input[Const.Enum.Eng.NAMESPACE] == "Anno.Plugs.Monitor" && input[Const.Enum.Eng.CLASS] == "Resource")
            {
                return(null);
            }
            #endregion
            #region 调用链唯一标识+ TraceId
            string TraceId       = "TraceId";       //调用链唯一标识
            string PreTraceId    = "PreTraceId";    //上一级调用链唯一标识
            string AppName       = "AppName";       //调用方AnnoService 名称
            string AppNameTarget = "AppNameTarget"; //目标AnnoService 名称
            string TTL           = "TTL";           //分布式调用计数器 默认0
            string GlobalTraceId = "GlobalTraceId";
            if (input.ContainsKey(TraceId))
            {
                input[PreTraceId] = input[TraceId];            //当前TraceId 变为上一级 TraceId(PreTraceId)
                input[TraceId]    = Guid.NewGuid().ToString(); //生成新的调用链唯一标识
            }
            else
            {
                input.Add(TraceId, Guid.NewGuid().ToString());
                input.Add(PreTraceId, string.Empty);
            }
            //追踪全局标识
            if (!input.ContainsKey(GlobalTraceId))
            {
                input.Add(GlobalTraceId, Guid.NewGuid().ToString());
            }
            #endregion

            #region App名称+AppName
            if (input.ContainsKey(AppName))
            {
                input[AppName] = SettingService.AppName ?? string.Empty;
            }
            else
            {
                input.Add(AppName, SettingService.AppName ?? string.Empty);
            }
            #endregion
            #region 目标APP地址
            if (micro != null)
            {
                string target = "Target";
                if (input.ContainsKey(target))
                {
                    input[target] = micro.Ip + ":" + micro.Port;
                }
                else
                {
                    input.Add(target, micro.Ip + ":" + micro.Port);
                }

                if (input.ContainsKey(AppNameTarget))
                {
                    input[AppNameTarget] = micro.Nickname ?? string.Empty;
                }
                else
                {
                    input.Add(AppNameTarget, micro.Nickname ?? string.Empty);
                }
            }
            #endregion
            #region App+跳转次数
            if (input.ContainsKey(TTL))
            {
                int.TryParse(input[TTL], out int ttl);
                input[TTL] = (ttl + 1).ToString();
            }
            else
            {
                input.Add(TTL, "0");
            }
            #endregion

            return(TracePool.CreateTrance(input));
            //TracePool.EnQueue(input);
        }