com.exasol.udfdebugging.modules.udflogs.UdfLogsModule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of udf-debugging-java Show documentation
Show all versions of udf-debugging-java Show documentation
Utilities for debugging, profiling and code coverage measure for UDFs.
The newest version!
package com.exasol.udfdebugging.modules.udflogs;
import java.net.InetSocketAddress;
import java.nio.file.Path;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import com.exasol.errorreporting.ExaError;
import com.exasol.udfdebugging.LocalServiceExposer;
import com.exasol.udfdebugging.Module;
/**
* This module redirects the STDOUT of UDFs to files on the test host.
*
* For that it uses the {@code ALTER SESSION SET SCRIPT_OUTPUT_ADDRESS} command of the Exasol database.
*
*/
public class UdfLogsModule implements Module {
private static final Logger LOGGER = Logger.getLogger(UdfLogsModule.class.getName());
private final LogRecorder logRecorder;
private final List capturedLogFiles = new ArrayList<>();
/**
* Create a new instance of {@link UdfLogsModule}.
*
* @param localServiceExposer proxy factory that makes ports of the test host available in the container
* @param exasolConnection connection to the exasol database
*/
public UdfLogsModule(final LocalServiceExposer localServiceExposer, final Connection exasolConnection) {
final Consumer logFileHandler = file -> {
this.capturedLogFiles.add(file);
LOGGER.log(Level.INFO, "Created log file for UDF output: {0}", file);
};
this.logRecorder = new LogRecorder(logFileHandler);
final InetSocketAddress inDbAddress = localServiceExposer
.exposeLocalServiceToDatabase(this.logRecorder.getPort());
redirectLogging(exasolConnection, inDbAddress);
}
@Override
public Stream getJvmOptions() {
return Stream.empty();
}
/**
* Get all log files that were captured.
*
* @return list of captured log files.
*/
public List getCapturedLogFiles() {
return this.capturedLogFiles;
}
private void redirectLogging(final Connection exasolConnection, final InetSocketAddress logServerAddress) {
try (final Statement statement = exasolConnection.createStatement()) {
final String logServerAddressString = logServerAddress.getHostString() + ":" + logServerAddress.getPort();
if (logServerAddressString.contains("'")) {
throw new IllegalArgumentException(ExaError.messageBuilder("F-UDJ-19")
.message("Invalid address {{address}}. The address must not contain a quotes.",
logServerAddressString)
.toString());
}
statement.executeUpdate("ALTER SESSION SET SCRIPT_OUTPUT_ADDRESS = '" + logServerAddressString + "';");
} catch (final SQLException exception) {
throw new IllegalStateException(
ExaError.messageBuilder("E-UDJ-16").message("Failed to set script output address.").toString(),
exception);
}
}
@Override
public void close() {
try {
this.logRecorder.close();
} catch (final Exception exception) {
// at least we tried
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy