Exemple #1
0
        /// <summary>
        /// 创建 <see cref="RequestCounter"/> 实例
        /// </summary>
        /// <param name="tagInfo">Tag信息</param>
        /// <param name="urlItem">所属Url项</param>
        public RequestCounter(RequestTagInfo tagInfo, UrlItem urlItem)
        {
            if (tagInfo == null) throw new ArgumentNullException("tagInfo");
            if (urlItem == null) throw new ArgumentNullException("urlItem");

            this.tagInfo = tagInfo;
            this.urlItem = urlItem;
        }
        /// <summary>
        /// 取得所有项
        /// </summary>
        public UrlItem[] GetAllItems()
        {
            cacheLock.EnterReadLock();
            var target = new UrlItem[cache.Count];

            try
            {
                cache.Values.CopyTo(target, 0);
            }
            catch
            { }
            finally
            {
                cacheLock.ExitReadLock();
            }

            return target;
        }
        /// <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);
        }
        private RequestStatInfo GetMetricInfoByUrl(UrlItem item, DateTime minute)
        {
            //统计各区间的请求次数
            var requestCount = 0L;
            var requestRanges = new Dictionary<Dictionary<string, string>, long>();
            foreach (var counter in item.GetAllCounters())
            {
                var count = (long)AggregateHelper.GetDoubles(MetricNames.Namespace, MetricNames.HttpRequest_Count, counter.TagInfo.FullTags, minute, minute, AggregateType.SUM)[0].GetValueOrDefault();
                if (count > 0)
                {
                    requestRanges.Add(counter.TagInfo.FullTags, count);
                    requestCount += count;
                }
            }

            //无请求时忽略统计
            if (requestCount == 0)
                return null;

            var statInfo = new RequestStatInfo();
            statInfo.RequestCount = requestCount;
            statInfo.RequestCountByRange = requestRanges;

            //统计请求耗时
            var reqCost = AggregateHelper.GetDoubles(MetricNames.Namespace, MetricNames.HttpRequest_Cost, item.Tags, minute, minute, AggregateType.MAX, AggregateType.MIN, AggregateType.AVG, AggregateType.SUM);
            statInfo.RequestCostMax = Round(reqCost[0], 2);
            statInfo.RequestCostMin = Round(reqCost[1], 2);
            statInfo.RequestCostAvg = Round(reqCost[2], 2);
            statInfo.RequestCostSum = reqCost[3];

            //统计请求内容大小
            var reqSize = AggregateHelper.GetDoubles(MetricNames.Namespace, MetricNames.HttpRequest_Size, item.Tags, minute, minute, AggregateType.MAX, AggregateType.MIN, AggregateType.AVG, AggregateType.SUM);
            statInfo.RequestSizeMax = (long?)reqSize[0];
            statInfo.RequestSizeMin = (long?)reqSize[1];
            statInfo.RequestSizeAvg = (long?)reqSize[2];
            statInfo.RequestSizeSum = (long?)reqSize[3];

            //统计响应内容大小
            var respSize = AggregateHelper.GetDoubles(MetricNames.Namespace, MetricNames.HttpResponse_Size, item.Tags, minute, minute, AggregateType.MAX, AggregateType.MIN, AggregateType.AVG, AggregateType.SUM);
            statInfo.ResponseSizeMax = (long?)respSize[0];
            statInfo.ResponseSizeMin = (long?)respSize[1];
            statInfo.ResponseSizeAvg = (long?)respSize[2];
            statInfo.ResponseSizeSum = (long?)respSize[3];

            statInfo.StatTime = minute;
            statInfo.Tags = new Dictionary<string,string>(item.Tags);

            return statInfo;
        }