Beispiel #1
0
        public StatsDPoint(
            string name,
            object value,
            string metricType,
            double?sampleRate,
            Dictionary <string, string> tags,
            IStatsDMetricStringSerializer metricStringSerializer,
            DateTime?utcTimestamp = null)
        {
            _metricStringSerializer = metricStringSerializer ?? throw new ArgumentNullException(nameof(metricStringSerializer));

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Name must be non-null, non-empty and non-whitespace", nameof(name));
            }

            if (string.IsNullOrWhiteSpace(metricType))
            {
                throw new ArgumentException("Metric type must be defined.", nameof(metricType));
            }

            if (utcTimestamp != null && utcTimestamp.Value.Kind != DateTimeKind.Utc)
            {
                throw new ArgumentException("Timestamps must be specified as UTC", nameof(utcTimestamp));
            }

            Name         = name;
            Value        = value ?? throw new ArgumentException("A value must be specified", nameof(value));
            MetricType   = metricType;
            SampleRate   = sampleRate;
            Tags         = tags;
            UtcTimestamp = utcTimestamp ?? DateTime.UtcNow;
        }
 public MetricSnapshotStatsDStringWriter(Stream stream, StatsDPointSampler sampler, MetricsStatsDOptions options)
 {
     _stream  = stream;
     Sampler  = sampler;
     _options = options;
     _metricMetricStringSerializer = options.MetricNameFormatter ?? StatsDFormatterConstants.Defaults.MetricPointTextWriter;
 }
Beispiel #3
0
 public MetricSnapshotStatsDStringWriter(Stream stream, StatsDPointSampler sampler, MetricsStatsDOptions options)
 {
     _stream  = stream ?? throw new ArgumentNullException(nameof(stream));
     _sampler = sampler;
     _options = options;
     _metricMetricStringSerializer = options.MetricNameFormatter ?? StatsDFormatterConstants.Defaults.MetricPointTextWriter;
 }
        public void Add(
            string context,
            string name,
            string field,
            object value,
            MetricTags metricTags,
            IStatsDMetricStringSerializer serializer,
            DateTime?timestamp)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name), "Name could not be null or whitespace");
            }

            if (string.IsNullOrWhiteSpace(field))
            {
                throw new ArgumentNullException(nameof(field), "Field could not be null or whitespace");
            }

            var tagsDictionary = metricTags.ToDictionary();

            tagsDictionary.TryGetValue(AppMetricsConstants.Pack.MetricTagsTypeKey, out var metricType);
            tagsDictionary.TryGetValue(StatsDFormatterConstants.ItemTagName, out var itemTag);

            var key = BuildKeyName(context, name, metricType, itemTag, field);

            var tags = tagsDictionary.Where(tag => !ExcludeTags.Contains(tag.Key)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            var statsDMetricType = StatsDSyntax.FormatMetricType(metricType);

            if (metricType == AppMetricsConstants.Pack.ApdexMetricTypeValue && field != "score")
            {
                statsDMetricType = StatsDSyntax.Count;
            }

            if (!StatsDSyntax.CanBeSampled(statsDMetricType) || metricType == AppMetricsConstants.Pack.MeterMetricTypeValue)
            {
                Points.Enqueue(new StatsDPoint(key, value, statsDMetricType, null, tags, serializer, timestamp));
                return;
            }

            tagsDictionary.TryGetValue(StatsDFormatterConstants.SampleRateTagName, out var tagSampleRateStr);
            if (!double.TryParse(tagSampleRateStr, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.InvariantInfo, out var tagSampleRate))
            {
                tagSampleRate = Options.DefaultSampleRate;
            }

            var sampler = _samplers.GetOrAdd(key, _ => new StatsDSampler());

            if (sampler.ShouldSend(tagSampleRate))
            {
                Points.Enqueue(new StatsDPoint(key, value, statsDMetricType, tagSampleRate, tags, serializer, timestamp));
            }
        }
Beispiel #5
0
        public void Add(
            string context,
            string name,
            IEnumerable <string> columns,
            IEnumerable <object> values,
            MetricTags tags,
            IStatsDMetricStringSerializer serializer,
            DateTime?timestamp)
        {
            var fields = columns.Zip(values, (column, data) => new { column, data }).ToDictionary(pair => pair.column, pair => pair.data);

            var tagsDictionary = tags.ToDictionary();

            tagsDictionary.TryGetValue(AppMetricsConstants.Pack.MetricTagsTypeKey, out var metricType);

            switch (metricType)
            {
            case "histogram":
                Add(context, name, "value", fields["median"], tags, serializer, timestamp);
                break;

            case "meter":
                Add(context, name, "value", fields["count.meter"], tags, serializer, timestamp);
                break;

            case "timer":
                var value = fields["median"] switch
                {
                    double d => Convert.ToInt64(d),
                    float f => Convert.ToInt64(f),
                    _ => (long)fields["median"]
                };
                var unit = tagsDictionary[AppMetricsConstants.Pack.MetricTagsUnitRateDurationKey].FromUnit();
                value = unit.Convert(TimeUnit.Milliseconds, value);
                Add(context, name, "value", value, tags, serializer, timestamp);
                break;

            default:
                foreach (var kvp in fields)
                {
                    Add(context, name, kvp.Key, kvp.Value, tags, serializer, timestamp);
                }
                break;
            }
        }