Beispiel #1
0
        private T SendDataOverWire <T>(ICollectorWire wire, string method, params object[] data)
        {
            try
            {
                var serializedData = _serializer.Serialize(data);
                var responseBody   = wire.SendData(method, _connectionInfo, serializedData);
                return(ParseResponse <T>(responseBody));
            }
            catch (Exceptions.HttpException ex)
            {
                Log.DebugFormat("Received a {0} {1} response invoking method \"{2}\"", (int)ex.StatusCode, ex.StatusCode, method);

                if (ex.StatusCode == HttpStatusCode.Gone)
                {
                    Log.Info("The server has requested that the agent disconnect. The agent is shutting down.");
                }

                throw;
            }
            catch (Exception ex)
            {
                Log.DebugFormat("An error occurred invoking method \"{0}\": {1}", method, ex);
                throw;
            }
        }
Beispiel #2
0
        private void Disable()
        {
            _dataRequestWire = new NoOpCollectorWire();

            if (string.IsNullOrEmpty(_configuration.AgentRunId?.ToString()))
            {
                return;
            }

            EventBus <ServerConfigurationUpdatedEvent> .Publish(new ServerConfigurationUpdatedEvent(ServerConfiguration.GetDefault()));
        }
Beispiel #3
0
        public ConnectionHandler(ISerializer serializer, ICollectorWireFactory collectorWireFactory, IProcessStatic processStatic, IDnsStatic dnsStatic, ILabelsService labelsService, Environment environment, ISystemInfo systemInfo, IAgentHealthReporter agentHealthReporter, IEnvironment environmentVariableHelper)
        {
            _serializer                = serializer;
            _collectorWireFactory      = collectorWireFactory;
            _processStatic             = processStatic;
            _dnsStatic                 = dnsStatic;
            _labelsService             = labelsService;
            _environment               = environment;
            _systemInfo                = systemInfo;
            _agentHealthReporter       = agentHealthReporter;
            _environmentVariableHelper = environmentVariableHelper;

            _connectionInfo  = new ConnectionInfo(_configuration);
            _dataRequestWire = new NoOpCollectorWire();
        }
Beispiel #4
0
        public void Connect()
        {
            // Need to disable before connecting so that we can easily tell that we just finished connecting during a configuration update
            Disable();

            try
            {
                ValidateNotBothHsmAndSecurityPolicies(_configuration);

                var preconnectResult = SendPreconnectRequest();
                _connectionInfo = new ConnectionInfo(_configuration, preconnectResult.RedirectHost);

                ValidateAgentTokenSettingToPoliciesReceived(preconnectResult.SecurityPolicies);

                if (_configuration.SecurityPoliciesTokenExists)
                {
                    ValidateAgentExpectedSecurityPoliciesExist(preconnectResult.SecurityPolicies);
                    ValidateAllRequiredPoliciesFromServerExist(preconnectResult.SecurityPolicies);

                    var securityPoliciesConfiguration = new SecurityPoliciesConfiguration(preconnectResult.SecurityPolicies);
                    EventBus <SecurityPoliciesConfigurationUpdatedEvent> .Publish(new SecurityPoliciesConfigurationUpdatedEvent(securityPoliciesConfiguration));
                }

                var serverConfiguration = SendConnectRequest();
                EventBus <ServerConfigurationUpdatedEvent> .Publish(new ServerConfigurationUpdatedEvent(serverConfiguration));

                LogSecurityPolicySettingsOnceAllSettingsResolved();
                GenerateFasterEventHarvestConfigMetrics(serverConfiguration.EventHarvestConfig);

                _dataRequestWire = _collectorWireFactory.GetCollectorWire(_configuration, serverConfiguration.RequestHeadersMap, _agentHealthReporter);
                SendAgentSettings();

                EventBus <AgentConnectedEvent> .Publish(new AgentConnectedEvent());

                Log.Info("Agent fully connected.");
            }

            catch (Exception e)
            {
                Disable();
                Log.Error($"Unable to connect to the New Relic service at {_connectionInfo} : {e}");
                throw;
            }
        }
Beispiel #5
0
        public void SetUp()
        {
            _compositeTestAgent = new CompositeTestAgent();
            var environment          = _compositeTestAgent.Container.Resolve <IEnvironment>();
            var collectorWireFactory = Mock.Create <ICollectorWireFactory>();

            _collectorWire = Mock.Create <ICollectorWire>();
            var systemInfo       = Mock.Create <ISystemInfo>();
            var processStatic    = Mock.Create <IProcessStatic>();
            var agentEnvironment = new NewRelic.Agent.Core.Environment(systemInfo, processStatic);

            Mock.Arrange(() => collectorWireFactory.GetCollectorWire(null, Arg.IsAny <IAgentHealthReporter>())).IgnoreArguments().Returns(_collectorWire);
            Mock.Arrange(() => _collectorWire.SendData("preconnect", Arg.IsAny <ConnectionInfo>(), Arg.IsAny <string>()))
            .Returns("{'return_value': { 'redirect_host': ''}}");

            _agentHealthReporter = Mock.Create <IAgentHealthReporter>();

            _connectionHandler = new ConnectionHandler(new JsonSerializer(), collectorWireFactory, Mock.Create <IProcessStatic>(), Mock.Create <IDnsStatic>(),
                                                       Mock.Create <ILabelsService>(), agentEnvironment, systemInfo, _agentHealthReporter, Mock.Create <IEnvironment>());
        }
        public void Setup()
        {
            _compositeTestAgent = new CompositeTestAgent();
            var environment          = _compositeTestAgent.Container.Resolve <IEnvironment>();
            var collectorWireFactory = Mock.Create <ICollectorWireFactory>();

            _collectorWire = Mock.Create <ICollectorWire>();
            var systemInfo          = Mock.Create <ISystemInfo>();
            var processStatic       = Mock.Create <IProcessStatic>();
            var agentEnvironment    = new NewRelic.Agent.Core.Environment(systemInfo, processStatic);
            var agentHealthReporter = Mock.Create <IAgentHealthReporter>();

            Mock.Arrange(() => collectorWireFactory.GetCollectorWire(null, Arg.IsAny <IAgentHealthReporter>())).IgnoreArguments().Returns(_collectorWire);
            Mock.Arrange(() => environment.GetEnvironmentVariable("NEW_RELIC_SECURITY_POLICIES_TOKEN")).Returns("ffff-fbff-ffff-ffff");

            _connectRawData = string.Empty;
            _receivedSecurityPoliciesException = false;

            _connectionHandler = new ConnectionHandler(new JsonSerializer(), collectorWireFactory, Mock.Create <IProcessStatic>(), Mock.Create <IDnsStatic>(),
                                                       Mock.Create <ILabelsService>(), agentEnvironment, systemInfo, agentHealthReporter, Mock.Create <IEnvironment>());
        }