Beispiel #1
0
        /// <summary>
        /// 获取相应计数器
        /// </summary>
        /// <param name="traceInfo">跟踪信息</param>
        /// <returns></returns>
        public RequestCounter GetCounter(UrlTraceInfo traceInfo)
        {
            if (traceInfo == null)
                throw new ArgumentNullException("traceInfo");

            Range requestCostRange = Range.Empty;
            Range responseSizeRange = Range.Empty;

            if (traceInfo.TraceItems.RequestCost)
                requestCostRange = RequestTagProvider.RequestCostRanges.First(r => traceInfo.RequestCost >= r.Lower && traceInfo.RequestCost < r.Upper);

            if (traceInfo.TraceItems.ResponseSize)
                responseSizeRange = RequestTagProvider.ResponseSizeRanges.First(r => traceInfo.ResponseSize >= r.Lower && traceInfo.ResponseSize < r.Upper);

            string key = string.Concat(requestCostRange.Id, "|", responseSizeRange.Id);
            RequestCounter counter;

            countersLock.EnterReadLock();
            counters.TryGetValue(key, out counter);
            countersLock.ExitReadLock();

            if (counter == null)
            {
                countersLock.EnterWriteLock();
                counters.TryGetValue(key, out counter);
                if (counter == null)
                {
                    try
                    {
                        RequestTagInfo tagInfo = new RequestTagInfo();
                        tagInfo.RequestCostRange = requestCostRange;
                        tagInfo.ResponseSizeRange = responseSizeRange;

                        tagInfo.FullTags = new Dictionary<string, string>();
                        tagInfo.FullTags.Add("HostAddress", hostAddress);
                        tagInfo.FullTags.Add("FilePath", filePath);

                        if (tagInfo.RequestCostRange.Id != 0) tagInfo.FullTags.Add("Latency", tagInfo.RequestCostRange.Name);
                        if (tagInfo.ResponseSizeRange.Id != 0) tagInfo.FullTags.Add("ResponseSize", tagInfo.ResponseSizeRange.Name);

                        counter = new RequestCounter(tagInfo, this);
                        counters.Add(key, counter);
                    }
                    catch
                    { }
                    finally
                    {
                        countersLock.ExitWriteLock();
                    }
                }
                else
                {
                    countersLock.ExitWriteLock();
                }
            }

            return counter;
        }
Beispiel #2
0
        /// <summary>
        /// 创建 <see cref="UrlItem"/> 实例
        /// </summary>
        /// <param name="traceInfo">跟踪信息</param>
        public UrlItem(UrlTraceInfo traceInfo)
        {
            if (traceInfo == null)
                throw new ArgumentNullException("traceInfo");

            hostAddress = traceInfo.HostAddress;
            filePath = traceInfo.FilePath;

            tags.Add(TagNames.HostAddress, hostAddress);
            tags.Add(TagNames.FilePath, filePath);
        }
Beispiel #3
0
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="traceInfo">跟踪信息</param>
        public RequestCounter Register(UrlTraceInfo traceInfo)
        {
            if (traceInfo == null)
                throw new ArgumentNullException("traceInfo");

            string key = string.Concat(traceInfo.HostAddress, "|", traceInfo.FilePath);
            UrlItem item;

            cacheLock.EnterReadLock();
            cache.TryGetValue(key, out item);
            cacheLock.ExitReadLock();

            if (item == null)
            {
                cacheLock.EnterWriteLock();
                cache.TryGetValue(key, out item);
                if (item == null)
                {
                    try
                    {
                        item = new UrlItem(traceInfo);
                        cache.Add(key, item);
                    }
                    catch
                    { }
                    finally
                    {
                        cacheLock.ExitWriteLock();
                    }
                }
                else
                {
                    cacheLock.ExitWriteLock();
                }
            }

            return item.GetCounter(traceInfo);
        }
Beispiel #4
0
        private static void WriteMetricInfo(UrlTraceInfo traceInfo)
        {
            if (traceInfo.TraceItems.Enabled)
            {
                try
                {
                    var tags = new Dictionary<string, string>();
                    tags.Add("HostAddress", traceInfo.HostAddress);
                    tags.Add("FilePath", traceInfo.FilePath);

                    if (traceInfo.TraceItems.RequestCost)
                        requestCostMetric.Set(tags, traceInfo.RequestCost);

                    if (traceInfo.TraceItems.RequestSize)
                        requestSizeMetric.Set(tags, traceInfo.RequestSize);

                    tags.Add("Latency", traceInfo.TraceItems.RequestCost ? requestCountMetric.GetRequestCostRange(traceInfo.RequestCost) : null);
                    tags.Add("ResponseSize", traceInfo.TraceItems.ResponseSize ? requestCountMetric.GetResponseSizeRange(traceInfo.ResponseSize) : null);
                    requestCountMetric.Set(tags, 1);
                }
                catch
                { }
            }
        }
Beispiel #5
0
        private static void WriteTraceInfo(UrlTraceInfo traceInfo)
        {
            var addInfo = new Dictionary<string, string>();
            addInfo.Add("HostAddress", traceInfo.HostAddress);
            addInfo.Add("AbsolutePath", traceInfo.FilePath);
            addInfo.Add("cooperation_id", AppConst.CFX_CooperationID);
            

            if (traceInfo.TraceItems.WSRequestInfo)
            {
                if (traceInfo.RequestType != null) addInfo.Add("RequestType", traceInfo.RequestType);
                if (traceInfo.ClientAppId != null) addInfo.Add("ClientAppId", traceInfo.ClientAppId);
            }

            if (traceInfo.TraceItems.RequestSize) addInfo.Add("RequestSize", traceInfo.RequestSize.ToString());
            if (traceInfo.TraceItems.ResponseStatus) addInfo.Add("ResponseStatus", traceInfo.ResponseStatus.ToString());
            if (traceInfo.TraceItems.RequestCost) addInfo.Add("RequestCost", traceInfo.RequestCost.ToString());
            if (traceInfo.TraceItems.ClientIP) addInfo.Add("ClientIP", traceInfo.ClientIP);

            try
            {
                if (traceInfo.TraceItems.RawUrl)
                    tracer.Log(LogType.URL, LogLevel.INFO,traceInfo.HttpMethod, traceInfo.RawUrl, addInfo);
                else
                    tracer.Log(LogType.URL, LogLevel.INFO, "", addInfo);
            }
            catch
            { }
        }
Beispiel #6
0
 public ResponseFilter(Stream sink, UrlTraceInfo context)
 {
     this.sink = sink;
     this.context = context;
 }
Beispiel #7
0
 public static void AddUrlTrace(UrlTraceInfo info)
 {
     if (info == null) return;
     if (info.FilePath.IndexOf("/.ashx") != -1) return;
     try
     {
         Task.Factory.StartNew(() =>
         {
             lock (Obj)
             {
                 try
                 {
                     AddInt(info.FilePath, ref Total);
                     if (info.ResponseStatus != 200)
                     {
                         AddInt(info.FilePath, ref Failures);
                     }
                     AddDouble(info.FilePath, info.RequestCost, ref Cost);
                     ConvertMax(info.FilePath, info.RequestCost, ref Max);
                     ConvertMin(info.FilePath, info.RequestCost, ref Min);
                     AddLong(info.FilePath, info.RequestSize, ref ReqSize);
                 }
                 catch { }
             }
         });
     }
     catch { }
 }
Beispiel #8
0
        public void ResponseFilter_Test()
        {
            StringBuilder s = new StringBuilder();
            Stream stres = new MemoryStream();
            UrlTraceInfo context = new UrlTraceInfo();
            ResponseFilter filter = new ResponseFilter(stres, context);


            string content = "this is a test";

            byte[] buff = System.Text.Encoding.UTF8.GetBytes(content);

            filter.Write(buff, 0, buff.Length);

            byte[] buff1 = new byte[buff.Length];


            filter.SetLength(buff.Length);
            filter.Seek(0, SeekOrigin.Begin);
            filter.Read(buff1, 0, buff.Length);
            string content1 = System.Text.Encoding.UTF8.GetString(buff1);

            Assert.IsTrue(filter.CanRead);
            Assert.IsTrue(filter.CanSeek);
            Assert.IsTrue(filter.CanWrite);
            Assert.AreEqual(filter.Length, 0);
            filter.Position = 0;
            Assert.AreEqual(filter.Position, 0);
            Assert.AreEqual(content, content1);

            filter.Flush();

        }
        /// <summary>
        /// 开始收集
        /// </summary>
        /// <param name="traceContext">跟踪上下文</param>
        public static void StartCollect(UrlTraceContext traceContext)
        {
            if (traceContext.TraceSpan == null)
                return;

            var request = HttpContext.Current.Request;
            var response = HttpContext.Current.Response;

            UrlTraceInfo traceInfo = new UrlTraceInfo();

            try
            {
                traceInfo.TraceItems = (UrlTraceItems)ConfigHelper.UrlTraceItems.Clone();
                traceInfo.HostAddress = request.ServerVariables["Server_Name"].ToLower(); //主机域名或IP

                //请求的目标服务方法
                if (traceContext.TraceSpan.SpanType == SpanType.WEB_SERVICE && traceInfo.TraceItems.WSRequestInfo)
                {
                    string requestType = request.Headers["Tracing-RequestType"];
                    if (requestType != null)
                    {
                        traceInfo.RequestType = requestType;
                    }
                    else
                    {
                        requestType = request.Headers["SOAPAction"];
                        if (requestType != null)
                        {
                            Match match = Regex.Match(requestType, "/?([^\"/]*)\"$", RegexOptions.Compiled);
                            if (match.Success && match.Groups.Count > 1)
                                traceInfo.RequestType = match.Groups[1].Value;
                        }
                    }

                    traceInfo.ClientAppId = request.Headers["ClientAppId"];
                }

                //请求内容大小
                if (traceInfo.TraceItems.RequestSize)
                {
                    long value;
                    if (long.TryParse(request.Headers["Content-Length"], out value))
                        traceInfo.RequestSize = value;
                }

                if (traceInfo.TraceItems.RawUrl)
                {
                    traceInfo.RawUrl = request.RawUrl;
                    traceInfo.HttpMethod = request.HttpMethod;
                }

                if (traceInfo.TraceItems.ResponseSize)
                    response.Filter = new ResponseFilter(response.Filter, traceInfo); //响应内容大小

                if (traceInfo.TraceItems.ClientIP)
                    traceInfo.ClientIP = IPHelper.GetClientIP(); //客户端IP

                traceContext.TraceInfo = traceInfo;
            }
            catch
            { }
        }