Пример #1
0
        /// <summary>处理Span集合。默认输出日志,可重定义输出控制台</summary>
        protected override void ProcessSpans(ISpanBuilder[] builders)
        {
            if (builders == null)
            {
                return;
            }

            // 剔除项
            if (Excludes != null)
            {
                builders = builders.Where(e => !Excludes.Any(y => y.IsMatch(e.Name))).ToArray();
            }
            builders = builders.Where(e => !e.Name.EndsWithIgnoreCase("/Trace/Report")).ToArray();
            if (builders.Length == 0)
            {
                return;
            }

            // 初始化
            Init();

            // 构建应用信息
            var info = new AppInfo(_process);

            try
            {
                // 调用WindowApi获取进程的连接数
                var tcps = NetHelper.GetAllTcpConnections();
                if (tcps != null && tcps.Length > 0)
                {
                    var pid = Process.GetCurrentProcess().Id;
                    info.Connections = tcps.Count(e => e.ProcessId == pid);
                }
            }
            catch { }

            // 发送,失败后进入队列
            var model = new TraceModel
            {
                AppId    = AppId,
                AppName  = AppName,
                ClientId = ClientId,
                Version  = _version,
                Info     = info,

                Builders = builders
            };

            try
            {
                // 检查令牌
                if (!AppSecret.IsNullOrEmpty())
                {
                    CheckAuthorize();
                }

                var rs = Client.Invoke <TraceResponse>("Trace/Report", model);
                // 处理响应参数
                if (rs != null)
                {
                    if (rs.Period > 0)
                    {
                        Period = rs.Period;
                    }
                    if (rs.MaxSamples > 0)
                    {
                        MaxSamples = rs.MaxSamples;
                    }
                    if (rs.MaxErrors > 0)
                    {
                        MaxErrors = rs.MaxErrors;
                    }
                    if (rs.Timeout > 0)
                    {
                        Timeout = rs.Timeout;
                    }
                    if (rs.Excludes != null)
                    {
                        Excludes = rs.Excludes;
                    }
                }
            }
            catch (ApiException ex)
            {
                Log?.Error(ex + "");
            }
            catch (Exception ex)
            {
                //XTrace.WriteException(ex);
                Log?.Error(ex + "");
                //throw;

                if (_fails.Count < MaxFails)
                {
                    _fails.Enqueue(model);
                }
                return;
            }

            // 如果发送成功,则继续发送以前失败的数据
            while (_fails.Count > 0)
            {
                model = _fails.Dequeue();
                try
                {
                    Client.Invoke <Object>("Trace/Report", model);
                }
                catch (ApiException ex)
                {
                    Log?.Error(ex + "");
                }
                catch (Exception ex)
                {
                    XTrace.WriteLine("二次上报失败,放弃该批次采样数据,{0}", model.Builders.FirstOrDefault()?.StartTime.ToDateTime());
                    XTrace.WriteException(ex);
                    //Log?.Error(ex + "");

                    // 星尘收集器上报,二次失败后放弃该批次数据,因其很可能是错误数据
                    //_fails.Enqueue(model);
                    break;
                }
            }
        }
Пример #2
0
        /// <summary>处理Span集合。默认输出日志,可重定义输出控制台</summary>
        protected override void ProcessSpans(ISpanBuilder[] builders)
        {
            if (builders == null)
            {
                return;
            }

            // 剔除项
            if (Excludes != null)
            {
                builders = builders.Where(e => !Excludes.Any(y => y.IsMatch(e.Name))).ToArray();
            }
            builders = builders.Where(e => !e.Name.EndsWithIgnoreCase("/Trace/Report")).ToArray();
            if (builders.Length == 0)
            {
                return;
            }

            // 初始化
            Init();

            // 构建应用信息
            if (_appInfo == null)
            {
                _appInfo = new AppInfo(_process)
                {
                    Version = _version
                }
            }
            ;
            else
            {
                _appInfo.Refresh();
            }

            // 发送,失败后进入队列
            var model = new TraceModel
            {
                AppId    = AppId,
                AppName  = AppName,
                ClientId = ClientId,
                Version  = _version,
                Info     = _appInfo,

                Builders = builders
            };

            try
            {
                //// 检查令牌
                //if (!Secret.IsNullOrEmpty()) CheckAuthorize();

                var rs = Client.Invoke <TraceResponse>("Trace/Report", model);
                // 处理响应参数
                if (rs != null)
                {
                    if (rs.Period > 0)
                    {
                        Period = rs.Period;
                    }
                    if (rs.MaxSamples > 0)
                    {
                        MaxSamples = rs.MaxSamples;
                    }
                    if (rs.MaxErrors > 0)
                    {
                        MaxErrors = rs.MaxErrors;
                    }
                    if (rs.Timeout > 0)
                    {
                        Timeout = rs.Timeout;
                    }
                    Excludes = rs.Excludes;
                }
            }
            catch (ApiException ex)
            {
                //if (ex.Code == 401 || ex.Code == 403) _token = null;

                Log?.Error(ex + "");
            }
            catch (Exception ex)
            {
                //if (ex is ApiException ae && (ae.Code == 401 || ae.Code == 403)) _token = null;

                Log?.Error(ex + "");

                if (_fails.Count < MaxFails)
                {
                    _fails.Enqueue(model);
                }
                return;
            }

            // 如果发送成功,则继续发送以前失败的数据
            while (_fails.Count > 0)
            {
                model = _fails.Dequeue();
                try
                {
                    Client.Invoke <Object>("Trace/Report", model);
                }
                catch (ApiException ex)
                {
                    Log?.Error(ex + "");
                }
                catch (Exception ex)
                {
                    XTrace.WriteLine("二次上报失败,放弃该批次采样数据,{0}", model.Builders.FirstOrDefault()?.StartTime.ToDateTime());
                    XTrace.WriteException(ex);
                    //Log?.Error(ex + "");

                    // 星尘收集器上报,二次失败后放弃该批次数据,因其很可能是错误数据
                    //_fails.Enqueue(model);
                    break;
                }
            }
        }