Search in sources :

Example 51 with Segment

use of com.newrelic.api.agent.Segment in project newrelic-java-agent by newrelic.

the class MemcachedClient_Instrumentation method reportDatastoreMetricsAsync.

// Async reporting - overload for OperationsFuture
private void reportDatastoreMetricsAsync(String op, OperationFuture<?> future) {
    String host = null;
    Integer port = null;
    try {
        MemcachedNode memcachedNode = MemcachedUtil.OPERATION_NODE.get();
        if (memcachedNode != null) {
            SocketAddress address = memcachedNode.getSocketAddress();
            if (address instanceof InetSocketAddress) {
                InetSocketAddress nodeAddress = (InetSocketAddress) address;
                host = nodeAddress.getHostName();
                port = nodeAddress.getPort();
            }
        }
    } catch (Throwable t) {
        AgentBridge.getAgent().getLogger().log(Level.FINER, t, "Unable to capture host/port for memcached operation");
    } finally {
        MemcachedUtil.OPERATION_NODE.set(null);
    }
    final Segment segment = AgentBridge.getAgent().getTransaction().createAndStartTracedActivity();
    if (segment != null) {
        ExternalParameters params = DatastoreParameters.product("Memcached").collection("cache").operation(op).instance(host, port).build();
        segment.reportAsExternal(params);
        if (future.isDone()) {
            segment.endAsync();
        } else {
            future.addListener(new OperationCompletionListener(segment));
        }
    }
}
Also used : ExternalParameters(com.newrelic.api.agent.ExternalParameters) InetSocketAddress(java.net.InetSocketAddress) MemcachedNode(net.spy.memcached.MemcachedNode) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Segment(com.newrelic.api.agent.Segment)

Example 52 with Segment

use of com.newrelic.api.agent.Segment in project newrelic-java-agent by newrelic.

the class MemcachedClient_Instrumentation method reportDatastoreMetricsAsync.

/**
 *  Async reporting - overload for GetFuture
 *
 *  Create traced activity and call reportAsExternal on the traced activitiy TracedMethod.
 *
 *  Callers of this helper should not have an @Trace.
 *
 * @param op Operation to report
 * @param future Future to add listener to.
 */
private void reportDatastoreMetricsAsync(String op, GetFuture<?> future) {
    String host = null;
    Integer port = null;
    try {
        MemcachedNode memcachedNode = MemcachedUtil.OPERATION_NODE.get();
        if (memcachedNode != null) {
            SocketAddress address = memcachedNode.getSocketAddress();
            if (address instanceof InetSocketAddress) {
                InetSocketAddress nodeAddress = (InetSocketAddress) address;
                host = nodeAddress.getHostName();
                port = nodeAddress.getPort();
            }
        }
    } catch (Throwable t) {
        AgentBridge.getAgent().getLogger().log(Level.FINER, t, "Unable to capture host/port for memcached operation");
    } finally {
        MemcachedUtil.OPERATION_NODE.set(null);
    }
    if (AgentBridge.getAgent().getTransaction(false) != null) {
        final Segment segment = NewRelic.getAgent().getTransaction().startSegment(op);
        ExternalParameters params = DatastoreParameters.product("Memcached").collection("cache").operation(op).instance(host, port).build();
        segment.reportAsExternal(params);
        if (future.isDone()) {
            segment.endAsync();
        } else {
            future.addListener(new GetCompletionListener(segment));
        }
    }
}
Also used : ExternalParameters(com.newrelic.api.agent.ExternalParameters) InetSocketAddress(java.net.InetSocketAddress) MemcachedNode(net.spy.memcached.MemcachedNode) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Segment(com.newrelic.api.agent.Segment)

Example 53 with Segment

use of com.newrelic.api.agent.Segment in project newrelic-java-agent by newrelic.

the class MemcachedClient_Instrumentation method reportDatastoreMetricsAsync.

/**
 *  Async reporting - overload for BulkFuture
 *
 *  Create traced activity and call reportAsExternal on the traced activitiy TracedMethod.
 *
 *  Callers of this helper should not have an @Trace.
 *
 * @param op Operation to report
 * @param future Future to add listener to.
 */
private void reportDatastoreMetricsAsync(String op, BulkFuture<?> future) {
    String host = null;
    Integer port = null;
    // This is where we would normally grab the host and port for the instance metrics from the MemcachedNode
    // instrumentation. But for a bulk operation like this one, this would be incorrect, because the bulk operation
    // can send off a whole bunch of operations to multiple memcached servers, while this logic is only capable of
    // capturing instance information for one of them. Tracked by JAVA-2640.
    // Let's clear the thread local despite the fact that we didn't use it.
    MemcachedUtil.OPERATION_NODE.set(null);
    if (AgentBridge.getAgent().getTransaction(false) != null) {
        final Segment segment = NewRelic.getAgent().getTransaction().startSegment(op);
        ExternalParameters params = DatastoreParameters.product("Memcached").collection("cache").operation(op).instance(host, port).build();
        segment.reportAsExternal(params);
        if (future.isDone()) {
            segment.endAsync();
        } else {
            future.addListener(new BulkGetCompletionListener(segment));
        }
    }
}
Also used : ExternalParameters(com.newrelic.api.agent.ExternalParameters) Segment(com.newrelic.api.agent.Segment)

Example 54 with Segment

use of com.newrelic.api.agent.Segment in project newrelic-java-agent by newrelic.

the class TransactionApiImpl method startSegment.

@Override
public Segment startSegment(String category, String segmentName) {
    Transaction tx = getTransactionIfExists();
    if (null == tx) {
        return NoOpSegment.INSTANCE;
    }
    if (category == null || category.isEmpty()) {
        // maybe log?
        category = MetricNames.CUSTOM;
    }
    if (segmentName == null || segmentName.isEmpty()) {
        // maybe log?
        segmentName = com.newrelic.agent.Segment.UNNAMED_SEGMENT;
    }
    Segment segment = tx.startSegment(category, segmentName);
    return segment == null ? NoOpSegment.INSTANCE : segment;
}
Also used : NoOpTransaction(com.newrelic.agent.bridge.NoOpTransaction) Segment(com.newrelic.api.agent.Segment) NoOpSegment(com.newrelic.agent.bridge.NoOpSegment)

Example 55 with Segment

use of com.newrelic.api.agent.Segment in project newrelic-java-agent by newrelic.

the class ExternalAsyncTest method startSegment.

private static Segment startSegment(OutboundHeaders outbound) {
    Segment externalEvent = NewRelic.getAgent().getTransaction().startSegment(ASYNC_TXA_NAME);
    Assert.assertNotNull(externalEvent);
    externalEvent.addOutboundRequestHeaders(outbound);
    return externalEvent;
}
Also used : Segment(com.newrelic.api.agent.Segment) NoOpSegment(com.newrelic.agent.bridge.NoOpSegment)

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