public static void HealthCheckData(ILogger logger, HealthCheckRegistration registration, HealthReportEntry entry) { if (entry.Data.Count > 0 && logger.IsEnabled(LogLevel.Debug)) { logger.Log( LogLevel.Debug, EventIds.HealthCheckData, new HealthCheckDataLogValue(registration.Name, entry.Data), null, (state, ex) => state.ToString()); } }
private HttpHealthCheckParameters GetParameters(IServiceProvider provider, string checkName) { IOptions <HealthCheckServiceOptions> options = provider.GetRequiredService <IOptions <HealthCheckServiceOptions> >(); HealthCheckRegistration registration = options.Value.Registrations.SingleOrDefault(r => string.Equals(checkName, r.Name, StringComparison.Ordinal)); Assert.IsNotNull(registration, "HealthCheck should be registered"); IHealthCheck check = registration.Factory(provider); Assert.IsInstanceOfType(check, typeof(HttpEndpointHealthCheck)); return(((HttpEndpointHealthCheck)check).Parameters); }
public static IHealthChecksBuilder AddSqlServer(this IHealthChecksBuilder builder, Func <IServiceProvider, string> connectionStringFactory, string query = default, string name = default, HealthStatus?failureStatus = default, IEnumerable <string> tags = default, TimeSpan?timeout = default) { if (connectionStringFactory == null) { throw new HealthCheckException(Resource.ERROR_CONNECTION_STRING_FACTORY_NULL); } name = string.IsNullOrEmpty(name) ? NAME : name; query = string.IsNullOrEmpty(query) ? QUERY : query; var registration = new HealthCheckRegistration(name, sp => new SqlServerHealthCheck(sp, connectionStringFactory(sp), query), failureStatus, tags, timeout); return(builder.Add(registration)); }
public IHealthChecksBuilder Add(HealthCheckRegistration registration) { if (registration == null) { throw new ArgumentNullException(nameof(registration)); } Services.Configure <HealthCheckServiceOptions>(options => { options.Registrations.Add(registration); }); return(this); }
#pragma warning restore SYSLIB1006 public static void HealthCheckEnd(ILogger logger, HealthCheckRegistration registration, HealthReportEntry entry, TimeSpan duration) { switch (entry.Status) { case HealthStatus.Healthy: HealthCheckEndHealthy(logger, registration.Name, entry.Status, duration.TotalMilliseconds, entry.Description); break; case HealthStatus.Degraded: HealthCheckEndDegraded(logger, registration.Name, entry.Status, duration.TotalMilliseconds, entry.Description, entry.Exception); break; case HealthStatus.Unhealthy: HealthCheckEndUnhealthy(logger, registration.Name, entry.Status, duration.TotalMilliseconds, entry.Description, entry.Exception); break; } }
private static void AddSmtpHealthCheckCallback( HealthCheckRegistration registration, out AddSmtpHealthCheckArguments arguments) { var factoryTarget = registration.Factory.Target; var options = factoryTarget ?.GetType() .GetField("options") ?.GetValue(factoryTarget) as SmtpHealthCheckOptions; arguments = new AddSmtpHealthCheckArguments { FailureStatus = registration.FailureStatus, Name = registration.Name, Options = options, Tags = registration.Tags, Timeout = registration.Timeout, }; }
public void PostConfigure(string name, HealthCheckServiceOptions options) { HealthCheckRegistration[] list = options.Registrations.ToArray(); options.Registrations.Clear(); var logger = _serviceProvider.GetRequiredService <ILogger <CachedHealthCheck> >(); foreach (HealthCheckRegistration registration in list) { // Wrap health checks with a caching wrapper. var newRegistration = new HealthCheckRegistration( registration.Name, new CachedHealthCheck(_serviceProvider, registration.Factory, logger), registration.FailureStatus, registration.Tags); options.Registrations.Add(newRegistration); } }
private HealthCheckServiceOptions CreateOptions() { var result = new HealthCheckServiceOptions(); foreach (var microsoftRegistration in options.Registrations) { result.Registrations.Add(microsoftRegistration); } foreach (var(name, check) in vostokTracker) { if (check.IsAdapter()) { continue; } var registration = new HealthCheckRegistration(name, check.ToMicrosoftCheck(), null, new[] { "vostok" }); result.Registrations.Add(registration); } return(result); }
public IHealthChecksBuilder Add(HealthCheckRegistration registration) { return(_builder.Add(registration)); }
/// <summary> /// Adds a health check registration delegate to the builder. /// </summary> /// <param name="registration">The health check registration delegate.</param> public IHealthCheckRunnerBuilder AddHealthCheck(HealthCheckRegistration registration) { Services.Configure <HealthCheckRunnerOptions>(Name, options => options.Registrations.Add(registration)); return(this); }
private void AddHealthCheck(ICollection <HealthCheckRegistration> healthChecks, Type healthCheckType, string healthCheckName) { var registration = new HealthCheckRegistration(healthCheckName, s => (IHealthCheck)ActivatorUtilities.GetServiceOrCreateInstance(s, healthCheckType), FailureStatus, Tags, TimeOut); healthChecks.Add(registration); }
public IHealthChecksBuilder Add(HealthCheckRegistration registration) { builder.Add(registration); registrations.Add(registration); return(this); }
private async Task <HealthReportEntry> RunCheckAsync(HealthCheckRegistration registration, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var scope = _scopeFactory.CreateAsyncScope(); await using (scope.ConfigureAwait(false)) { var healthCheck = registration.Factory(scope.ServiceProvider); // If the health check does things like make Database queries using EF or backend HTTP calls, // it may be valuable to know that logs it generates are part of a health check. So we start a scope. using (_logger.BeginScope(new HealthCheckLogScope(registration.Name))) { var stopwatch = ValueStopwatch.StartNew(); var context = new HealthCheckContext { Registration = registration }; Log.HealthCheckBegin(_logger, registration.Name); HealthReportEntry entry; CancellationTokenSource?timeoutCancellationTokenSource = null; try { HealthCheckResult result; var checkCancellationToken = cancellationToken; if (registration.Timeout > TimeSpan.Zero) { timeoutCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); timeoutCancellationTokenSource.CancelAfter(registration.Timeout); checkCancellationToken = timeoutCancellationTokenSource.Token; } result = await healthCheck.CheckHealthAsync(context, checkCancellationToken).ConfigureAwait(false); var duration = stopwatch.GetElapsedTime(); entry = new HealthReportEntry( status: result.Status, description: result.Description, duration: duration, exception: result.Exception, data: result.Data, tags: registration.Tags); Log.HealthCheckEnd(_logger, registration, entry, duration); Log.HealthCheckData(_logger, registration, entry); } catch (OperationCanceledException ex) when(!cancellationToken.IsCancellationRequested) { var duration = stopwatch.GetElapsedTime(); entry = new HealthReportEntry( status: registration.FailureStatus, description: "A timeout occurred while running check.", duration: duration, exception: ex, data: null, tags: registration.Tags); Log.HealthCheckError(_logger, registration, ex, duration); } // Allow cancellation to propagate if it's not a timeout. catch (Exception ex) when(ex as OperationCanceledException == null) { var duration = stopwatch.GetElapsedTime(); entry = new HealthReportEntry( status: registration.FailureStatus, description: ex.Message, duration: duration, exception: ex, data: null, tags: registration.Tags); Log.HealthCheckError(_logger, registration, ex, duration); } finally { timeoutCancellationTokenSource?.Dispose(); } return(entry); } } }
public IHealthChecksBuilder Add(HealthCheckRegistration registration) { return(this); }
public static void HealthCheckError(ILogger logger, HealthCheckRegistration registration, Exception exception, TimeSpan duration) => HealthCheckError(logger, registration.Name, duration.TotalMilliseconds, exception);
private async Task <HealthReportEntry> RunCheckAsync(IServiceScope scope, HealthCheckRegistration registration, CacheableHealthChecksServiceOptions cacheOptionsValue, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); // If the health check does things like make Database queries using EF or backend HTTP calls, // it may be valuable to know that logs it generates are part of a health check. So we start a scope. using (logger.BeginScope(new HealthCheckLogScope(registration.Name))) { var cacheKey = $"{cacheOptionsValue.CachePrefix}{registration.Name}"; if (memoryCache.TryGetValue(cacheKey, out HealthReportEntry entry)) { Log.HealthCheckFromCache(logger, registration, entry); Log.HealthCheckData(logger, registration, entry); } else { var cacheable = registration.Tags.Any(a => a == cacheOptionsValue.Tag); var optionsSnapshot = scope.ServiceProvider.GetService <IOptionsSnapshot <RegistrationOptions> >(); var healthCheck = registration.Factory(scope.ServiceProvider); var stopwatch = ValueStopwatch.StartNew(); var context = new HealthCheckContext { Registration = registration }; Log.HealthCheckBegin(logger, registration); CancellationTokenSource timeoutCancellationTokenSource = null; try { HealthCheckResult result; var checkCancellationToken = cancellationToken; if (registration.Timeout > TimeSpan.Zero) { timeoutCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); timeoutCancellationTokenSource.CancelAfter(registration.Timeout); checkCancellationToken = timeoutCancellationTokenSource.Token; } result = await healthCheck.CheckHealthAsync(context, checkCancellationToken) .ConfigureAwait(false); var duration = stopwatch.GetElapsedTime(); if (cacheable) { entry = BuildAndCacheReportEntry(cacheKey, registration, cacheOptionsValue, optionsSnapshot, result, duration); } else { entry = new HealthReportEntry( status: result.Status, description: result.Description, duration: duration, exception: result.Exception, data: result.Data, tags: registration.Tags); } Log.HealthCheckEnd(logger, registration, entry, duration); Log.HealthCheckData(logger, registration, entry); } catch (OperationCanceledException ex) when(!cancellationToken.IsCancellationRequested) { var duration = stopwatch.GetElapsedTime(); if (cacheable && cacheOptionsValue.CacheExceptions) { entry = BuildAndCacheExceptionReport(cacheKey, registration, cacheOptionsValue, optionsSnapshot, ex, duration); } else { entry = new HealthReportEntry( status: HealthStatus.Unhealthy, description: "A timeout occured while running check.", duration: duration, exception: ex, data: null); } Log.HealthCheckError(logger, registration, ex, duration); } // Allow cancellation to propagate if it's not a timeout. catch (Exception ex) when(ex as OperationCanceledException == null) { var duration = stopwatch.GetElapsedTime(); if (cacheable && cacheOptionsValue.CacheExceptions) { entry = BuildAndCacheExceptionReport(cacheKey, registration, cacheOptionsValue, optionsSnapshot, ex, duration); } else { entry = new HealthReportEntry( status: HealthStatus.Unhealthy, description: ex.Message, duration: duration, exception: ex, data: null); } Log.HealthCheckError(logger, registration, ex, duration); } finally { timeoutCancellationTokenSource?.Dispose(); } } return(entry); } }
public static void HealthCheckFromCache(ILogger logger, HealthCheckRegistration registration, HealthReportEntry entry) { healthCheckFromCache(logger, registration.Name, null); }
private bool ContainsTag(HealthCheckRegistration registration) { return(registration.Tags.Contains(Tag)); }
static bool PassAlways(HealthCheckRegistration _) => true;
public static void HealthCheckBegin(ILogger logger, HealthCheckRegistration registration) { _healthCheckBegin(logger, registration.Name, null); }