surrealdb-core 3.2.3

A scalable, distributed, collaborative, document-graph database, for the realtime web
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
//! Structured observability events dispatched by the core to observers.
//!
//! Every event is split into a `Safe` half (bounded, no customer data) and a
//! `Ctx` half (may contain namespace/database/user identifiers or SQL text).
//! Consumers writing to unauthenticated sinks MUST only read the `Safe` half.

use std::net::IpAddr;
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::time::Duration;

use uuid::Uuid;

use crate::expr::{Expr, TopLevelExpr};
use crate::iam::Level;

/// Outcome of a query, statement, or RPC call.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum Outcome {
	Success,
	Error,
	Cancelled,
}

impl Outcome {
	/// Stable lower-case label for this outcome.
	pub const fn as_label(self) -> &'static str {
		match self {
			Self::Success => "success",
			Self::Error => "error",
			Self::Cancelled => "cancelled",
		}
	}
}

/// Map a `Result` to its observable outcome.
///
/// Only `Success` and `Error` are produced here; `Cancelled` is signalled
/// explicitly by callers that know the operation was aborted (for example a
/// transaction cancel) and cannot be inferred from a `Result` alone.
impl<T, E> From<&Result<T, E>> for Outcome {
	fn from(result: &Result<T, E>) -> Self {
		match result {
			Ok(_) => Self::Success,
			Err(_) => Self::Error,
		}
	}
}

/// Bounded classification for a single SurrealQL statement.
///
/// All variants are fixed at compile time so using this as a metric attribute
/// yields bounded cardinality. Any expression that does not map to a known
/// variant collapses to [`StatementType::Other`].
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum StatementType {
	Select,
	Create,
	Update,
	Upsert,
	Delete,
	Relate,
	Insert,
	Define,
	Remove,
	Rebuild,
	Alter,
	Info,
	Live,
	Kill,
	Let,
	Return,
	Foreach,
	IfElse,
	Sleep,
	Explain,
	Begin,
	Commit,
	Cancel,
	Access,
	Use,
	Option,
	Show,
	Break,
	Continue,
	Throw,
	Block,
	Other,
}

impl StatementType {
	/// Stable snake-case label safe for use as a metric attribute value.
	pub const fn as_label(self) -> &'static str {
		match self {
			Self::Select => "select",
			Self::Create => "create",
			Self::Update => "update",
			Self::Upsert => "upsert",
			Self::Delete => "delete",
			Self::Relate => "relate",
			Self::Insert => "insert",
			Self::Define => "define",
			Self::Remove => "remove",
			Self::Rebuild => "rebuild",
			Self::Alter => "alter",
			Self::Info => "info",
			Self::Live => "live",
			Self::Kill => "kill",
			Self::Let => "let",
			Self::Return => "return",
			Self::Foreach => "foreach",
			Self::IfElse => "ifelse",
			Self::Sleep => "sleep",
			Self::Explain => "explain",
			Self::Begin => "begin",
			Self::Commit => "commit",
			Self::Cancel => "cancel",
			Self::Access => "access",
			Self::Use => "use",
			Self::Option => "option",
			Self::Show => "show",
			Self::Break => "break",
			Self::Continue => "continue",
			Self::Throw => "throw",
			Self::Block => "block",
			Self::Other => "other",
		}
	}

	/// Classify a [`TopLevelExpr`] into its bounded statement category.
	pub(crate) fn from_top_level(expr: &TopLevelExpr) -> Self {
		match expr {
			TopLevelExpr::Begin => Self::Begin,
			TopLevelExpr::Cancel => Self::Cancel,
			TopLevelExpr::Commit => Self::Commit,
			TopLevelExpr::Access(_) => Self::Access,
			TopLevelExpr::Kill(_) => Self::Kill,
			TopLevelExpr::Live(_) => Self::Live,
			TopLevelExpr::Option(_) => Self::Option,
			TopLevelExpr::Use(_) => Self::Use,
			TopLevelExpr::Show(_) => Self::Show,
			TopLevelExpr::Expr(expr) => Self::from_expr(expr),
		}
	}

	/// Classify a bare [`Expr`] into its bounded statement category.
	pub(crate) fn from_expr(expr: &Expr) -> Self {
		match expr {
			Expr::Select(_) => Self::Select,
			Expr::Create(_) => Self::Create,
			Expr::Update(_) => Self::Update,
			Expr::Upsert(_) => Self::Upsert,
			Expr::Delete(_) => Self::Delete,
			Expr::Relate(_) => Self::Relate,
			Expr::Insert(_) => Self::Insert,
			Expr::Define(_) => Self::Define,
			Expr::Remove(_) => Self::Remove,
			Expr::Rebuild(_) => Self::Rebuild,
			Expr::Alter(_) => Self::Alter,
			Expr::Info(_) => Self::Info,
			Expr::Foreach(_) => Self::Foreach,
			Expr::IfElse(_) => Self::IfElse,
			Expr::Sleep(_) => Self::Sleep,
			Expr::Explain {
				..
			} => Self::Explain,
			Expr::Let(_) => Self::Let,
			Expr::Return(_) => Self::Return,
			Expr::Break => Self::Break,
			Expr::Continue => Self::Continue,
			Expr::Throw(_) => Self::Throw,
			Expr::Block(_) => Self::Block,
			// Anything that isn't a recognised statement-shaped
			// expression collapses to `Other`. Enumerated rather than
			// using a wildcard so a new `Expr` variant forces a
			// classification decision at compile time.
			Expr::Literal(_)
			| Expr::Param(_)
			| Expr::Idiom(_)
			| Expr::Table(_)
			| Expr::Mock(_)
			| Expr::Constant(_)
			| Expr::Prefix {
				..
			}
			| Expr::Postfix {
				..
			}
			| Expr::Binary {
				..
			}
			| Expr::FunctionCall(_)
			| Expr::Closure(_) => Self::Other,
			// GQL MATCH is not a SurrealQL statement-shaped expression.
			#[cfg(feature = "gql")]
			Expr::Match(_) => Self::Other,
		}
	}

	pub(crate) fn is_dml(self) -> bool {
		matches!(
			self,
			Self::Create | Self::Update | Self::Upsert | Self::Delete | Self::Relate | Self::Insert
		)
	}
}

/// Scope of an authentication credential, bounded to the [`Level`] discriminator.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum AuthScope {
	None,
	Root,
	Namespace,
	Database,
	Record,
}

impl AuthScope {
	/// Stable lower-case label for this scope.
	pub const fn as_label(self) -> &'static str {
		match self {
			Self::None => "none",
			Self::Root => "root",
			Self::Namespace => "namespace",
			Self::Database => "database",
			Self::Record => "record",
		}
	}
}

impl From<&Level> for AuthScope {
	fn from(level: &Level) -> Self {
		match level {
			Level::No => Self::None,
			Level::Root => Self::Root,
			Level::Namespace(_) => Self::Namespace,
			Level::Database(_, _) => Self::Database,
			Level::Record(_, _, _) => Self::Record,
		}
	}
}

/// Authentication-related action.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum AuthAction {
	Signin,
	Signup,
	Authenticate,
	Refresh,
	Invalidate,
	Revoke,
}

impl AuthAction {
	pub const fn as_label(self) -> &'static str {
		match self {
			Self::Signin => "signin",
			Self::Signup => "signup",
			Self::Authenticate => "authenticate",
			Self::Refresh => "refresh",
			Self::Invalidate => "invalidate",
			Self::Revoke => "revoke",
		}
	}
}

/// Lifecycle action of a session.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum SessionAction {
	Connect,
	Disconnect,
}

impl SessionAction {
	pub const fn as_label(self) -> &'static str {
		match self {
			Self::Connect => "connect",
			Self::Disconnect => "disconnect",
		}
	}
}

/// Wire protocol backing a session.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum SessionProtocol {
	WebSocket,
	Http,
}

impl SessionProtocol {
	pub const fn as_label(self) -> &'static str {
		match self {
			Self::WebSocket => "websocket",
			Self::Http => "http",
		}
	}
}

/// Direction of bytes counted on a [`NetworkBytesEvent`].
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum NetworkDirection {
	Received,
	Sent,
}

impl NetworkDirection {
	pub const fn as_label(self) -> &'static str {
		match self {
			Self::Received => "received",
			Self::Sent => "sent",
		}
	}
}

/// Bounded classification of an HTTP request method.
///
/// Used as a metric attribute and audit field; all variants are fixed at
/// compile time so cardinality is closed. Anything outside the standard
/// set collapses to [`HttpMethod::Other`].
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum HttpMethod {
	Get,
	Post,
	Put,
	Patch,
	Delete,
	Head,
	Options,
	Connect,
	Trace,
	Other,
}

impl HttpMethod {
	/// Stable lower-case label safe for use as a metric attribute value.
	pub const fn as_label(self) -> &'static str {
		match self {
			Self::Get => "get",
			Self::Post => "post",
			Self::Put => "put",
			Self::Patch => "patch",
			Self::Delete => "delete",
			Self::Head => "head",
			Self::Options => "options",
			Self::Connect => "connect",
			Self::Trace => "trace",
			Self::Other => "other",
		}
	}

	/// Classify a method name from an `http::Method`-like source.
	///
	/// Accepts the upper-case canonical form (`GET`, `POST`, …) as well as
	/// any other casing; non-standard names collapse to
	/// [`HttpMethod::Other`].
	pub fn from_method_str(name: &str) -> Self {
		match name.as_bytes() {
			b"GET" => Self::Get,
			b"POST" => Self::Post,
			b"PUT" => Self::Put,
			b"PATCH" => Self::Patch,
			b"DELETE" => Self::Delete,
			b"HEAD" => Self::Head,
			b"OPTIONS" => Self::Options,
			b"CONNECT" => Self::Connect,
			b"TRACE" => Self::Trace,
			_ => Self::Other,
		}
	}
}

/// Bounded classification of the HTTP wire-protocol version.
///
/// Used as a metric attribute. Unknown versions collapse to
/// [`HttpVersion::Other`].
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum HttpVersion {
	Http10,
	Http11,
	Http2,
	Http3,
	Other,
}

impl HttpVersion {
	/// Stable label safe for use as a metric attribute value.
	pub const fn as_label(self) -> &'static str {
		match self {
			Self::Http10 => "1_0",
			Self::Http11 => "1_1",
			Self::Http2 => "2",
			Self::Http3 => "3",
			Self::Other => "other",
		}
	}

	/// Classify a version string of the form `HTTP/1.0`, `HTTP/1.1`,
	/// `HTTP/2.0`, etc. Unknown forms collapse to [`HttpVersion::Other`].
	pub fn from_version_str(version: &str) -> Self {
		// Tolerate either the wire form (`HTTP/1.1`) or the bare numeric
		// suffix (`1.1`). Anything else collapses to `Other`.
		let stripped = version.strip_prefix("HTTP/").unwrap_or(version);
		match stripped {
			"1.0" | "10" => Self::Http10,
			"1.1" | "11" => Self::Http11,
			"2.0" | "2" => Self::Http2,
			"3.0" | "3" => Self::Http3,
			_ => Self::Other,
		}
	}
}

// --- StatementEvent ---

/// Safe portion of a [`StatementEvent`]. Attributes: bounded, no customer data.
#[derive(Clone, Copy, Debug)]
pub struct StatementEventSafe {
	pub kind: StatementType,
	pub outcome: Outcome,
	pub duration: Duration,
	pub read_only: bool,
	/// Number of rows returned (SELECT) or affected (CREATE / UPDATE /
	/// UPSERT / DELETE / RELATE / INSERT). Non-DML statements emit `0`.
	///
	/// For DML statements the count reflects records actually mutated by
	/// the iterator -- not the length of the post-RETURN value. This
	/// matters because `RETURN NONE` causes the iterator to drop every
	/// per-document value (it would otherwise yield an empty array even
	/// when N records were modified), and `RETURN BEFORE` on a fresh
	/// `CREATE` would otherwise collapse to `Value::None`.
	///
	/// Bounded scalar, non-identifying -- safe to surface as a counter
	/// increment on the dimensional metrics observer.
	pub result_rows: u64,
	/// Bounded error classification when `outcome` is [`Outcome::Error`].
	/// `None` for success / cancelled. Drawn from a fixed enum at the
	/// recording site so it stays safe for label-cardinality purposes.
	pub error_class: Option<&'static str>,
}

/// Pre-resolved tenant identity attached to events that originate inside the
/// executor or transaction layer.
///
/// Built once per request from the active [`crate::dbs::Session`] and stashed
/// on the [`crate::ctx::Context`] (and on each [`crate::kvs::Transaction`]
/// derived from that context) so emit sites can populate the `*Ctx` half of
/// each event without re-parsing the session value tree on every call.
///
/// `user` follows the same record-access collapsing rule as
/// [`NetworkBytesEventCtx::from_session`]: anonymous sessions map to `None`,
/// record-access principals to a fixed `<record>` sentinel, and everything
/// else to the actor id.
#[derive(Clone, Debug, Default)]
pub struct TenantIdentity {
	pub namespace: Option<String>,
	pub database: Option<String>,
	pub user: Option<String>,
	pub session_id: Option<Uuid>,
	pub client_ip: Option<IpAddr>,
}

impl TenantIdentity {
	/// Build a [`TenantIdentity`] from an authenticated [`crate::dbs::Session`].
	pub fn from_session(sess: &crate::dbs::Session) -> Self {
		let user = if sess.au.is_anon() {
			None
		} else if sess.au.is_record() {
			Some("<record>".to_owned())
		} else {
			Some(sess.au.id().to_owned())
		};
		let client_ip = sess.ip.as_deref().and_then(|raw| raw.parse::<IpAddr>().ok());
		Self {
			namespace: sess.ns.clone(),
			database: sess.db.clone(),
			user,
			session_id: sess.id,
			client_ip,
		}
	}

	/// Project into a [`StatementEventCtx`] preserving the (optional) SQL.
	pub fn to_statement_ctx(&self, sql: Option<String>) -> StatementEventCtx {
		StatementEventCtx {
			sql,
			namespace: self.namespace.clone(),
			database: self.database.clone(),
			user: self.user.clone(),
			session_id: self.session_id,
			client_ip: self.client_ip,
		}
	}

	/// Project into a [`QueryEventCtx`].
	pub fn to_query_ctx(&self) -> QueryEventCtx {
		QueryEventCtx {
			namespace: self.namespace.clone(),
			database: self.database.clone(),
			user: self.user.clone(),
			session_id: self.session_id,
			client_ip: self.client_ip,
		}
	}

	/// Project into a [`TransactionEventCtx`].
	pub fn to_transaction_ctx(&self) -> TransactionEventCtx {
		TransactionEventCtx {
			namespace: self.namespace.clone(),
			database: self.database.clone(),
			user: self.user.clone(),
			session_id: self.session_id,
			client_ip: self.client_ip,
		}
	}

	/// Project into a [`RpcEventCtx`].
	pub fn to_rpc_ctx(&self) -> RpcEventCtx {
		RpcEventCtx {
			namespace: self.namespace.clone(),
			database: self.database.clone(),
			user: self.user.clone(),
			session_id: self.session_id,
			client_ip: self.client_ip,
		}
	}

	/// Project into an [`AuthEventCtx`].
	pub fn to_auth_ctx(&self) -> AuthEventCtx {
		AuthEventCtx {
			namespace: self.namespace.clone(),
			database: self.database.clone(),
			user: self.user.clone(),
			session_id: self.session_id,
			client_ip: self.client_ip,
		}
	}
}

/// Contextual portion of a [`StatementEvent`]. MAY contain customer data.
#[derive(Clone, Debug, Default)]
pub struct StatementEventCtx {
	/// Full statement SQL text. Only populated when the active observer opts in
	/// via [`crate::observe::ExecutionObserver::needs_statement_text`]. Must
	/// never be written to unauthenticated sinks.
	pub sql: Option<String>,
	pub namespace: Option<String>,
	pub database: Option<String>,
	pub user: Option<String>,
	pub session_id: Option<Uuid>,
	pub client_ip: Option<IpAddr>,
}

/// Emitted once per top-level statement completion.
#[derive(Clone, Debug)]
pub struct StatementEvent {
	pub safe: StatementEventSafe,
	pub ctx: StatementEventCtx,
}

// --- QueryEvent ---

/// Per-query counters summarising statement outcomes within a query batch.
#[derive(Clone, Copy, Debug, Default)]
pub struct QueryCounters {
	pub total: u32,
	pub ok: u32,
	pub err: u32,
}

/// Safe portion of a [`QueryEvent`].
#[derive(Clone, Copy, Debug)]
pub struct QueryEventSafe {
	pub outcome: Outcome,
	pub duration: Duration,
	pub counters: QueryCounters,
	/// Bounded error classification when `outcome` is [`Outcome::Error`].
	pub error_class: Option<&'static str>,
}

/// Contextual portion of a [`QueryEvent`].
#[derive(Clone, Debug, Default)]
pub struct QueryEventCtx {
	pub namespace: Option<String>,
	pub database: Option<String>,
	pub user: Option<String>,
	pub session_id: Option<Uuid>,
	pub client_ip: Option<IpAddr>,
}

/// Emitted once per executor query batch.
#[derive(Clone, Debug)]
pub struct QueryEvent {
	pub safe: QueryEventSafe,
	pub ctx: QueryEventCtx,
}

// --- TransactionEvent ---

/// A point-in-time snapshot of the counters held by a `Transaction`.
///
/// `key_bytes_*` / `value_bytes_*` are the raw atomics. `total_bytes_*` and
/// `ops_total` are derived in [`TransactionMetrics::snapshot`] from the
/// matching atomic fields, so consumers can read either form without paying
/// for an extra atomic on the hot path.
#[derive(Clone, Copy, Debug, Default)]
pub struct TransactionMetricsSnapshot {
	pub keys_read: u64,
	pub keys_written: u64,
	pub key_bytes_read: u64,
	pub value_bytes_read: u64,
	pub key_bytes_written: u64,
	pub value_bytes_written: u64,
	/// `key_bytes_read + value_bytes_read`, derived in [`TransactionMetrics::snapshot`].
	pub total_bytes_read: u64,
	/// `key_bytes_written + value_bytes_written`, derived in [`TransactionMetrics::snapshot`].
	pub total_bytes_written: u64,
	pub ops_get: u32,
	pub ops_scan: u32,
	pub ops_put: u32,
	pub ops_set: u32,
	pub ops_del: u32,
	/// `ops_get + ops_scan + ops_put + ops_set + ops_del`, derived in
	/// [`TransactionMetrics::snapshot`] (saturating sum).
	pub ops_total: u32,
}

/// Atomic counters for a single transaction.
///
/// Held on the `Transaction` and updated from its KV methods. Operations use
/// [`Ordering::Relaxed`] because the counters never gate other state and are
/// only read in aggregate by [`TransactionMetrics::snapshot`] when the
/// transaction finishes.
#[derive(Debug, Default)]
pub struct TransactionMetrics {
	keys_read: AtomicU64,
	keys_written: AtomicU64,
	key_bytes_read: AtomicU64,
	value_bytes_read: AtomicU64,
	key_bytes_written: AtomicU64,
	value_bytes_written: AtomicU64,
	ops_get: AtomicU32,
	ops_scan: AtomicU32,
	ops_put: AtomicU32,
	ops_set: AtomicU32,
	ops_del: AtomicU32,
}

impl TransactionMetrics {
	/// Create a new empty counter set.
	pub fn new() -> Self {
		Self::default()
	}

	/// Record a point get: one `get`/`exists`/`getm` op returning
	/// `keys_found` key(s) whose encoded keys/values sum to `key_bytes` /
	/// `value_bytes` respectively.
	pub fn record_get(&self, keys_found: u64, key_bytes: u64, value_bytes: u64) {
		self.ops_get.fetch_add(1, Ordering::Relaxed);
		if keys_found > 0 {
			self.keys_read.fetch_add(keys_found, Ordering::Relaxed);
		}
		if key_bytes > 0 {
			self.key_bytes_read.fetch_add(key_bytes, Ordering::Relaxed);
		}
		if value_bytes > 0 {
			self.value_bytes_read.fetch_add(value_bytes, Ordering::Relaxed);
		}
	}

	/// Record a scan/range read: one scan op returning `keys_found` keys
	/// whose encoded keys/values sum to `key_bytes` / `value_bytes`
	/// respectively (`value_bytes` is 0 for keys-only scans).
	pub fn record_scan(&self, keys_found: u64, key_bytes: u64, value_bytes: u64) {
		self.ops_scan.fetch_add(1, Ordering::Relaxed);
		if keys_found > 0 {
			self.keys_read.fetch_add(keys_found, Ordering::Relaxed);
		}
		if key_bytes > 0 {
			self.key_bytes_read.fetch_add(key_bytes, Ordering::Relaxed);
		}
		if value_bytes > 0 {
			self.value_bytes_read.fetch_add(value_bytes, Ordering::Relaxed);
		}
	}

	/// Record a `set` (upsert) of one key with `key_bytes` / `value_bytes`
	/// payload bytes.
	pub fn record_set(&self, key_bytes: u64, value_bytes: u64) {
		self.ops_set.fetch_add(1, Ordering::Relaxed);
		self.keys_written.fetch_add(1, Ordering::Relaxed);
		if key_bytes > 0 {
			self.key_bytes_written.fetch_add(key_bytes, Ordering::Relaxed);
		}
		if value_bytes > 0 {
			self.value_bytes_written.fetch_add(value_bytes, Ordering::Relaxed);
		}
	}

	/// Record a `put` (insert-if-missing) of one key with `key_bytes` /
	/// `value_bytes` payload bytes. Also covers `putc` / `replace`.
	pub fn record_put(&self, key_bytes: u64, value_bytes: u64) {
		self.ops_put.fetch_add(1, Ordering::Relaxed);
		self.keys_written.fetch_add(1, Ordering::Relaxed);
		if key_bytes > 0 {
			self.key_bytes_written.fetch_add(key_bytes, Ordering::Relaxed);
		}
		if value_bytes > 0 {
			self.value_bytes_written.fetch_add(value_bytes, Ordering::Relaxed);
		}
	}

	/// Record one delete op affecting `keys_deleted` keys (0 when the delete
	/// is over a range whose size is not reported back to the caller). The
	/// `key_bytes` argument is summed into `key_bytes_written` because the
	/// tombstone occupies write capacity, even though no value is stored.
	pub fn record_del(&self, keys_deleted: u64, key_bytes: u64) {
		self.ops_del.fetch_add(1, Ordering::Relaxed);
		if keys_deleted > 0 {
			self.keys_written.fetch_add(keys_deleted, Ordering::Relaxed);
		}
		if key_bytes > 0 {
			self.key_bytes_written.fetch_add(key_bytes, Ordering::Relaxed);
		}
	}

	/// Freeze the current counter values into a `Copy` snapshot suitable for
	/// attaching to a [`TransactionEvent`]. Derived totals (`total_bytes_*`,
	/// `ops_total`) are computed here so consumers do not pay extra atomic
	/// updates on the hot path.
	pub fn snapshot(&self) -> TransactionMetricsSnapshot {
		let key_bytes_read = self.key_bytes_read.load(Ordering::Relaxed);
		let value_bytes_read = self.value_bytes_read.load(Ordering::Relaxed);
		let key_bytes_written = self.key_bytes_written.load(Ordering::Relaxed);
		let value_bytes_written = self.value_bytes_written.load(Ordering::Relaxed);
		let ops_get = self.ops_get.load(Ordering::Relaxed);
		let ops_scan = self.ops_scan.load(Ordering::Relaxed);
		let ops_put = self.ops_put.load(Ordering::Relaxed);
		let ops_set = self.ops_set.load(Ordering::Relaxed);
		let ops_del = self.ops_del.load(Ordering::Relaxed);
		TransactionMetricsSnapshot {
			keys_read: self.keys_read.load(Ordering::Relaxed),
			keys_written: self.keys_written.load(Ordering::Relaxed),
			key_bytes_read,
			value_bytes_read,
			key_bytes_written,
			value_bytes_written,
			total_bytes_read: key_bytes_read.saturating_add(value_bytes_read),
			total_bytes_written: key_bytes_written.saturating_add(value_bytes_written),
			ops_get,
			ops_scan,
			ops_put,
			ops_set,
			ops_del,
			ops_total: ops_get
				.saturating_add(ops_scan)
				.saturating_add(ops_put)
				.saturating_add(ops_set)
				.saturating_add(ops_del),
		}
	}
}

/// Safe portion of a [`TransactionEvent`].
#[derive(Clone, Copy, Debug)]
pub struct TransactionEventSafe {
	pub outcome: Outcome,
	pub write: bool,
	pub duration: Duration,
	pub metrics: TransactionMetricsSnapshot,
	/// Bounded error classification when `outcome` is [`Outcome::Error`].
	pub error_class: Option<&'static str>,
}

/// Contextual portion of a [`TransactionEvent`]. Carries optional
/// tenant-identifying context for enterprise audit destinations.
#[derive(Clone, Debug, Default)]
pub struct TransactionEventCtx {
	pub namespace: Option<String>,
	pub database: Option<String>,
	pub user: Option<String>,
	pub session_id: Option<Uuid>,
	pub client_ip: Option<IpAddr>,
}

/// Emitted once per transaction completion (commit or cancel).
#[derive(Clone, Debug)]
pub struct TransactionEvent {
	pub safe: TransactionEventSafe,
	pub ctx: TransactionEventCtx,
}

// --- RpcEvent ---

/// Safe portion of an [`RpcEvent`].
///
/// `method` holds the already-bounded [`crate::rpc::Method`] and is safe to
/// emit as an attribute because all variants are known at compile time.
#[derive(Clone, Copy, Debug)]
pub struct RpcEventSafe {
	pub method: crate::rpc::Method,
	pub outcome: Outcome,
	pub duration: Duration,
	/// Bounded error classification when `outcome` is [`Outcome::Error`].
	pub error_class: Option<&'static str>,
}

/// Contextual portion of an [`RpcEvent`].
#[derive(Clone, Debug, Default)]
pub struct RpcEventCtx {
	pub namespace: Option<String>,
	pub database: Option<String>,
	pub user: Option<String>,
	pub session_id: Option<Uuid>,
	pub client_ip: Option<IpAddr>,
}

/// Emitted once per RPC method invocation.
#[derive(Clone, Debug)]
pub struct RpcEvent {
	pub safe: RpcEventSafe,
	pub ctx: RpcEventCtx,
}

// --- AuthEvent ---

/// Safe portion of an [`AuthEvent`].
#[derive(Clone, Copy, Debug)]
pub struct AuthEventSafe {
	pub action: AuthAction,
	pub scope: AuthScope,
	pub outcome: Outcome,
	/// Bounded error classification when `outcome` is [`Outcome::Error`].
	pub error_class: Option<&'static str>,
}

/// Contextual portion of an [`AuthEvent`].
#[derive(Clone, Debug, Default)]
pub struct AuthEventCtx {
	pub namespace: Option<String>,
	pub database: Option<String>,
	pub user: Option<String>,
	pub session_id: Option<Uuid>,
	pub client_ip: Option<IpAddr>,
}

/// Emitted once per authentication attempt (both successful and failed).
#[derive(Clone, Debug)]
pub struct AuthEvent {
	pub safe: AuthEventSafe,
	pub ctx: AuthEventCtx,
}

// --- SessionEvent ---

/// Safe portion of a [`SessionEvent`].
#[derive(Clone, Copy, Debug)]
pub struct SessionEventSafe {
	pub action: SessionAction,
	pub protocol: SessionProtocol,
	/// Populated on `Disconnect` with the total session lifetime.
	pub duration: Option<Duration>,
}

/// Contextual portion of a [`SessionEvent`].
#[derive(Clone, Debug, Default)]
pub struct SessionEventCtx {
	pub session_id: Option<Uuid>,
	pub service_name: Option<String>,
	pub client_ip: Option<IpAddr>,
	pub namespace: Option<String>,
	pub database: Option<String>,
	pub user: Option<String>,
}

/// Emitted on session connect and disconnect.
#[derive(Clone, Debug)]
pub struct SessionEvent {
	pub safe: SessionEventSafe,
	pub ctx: SessionEventCtx,
}

// --- NetworkBytesEvent ---

/// Safe portion of a [`NetworkBytesEvent`]. `direction` and `protocol` are
/// bounded enums; `bytes` is a non-identifying scalar. Suitable for
/// unauthenticated, unlabelled aggregate counters.
#[derive(Clone, Copy, Debug)]
pub struct NetworkBytesEventSafe {
	pub direction: NetworkDirection,
	pub protocol: SessionProtocol,
	pub bytes: u64,
}

/// Contextual portion of a [`NetworkBytesEvent`]. May contain
/// tenant-identifying fields. MUST NOT be emitted to unauthenticated sinks.
#[derive(Clone, Debug, Default)]
pub struct NetworkBytesEventCtx {
	pub namespace: Option<String>,
	pub database: Option<String>,
	pub user: Option<String>,
}

impl NetworkBytesEventCtx {
	/// Build a ctx from an authenticated [`Session`].
	///
	/// Anonymous sessions leave `user` as `None`. Record-access principals
	/// collapse to a fixed `<record>` sentinel so dimensional metric
	/// cardinality stays bounded by the number of namespaces/databases
	/// rather than by the number of end-customer record ids that have
	/// signed in.
	pub fn from_session(sess: &crate::dbs::Session) -> Self {
		let user = if sess.au.is_anon() {
			None
		} else if sess.au.is_record() {
			Some("<record>".to_owned())
		} else {
			Some(sess.au.id().to_owned())
		};
		Self {
			namespace: sess.ns.clone(),
			database: sess.db.clone(),
			user,
		}
	}
}

/// Emitted whenever the server observes inbound or outbound bytes on a
/// client-facing protocol (HTTP, WebSocket).
///
/// The community observer reads only `safe` and increments a single
/// unlabelled counter per direction. Enterprise dimensional observers read
/// `ctx` and increment a tenant-attributed counter so cross-tenant byte
/// totals can be attributed for chargeback / showback.
#[derive(Clone, Debug)]
pub struct NetworkBytesEvent {
	pub safe: NetworkBytesEventSafe,
	pub ctx: NetworkBytesEventCtx,
}

// --- HttpRequestEvent ---

/// Safe portion of an [`HttpRequestStartEvent`] / [`HttpRequestEvent`].
///
/// Contains only bounded, non-identifying fields suitable for use on the
/// unauthenticated `/metrics` endpoint:
///
/// - `method` is the bounded [`HttpMethod`] enum.
/// - `route` is the matched Axum route template (e.g. `/sql`, `/key/:tb/:id`) or `None` for
///   unmatched paths. Sourced from the statically-declared router so the value set is closed.
/// - `version` is the bounded [`HttpVersion`] enum.
#[derive(Clone, Copy, Debug)]
pub struct HttpRequestStartEventSafe {
	pub method: HttpMethod,
	pub route: Option<&'static str>,
	pub version: HttpVersion,
}

/// Contextual portion of an [`HttpRequestStartEvent`] / [`HttpRequestEvent`].
///
/// May contain tenant-identifying fields. MUST NOT be emitted to
/// unauthenticated sinks; community observers read only the `safe` half.
#[derive(Clone, Debug, Default)]
pub struct HttpRequestEventCtx {
	pub namespace: Option<String>,
	pub database: Option<String>,
	pub user: Option<String>,
	pub session_id: Option<Uuid>,
	pub client_ip: Option<IpAddr>,
}

impl HttpRequestEventCtx {
	/// Build a ctx from an authenticated [`crate::dbs::Session`].
	///
	/// Mirrors [`NetworkBytesEventCtx::from_session`] for the namespace /
	/// database / user fields (record-access principals collapse to the
	/// `<record>` sentinel) and additionally carries the session id and
	/// client IP from the session. The session's `ip` is stored as a
	/// string and may not parse cleanly (proxy headers can include
	/// trailing port specifiers); a parse failure leaves `client_ip` as
	/// `None` rather than corrupting the field.
	pub fn from_session(sess: &crate::dbs::Session) -> Self {
		let user = if sess.au.is_anon() {
			None
		} else if sess.au.is_record() {
			Some("<record>".to_owned())
		} else {
			Some(sess.au.id().to_owned())
		};
		let client_ip = sess.ip.as_deref().and_then(|raw| raw.parse::<IpAddr>().ok());
		Self {
			namespace: sess.ns.clone(),
			database: sess.db.clone(),
			user,
			session_id: sess.id,
			client_ip,
		}
	}

	/// Project this ctx into a [`NetworkBytesEventCtx`] so the same
	/// authenticated session attribution can be reused for byte
	/// counters without re-deriving.
	pub fn to_network_bytes(&self) -> NetworkBytesEventCtx {
		NetworkBytesEventCtx {
			namespace: self.namespace.clone(),
			database: self.database.clone(),
			user: self.user.clone(),
		}
	}
}

/// Emitted at the start of an HTTP request, before the inner service runs.
///
/// Pairs with a matching [`HttpRequestEvent`] dispatched on completion.
/// Active-request gauges live here so they can be incremented up-front and
/// decremented on completion regardless of how the inner stack returns.
#[derive(Clone, Debug)]
pub struct HttpRequestStartEvent {
	pub safe: HttpRequestStartEventSafe,
	pub ctx: HttpRequestEventCtx,
}

/// Safe portion of an [`HttpRequestEvent`]. Bounded, no customer data.
///
/// Status codes are kept as `u16` rather than a bounded enum: HTTP defines
/// 5 hundred-blocks of values and a `u16` already restricts cardinality
/// adequately while keeping the surface honest about what was returned.
/// Operators that don't want per-status-code series should aggregate at the
/// scrape backend.
#[derive(Clone, Copy, Debug)]
pub struct HttpRequestEventSafe {
	pub method: HttpMethod,
	pub route: Option<&'static str>,
	pub status_code: Option<u16>,
	pub version: HttpVersion,
	pub outcome: Outcome,
	pub duration: Duration,
	pub request_size: Option<u64>,
	pub response_size: Option<u64>,
	/// Bounded error classification when `outcome` is [`Outcome::Error`].
	pub error_class: Option<&'static str>,
}

/// Emitted once per HTTP request completion, immediately after the inner
/// stack returns. Carries the full request lifecycle data: method / route /
/// status / version / latency / wire sizes plus the resolved tenant ctx.
#[derive(Clone, Debug)]
pub struct HttpRequestEvent {
	pub safe: HttpRequestEventSafe,
	pub ctx: HttpRequestEventCtx,
}

#[cfg(test)]
mod tests {
	use std::sync::Arc;
	use std::thread;

	use super::*;

	#[test]
	fn transaction_metrics_record_accumulates() {
		let m = TransactionMetrics::new();
		m.record_get(1, 16, 128);
		m.record_get(0, 0, 0);
		m.record_scan(4, 32, 512);
		m.record_set(8, 64);
		m.record_put(4, 32);
		m.record_del(3, 12);

		let s = m.snapshot();
		assert_eq!(s.ops_get, 2);
		assert_eq!(s.ops_scan, 1);
		assert_eq!(s.ops_set, 1);
		assert_eq!(s.ops_put, 1);
		assert_eq!(s.ops_del, 1);
		assert_eq!(s.keys_read, 5);
		assert_eq!(s.key_bytes_read, 48); // 16 + 32
		assert_eq!(s.value_bytes_read, 640); // 128 + 512
		assert_eq!(s.keys_written, 5); // 1 set + 1 put + 3 deleted
		assert_eq!(s.key_bytes_written, 24); // 8 + 4 + 12
		assert_eq!(s.value_bytes_written, 96); // 64 + 32
	}

	#[test]
	fn transaction_metrics_record_ignores_zero_byte_payloads() {
		let m = TransactionMetrics::new();
		m.record_get(0, 0, 0); // missing point read
		m.record_scan(0, 0, 0); // empty scan
		m.record_set(0, 0); // empty key + value write
		m.record_put(0, 0); // empty key + value write
		m.record_del(0, 0); // range delete with unknown size

		let s = m.snapshot();
		assert_eq!(s.ops_get, 1);
		assert_eq!(s.ops_scan, 1);
		assert_eq!(s.ops_set, 1);
		assert_eq!(s.ops_put, 1);
		assert_eq!(s.ops_del, 1);
		assert_eq!(s.keys_read, 0);
		assert_eq!(s.key_bytes_read, 0);
		assert_eq!(s.value_bytes_read, 0);
		// set / put always count the key, even with an empty value.
		assert_eq!(s.keys_written, 2);
		assert_eq!(s.key_bytes_written, 0);
		assert_eq!(s.value_bytes_written, 0);
	}

	#[test]
	fn transaction_metrics_snapshot_derives_totals() {
		// `total_bytes_*` and `ops_total` are derived in `snapshot()` rather
		// than tracked as separate atomics, to keep the hot-path atomic count
		// per call unchanged when key/value byte tracking was split.
		let m = TransactionMetrics::new();
		m.record_get(1, 4, 16);
		m.record_scan(2, 8, 64);
		m.record_set(4, 100);
		m.record_put(2, 50);
		m.record_del(1, 6);

		let s = m.snapshot();
		assert_eq!(s.total_bytes_read, s.key_bytes_read + s.value_bytes_read);
		assert_eq!(s.total_bytes_read, 12 + 80);
		assert_eq!(s.total_bytes_written, s.key_bytes_written + s.value_bytes_written);
		assert_eq!(s.total_bytes_written, (4 + 2 + 6) + (100 + 50));
		assert_eq!(s.ops_total, s.ops_get + s.ops_scan + s.ops_put + s.ops_set + s.ops_del);
		assert_eq!(s.ops_total, 5);
	}

	#[test]
	fn transaction_metrics_are_concurrent_safe() {
		// Counters are `Relaxed` atomics: they MUST accumulate losslessly across
		// threads or we silently under-count in the transaction event.
		let m = std::sync::Arc::new(TransactionMetrics::new());
		let mut handles = Vec::new();
		for _ in 0..8 {
			let m = Arc::clone(&m);
			handles.push(thread::spawn(move || {
				for _ in 0..1_000 {
					m.record_get(1, 1, 1);
					m.record_set(1, 1);
				}
			}));
		}
		for h in handles {
			h.join().unwrap();
		}
		let s = m.snapshot();
		assert_eq!(s.ops_get, 8_000);
		assert_eq!(s.ops_set, 8_000);
		assert_eq!(s.keys_read, 8_000);
		assert_eq!(s.key_bytes_read, 8_000);
		assert_eq!(s.value_bytes_read, 8_000);
		assert_eq!(s.keys_written, 8_000);
		assert_eq!(s.key_bytes_written, 8_000);
		assert_eq!(s.value_bytes_written, 8_000);
	}

	#[test]
	fn transaction_metrics_snapshot_is_non_destructive() {
		let m = TransactionMetrics::new();
		m.record_get(2, 4, 8);
		let a = m.snapshot();
		let b = m.snapshot();
		assert_eq!(a.ops_get, b.ops_get);
		assert_eq!(a.keys_read, b.keys_read);
		assert_eq!(a.key_bytes_read, b.key_bytes_read);
		assert_eq!(a.value_bytes_read, b.value_bytes_read);
		assert_eq!(a.total_bytes_read, b.total_bytes_read);
	}

	#[test]
	fn bounded_enums_have_stable_labels() {
		// The `as_label` helpers are metric-attribute values. Their output
		// must match `[a-z_]+` because we rely on it being Prometheus-safe.
		// Every variant of every bounded enum that lands on a metric label
		// is exercised here so adding a new variant without updating its
		// `as_label` formatting is caught at test time rather than as a
		// label-cardinality footgun in production.
		let mut labels: Vec<&str> = vec![
			Outcome::Success.as_label(),
			Outcome::Error.as_label(),
			Outcome::Cancelled.as_label(),
			SessionProtocol::Http.as_label(),
			SessionProtocol::WebSocket.as_label(),
		];
		labels.extend([
			StatementType::Select.as_label(),
			StatementType::Create.as_label(),
			StatementType::Update.as_label(),
			StatementType::Upsert.as_label(),
			StatementType::Delete.as_label(),
			StatementType::Relate.as_label(),
			StatementType::Insert.as_label(),
			StatementType::Define.as_label(),
			StatementType::Remove.as_label(),
			StatementType::Rebuild.as_label(),
			StatementType::Alter.as_label(),
			StatementType::Info.as_label(),
			StatementType::Live.as_label(),
			StatementType::Kill.as_label(),
			StatementType::Let.as_label(),
			StatementType::Return.as_label(),
			StatementType::Foreach.as_label(),
			StatementType::IfElse.as_label(),
			StatementType::Sleep.as_label(),
			StatementType::Explain.as_label(),
			StatementType::Begin.as_label(),
			StatementType::Commit.as_label(),
			StatementType::Cancel.as_label(),
			StatementType::Access.as_label(),
			StatementType::Use.as_label(),
			StatementType::Option.as_label(),
			StatementType::Show.as_label(),
			StatementType::Break.as_label(),
			StatementType::Continue.as_label(),
			StatementType::Throw.as_label(),
			StatementType::Block.as_label(),
			StatementType::Other.as_label(),
		]);
		labels.extend([NetworkDirection::Sent.as_label(), NetworkDirection::Received.as_label()]);
		labels.extend([
			AuthScope::None.as_label(),
			AuthScope::Root.as_label(),
			AuthScope::Namespace.as_label(),
			AuthScope::Database.as_label(),
			AuthScope::Record.as_label(),
		]);
		labels.extend([
			AuthAction::Signin.as_label(),
			AuthAction::Signup.as_label(),
			AuthAction::Authenticate.as_label(),
			AuthAction::Refresh.as_label(),
			AuthAction::Invalidate.as_label(),
			AuthAction::Revoke.as_label(),
		]);
		labels.extend([SessionAction::Connect.as_label(), SessionAction::Disconnect.as_label()]);
		labels.extend([
			HttpMethod::Get.as_label(),
			HttpMethod::Post.as_label(),
			HttpMethod::Put.as_label(),
			HttpMethod::Patch.as_label(),
			HttpMethod::Delete.as_label(),
			HttpMethod::Head.as_label(),
			HttpMethod::Options.as_label(),
			HttpMethod::Connect.as_label(),
			HttpMethod::Trace.as_label(),
			HttpMethod::Other.as_label(),
		]);
		for l in labels {
			assert!(!l.is_empty());
			assert!(
				l.chars().all(|c| c.is_ascii_lowercase() || c == '_'),
				"`{l}` is not a safe label value",
			);
		}
		// HttpVersion labels are slightly different: they use digits and
		// underscores (e.g. "1_1"). Validate them with a relaxed predicate
		// so they don't fail the lower-case-only check above but still
		// match Prometheus label-value conventions.
		for l in [
			HttpVersion::Http10.as_label(),
			HttpVersion::Http11.as_label(),
			HttpVersion::Http2.as_label(),
			HttpVersion::Http3.as_label(),
			HttpVersion::Other.as_label(),
		] {
			assert!(!l.is_empty());
			assert!(
				l.chars().all(|c| c.is_ascii_alphanumeric() || c == '_'),
				"`{l}` is not a safe label value",
			);
		}
	}

	#[test]
	fn http_method_from_str_classifies_known_methods() {
		assert_eq!(HttpMethod::from_method_str("GET"), HttpMethod::Get);
		assert_eq!(HttpMethod::from_method_str("POST"), HttpMethod::Post);
		assert_eq!(HttpMethod::from_method_str("PUT"), HttpMethod::Put);
		assert_eq!(HttpMethod::from_method_str("PATCH"), HttpMethod::Patch);
		assert_eq!(HttpMethod::from_method_str("DELETE"), HttpMethod::Delete);
		assert_eq!(HttpMethod::from_method_str("HEAD"), HttpMethod::Head);
		assert_eq!(HttpMethod::from_method_str("OPTIONS"), HttpMethod::Options);
		assert_eq!(HttpMethod::from_method_str("CONNECT"), HttpMethod::Connect);
		assert_eq!(HttpMethod::from_method_str("TRACE"), HttpMethod::Trace);
		// Anything non-standard (or wrong casing) collapses to Other.
		assert_eq!(HttpMethod::from_method_str("BREW"), HttpMethod::Other);
		assert_eq!(HttpMethod::from_method_str("get"), HttpMethod::Other);
	}

	#[test]
	fn http_version_from_str_classifies_wire_form() {
		assert_eq!(HttpVersion::from_version_str("HTTP/1.0"), HttpVersion::Http10);
		assert_eq!(HttpVersion::from_version_str("HTTP/1.1"), HttpVersion::Http11);
		assert_eq!(HttpVersion::from_version_str("HTTP/2.0"), HttpVersion::Http2);
		assert_eq!(HttpVersion::from_version_str("HTTP/3.0"), HttpVersion::Http3);
		// Bare-numeric form is also accepted because the hyper Debug impl
		// renders it that way for some HTTP/2 / HTTP/3 connections.
		assert_eq!(HttpVersion::from_version_str("2"), HttpVersion::Http2);
		assert_eq!(HttpVersion::from_version_str("HTTP/0.9"), HttpVersion::Other);
	}

	#[test]
	fn outcome_from_result_maps_success_and_error() {
		let ok: Result<u32, &str> = Ok(7);
		let err: Result<u32, &str> = Err("boom");
		assert_eq!(Outcome::from(&ok), Outcome::Success);
		assert_eq!(Outcome::from(&err), Outcome::Error);
	}

	mod network_ctx_from_session {
		use std::sync::Arc;

		use super::super::NetworkBytesEventCtx;
		use crate::dbs::Session;
		use crate::iam::{Auth, Role};

		#[test]
		fn root_user_populates_user() {
			let sess = Session {
				au: Arc::new(Auth::for_root(Role::Owner)),
				..Session::default()
			}
			.with_ns("acme")
			.with_db("prod");
			let ctx = NetworkBytesEventCtx::from_session(&sess);
			assert_eq!(ctx.namespace.as_deref(), Some("acme"));
			assert_eq!(ctx.database.as_deref(), Some("prod"));
			// `Auth::for_root` sets the actor id to `system_auth`.
			assert_eq!(ctx.user.as_deref(), Some("system_auth"));
		}

		#[test]
		fn record_principal_collapses_to_sentinel() {
			// Use a record-shaped principal id; the helper MUST NOT
			// surface it verbatim because record ids are unbounded.
			let sess = Session {
				au: Arc::new(Auth::for_record("user:abc123".to_owned(), "acme", "prod", "web")),
				..Session::default()
			};
			let ctx = NetworkBytesEventCtx::from_session(&sess);
			assert_eq!(ctx.user.as_deref(), Some("<record>"));
			assert!(
				!ctx.user.as_ref().unwrap().contains("abc123"),
				"record id leaked into ctx.user: {:?}",
				ctx.user
			);
		}

		#[test]
		fn anonymous_session_leaves_user_none() {
			let sess = Session::default();
			let ctx = NetworkBytesEventCtx::from_session(&sess);
			assert!(ctx.user.is_none());
		}

		#[test]
		fn ns_db_carry_through_verbatim() {
			let sess = Session::default().with_ns("a-ns").with_db("a-db");
			let ctx = NetworkBytesEventCtx::from_session(&sess);
			assert_eq!(ctx.namespace.as_deref(), Some("a-ns"));
			assert_eq!(ctx.database.as_deref(), Some("a-db"));
		}
	}

	mod tenant_identity_projections {
		//! Coverage for the `TenantIdentity::to_{rpc,auth}_ctx`
		//! projections used by the RPC dispatch site.
		//!
		//! These projections preserve the
		//! [`super::super::TenantIdentity::from_session`] collapsing
		//! rule (anonymous -> `None`, record-access -> `<record>`
		//! sentinel, else -> actor id) so per-tenant dimensional
		//! dashboards and audit destinations never receive raw
		//! record-access principal ids and never see synthetic
		//! empty-string users for unauthenticated traffic.

		use std::sync::Arc;

		use super::super::TenantIdentity;
		use crate::dbs::Session;
		use crate::iam::{Auth, Role};

		#[test]
		fn rpc_ctx_collapses_record_principal() {
			let sess = Session {
				au: Arc::new(Auth::for_record("user:abc123".to_owned(), "acme", "prod", "web")),
				..Session::default()
			};
			let identity = TenantIdentity::from_session(&sess);
			let rpc = identity.to_rpc_ctx();
			assert_eq!(rpc.user.as_deref(), Some("<record>"));
			assert!(
				!rpc.user.as_ref().unwrap().contains("abc123"),
				"record id leaked into rpc ctx.user: {:?}",
				rpc.user
			);
		}

		#[test]
		fn auth_ctx_collapses_record_principal() {
			let sess = Session {
				au: Arc::new(Auth::for_record("user:abc123".to_owned(), "acme", "prod", "web")),
				..Session::default()
			};
			let identity = TenantIdentity::from_session(&sess);
			let auth = identity.to_auth_ctx();
			assert_eq!(auth.user.as_deref(), Some("<record>"));
			assert!(
				!auth.user.as_ref().unwrap().contains("abc123"),
				"record id leaked into auth ctx.user: {:?}",
				auth.user
			);
		}

		#[test]
		fn projections_drop_user_for_anonymous() {
			let sess = Session::default();
			let identity = TenantIdentity::from_session(&sess);
			let rpc = identity.to_rpc_ctx();
			let auth = identity.to_auth_ctx();
			assert!(rpc.user.is_none(), "anonymous session must not surface a user label");
			assert!(auth.user.is_none(), "anonymous session must not surface a user label");
		}

		#[test]
		fn projections_carry_root_user_id() {
			let sess = Session {
				au: Arc::new(Auth::for_root(Role::Owner)),
				..Session::default()
			}
			.with_ns("acme")
			.with_db("prod");
			let identity = TenantIdentity::from_session(&sess);
			let rpc = identity.to_rpc_ctx();
			let auth = identity.to_auth_ctx();
			assert_eq!(rpc.namespace.as_deref(), Some("acme"));
			assert_eq!(rpc.database.as_deref(), Some("prod"));
			assert_eq!(rpc.user.as_deref(), Some("system_auth"));
			assert_eq!(auth.user.as_deref(), Some("system_auth"));
		}
	}
}