From c88a7c1b919b242db38f7bdfddd7add6e81889e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Pertus?= Date: Wed, 26 Jun 2019 13:23:50 +0200 Subject: [PATCH] =?UTF-8?q?Raising=20a=20better=20exception=20message=20fr?= =?UTF-8?q?om=20Azure=20Monitoring=20http=20re=E2=80=A6=20(#597)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update ScrapeAsync method to raise better exception when Azure monitoring is returning a JSON Payload in its Response * Update src/Promitor.Core.Scraping/Scraper.cs Co-Authored-By: Tom Kerkhove * Update src/Promitor.Core.Scraping/Scraper.cs Co-Authored-By: Tom Kerkhove * fix naming convention "errorResponseException" * removing blank line --- src/Promitor.Core.Scraping/Scraper.cs | 39 ++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Promitor.Core.Scraping/Scraper.cs b/src/Promitor.Core.Scraping/Scraper.cs index 0ff52d425..7a2241a18 100644 --- a/src/Promitor.Core.Scraping/Scraper.cs +++ b/src/Promitor.Core.Scraping/Scraper.cs @@ -84,9 +84,46 @@ public async Task ScrapeAsync(MetricDefinition metricDefinition) var metricsTimestampFeatureFlag = FeatureFlag.IsActive(FeatureFlag.Names.MetricsTimestamp, defaultFlagState: true); - var gauge = Metrics.CreateGauge(metricDefinition.Name, metricDefinition.Description, includeTimestamp: metricsTimestampFeatureFlag, labelNames:"resource_uri"); + var gauge = Metrics.CreateGauge(metricDefinition.Name, metricDefinition.Description, includeTimestamp: metricsTimestampFeatureFlag, labelNames: "resource_uri"); gauge.WithLabels(scrapedMetricResult.ResourceUri).Set(scrapedMetricResult.MetricValue); } + catch (ErrorResponseException errorResponseException) + { + string reason = string.Empty; + + if (!string.IsNullOrEmpty(errorResponseException.Message)) + { + reason = errorResponseException.Message; + } + + if (errorResponseException.Response != null && !string.IsNullOrEmpty(errorResponseException.Response.Content)) + { + try + { + var definition = new { error = new { code = "", message = "" } }; + var jsonError = JsonConvert.DeserializeAnonymousType(errorResponseException.Response.Content, definition); + + if (jsonError != null && jsonError.error != null) + { + if (!string.IsNullOrEmpty(jsonError.error.message)) + { + reason = $"{jsonError.error.code}: {jsonError.error.message}"; + } + else if (!string.IsNullOrEmpty(jsonError.error.code)) + { + reason = $"{jsonError.error.code}"; + } + } + } + catch (Exception) + { + // do nothing. maybe a bad deserialization of json content. Just fallback on outer exception message. + _exceptionTracker.Track(errorResponseException); + } + } + + _exceptionTracker.Track(new Exception(reason)); + } catch (Exception exception) { _exceptionTracker.Track(exception);