private void FlowCorrelationIdAndRequestOrigin(HttpContext context) { string correlationId = null; StringValues correlationIdHeader, originHeader; if (context.Request.Headers.TryGetValue(ServiceFabricDiagnostics.CorrelationHeaderName, out correlationIdHeader)) { correlationId = correlationIdHeader.FirstOrDefault(); } if (String.IsNullOrWhiteSpace(correlationId)) { correlationId = Guid.NewGuid().ToString("N"); } if (context.Request.Headers.TryGetValue(ServiceFabricDiagnostics.RequestOriginHeaderName, out originHeader)) { ServiceFabricDiagnostics.SetRequestOrigin(originHeader.FirstOrDefault()); } ServiceFabricDiagnostics.SetRequestCorrelationId(correlationId); }
private async Task CheckServiceHealthAsync(Uri serviceName, Func <Uri, ServicePartitionInformation, Task <string> > func, CancellationToken cancellationToken) { ServiceFabricDiagnostics.SetRequestCorrelationId(Guid.NewGuid().ToString()); var partitionList = await fabricClient.QueryManager.GetPartitionListAsync(serviceName); foreach (var partition in partitionList) { HealthInformation healthInfo = null; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); try { string message = await func(serviceName, partition.PartitionInformation); if (!string.IsNullOrEmpty(message)) { healthInfo = this.ToHealthInformation(HealthCheckPropertyName, HealthState.Warning, this.healthInfoTimeToLive, $"Health check of {serviceName}, partition {partition.PartitionInformation.Id} failed with {message}", removedWhenExpired: true); } } catch (Exception ex) { healthInfo = this.ToHealthInformation(HealthCheckPropertyName, HealthState.Warning, this.healthInfoTimeToLive, $"Health check of {serviceName}, partition {partition.PartitionInformation.Id} failed with exception {ex}", removedWhenExpired: true); } finally { stopwatch.Stop(); if (!cancellationToken.IsCancellationRequested) { if (healthInfo == null) { if (stopwatch.Elapsed > responseTimeWarningThreshold) { healthInfo = this.ToHealthInformation(HealthCheckPropertyName, HealthState.Warning, this.healthInfoTimeToLive, $"Health check of {serviceName}, partition {partition.PartitionInformation.Id} took {stopwatch.Elapsed.TotalSeconds} seconds to complete.", removedWhenExpired: true); } else { healthInfo = this.ToHealthInformation(HealthCheckPropertyName, HealthState.Ok, this.healthInfoTimeToLive, "OK", removedWhenExpired: true); } } this.fabricClient.HealthManager.ReportHealth(new PartitionHealthReport(partition.PartitionInformation.Id, healthInfo)); } } } }