Beispiel #1
0
        private bool Match(string attribute, Regex regex, MetricsHelper <T> helper, bool reject)
        {
            string value;

            try
            {
                value = helper.Resolve(attribute);
            }
            catch (Exception)
            {
                return(!reject);
            }
            return(regex.IsMatch(value));
        }
Beispiel #2
0
        public Entry?GetMatching(MetricsHelper <T> helper, Entry?previous)
        {
            //
            // Check the accept and reject filters.
            //
            foreach (KeyValuePair <string, Regex> e in _accept)
            {
                if (!Match(e.Key, e.Value, helper, false))
                {
                    return(null);
                }
            }

            foreach (KeyValuePair <string, Regex> e in _reject)
            {
                if (Match(e.Key, e.Value, helper, true))
                {
                    return(null);
                }
            }

            //
            // Compute the key from the GroupBy property.
            //
            string key;

            try
            {
                if (_groupByAttributes.Count == 1)
                {
                    key = helper.Resolve(_groupByAttributes[0]);
                }
                else
                {
                    var os = new StringBuilder();
                    IEnumerator <string> q = _groupBySeparators.GetEnumerator();
                    foreach (string p in _groupByAttributes)
                    {
                        os.Append(helper.Resolve(p));
                        if (q.MoveNext())
                        {
                            os.Append(q.Current);
                        }
                    }
                    key = os.ToString();
                }
            }
            catch (Exception)
            {
                return(null);
            }

            //
            // Lookup the metrics object.
            //
            lock (this)
            {
                if (previous != null && previous.GetId().Equals(key))
                {
                    Debug.Assert(_objects[key] == previous);
                    return(previous);
                }

                if (!_objects.TryGetValue(key, out MetricsMap <T> .Entry? e))
                {
                    try
                    {
                        var t = new T();
                        t.Id = key;
                        e    = new Entry(this, t);
                        _objects.Add(key, e);
                    }
                    catch (Exception)
                    {
                        Debug.Assert(false);
                    }
                }
                e.Attach(helper);
                return(e);
            }
        }
Beispiel #3
0
        internal Entry?GetMatching(MetricsHelper <T> helper, Entry?previous)
        {
            // Check the accept filters.
            foreach ((string name, Regex value) in _accept)
            {
                if (!Match(name, value, helper, false))
                {
                    return(null);
                }
            }

            // Check the reject filters.
            foreach ((string name, Regex value) in _reject)
            {
                if (Match(name, value, helper, true))
                {
                    return(null);
                }
            }

            // Compute the key from the GroupBy property.
            string key;

            try
            {
                if (_groupByAttributes.Count == 1)
                {
                    key = helper.Resolve(_groupByAttributes[0]);
                }
                else
                {
                    var os = new StringBuilder();
                    IEnumerator <string> q = _groupBySeparators.GetEnumerator();
                    foreach (string p in _groupByAttributes)
                    {
                        os.Append(helper.Resolve(p));
                        if (q.MoveNext())
                        {
                            os.Append(q.Current);
                        }
                    }
                    key = os.ToString();
                }
            }
            catch
            {
                return(null);
            }

            // Lookup the metrics object.
            lock (_mutex)
            {
                if (previous != null && previous.Id.Equals(key))
                {
                    Debug.Assert(_entries[key] == previous);
                    return(previous);
                }

                if (!_entries.TryGetValue(key, out MetricsMap <T> .Entry? e))
                {
                    var t = new T();
                    t.Id = key;
                    e    = new Entry(_mutex, this, t);
                    _entries.Add(key, e);
                }
                e.Attach(helper);
                return(e);
            }
        }