Skip to content

Commit

Permalink
Raising a better exception message from Azure Monitoring http re… (#597)
Browse files Browse the repository at this point in the history
* 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 <kerkhove.tom@gmail.com>

* Update src/Promitor.Core.Scraping/Scraper.cs

Co-Authored-By: Tom Kerkhove <kerkhove.tom@gmail.com>

* fix naming convention "errorResponseException"

* removing blank line
  • Loading branch information
Mimetis authored and tomkerkhove committed Jun 26, 2019
1 parent c532149 commit c88a7c1
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/Promitor.Core.Scraping/Scraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c88a7c1

Please sign in to comment.