示例#1
0
        public void Logger_Stats_MessageSizeLimit()
        {
            const string    testName    = "Logger_Stats_MessageSizeLimit";
            TestLogConsumer logConsumer = new TestLogConsumer();

            TraceLogger.LogConsumers.Add(logConsumer);
            TraceLogger logger1 = TraceLogger.GetLogger(testName);

            const string StatsCounterBaseName = "LoggerTest.Stats.Size";

            for (int i = 1; i <= 1000; i++)
            {
                StatisticName    counterName = new StatisticName(StatsCounterBaseName + "." + i);
                CounterStatistic ctr         = CounterStatistic.FindOrCreate(counterName);
                ctr.IncrementBy(i);
            }

            LogStatistics statsLogger = new LogStatistics(TimeSpan.Zero, true);

            statsLogger.DumpCounters().Wait();

            int count = logConsumer.GetEntryCount((int)ErrorCode.PerfCounterDumpAll);

            Console.WriteLine(count + " stats log message entries written");
            Assert.IsTrue(count > 1, "Should be some stats log message entries - saw " + count);
            Assert.AreEqual(0, logConsumer.GetEntryCount((int)ErrorCode.Logger_LogMessageTruncated), "Should not see any 'Message truncated' message");
        }
 public void Counter_InitialValue()
 {
     StatisticName name = new StatisticName(CounterName);
     ICounter<long> ctr = CounterStatistic.FindOrCreate(name);
     Assert.Equal(name.ToString(), ctr.Name);
     Assert.True(ctr.ToString().Contains(name.Name));
     Assert.Equal(0, ctr.GetCurrentValue());
 }
 public void Counter_Increment()
 {
     StatisticName name = new StatisticName(CounterName);
     CounterStatistic ctr = CounterStatistic.FindOrCreate(name);
     Assert.Equal(0, ctr.GetCurrentValue());
     ctr.Increment();
     Assert.Equal(1, ctr.GetCurrentValue());
 }
 public void Counter_SetValue()
 {
     StatisticName name = new StatisticName(CounterName);
     int val = random.Next(1000000);
     CounterStatistic ctr = CounterStatistic.FindOrCreate(name);
     ctr.IncrementBy(val);
     Assert.Equal(val, ctr.GetCurrentValue());
 }
        private void WriteMetric(string name, UpdateMode mode = UpdateMode.Increment, double?value = null)
        {
            if (!isInitialized.Value)
            {
                return;
            }

            PerfCounterConfigData cd = GetCounter(name);

            if (cd == null || cd.PerfCounter == null)
            {
                return;
            }

            StatisticName statsName       = cd.Name;
            string        perfCounterName = GetPerfCounterName(cd);

            try
            {
                if (logger.IsVerbose3)
                {
                    logger.Verbose3(ErrorCode.PerfCounterWriting, "Writing perf counter {0}", perfCounterName);
                }

                switch (mode)
                {
                case UpdateMode.Increment:
                    if (value.HasValue)
                    {
                        cd.PerfCounter.IncrementBy((long)value.Value);
                    }
                    else
                    {
                        cd.PerfCounter.Increment();
                    }
                    break;

                case UpdateMode.Decrement:
                    if (value.HasValue)
                    {
                        cd.PerfCounter.RawValue = cd.PerfCounter.RawValue - (long)value.Value;
                    }
                    else
                    {
                        cd.PerfCounter.Decrement();
                    }
                    break;

                case UpdateMode.Set:
                    cd.PerfCounter.RawValue = (long)value.Value;
                    break;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ErrorCode.PerfCounterUnableToWrite, string.Format("Unable to write to Windows perf counter '{0}'", statsName), ex);
            }
        }
        public static int WriteCounters()
        {
            if (logger.IsVerbose)
            {
                logger.Verbose("Writing Windows perf counters.");
            }

            int numWriteErrors = 0;

            foreach (PerfCounterConfigData cd in perfCounterData)
            {
                StatisticName name            = cd.Name;
                string        perfCounterName = GetPerfCounterName(cd);

                try
                {
                    if (cd.PerfCounter == null)
                    {
                        if (logger.IsVerbose)
                        {
                            logger.Verbose(ErrorCode.PerfCounterUnableToConnect, "No perf counter found for {0}", name);
                        }
                        cd.PerfCounter = CreatePerfCounter(perfCounterName);
                    }

                    if (cd.CounterStat == null)
                    {
                        if (logger.IsVerbose)
                        {
                            logger.Verbose(ErrorCode.PerfCounterRegistering, "Searching for statistic {0}", name);
                        }
                        ICounter <long> ctr = IntValueStatistic.Find(name);
                        cd.CounterStat = ctr ?? CounterStatistic.FindOrCreate(name);
                    }

                    long val;
                    //if (cd.UseDeltaValue)
                    //{
                    //    ((CounterStatistic)cd.CounterStat).GetCurrentValueAndDeltaAndResetDelta(out val);
                    //}
                    //else
                    {
                        val = cd.CounterStat.GetCurrentValue();
                    }
                    if (logger.IsVerbose3)
                    {
                        logger.Verbose3(ErrorCode.PerfCounterWriting, "Writing perf counter {0} Value={1}", perfCounterName, val);
                    }
                    cd.PerfCounter.RawValue = val;
                }
                catch (Exception ex)
                {
                    numWriteErrors++;
                    logger.Error(ErrorCode.PerfCounterUnableToWrite, string.Format("Unable to write to Windows perf counter '{0}'", name), ex);
                }
            }
            return(numWriteErrors);
        }
 public void Counter_DecrementBy()
 {
     StatisticName name = new StatisticName(CounterName);
     int startValue = 10;
     int newValue = startValue - 1;
     CounterStatistic ctr = CounterStatistic.FindOrCreate(name);
     ctr.IncrementBy(startValue);
     Assert.Equal(startValue, ctr.GetCurrentValue());
     ctr.DecrementBy(1);
     Assert.Equal(newValue, ctr.GetCurrentValue());
 }
 public void Counter_IncrementFromMinInt()
 {
     StatisticName name = new StatisticName(CounterName);
     int val = int.MinValue;
     CounterStatistic ctr = CounterStatistic.FindOrCreate(name);
     ctr.IncrementBy(val);
     Assert.Equal(val, ctr.GetCurrentValue());
     ctr.Increment();
     Assert.Equal(val + 1, ctr.GetCurrentValue());
     ctr.Increment();
     Assert.Equal(val + 2, ctr.GetCurrentValue());
 }
示例#9
0
        public void Counter_IncrementFromMaxInt()
        {
            StatisticName name    = new StatisticName("Counter6");
            int           val     = int.MaxValue;
            long          longVal = int.MaxValue;

            Assert.AreEqual(longVal, val);
            CounterStatistic ctr = CounterStatistic.FindOrCreate(name);

            ctr.IncrementBy(val);
            Assert.AreEqual(val, ctr.GetCurrentValue());
            ctr.Increment();
            Assert.AreEqual(longVal + 1, ctr.GetCurrentValue());
            ctr.Increment();
            Assert.AreEqual(longVal + 2, ctr.GetCurrentValue());
        }
        private bool Initialize()
        {
            try
            {
                // (1) Start with list of static counters
                var newPerfCounterData = new List <PerfCounterConfigData>(PerfCounterConfigData.StaticPerfCounters);

                // TODO: get rid of this static access. Telemetry consumers now allow being injected with dependencies, so extract it as such
                var grainTypes = LogManager.GrainTypes;
                if (grainTypes != null)
                {
                    // (2) Then search for grain DLLs and pre-create activation counters for any grain types found
                    foreach (var grainType in grainTypes)
                    {
                        var counterName = new StatisticName(StatisticNames.GRAIN_COUNTS_PER_GRAIN, grainType);
                        newPerfCounterData.Add(new PerfCounterConfigData {
                            Name = counterName, UseDeltaValue = false
                        });
                    }

                    this.initializedGrainCounters = true;
                }

                if (!this.isInstalling)
                {
                    foreach (var cd in newPerfCounterData)
                    {
                        var perfCounterName = GetPerfCounterName(cd);
                        cd.PerfCounter = CreatePerfCounter(perfCounterName);
                    }
                }

                lock (this.initializationLock)
                {
                    this.perfCounterData.Clear();
                    this.perfCounterData.AddRange(newPerfCounterData);
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }
        public static int WriteCounters()
        {
            if (logger.IsVerbose)
            {
                logger.Verbose("Writing counters.");
            }

            int numWriteErrors = 0;

            foreach (CounterConfigData cd in CounterConfigData.StaticCounters)
            {
                StatisticName name            = cd.Name;
                string        perfCounterName = GetPerfCounterName(cd);

                try
                {
                    if (logger.IsVerbose3)
                    {
                        logger.Verbose3(ErrorCode.PerfCounterWriting, "Writing counter {0}", perfCounterName);
                    }

                    if (cd.CounterStat == null)
                    {
                        if (logger.IsVerbose)
                        {
                            logger.Verbose(ErrorCode.PerfCounterRegistering, "Searching for statistic {0}", name);
                        }
                        ICounter <long> ctr = IntValueStatistic.Find(name);
                        cd.CounterStat = ctr ?? CounterStatistic.FindOrCreate(name);
                    }

                    if (cd.CounterStat != null)
                    {
                        logger.TrackMetric(perfCounterName, cd.CounterStat.GetCurrentValue());
                    }
                }
                catch (Exception ex)
                {
                    numWriteErrors++;
                    logger.Error(ErrorCode.PerfCounterUnableToWrite, string.Format("Unable to write to counter '{0}'", name), ex);
                }
            }
            return(numWriteErrors);
        }
示例#12
0
    /// <summary>Uses reflection to create an Orleans statistic with a float value.</summary>
    /// <param name="statisticName">The Orleans statistic name.</param>
    /// <param name="fetcher">The function used to get the next float value.</param>
    private static void FindOrCreateFloatValueStatistic(StatisticName statisticName, Func <float> fetcher)
    {
        var coreAssembly = typeof(ICounter).Assembly;

        var floatValueStatisticType = coreAssembly.GetType("Orleans.Runtime.FloatValueStatistic");

        if (floatValueStatisticType == null)
        {
            throw new PlatformNotSupportedException("The Orleans.Runtime.FloatValueStatistic type does not exist");
        }

        var findOrCreateMethod = floatValueStatisticType.GetMethods().Single(method => method.IsPublic && method.IsStatic && "FindOrCreate".Equals(method.Name, StringComparison.Ordinal) && method.GetParameters().Length == 2);

        if (findOrCreateMethod == null)
        {
            throw new PlatformNotSupportedException("The FindOrCreate method of the Orleans.Runtime.FloatValueStatistic type does not exist");
        }

        findOrCreateMethod.Invoke(null, new object[] { statisticName, fetcher });
    }
        internal static void GetCounterData()
        {
            perfCounterData.Clear();

            // (1) Start with list of static counters
            perfCounterData.AddRange(PerfCounterConfigData.StaticPerfCounters);

            // (2) Then search for grain DLLs and pre-create activation counters for any grain types found
            var loadedGrainClasses = GrainTypeManager.Instance.GrainClassTypeData;
            foreach (var grainClass in loadedGrainClasses)
            {
                var counterName = new StatisticName(StatisticNames.GRAIN_COUNTS_PER_GRAIN, grainClass.Key);
                perfCounterData.Add(new PerfCounterConfigData
                {
                    Name = counterName,
                    UseDeltaValue = false,
                    CounterStat = CounterStatistic.FindOrCreate(counterName, false),
                });
            }
        }
示例#14
0
    /// <summary>Uses reflection to create an Orleans statistic with an integer value.</summary>
    /// <param name="statisticName">The Orleans statistic name.</param>
    /// <param name="fetcher">The function used to get the next integer value.</param>
    private static void FindOrCreateIntValueStatistic(StatisticName statisticName, Func <long> fetcher)
    {
        var coreAssembly = typeof(ICounter).Assembly;

        var intValueStatisticType = coreAssembly.GetType("Orleans.Runtime.IntValueStatistic");

        if (intValueStatisticType == null)
        {
            throw new PlatformNotSupportedException("The Orleans.Runtime.IntValueStatistic type does not exist");
        }

        var findOrCreateMethod = intValueStatisticType.GetMethod("FindOrCreate", BindingFlags.Public | BindingFlags.Static);

        if (findOrCreateMethod == null)
        {
            throw new PlatformNotSupportedException("The FindOrCreate method of the Orleans.Runtime.IntValueStatistic type does not exist");
        }

        findOrCreateMethod.Invoke(null, new object[] { statisticName, fetcher, Type.Missing });
    }
        internal static void GetCounterData()
        {
            perfCounterData.Clear();

            // (1) Start with list of static counters
            perfCounterData.AddRange(PerfCounterConfigData.StaticPerfCounters);

            // (2) Then search for grain DLLs and pre-create activation counters for any grain types found
            var loadedGrainClasses = GrainTypeManager.Instance.GrainClassTypeData;

            foreach (var grainClass in loadedGrainClasses)
            {
                var counterName = new StatisticName(StatisticNames.GRAIN_COUNTS_PER_GRAIN, grainClass.Key);
                perfCounterData.Add(new PerfCounterConfigData
                {
                    Name          = counterName,
                    UseDeltaValue = false,
                    CounterStat   = CounterStatistic.FindOrCreate(counterName, false),
                });
            }
        }
示例#16
0
        public void Logger_Stats_MessageSizeLimit()
        {
            const string    testName    = "Logger_Stats_MessageSizeLimit";
            TestLogConsumer logConsumer = new TestLogConsumer(output);

            LogManager.LogConsumers.Add(logConsumer);
            Logger logger1 = LogManager.GetLogger(testName);

            const string StatsCounterBaseName = "LoggerTest.Stats.Size";

            var createdCounters = new List <string>();

            try
            {
                for (int i = 1; i <= 1000; i++)
                {
                    string           name        = StatsCounterBaseName + "." + i;
                    StatisticName    counterName = new StatisticName(name);
                    CounterStatistic ctr         = CounterStatistic.FindOrCreate(counterName);
                    ctr.IncrementBy(i);
                    createdCounters.Add(name);
                }

                LogStatistics statsLogger = new LogStatistics(TimeSpan.Zero, true);
                statsLogger.DumpCounters().Wait();

                int count = logConsumer.GetEntryCount((int)ErrorCode.PerfCounterDumpAll);
                output.WriteLine(count + " stats log message entries written");
                Assert.True(count > 1, "Should be some stats log message entries - saw " + count);
                Assert.Equal(0, logConsumer.GetEntryCount((int)ErrorCode.Logger_LogMessageTruncated));   //  "Should not see any 'Message truncated' message"
            }
            finally
            {
                createdCounters.ForEach(name => CounterStatistic.Delete(name));
            }
        }
        private void WriteMetric(string name, UpdateMode mode = UpdateMode.Increment, double?value = null)
        {
            if (!this.isInitialized.Value)
            {
                return;
            }

            // Attempt to initialize grain-specific counters if they haven't been initialized yet.
            if (!this.initializedGrainCounters)
            {
                this.Initialize();
            }

            PerfCounterConfigData cd = GetCounter(name);

            if (cd == null || cd.PerfCounter == null)
            {
                return;
            }

            StatisticName statsName       = cd.Name;
            string        perfCounterName = GetPerfCounterName(cd);

            try
            {
                if (this.logger.IsEnabled(LogLevel.Trace))
                {
                    this.logger.Trace(ErrorCode.PerfCounterWriting, "Writing perf counter {0}", perfCounterName);
                }

                switch (mode)
                {
                case UpdateMode.Increment:
                    if (value.HasValue)
                    {
                        cd.PerfCounter.IncrementBy((long)value.Value);
                    }
                    else
                    {
                        cd.PerfCounter.Increment();
                    }
                    break;

                case UpdateMode.Decrement:
                    if (value.HasValue)
                    {
                        cd.PerfCounter.RawValue = cd.PerfCounter.RawValue - (long)value.Value;
                    }
                    else
                    {
                        cd.PerfCounter.Decrement();
                    }
                    break;

                case UpdateMode.Set:
                    cd.PerfCounter.RawValue = (long)value.Value;
                    break;
                }
            }
            catch (Exception ex)
            {
                this.logger.Error(ErrorCode.PerfCounterUnableToWrite, string.Format("Unable to write to Windows perf counter '{0}'", statsName), ex);
            }
        }