From 4853b3e768e91054dafce41e1788f4f2b1265be7 Mon Sep 17 00:00:00 2001 From: xchen Date: Wed, 18 Sep 2024 20:39:07 -0700 Subject: [PATCH] use different string matching --- .../Extensions/MetricResultExtension.cs | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Promitor.Integrations.AzureMonitor/Extensions/MetricResultExtension.cs b/src/Promitor.Integrations.AzureMonitor/Extensions/MetricResultExtension.cs index 40a15d4ab..10b2ca7ef 100644 --- a/src/Promitor.Integrations.AzureMonitor/Extensions/MetricResultExtension.cs +++ b/src/Promitor.Integrations.AzureMonitor/Extensions/MetricResultExtension.cs @@ -12,14 +12,31 @@ public static class MetricResultExtension public static string ParseResourceIdFromResultId(this MetricResult metricResult) { - Match match = resourceIdRegex.Match(metricResult.Id); - if (!match.Success || string.IsNullOrEmpty(match.Groups[1].Value)) + // Match match = resourceIdRegex.Match(metricResult.Id); + // if (!match.Success || string.IsNullOrEmpty(match.Groups[1].Value)) + // { + // throw new InvalidOperationException($"The expected resource ID pattern was not found in the input string {metricResult.Id}"); + // } + + // string resourceId = match.Groups[1].Value; + // return resourceId; + return ExtractResourceId(metricResult.Id); + } + + private static string ExtractResourceId(string fullId) + { + // Find the index of the second occurrence of "/providers/" + int firstIndex = fullId.IndexOf("/providers/"); + int secondIndex = fullId.IndexOf("/providers/", firstIndex + 1); + + // If the second "/providers/" is found, slice the string up to that point + if (secondIndex != -1) { - throw new InvalidOperationException($"The expected resource ID pattern was not found in the input string {metricResult.Id}"); + return fullId.Substring(0, secondIndex); } - string resourceId = match.Groups[1].Value; - return resourceId; + // If not found, return the full string + return fullId; } }