Skip to content
/ server Public

Comments

MDEV-18386: Add server_audit_timestamp_format to customize audit log timestamps#4633

Open
abhishek593 wants to merge 9 commits intoMariaDB:mainfrom
abhishek593:MDEV-18386
Open

MDEV-18386: Add server_audit_timestamp_format to customize audit log timestamps#4633
abhishek593 wants to merge 9 commits intoMariaDB:mainfrom
abhishek593:MDEV-18386

Conversation

@abhishek593
Copy link

This PR adds a new global system variable, server_audit_timestamp_format, to the MariaDB Audit Plugin.

Key changes:

  • Users can now specify a custom strftime format string for audit log timestamps.
  • Default behavior is preserved if the variable is left empty.
  • Added a new test case server_audit_timestamp.test to verify custom formats, including timezone offsets (%z).
  • Updated existing audit tests to reflect the new system variable.

@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Feb 9, 2026
@gkodinov gkodinov self-assigned this Feb 11, 2026
Copy link
Member

@gkodinov gkodinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a preliminary review. Thank you for your contribution!

Copy link
Member

@gkodinov gkodinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was a good first iteration. Thanks for working on that. Another round of comments coming up.

Copy link
Member

@gkodinov gkodinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after fixing the below. Please expect a final review.

NULL, NULL, 1024, 0, 0x7FFFFFFF, 1);
static MYSQL_SYSVAR_STR(timestamp_format, timestamp_format,
PLUGIN_VAR_RQCMDARG,
"The format string the strftime() routine applies with its format argument",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say: a format string used to print the timestamp into the audit log messages. The format used is the same as strftime. Default is empty, meaning use the existing hard-coded format.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (timestamp_format_buffer[0])
ts_len= strftime(ts_tmp, sizeof(ts_tmp), timestamp_format_buffer, &tm_time);

if (ts_len == 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just do "else" here. Easier to read.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

{
if (!(is_active= (logger_write(logfile, message, len) == (int) len)))
struct tm tm_time;
char ts_tmp[TIMESTAMP_OUTPUT_LENGTH];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

256 bytes in the stack ... this is a bit much. I'd allocate a buffer outside of the lock and pass it down. Or, better, it's going at the start of message anyway, can't you just print it there directly instead of using a temp buffer and then memcpy() into it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, so here's what I changed. Removed the temp buffer, and directly stored the output of strftime in message buffer at the beginning, then used memmove to shift the output from the start to the end of reserved space.

/**
Write to the log, acquiring the lock.
*/
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't do white-space only changes please!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const char *userip, unsigned int userip_len,
unsigned int connection_id, unsigned int port,
long long query_id, const char *operation)
const char *serverhost, size_t serverhost_len,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no white-space only changes please

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

time_t ctime;
size_t csize;
char message[1024];
char raw_message[1024 + TIMESTAMP_OUTPUT_LENGTH];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use some named const size_t for the 1k constant

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


(void) time(&ctime);
csize= log_header(message, sizeof(message)-1, &ctime,
csize= log_header(message, 1024-1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to do this? Can't you just pass the actual length as you used to do? And then do the calculation inside this func?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously message was a fixed size array (1024). In the new version, its a pointer to an offset within a large buffer, so we must explicitly pass the correct size (MAX_AUDIT_PAYLOAD_LENGTH).

time_t ctime;
size_t csize;
char message[1024];
char raw_message[1024 + TIMESTAMP_OUTPUT_LENGTH];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here: use a named constant for 1k

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@gkodinov gkodinov assigned vuvova and unassigned gkodinov Feb 13, 2026
@vuvova vuvova self-requested a review February 21, 2026 09:01
timestamp_format);
CLIENT_ERROR(1, "server_audit_timestamp_format can't exceed %d characters.",
MYF(ME_WARNING), TIMESTAMP_FORMAT_LENGTH - 1);
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validity checks must be done in the check callback, not in the update callbac

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

PLUGIN_VAR_RQCMDARG,
"A format string used to print the timestamp into the audit log messages "
"The format used is the same as strftime and the default is empty which uses "
"the existing hard-coded format",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small issue: don't make it "empty = hard-coded", it's not very intuitive, simply set the default value of timestamp_format to the correct (backward compatible) value.

Big issue: don't use strftime. The server already has date-to-string formatting function DATE_FORMAT(). We should not let different parts of the server to implement formatting differently. For example, %M should always and everywhere have the same meaning.

That is, invoke server's date-to-string function. And to do that, it must be in a service. But, perhaps, you can add a new method to the timezone service and not create a new one.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the implementation and added a new method in the timezone service.

@abhishek593 abhishek593 requested a review from vuvova February 22, 2026 08:31
Copy link
Member

@vuvova vuvova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks quite good. The service is almost correct and its usage is correct. %z should print global time zone. And I don't quite understand why you moved time->string conversion away from log_header

select 5;
5
5
set global server_audit_timestamp_format='%Y%m%d %H:%i:%s';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can try set global server_audit_timestamp_format=default instead

--plugin-load-add=server_audit
--server_audit_file_path='server_audit_timestamp.log'
--server_audit_output_type=file
--server_audit_events='query'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's test setting of timestamp format on the command line too

set global server_audit_timestamp_format='CUSTOM-DATE %Y-%m-%d';
select 2;

# Custom format 2: Including timezone (if supported by system strftime)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can now remove references to the "system strftime" from comments

#endif
#ifndef MAX_AUDIT_QUERY_LENGTH
#define MAX_AUDIT_QUERY_LENGTH 2048
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's drop these #ifndef/#endif, simply define what you want. Easier to read and we don't really expect anyone to define this outside of the plugin

str.length(0);
make_date_time(thd ? thd : current_thd, &fmt, ltime, MYSQL_TIMESTAMP_DATETIME,
thd ? thd->variables.lc_time_names :
global_system_variables.lc_time_names,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The service is good. How you use it is good. It is very correct to pass NULL as the thd here:

      thd_gmt_sec_to_TIME(NULL, &ltime, ts);
      ...
      thd_TIME_to_str(NULL, &ltime, timestamp_format_buffer,

which means "use global time zone and locale", because session settings should not affect the global audit log.

But make_date_time() uses session time zone for %z format. I suspect you need to modify make_date_time to take a time zone as an argument, just as it takes locale.

"%04d%02d%02d %02d:%02d:%02d,%.*s,%.*s,%.*s%s,%d,%lld,%s",
tm_time.tm_year+1900, tm_time.tm_mon+1, tm_time.tm_mday,
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec,
",%.*s,%.*s,%.*s%s,%d,%lld,%s",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it'd be natural to generate the timestamp string here and print like

      "%s,%.*s,%.*s,%.*s%s,%d,%lld,%s", ts_start, ...

Why did you move it to write_log() ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

3 participants