Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Timer"/> class by using
        /// the given configuration.
        /// </summary>
        /// <param name="builder">
        /// </param>
        public BucketTimer(Builder builder) : base(builder.Config, builder.Context)
        {
            unit_         = builder.TimeUnit;
            measure_unit_ = builder.MeasureUnit;

            MetricContext context = builder.Context;

            MetricConfig config =
                builder
                .Config
                .WithAdditionalTag("unit", measure_unit_.Name());

            count_ = new BucketCounter(config.WithAdditionalTag(kStatistic, kCount),
                                       context);
            max_ = new StepMaxGauge(config.WithAdditionalTag(kStatistic, kMax));
            min_ = new StepMinGauge(config.WithAdditionalTag(kStatistic, kMin));

            // We should not convert the value of the total time, since
            // it is already a time the rate reported will be the
            // percentage of time that was spent within the defined reporting
            // interval.
            MetricConfig time_config =
                config
                .WithAdditionalTag(kStatistic, kTotal)
                .WithAdditionalTag("unit", "nounit");

            total_time_ = new BucketCounter(time_config, TimeUnit.Ticks, context);

            overflow_count_ =
                new BucketCounter(
                    config
                    .WithAdditionalTag(kStatistic, kCount)
                    .WithAdditionalTag(kBucket, "bucket=overflow"),
                    context);

            buckets_ = builder.Buckets;

            // Compute the size of the maximum bucket name and create a padding
            // format to allow lexicographically sort of buckets.
            int    num_digits = buckets_[buckets_.Length - 1].ToString().Length;
            string padding    = "".PadLeft(num_digits, '0');

            string label = unit_.Abbreviation();

            bucket_count_ = new BucketCounter[buckets_.Length];
            for (int i = 0; i < buckets_.Length; i++)
            {
                bucket_count_[i] = new BucketCounter(
                    config
                    .WithAdditionalTag(kStatistic, kCount)
                    .WithAdditionalTag(kBucket,
                                       "bucket={0}{1}".Fmt(buckets_[i].ToString(padding), label)),
                    context);
            }

            Func <double, double> convert =
                measure => ConvertToUnit(measure, measure_unit_);

            var metrics = new List <IMetric> {
                total_time_,
                new StepMeasureTransformer(min_, convert),
                new StepMeasureTransformer(max_, convert),
                overflow_count_,
                count_
            };

            metrics.AddRange(bucket_count_);

            metrics_ = new ReadOnlyCollection <IMetric>(metrics);
        }