use of com.newrelic.api.agent.Segment in project newrelic-java-agent by newrelic.
the class HttpAsyncClient4_Instrumentation method execute.
/* Most common uses of the execute methods defined in the HttpAsyncClient interface should eventually call into this
* particular execute signature, thus instrumenting only this one should result in fairly thorough instrumentation
* coverage without double counting external calls. Possible exceptions include uses of execute in
* CachingHttpAsyncClient and those defined in the HttpPipeliningClient interface.
*/
public <T> Future<T> execute(HttpAsyncRequestProducer requestProducer, HttpAsyncResponseConsumer_Instrumentation<T> responseConsumer, HttpContext context, FutureCallback<T> callback) {
try {
URI uri = new URI(requestProducer.getTarget().toURI());
String scheme = uri.getScheme();
final Transaction txn = AgentBridge.getAgent().getTransaction(false);
// only instrument HTTP or HTTPS calls
final String lowerCaseScheme = scheme.toLowerCase();
if (("http".equals(lowerCaseScheme) || "https".equals(lowerCaseScheme)) && txn != null) {
try {
// Calls to generateRequest() don't appear to create a new HttpRequest object each time, but it does
// seem like other people could implement their own version that does. Something to be aware of.
final HttpRequest httpRequest = requestProducer.generateRequest();
Segment segment = txn.startSegment("External");
segment.addOutboundRequestHeaders(new OutboundWrapper(httpRequest));
// forward uri and segment to HttpAsyncResponseConsumer_Instrumentation
responseConsumer.segment = segment;
responseConsumer.uri = uri;
} catch (IOException | HttpException e) {
AgentBridge.getAgent().getLogger().log(Level.FINEST, e, "Caught exception in HttpAsyncClient4 instrumentation: {0}");
}
}
} catch (URISyntaxException uriSyntaxException) {
// if Java can't parse the URI, HttpAsyncClient won't be able to either
// let's just proceed without instrumentation
}
return Weaver.callOriginal();
}
use of com.newrelic.api.agent.Segment in project newrelic-java-agent by newrelic.
the class ExternalAsyncTest method testNoTransaction.
@Test
public void testNoTransaction() {
Segment externalEvent = NewRelic.getAgent().getTransaction().startSegment("foo");
Assert.assertEquals(externalEvent, NoOpSegment.INSTANCE);
verifyTimesSet(0);
}
use of com.newrelic.api.agent.Segment in project newrelic-java-agent by newrelic.
the class ExternalAsyncTest method testFailure.
@Test
public void testFailure() throws Exception {
long time = doInTransaction((Callable<Void>) () -> {
Segment externalEvent = startSegment(new Outbound());
Throwable error = new Exception();
finishExternalEvent(externalEvent, error, new Inbound(), HOST, LIBRARY, URI, OPERATION_NAME);
return null;
});
verifyTimesSet(1);
verifyScopedMetricsPresent("OtherTransaction/Custom/com.newrelic.agent.async.ExternalAsyncTest/doInTransaction", "External/www.example.com/library/operation");
verifyUnscopedMetricsPresent("External/www.example.com/all", "External/allOther", "External/all");
verifyTransactionSegmentsBreadthFirst("OtherTransaction/Custom/com.newrelic.agent.async.ExternalAsyncTest/doInTransaction", "Java/com.newrelic.agent.async.ExternalAsyncTest/doInTransaction", Thread.currentThread().getName(), "External/www.example.com/library/operation", NO_ASYNC_CONTEXT);
// ??
verifyNoExceptions();
}
use of com.newrelic.api.agent.Segment in project newrelic-java-agent by newrelic.
the class AmazonSNSAsync_Instrumentation method publishAsync.
@Trace
public Future<PublishResult> publishAsync(final PublishRequest request, AsyncHandler<PublishRequest, PublishResult> asyncHandler) {
final AsyncHandler<PublishRequest, PublishResult> originalHandler = asyncHandler;
final Transaction transaction = NewRelic.getAgent().getTransaction();
final Segment segment = startSegment(request, transaction);
// replace the handler with ours that wraps the original (which might be null)
asyncHandler = new AsyncHandler<PublishRequest, PublishResult>() {
@Override
public void onError(Exception exception) {
try {
if (originalHandler != null) {
originalHandler.onError(exception);
}
} finally {
segment.end();
}
}
@Override
public void onSuccess(PublishRequest request, PublishResult publishResult) {
try {
if (originalHandler != null) {
originalHandler.onSuccess(request, publishResult);
}
} finally {
segment.end();
}
}
};
return Weaver.callOriginal();
}
use of com.newrelic.api.agent.Segment in project newrelic-java-agent by newrelic.
the class SnsClientInstrumentationHelper method startSegmentAndReportAsExternal.
public static Segment startSegmentAndReportAsExternal(PublishRequest publishRequest) {
Transaction transaction = NewRelic.getAgent().getTransaction();
Segment segment = transaction.startSegment("SNS");
MessageProduceParameters params = SnsClientInstrumentationHelper.makeMessageProducerParameters(publishRequest);
segment.reportAsExternal(params);
return segment;
}
Aggregations