public void InfluxTag_CanParse_MultiFromStringArray() { // string[] array InfluxUtils.ToInfluxTags("key1", "key2").Should().BeEmpty(); InfluxUtils.ToInfluxTags("key1=value1", "key2=value2").Should().BeEquivalentTo(new InfluxTag("key1", "value1"), new InfluxTag("key2", "value2")); InfluxUtils.ToInfluxTags("key1", "key2=value2", "key3", "key4").Should().BeEquivalentTo(new InfluxTag("key2", "value2")); }
public void InfluxTag_CanParse_FromMultiMetricTags() { InfluxUtils.ToInfluxTags(new MetricTags("key1", "key2"), new MetricTags("key3", "key4")).Should().BeEmpty(); InfluxUtils.ToInfluxTags(new MetricTags("key1=value1", "key2"), new MetricTags("key3=value3", "key4")).Should().BeEquivalentTo(new InfluxTag("key1", "value1"), new InfluxTag("key3", "value3")); InfluxUtils.ToInfluxTags(new MetricTags("key1", "key2=value2"), new MetricTags("key3", "key4=value4")).Should().BeEquivalentTo(new InfluxTag("key2", "value2"), new InfluxTag("key4", "value4")); InfluxUtils.ToInfluxTags(new MetricTags("key1=value1", "key2=value2"), new MetricTags("key3=value3", "key4=value4")).Should().BeEquivalentTo(new InfluxTag("key1", "value1"), new InfluxTag("key2", "value2"), new InfluxTag("key3", "value3"), new InfluxTag("key4", "value4")); }
/// <summary> /// Formats the field value in the appropriate line protocol format based on the type of the value object. /// The value type must be a string, boolean, or integral or floating-point type. /// </summary> /// <param name="value">The field value to format.</param> /// <returns>The field value formatted as a string used in the line protocol format.</returns> private static JsonValue FormatValue(Object value) { Type type = value?.GetType(); if (value == null) { throw new ArgumentNullException(nameof(value)); } if (!InfluxUtils.IsValidValueType(type)) { throw new ArgumentException(nameof(value), $"Value is not one of the supported types: {type} - Valid types: {String.Join(", ", InfluxUtils.ValidValueTypes.Select(t => t.Name))}"); } if (InfluxUtils.IsIntegralType(type)) { return(FormatValue(Convert.ToInt64(value))); } if (InfluxUtils.IsFloatingPointType(type)) { return(FormatValue(Convert.ToDouble(value))); } if (value is String) { return(FormatValue((String)value)); } if (value is Char) { return(FormatValue(value.ToString())); } return(FormatValue(value.ToString())); }
public void InfluxTag_CanParse_MultiFromCommaSeparatedString() { // comma-separated single string InfluxUtils.ToInfluxTags("key").Should().BeEmpty(); InfluxUtils.ToInfluxTags("key1=value1,key2=value2").Should().BeEquivalentTo(new InfluxTag("key1", "value1"), new InfluxTag("key2", "value2")); InfluxUtils.ToInfluxTags("key1,key2=value2,key3,key4").Should().BeEquivalentTo(new InfluxTag("key2", "value2")); }
/// <summary> /// Creates a new <see cref="InfluxRecord"/> from the specified name, item name, tags, and fields. /// This uses the timestamp defined on this metrics converter instance. /// </summary> /// <param name="name">The measurement or series name. This value is required and cannot be null or empty.</param> /// <param name="itemName">The set item name. Can contain comma-separated key/value pairs.</param> /// <param name="tags">The optional tags to associate with this record.</param> /// <param name="fields">The <see cref="InfluxField"/> values for the output fields.</param> /// <returns>A new <see cref="InfluxRecord"/> from the specified name, tags, and fields.</returns> public InfluxRecord GetRecord(String name, String itemName, MetricTags tags, IEnumerable <InfluxField> fields) { var jtags = InfluxUtils.JoinTags(itemName, GlobalTags, tags); // global tags must be first so they can get overridden var record = new InfluxRecord(name, jtags, fields, Timestamp); return(record); }
public void InfluxTag_CanParse_SingleFromString() { // valid input strings InfluxUtils.ToInfluxTag("key=value").Should().Be(new InfluxTag("key", "value")); InfluxUtils.ToInfluxTag("key with spaces=value with spaces").Should().Be(new InfluxTag("key with spaces", "value with spaces")); InfluxUtils.ToInfluxTag("key,with,commas=value,with,commas").Should().Be(new InfluxTag("key,with,commas", "value,with,commas")); InfluxUtils.ToInfluxTag("key\"with\"quot=value\"with\"quot").Should().Be(new InfluxTag("key\"with\"quot", "value\"with\"quot")); }
public void InfluxField_SupportsValidValueTypes() { var validTypes = InfluxUtils.ValidValueTypes; foreach (var type in validTypes) { InfluxUtils.IsValidValueType(type).Should().BeTrue(); } }
/// <summary> /// Formats the metric name using the <see cref="MetricNameFormatter"/> if it is set, otherwise returns null. /// This will apply lowercasing and replace space characters if it is configured on this <see cref="InfluxdbFormatter"/> instance. /// </summary> /// <param name="context">The metrics context name.</param> /// <param name="name">The metric name.</param> /// <param name="unit">The metric units.</param> /// <param name="tags">The metric tags.</param> /// <returns>The metric name after applying the formatters and transformations, or null if the <see cref="MetricNameFormatter"/> is not set.</returns> public virtual String FormatMetricName(String context, String name, Unit unit, String[] tags) { String value = MetricNameFormatter?.Invoke(context, name, unit, tags); if (value == null) { return(null); // return null so that caller knows that it can call its own default implementation if it has one } return(InfluxUtils.LowerAndReplaceSpaces(value, LowercaseNames, ReplaceSpaceChar)); }
/// <summary> /// Formats the context name using the <see cref="ContextNameFormatter"/> if it is set, otherwise returns null. /// This will apply lowercasing and replace space characters if it is configured on this <see cref="InfluxdbFormatter"/> instance. /// </summary> /// <param name="contextStack">The list of parent context names.</param> /// <param name="contextName">The current context name.</param> /// <returns>The context name after applying the formatters and transformations, or null if the <see cref="ContextNameFormatter"/> is not set.</returns> public virtual String FormatContextName(IEnumerable <String> contextStack, String contextName) { String value = ContextNameFormatter?.Invoke(contextStack, contextName); if (value == null) { return(null); // return null so that caller knows that it can call its own default implementation if it has one } return(InfluxUtils.LowerAndReplaceSpaces(value, LowercaseNames, ReplaceSpaceChar)); }
public void InfluxTag_CanParse_InvalidValueReturnsEmpty() { // invalid input strings InfluxTag empty = InfluxTag.Empty; String nullReason = "Because the input string should contain a single key and value separated by an equals sign."; InfluxUtils.ToInfluxTag("key").Should().Be(empty, nullReason); InfluxUtils.ToInfluxTag("key=").Should().Be(empty, nullReason); InfluxUtils.ToInfluxTag("=value").Should().Be(empty, nullReason); InfluxUtils.ToInfluxTag("key=value1=value2").Should().Be(empty, nullReason); InfluxUtils.ToInfluxTag("key,value").Should().Be(empty, nullReason); InfluxUtils.ToInfluxTag("key==").Should().Be(empty, nullReason); InfluxUtils.ToInfluxTag("==val").Should().Be(empty, nullReason); }
/// <summary> /// Creates new <see cref="InfluxRecord"/> instances for each HealthCheck result in the specified <paramref name="status"/>. /// </summary> /// <param name="status">The health status.</param> /// <returns></returns> public IEnumerable <InfluxRecord> GetRecords(HealthStatus status) { foreach (var result in status.Results) { //var name = InfluxUtils.LowerAndReplaceSpaces(result.Name); //var nameWithTags = Regex.IsMatch(result.Name, "^[Nn]ame=") ? result.Name : $"Name={result.Name}"; var split = Regex.Split(result.Name, @"(?<!\\)[,]").Select(t => t.Trim()).Where(t => t.Length > 0).ToArray(); if (!Regex.IsMatch(split[0], "^[Nn]ame=")) { split[0] = $"Name={InfluxUtils.LowerAndReplaceSpaces(split[0])}"; } var name = String.Join(",", split); yield return(GetRecord("Health Checks", name, new[] { new InfluxField("IsHealthy", result.Check.IsHealthy), new InfluxField("Message", result.Check.Message), })); } }
/// <summary> /// Creates an HTTP URI for InfluxDB using the values specified in the <see cref="InfluxConfig"/> object. /// </summary> /// <param name="config">The configuration object to get the relevant fields to build the HTTP URI from.</param> /// <returns>A new InfluxDB URI using the configuration specified in the <paramref name="config"/> parameter.</returns> protected static Uri FormatInfluxUri(InfluxConfig config) { UInt16 port = (config.Port ?? 0) > 0 ? config.Port.Value : InfluxConfig.Default.PortHttp; return(InfluxUtils.FormatInfluxUri(InfluxUtils.SchemeHttp, config.Hostname, port, config.Database, config.Username, config.Password, config.RetentionPolicy, config.Precision)); }
/// <summary> /// Formats the field key name using the <see cref="FieldKeyFormatter"/> if it is set, otherwise uses the unmodified key value. /// This will apply lowercasing and replace space characters if it is configured on this <see cref="InfluxdbFormatter"/> instance. /// </summary> /// <param name="fieldKey">The <see cref="InfluxField.Key"/> string value to format.</param> /// <returns>The field key name after applying the formatters and transformations.</returns> public virtual String FormatFieldKey(String fieldKey) { String value = FieldKeyFormatter?.Invoke(fieldKey) ?? fieldKey; return(InfluxUtils.LowerAndReplaceSpaces(value, LowercaseNames, ReplaceSpaceChar)); }
/// <summary> /// Formats the tag key name using the <see cref="TagKeyFormatter"/> if it is set, otherwise uses the unmodified key value. /// This will apply lowercasing and replace space characters if it is configured on this <see cref="InfluxdbFormatter"/> instance. /// </summary> /// <param name="tagKey">The <see cref="InfluxTag.Key"/> string value to format.</param> /// <returns>The tag key name after applying the formatters and transformations.</returns> public virtual String FormatTagKey(String tagKey) { String value = TagKeyFormatter?.Invoke(tagKey) ?? tagKey; return(InfluxUtils.LowerAndReplaceSpaces(value, LowercaseNames, ReplaceSpaceChar)); }