use of com.newrelic.agent.tracers.DefaultTracer in project newrelic-java-agent by newrelic.
the class HandlerMethodInvoker3PointCut method doGetTracer.
@Override
public Tracer doGetTracer(final Transaction transaction, ClassMethodSignature sig, Object invoker, final Object[] args) {
StringBuilder tracerName = new StringBuilder("Spring/Java");
String methodName = ((Method) args[0]).getName();
Class<?> controller = args[1].getClass();
// build the tracer name;
tracerName.append(getControllerName(methodName, controller));
return new DefaultTracer(transaction, sig, invoker, new SimpleMetricNameFormat(tracerName.toString()));
}
use of com.newrelic.agent.tracers.DefaultTracer 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.tracers.DefaultTracer in project newrelic-java-agent by newrelic.
the class WebRequestDispatcherTest method createDispatcher.
private WebRequestDispatcher createDispatcher(MockHttpRequest httpRequest) throws Exception {
Transaction tx = Transaction.getTransaction();
Response httpResponse = new MockHttpResponse();
ClassMethodSignature sig = new ClassMethodSignature(getClass().getName(), "methodName", "()V");
DefaultTracer tracer = new OtherRootTracer(tx, sig, this, new SimpleMetricNameFormat("test"));
tx.getTransactionActivity().tracerStarted(tracer);
WebRequestDispatcher dispatcher = new WebRequestDispatcher(httpRequest, httpResponse, tx);
tx.setDispatcher(dispatcher);
return dispatcher;
}
use of com.newrelic.agent.tracers.DefaultTracer in project newrelic-java-agent by newrelic.
the class TransactionTraceTest method testExecContext.
@Test
public void testExecContext() throws Exception {
setUp(false, true, false);
Transaction tx = Transaction.getTransaction();
Tracer rootTracer = new OtherRootTracer(tx, new ClassMethodSignature("Test", "root", "()V"), this, new OtherTransSimpleMetricNameFormat("myMetricName"));
DefaultTracer tracer1 = new DefaultTracer(tx, new ClassMethodSignature("Test", "dude", "()V"), this);
DefaultTracer tracer2 = new DefaultTracer(tx, new ClassMethodSignature("Test", "dude2", "()V"), this);
tx.getTransactionActivity().tracerStarted(rootTracer);
tx.getTransactionActivity().tracerStarted(tracer1);
tx.getTransactionActivity().tracerStarted(tracer2);
tracer2.finish(0, null);
tracer1.finish(0, null);
rootTracer.finish(0, null);
long startTime = System.currentTimeMillis();
String frontendMetricName = "Frontend/dude";
String requestUri = "/dude";
String appName = "dude";
TransactionData transactionData = new TransactionDataTestBuilder(appName, iAgentConfig, rootTracer).setStartTime(startTime).setRequestUri(requestUri).setFrontendMetricName(frontendMetricName).setTracers(Arrays.asList(rootTracer, tracer1, tracer2)).build();
TransactionTrace trace = TransactionTrace.getTransactionTrace(transactionData, SqlObfuscator.getDefaultSqlObfuscator());
JSONParser parser = new JSONParser();
// Get the raw data
JSONArray jsonArray = (JSONArray) AgentHelper.serializeJSON(trace);
// Manually create an "inflated" version for comparison with simple compression
JSONArray inflatedJsonArray = new JSONArray();
inflatedJsonArray.addAll(jsonArray);
// Extract and inflate
Object decodedTTData = decodeTransactionTraceData(inflatedJsonArray.get(4));
// Store back in array to compare with simple compression below
inflatedJsonArray.set(4, decodedTTData);
Assert.assertNotNull(jsonArray);
// Second, serialize with simple compression off (data will be deflated)
byte[] jsonByteArray = AgentHelper.serializeJSONToByteArray(trace);
JSONArray parsedJsonByteArray = (JSONArray) parser.parse(new String(jsonByteArray, StandardCharsets.UTF_8));
Assert.assertEquals(jsonArray, parsedJsonByteArray);
// Third, turn on simple compression and compare with raw data above
setUp(false, true, true);
byte[] simpleCompressionJsonByteArray = AgentHelper.serializeJSONToByteArray(trace);
JSONArray parsedSimpleCompressionJsonByteArray = (JSONArray) parser.parse(new String(simpleCompressionJsonByteArray, StandardCharsets.UTF_8));
Assert.assertEquals(inflatedJsonArray, parsedSimpleCompressionJsonByteArray);
JSONArray jsonRootSegment = (JSONArray) decodedTTData;
Assert.assertEquals(8, jsonArray.size());
JSONArray rootSeg = (JSONArray) jsonRootSegment.get(3);
Assert.assertNotNull(rootSeg);
verifyAllTraceSegmentsHaveExecContext(rootSeg);
}
use of com.newrelic.agent.tracers.DefaultTracer in project newrelic-java-agent by newrelic.
the class BasicRequestDispatcherTracerTest method createDispatcher.
private WebRequestDispatcher createDispatcher(Request httpRequest) throws Exception {
Transaction tx = Transaction.getTransaction();
Response httpResponse = new MockHttpResponse();
ClassMethodSignature sig = new ClassMethodSignature(getClass().getName(), "dude", "()V");
DefaultTracer tracer = new OtherRootTracer(tx, sig, this, new SimpleMetricNameFormat("test"));
tx.getTransactionActivity().tracerStarted(tracer);
WebRequestDispatcher dispatcher = new WebRequestDispatcher(httpRequest, httpResponse, tx);
tx.setDispatcher(dispatcher);
return dispatcher;
}
Aggregations