Search in sources :

Example 1 with PidFinder

use of com.microsoft.applicationinsights.agent.bootstrap.diagnostics.PidFinder in project ApplicationInsights-Java by microsoft.

the class MainEntryPoint method start.

// TODO turn this into an interceptor
@SuppressWarnings("SystemOut")
public static void start(Instrumentation instrumentation, File javaagentFile) {
    boolean success = false;
    Logger startupLogger = null;
    try {
        if (DEBUG_SIGNED_JAR_ACCESS) {
            JarVerifierClassFileTransformer transformer = new JarVerifierClassFileTransformer();
            instrumentation.addTransformer(transformer, true);
            instrumentation.retransformClasses(Class.forName("java.util.jar.JarVerifier"));
            instrumentation.removeTransformer(transformer);
        }
        Path agentPath = javaagentFile.toPath();
        // need to initialize version before initializing DiagnosticsHelper
        agentVersion = SdkVersionFinder.initVersion(agentPath);
        DiagnosticsHelper.setAgentJarFile(agentPath);
        // configuration is only read this early in order to extract logging configuration
        rpConfiguration = RpConfigurationBuilder.create(agentPath);
        configuration = ConfigurationBuilder.create(agentPath, rpConfiguration);
        startupLogger = configureLogging(configuration.selfDiagnostics, agentPath);
        StatusFile.startupLogger = startupLogger;
        ConfigurationBuilder.logConfigurationWarnMessages();
        MDC.put(DiagnosticsHelper.MDC_PROP_OPERATION, "Startup");
        // TODO convert to agent builder concept
        AppIdSupplier appIdSupplier = AiComponentInstaller.beforeAgent(instrumentation);
        StartAppIdRetrieval.setAppIdSupplier(appIdSupplier);
        AgentInstaller.installBytebuddyAgent(instrumentation, ConfigOverride.getConfig(configuration), false);
        startupLogger.info("ApplicationInsights Java Agent {} started successfully (PID {})", agentVersion, new PidFinder().getValue());
        startupLogger.info("Java version: {}, vendor: {}, home: {}", System.getProperty("java.version"), System.getProperty("java.vendor"), System.getProperty("java.home"));
        success = true;
        LoggerFactory.getLogger(DiagnosticsHelper.DIAGNOSTICS_LOGGER_NAME).info("Application Insights Codeless Agent {} Attach Successful", agentVersion);
    } catch (ThreadDeath td) {
        throw td;
    } catch (Throwable t) {
        FriendlyException friendlyException = getFriendlyException(t);
        String banner = "ApplicationInsights Java Agent " + agentVersion + " failed to start (PID " + new PidFinder().getValue() + ")";
        if (friendlyException != null) {
            logErrorMessage(startupLogger, friendlyException.getMessageWithBanner(banner), true, t, javaagentFile);
        } else {
            logErrorMessage(startupLogger, banner, false, t, javaagentFile);
        }
    } finally {
        try {
            StatusFile.putValueAndWrite("AgentInitializedSuccessfully", success, startupLogger != null);
        } catch (Throwable t) {
            if (startupLogger != null) {
                startupLogger.error("Error writing status.json", t);
            } else {
                t.printStackTrace();
            }
        }
        MDC.clear();
    }
}
Also used : Path(java.nio.file.Path) PidFinder(com.microsoft.applicationinsights.agent.bootstrap.diagnostics.PidFinder) Logger(org.slf4j.Logger) FriendlyException(com.microsoft.applicationinsights.agent.internal.common.FriendlyException)

Aggregations

PidFinder (com.microsoft.applicationinsights.agent.bootstrap.diagnostics.PidFinder)1 FriendlyException (com.microsoft.applicationinsights.agent.internal.common.FriendlyException)1 Path (java.nio.file.Path)1 Logger (org.slf4j.Logger)1