コード例 #1
0
        public MetricPointTests()
        {
            this.meter     = new Meter(Utils.GetCurrentMethodName());
            this.histogram = this.meter.CreateHistogram <long>("histogram");

            // Evenly distribute the bound values over the range [0, MaxValue)
            this.bounds = new double[10];
            for (int i = 0; i < this.bounds.Length; i++)
            {
                this.bounds[i] = i * 1000 / this.bounds.Length;
            }

            var exportedItems = new List <Metric>();

            this.provider = Sdk.CreateMeterProviderBuilder()
                            .AddMeter(this.meter.Name)
                            .AddInMemoryExporter(exportedItems)
                            .AddView(this.histogram.Name, new ExplicitBucketHistogramConfiguration()
            {
                Boundaries = this.bounds
            })
                            .Build();

            this.histogram.Record(500);

            this.provider.ForceFlush();

            this.metric = exportedItems[0];
            var metricPointsEnumerator = this.metric.GetMetricPoints().GetEnumerator();

            metricPointsEnumerator.MoveNext();
            this.metricPoint = metricPointsEnumerator.Current;
        }
コード例 #2
0
        public MetricsData(int version, Metric metric, ref MetricPoint metricPoint) : base(version)
        {
            if (metric == null)
            {
                throw new ArgumentNullException(nameof(metric));
            }

            IList <MetricDataPoint> metricDataPoints = new List <MetricDataPoint>();
            MetricDataPoint         metricDataPoint  = null;

            switch (metric.MetricType)
            {
            case MetricType.DoubleSum:
                metricDataPoint = new MetricDataPoint(metric.Name, metricPoint.GetSumDouble());
                metricDataPoint.DataPointType = DataPointType.Aggregation;
                break;

            case MetricType.DoubleGauge:
                metricDataPoint = new MetricDataPoint(metric.Name, metricPoint.GetGaugeLastValueDouble());
                metricDataPoint.DataPointType = DataPointType.Measurement;
                break;
            }

            metricDataPoints.Add(metricDataPoint);
            Metrics    = metricDataPoints;
            Properties = new ChangeTrackingDictionary <string, string>();
            foreach (var tag in metricPoint.Tags)
            {
                Properties.Add(new KeyValuePair <string, string>(tag.Key, tag.Value?.ToString()));
            }
            Properties.Add(AggregationIntervalMsKey, DefaultExportIntervalMilliseconds);
        }
コード例 #3
0
        public static MetricPoint MakePoint(string metric, string db, string rp = "default")
        {
            MetricPoint metricPoint = new MetricPoint(metric, db, rp);

            metricPoint.OnSubmit += new Action <MetricPoint>(WritePoint);
            return(metricPoint);
        }
コード例 #4
0
 public void Submit(MetricPoint point)
 {
     if ((point == null ? false : point.IsValid))
     {
         this._queueGroup.Enqueue(MetricPoint.CombineDBRP(point.DB, point.RP), point);
     }
 }
コード例 #5
0
 public static void WritePoint(string metric, IDictionary <string, string> fields, IDictionary <string, string> tags, DateTime timestampInUTC, string db, string rp)
 {
     ParamterUtil.CheckEmptyString("metric", metric);
     ParamterUtil.CheckEmptyString("db", db);
     ParamterUtil.CheckEmptyString("rp", rp);
     if (AppContext.MeasurementEnabled)
     {
         if ((fields == null ? false : fields.Count > 0))
         {
             MetricPoint metricPoint = new MetricPoint(metric, db, rp);
             foreach (KeyValuePair <string, string> field in fields)
             {
                 BuildFields(metricPoint.Fields, field.Key, field.Value);
             }
             if ((tags == null ? false : tags.Count > 0))
             {
                 foreach (KeyValuePair <string, string> tag in tags)
                 {
                     BuildTags(metricPoint.Tags, tag.Key, tag.Value);
                 }
             }
             metricPoint.TimeStamp = timestampInUTC.ToString("yyyy/MM/dd HH:mm:ss.ffffff");
             MeasurementHost.Instance.Submit(metricPoint);
         }
     }
 }
コード例 #6
0
        public void HistogramDistributeToAllBucketsDefault()
        {
            var histogramPoint = new MetricPoint(AggregationType.Histogram, DateTimeOffset.Now, null, null, Metric.DefaultHistogramBounds);

            histogramPoint.Update(-1);
            histogramPoint.Update(0);
            histogramPoint.Update(2);
            histogramPoint.Update(5);
            histogramPoint.Update(8);
            histogramPoint.Update(10);
            histogramPoint.Update(11);
            histogramPoint.Update(25);
            histogramPoint.Update(40);
            histogramPoint.Update(50);
            histogramPoint.Update(70);
            histogramPoint.Update(75);
            histogramPoint.Update(99);
            histogramPoint.Update(100);
            histogramPoint.Update(246);
            histogramPoint.Update(250);
            histogramPoint.Update(499);
            histogramPoint.Update(500);
            histogramPoint.Update(999);
            histogramPoint.Update(1000);
            histogramPoint.Update(1001);
            histogramPoint.Update(10000000);
            histogramPoint.TakeSnapshot(true);

            Assert.Equal(22, histogramPoint.LongValue);
            for (int i = 0; i < histogramPoint.BucketCounts.Length; i++)
            {
                Assert.Equal(2, histogramPoint.BucketCounts[i]);
            }
        }
コード例 #7
0
 public static void WritePoint(MetricPoint point)
 {
     if (AppContext.MeasurementEnabled)
     {
         MeasurementHost.Instance.Submit(point);
     }
 }
コード例 #8
0
        public MetricsData(int version, Metric metric, ref MetricPoint metricPoint) : base(version)
        {
            IList <MetricDataPoint> metricDataPoints = new List <MetricDataPoint>();
            MetricDataPoint         metricDataPoint  = null;

            switch (metric.MetricType)
            {
            case MetricType.DoubleSum:
                metricDataPoint = new MetricDataPoint(metric.Name, metricPoint.GetSumDouble());
                metricDataPoint.DataPointType = DataPointType.Aggregation;
                break;

            case MetricType.DoubleGauge:
                metricDataPoint = new MetricDataPoint(metric.Name, metricPoint.GetGaugeLastValueDouble());
                metricDataPoint.DataPointType = DataPointType.Measurement;
                break;
            }

            metricDataPoints.Add(metricDataPoint);
            Metrics    = metricDataPoints;
            Properties = new ChangeTrackingDictionary <string, string>();
            foreach (var tag in metricPoint.Tags)
            {
                Properties.Add(new KeyValuePair <string, string>(tag.Key, tag.Value?.ToString()));
            }
        }
コード例 #9
0
        public void HistogramDistributeToAllBucketsCustom()
        {
            var boundaries     = new double[] { 10, 20 };
            var histogramPoint = new MetricPoint(AggregationType.Histogram, DateTimeOffset.Now, null, null, boundaries);

            // 5 recordings <=10
            histogramPoint.Update(-10);
            histogramPoint.Update(0);
            histogramPoint.Update(1);
            histogramPoint.Update(9);
            histogramPoint.Update(10);

            // 2 recordings >10, <=20
            histogramPoint.Update(11);
            histogramPoint.Update(19);

            histogramPoint.TakeSnapshot(true);

            // Sum of all recordings
            Assert.Equal(40, histogramPoint.DoubleValue);

            // Count  = # of recordings
            Assert.Equal(7, histogramPoint.LongValue);
            Assert.Equal(boundaries.Length + 1, histogramPoint.BucketCounts.Length);
            Assert.Equal(5, histogramPoint.BucketCounts[0]);
            Assert.Equal(2, histogramPoint.BucketCounts[1]);
            Assert.Equal(0, histogramPoint.BucketCounts[2]);
        }
コード例 #10
0
    private void ReportCoreMetrics(Object stateInfo)
    {
        var rCount = Interlocked.CompareExchange(ref roomCount, 0, 0);
        var pCount = Interlocked.CompareExchange(ref playerCount, 0, 0);
        var mCount = Interlocked.Exchange(ref msgCount, 0);

        var rPoint = new MetricPoint()
        {
            Name  = "RoomCount",
            Value = rCount,
            Attr  = MetricAttr.Core,
        };
        var pPoint = new MetricPoint()
        {
            Name  = "PlayerCount",
            Value = pCount,
            Attr  = MetricAttr.Core,
        };
        var mPoint = new MetricPoint()
        {
            Name  = "MessageCount",
            Value = mCount,
            Attr  = MetricAttr.Core,
        };

        Report(rPoint, pPoint, mPoint);
    }
コード例 #11
0
        public void HistogramWithOnlySumCount()
        {
            var boundaries     = new double[] { };
            var histogramPoint = new MetricPoint(AggregationType.HistogramSumCount, DateTimeOffset.Now, null, null, boundaries);

            histogramPoint.Update(-10);
            histogramPoint.Update(0);
            histogramPoint.Update(1);
            histogramPoint.Update(9);
            histogramPoint.Update(10);
            histogramPoint.Update(11);
            histogramPoint.Update(19);

            histogramPoint.TakeSnapshot(true);

            var count = histogramPoint.GetHistogramCount();
            var sum   = histogramPoint.GetHistogramSum();

            // Sum of all recordings
            Assert.Equal(40, sum);

            // Count  = # of recordings
            Assert.Equal(7, count);

            // There should be no enumeration of BucketCounts and ExplicitBounds for HistogramSumCount
            var enumerator = histogramPoint.GetHistogramBuckets().GetEnumerator();

            Assert.False(enumerator.MoveNext());
        }
コード例 #12
0
 private void ProcessPoints(object sender, List <MetricPoint> points)
 {
     try
     {
         if ((points == null ? false : points.Count > 0))
         {
             if ((sender == null ? false : sender is WorkerQueue <MetricPoint>))
             {
                 string[] strArrays = MetricPoint.ParseDBRP((sender as WorkerQueue <MetricPoint>).QueueName);
                 string   str       = strArrays[0];
                 PushToServer(points, str, strArrays[1]);
             }
         }
     }
     catch (Exception exception)
     {
         DebugUtil.LogException(exception);
     }
 }
コード例 #13
0
        public void HistogramDistributeToAllBucketsCustom()
        {
            var boundaries     = new double[] { 10, 20 };
            var histogramPoint = new MetricPoint(AggregationType.Histogram, DateTimeOffset.Now, null, null, boundaries);

            // 5 recordings <=10
            histogramPoint.Update(-10);
            histogramPoint.Update(0);
            histogramPoint.Update(1);
            histogramPoint.Update(9);
            histogramPoint.Update(10);

            // 2 recordings >10, <=20
            histogramPoint.Update(11);
            histogramPoint.Update(19);

            histogramPoint.TakeSnapshot(true);

            var count = histogramPoint.GetHistogramCount();
            var sum   = histogramPoint.GetHistogramSum();

            // Sum of all recordings
            Assert.Equal(40, sum);

            // Count  = # of recordings
            Assert.Equal(7, count);

            int index                = 0;
            int actualCount          = 0;
            var expectedBucketCounts = new long[] { 5, 2, 0 };

            foreach (var histogramMeasurement in histogramPoint.GetHistogramBuckets())
            {
                Assert.Equal(expectedBucketCounts[index], histogramMeasurement.BucketCount);
                index++;
                actualCount++;
            }

            Assert.Equal(boundaries.Length + 1, actualCount);
        }
コード例 #14
0
        public void HistogramDistributeToAllBucketsDefault()
        {
            var histogramPoint = new MetricPoint(AggregationType.Histogram, DateTimeOffset.Now, null, null, Metric.DefaultHistogramBounds);

            histogramPoint.Update(-1);
            histogramPoint.Update(0);
            histogramPoint.Update(2);
            histogramPoint.Update(5);
            histogramPoint.Update(8);
            histogramPoint.Update(10);
            histogramPoint.Update(11);
            histogramPoint.Update(25);
            histogramPoint.Update(40);
            histogramPoint.Update(50);
            histogramPoint.Update(70);
            histogramPoint.Update(75);
            histogramPoint.Update(99);
            histogramPoint.Update(100);
            histogramPoint.Update(246);
            histogramPoint.Update(250);
            histogramPoint.Update(499);
            histogramPoint.Update(500);
            histogramPoint.Update(999);
            histogramPoint.Update(1000);
            histogramPoint.Update(1001);
            histogramPoint.Update(10000000);
            histogramPoint.TakeSnapshot(true);

            var count = histogramPoint.GetHistogramCount();

            Assert.Equal(22, count);

            int actualCount = 0;

            foreach (var histogramMeasurement in histogramPoint.GetHistogramBuckets())
            {
                Assert.Equal(2, histogramMeasurement.BucketCount);
                actualCount++;
            }
        }
コード例 #15
0
        public void HistogramWithOnlySumCount()
        {
            var boundaries     = new double[] { };
            var histogramPoint = new MetricPoint(AggregationType.HistogramSumCount, DateTimeOffset.Now, null, null, boundaries);

            histogramPoint.Update(-10);
            histogramPoint.Update(0);
            histogramPoint.Update(1);
            histogramPoint.Update(9);
            histogramPoint.Update(10);
            histogramPoint.Update(11);
            histogramPoint.Update(19);

            histogramPoint.TakeSnapshot(true);

            // Sum of all recordings
            Assert.Equal(40, histogramPoint.DoubleValue);

            // Count  = # of recordings
            Assert.Equal(7, histogramPoint.LongValue);
            Assert.Null(histogramPoint.BucketCounts);
            Assert.Null(histogramPoint.ExplicitBounds);
        }
コード例 #16
0
        public MetricPoint MakePoint()
        {
            MetricPoint metricPoint = MakePoint(this._metric, this._db, this._rp);

            return(metricPoint);
        }
コード例 #17
0
    public override IMessage OnHotelBroadCast(ByteString msg)
    {
        HotelBroadcast broadcast = new HotelBroadcast();

        ByteUtils.ByteStringToObject(broadcast, msg);
        Logger.Info("HotelBroadcast start, userID:{0} gameID:{1} roomID:{2} cpProto:{3}", broadcast.UserID, broadcast.GameID, broadcast.RoomID, broadcast.CpProto.ToStringUtf8());

        HotelBroadcastAck broadcastAck = new HotelBroadcastAck()
        {
            UserID = broadcast.UserID, Status = (UInt32)ErrorCode.Ok
        };

        PushToHotelMsg pushMsg = new PushToHotelMsg()
        {
            PushType = PushMsgType.UserTypeAll,
            GameID   = broadcast.GameID,
            RoomID   = broadcast.RoomID,
            CpProto  = broadcast.CpProto,
        };

        pushMsg.DstUids.Add(broadcast.UserID);

        PushToHotel(broadcast.RoomID, pushMsg);

        //测试主动推送给MVS的两个消息
        string str = broadcast.CpProto.ToStringUtf8();

        Logger.Info("HotelBroadcast, str = {0}", str);

        String[] result = str.Split("|");
        if (result.Length > 1)
        {
            if (result[0] == "joinover")
            {
                String[] param = result[1].Split(",");
                if (param.Length > 1)
                {
                    UInt64 roomID = UInt64.Parse(param[0]);
                    UInt32 gameID = UInt32.Parse(param[1]);
                    PushJoinOver(roomID, gameID);
                }
            }
            else if (result[0] == "joinopen")
            {
                PushJoinOpen(broadcast.RoomID, broadcast.GameID);
            }
            else if (result[0] == "kickplayer")
            {
                String[] param = result[1].Split(",");
                if (param.Length > 2)
                {
                    UInt64 roomID = UInt64.Parse(param[0]);
                    UInt32 destID = UInt32.Parse(param[1]);
                    PushKickPlayer(roomID, destID);
                }
            }
            else if (result[0] == "getRoomDetail")
            {
                String[] param = result[1].Split(",");
                if (param.Length > 1)
                {
                    UInt32 gameID = UInt32.Parse(param[0]);
                    UInt64 roomID = UInt64.Parse(param[1]);
                    PushGetRoomDetail(roomID, gameID, 2);
                }
            }
            else if (result[0] == "setRoomProperty")
            {
                ByteString roomProperty = Google.Protobuf.ByteString.CopyFromUtf8(result[1]);
                PushSetRoomProperty(broadcast.RoomID, broadcast.GameID, roomProperty);
            }
            else if (result[0] == "createRoom")
            {
                CreateRoom request = new CreateRoom()
                {
                    GameID   = broadcast.GameID,
                    Ttl      = 600,
                    RoomInfo = new RoomInfo()
                    {
                        RoomName     = "game server room",
                        MaxPlayer    = 2,
                        Mode         = 1,
                        CanWatch     = 1,
                        Visibility   = 1,
                        RoomProperty = Google.Protobuf.ByteString.CopyFromUtf8("hello"),
                    },
                    WatchSetting = new WatchSetting()
                    {
                        MaxWatch        = 3,
                        WatchPersistent = false,
                        WatchDelayMs    = 10 * 1000,
                        CacheTime       = 60 * 1000,
                    },
                };
                var reply = CreateRoom(request);
                Logger.Debug("create room request: {0}, reply: {1}", request, reply);
            }
            else if (result[0] == "touchRoom")
            {
                String[]  param   = result[1].Split(",");
                TouchRoom request = new TouchRoom()
                {
                    GameID = broadcast.GameID,
                    RoomID = UInt64.Parse(param[0]),
                    Ttl    = UInt32.Parse(param[1]),
                };
                var reply = TouchRoom(request);
                Logger.Debug("touch room request: {0}, reply: {1}", request, reply);
            }
            else if (result[0] == "destroyRoom")
            {
                DestroyRoom request = new DestroyRoom()
                {
                    GameID = broadcast.GameID,
                    RoomID = UInt64.Parse(result[1]),
                };
                var reply = DestroyRoom(request);
                Logger.Debug("destroy room request: {0}, reply: {1}", request, reply);
            }
            else if (result[0] == "setFrameSyncRate")
            {
                if (result.Length < 3)
                {
                    Logger.Error("set frame sync rate error: no cacheFrameMS");
                    return(new HotelBroadcastAck()
                    {
                        UserID = broadcast.UserID, Status = (UInt32)ErrorCode.BadRequest
                    });
                }

                var rate         = UInt32.Parse(result[1]);
                var cacheFrameMS = Int32.Parse(result[2]);
                SetFrameSyncRate(broadcast.RoomID, broadcast.GameID, rate, 1, cacheFrameMS);
                Logger.Debug("set frame sync rate: {0}", rate);
            }
            else if (result[0] == "getCacheData")
            {
                Int32 cacheFrameMS = Int32.Parse(result[1]);
                GetCacheData(broadcast.RoomID, broadcast.GameID, cacheFrameMS);
                Logger.Debug("get cache frame data: {0}", cacheFrameMS);
            }
            else if (result[0] == "frameBroadcast")
            {
                var cpProto = result[1];
                FrameBroadcast(broadcast.RoomID, broadcast.GameID, ByteString.CopyFromUtf8(cpProto), 2);
                Logger.Info("frame broadcast: {0}", cpProto);
            }
            else if (result[0] == "metric")
            {
                if (result.Length >= 3)
                {
                    string      name  = result[1];
                    double      value = double.Parse(result[2]);
                    MetricPoint point = new MetricPoint()
                    {
                        Name  = name,
                        Value = value,
                        Attr  = MetricAttr.Custom,
                    };
                    ReportAck ack = ReportMetrics(point);
                    Logger.Info("Set matric response: {0}", ack);
                }
            }
        }

        Logger.Info("HotelBroadcast end, userID:{0} gameID:{1} roomID:{2} cpProto:{3}", broadcast.UserID, broadcast.GameID, broadcast.RoomID, broadcast.CpProto.ToStringUtf8());

        return(broadcastAck);
    }
コード例 #18
0
        public MetricsData(int version, Metric metric, MetricPoint metricPoint) : base(version)
        {
            if (metric == null)
            {
                throw new ArgumentNullException(nameof(metric));
            }

            IList <MetricDataPoint> metricDataPoints = new List <MetricDataPoint>();
            MetricDataPoint         metricDataPoint  = null;

            switch (metric.MetricType)
            {
            case MetricType.DoubleSum:
                metricDataPoint = new MetricDataPoint(metric.Name, metricPoint.GetSumDouble())
                {
                    DataPointType = DataPointType.Aggregation
                };
                break;

            case MetricType.DoubleGauge:
                metricDataPoint = new MetricDataPoint(metric.Name, metricPoint.GetGaugeLastValueDouble())
                {
                    DataPointType = DataPointType.Measurement
                };
                break;

            case MetricType.LongSum:
                // potential for minor precision loss implicitly going from long->double
                // see: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/numeric-conversions#implicit-numeric-conversions
                metricDataPoint = new MetricDataPoint(metric.Name, metricPoint.GetSumLong())
                {
                    DataPointType = DataPointType.Aggregation
                };
                break;

            case MetricType.LongGauge:
                // potential for minor precision loss implicitly going from long->double
                // see: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/numeric-conversions#implicit-numeric-conversions
                metricDataPoint = new MetricDataPoint(metric.Name, metricPoint.GetGaugeLastValueLong())
                {
                    DataPointType = DataPointType.Measurement
                };
                break;

            case MetricType.Histogram:
                metricDataPoint = new MetricDataPoint(metric.Name, metricPoint.GetHistogramSum());
                metricDataPoint.DataPointType = DataPointType.Aggregation;
                long histogramCount = metricPoint.GetHistogramCount();
                // Current schema only supports int values for count
                // if the value is within integer range we will use it otherwise ignore it.
                metricDataPoint.Count = (histogramCount <= int.MaxValue && histogramCount >= int.MinValue) ? (int?)histogramCount : null;
                break;
            }

            metricDataPoints.Add(metricDataPoint);
            Metrics    = metricDataPoints;
            Properties = new ChangeTrackingDictionary <string, string>();
            foreach (var tag in metricPoint.Tags)
            {
                if (tag.Key.Length <= SchemaConstants.MetricsData_Properties_MaxKeyLength && tag.Value != null)
                {
                    // Note: if Key exceeds MaxLength or if Value is null, the entire KVP will be dropped.

                    Properties.Add(new KeyValuePair <string, string>(tag.Key, tag.Value.ToString().Truncate(SchemaConstants.MetricsData_Properties_MaxValueLength)));
                }
            }
            Properties.Add(AggregationIntervalMsKey, DefaultExportIntervalMilliseconds);
        }