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]);
}
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());
}
Aggregations