Esempio n. 1
0
        private static void LogSeed(int seed, ITestOutputHelper xUnitOutputHelper, IApmLogger loggerForNonXunitSinks, string dbgSeedDescription)
        {
            var message = $"Random generator seed: {seed} ({dbgSeedDescription})";

            xUnitOutputHelper.WriteLine(message);
            FindLevelToLog(loggerForNonXunitSinks)?.Let(foundLogLevel => loggerForNonXunitSinks.IfLevel(foundLogLevel)?.Log(message));
        }
Esempio n. 2
0
        private void Serialize(object item, string eventType, TextWriter writer)
        {
            writer.Write("{\"");
            writer.Write(eventType);
            writer.Write("\":");
            var traceLogger = _logger?.IfLevel(LogLevel.Trace);

            if (traceLogger.HasValue)
            {
                var serialized = _payloadItemSerializer.Serialize(item);
                writer.Write(serialized);
                traceLogger.Value.Log("Serialized item to send: {ItemToSend} as {SerializedItem}", item, serialized);
            }
            else
            {
                _payloadItemSerializer.Serialize(item, writer);
            }

            writer.Write("}\n");
        }
Esempio n. 3
0
        /// <summary>
        /// We need this private ctor to avoid calling configStore.CurrentSnapshot twice (and thus possibly using different
        /// snapshots)
        /// when passing isEnabled: initialConfigSnapshot.CentralConfig and config: initialConfigSnapshot to base
        /// </summary>
        private CentralConfigFetcher(IApmLogger logger, IConfigStore configStore, IConfigSnapshot initialConfigSnapshot, Service service
                                     , HttpMessageHandler httpMessageHandler, IAgentTimer agentTimer, string dbgName
                                     )
            : base(/* isEnabled: */ initialConfigSnapshot.CentralConfig, logger, ThisClassName, service, initialConfigSnapshot, httpMessageHandler)
        {
            _logger = logger?.Scoped(ThisClassName + (dbgName == null ? "" : $" (dbgName: `{dbgName}')"));

            _initialSnapshot = initialConfigSnapshot;

            var isCentralConfigOptEqDefault = _initialSnapshot.CentralConfig == ConfigConsts.DefaultValues.CentralConfig;
            var centralConfigStatus         = _initialSnapshot.CentralConfig ? "enabled" : "disabled";

            if (!isCentralConfigOptEqDefault)
            {
                centralConfigStatus = centralConfigStatus.ToUpper();
            }
            _logger.IfLevel(isCentralConfigOptEqDefault ? LogLevel.Debug : LogLevel.Information)
            ?.Log("Central configuration feature is {CentralConfigStatus} because CentralConfig option's value is {CentralConfigOptionValue}"
                  + " (default value is {CentralConfigOptionDefaultValue})"
                  , centralConfigStatus, _initialSnapshot.CentralConfig, ConfigConsts.DefaultValues.CentralConfig);

            if (!_initialSnapshot.CentralConfig)
            {
                return;
            }

            _configStore = configStore;

            _agentTimer = agentTimer ?? new AgentTimer();

            _getConfigAbsoluteUrl = BackendCommUtils.ApmServerEndpoints.BuildGetConfigAbsoluteUrl(initialConfigSnapshot.ServerUrls.First(), service);
            _logger.Debug()
            ?.Log("Combined absolute URL for APM Server get central configuration endpoint: `{Url}'. Service: {Service}."
                  , _getConfigAbsoluteUrl, service);

            StartWorkLoop();
        }
Esempio n. 4
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);
        }