Class PostgreSqlJson
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Callable<Integer>
PostgreSqlJson
class is a command-line tool for reading
PostgreSQL DBMS logs in JSON format and sending them to an OpenTelemetry collector.
It can optionally use a LogEnricher
to add additional data to the logs.-
Field Summary
FieldsModifier and TypeFieldDescription(package private) String
Path to file to save current processed position in log files.(package private) LogEnricher
LogEnricher instance to use in log processing.(package private) int
Database query cache size.(package private) String
The database name.(package private) String
The host name of the PostgreSQL server.(package private) String
Password for PG connection.(package private) int
The port number the PostgreSQL server is listening on.(package private) String
The database user on whose behalf the connection is being made.Store current log position for each log file.(package private) long
Interval of saving (in second) of the current read position in the log files.(package private) String
Path to PostgreSQL log directory in JSON format. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncall()
Calls the method responsible for watching PostgreSQL logs.void
close()
protected WatchService
Returns a newWatchService
instance that can be used to watch objects for changes in a file tree.protected void
initialLogImport
(File sourceDirectory) Performs the initial import of PostgreSQL JSON log files from the specified directory.(package private) void
Initializes the log enricher for PostgreSQL logs.static void
Main method to execute the application.(package private) void
parseLogLine
(String line, String logName) Parses a single line of PostgreSQL JSON log and logs it according to its severity level.protected Thread
Manages the tasks related to handling positional information for PostgreSQL log files.(package private) void
readJsonLog
(File jsonLog) Reads a JSON log file and processes each line.protected void
registerWatchEvent
(Path dirToWatch, WatchService watchService) Registers the specified dir with the given watch service to listen for file creation and modification events.protected void
Saves the current read positions of PostgreSQL log files.int
Monitors the PostgreSQL log directory for changes and processes the logs.
-
Field Details
-
position
Store current log position for each log file. -
logEnricher
LogEnricher logEnricherLogEnricher instance to use in log processing. -
watchDir
String watchDirPath to PostgreSQL log directory in JSON format. -
saveInterval
long saveIntervalInterval of saving (in second) of the current read position in the log files. The value must be within a range from 1 till 1000 second. -
posgreSqlHost
String posgreSqlHostThe host name of the PostgreSQL server. -
posgreSqlPort
int posgreSqlPortThe port number the PostgreSQL server is listening on. -
posgreSqlDatabase
String posgreSqlDatabaseThe database name. -
posgreSqlUserName
String posgreSqlUserNameThe database user on whose behalf the connection is being made. -
posgreSqlPassword
String posgreSqlPasswordPassword for PG connection. -
maximumQueryCacheSize
int maximumQueryCacheSizeDatabase query cache size. -
currentLogPositionFile
String currentLogPositionFilePath to file to save current processed position in log files. Required write capability for this program.
-
-
Constructor Details
-
PostgreSqlJson
public PostgreSqlJson()
-
-
Method Details
-
main
Main method to execute the application. Initializes the PostgreSqlJson class, executes the command line arguments, and exits the system if required.- Parameters:
args
- Command line arguments passed to the application.
-
call
Calls the method responsible for watching PostgreSQL logs. -
watchPostgreSqlLogs
Monitors the PostgreSQL log directory for changes and processes the logs.This method sets up a WatchService to continuously monitor the specified directory for new or modified log files in JSON format. When such a file is detected, it is processed to extract relevant log information.
The method also makes use of a log enricher (if configured) to enhance the log data with additional information.
- Returns:
- system exit code
- Throws:
IOException
- if an I/O error occurs initializing the watcher or processing the logs.InterruptedException
- if the watch service is interrupted while waiting for events.
-
initLogEnricher
void initLogEnricher()Initializes the log enricher for PostgreSQL logs.This method creates an instance of
LogEnricherPostgreSql
with the specified PostgreSQL host, port, database, username, password, and query cache size.If the PostgreSQL host is not set or is empty, the method does not initialize the log enricher, logs an error and instantiate EnrichmentOff instead of LogEnricherPostgreSql.
-
registerWatchEvent
Registers the specified dir with the given watch service to listen for file creation and modification events.- Parameters:
dirToWatch
- the directory path to be watchedwatchService
- the watch service to which the directory should be registered- Throws:
IOException
- if an I/O error occurs
-
getWatchService
Returns a newWatchService
instance that can be used to watch objects for changes in a file tree.- Returns:
- a new
WatchService
instance - Throws:
IOException
- if an I/O error occurs
-
initialLogImport
Performs the initial import of PostgreSQL JSON log files from the specified directory.This method reads all JSON log files in the given directory, sorts them by name, and processes each file by calling the
readJsonLog(File)
method.- Parameters:
sourceDirectory
- the directory containing the PostgreSQL JSON log files to be imported.- Throws:
IOException
- if an I/O error occurs while reading the log files.
-
parseLogLine
Parses a single line of PostgreSQL JSON log and logs it according to its severity level.This method reads a log line in JSON format, determines the severity level of the log message, and logs the message using the appropriate logging level. If the corresponding logging level (TRACE, DEBUG, INFO, WARN, ERROR) is not enabled, the method will return immediately without further processing the log line.
The method uses the
error_severity
field from the JSON log to determine the logging level and creates a logging event with additional key-value pairs extracted from the JSON log. It also handles various log message formats, adjusting or adding specific log attributes such as duration, plan, parse, and bind if they are present in the message.- Parameters:
line
- the log line in JSON format to be parsed.logName
- the name of the log file from which the line was read.
-
positionFileTasks
Manages the tasks related to handling positional information for PostgreSQL log files.This method processes and updates the positional information of log files to ensure that logs are correctly read and parsed from the last known position. It handles the initialization, periodic updates, and finalization of positions for different log files. The method ensures that log parsing can resume correctly after restarts or interruptions by accurately maintaining and updating the position information.
The method typically involves the following tasks:
- Initializing the position map for new log files.
- Updating the position as logs are read and processed.
- Saving the current position to persistent storage.
This method is crucial for log processing scenarios where it's important to not miss any log entries and to avoid reprocessing already handled logs.
- Returns:
- return shutdown hook thread
- Throws:
IOException
- if an I/O error occurs while managing the log file positions.
-
saveLogFilesPosition
protected void saveLogFilesPosition()Saves the current read positions of PostgreSQL log files.This method writes the current positions of log files to a specified file, ensuring accurate resume points after restarts or interruptions. It ensures atomicity and consistency to prevent data loss.
-
readJsonLog
Reads a JSON log file and processes each line. This method reads through the specified JSON log file starting from the last saved position, parses each line, and processes it according to the log's severity level. It updates the read position after processing each log entry to ensure accurate resumption.- Parameters:
jsonLog
- the JSON log file to read- Throws:
IOException
- if an error occurs while reading the log file
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-