Standalone Java can't trace main method

I m not able trace main method?? But i m able to trace other 2 methods

Source code


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.sql.*;

import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

//import org.openqa.selenium.firefox.FirefoxDriver;
//comment the above line and uncomment below line to use Chrome
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;


import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.TracedMethod;


import java.util.Date;
import java.util.concurrent.TimeUnit;


public class test {

	 
	// mysql -h 192.168.112.221 -u yodlee -pyodlee@123;

	  static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
	   static final String DB_URL = "jdbc:mysql://localhost/1/nr_data?serverTimezone=UTC";

	   //Database credentials
	   static final String USER = "xxxxxxx";
	   static final String PASS = "xxxxxxx";
	   
	   
	@Trace(dispatcher=true)
    public static void main(String[] args) {
        // declaration and instantiation of objects/variables
    //System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
		//WebDriver driver = new FirefoxDriver();
		//comment the above 2 lines and uncomment below 2 lines to use Chrome
    	WebDriver driver;
    	
    	
 
    		System.setProperty("webdriver.chrome.driver","/usr/bin/chromedriver");

      		 driver = new ChromeDriver();
    		
    		DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            ChromeOptions options = new ChromeOptions();
           
          
            
            options.addArguments("--verbose");
            options.addArguments("--whitelisted-ips");
            options.addArguments("chrome.switches","--disable-extensions");
            options.addArguments("--headless");
            options.addArguments("--no-sandbox");
            options.addArguments("--disable-dev-shm-usage");
            options.addArguments("user-data-dir=/home/ctier/ab/chromedata/userdata/1");
            options.addArguments("disk-cache-dir/home/ctier/ab/chromedata/diskdata/1");
            capabilities.setCapability("chromeOptions", (Object)options);
            driver = new ChromeDriver(capabilities);
    	
            ArrayList<String> alist=new ArrayList<String>(); 
                   alist.add("https://www.google.com");
                   alist.add("https://www.espncricinfo.com");
                   alist.add("https://www.youtube.com/");
                   alist.add("https://www.sify.com/");
                   alist.add("https://www.yahoo.com/");
                   alist.add("https://www.yodlee.com/");
                   alist.add("https://www.paypal.com/");
                   alist.add("https://www.rediff.com/");
                   long startTime = new Date().getTime();

		        try {
					Thread.sleep(5000);
					System.out.println("processing wating ");
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

        

        for(int i=0;i<alist.size();i++){
        	  System.out.println("processing "+ alist.get(i));
        	  //NewRelic.addCustomParameter("Site",alist.get(i));

		        driver.get(alist.get(i));
		        inserttodb(fetchData(driver));
       
				       try {
				 			Thread.sleep(55000);
				 		} catch (InterruptedException e1) {
				 			// TODO Auto-generated catch block
				 			e1.printStackTrace();
				 		}


       
        }
        
        long endTime = new Date().getTime();
        
        driver.close();
        driver.quit();
        long timeElapsed = endTime - startTime;
		System.out.println("Execution time in milliseconds: " + timeElapsed);
        System.exit(0);
    
    }
    
 	
    
 ////
    @Trace(dispatcher=true)
	public static String fetchData(WebDriver driver)
	{
		 String actualTitle = "";
		 actualTitle = driver.getTitle();
	     System.out.println(actualTitle);
	     return actualTitle;
	}
    
///// insert data
    @Trace(dispatcher=true)
	public static void inserttodb(String titleD)
	{
		
		   Connection conn = null;
		   Statement stmt = null;
		   try{
		      //STEP 2: Register JDBC driver
		      Class.forName("com.mysql.jdbc.Driver");
	
		      //STEP 3: Open a connection
		      System.out.println("Connecting to a selected database...");
		      conn = DriverManager.getConnection(DB_URL, USER, PASS);
		      System.out.println("Connected database successfully...");
		      
		      //STEP 4: Execute a query
		      System.out.println("Inserting records into the table...");
		      stmt = conn.createStatement();
		      
		      String sql = "INSERT INTO webtitles " +
		                   "VALUES ('"+titleD+"')";
		      stmt.executeUpdate(sql);
		   
	
		   }catch(SQLException se){
		      //Handle errors for JDBC
		      se.printStackTrace();
		   }catch(Exception e){
		      //Handle errors for Class.forName
		      e.printStackTrace();
		   }finally{
		      //finally block used to close resources
		      try{
		         if(stmt!=null)
		            conn.close();
		      }catch(SQLException se){
		      }// do nothing
		      try{
		         if(conn!=null)
		            conn.close();
		      }catch(SQLException se){
		         se.printStackTrace();
		      }//end finally try
		   }//end try
		   //System.out.println("Goodbye!");
	
		
		  
	}
  


}

Hello Aelahi,

Thanks for getting in touch! Good call out regarding adding @Trace to the main method. Based on our @Trace API documentation examples, I’m fairly certain that adding @Trace to main won’t work like it will with other methods. I hope that helps!

Cheers,
Giselle

Thanks Giselle for reply, It will great help if you provide sample, As i m not getting any help from DOCS

If the agent is having an issue with the instrumentation, it should report that in the logs. Also, at the most verbose level of finest it should show a started transaction or a reason for not having a transaction. There is information at https://docs.newrelic.com/docs/agents/java-agent/troubleshooting/generate-debug-logs-troubleshooting-java on how to generate verbose logging information.
If the method hands off to a worker or process that is out of the scope of the agent, that could explain not reporting for that method.
We have had customers in the past inquire about instrumenting the main method but each situation is a little bit different. We don’t have an example to share at this time.

Hello Aelahi,

It looks like Jeanie helped you out and I also wanted to mention that I did reach out to some other resources as well. The issue with instrumenting the main method is that it will never end. Basically the agent records transactions based on start of transaction to when it finishes. Since the main method - in theory - won’t end until the application is turned off, it’s a bad idea to instrument it, even if it would work. That could lead to issues with extremely long running transactions and high memory usage. This makes sense and I’m surprised it didn’t dawn on me earlier. That said, I hope that explanation helps!

1 Like