Exemple #1
0
        /// <summary>
        /// Record an event metric using a programmatic declaration
        /// </summary>
        /// <param name="pagesLoaded"></param>
        public static void RecordCacheMetric(int pagesLoaded)
        {
            EventMetricDefinition cacheMetric;

            //so we can be called multiple times we want to see if the definition already exists.
            if (EventMetricDefinition.TryGetValue("GibraltarSample", "Database.Engine", "Cache", out cacheMetric) == false)
            {
                cacheMetric = new EventMetricDefinition("GibraltarSample", "Database.Engine", "Cache");

                //add the values (that are part of the definition)
                cacheMetric.AddValue("pages", typeof(int), SummaryFunction.Average, "Pages", "Pages in Cache", "Total number of pages in cache");
                cacheMetric.AddValue("size", typeof(int), SummaryFunction.Average, "Bytes", "Cache Size", "Total number of bytes used by pages in cache");

                //and now that we're done, we need to register this definition.  This locks the definition
                //and makes it go live.  Note that it's based by ref because if another thread registered the same metric, we'll get the
                //registered object (whoever one the race), not necessarily the one we've just created to pass in.
                EventMetricDefinition.Register(ref cacheMetric);
            }

            //Now we can get the specific metric we want to record samples under (this is an instance of the definition)
            EventMetric cacheEventMetric = EventMetric.Register(cacheMetric, null);

            //now go ahead and write that sample.
            EventMetricSample newSample = cacheEventMetric.CreateSample();

            newSample.SetValue("pages", pagesLoaded);
            newSample.SetValue("size", pagesLoaded * 8196);
            newSample.Write();
        }
Exemple #2
0
        public void EventMetricsByMethodsPerformanceTest()
        {
            EventMetricDefinition eventDefinition;

            if (false == EventMetricDefinition.TryGetValue("PerformanceTestsMetrics", "Performance.EventMetrics.Methods", "UserEvent", out eventDefinition))
            {
                eventDefinition             = new EventMetricDefinition("PerformanceTestsMetrics", "Performance.EventMetrics.Methods", "UserEvent");
                eventDefinition.Caption     = "User Event";
                eventDefinition.Description = "Unit test event metric with typical data.";
                eventDefinition.AddValue("fileName", typeof(string), SummaryFunction.Count, null, "File name", "The name of the file");
                eventDefinition.AddValue("operation", typeof(UserFileOperation), SummaryFunction.Count, null, "Operation", "The type of file operation being performed.");
                eventDefinition.AddValue("duration", typeof(TimeSpan), SummaryFunction.Average, "ms", "Duration", "The duration for this file operation.");
                EventMetricDefinition.Register(ref eventDefinition, "duration");
            }

            Assert.IsNotNull(eventDefinition);
            Assert.IsTrue(eventDefinition.IsReadOnly);

            Trace.TraceInformation("Event metric definition registered by methods.");

            EventMetric eventMetric = EventMetric.Register(eventDefinition, "MethodsPerformanceTest");

            Assert.IsNotNull(eventMetric);

            string         fileName       = @"C:\Dummy\File\Name.txt";
            DateTimeOffset operationStart = DateTimeOffset.UtcNow;
            DateTimeOffset operationEnd   = operationStart.AddMilliseconds(1234);

            //first, lets get everything to flush so we have our best initial state.
            Log.Information(LogWriteMode.WaitForCommit, "Test.Agent.Metrics.Performance", "Preparing for Test", "Flushing queue");

            //now that we know it's flushed everything, lets do our timed loop.
            DateTimeOffset startTime = DateTimeOffset.UtcNow;

            for (int curMessage = 0; curMessage < LoopsPerEventTest; curMessage++)
            {
                EventMetricSample eventSample = eventMetric.CreateSample();
                eventSample.SetValue("fileName", fileName);
                eventSample.SetValue("operation", UserFileOperation.Write);
                eventSample.SetValue("duration", operationEnd - operationStart);
                eventSample.Write();
            }
            DateTimeOffset messageEndTime = DateTimeOffset.UtcNow;

            //one wait for commit message to force the buffer to flush.
            Log.Information(LogWriteMode.WaitForCommit, "Test.Agent.Metrics.Performance", "Waiting for Samples to Commit", null);

            //and store off our time
            DateTimeOffset endTime = DateTimeOffset.UtcNow;

            TimeSpan  testDuration    = endTime - startTime;
            TimeSpan  loopDuration    = messageEndTime - startTime;
            const int messagesPerTest = LoopsPerEventTest * MessagesPerEventLoop;

            Trace.TraceInformation("Event Metrics by Methods Test committed {0:N0} events in {1:F3} ms (average {2:F4} ms per message).  Average loop time {3:F4} ms ({4} values per message) and final flush time {5:F3} ms.",
                                   messagesPerTest, testDuration.TotalMilliseconds, (testDuration.TotalMilliseconds / messagesPerTest),
                                   (loopDuration.TotalMilliseconds / LoopsPerEventTest), ValuesPerEventMessage,
                                   (endTime - messageEndTime).TotalMilliseconds);
        }
Exemple #3
0
        private static EventMetricDefinition DefineRequestMetric(string applicationName)
        {
            var definition = new EventMetricDefinition(applicationName, "AspNetCore", "Request");

            definition.AddValue("request", typeof(string), SummaryFunction.Count, "Request", "Request",
                                "The query that was executed.");
            definition.AddValue("duration", typeof(TimeSpan), SummaryFunction.Average, "Duration", "Duration",
                                "Time taken to execute the query");
            definition.AddValue("error", typeof(string), SummaryFunction.Count, "Errors", "Errors",
                                "Errors executing query.");
            EventMetricDefinition.Register(ref definition);
            return(definition);
        }
Exemple #4
0
        private static EventMetricDefinition DefineRequestMetric()
        {
            var definition = new EventMetricDefinition(Constants.LogSystem, Constants.MetricCategory, "Request");

            definition.AddValue("request", typeof(string), SummaryFunction.Count, "Request", "Request",
                                "The query that was executed.");
            definition.AddValue("duration", typeof(TimeSpan), SummaryFunction.Average, "Duration", "Duration",
                                "Time taken to execute the query");
            definition.AddValue("error", typeof(string), SummaryFunction.Count, "Errors", "Errors",
                                "Errors executing query.");
            EventMetricDefinition.Register(ref definition);
            return(definition);
        }
        private void SynchronizedMetricRegistration()
        {
            string name = Thread.CurrentThread.Name;

            Trace.TraceInformation("{0} started", name);
            EventMetricDefinition newDefinition = new EventMetricDefinition("EventMetricTests", "Gibraltar.Monitor.Test", "Sync");

            newDefinition.AddValue("delta", typeof(double), SummaryFunction.RunningSum, null, "Delta", "The applied delta");

            try
            {
                Interlocked.Increment(ref m_ThreadCounter);
                lock (m_SyncLock)
                {
                    // Do nothing, just release it immediately.
                }

                EventMetricDefinition.Register(ref newDefinition);

                EventMetric metric = EventMetric.Register(newDefinition, name);

                Trace.TraceInformation("{0} completed registration of event metric", name);

                EventMetricSample sample = metric.CreateSample();
                sample.SetValue("delta", Thread.CurrentThread.ManagedThreadId);
                sample.Write();
            }
            catch (Exception ex)
            {
                m_ThreadFailed = true;
                Trace.TraceError("{0} got {1}: {2}", name, ex.GetType().Name, ex.Message, ex);
            }

            Interlocked.Decrement(ref m_ThreadCounter);
        }
        /// <summary>
        /// Perform the one-time work to create a metric
        /// </summary>
        /// <param name="name">Dot-delimited display name for this metric</param>
        private void InitializeMetric(string name)
        {
            // Handle edge cases
            if (string.IsNullOrEmpty(name))
            {
                name = DefaultInstance;
            }

            name = name.Trim();
            while (!string.IsNullOrEmpty(name) && name[0] == '.')
            {
                name = name.Substring(1).Trim();
            }

            // Set up category and instance as Loupe wants
            string category;
            string instance;

            var pos = name.LastIndexOf('.'); // check for delimited name

            if (pos <= 0)
            {
                // If not deliminated, just use base category
                category = RootCategory;
                instance = name;
            }
            else
            {
                // If delimited, just use the last part as instance name
                // and combine the rest with category
                category = RootCategory + '.' + name.Substring(0, pos);
                instance = name.Substring(pos + 1);
            }

            _logCategory = category + "." + instance; // Initializ category for logging

            EventMetricDefinition metricDefinition;

            // Create the metric on first call then use cached copy thereafter
            if (!EventMetricDefinition.TryGetValue(MetricSystem, category, instance, out metricDefinition))
            {
                metricDefinition = new EventMetricDefinition(MetricSystem, category, instance);
                metricDefinition.AddValue(DurationCaption, typeof(TimeSpan), SummaryFunction.Average,
                                          null, DurationCaption, null);
                EventMetricDefinition.Register(ref metricDefinition);
            }

            // Grab the metric from cache
            Metric = EventMetric.Register(metricDefinition, null);
        }
        /// <summary>
        /// Helper function to retrieve the desired EventMetric
        /// </summary>
        private static EventMetric GetMetric(string caption, string category, string instance)
        {
            EventMetricDefinition cacheMetric;

            //so we can be called multiple times we want to see if the definition already exists.
            if (EventMetricDefinition.TryGetValue("PostSharp", category, caption, out cacheMetric) == false)
            {
                cacheMetric = new EventMetricDefinition("PostSharp", category, caption);

                //add the values (that are part of the definition)
                cacheMetric.DefaultValue = cacheMetric.AddValue("duration", typeof(TimeSpan), SummaryFunction.Average,
                                                                "ms", "Duration", "Average execution duration");
                cacheMetric.AddValue("namespace", typeof(string), SummaryFunction.Count, "", "Namespace", "Namespace");
                cacheMetric.AddValue("class", typeof(string), SummaryFunction.Count, "", "Class",
                                     "Class name ignoring namespace");
                cacheMetric.AddValue("method", typeof(string), SummaryFunction.Count, "", "Method",
                                     "Method name ignoring class and namespace");
                cacheMetric.AddValue("fullname", typeof(string), SummaryFunction.Count, "", "FullName",
                                     "Fully qualified name of the class and method");
                EventMetricDefinition.Register(ref cacheMetric);
            }

            EventMetric cacheEventMetric = EventMetric.Register(cacheMetric, instance);
            return cacheEventMetric;
        }
 public static EventMetricDefinition AddCount(this EventMetricDefinition definition, string name, string caption, string description)
 {
     definition.AddValue(name, typeof(string), SummaryFunction.Count, null, caption, description);
     return(definition);
 }
 public static EventMetricDefinition AddFlag(this EventMetricDefinition definition, string name, string caption,
                                             string description)
 {
     definition.AddValue(name, typeof(bool), SummaryFunction.Average, "Hits", caption, description);
     return(definition);
 }
 public static EventMetricDefinition AddDuration(this EventMetricDefinition definition, string name, string caption, string description)
 {
     definition.AddValue(name, typeof(double), SummaryFunction.Average, "Milliseconds", caption, description);
     return(definition);
 }
Exemple #11
0
        /// <summary>
        /// Perform the one-time work to create a metric
        /// </summary>
        /// <param name="name">Dot-delimited display name for this metric</param>
        private void InitializeMetric(string name)
        {
            // Handle edge cases
            if (string.IsNullOrEmpty(name))
                name = DefaultInstance;

            name = name.Trim();
            while (!string.IsNullOrEmpty(name) && name[0] == '.')
                name = name.Substring(1).Trim();

            // Set up category and instance as Loupe wants
            string category;
            string instance;

            var pos = name.LastIndexOf('.'); // check for delimited name
            if (pos <= 0)
            {
                // If not deliminated, just use base category
                category = RootCategory;
                instance = name;
            }
            else
            {
                // If delimited, just use the last part as instance name
                // and combine the rest with category
                category = RootCategory + '.' + name.Substring(0, pos);
                instance = name.Substring(pos + 1);
            }

            _logCategory = category + "." + instance; // Initializ category for logging

            EventMetricDefinition metricDefinition;

            // Create the metric on first call then use cached copy thereafter
            if (!EventMetricDefinition.TryGetValue(MetricSystem, category, instance, out metricDefinition))
            {
                metricDefinition = new EventMetricDefinition(MetricSystem, category, instance);
                metricDefinition.AddValue(DurationCaption, typeof(TimeSpan), SummaryFunction.Average,
                    null, DurationCaption, null);
                EventMetricDefinition.Register(ref metricDefinition);
            }

            // Grab the metric from cache
            Metric = EventMetric.Register(metricDefinition, null);
        }
        public void Setup()
        {
            // This is an example of how to create an event metric definition programatically.

            EventMetricDefinition newMetricDefinition;

            // Define an event metric manually (the long way).  First, see if it's already registered...
            if (EventMetricDefinition.TryGetValue("EventMetricTests", "Gibraltar.Monitor.Test", "Manual", out newMetricDefinition) == false)
            {
                // It's not registered yet, so we need to fill out the template to define it.
                EventMetricDefinition newEventMetric = new EventMetricDefinition("EventMetricTests", "Gibraltar.Monitor.Test", "Manual");

                // Now we want to add a few value columns to make it useful.
                // NOTE:  This is designed to exactly match UserEventObject for convenience in analzing results.
                // The dummy data values we're using are unitless, so we'll just use null for the required unitCaption parameter.
                newEventMetric.AddValue("short_average", typeof(short), SummaryFunction.Average, null, "Short Average", "Data of type Short");
                newEventMetric.AddValue("short_sum", typeof(short), SummaryFunction.Sum, null, "Short Sum", "Data of type Short");
                newEventMetric.AddValue("short_runningaverage", typeof(short), SummaryFunction.RunningAverage, null, "Short Running Average", "Data of type Short");
                newEventMetric.AddValue("short_runningsum", typeof(short), SummaryFunction.RunningSum, null, "Short Running Sum", "Data of type Short");
                newEventMetric.AddValue("ushort_average", typeof(ushort), SummaryFunction.Average, null, "UShort Average", "Data of type UShort");
                newEventMetric.AddValue("ushort_sum", typeof(ushort), SummaryFunction.Sum, null, "UShort Sum", "Data of type UShort");

                // Pick an interesting value column as the default to be graphed for this metric.  We'll pass it below.
                EventMetricValueDefinition defaultValue =
                    newEventMetric.AddValue("int_average", typeof(int), SummaryFunction.Average, null, "Int Average", "Data of type Int");

                newEventMetric.AddValue("int_sum", typeof(int), SummaryFunction.Sum, null, "Int Sum", "Data of type Int");
                newEventMetric.AddValue("uint_average", typeof(uint), SummaryFunction.Average, null, "UInt Average", "Data of type UInt");
                newEventMetric.AddValue("uint_sum", typeof(uint), SummaryFunction.Sum, null, "UInt Sum", "Data of type UInt");
                newEventMetric.AddValue("long_average", typeof(long), SummaryFunction.Average, null, "Long Average", "Data of type Long");
                newEventMetric.AddValue("long_sum", typeof(long), SummaryFunction.Sum, null, "Long Sum", "Data of type Long");
                newEventMetric.AddValue("ulong_average", typeof(ulong), SummaryFunction.Average, null, "ULong Average", "Data of type ULong");
                newEventMetric.AddValue("ulong_sum", typeof(ulong), SummaryFunction.Sum, null, "ULong Sum", "Data of type ULong");
                newEventMetric.AddValue("decimal_average", typeof(decimal), SummaryFunction.Average, null, "Decimal Average", "Data of type Decimal");
                newEventMetric.AddValue("decimal_sum", typeof(decimal), SummaryFunction.Sum, null, "Decimal Sum", "Data of type Decimal");
                newEventMetric.AddValue("double_average", typeof(double), SummaryFunction.Average, null, "Double Average", "Data of type Double");
                newEventMetric.AddValue("double_sum", typeof(double), SummaryFunction.Sum, null, "Double Sum", "Data of type Double");
                newEventMetric.AddValue("float_average", typeof(float), SummaryFunction.Average, null, "Float Average", "Data of type Float");
                newEventMetric.AddValue("float_sum", typeof(float), SummaryFunction.Sum, null, "Float Sum", "Data of type Float");
                newEventMetric.AddValue("timespan_average", typeof(TimeSpan), SummaryFunction.Average, null, "TimeSpan Average", "Data of type TimeSpan");
                newEventMetric.AddValue("timespan_sum", typeof(TimeSpan), SummaryFunction.Sum, null, "TimeSpan Sum", "Data of type TimeSpan");
                newEventMetric.AddValue("timespan_runningaverage", typeof(TimeSpan), SummaryFunction.RunningAverage, null, "TimeSpan Running Average", "Data of type TimeSpan represented as a running average.");
                newEventMetric.AddValue("timespan_runningsum", typeof(TimeSpan), SummaryFunction.RunningSum, null, "TimeSpan Running Sum", "Data of type TimeSpan represented as a running sum.");
                newEventMetric.AddValue("string", typeof(string), SummaryFunction.Count, null, "String", "Data of type String");
                newEventMetric.AddValue("system.enum", typeof(UserDataEnumeration), SummaryFunction.Count, null, "System.Enum", "Data of type System.Enum");

                // Finally, register it with Gibraltar, and specify the default value column we saved above.
                EventMetricDefinition.Register(ref newEventMetric, defaultValue);
            }

            EventMetricDefinition metricDefinition;

            Assert.IsTrue(EventMetricDefinition.TryGetValue("EventMetricTests", "Gibraltar.Monitor.Test", "Manual", out metricDefinition));
            Assert.IsNotNull(metricDefinition);
        }