Search in sources :

Example 76 with Segment

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();
}
Also used : HttpRequest(org.apache.http.HttpRequest) Transaction(com.newrelic.agent.bridge.Transaction) HttpException(org.apache.http.HttpException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Segment(com.newrelic.api.agent.Segment) OutboundWrapper(com.nr.agent.instrumentation.httpasyncclient4.OutboundWrapper)

Example 77 with Segment

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);
}
Also used : Segment(com.newrelic.api.agent.Segment) NoOpSegment(com.newrelic.agent.bridge.NoOpSegment) Test(org.junit.Test)

Example 78 with Segment

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();
}
Also used : Segment(com.newrelic.api.agent.Segment) NoOpSegment(com.newrelic.agent.bridge.NoOpSegment) URISyntaxException(java.net.URISyntaxException) Test(org.junit.Test)

Example 79 with Segment

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();
}
Also used : PublishResult(com.amazonaws.services.sns.model.PublishResult) Transaction(com.newrelic.api.agent.Transaction) PublishRequest(com.amazonaws.services.sns.model.PublishRequest) Segment(com.newrelic.api.agent.Segment) Trace(com.newrelic.api.agent.Trace)

Example 80 with Segment

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;
}
Also used : Transaction(com.newrelic.api.agent.Transaction) MessageProduceParameters(com.newrelic.api.agent.MessageProduceParameters) Segment(com.newrelic.api.agent.Segment)

Aggregations

Segment (com.newrelic.api.agent.Segment)138 Trace (com.newrelic.api.agent.Trace)13 URI (java.net.URI)6 NoOpSegment (com.newrelic.agent.bridge.NoOpSegment)5 URISyntaxException (java.net.URISyntaxException)4 HttpResponse (akka.http.scaladsl.model.HttpResponse)3 Transaction (com.newrelic.agent.bridge.Transaction)3 DatastoreParameters (com.newrelic.api.agent.DatastoreParameters)3 ExternalParameters (com.newrelic.api.agent.ExternalParameters)3 InetSocketAddress (java.net.InetSocketAddress)3 Test (org.junit.Test)3 Transaction (com.newrelic.agent.Transaction)2 MessageProduceParameters (com.newrelic.api.agent.MessageProduceParameters)2 Transaction (com.newrelic.api.agent.Transaction)2 OutboundWrapper (com.nr.agent.instrumentation.asynchttpclient.OutboundWrapper)2 HelloReply (io.grpc.examples.helloworld.HelloReply)2 HelloRequest (io.grpc.examples.helloworld.HelloRequest)2 IOException (java.io.IOException)2 SocketAddress (java.net.SocketAddress)2 MemcachedNode (net.spy.memcached.MemcachedNode)2