Esempio n. 1
0
        protected override async Task WorkLoopIteration()
        {
            ++_dbgIterationsCount;
            var                 waitingLogSeverity = LogLevel.Trace;
            WaitInfoS           waitInfo;
            HttpRequestMessage  httpRequest      = null;
            HttpResponseMessage httpResponse     = null;
            string              httpResponseBody = null;

            try
            {
                httpRequest = BuildHttpRequest(_eTag);

                (httpResponse, httpResponseBody) = await FetchConfigHttpResponseAsync(httpRequest);

                ConfigDelta configDelta;
                (configDelta, waitInfo) = ProcessHttpResponse(httpResponse, httpResponseBody);
                if (configDelta != null)
                {
                    UpdateConfigStore(configDelta);
                    _eTag = httpResponse.Headers.ETag;
                }
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception ex)
            {
                var severity = LogLevel.Error;
                waitInfo = new WaitInfoS(WaitTimeIfAnyError, "Default wait time is used because exception was thrown"
                                         + " while fetching configuration from APM Server and parsing it.");

                if (ex is FailedToFetchConfigException fEx)
                {
                    severity = fEx.Severity;
                    fEx.WaitInfo?.Let(it => { waitInfo = it; });
                }

                if (severity == LogLevel.Error)
                {
                    waitingLogSeverity = LogLevel.Information;
                }

                _logger.IfLevel(severity)
                ?.LogException(ex, "Exception was thrown while fetching configuration from APM Server and parsing it."
                               + " ETag: `{ETag}'. URL: `{Url}'. Apm Server base URL: `{ApmServerUrl}'. WaitInterval: {WaitInterval}."
                               + " dbgIterationsCount: {dbgIterationsCount}."
                               + Environment.NewLine + "+-> Request:{HttpRequest}"
                               + Environment.NewLine + "+-> Response:{HttpResponse}"
                               + Environment.NewLine + "+-> Response body [length: {HttpResponseBodyLength}]:{HttpResponseBody}"
                               , _eTag.AsNullableToString(), _getConfigAbsoluteUrl, HttpClientInstance.BaseAddress, waitInfo.Interval.ToHms(),
                               _dbgIterationsCount
                               , httpRequest == null ? " N/A" : Environment.NewLine + TextUtils.Indent(httpRequest.ToString())
                               , httpResponse == null ? " N/A" : Environment.NewLine + TextUtils.Indent(httpResponse.ToString())
                               , httpResponseBody == null ? "N/A" : httpResponseBody.Length.ToString()
                               , httpResponseBody == null ? " N/A" : Environment.NewLine + TextUtils.Indent(httpResponseBody));
            }
            finally
            {
                httpRequest?.Dispose();
                httpResponse?.Dispose();
            }

            _logger.IfLevel(waitingLogSeverity)
            ?.Log("Waiting {WaitInterval}... {WaitReason}. dbgIterationsCount: {dbgIterationsCount}."
                  , waitInfo.Interval.ToHms(), waitInfo.Reason, _dbgIterationsCount);
            await _agentTimer.Delay(_agentTimer.Now + waitInfo.Interval, CtsInstance.Token);
        }