/// <summary> /// Create a new metric object with the provided instance name and add it to the collection /// </summary> /// <param name="instanceName">The instance name to use, or blank or null for the default metric.</param> /// <returns>The new metric object that was added to the collection</returns> public EventMetric Add(string instanceName) { //Create a new metric object with the provided instance name (it will get added to us automatically) EventMetric newMetric = new EventMetric(Definition, instanceName); //finally, return the newly created metric object to our caller return(newMetric); }
/// <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="key">The metric name to locate in the collection</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> public bool TryGetValue(string key, out EventMetric value) { //We are playing a few games to get native typing here. Because it's an OUt value, we //have to swap types around ourselves so we can cast. IMetric innerValue; //gateway to our inner dictionary try get value bool result = base.TryGetValue(key, out innerValue); value = innerValue as EventMetric; //No one expects exceptions from try get value, so if it's the wrong type, return null return(value != null); }
/// <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); }
/// <summary> /// Create a new Event metric sample object for the provided metric and raw sample packet. /// </summary> /// <remarks>The metric sample is automatically added to the samples collection of the provided metric object.</remarks> /// <param name="metric">The metric object this sample applies to.</param> /// <param name="metricSamplePacket">The raw sample data packet.</param> internal EventMetricSample(EventMetric metric, EventMetricSamplePacket metricSamplePacket) : base(metric, metricSamplePacket) { //and now that we've been created, make sure our metric definition set is locked. metric.Definition.IsReadOnly = true; }
/// <summary> /// Create a new sample collection for the specified metric object /// </summary> /// <param name="metric"></param> public EventMetricSampleCollection(EventMetric metric) : base(metric) { m_EventMetric = metric; }
/// <summary> /// Determines if the provided object is identical to this object. /// </summary> /// <param name="other">The object to compare this object to</param> /// <returns>True if the objects represent the same data.</returns> public bool Equals(EventMetric other) { //We're really just a type cast, refer to our base object return(base.Equals(other)); }
/// <summary> /// Indicates the relative sort order of this object to another of the same type. /// </summary> /// <param name="other"></param> /// <returns></returns> public int CompareTo(EventMetric other) { //we let our base object do the compare, we're really just casting things return(base.CompareTo(other)); }