Skip to content

Commit e6268b4

Browse files
committed
Make ThrowOnPostFail only apply to 5xx response codes
Also make Metadata POST honor the ThrowOnPostFail option.
1 parent 6808bce commit e6268b4

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

BosunReporter/BosunOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class BosunOptions
3232
/// </summary>
3333
public int MaxPendingPayloads = 240;
3434
/// <summary>
35-
/// If true, BosunReporter will throw an exception every time posting to the Bosun API fails.
35+
/// If true, BosunReporter will throw an exception every time posting to the Bosun API fails with a server error (response code 5xx).
3636
/// </summary>
3737
public bool ThrowOnPostFail = false;
3838
/// <summary>

BosunReporter/Exceptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ namespace BosunReporter
66
{
77
public class BosunPostException : Exception
88
{
9+
public HttpStatusCode StatusCode { get; }
10+
911
internal BosunPostException(HttpStatusCode statusCode, string responseBody, Exception innerException)
1012
: base("Posting to the Bosun API failed with status code " + statusCode, innerException)
1113
{
1214
Data["ResponseBody"] = responseBody;
15+
StatusCode = statusCode;
1316
}
1417

1518
internal BosunPostException(Exception innerException)

BosunReporter/MetricsCollector.cs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -525,15 +525,15 @@ private void Snapshot(object isCalledFromTimer)
525525
LastSerializationInfo = info;
526526
AfterSerialization?.Invoke(info);
527527
}
528-
catch (Exception e)
528+
catch (Exception ex)
529529
{
530-
if (HasExceptionHandler)
530+
if (ShouldThrowException(ex))
531531
{
532-
OnBackgroundException(e);
533-
return;
532+
if (HasExceptionHandler)
533+
OnBackgroundException(ex);
534+
else
535+
throw;
534536
}
535-
536-
throw;
537537
}
538538
}
539539

@@ -573,11 +573,11 @@ private void Flush(object isCalledFromTimer)
573573

574574
} while (any);
575575
}
576-
catch (BosunPostException ex)
576+
catch (Exception ex)
577577
{
578578
// there was a problem flushing - back off for the next five seconds (Bosun may simply be restarting)
579579
_skipFlushes = 4;
580-
if (ThrowOnPostFail)
580+
if (ShouldThrowException(ex))
581581
{
582582
if (HasExceptionHandler)
583583
OnBackgroundException(ex);
@@ -784,12 +784,15 @@ private void PostMetaData(object _)
784784
}
785785
});
786786
}
787-
catch (BosunPostException ex)
787+
catch (Exception ex)
788788
{
789-
if (HasExceptionHandler)
790-
OnBackgroundException(ex);
791-
else
792-
throw;
789+
if (ShouldThrowException(ex))
790+
{
791+
if (HasExceptionHandler)
792+
OnBackgroundException(ex);
793+
else
794+
throw;
795+
}
793796
}
794797
}
795798

@@ -826,14 +829,32 @@ private string GatherMetaData()
826829

827830
private void OnPayloadDropped(BosunQueueFullException ex)
828831
{
829-
if (ThrowOnQueueFull)
832+
if (ShouldThrowException(ex))
830833
{
831834
if (HasExceptionHandler)
832835
OnBackgroundException(ex);
833836
else
834837
throw ex;
835838
}
836839
}
840+
841+
private bool ShouldThrowException(Exception ex)
842+
{
843+
var post = ex as BosunPostException;
844+
if (post != null)
845+
{
846+
if (ThrowOnPostFail)
847+
return true;
848+
849+
var status = (int)post.StatusCode;
850+
return status < 500 || status >= 600; // always want to send the exception when it's a non-500
851+
}
852+
853+
if (ex is BosunQueueFullException)
854+
return ThrowOnQueueFull;
855+
856+
return true;
857+
}
837858
}
838859

839860
public class AfterSerializationInfo

0 commit comments

Comments
 (0)