protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (!request.Headers.Contains(ServiceFabricDiagnostics.CorrelationHeaderName))
            {
                SetCorrelationHeader(request, ServiceFabricDiagnostics.GetRequestCorrelationId());
            }

            if (!request.Headers.Contains(ServiceFabricDiagnostics.RequestOriginHeaderName) && this.context != null)
            {
                SetRequestOriginHeader(request, this.context.ServiceName.ToString());
            }

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            string origin        = GetHeaderValue(request, ServiceFabricDiagnostics.RequestOriginHeaderName);
            string correlationId = GetHeaderValue(request, ServiceFabricDiagnostics.CorrelationHeaderName);

            HttpClientEventSource.Current.HttpRequestStart(request, origin, correlationId);
            try
            {
                return(await base.SendAsync(request, cancellationToken));
            }
            catch (Exception ex)
            {
                HttpClientEventSource.Current.HttpRequestFailed(request, origin, correlationId, ex);
                throw;
            }
            finally
            {
                stopwatch.Stop();
                HttpClientEventSource.Current.HttpRequestStop(request, origin, correlationId, stopwatch.Elapsed.TotalMilliseconds);
            }
        }
 public void ServiceMessage(ServiceContext serviceContext, string message, params object[] args)
 {
     if (this.IsEnabled())
     {
         string finalMessage = string.Format(message, args);
         ServiceMessage(
             serviceContext.ServiceName.ToString(),
             serviceContext.ServiceTypeName,
             GetReplicaOrInstanceId(serviceContext),
             serviceContext.PartitionId,
             serviceContext.CodePackageActivationContext.ApplicationName,
             serviceContext.CodePackageActivationContext.ApplicationTypeName,
             serviceContext.NodeContext.NodeName,
             ServiceFabricDiagnostics.GetRequestCorrelationId() ?? string.Empty,
             finalMessage);
     }
 }
        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);
        }
        public void ServiceRequestFailed(ServiceContext serviceContext, string method, string requestTypeName, string exception, string correlationId = null, string fromService = null)
        {
            string serviceName = serviceContext != null?serviceContext.ServiceName.ToString() : null;

            string serviceTypeName     = serviceContext != null ? serviceContext.ServiceTypeName : null;
            long   replicaOrInstanceId = serviceContext != null?GetReplicaOrInstanceId(serviceContext) : 0;

            Guid   partitionId         = serviceContext != null ? serviceContext.PartitionId : Guid.Empty;
            string applicationName     = serviceContext != null ? serviceContext.CodePackageActivationContext.ApplicationName : null;
            string applicationTypeName = serviceContext != null ? serviceContext.CodePackageActivationContext.ApplicationTypeName : null;

            if (correlationId == null)
            {
                correlationId = ServiceFabricDiagnostics.GetRequestCorrelationId() ?? string.Empty;
            }
            if (fromService == null)
            {
                fromService = ServiceFabricDiagnostics.GetRequestOrigin() ?? string.Empty;
            }
            ServiceRequestFailed(requestTypeName, method, exception, serviceName, serviceTypeName, replicaOrInstanceId, partitionId, applicationName, applicationTypeName, correlationId, fromService);
        }
Пример #5
0
        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));
                    }
                }
            }
        }