Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raising a better exception message from Azure Monitoring http response #597

Merged
merged 5 commits into from
Jun 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Mimetis marked this conversation as resolved.
Show resolved Hide resolved
_exceptionTracker.Track(errorResponseException);
}
}

_exceptionTracker.Track(new Exception(reason));
}
catch (Exception exception)
{
_exceptionTracker.Track(exception);
Expand Down