use of com.newrelic.agent.service.ServiceManagerImpl in project newrelic-java-agent by newrelic.
the class MockCoreService method getMockAgentAndBootstrapTheServiceManager.
public static CoreService getMockAgentAndBootstrapTheServiceManager() throws Exception {
AgentHelper.initializeConfig();
MockCoreService mockCoreService = new MockCoreService();
mockCoreService.setInstrumentation(Mockito.mock(InstrumentationProxy.class));
Mockito.when(mockCoreService.getInstrumentation().getAllLoadedClasses()).thenReturn(new Class[] {});
ConfigService configService = ConfigServiceFactory.createConfigService(mock(Logger.class), false);
ServiceManager serviceManager = new ServiceManagerImpl(mockCoreService, configService);
ServiceFactory.setServiceManager(serviceManager);
serviceManager.start();
return mockCoreService;
}
use of com.newrelic.agent.service.ServiceManagerImpl in project newrelic-java-agent by newrelic.
the class AgentLinkingMetadataTest method getLinkingMetadata.
@Test
public void getLinkingMetadata() {
// Given
final String expectedTraceId = "traceId1234";
final String expectedSpanId = "spanId5678";
final String expectedEntityGuid = "entityGuid91011";
final String expectedEntityName = "entityName91011";
final String expectedEntityType = AgentLinkingMetadata.ENTITY_TYPE_DEFAULT;
TraceMetadataImpl traceMetadataMock = mock(TraceMetadataImpl.class);
ServiceManagerImpl serviceManagerMock = mock(ServiceManagerImpl.class);
RPMServiceManagerImpl rpmServiceManagerMock = mock(RPMServiceManagerImpl.class);
RPMService rpmServiceMock = mock(RPMService.class);
ConfigServiceImpl configServiceMock = mock(ConfigServiceImpl.class);
AgentConfigImpl agentConfigMock = mock(AgentConfigImpl.class);
ServiceFactory.setServiceManager(serviceManagerMock);
// When
when(traceMetadataMock.getTraceId()).thenReturn(expectedTraceId);
when(traceMetadataMock.getSpanId()).thenReturn(expectedSpanId);
when(serviceManagerMock.getRPMServiceManager()).thenReturn(rpmServiceManagerMock);
when(serviceManagerMock.getConfigService()).thenReturn(configServiceMock);
when(rpmServiceManagerMock.getRPMService()).thenReturn(rpmServiceMock);
when(configServiceMock.getDefaultAgentConfig()).thenReturn(agentConfigMock);
when(agentConfigMock.getApplicationName()).thenReturn(expectedEntityName);
when(rpmServiceMock.getEntityGuid()).thenReturn(expectedEntityGuid);
// Then
Map<String, String> linkingMetadata = AgentLinkingMetadata.getLinkingMetadata(traceMetadataMock, ServiceFactory.getConfigService(), ServiceFactory.getRPMService());
assertFalse("linkingMetadata map shouldn't be empty", linkingMetadata.isEmpty());
// Can't assert on a specific hostname value as it will resolve to the actual hostname of the machine running the test
assertFalse("hostname shouldn't be empty", linkingMetadata.get(AgentLinkingMetadata.HOSTNAME).isEmpty());
assertEquals(expectedEntityGuid, linkingMetadata.get(AgentLinkingMetadata.ENTITY_GUID));
assertEquals(expectedEntityName, linkingMetadata.get(AgentLinkingMetadata.ENTITY_NAME));
assertEquals(expectedEntityType, linkingMetadata.get(AgentLinkingMetadata.ENTITY_TYPE));
assertEquals(expectedTraceId, linkingMetadata.get(AgentLinkingMetadata.TRACE_ID));
assertEquals(expectedSpanId, linkingMetadata.get(AgentLinkingMetadata.SPAN_ID));
}
use of com.newrelic.agent.service.ServiceManagerImpl in project newrelic-java-agent by newrelic.
the class Agent method tryToInitializeServiceManager.
private static boolean tryToInitializeServiceManager(Instrumentation inst) {
try {
CoreService coreService = new CoreServiceImpl(inst);
ConfigService configService = ConfigServiceFactory.createConfigService(Agent.LOG, System.getProperty("newrelic.checkconfig") != null);
ServiceManager serviceManager = new ServiceManagerImpl(coreService, configService);
ServiceFactory.setServiceManager(serviceManager);
if (isLicenseKeyEmpty(serviceManager.getConfigService().getDefaultAgentConfig().getLicenseKey())) {
LOG.error("license_key is empty in the config. Not starting New Relic Agent.");
return false;
}
if (!serviceManager.getConfigService().getDefaultAgentConfig().isAgentEnabled()) {
LOG.warning("agent_enabled is false in the config. Not starting New Relic Agent.");
return false;
}
// Now that we know the agent is enabled, add the ApiClassTransformer
BootstrapLoader.forceCorrectNewRelicApi(inst);
// init problem classes before class transformer service is active
InitProblemClasses.loadInitialClasses();
} catch (ForceDisconnectException e) {
/* Note: Our use of ForceDisconnectException is a bit misleading here as we haven't even tried to connect
* to RPM at this point (that happens a few lines down when we call serviceManager.start()). This exception
* comes from ConfigServiceFactory when it attempts to validate the local yml and finds that both HSM and
* LASP are enabled. The LASP spec says in this scenario that "Shutdown will follow the behavior of the
* ForceDisconnectException response from "New Relic." Not specifically that we should throw ForceDisconnectException.
* Perhaps we should throw a different, more accurately named exception, that simply has the same behavior
* as ForceDisconnectException as it will be replaced by a 410 response code in Protocol 17.
*/
LOG.log(Level.SEVERE, e.getMessage());
return false;
} catch (Throwable t) {
// this is the last point where we can stop the agent gracefully if something has gone wrong.
LOG.log(Level.SEVERE, t, "Unable to start the New Relic Agent. Your application will continue to run but it will not be monitored.");
return false;
}
return true;
}
use of com.newrelic.agent.service.ServiceManagerImpl in project newrelic-java-agent by newrelic.
the class AgentLinkingMetadataTest method getLogEventLinkingMetadataWithEmptyTraceAttributes.
@Test
public void getLogEventLinkingMetadataWithEmptyTraceAttributes() {
// Given
final String expectedTraceId = "";
final String expectedSpanId = "";
final String expectedEntityGuid = "entityGuid91011";
final String expectedEntityName = "entityName91011";
TraceMetadataImpl traceMetadataMock = mock(TraceMetadataImpl.class);
ServiceManagerImpl serviceManagerMock = mock(ServiceManagerImpl.class);
RPMServiceManagerImpl rpmServiceManagerMock = mock(RPMServiceManagerImpl.class);
RPMService rpmServiceMock = mock(RPMService.class);
ConfigServiceImpl configServiceMock = mock(ConfigServiceImpl.class);
AgentConfigImpl agentConfigMock = mock(AgentConfigImpl.class);
ServiceFactory.setServiceManager(serviceManagerMock);
// When
when(traceMetadataMock.getTraceId()).thenReturn(expectedTraceId);
when(traceMetadataMock.getSpanId()).thenReturn(expectedSpanId);
when(serviceManagerMock.getRPMServiceManager()).thenReturn(rpmServiceManagerMock);
when(serviceManagerMock.getConfigService()).thenReturn(configServiceMock);
when(rpmServiceManagerMock.getRPMService()).thenReturn(rpmServiceMock);
when(configServiceMock.getDefaultAgentConfig()).thenReturn(agentConfigMock);
when(agentConfigMock.getApplicationName()).thenReturn(expectedEntityName);
when(rpmServiceMock.getEntityGuid()).thenReturn(expectedEntityGuid);
// Then
Map<String, String> linkingMetadata = AgentLinkingMetadata.getLogEventLinkingMetadata(traceMetadataMock, ServiceFactory.getConfigService(), ServiceFactory.getRPMService());
assertFalse("linkingMetadata map shouldn't be empty", linkingMetadata.isEmpty());
// Can't assert on a specific hostname value as it will resolve to the actual hostname of the machine running the test
assertFalse("hostname shouldn't be empty", linkingMetadata.get(AgentLinkingMetadata.HOSTNAME).isEmpty());
assertFalse("entity.type should not be included in LogEvent linking metadata", linkingMetadata.containsKey(AgentLinkingMetadata.ENTITY_TYPE));
assertEquals(expectedEntityName, linkingMetadata.get(AgentLinkingMetadata.ENTITY_NAME));
assertEquals(expectedEntityGuid, linkingMetadata.get(AgentLinkingMetadata.ENTITY_GUID));
// trace.id and span.id would be empty values if getLogEventLinkingMetadata was called outside of a transaction, in which case they are omitted
assertFalse("empty trace.id value should not be included in LogEvent linking metadata", linkingMetadata.containsKey(AgentLinkingMetadata.TRACE_ID));
assertFalse("empty span.id value should not be included in LogEvent linking metadata", linkingMetadata.containsKey(AgentLinkingMetadata.SPAN_ID));
}
use of com.newrelic.agent.service.ServiceManagerImpl in project newrelic-java-agent by newrelic.
the class AgentLinkingMetadataTest method getLinkingMetadataWithEmptyTraceAttributes.
@Test
public void getLinkingMetadataWithEmptyTraceAttributes() {
// Given
final String expectedTraceId = "";
final String expectedSpanId = "";
final String expectedEntityGuid = "entityGuid91011";
final String expectedEntityName = "entityName91011";
final String expectedEntityType = AgentLinkingMetadata.ENTITY_TYPE_DEFAULT;
TraceMetadataImpl traceMetadataMock = mock(TraceMetadataImpl.class);
ServiceManagerImpl serviceManagerMock = mock(ServiceManagerImpl.class);
RPMServiceManagerImpl rpmServiceManagerMock = mock(RPMServiceManagerImpl.class);
RPMService rpmServiceMock = mock(RPMService.class);
ConfigServiceImpl configServiceMock = mock(ConfigServiceImpl.class);
AgentConfigImpl agentConfigMock = mock(AgentConfigImpl.class);
ServiceFactory.setServiceManager(serviceManagerMock);
// When
when(traceMetadataMock.getTraceId()).thenReturn(expectedTraceId);
when(traceMetadataMock.getSpanId()).thenReturn(expectedSpanId);
when(serviceManagerMock.getRPMServiceManager()).thenReturn(rpmServiceManagerMock);
when(serviceManagerMock.getConfigService()).thenReturn(configServiceMock);
when(rpmServiceManagerMock.getRPMService()).thenReturn(rpmServiceMock);
when(configServiceMock.getDefaultAgentConfig()).thenReturn(agentConfigMock);
when(agentConfigMock.getApplicationName()).thenReturn(expectedEntityName);
when(rpmServiceMock.getEntityGuid()).thenReturn(expectedEntityGuid);
// Then
Map<String, String> linkingMetadata = AgentLinkingMetadata.getLinkingMetadata(traceMetadataMock, ServiceFactory.getConfigService(), ServiceFactory.getRPMService());
assertFalse("linkingMetadata map shouldn't be empty", linkingMetadata.isEmpty());
// Can't assert on a specific hostname value as it will resolve to the actual hostname of the machine running the test
assertFalse("hostname shouldn't be empty", linkingMetadata.get(AgentLinkingMetadata.HOSTNAME).isEmpty());
assertEquals(expectedEntityGuid, linkingMetadata.get(AgentLinkingMetadata.ENTITY_GUID));
assertEquals(expectedEntityName, linkingMetadata.get(AgentLinkingMetadata.ENTITY_NAME));
assertEquals(expectedEntityType, linkingMetadata.get(AgentLinkingMetadata.ENTITY_TYPE));
// trace.id and span.id would be empty values if getLinkingMetadata was called outside of a transaction.
// With the getLinkingMetadata API the returned map includes keys with empty values
assertEquals(expectedTraceId, linkingMetadata.get(AgentLinkingMetadata.TRACE_ID));
assertEquals(expectedSpanId, linkingMetadata.get(AgentLinkingMetadata.SPAN_ID));
}
Aggregations