Search in sources :

Example 1 with AgentOptionParseException

use of com.teamscale.jacoco.agent.options.AgentOptionParseException in project teamscale-jacoco-agent by cqse.

the class AgentBase method premain.

/**
 * Called by the actual premain method once the agent is isolated from the rest of the application.
 */
public static void premain(String options, Instrumentation instrumentation) throws Exception {
    AgentOptions agentOptions;
    DelayedLogger delayedLogger = new DelayedLogger();
    try {
        agentOptions = AgentOptionsParser.parse(options, delayedLogger);
    } catch (AgentOptionParseException e) {
        try (LoggingUtils.LoggingResources ignored = initializeFallbackLogging(options, delayedLogger)) {
            delayedLogger.error("Failed to parse agent options: " + e.getMessage(), e);
            System.err.println("Failed to parse agent options: " + e.getMessage());
            // we perform actual logging output after writing to console to
            // ensure the console is reached even in case of logging issues
            // (see TS-23151). We use the Agent class here (same as below)
            Logger logger = LoggingUtils.getLogger(Agent.class);
            delayedLogger.logTo(logger);
            throw e;
        }
    }
    loggingResources = LoggingUtils.initializeLogging(agentOptions.getLoggingConfig());
    Logger logger = LoggingUtils.getLogger(Agent.class);
    delayedLogger.logTo(logger);
    HttpUtils.setShouldValidateSsl(agentOptions.shouldValidateSsl());
    logger.info("Starting JaCoCo's agent");
    JacocoAgentBuilder agentBuilder = new JacocoAgentBuilder(agentOptions);
    org.jacoco.agent.rt.internal_3570298.PreMain.premain(agentBuilder.createJacocoAgentOptions(), instrumentation);
    AgentBase agent = agentBuilder.createAgent(instrumentation);
    agent.registerShutdownHook();
}
Also used : LoggingResources(com.teamscale.jacoco.agent.util.LoggingUtils.LoggingResources) AgentOptionParseException(com.teamscale.jacoco.agent.options.AgentOptionParseException) JacocoAgentBuilder(com.teamscale.jacoco.agent.options.JacocoAgentBuilder) Logger(org.slf4j.Logger) AgentOptions(com.teamscale.jacoco.agent.options.AgentOptions)

Example 2 with AgentOptionParseException

use of com.teamscale.jacoco.agent.options.AgentOptionParseException in project teamscale-jacoco-agent by cqse.

the class TeamscaleConfig method getCommitFromManifest.

/**
 * Reads `Branch` and `Timestamp` entries from the given jar/war file's manifest and builds a commit descriptor out
 * of it.
 */
private CommitDescriptor getCommitFromManifest(File jarFile) throws AgentOptionParseException {
    Manifest manifest = getManifestFromJarFile(jarFile);
    String branch = manifest.getMainAttributes().getValue("Branch");
    String timestamp = manifest.getMainAttributes().getValue("Timestamp");
    if (StringUtils.isEmpty(branch)) {
        throw new AgentOptionParseException("No entry 'Branch' in MANIFEST");
    } else if (StringUtils.isEmpty(timestamp)) {
        throw new AgentOptionParseException("No entry 'Timestamp' in MANIFEST");
    }
    logger.debug("Found commit " + branch + ":" + timestamp + " in file " + jarFile);
    return new CommitDescriptor(branch, timestamp);
}
Also used : AgentOptionParseException(com.teamscale.jacoco.agent.options.AgentOptionParseException) CommitDescriptor(com.teamscale.client.CommitDescriptor) Manifest(java.util.jar.Manifest)

Example 3 with AgentOptionParseException

use of com.teamscale.jacoco.agent.options.AgentOptionParseException in project teamscale-jacoco-agent by cqse.

the class AgentBase method initializeFallbackLogging.

/**
 * Initializes fallback logging in case of an error during the parsing of the options to {@link #premain(String,
 * Instrumentation)} (see TS-23151). This tries to extract the logging configuration and use this and falls back to
 * the default logger.
 */
private static LoggingResources initializeFallbackLogging(String premainOptions, DelayedLogger delayedLogger) {
    for (String optionPart : premainOptions.split(",")) {
        if (optionPart.startsWith(AgentOptionsParser.LOGGING_CONFIG_OPTION + "=")) {
            return createFallbackLoggerFromConfig(optionPart.split("=", 2)[1], delayedLogger);
        }
        if (optionPart.startsWith(AgentOptionsParser.CONFIG_FILE_OPTION + "=")) {
            String configFileValue = optionPart.split("=", 2)[1];
            Optional<String> loggingConfigLine = Optional.empty();
            try {
                File configFile = new FilePatternResolver(delayedLogger).parsePath(AgentOptionsParser.CONFIG_FILE_OPTION, configFileValue).toFile();
                loggingConfigLine = FileSystemUtils.readLinesUTF8(configFile).stream().filter(line -> line.startsWith(AgentOptionsParser.LOGGING_CONFIG_OPTION + "=")).findFirst();
            } catch (IOException | AgentOptionParseException e) {
                delayedLogger.error("Failed to load configuration from " + configFileValue + ": " + e.getMessage(), e);
            }
            if (loggingConfigLine.isPresent()) {
                return createFallbackLoggerFromConfig(loggingConfigLine.get().split("=", 2)[1], delayedLogger);
            }
        }
    }
    return LoggingUtils.initializeDefaultLogging();
}
Also used : FilePatternResolver(com.teamscale.jacoco.agent.options.FilePatternResolver) AgentOptionParseException(com.teamscale.jacoco.agent.options.AgentOptionParseException) IOException(java.io.IOException) File(java.io.File)

Example 4 with AgentOptionParseException

use of com.teamscale.jacoco.agent.options.AgentOptionParseException in project teamscale-jacoco-agent by cqse.

the class TeamscaleConfig method getRevisionFromManifest.

/**
 * Reads `Git_Commit` entry from the given jar/war file's manifest and sets it as revision.
 */
private String getRevisionFromManifest(File jarFile) throws AgentOptionParseException {
    Manifest manifest = getManifestFromJarFile(jarFile);
    String revision = manifest.getMainAttributes().getValue("Revision");
    if (StringUtils.isEmpty(revision)) {
        // currently needed option for a customer
        if (manifest.getAttributes("Git") != null) {
            revision = manifest.getAttributes("Git").getValue("Git_Commit");
        }
        if (StringUtils.isEmpty(revision)) {
            throw new AgentOptionParseException("No entry 'Revision' in MANIFEST");
        }
    }
    logger.debug("Found revision " + revision + " in file " + jarFile);
    return revision;
}
Also used : AgentOptionParseException(com.teamscale.jacoco.agent.options.AgentOptionParseException) Manifest(java.util.jar.Manifest)

Aggregations

AgentOptionParseException (com.teamscale.jacoco.agent.options.AgentOptionParseException)4 Manifest (java.util.jar.Manifest)2 CommitDescriptor (com.teamscale.client.CommitDescriptor)1 AgentOptions (com.teamscale.jacoco.agent.options.AgentOptions)1 FilePatternResolver (com.teamscale.jacoco.agent.options.FilePatternResolver)1 JacocoAgentBuilder (com.teamscale.jacoco.agent.options.JacocoAgentBuilder)1 LoggingResources (com.teamscale.jacoco.agent.util.LoggingUtils.LoggingResources)1 File (java.io.File)1 IOException (java.io.IOException)1 Logger (org.slf4j.Logger)1