public void ToKustoExpression_PrometheusQueryShouldReturnValidKustoQuery(string name, LabelMatcher.Types.Type type, string val, string expected) { // Arrange // Act var kql = KustoManipulations.ToKustoExpression(name, type, val); // Assert CollectionAssert.AreEqual(expected, kql); }
public static string ToKustoExpression(string name, LabelMatcher.Types.Type type, string value) { var keyMap = new Dictionary <string, string>() { { "__name__", "Name" }, { "job", "Job" }, { "instance", "Instance" } }; var resultName = keyMap.ContainsKey(name) ? keyMap[name] : $"tostring(Labels.{name})"; const string queryTemplate = "( {0} {1} '{2}' )"; return(type switch { LabelMatcher.Types.Type.Eq => string.Format(queryTemplate, resultName, "==", value), LabelMatcher.Types.Type.Neq => string.Format(queryTemplate, resultName, "!=", value), LabelMatcher.Types.Type.Re => string.Format(queryTemplate, resultName, "matches regex", value), LabelMatcher.Types.Type.Nre => string.Format(queryTemplate, resultName, "!contains", value), _ => string.Empty });
private static String GenerateValueExpression(string name, LabelMatcher.Types.Type type, string value) { switch (type) { case LabelMatcher.Types.Type.Eq: return($"tostring(label.{name}) == \"{value}\""); case LabelMatcher.Types.Type.Neq: return($"tostring(label.{name}) != \"{value}\""); case LabelMatcher.Types.Type.Re: return($"tostring(label.{name}) matches regex \"{value}\""); case LabelMatcher.Types.Type.Nre: //operator missing return($"tostring(label.{name}) !contains \"{value}\""); default: break; } return(String.Empty); }