Ejemplo n.º 1
0
        /// <summary>
        /// Retrieve an item from the collection by its key if present.  If not present, the default value of the object is returned.
        /// </summary>
        /// <param name="metricTypeName">The unique metric type</param>
        /// <param name="categoryName">The name of the category with which this definition is associated.</param>
        /// <param name="counterName">The name of the definition within the category.</param>
        /// <param name="value">When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.</param>
        /// <returns>true if the collection contains an element with the specified key; otherwise false.</returns>
        /// <exception cref="ArgumentNullException">The provided metricsSystem, categoryName, or counterName was null.</exception>
        public bool TryGetValue(string metricTypeName, string categoryName, string counterName, out IMetricDefinition value)
        {
            //get the key for the provided values
            string key = MetricDefinition.GetKey(metricTypeName, categoryName, counterName);

            lock (m_Lock)
            {
                //gateway to our inner dictionary try get value
                return(m_DictionaryByName.TryGetValue(key, out value));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Determines whether the collection contains an element with the specified key.
        /// </summary>
        /// <param name="metricTypeName">The unique metric type</param>
        /// <param name="categoryName">The name of the category with which this definition is associated.</param>
        /// <param name="counterName">The name of the definition within the category.</param>
        /// <returns>true if the collection contains an element with the key; otherwise, false.</returns>
        public bool ContainsKey(string metricTypeName, string categoryName, string counterName)
        {
            //get the key for the provided values
            string key = MetricDefinition.GetKey(metricTypeName, categoryName, counterName);

            lock (m_Lock)
            {
                //gateway to our alternate inner dictionary
                return(m_DictionaryByName.ContainsKey(key));
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Retrieve metric object by its type, category, and counter names.
 /// </summary>
 /// <param name="metricTypeName">The unique metric type</param>
 /// <param name="categoryName">The name of the category with which this definition is associated.</param>
 /// <param name="counterName">The name of the definition within the category.</param>
 /// <returns></returns>
 public IMetricDefinition this[string metricTypeName, string categoryName, string counterName]
 {
     get
     {
         //create the key from the parts we got
         string key = MetricDefinition.GetKey(metricTypeName, categoryName, counterName);
         lock (m_Lock)
         {
             return(m_DictionaryByName[key]);
         }
     }
 }
        /// <summary>Creates a new metric instance from the provided definition information, or returns any existing instance if found.</summary>
        /// <remarks>If the metric definition doesn't exist, it will be created.  If the metric doesn't exist, it will be created.
        /// If the metric definition does exist, but is not a Custom Sampled Metric (or a derived class) an exception will be thrown.</remarks>
        /// <param name="definitions">The definitions dictionary this definition is a part of</param>
        /// <param name="metricTypeName">The unique metric type</param>
        /// <param name="categoryName">The name of the category with which this definition is associated.</param>
        /// <param name="counterName">The name of the definition within the category.</param>
        /// <param name="metricSampleType">The type of data captured for each metric under this definition.</param>
        /// <param name="instanceName">The unique name of this instance within the metric's collection.</param>
        public static CustomSampledMetric AddOrGet(MetricDefinitionCollection definitions, string metricTypeName, string categoryName, string counterName, MetricSampleType metricSampleType, string instanceName)
        {
            //we must have a definitions collection, or we have a problem
            if (definitions == null)
            {
                throw new ArgumentNullException(nameof(definitions));
            }

            //we need to find the definition, adding it if necessary
            string            definitionKey = MetricDefinition.GetKey(metricTypeName, categoryName, counterName);
            IMetricDefinition definition;

            //Establish a lock on the definitions collection so our lookup & create are atomic.
            lock (definitions.Lock)
            {
                if (definitions.TryGetValue(definitionKey, out definition))
                {
                    //if the metric definition exists, but is of the wrong type we have a problem.
                    if ((definition is CustomSampledMetricDefinition) == false)
                    {
                        throw new ArgumentException("A metric already exists with the provided type, category, and counter name but it is not compatible with being a custom sampled metric.  Please use a different counter name.", nameof(counterName));
                    }
                }
                else
                {
                    //we didn't find one, make a new one
                    definition =
                        new CustomSampledMetricDefinition(definitions, metricTypeName, categoryName, counterName,
                                                          metricSampleType);
                }
            }

            //now we have our definition, proceed to create a new metric if it doesn't exist
            string  metricKey = MetricDefinition.GetKey(metricTypeName, categoryName, counterName, instanceName);
            IMetric metric;

            //see if we can get the metric already.  If not, we'll create it
            lock (((MetricCollection)definition.Metrics).Lock)
            {
                if (definition.Metrics.TryGetValue(metricKey, out metric) == false)
                {
                    metric = new CustomSampledMetric((CustomSampledMetricDefinition)definition, instanceName);
                }
            }

            return((CustomSampledMetric)metric);
        }
Ejemplo n.º 5
0
        /// <summary>Creates a new metric instance or returns an existing one from the provided definition information, or returns any existing instance if found.</summary>
        /// <remarks>If the metric definition doesn't exist, it will be created.  If the metric doesn't exist, it will be created.
        /// If the metric definition does exist, but is not an Event Metric (or a derived class) an exception will be thrown.</remarks>
        /// <param name="definitions">The definitions dictionary this definition is a part of</param>
        /// <param name="metricTypeName">The unique metric type</param>
        /// <param name="categoryName">The name of the category with which this definition is associated.</param>
        /// <param name="counterName">The name of the definition within the category.</param>
        /// <param name="instanceName">The unique name of this instance within the metric's collection.</param>
        /// <returns>The event metric object for the specified event metric instance.</returns>
        public static EventMetric AddOrGet(MetricDefinitionCollection definitions, string metricTypeName, string categoryName, string counterName, string instanceName)
        {
            //we must have a definitions collection, or we have a problem
            if (definitions == null)
            {
                throw new ArgumentNullException(nameof(definitions));
            }

            //we need to find the definition, adding it if necessary
            string            definitionKey = MetricDefinition.GetKey(metricTypeName, categoryName, counterName);
            IMetricDefinition definition;

            if (definitions.TryGetValue(definitionKey, out definition))
            {
                //if the metric definition exists, but is of the wrong type we have a problem.
                if ((definition is EventMetricDefinition) == false)
                {
                    throw new ArgumentException("A metric already exists with the provided type, category, and counter name but it is not compatible with being an event metric.  Please use a different counter name.", nameof(counterName));
                }
            }
            else
            {
                //we didn't find one, make a new one
                definition = new EventMetricDefinition(definitions, metricTypeName, categoryName, counterName);
                definitions.Add(definition); // Add it to the collection, no longer done in the constructor.
                // ToDo: Reconsider this implementation; putting incomplete event metric definitions in the collection is not ideal,
                // and creating a metric from an empty event metric definition is fairly pointless.
            }

            //now we have our definition, proceed to create a new metric if it doesn't exist
            string  metricKey = MetricDefinition.GetKey(metricTypeName, categoryName, counterName, instanceName);
            IMetric metric;

            //see if we can get the metric already.  If not, we'll create it
            lock (((MetricCollection)definition.Metrics).Lock) //make sure the get & add are atomic
            {
                if (definition.Metrics.TryGetValue(metricKey, out metric) == false)
                {
                    metric = new EventMetric((EventMetricDefinition)definition, instanceName);
                }
            }

            return((EventMetric)metric);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Retrieves the specified metric instance, or creates it if it doesn't exist
        /// </summary>
        /// <param name="instanceName"></param>
        /// <returns>The custom sampled metric object.</returns>
        private CustomSampledMetric EnsureMetricExists(string instanceName)
        {
            //Find the right metric sample instance, creating it if we have to.
            string  metricKey = MetricDefinition.GetKey(this, instanceName);
            IMetric ourMetric;

            //This must be protected in a multi-threaded environment
            lock (Metrics.Lock)
            {
                if (Metrics.TryGetValue(metricKey, out ourMetric) == false)
                {
                    //it doesn't exist - go ahead and add it
                    ourMetric = new CustomSampledMetric(this, instanceName);
                }
            }

            //and return the metric
            return((CustomSampledMetric)ourMetric);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Add the supplied Metric item to this collection.
        /// </summary>
        /// <remarks>Metrics automatically add themselves when they are created, so it isn't necessary (and will produce errors) to manually add them.</remarks>
        /// <param name="item">The new Metric item to add to this collection</param>
        public void Add(IMetric item)
        {
            //we really don't want to support this method, but we have to for ICollection<T> compatibility.  So we're going to ruthlessly
            //verify that the metric object was created correctly.

            if (item == null)
            {
                throw new ArgumentNullException(nameof(item), "A metric item must be provided to add it to the collection.");
            }

            //make sure the metric is for the right definition, namely our definition.
            if (MetricDefinition.GetKey(item) != m_MetricDefinition.Name)
            {
                throw new ArgumentOutOfRangeException(nameof(item), "The provided metric item is not related to the metric definition that owns this metrics collection.");
            }

            //we're about to modify the collection, get a lock.  We don't want the lock to cover the changed event since
            //we really don't know how long that will take, and it could be deadlock prone.
            lock (m_Lock)
            {
                //make sure we don't already have it
                if (m_Dictionary.ContainsKey(item.Id))
                {
                    throw new ArgumentException("The specified metric item is already in the collection.", nameof(item));
                }

                if (m_DictionaryByName.ContainsKey(item.Name))
                {
                    throw new ArgumentException("A metric item for the same metric is already in the collection.", nameof(item));
                }

                //add it to all of our collections, and to the definition metric cache.
                m_Dictionary.Add(item.Id, item);
                m_DictionaryByName.Add(item.Name, item);
                m_List.Add(item);
                ((MetricDefinitionCollection)m_MetricDefinition.Definitions).AddMetric(item);
            }

            //and fire our event
            OnCollectionChanged(new CollectionChangedEventArgs <IMetricCollection, IMetric>(this, item, CollectionAction.Added));
        }
Ejemplo n.º 8
0
 /// <summary>Creates a new event metric object from the metric definition looked up with the provided key information.</summary>
 /// <remarks>The metric definition must already exist or an exception will be raised.</remarks>
 /// <param name="definitions">The definitions dictionary this definition is a part of</param>
 /// <param name="metricTypeName">The unique metric type</param>
 /// <param name="categoryName">The name of the category with which this definition is associated.</param>
 /// <param name="counterName">The name of the definition within the category.</param>
 /// <param name="instanceName">The unique name of this instance within the metric's collection.</param>
 public EventMetric(MetricDefinitionCollection definitions, string metricTypeName, string categoryName, string counterName, string instanceName)
     : this((EventMetricDefinition)definitions[MetricDefinition.GetKey(metricTypeName, categoryName, counterName)], instanceName)
 {
 }