Search in sources :

Example 11 with ExternalParameters

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

the class AgentPreprocessorsTest method testRewriteSlowQueryBuilderWithHighSecurityAndListToCollect.

@Test
public void testRewriteSlowQueryBuilderWithHighSecurityAndListToCollect() throws Exception {
    final ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    final String classname = "com.newrelic.agent.instrumentation.weaver.preprocessors.AgentPreprocessorsTest$ExternalParametersFactoryTestClass7";
    Map<String, Object> confProps = new HashMap<>();
    confProps.put("high_security", true);
    Map<String, Object> ttConfig = new HashMap<>();
    ttConfig.put("collect_slow_queries_from", INSTRUMENTATION_TITLE + ",some-other-module");
    confProps.put("transaction_tracer", ttConfig);
    AgentConfig agentConfig = AgentConfigImpl.createAgentConfig(confProps);
    byte[] bytes = getClassBytesFromClassLoaderResource(classname, classloader);
    Assert.assertNotNull(bytes);
    ClassNode source = WeaveUtils.convertToClassNode(bytes);
    ClassNode result = new ClassNode(WeaveUtils.ASM_API_LEVEL);
    ClassVisitor cv = new CheckClassAdapter(result);
    AgentPreprocessors preprocessors = new AgentPreprocessors(agentConfig, null);
    preprocessors.setInstrumentationTitle(INSTRUMENTATION_TITLE);
    cv = preprocessors.rewriteSlowQueryIfRequired(cv);
    source.accept(cv);
    Class<?> clazz = addToClassloader(result, classloader);
    Assert.assertNotNull(clazz);
    ExternalParametersFactoryTestClass7 testClass = (ExternalParametersFactoryTestClass7) clazz.newInstance();
    assertNotNull(testClass);
    ExternalParameters regularDatastore = testClass.createRegularDatastore();
    assertNotNull(regularDatastore);
    assertTrue(regularDatastore instanceof DatastoreParameters);
    ExternalParameters slowQueryDatastore = testClass.createSlowQueryDatastore();
    assertNotNull(slowQueryDatastore);
    assertTrue(slowQueryDatastore instanceof SlowQueryDatastoreParameters);
    ExternalParameters slowQueryWithInputDatastore = testClass.createSlowQueryWithInputDatastore();
    assertNotNull(slowQueryWithInputDatastore);
    assertTrue(slowQueryWithInputDatastore instanceof SlowQueryWithInputDatastoreParameters);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DatastoreParameters(com.newrelic.api.agent.DatastoreParameters) SlowQueryDatastoreParameters(com.newrelic.api.agent.SlowQueryDatastoreParameters) SlowQueryWithInputDatastoreParameters(com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters) ClassVisitor(org.objectweb.asm.ClassVisitor) TokenNullCheckClassVisitor(com.newrelic.agent.instrumentation.weaver.preprocessors.AgentPreprocessors.TokenNullCheckClassVisitor) SlowQueryDatastoreParameters(com.newrelic.api.agent.SlowQueryDatastoreParameters) SlowQueryWithInputDatastoreParameters(com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters) AgentConfig(com.newrelic.agent.config.AgentConfig) ExternalParameters(com.newrelic.api.agent.ExternalParameters) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) Test(org.junit.Test)

Example 12 with ExternalParameters

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

the class ApiTest method runTestDatastoreSlowQueryAPI.

@Trace(dispatcher = true)
private void runTestDatastoreSlowQueryAPI() {
    String vendor = "MongoDB";
    String collection = "Users";
    String operation = "SELECT";
    String host = "awesome-host";
    Integer port = 27017;
    BsonDocument rawQuery = new BsonDocument("key", new BsonBoolean(true));
    QueryConverter<BsonDocument> MONGO_QUERY_CONVERTER = new QueryConverter<BsonDocument>() {

        @Override
        public String toRawQueryString(BsonDocument query) {
            return query.toString();
        }

        @Override
        public String toObfuscatedQueryString(BsonDocument query) {
            return query.toString();
        }
    };
    ExternalParameters params = DatastoreParameters.product(vendor).collection(collection).operation(operation).instance(host, port).noDatabaseName().slowQuery(rawQuery, MONGO_QUERY_CONVERTER).build();
    NewRelic.getAgent().getTracedMethod().reportAsExternal(params);
    try {
        Thread.sleep(600);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Tracer rootTracer = Transaction.getTransaction(false).getRootTracer();
    rootTracer.finish(0, null);
    Map<String, Object> attributes = rootTracer.getAgentAttributes();
    Assert.assertNotNull(attributes.get("sql"));
    Assert.assertNotNull(attributes.get("sql_obfuscated"));
}
Also used : BsonDocument(org.bson.BsonDocument) QueryConverter(com.newrelic.api.agent.QueryConverter) ExternalParameters(com.newrelic.api.agent.ExternalParameters) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) BsonBoolean(org.bson.BsonBoolean) Trace(com.newrelic.api.agent.Trace)

Example 13 with ExternalParameters

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

the class ApiTest method runTestMessagingAPI.

@Trace(dispatcher = true)
private void runTestMessagingAPI() {
    URL myURL = null;
    try {
        Thread.sleep(600);
        myURL = new URL("http://localhost:8088");
        HttpUriRequest request = RequestBuilder.get().setUri(myURL.toURI()).build();
        ApiTestHelper.OutboundWrapper outboundRequestWrapper = new ApiTestHelper.OutboundWrapper(request, HeaderType.MESSAGE);
        // MessageProducer
        ExternalParameters messageProduceParameters = MessageProduceParameters.library("JMS").destinationType(DestinationType.NAMED_QUEUE).destinationName("Message Destination").outboundHeaders(outboundRequestWrapper).build();
        NewRelic.getAgent().getTracedMethod().reportAsExternal(messageProduceParameters);
        Assert.assertTrue(request.getHeaders("NewRelicID").length != 0);
        Assert.assertTrue(request.getHeaders("NewRelicTransaction").length != 0);
        CloseableHttpClient connection = HttpClientBuilder.create().build();
        CloseableHttpResponse response = connection.execute(request);
        // MessageConsumer
        ExternalParameters messageResponseParameters = MessageConsumeParameters.library("JMS").destinationType(DestinationType.TEMP_QUEUE).destinationName("Message Destination").inboundHeaders(new ApiTestHelper.InboundWrapper(response, HeaderType.MESSAGE)).build();
        NewRelic.getAgent().getTracedMethod().reportAsExternal(messageResponseParameters);
        Assert.assertTrue(response.getHeaders("NewRelicAppData").length != 0);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ExternalParameters(com.newrelic.api.agent.ExternalParameters) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) Trace(com.newrelic.api.agent.Trace)

Example 14 with ExternalParameters

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

the class ApiTest method runTestExternalAPI.

@Trace(dispatcher = true)
private void runTestExternalAPI() {
    String library = "HttpClient";
    URI uri = null;
    try {
        uri = new URI("http://localhost:8088/test/this/path?name=Bob");
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    String operation = "execute";
    ExternalParameters params = GenericParameters.library(library).uri(uri).procedure(operation).build();
    NewRelic.getAgent().getTracedMethod().reportAsExternal(params);
    Tracer rootTracer = Transaction.getTransaction().getRootTracer();
    rootTracer.finish(0, null);
    Assert.assertEquals("http://localhost:8088/test/this/path", rootTracer.getTransactionSegmentUri());
    Assert.assertEquals("External/localhost/HttpClient/execute", rootTracer.getTransactionSegmentName());
    Assert.assertEquals("External/localhost/HttpClient/execute", rootTracer.getMetricName());
}
Also used : ExternalParameters(com.newrelic.api.agent.ExternalParameters) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Trace(com.newrelic.api.agent.Trace)

Example 15 with ExternalParameters

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

the class AmazonSNS_Instrumentation method publish.

@Trace
public PublishResult publish(PublishRequest publishRequest) {
    TracedMethod tracedMethod = NewRelic.getAgent().getTracedMethod();
    ExternalParameters params = SNSInstrumentationHelper.makeMessageProducerParameters(publishRequest);
    tracedMethod.reportAsExternal(params);
    return Weaver.callOriginal();
}
Also used : ExternalParameters(com.newrelic.api.agent.ExternalParameters) TracedMethod(com.newrelic.api.agent.TracedMethod) Trace(com.newrelic.api.agent.Trace)

Aggregations

ExternalParameters (com.newrelic.api.agent.ExternalParameters)23 Test (org.junit.Test)12 AgentConfig (com.newrelic.agent.config.AgentConfig)8 TokenNullCheckClassVisitor (com.newrelic.agent.instrumentation.weaver.preprocessors.AgentPreprocessors.TokenNullCheckClassVisitor)8 DatastoreParameters (com.newrelic.api.agent.DatastoreParameters)8 SlowQueryDatastoreParameters (com.newrelic.api.agent.SlowQueryDatastoreParameters)8 SlowQueryWithInputDatastoreParameters (com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters)8 HashMap (java.util.HashMap)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 ClassVisitor (org.objectweb.asm.ClassVisitor)8 ClassNode (org.objectweb.asm.tree.ClassNode)8 CheckClassAdapter (org.objectweb.asm.util.CheckClassAdapter)8 Trace (com.newrelic.api.agent.Trace)7 URISyntaxException (java.net.URISyntaxException)4 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)3 Tracer (com.newrelic.agent.tracers.Tracer)3 BasicRequestRootTracer (com.newrelic.agent.tracers.servlet.BasicRequestRootTracer)3 Segment (com.newrelic.api.agent.Segment)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2