private void GetBackupServerHealth() { try { var builder = new UriBuilder("http://localhost:9080/health/"); var uri = builder.Uri; using (var client = new HttpClient()) { Task <HttpResponseMessage> taskResponse = Task.Run(() => client.GetAsync(uri)); var response = taskResponse.Result; Task <string> taskResponseBody = Task.Run(() => response.Content.ReadAsStringAsync()); string responseBody = taskResponseBody.Result; HealthResponse respond = JsonConvert.DeserializeObject <HealthResponse>(responseBody); ServerHealth.BackupReadiness = respond.Checks.Where(x => x.Name.Equals("Backup database readiness check", StringComparison.InvariantCulture)).First().Status; ServerHealth.BackupLiveness = respond.Checks.Where(x => x.Name.Equals("Backup database connection health check", StringComparison.InvariantCulture)).First().Status; } } catch (Exception e) { if (e is JsonReaderException || e is System.Net.Sockets.SocketException || e is HttpRequestException || e is AggregateException) { ServerHealth.BackupReadiness = "N/A"; ServerHealth.BackupLiveness = "N/A"; return; } throw; } }
private HealthResponse GetHealthByType(HealthType type) { try { // get results var results = _healthCheckRegistry.GetResults(type); // prepare response HealthResponse healthResponse = new HealthResponse(); healthResponse.Checks = results; healthResponse.Status = State.UP; // check if any check is down if (results.Where(e => State.DOWN.Equals(e.Status)).Any()) { healthResponse.Status = State.DOWN; } return(healthResponse); } catch { return(null); } }
/// <summary> /// /// </summary> /// <param name="connectionString">RavenDB connection string</param> /// <param name="database">Optional : database name to connect to</param> /// <returns></returns> public static HealthResponse CheckHealth(string connectionString, string database = null) { try { ConnectionStringParser <RavenConnectionStringOptions> parser = ConnectionStringParser <RavenConnectionStringOptions> .FromConnectionString(connectionString); parser.Parse(); var store = new Raven.Client.Document.DocumentStore { Url = parser.ConnectionStringOptions.Url, DefaultDatabase = database == null ? parser.ConnectionStringOptions.DefaultDatabase : database }; store.Initialize(); // Client doesn't seem to throw an exception until we try to do something so let's just do something simple and get the build number of the server. var build = store.DatabaseCommands.GlobalAdmin.GetBuildNumber(); // Dispose the store object store.Dispose(); return(HealthResponse.Healthy(new { server = store.Url, database = store.DefaultDatabase, serverBuild = build.BuildVersion })); } catch (Exception ex) { return(HealthResponse.Unhealthy(ex)); } }
public void Constructor_GivenEmptyResults_SetsStatusToPass() { var results = Enumerable.Empty <HealthCheckResult>(); var response = new HealthResponse(results); response.Status.Should().Be(HealthStatus.Pass); }
public void Constructor_GivenNonEmptyResultsWithNoStatus_SetsStatusToPass() { var results = new[] { new HealthCheckResult(), new HealthCheckResult() }; var response = new HealthResponse(results); response.Status.Should().Be(HealthStatus.Pass); }
public void Constructor_GivenNullResults_SetsStatusToPass() { IEnumerable <HealthCheckResult> results = null; var response = new HealthResponse(results); response.Status.Should().Be(HealthStatus.Pass); }
public void Constructor_GivenEmptyResults_DoesNotSetChecks() { var results = Enumerable.Empty <HealthCheckResult>(); var response = new HealthResponse(results); response.Checks.Should().BeNull(); response.GetChecks().Should().BeEmpty(); }
public void Constructor_GivenNullResults_DoesNotSetChecks() { IEnumerable <HealthCheckResult> results = null; var response = new HealthResponse(results); response.Checks.Should().BeNull(); response.GetChecks().Should().BeEmpty(); }
public void Constructor_SetsChecks() { var results = new[] { new HealthCheckResult(), new HealthCheckResult() }; var response = new HealthResponse(results); response.Checks.Should().NotBeNull(); response.GetChecks().Should().BeEquivalentTo(results); }
/// <summary> /// Formats the specified <see cref="HealthResponse"/> as draft RFC compliant JSON. /// </summary> /// <param name="healthResponse">The <see cref="HealthResponse"/> to format.</param> /// <returns>The formatted <see cref="HealthResponse"/>.</returns> public string Format(HealthResponse healthResponse) { if (healthResponse == null) { throw new ArgumentNullException(nameof(healthResponse)); } return(JsonConvert.SerializeObject(healthResponse, typeof(HealthResponse), Formatting, Settings)); }
public IActionResult Get() { var response = new HealthResponse() { Name = "Supermarket.WebApi", Status = "All Good" }; return(new OkObjectResult(response)); }
public static HealthResponse CheckHealth(string connectionString) { try { var client = new MongoClient(connectionString); return(HealthResponse.Healthy(new { databases = client.ListDatabases().ToList(), server = client.Settings.Server })); } catch (Exception ex) { return(HealthResponse.Unhealthy(ex)); } }
public void Configuration(IAppBuilder app) { // Setup logging ApplicationLog.AddConsole(); Logger logger = ApplicationLog.CreateLogger <Startup>(); logger.Info("Initializing service"); // Build an IConfiguration instance using the ConfigurationBuilder as normal Dictionary <string, string> collection = new Dictionary <string, string>() { { "key1", "value1" }, { "key2", "value2" } }; var config1 = new ConfigurationBuilder().AddInMemoryCollection(collection).Build(); var config3 = new ConfigurationBuilder().AddJsonFile("Config.json").Build(); // AppConfig is a static class that groups together instances of IConfiguration and makes them available statically anywhere in the application AppConfig.AddConfigurationObject(config1, "memorySource"); AppConfig.AddConfigurationObject(config3, "jsonSource"); // The above configuration sources can now be referenced easily with a static helper function Console.WriteLine("key1 key in memorySource: " + AppConfig.Get("memorySource", "key1")); Console.WriteLine("config:setting key in jsonSource: " + AppConfig.Get("jsonSource", "config:setting")); // Runtime configuration can be updated easily as well AppConfig.Set("jsonSource", "config:setting", "http://localhost:5001"); Console.WriteLine("Modified config:setting key in jsonSource: " + AppConfig.Get("jsonSource", "config:setting")); // Redis health check (Requires StackExchange.Redis) //HealthCheckRegistry.RegisterHealthCheck("Redis", () => RedisHealthCheck.CheckHealth("localhost")); // PostgreSQL health check (Requires Npgsql) //HealthCheckRegistry.RegisterHealthCheck("Postgresql", () => PostgresqlHealthCheck.CheckHealth("Host=localhost;Username=postgres;Password=postgres;Database=postgres")); // SQL Server health check (Requires System.Data.SqlClient) //HealthCheckRegistry.RegisterHealthCheck("SqlServer", () => SqlServerCheck.CheckHealth("Server=localhost;Database=master;User Id=sa;Password=password; ")); // HealthCheckRegistry.RegisterHealthCheck("mongodb", () => MongoHealthCheck.CheckHealth("mongodb://localhost:27017")); /* * Health checks are simply functions that return either healthy or unhealthy with an optional message string */ HealthCheckRegistry.RegisterHealthCheck("MyCustomMonitor", () => HealthResponse.Healthy("Test Message")); HealthCheckRegistry.RegisterHealthCheck("MyCustomMonitor2", () => HealthResponse.Healthy("Test Message2")); HealthCheckRegistry.RegisterHealthCheck("SampleOperation", () => SampleHealthCheckOperation()); // Activate the info endpoint app.UseInfoEndpoint(); // Activate the environment endpoint app.UseEnvironmentEndpoint(); // Activate the health endpoint app.UseHealthEndpoint(); }
public virtual IActionResult Health(HealthRequest request) { HealthResponse response = new HealthResponse() { Data = new Dictionary <string, string>() }; response.Data["healthy"] = "TRUE"; IActionResult result = Ok(response); return(result); }
/// <summary> /// Add a health response and aggregate it to the complete health state. /// </summary> /// <param name="healthResponse"></param> public void AddHealthResponse(HealthResponse healthResponse) { _healthResponse.Resources.Add(healthResponse); if (healthResponse.Status == HealthResponse.StatusEnum.Warning) { _warnings++; _lastWarningMessage = healthResponse.Message; } if (healthResponse.Status == HealthResponse.StatusEnum.Error) { _errors++; _lastErrorMessage = healthResponse.Message; } }
/// <summary> /// Check that a connection to Redis can be established and attempt to pull back server statistics /// </summary> /// <param name="connectionString">A StackExchange.Redis connection string</param> /// <returns>A <see cref="HealthResponse"/> object that contains the return status of this health check</returns> public static HealthResponse CheckHealth(string connectionString) { try { ConnectionMultiplexer conn = ConnectionMultiplexer.Connect(connectionString); string[] server = conn.GetStatus().Split(';'); conn.Close(); return(HealthResponse.Healthy(server[0])); } catch (Exception e) { return(HealthResponse.Unhealthy(e)); } }
public override async Task <HealthResponse> AddPersonHealthData(HealthRequest request, ServerCallContext context) { var healthData = new HealthData { Name = request.Name, HealthParameter1 = request.HealthParameter1, HealthParameter2 = request.HealthParameter2 }; _personDbContext.Health.Add(healthData); await _personDbContext.SaveChangesAsync(); var results = new HealthResponse { Message = "Health Data Save Successfully." }; return(results); }
public async Task <HealthResponse> ServiceHealthAsync() { FulcrumAssert.IsValidatedAndNotNull(tenant, $"{Namespace}: 4B819109-2E18-481F-B001-D87F026D688C"); var aggregator = new ResourceHealthAggregator(tenant, "Lever KeyTranslator Facade"); // TODO: (XF-39) Implement service health in ICoreLogicProvider //aggregator.AddResourceHealth("Database", _keyTranslatorLogic); var result = aggregator.GetAggregatedHealthResponse(); var response = new HealthResponse { Resource = "Frobozz.PersonProfiles", Status = HealthResponse.StatusEnum.Ok }; return(await Task.FromResult(result)); }
public void CheckAudienceAndRatingsService(HealthResponse healthResponse) { NestedServices ns = new NestedServices(); ns.serviceName = "Audience and Ratings Service"; string message = string.Empty; ServiceStatus status = ServiceStatus.Open; Task t = Task.Run(() => { status = _SMSClient.Handler.GetStatus(); }); if (!t.Wait(5000)) { message = "timeout of 5 seconds"; } if (status == ServiceStatus.Closed) { ns.httpResponseCode = HttpStatusCode.ServiceUnavailable; ns.message = "Audience and Ratings Service is down"; healthResponse.nestedServices.Add(ns); } if (!string.IsNullOrEmpty(message)) { ns.httpResponseCode = HttpStatusCode.GatewayTimeout; ns.message = "Audience and Ratings Service is " + message; healthResponse.nestedServices.Add(ns); } }
/// <summary> /// Check that a connection can be established to Postgresql and return the server version /// </summary> /// <param name="connectionString">An Npgsql connection string</param> /// <returns>A <see cref="HealthResponse"/> object that contains the return status of this health check</returns> public static HealthResponse CheckHealth(string connectionString) { try { SqlConnection conn = new SqlConnection(connectionString); conn.Open(); string host = conn.DataSource; string version = conn.ServerVersion; conn.Close(); conn.Dispose(); return(HealthResponse.Healthy(new { host = host, version = version })); } catch (Exception e) { return(HealthResponse.Unhealthy(e)); } }
public void SetStatusCode_SetsStatusCode(HealthStatus status, int expectedStatusCode) { var mockRunner = new Mock <IHealthCheckRunner>(); mockRunner.Setup(m => m.PassStatusCode).Returns(299); mockRunner.Setup(m => m.WarnStatusCode).Returns(399); mockRunner.Setup(m => m.FailStatusCode).Returns(599); var runner = mockRunner.Object; var response = new HealthResponse { Status = status }; response.SetStatusCode(runner); response.StatusCode.Should().Be(expectedStatusCode); }
async Task<bool> CheckIfAlive(ServiceConfig config) { using var httpClient = new HttpClient(); HttpResponseMessage response = await httpClient.GetAsync(config.GetHostLinkFrom(this._config.TestingSystemWorker) + "/health"); if (response.IsSuccessStatusCode) { string apiResponse = await response.Content.ReadAsStringAsync(); HealthResponse obj = JsonConvert.DeserializeObject<HealthResponse>(apiResponse); return obj.Status == "Healthy"; } else { return false; } }
private Dictionary <string, HttpHandler> ConfigureHandlers(OpsEndpointsMiddlewareOptions options) { var ready = new HttpHandler(context => { var op = options; const string readyBody = "ready\n"; if (op.HealthModel.Ready()) { context.Response.StatusCode = 200; context.Response.ContentType = "text/plain"; return(context.Response.WriteAsync(readyBody)); } context.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable; return(Task.CompletedTask); }); var about = new HttpHandler(context => { var op = options; AboutResponse response = op.HealthModel.About().ToAboutResponse(); context.Response.StatusCode = 200; context.Response.ContentType = "application/json"; return(context.Response.WriteAsync(JsonConvert.SerializeObject(response, _serializerSettings))); }); var health = new HttpHandler(context => { var op = options; HealthResponse response = op.HealthModel.Health().ToHealthResponse(); context.Response.StatusCode = 200; context.Response.ContentType = "application/json"; return(context.Response.WriteAsync(JsonConvert.SerializeObject(response, _serializerSettings))); }); return(new Dictionary <string, HttpHandler> { { "/about", about }, { "/health", health }, { "/ready", ready } }); }
public void Constructor_GivenNonEmptyResults_SetsStatusToHighestStatus2() { var results = new[] { new HealthCheckResult { Status = HealthStatus.Pass }, new HealthCheckResult { Status = HealthStatus.Warn }, new HealthCheckResult { Status = HealthStatus.Warn } }; var response = new HealthResponse(results); response.Status.Should().Be(HealthStatus.Warn); }
/// <summary> /// Check that a connection can be established to Postgresql and return the server version /// </summary> /// <param name="connectionString">An Npgsql connection string</param> /// <returns>A <see cref="HealthResponse"/> object that contains the return status of this health check</returns> public static HealthResponse CheckHealth(string connectionString) { try { NpgsqlConnection conn = new NpgsqlConnection(connectionString); NpgsqlConnection.ClearPool(conn); conn.Open(); string host = conn.Host; string version = conn.PostgreSqlVersion.ToString(); int port = conn.Port; conn.Close(); conn.Dispose(); return(HealthResponse.Healthy(new { host = host, port = port, version = version })); } catch (Exception e) { return(HealthResponse.Unhealthy(e)); } }
public async Task <IActionResult> HealthAsync() { var response = new HealthResponse { AppVersion = GetApplicationVersion() }; try { var result = await _queryHandler.Handle <DbHealthCheckQuery, DbHealthCheckResult>(new DbHealthCheckQuery()); response.DatabaseHealth.Successful = result.CanConnect; } catch (Exception ex) { response.DatabaseHealth.Successful = false; response.DatabaseHealth.ErrorMessage = ex.Message; response.DatabaseHealth.Data = ex.Data; } return(!response.DatabaseHealth.Successful ? StatusCode((int)HttpStatusCode.InternalServerError, response) : Ok(response)); }
public async Task <HealthResponse> GetHealthResponseAsync() { var request = new HttpRequestMessage(HttpMethod.Get, path); var httpclient = services.GetService <IHttpClientFactory>(); var client = httpclient.CreateClient("AdminAuth"); HttpResponseMessage result = await client.SendAsync(request); if (result.IsSuccessStatusCode) { var parsedJson = JsonConvert.DeserializeObject <Models.Health>(await result.Content.ReadAsStringAsync()); HealthResponse health = new HealthResponse(true, parsedJson); return(health); } else { var parsedJson = JsonConvert.DeserializeObject <RequestError>(await result.Content.ReadAsStringAsync()); HealthResponse health = new HealthResponse(false, parsedJson); return(health); } }
private async static Task WriteResponse(HttpContext httpContext, HealthReport healthReport) { var results = healthReport.Entries.Select(x => { var entry = x.Value; var result = new HealthCheckResult() { ComponentName = GetComponentName(x.Key), MeasurementName = GetMeasurementName(x.Key), Status = MapStatus(entry.Status), Output = entry.Description, ["exception"] = entry.Exception?.ToString(), ["duration"] = entry.Duration }; // TODO: Detect collisions between entry.Exception/result.Output, // entry.Description/result["description", or entry.Duration/result["duration"]. foreach (var data in entry.Data) { result[data.Key] = data.Value; } return(result); }).ToList(); var response = new HealthResponse(results) { Status = MapStatus(healthReport.Status), Notes = new List <string> { $"TotalDuration: {healthReport.TotalDuration}" } }; httpContext.Response.ContentType = response.ContentType; await httpContext.Response.WriteAsync(response.Serialize(Indent)); }
/// <summary> /// Call e healt check delegate and aggregate the answer to the complete health state. /// </summary> /// <param name="resourceName">The name to use for the resource</param> /// <param name="healthDelegate">A method that returns a health, that we will add to the aggregated health.</param> public async Task AddResourceHealthAsync(string resourceName, GetResourceHealthDelegate healthDelegate) { HealthResponse response; try { response = await healthDelegate(Tenant); if (string.IsNullOrWhiteSpace(response.Resource)) { response.Resource = resourceName; } } catch (Exception e) { response = new HealthResponse(resourceName) { Status = HealthResponse.StatusEnum.Error, Message = e.Message }; } AddHealthResponse(response); }
public async Task <IActionResult> Health() { var response = new HealthResponse { Version = GetApplicationVersion() }; try { const string username = "******"; var query = new GetUserByUsernameQuery(username); await _queryHandler.Handle <GetUserByUsernameQuery, UserDto>(query); response.TestApiHealth.Successful = true; } catch (Exception ex) { response.TestApiHealth.Successful = false; response.TestApiHealth.ErrorMessage = ex.Message; response.TestApiHealth.Data = ex.Data; } try { await _bookingsApiClient.CheckServiceHealthAsync(); response.BookingsApiHealth.Successful = true; } catch (Exception ex) { response.BookingsApiHealth.Successful = false; response.BookingsApiHealth.ErrorMessage = ex.Message; response.BookingsApiHealth.Data = ex.Data; } try { await _userApiClient.CheckServiceHealthAsync(); response.UserApiHealth.Successful = true; } catch (Exception ex) { response.UserApiHealth.Successful = false; response.UserApiHealth.ErrorMessage = ex.Message; response.UserApiHealth.Data = ex.Data; } try { await _videoApiClient.CheckServiceHealthAsync(); response.VideoApiHealth.Successful = true; } catch (Exception ex) { response.VideoApiHealth.Successful = false; response.VideoApiHealth.ErrorMessage = ex.Message; response.VideoApiHealth.Data = ex.Data; } return(response.TestApiHealth.Successful && response.BookingsApiHealth.Successful && response.UserApiHealth.Successful && response.VideoApiHealth.Successful ? Ok(response) : StatusCode((int)HttpStatusCode.InternalServerError, response)); }