コード例 #1
0
ファイル: MetricsHost.cs プロジェクト: deeja/metrics
        /// <summary>
        ///     Creates a new timer metric and registers it under the given type and name
        /// </summary>
        /// <param name="owner">The type that owns the metric</param>
        /// <param name="name">The metric name</param>
        /// <param name="durationUnit">The duration scale unit of the new timer</param>
        /// <param name="rateUnit">The rate unit of the new timer</param>
        /// <returns></returns>
        public TimerMetric Timer(Type owner, string name, TimeUnit durationUnit, TimeUnit rateUnit)
        {
            var metricName = new MetricName(owner, name);

            if (_store.TryGetValue(metricName, out var existingMetric))
            {
                return((TimerMetric)existingMetric);
            }

            var metric          = new TimerMetric(metricName, durationUnit, rateUnit);
            var justAddedMetric = _store.GetOrAdd(metricName, metric);

            return(justAddedMetric == null ? metric : (TimerMetric)justAddedMetric);
        }
コード例 #2
0
ファイル: InMemoryMetricsStore.cs プロジェクト: deeja/metrics
        public IImmutableDictionary <MetricName, IMetric> GetSample(MetricType typeFilter = MetricType.All)
        {
            if (typeFilter.HasFlagFast(MetricType.All))
            {
                return(NoSample);
            }

            var filtered = new Dictionary <MetricName, IMetric>();

            foreach (var entry in _inner)
            {
                switch (entry.Value)
                {
                case GaugeMetric _ when typeFilter.HasFlagFast(MetricType.Gauge):
                case CounterMetric _ when typeFilter.HasFlagFast(MetricType.Counter):
                case MeterMetric _ when typeFilter.HasFlagFast(MetricType.Meter):
                case HistogramMetric _ when typeFilter.HasFlagFast(MetricType.Histogram):
                case TimerMetric _ when typeFilter.HasFlagFast(MetricType.Timer):
                    continue;

                default:
                    filtered.Add(entry.Key, entry.Value);
                    break;
                }
            }

            return(filtered.ToImmutableSortedDictionary(k => k.Key, v =>
            {
                return v.Value switch
                {
                    GaugeMetric gauge => gauge.Copy(),
                    CounterMetric counter => counter.Copy(),
                    MeterMetric meter => meter.Copy(),
                    HistogramMetric histogram => histogram.Copy(),
                    TimerMetric timer => timer.Copy(),
                    _ => throw new ArgumentException()
                };
            }));