Skip to content

Commit

Permalink
Cleaned up configurations for watchers #18 #17, #16, #5.
Browse files Browse the repository at this point in the history
  • Loading branch information
spetz committed Mar 18, 2016
1 parent 8e4906e commit 1849d29
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 13 deletions.
14 changes: 12 additions & 2 deletions src/Sentry.Watchers.Api/ApiWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,18 @@ public async Task<IWatcherCheckResult> ExecuteAsync()
break;
default: throw new ArgumentException($"Invalid HTTP method: {method}.", nameof(method));
}

var isValid = HasValidResponse(response);
if (!isValid)
{
return WatcherCheckResult.Create(this, false,
$"API endpoint: '{fullUrl}' has returned an invalid response with status code: {response.StatusCode}.");
}

if (_configuration.EnsureThatAsync != null)
isValid = await _configuration.EnsureThatAsync?.Invoke(response);

isValid = isValid && (_configuration.EnsureThat?.Invoke(response) ?? true);

return ApiWatcherCheckResult.Create(this, isValid, _configuration.Uri, _configuration.Request,
_httpClient.DefaultRequestHeaders, response,
Expand Down Expand Up @@ -92,8 +103,7 @@ private void SetRequestHeaders()
}

private bool HasValidResponse(HttpResponseMessage response)
=> (_configuration.EnsureThat?.Invoke(response) ?? true) &&
(response.IsSuccessStatusCode || _configuration.SkipStatusCodeValidation);
=> response.IsSuccessStatusCode || _configuration.SkipStatusCodeValidation;

public static ApiWatcher Create(string name, string url, HttpRequest request, Action<ApiWatcherConfiguration.Builder> configuration = null)
{
Expand Down
11 changes: 7 additions & 4 deletions src/Sentry.Watchers.MongoDb/MongoDbWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class MongoDbWatcher : IWatcher
{
private readonly MongoDbWatcherConfiguration _configuration;
private readonly MongoClient _client;

public string Name { get; }

protected MongoDbWatcher(string name, MongoDbWatcherConfiguration configuration)
Expand Down Expand Up @@ -43,6 +42,7 @@ private MongoClientSettings InitializeSettings()

private MongoServerAddress GetServerAddress()
{
//Remove the "mongodb://" substring
var cleanedConnectionString = _configuration.ConnectionString.Substring(10);
var hostAndPort = cleanedConnectionString.Split(':');

Expand All @@ -61,13 +61,16 @@ public async Task<IWatcherCheckResult> ExecuteAsync()
isValid = databases.Current.Any(x => x["name"] == _configuration.Database);
}

if(!isValid)
return WatcherCheckResult.Create(this, false, $"Database: '{_configuration.Database}' has not been found.");
if (!isValid)
{
return WatcherCheckResult.Create(this, false,
$"Database: '{_configuration.Database}' has not been found.");
}

if (_configuration.EnsureThatAsync != null)
isValid = await _configuration.EnsureThatAsync?.Invoke(database);

isValid = (_configuration.EnsureThat?.Invoke(database) ?? true) && isValid;
isValid = isValid && (_configuration.EnsureThat?.Invoke(database) ?? true);

return WatcherCheckResult.Create(this, isValid);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Sentry.Watchers.MongoDb/MongoDbWatcherConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ protected internal MongoDbWatcherConfiguration(string database, string connectio

ValidateAndSetDatabase(database);
ConnectionString = connectionString;
ServerSelectionTimeout = TimeSpan.FromSeconds(10);
ConnectTimeout = TimeSpan.FromSeconds(10);
}

protected internal MongoDbWatcherConfiguration(string database, MongoClientSettings settings)
Expand Down
10 changes: 8 additions & 2 deletions src/Sentry.Watchers.MsSql/MsSqlWatcher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using Sentry.Core;
Expand All @@ -11,7 +12,6 @@ public class MsSqlWatcher : IWatcher
{
private readonly MsSqlWatcherConfiguration _configuration;
private readonly DynamicParameters _queryParameters = new DynamicParameters();

public string Name { get; }

protected MsSqlWatcher(string name, MsSqlWatcherConfiguration configuration)
Expand Down Expand Up @@ -49,7 +49,13 @@ public async Task<IWatcherCheckResult> ExecuteAsync()

var result = await connection.QueryAsync<dynamic>(_configuration.Query, _queryParameters,
commandTimeout: (int) _configuration.Timeout.TotalSeconds);
var isValid = _configuration.EnsureThat?.Invoke(result) ?? true;

var collection = result.ToList();
var isValid = true;
if (_configuration.EnsureThatAsync != null)
isValid = await _configuration.EnsureThatAsync?.Invoke(collection);

isValid = isValid && (_configuration.EnsureThat?.Invoke(collection) ?? true);

return WatcherCheckResult.Create(this, isValid);
}
Expand Down
12 changes: 12 additions & 0 deletions src/Sentry.Watchers.MsSql/MsSqlWatcherConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Sentry.Watchers.MsSql
{
Expand All @@ -9,6 +10,7 @@ public class MsSqlWatcherConfiguration
public string Query { get; protected set; }
public IDictionary<string, object> QueryParameters { get; protected set; }
public Func<IEnumerable<dynamic>, bool> EnsureThat { get; protected set; }
public Func<IEnumerable<dynamic>, Task<bool>> EnsureThatAsync { get; protected set; }
public TimeSpan Timeout { get; protected set; }

protected internal MsSqlWatcherConfiguration(string connectionString)
Expand Down Expand Up @@ -64,6 +66,16 @@ public Builder EnsureThat(Func<IEnumerable<dynamic>, bool> ensureThat)
return this;
}

public Builder EnsureThatAsync(Func<IEnumerable<dynamic>, Task<bool>> ensureThat)
{
if (ensureThat == null)
throw new ArgumentException("Ensure that async predicate can not be null.", nameof(ensureThat));

_configuration.EnsureThatAsync = ensureThat;

return this;
}

public MsSqlWatcherConfiguration Build() => _configuration;
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/Sentry.Watchers.Website/WebsiteWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ public async Task<IWatcherCheckResult> ExecuteAsync()
{
var response = await _httpClient.GetAsync(string.Empty);
var isValid = HasValidResponse(response);
if (!isValid)
{
return WatcherCheckResult.Create(this, false,
$"Websiste for URL: '{_configuration.Uri}' has returned an invalid response with status code: {response.StatusCode}.");
}

if (_configuration.EnsureThatAsync != null)
isValid = await _configuration.EnsureThatAsync?.Invoke(response);

isValid = isValid && (_configuration.EnsureThat?.Invoke(response) ?? true);

return WebsiteWatcherCheckResult.Create(this, isValid, _configuration.Uri,
_httpClient.DefaultRequestHeaders, response,
Expand Down Expand Up @@ -71,8 +81,7 @@ private void SetRequestHeaders()
}

private bool HasValidResponse(HttpResponseMessage response)
=> (_configuration.WhereValidResponseIs?.Invoke(response) ?? true) &&
(response.IsSuccessStatusCode || _configuration.SkipStatusCodeValidation);
=> response.IsSuccessStatusCode || _configuration.SkipStatusCodeValidation;

public static WebsiteWatcher Create(string name, string url, Action<WebsiteWatcherConfiguration.Builder> configuration = null)
{
Expand Down
21 changes: 18 additions & 3 deletions src/Sentry.Watchers.Website/WebsiteWatcherConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace Sentry.Watchers.Website
{
Expand All @@ -10,7 +11,8 @@ public class WebsiteWatcherConfiguration
public bool SkipStatusCodeValidation { get; protected set; }
public IDictionary<string, string> Headers { get; protected set; }
public TimeSpan Timeout { get; protected set; }
public Func<HttpResponseMessage, bool> WhereValidResponseIs { get; protected set; }
public Func<HttpResponseMessage, bool> EnsureThat { get; protected set; }
public Func<HttpResponseMessage, Task<bool>> EnsureThatAsync { get; protected set; }

protected internal WebsiteWatcherConfiguration(string url)
{
Expand Down Expand Up @@ -73,9 +75,22 @@ public Builder SkipStatusCodeValidation()
return this;
}

public Builder EnsureThat(Func<HttpResponseMessage, bool> predicate)
public Builder EnsureThat(Func<HttpResponseMessage, bool> ensureThat)
{
_configuration.WhereValidResponseIs = predicate;
if (ensureThat == null)
throw new ArgumentException("Ensure that predicate can not be null.", nameof(ensureThat));

_configuration.EnsureThat = ensureThat;

return this;
}

public Builder EnsureThatAsync(Func<HttpResponseMessage, Task<bool>> ensureThat)
{
if (ensureThat == null)
throw new ArgumentException("Ensure that async predicate can not be null.", nameof(ensureThat));

_configuration.EnsureThatAsync = ensureThat;

return this;
}
Expand Down

0 comments on commit 1849d29

Please sign in to comment.