Пример #1
0
        public void Smoke_test_for_counter_with_context()
        {
            Metric.Config
            .WithReporting(r => (reports = r).WithElasticSearchReport(
                               TimeSpan.FromSeconds(1), sutConfigurer
                               ));
            Counter c = Metric.Counter("test1", Unit.Calls);

            c.Increment("item1", 3);
            c.Increment("item2", 4);
            Thread.Sleep(2000);
            c.Increment("item1", 5);
            c.Increment("item3", 7);
            Thread.Sleep(2000);

            var records = base.GetAllCounters("es-test", "test1");

            Assert.That(records.Any(r => r.Value <Int32>("Count") == 7));
            Assert.That(records.Any(r => r.Value <Int32>("Count") == 19));

            records = base.GetAllCountersDiff("es-test", "test1");
            Assert.That(records.Any(r => r.Value <Int32>("Count") == 7));
            Assert.That(records.Any(r => r.Value <Int32>("Count") == 12));

            var lastRecord = records.Single(r => r.Value <Int32>("Count") == 12);

            Assert.That(lastRecord.Value <Int32>("item1-Count"), Is.EqualTo(5));
            Assert.That(lastRecord.Value <Int32>("item3-Count"), Is.EqualTo(7));
        }
Пример #2
0
        public void Increment_Amount(string name, long amount)
        {
            var counter = Metric.Counter(name, Unit.Bytes);
            var test    = Create(counter);

            test.Increment(amount);
            dynamic count = counter;

            Assert.Equal(amount, count.Value.Count);
        }
Пример #3
0
        public void Increment_Amount_child(string name, string name2, long amount)
        {
            var counter = Metric.Counter(name, name);
            var test    = Create(counter);

            test.Increment(name2, amount);
            dynamic count = counter;

            Assert.Equal(amount, count.Value.Count);
            Assert.Equal(amount, count.Value.Items[0].Count);
        }
Пример #4
0
        public void Decrement(string name)
        {
            var     counter = Metric.Counter(name, Unit.Bytes);
            dynamic count   = counter;
            var     test    = Create(counter);

            test.Increment();
            test.Increment();
            test.Increment();
            Assert.Equal(3, count.Value.Count);
            test.Decrement();
            Assert.Equal(2, count.Value.Count);
            test.Decrement();
            Assert.Equal(1, count.Value.Count);
            test.Decrement();
            Assert.Equal(0, count.Value.Count);
        }
Пример #5
0
        public void Verify_index_type()
        {
            Metric.Config
            .WithReporting(r => (reports = r).WithElasticSearchReport(
                               TimeSpan.FromSeconds(1), sutConfigurer
                               ));
            Counter c = Metric.Counter("test", Unit.Calls);

            c.Increment(3);
            Thread.Sleep(3000);

            var indexName = prefix + "-" + DateTime.UtcNow.ToString("yyyyMMdd");

            using (var client = new WebClient())
            {
                var result = client.DownloadString("http://" + esAddress + ":" + esPort + "/" + indexName + "/_search");
            }
        }
Пример #6
0
        static Counter getCounterForMethod(string methodName)
        {
            if (countersForMethods == null)
            {
                countersForMethods = new Dictionary <string, Counter>();
            }

            if (countersForMethods.ContainsKey(methodName))
            {
                return(countersForMethods[methodName]);
            }
            else
            {
                Counter counter = Metric.Counter(methodName, Unit.Calls);
                countersForMethods[methodName] = counter;
                return(counter);
            }
        }
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            if (string.IsNullOrWhiteSpace(_address) || string.IsNullOrWhiteSpace(_db))
            {
                //do nothing....
            }
            else
            {
                Metric.Config
                .WithReporting(report => report
                               .WithInfluxDbHttp(_address, _db, TimeSpan.FromSeconds(2), null, c => c
                                                 .WithConverter(new DefaultConverter().WithGlobalTags("host=" + Dns.GetHostAddresses(Dns.GetHostName()).LastOrDefault() + ",env=dev"))
                                                 .WithFormatter(new DefaultFormatter().WithLowercase(true))
                                                 .WithWriter(new InfluxdbHttpWriter(c, 1))));
                _tpsRequestsCounter = Metric.Counter(_metricsName + "_tps", Unit.Requests);
                _tpsRequestsCounter.Increment();
            }

            base.OnActionExecuting(actionContext);
        }
Пример #8
0
        static void PushData(
            [DefaultValue("localhost")] string host,
            [DefaultValue(8086)] int port,
            [DefaultValue("admin")] string user,
            [DefaultValue("admin")] string pass,
            [DefaultValue("test")] string db,
            [DefaultValue(5)] int influxFreq,
            [DefaultValue(5)] int consoleFreq,
            [DefaultValue(false)] bool ssl,
            [DefaultValue(false)] bool compact,
            [DefaultValue(false)] bool verbose)
        {
            Metric.Config.WithReporting(report => {
                report.WithInflux(host, port, user, pass, db, TimeSpan.FromSeconds(influxFreq), new ConfigOptions {
                    UseHttps            = ssl,
                    MetricNameConverter = compact ? ConfigOptions.CompactConverter : ConfigOptions.IdentityConverter,
                    Verbose             = verbose
                });

                report.WithConsoleReport(TimeSpan.FromSeconds(consoleFreq));
            });

            var rand = new Random(Guid.NewGuid().GetHashCode());

            while (true)
            {
                var t = Metric.Timer("my.timer", Unit.Events).NewContext();

                Metric.Counter("my.counter", Unit.Events).Increment();
                Metric.Counter("my.counter", Unit.Events).Increment("my-val-1");
                Metric.Counter("my.counter", Unit.Events).Increment("my-val-2");

                Metric.Meter("my.meter", Unit.Events).Mark(rand.Next(0, 1000));

                Metric.Gauge("my.gauge", rand.NextDouble, Unit.Custom("cpu%"));

                Thread.Sleep(rand.Next(500, 3000));

                t.Dispose();
            }
        }
Пример #9
0
        public void Decrement_child(string name, string name2)
        {
            var     counter = Metric.Counter(name, name);
            dynamic count   = counter;
            var     test    = Create(counter);

            test.Increment(name2);
            test.Increment(name2);
            test.Increment(name2);
            Assert.Equal(3, count.Value.Count);
            Assert.Equal(3, count.Value.Items[0].Count);
            test.Decrement(name2);
            Assert.Equal(2, count.Value.Count);
            Assert.Equal(2, count.Value.Items[0].Count);
            test.Decrement(name2);
            Assert.Equal(1, count.Value.Count);
            Assert.Equal(1, count.Value.Items[0].Count);
            test.Decrement(name2);
            Assert.Equal(0, count.Value.Count);
            Assert.Equal(0, count.Value.Items[0].Count);
        }
Пример #10
0
        static void CounterTest()
        {
            var counter = Metric.Counter("test.counter", Unit.Custom("并发"));

            Action doWork  = () => { System.Threading.Thread.Sleep(ran.Next(10, 300)); };
            Action idlesse = () => { System.Threading.Thread.Sleep(ran.Next(0, 500)); };

            for (var i = 0; i < 20; i++)
            {
                Task.Run(() =>
                {
                    while (true)
                    {
                        counter.Increment();
                        doWork();
                        counter.Decrement();
                        idlesse();
                    }
                });
            }
        }
Пример #11
0
        public void Smoke_test_for_counter()
        {
            Metric.Config
            .WithReporting(r => (reports = r).WithElasticSearchReport(
                               TimeSpan.FromSeconds(1), sutConfigurer
                               ));
            Counter c = Metric.Counter("test", Unit.Calls);

            c.Increment(3);
            Thread.Sleep(2000);
            c.Increment(5);
            Thread.Sleep(2000);

            var records = base.GetAllCounters("es-test", "test");

            Assert.That(records.Any(r => r.Value <Int32>("Count") == 3));
            Assert.That(records.Any(r => r.Value <Int32>("Count") == 8));

            records = base.GetAllCountersDiff("es-test", "test");
            Assert.That(records.Any(r => r.Value <Int32>("Count") == 3));
            Assert.That(records.Any(r => r.Value <Int32>("Count") == 5));
        }
Пример #12
0
        public void AggregateCounters()
        {
            var buffer = new MetricsBuffer();

            Func <string, float?> counter = s => {
                var value = buffer.Counters.SingleOrDefault(kvp => kvp.Key == s);
                return(value.Equals(default(KeyValuePair <string, float>)) ? (float?)null : value.Value);
            };

            buffer.Aggregate(Metric.Counter("a", 10));
            buffer.Aggregate(Metric.Counter("b", 0));

            // ReSharper disable PossibleInvalidOperationException
            Assert.AreEqual(10, counter("a").Value);
            Assert.AreEqual(0, counter("b").Value);

            buffer.Aggregate(Metric.Counter("a", 1));
            buffer.Aggregate(Metric.Counter("b", -1));

            Assert.AreEqual(11, counter("a").Value);
            Assert.AreEqual(-1, counter("b").Value);
            // ReSharper restore PossibleInvalidOperationException
        }
Пример #13
0
        static void Main(string[] args)
        {
            Metric.Config
            //.WithAllCounters()
            .WithReporting(config => config
                           .WithConsoleReport(TimeSpan.FromSeconds(30))
                           .WithApplicationInsights(ConfigurationManager.AppSettings["AI_InstrumentationKey"].ToString(),
                                                    Guid.NewGuid().ToString(), TimeSpan.FromSeconds(30))
                           );

            // create some metrics
            Counter counter = Metric.Counter("MyItemCounter", Unit.Items);

            counter.Increment();
            counter.Increment();
            counter.Increment();


            Counter counterSuppressed = Metric.Counter("MySuppressedCounter", Unit.Items, ApplicationInsightsReport.DoNotReport);

            counterSuppressed.Increment();

            Metric.Gauge("MyPercentageGauge", () => DateTime.Now.Second, Unit.Percent);

            Meter meter = Metric.Meter("MyErrorsPerSecMeter", Unit.Errors, TimeUnit.Seconds);

            meter.Mark();
            meter.Mark();
            meter.Mark();
            meter.Mark();
            meter.Mark();
            meter.Mark();
            meter.Mark();
            meter.Mark();
            meter.Mark();
            meter.Mark();

            Histogram histogram = Metric.Histogram("MyItemsHistogram", Unit.Items);

            histogram.Update(456);
            histogram.Update(123);
            histogram.Update(789);

            HealthChecks.RegisterHealthCheck("Seconds", () =>
            {
                if (DateTime.Now.Second > 55)
                {
                    return(HealthCheckResult.Unhealthy("Seconds > 55"));
                }
                else
                {
                    return(HealthCheckResult.Healthy("Seconds <= 55"));
                }
            });
            HealthChecks.RegisterHealthCheck("AlwaysTrue", () => HealthCheckResult.Healthy("Always True check"));

            var rnd = new Random();

            Metrics.Timer timer = Metric.Timer("MyEventsTimer", Unit.Events);
            for (int i = 0; i < 3; i++)
            {
                using (var context = timer.NewContext("TimerCtx"))
                {
                    Thread.Sleep(132 + rnd.Next(15));
                }
            }

            Console.WriteLine("Press any key to end...");
            Console.ReadKey();
        }
Пример #14
0
 public void Counter(string name, int value = 1)
 {
     Metric.Counter(name, Unit.None).Increment();
 }
Пример #15
0
        public async Task CounterAsync(string measurement, double value, double sample = 1, Func <ITagBuilder, ITagBuilder> builder = null)
        {
            var tags = BuildTags(builder);

            await PublishAsync(Metric.Counter(measurement, value, sample, tags));
        }
Пример #16
0
        public void Counter(string measurement, double value, double sample = 1, Func <ITagBuilder, ITagBuilder> builder = null)
        {
            var tags = BuildTags(builder);

            Publish(Metric.Counter(measurement, value, sample, tags));
        }
Пример #17
0
 public MetricTests()
 {
     _counterMetric  = Metric.Counter(MetricCounterName, Unit.None);
     _timerMetric    = Metric.Timer(MetricTimerName);
     _discreteMetric = Metric.Discrete <int>(MetricDiscreteName, Unit.None);
 }
Пример #18
0
        public void PriorCounterReturnedTest()
        {
            var metric2 = Metric.Counter(MetricCounterName, Unit.None);

            Assert.Same(_counterMetric, metric2);
        }
 public void DecrementMetric(string name)
 {
     Metric.Counter(name, Unit.None).Decrement();
 }
 public IdentityPlayerController(IIdentityPlayerService identityPlayerService)
 {
     _identityPlayerService = identityPlayerService;
     _mapper  = new MapperConfiguration(cfg => cfg.CreateMap <Player, PlayerViewModel>()).CreateMapper();
     _counter = Metric.Counter("IdentityPlayerController.GetIdentityPlayer", Unit.Calls);
 }
Пример #21
0
 public Task CounterAsync(string name, int value = 1)
 {
     Metric.Counter(name, Unit.None).Increment();
     return(TaskHelper.Completed());
 }
Пример #22
0
 public Task CounterAsync(string statName, int value = 1)
 {
     Metric.Counter(statName, Unit.None, "foundatio").Increment();
     return(TaskHelper.Completed());
 }
 public void DecrementMetric(string name, double value)
 {
     Metric.Counter(name, Unit.None).Decrement((long)value);
 }
Пример #24
0
        //
        // Summary:
        //     A counter is a simple incrementing and decrementing 64-bit integer. Ex number
        //     of active requests.
        //
        // Parameters:
        //   name:
        //     Name of the metric. Must be unique across all counters in this context.
        //
        //   unit:
        //     Description of what the is being measured ( Unit.Requests , Unit.Items etc )
        //     .
        //
        //   tags:
        //     Optional set of tags that can be associated with the metric. Tags can be string
        //     array or comma separated values in a string. ex: tags: "tag1,tag2" or tags: new[]
        //     {"tag1", "tag2"}
        //
        // Returns:
        //     Reference to the metric
        public static Counter Counter(string name, Unit unit, MetricTags tags = default(MetricTags))
        {
            var metricTags = GenerateTags(tags);

            return(Metric.Counter(name, unit, metricTags));
        }
Пример #25
0
 // Command Counter
 //      Count = 2550 Commands
 //Total Items = 5
 //     Item 0 = 20.90%   533 Commands [BillCustomer]
 //     Item 1 = 19.22%   490 Commands [MakeInvoice]
 //     Item 2 = 19.41%   495 Commands [MarkAsPreffered]
 //     Item 3 = 20.98%   535 Commands [SendEmail]
 //     Item 4 = 19.49%   497 Commands [ShipProduct]
 public static Counter Counter(string name, Unit unit, MetricTags tags)
 {
     return(Metric.Counter(name, unit, tags));
 }