コード例 #1
0
        private string ValidateTrace(Downstream target, TraceResponse resp, string service, int level, string traceID, bool sampled, string baggage)
        {
            if (!EqualTraceIDs(traceID, resp.Span.TraceId))
            {
                return($"Trace ID mismatch in S{level}({service}): expected {traceID}, received {resp.Span.TraceId}");
            }
            if (baggage != resp.Span.Baggage)
            {
                return($"Baggage mismatch in S{level}({service}): expected {baggage}, received {resp.Span.Baggage}");
            }
            if (sampled != resp.Span.Sampled)
            {
                return($"Sampled mismatch in S{level}({service}): expected {sampled}, received {resp.Span.Sampled}");
            }
            if (target != null)
            {
                if (resp.Downstream == null)
                {
                    return($"Missing downstream in S{level}({service})");
                }

                return(ValidateTrace(target.Downstream_, resp.Downstream, target.Host, level + 1, traceID, sampled, baggage));
            }

            if (resp.Downstream != null)
            {
                return($"Unexpected downstream in S{level}({service})");
            }

            return(null);
        }
コード例 #2
0
        public async Task <TraceResponse> PrepareResponseAsync(Downstream downstream)
        {
            var response = new TraceResponse(ObserveSpan());

            if (downstream != null)
            {
                var downstreamResponse = await CallDownstreamAsync(downstream);

                response.Downstream = downstreamResponse;
            }

            return(response);
        }
コード例 #3
0
ファイル: TraceController.cs プロジェクト: wenfeifei/Stardust
        public TraceResponse Report([FromBody] TraceModel model, String token)
        {
            var builders = model?.Builders.Cast <ISpanBuilder>().ToArray();

            //var builders = new ISpanBuilder[0];
            if (model == null || model.AppId.IsNullOrEmpty() || builders == null || builders.Length == 0)
            {
                return(null);
            }

            var ip = HttpContext.GetUserHost();

            if (ip.IsNullOrEmpty())
            {
                ip = ManageProvider.UserHost;
            }

            var set = Setting.Current;

            // 新版验证方式,访问令牌
            Data.App ap = null;
            if (!token.IsNullOrEmpty() && token.Split(".").Length == 3)
            {
                ap = _service.DecodeToken(token, set);
                if (ap == null || ap.Name != model.AppId)
                {
                    throw new InvalidOperationException($"授权不匹配[{model.AppId}]!=[{ap.Name}]!");
                }
            }
            Data.App.UpdateInfo(model, ip);

            // 该应用的跟踪配置信息
            var app = AppTracer.FindByName(model.AppId);

            if (app == null)
            {
                app = new AppTracer
                {
                    Name        = model.AppId,
                    DisplayName = model.AppName,
                    Enable      = set.AutoRegister,
                };
                app.Save();
            }

            // 校验应用
            if (app == null || !app.Enable)
            {
                throw new Exception($"无效应用[{model.AppId}/{model.AppName}]");
            }

            // 插入数据
            Task.Run(() => ProcessData(app, model, ip, builders));

            // 构造响应
            var rs = new TraceResponse
            {
                Period     = app.Period,
                MaxSamples = app.MaxSamples,
                MaxErrors  = app.MaxErrors,
                Timeout    = app.Timeout,
                //Excludes = app.Excludes?.Split(",", ";"),
            };

            // 新版本才返回Excludes,老版本客户端在处理Excludes时有BUG,错误处理/
            if (!model.Version.IsNullOrEmpty())
            {
                rs.Excludes = app.Excludes?.Split(",", ";");
            }

            return(rs);
        }
コード例 #4
0
        public TraceResponse Report([FromBody] TraceModel model, String token)
        {
            var builders = model?.Builders;

            if (model == null || model.AppId.IsNullOrEmpty())
            {
                return(null);
            }

            var ip = HttpContext.GetUserHost();

            if (ip.IsNullOrEmpty())
            {
                ip = ManageProvider.UserHost;
            }

            var set = Setting.Current;

            // 新版验证方式,访问令牌
            App ap = null;

            if (!token.IsNullOrEmpty() && token.Split(".").Length == 3)
            {
                ap = _service.DecodeToken(token, set);
                //if (ap == null || ap.Name != model.AppId) throw new InvalidOperationException($"授权不匹配[{model.AppId}]!=[{ap?.Name}]!");
                if (ap == null)
                {
                    throw new InvalidOperationException($"授权不匹配[{model.AppId}]!=[{ap?.Name}]!");
                }
            }
            App.UpdateInfo(model, ip);

            var clientId = model.ClientId;

            if (clientId.IsNullOrEmpty())
            {
                var(jwt, ex) = _service.DecodeToken(token, set.TokenSecret);
                clientId     = jwt?.Id;
            }
            AppOnline.UpdateOnline(ap, clientId, ip, token, model.Info);

            // 该应用的追踪配置信息
            var app = AppTracer.FindByName(model.AppId);

            if (app == null)
            {
                app = new AppTracer
                {
                    Name        = model.AppId,
                    DisplayName = model.AppName,
                    Enable      = set.AutoRegister,
                };
                app.Save();
            }

            // 校验应用
            if (app == null || !app.Enable)
            {
                throw new Exception($"无效应用[{model.AppId}/{model.AppName}]");
            }

            // 插入数据
            if (builders != null && builders.Length > 0)
            {
                Task.Run(() => ProcessData(app, model, ip, builders));
            }

            // 构造响应
            var rs = new TraceResponse
            {
                Period     = app.Period,
                MaxSamples = app.MaxSamples,
                MaxErrors  = app.MaxErrors,
                Timeout    = app.Timeout,
                //Excludes = app.Excludes?.Split(",", ";"),
            };

            // Vip客户端。高频次大样本采样,10秒100次,逗号分割,支持*模糊匹配
            if (app.IsVip(model.ClientId))
            {
                rs.Period     = 10;
                rs.MaxSamples = 100;
            }

            // 新版本才返回Excludes,老版本客户端在处理Excludes时有BUG,错误处理/
            if (!model.Version.IsNullOrEmpty())
            {
                rs.Excludes = app.Excludes?.Split(",", ";");
            }

            return(rs);
        }