Search in sources :

Example 1 with MockDistributedTraceService

use of com.newrelic.agent.util.MockDistributedTraceService in project newrelic-java-agent by newrelic.

the class HeadersUtilTest method createDTHeadersSetsSpanIdEvenIfTxNotSampled.

@Test
public void createDTHeadersSetsSpanIdEvenIfTxNotSampled() {
    ConfigService mockConfigService = new MockConfigService(AgentConfigImpl.createAgentConfig(ImmutableMap.of(AgentConfigImpl.APP_NAME, "Unit Test", AgentConfigImpl.DISTRIBUTED_TRACING, Collections.singletonMap(DistributedTracingConfig.ENABLED, true), AgentConfigImpl.SPAN_EVENTS, ImmutableMap.of(SpanEventsConfig.ENABLED, true, SpanEventsConfig.COLLECT_SPAN_EVENTS, true))));
    MockServiceManager mockServiceManager = new MockServiceManager(mockConfigService);
    mockServiceManager.setDistributedTraceService(new MockDistributedTraceService());
    ServiceFactory.setServiceManager(mockServiceManager);
    Transaction tx = Transaction.getTransaction();
    tx.setPriorityIfNotNull(0F);
    tx.startTransactionIfBeginning(new MockDispatcherTracer(tx));
    Tracer mockTracer = new DefaultTracer(tx, new ClassMethodSignature(getClass().getName(), "tracerMethod", "()V"), this);
    OutboundHeadersMap map = new OutboundHeadersMap(HeaderType.HTTP);
    assertTrue("DT headers should have been set", HeadersUtil.createAndSetDistributedTraceHeaders(tx, mockTracer, map));
    assertFalse(tx.sampled());
    assertTrue("map should contain newrelic header", map.containsKey("newrelic"));
    String decodedHeader = new String(Base64.decodeBase64(map.get("newrelic")), StandardCharsets.UTF_8);
    JsonObject jsonObject = new Gson().fromJson(decodedHeader, JsonObject.class);
    assertFalse("d.sa should be false because tx is not sampled", jsonObject.getAsJsonObject("d").get("sa").getAsBoolean());
    assertEquals("d.pr should be zero", 0f, jsonObject.getAsJsonObject("d").get("pr").getAsFloat(), 0.0001);
    assertNotNull("d.id should not be null", jsonObject.getAsJsonObject("d").get("id"));
    assertEquals("d.id should be the span id.", mockTracer.getGuid(), jsonObject.getAsJsonObject("d").get("id").getAsString());
    String traceParent = map.get("traceparent");
    assertEquals("traceparent parentId field should match span id.", mockTracer.getGuid(), traceParent.split("-")[2]);
    String traceState = map.get("tracestate");
    assertEquals("tracestate spanId field should match span id.", mockTracer.getGuid(), traceState.split("-")[4]);
    assertEquals("tracestate txId field should match tx id.", tx.getGuid(), traceState.split("-")[5]);
}
Also used : Tracer(com.newrelic.agent.tracers.Tracer) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) MockDistributedTraceService(com.newrelic.agent.util.MockDistributedTraceService) ConfigService(com.newrelic.agent.config.ConfigService) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) Test(org.junit.Test)

Example 2 with MockDistributedTraceService

use of com.newrelic.agent.util.MockDistributedTraceService in project newrelic-java-agent by newrelic.

the class SpanProxyTest method before.

@Before
public void before() throws Exception {
    Map<String, Object> config = new HashMap<>();
    config.put(AgentConfigImpl.APP_NAME, "Test");
    Map<String, Object> dtConfig = new HashMap<>();
    dtConfig.put("enabled", true);
    config.put("distributed_tracing", dtConfig);
    Map<String, Object> spanConfig = new HashMap<>();
    spanConfig.put("collect_span_events", true);
    config.put("span_events", spanConfig);
    ConfigService configService = ConfigServiceFactory.createConfigService(AgentConfigImpl.createAgentConfig(config), Collections.<String, Object>emptyMap());
    MockServiceManager serviceManager = new MockServiceManager(configService);
    ServiceFactory.setServiceManager(serviceManager);
    serviceManager.setConfigService(configService);
    ServiceFactory.getServiceManager().start();
    serviceManager.setDistributedTraceService(new MockDistributedTraceService());
}
Also used : ConfigService(com.newrelic.agent.config.ConfigService) HashMap(java.util.HashMap) MockServiceManager(com.newrelic.agent.MockServiceManager) MockDistributedTraceService(com.newrelic.agent.util.MockDistributedTraceService) Before(org.junit.Before)

Aggregations

ConfigService (com.newrelic.agent.config.ConfigService)2 MockDistributedTraceService (com.newrelic.agent.util.MockDistributedTraceService)2 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 MockServiceManager (com.newrelic.agent.MockServiceManager)1 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)1 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)1 Tracer (com.newrelic.agent.tracers.Tracer)1 HashMap (java.util.HashMap)1 Before (org.junit.Before)1 Test (org.junit.Test)1