Exemplo n.º 1
0
        /// <summary>
        /// Use this constructor when instantiating from a field.
        /// </summary>
        public BosunTag(FieldInfo fieldInfo, BosunTagAttribute attribute, Func <string, string> nameReplacer)
        {
            IsFromDefault = false;
            IsOptional    = attribute.IsOptional;

            FieldInfo = fieldInfo;
            if (!FieldInfo.IsInitOnly || (FieldInfo.FieldType != typeof(string) && !FieldInfo.FieldType.IsEnum))
            {
                throw new InvalidOperationException(
                          $"The BosunTag attribute can only be applied to readonly string or enum fields. {fieldInfo.DeclaringType.FullName}.{fieldInfo.Name} is invalid.");
            }

            Attribute = attribute;

            if (attribute.Name != null)
            {
                Name = attribute.Name;
            }
            else if (nameReplacer != null)
            {
                Name = nameReplacer(fieldInfo.Name);
            }
            else
            {
                Name = fieldInfo.Name;
            }

            if (!BosunValidation.IsValidTagName(Name))
            {
                throw new InvalidOperationException($"\"{Name}\" is not a valid Bosun Tag name. Field: {fieldInfo.DeclaringType.FullName}.{fieldInfo.Name}.");
            }
        }
Exemplo n.º 2
0
        public GaugeAggregatorAttribute(AggregateMode aggregateMode, string suffix, double percentile)
        {
            AggregateMode = aggregateMode;

            string defaultSuffix;

            switch (aggregateMode)
            {
            case AggregateMode.Average:
                Percentile    = -1.0;
                defaultSuffix = "_avg";
                break;

            case AggregateMode.Last:
                Percentile    = -2.0;
                defaultSuffix = "";
                break;

            case AggregateMode.Count:
                Percentile    = -3.0;
                defaultSuffix = "_count";
                break;

            case AggregateMode.Median:
                Percentile    = 0.5;
                defaultSuffix = "_median";
                break;

            case AggregateMode.Percentile:
                if (Double.IsNaN(percentile) || percentile < 0 || percentile > 1)
                {
                    throw new ArgumentOutOfRangeException("percentile", "Percentile must be specified for gauge aggregators with percentile mode. Percentile must be between 0 and 1 (inclusive)");
                }
                Percentile    = percentile;
                defaultSuffix = "_" + (int)(percentile * 100);
                break;

            case AggregateMode.Max:
                Percentile    = 1.0;
                defaultSuffix = "_max";
                break;

            case AggregateMode.Min:
                Percentile    = 0.0;
                defaultSuffix = "_min";
                break;

            default:
                throw new Exception("Gauge mode not implemented.");
            }

            Suffix = suffix ?? defaultSuffix;
            if (Suffix.Length > 0 && !BosunValidation.IsValidMetricName(Suffix))
            {
                throw new Exception("\"" + Suffix + "\" is not a valid metric suffix.");
            }
        }
Exemplo n.º 3
0
        internal string GetTagsJson(
            ReadOnlyDictionary <string, string> defaultTags,
            TagValueConverterDelegate tagValueConverter,
            Dictionary <Type, List <BosunTag> > tagsByTypeCache)
        {
            var sb = new StringBuilder();

            foreach (var tag in GetTagsList(defaultTags, tagsByTypeCache))
            {
                var value = tag.IsFromDefault ? defaultTags[tag.Name] : tag.FieldInfo.GetValue(this)?.ToString();
                if (tagValueConverter != null)
                {
                    value = tagValueConverter(tag.Name, value);
                }

                if (value == null)
                {
                    if (tag.IsOptional)
                    {
                        continue;
                    }

                    throw new InvalidOperationException(
                              $"null is not a valid tag value for {GetType().FullName}.{tag.FieldInfo.Name}. This tag was declared as non-optional.");
                }
                if (!BosunValidation.IsValidTagValue(value))
                {
                    throw new InvalidOperationException(
                              $"Invalid value for tag {GetType().FullName}.{tag.FieldInfo.Name}. \"{value}\" is not a valid tag value. " +
                              $"Only characters in the regex class [a-zA-Z0-9\\-_./] are allowed.");
                }

                // everything is already validated, so we can skip a more formal JSON parser which would handle escaping
                sb.Append(",\"" + tag.Name + "\":\"" + value + "\"");
            }

            if (sb.Length == 0)
            {
                if (!IsExternalCounter())
                {
                    throw new InvalidOperationException(
                              $"At least one tag value must be specified for every metric. {GetType().FullName} was instantiated without any tag values.");
                }

                sb.Append('{');
            }
            else
            {
                sb[0] = '{'; // replaces the first comma
            }

            sb.Append('}');
            return(sb.ToString());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Applies an <see cref="AggregateMode"/> to an <see cref="AggregateGauge"/>.
        /// </summary>
        /// <param name="mode">The aggregate mode. Don't use AggregateMode.Percentile with this constructor.</param>
        /// <param name="suffix">Overrides the default suffix for the aggregate mode.</param>
        /// <param name="percentile">
        /// The percentile represented as a double. For example, 0.95 = 95th percentile. Using more than two digits is not recommended. In order to use this
        /// argument, <paramref name="mode"/> must be AggregateMode.Percentile.
        /// </param>
        public GaugeAggregatorAttribute(AggregateMode mode, string suffix, double percentile)
        {
            AggregateMode = mode;
            Percentile    = AggregateGauge.AggregateModeToPercentileAndSuffix(mode, percentile, out var defaultSuffix);
            Suffix        = suffix ?? defaultSuffix;

            if (Suffix.Length > 0 && !BosunValidation.IsValidMetricName(Suffix))
            {
                throw new Exception("\"" + Suffix + "\" is not a valid metric suffix.");
            }
        }