Пример #1
0
        private static async Task StartMonitoringAsync(string uri, string key, string collection, CancellationToken ctsToken)
        {
            IRemainingWorkEstimator estimator = await CreateEstimatorAsync(uri, key, collection);

            StringBuilder builder = new StringBuilder();

            while (!ctsToken.IsCancellationRequested)
            {
                builder.Clear();

                IReadOnlyList <RemainingPartitionWork> remainingWork = await estimator.GetEstimatedRemainingWorkPerPartitionAsync();

                for (int i = 0; i < remainingWork.Count; i++)
                {
                    var work = remainingWork[i];
                    if (i != 0)
                    {
                        builder.Append(",");
                    }
                    builder.AppendFormat(work.PartitionKeyRangeId + ":" + work.RemainingWork);
                }
                Console.WriteLine($"### Estimated work: {builder}");

                try
                {
                    await Task.Delay(5000, ctsToken);
                }
                catch (TaskCanceledException) { }
            }
        }
        public async Task <CosmosDBTriggerMetrics> GetMetricsAsync()
        {
            int  partitionCount = 0;
            long remainingWork  = 0;
            IReadOnlyList <RemainingPartitionWork> partitionWorkList = null;

            try
            {
                IRemainingWorkEstimator workEstimator = await GetWorkEstimatorAsync();

                partitionWorkList = await workEstimator.GetEstimatedRemainingWorkPerPartitionAsync();

                partitionCount = partitionWorkList.Count;
                remainingWork  = partitionWorkList.Sum(item => item.RemainingWork);
            }
            catch (Exception e) when(e is DocumentClientException || e is InvalidOperationException)
            {
                if (!TryHandleDocumentClientException(e))
                {
                    _logger.LogWarning("Unable to handle {0}: {1}", e.GetType().ToString(), e.Message);
                    if (e is InvalidOperationException)
                    {
                        throw;
                    }
                }
            }
            catch (System.Net.Http.HttpRequestException e)
            {
                string errormsg;

                var webException = e.InnerException as WebException;
                if (webException != null &&
                    webException.Status == WebExceptionStatus.ProtocolError)
                {
                    string statusCode = ((HttpWebResponse)webException.Response).StatusCode.ToString();
                    string statusDesc = ((HttpWebResponse)webException.Response).StatusDescription;
                    errormsg = string.Format("CosmosDBTrigger status {0}: {1}.", statusCode, statusDesc);
                }
                else if (webException != null &&
                         webException.Status == WebExceptionStatus.NameResolutionFailure)
                {
                    errormsg = string.Format("CosmosDBTrigger Exception message: {0}.", webException.Message);
                }
                else
                {
                    errormsg = e.ToString();
                }

                _logger.LogWarning(errormsg);
            }

            return(new CosmosDBTriggerMetrics
            {
                Timestamp = DateTime.UtcNow,
                PartitionCount = partitionCount,
                RemainingWork = remainingWork
            });
        }