subtest txn_mode_tracing_on
# When tracing is on we should log all execution events.

exec-sql
SET CLUSTER SETTING sql.telemetry.transaction_sampling.max_event_frequency = 10;
----

exec-sql
SET CLUSTER SETTING sql.telemetry.transaction_sampling.statement_events_per_transaction.max = 100;
----

exec-sql
SET CLUSTER SETTING sql.telemetry.query_sampling.mode = "transaction";
----

exec-sql
SET CLUSTER SETTING sql.telemetry.query_sampling.enabled = true;
----

exec-sql
CREATE TABLE t()
----

spy-sql unixSecs=0.1
SET TRACING = ON;
----

spy-sql unixSecs=0.1 useRealTracing=true
SELECT * FROM t LIMIT 1;
----
{
	"ApplicationName": "telemetry-logging-datadriven",
	"Database": "defaultdb",
	"Distribution": "full",
	"EventType": "sampled_query",
	"PlanGist": "AgHQAQIAAAAAAg==",
	"ScanCount": 1,
	"Statement": "SELECT * FROM \"\".\"\".t LIMIT ‹1›",
	"StatementFingerprintID": "13897981974204408897",
	"StmtPosInTxn": 1,
	"Tag": "SELECT",
	"User": "root"
}
{
	"ApplicationName": "telemetry-logging-datadriven",
	"Committed": true,
	"EventType": "sampled_transaction",
	"NumRows": 0,
	"RowsRead": 0,
	"RowsWritten": 0,
	"StatementFingerprintIDs": [
		"13897981974204408897"
	],
	"TransactionFingerprintID": "8051364883217634206",
	"User": "root"
}

spy-sql unixSecs=0.1 useRealTracing=true
SELECT 1, 2, 3;
----
{
	"ApplicationName": "telemetry-logging-datadriven",
	"Database": "defaultdb",
	"Distribution": "local",
	"EventType": "sampled_query",
	"NumRows": 1,
	"OutputRowsEstimate": 1,
	"PlanGist": "AgICBgYG",
	"Statement": "SELECT ‹1›, ‹2›, ‹3›",
	"StatementFingerprintID": "16048716862824136030",
	"StmtPosInTxn": 1,
	"Tag": "SELECT",
	"User": "root"
}
{
	"ApplicationName": "telemetry-logging-datadriven",
	"Committed": true,
	"EventType": "sampled_transaction",
	"NumRows": 1,
	"RowsRead": 0,
	"RowsWritten": 0,
	"StatementFingerprintIDs": [
		"16048716862824136030"
	],
	"TransactionFingerprintID": "8204366343298189953",
	"User": "root"
}

spy-sql unixSecs=0.1 useRealTracing=true
SELECT 'hello';
----
{
	"ApplicationName": "telemetry-logging-datadriven",
	"Database": "defaultdb",
	"Distribution": "local",
	"EventType": "sampled_query",
	"NumRows": 1,
	"OutputRowsEstimate": 1,
	"PlanGist": "AgICAgYC",
	"Statement": "SELECT ‹'hello'›",
	"StatementFingerprintID": "2101516650360649860",
	"StmtPosInTxn": 1,
	"Tag": "SELECT",
	"User": "root"
}
{
	"ApplicationName": "telemetry-logging-datadriven",
	"Committed": true,
	"EventType": "sampled_transaction",
	"NumRows": 1,
	"RowsRead": 0,
	"RowsWritten": 0,
	"StatementFingerprintIDs": [
		"2101516650360649860"
	],
	"TransactionFingerprintID": "12846987492365242203",
	"User": "root"
}

spy-sql unixSecs=0.1
SET TRACING = off;
----

subtest end

subtest txn_mode_console_query
# When tracing is on we should log all execution events.

exec-sql
SET CLUSTER SETTING sql.telemetry.transaction_sampling.max_event_frequency = 10;
----

exec-sql
SET CLUSTER SETTING sql.telemetry.transaction_sampling.statement_events_per_transaction.max = 100;
----

exec-sql
SET CLUSTER SETTING sql.telemetry.query_sampling.mode = "transaction";
----

exec-sql
SET CLUSTER SETTING sql.telemetry.query_sampling.enabled = true;
----

spy-sql unixSecs=0.1
SET application_name = '$ internal-console-app';
----
{
	"ApplicationName": "$ internal-console-app",
	"Database": "defaultdb",
	"Distribution": "local",
	"EventType": "sampled_query",
	"PlanGist": "Ais=",
	"Statement": "SET application_name = ‹'$ internal-console-app'›",
	"StatementFingerprintID": "16494915433690409681",
	"StmtPosInTxn": 1,
	"Tag": "SET",
	"User": "root"
}

spy-sql unixSecs=0.1
SELECT * FROM t LIMIT 1;
----
{
	"ApplicationName": "$ internal-console-app",
	"Database": "defaultdb",
	"Distribution": "full",
	"EventType": "sampled_query",
	"PlanGist": "AgHQAQIAAAAAAg==",
	"ScanCount": 1,
	"Statement": "SELECT * FROM \"\".\"\".t LIMIT ‹1›",
	"StatementFingerprintID": "13897981974204408897",
	"StmtPosInTxn": 1,
	"Tag": "SELECT",
	"User": "root"
}
{
	"ApplicationName": "$ internal-console-app",
	"Committed": true,
	"EventType": "sampled_transaction",
	"NumRows": 0,
	"RowsRead": 0,
	"RowsWritten": 0,
	"SkippedTransactions": 5,
	"StatementFingerprintIDs": [
		"13897981974204408897"
	],
	"TransactionFingerprintID": "8051364883217634206",
	"User": "root"
}

spy-sql unixSecs=0.1
SELECT 1, 2, 3;
----
{
	"ApplicationName": "$ internal-console-app",
	"Database": "defaultdb",
	"Distribution": "local",
	"EventType": "sampled_query",
	"NumRows": 1,
	"OutputRowsEstimate": 1,
	"PlanGist": "AgICBgYG",
	"Statement": "SELECT ‹1›, ‹2›, ‹3›",
	"StatementFingerprintID": "16048716862824136030",
	"StmtPosInTxn": 1,
	"Tag": "SELECT",
	"User": "root"
}
{
	"ApplicationName": "$ internal-console-app",
	"Committed": true,
	"EventType": "sampled_transaction",
	"NumRows": 1,
	"RowsRead": 0,
	"RowsWritten": 0,
	"StatementFingerprintIDs": [
		"16048716862824136030"
	],
	"TransactionFingerprintID": "8204366343298189953",
	"User": "root"
}

spy-sql unixSecs=0.1
SELECT 'hello';
----
{
	"ApplicationName": "$ internal-console-app",
	"Database": "defaultdb",
	"Distribution": "local",
	"EventType": "sampled_query",
	"NumRows": 1,
	"OutputRowsEstimate": 1,
	"PlanGist": "AgICAgYC",
	"Statement": "SELECT ‹'hello'›",
	"StatementFingerprintID": "2101516650360649860",
	"StmtPosInTxn": 1,
	"Tag": "SELECT",
	"User": "root"
}
{
	"ApplicationName": "$ internal-console-app",
	"Committed": true,
	"EventType": "sampled_transaction",
	"NumRows": 1,
	"RowsRead": 0,
	"RowsWritten": 0,
	"StatementFingerprintIDs": [
		"2101516650360649860"
	],
	"TransactionFingerprintID": "12846987492365242203",
	"User": "root"
}

# Reset app name. Note that the sampled_query event
# will still be logged due to the current application
# being set to the internal console app name.
spy-sql unixSecs=0.1
SET application_name = 'telemetry-logging';
----
{
	"ApplicationName": "telemetry-logging",
	"Database": "defaultdb",
	"Distribution": "local",
	"EventType": "sampled_query",
	"PlanGist": "Ais=",
	"Statement": "SET application_name = ‹'telemetry-logging'›",
	"StatementFingerprintID": "16494915433690409681",
	"StmtPosInTxn": 1,
	"Tag": "SET",
	"User": "root"
}

subtest end

subtest stmt_mode_force_non_dml_tcl_statements
# sampled_query events should be force logged if they are not of type DML or TCL

reset-last-sampled
----


reset-telemetry-cluster-settings
----


# This first SELECT statement should be emitted.
spy-sql unixSecs=1
SELECT 1;
----
{
	"ApplicationName": "telemetry-logging",
	"Database": "defaultdb",
	"Distribution": "local",
	"EventType": "sampled_query",
	"NumRows": 1,
	"OutputRowsEstimate": 1,
	"PlanGist": "AgICAgYC",
	"Statement": "SELECT ‹1›",
	"StatementFingerprintID": "2101516650360649860",
	"StmtPosInTxn": 1,
	"Tag": "SELECT",
	"User": "root"
}

# Now we'll execute some statements stubbed at the same time.
# All statement types that aren't TCL or DML should be logged.

# CREATE statement should always be emitted.
spy-sql unixSecs=1
CREATE TABLE foo (a INT, b INT);
----
{
	"ApplicationName": "telemetry-logging",
	"Database": "defaultdb",
	"Distribution": "local",
	"EventType": "sampled_query",
	"PlanGist": "AiXKAQ==",
	"Statement": "CREATE TABLE defaultdb.public.foo (a INT8, b INT8)",
	"StatementFingerprintID": "12417278439037856579",
	"StmtPosInTxn": 1,
	"Tag": "CREATE TABLE",
	"User": "root"
}

# Skipped due to not enough time elapsed.
spy-sql unixSecs=1
SELECT * FROM foo
----


# Skipped due to not enough time elapsed.
spy-sql unixSecs=1
BEGIN;
INSERT INTO foo VALUES (1, 2), (3, 4);
COMMIT;
----


spy-sql unixSecs=1
BEGIN;
SET TRANSACTION PRIORITY HIGH;
SELECT 1;
CREATE USER craig;
GRANT admin TO craig;
COMMIT;
----
{
	"ApplicationName": "telemetry-logging",
	"Database": "defaultdb",
	"Distribution": "local",
	"EventType": "sampled_query",
	"PlanGist": "Ais=",
	"SkippedQueries": 7,
	"Statement": "CREATE USER craig",
	"StatementFingerprintID": "4157831042514459868",
	"StmtPosInTxn": 3,
	"Tag": "CREATE ROLE",
	"User": "root"
}
{
	"ApplicationName": "telemetry-logging",
	"Database": "defaultdb",
	"Distribution": "local",
	"EventType": "sampled_query",
	"PlanGist": "Ais=",
	"Statement": "GRANT admin TO craig",
	"StatementFingerprintID": "9654126880073528913",
	"StmtPosInTxn": 4,
	"Tag": "GRANT",
	"User": "root"
}


subtest end
