MCPcopy Create free account
hub / github.com/PostHog/duckgres / TestQueryLoggerFlushBatchPersistsOrgID

Function TestQueryLoggerFlushBatchPersistsOrgID

server/querylog_test.go:449–539  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

447type captureQueryLogExecutor struct {
448 feedbackExecutor
449 entries []QueryLogEntry
450}
451
452func (e *captureQueryLogExecutor) Log(entry QueryLogEntry) {
453 e.entries = append(e.entries, entry)
454}
455
456func TestLogQueryPrefersExecutorQueryLogSink(t *testing.T) {
457 c, ql, cleanup := newFeedbackClientConn(t)
458 defer cleanup()
459 exec := &captureQueryLogExecutor{}
460 c.executor = exec
461 c.server.cfg.QueryLog.Enabled = true
462
463 c.logQuery(time.Unix(1700000000, 0).UTC(), "SELECT 1", "", "SELECT", 1, 0, "", "", "simple")
464
465 if len(exec.entries) != 1 {
466 t.Fatalf("expected executor query-log sink to receive one entry, got %d", len(exec.entries))
467 }
468 if exec.entries[0].Query != "SELECT 1" {
469 t.Fatalf("unexpected forwarded query-log entry: %#v", exec.entries[0])
470 }
471 select {
472 case entry := <-ql.ch:
473 t.Fatalf("server query logger should not receive entry when executor sink is available: %#v", entry)
474 default:
475 }
476}
477
478// TestLogQueryStampsScopedQueryID pins the correlation contract: every event
479// logged for one statement carries that statement's ID, and a new statement
480// gets a new one. Reusing the previous statement's ID would silently merge two
481// statements in the query log.
482func TestLogQueryStampsScopedQueryID(t *testing.T) {
483 c, _, cleanup := newFeedbackClientConn(t)
484 defer cleanup()
485 exec := &captureQueryLogExecutor{}
486 c.executor = exec
487 c.server.cfg.QueryLog.Enabled = true
488
489 first := c.beginQueryMetrics(time.Unix(1700000000, 0).UTC())
490 // Two events for one statement — e.g. a per-statement log inside a batch
491 // followed by the statement's terminal event.
492 c.logQuery(time.Unix(1700000000, 0).UTC(), "SELECT 1", "", "SELECT", 1, 0, "", "", "simple")
493 c.logQuery(time.Unix(1700000000, 0).UTC(), "SELECT 1", "", "SELECT", 1, 0, "", "", "simple")
494 c.finishQueryMetrics(first)
495
496 second := c.beginQueryMetrics(time.Unix(1700000001, 0).UTC())
497 c.logQuery(time.Unix(1700000001, 0).UTC(), "SELECT 2", "", "SELECT", 1, 0, "", "", "simple")
498 c.finishQueryMetrics(second)
499
500 if len(exec.entries) != 3 {
501 t.Fatalf("expected 3 entries, got %d", len(exec.entries))
502 }
503 for i, entry := range exec.entries {
504 if entry.QueryID == "" {
505 t.Fatalf("entry %d has no query_id", i)
506 }

Callers

nothing calls this directly

Calls 5

flushBatchMethod · 0.95
CloseMethod · 0.65
ExecMethod · 0.65
ScanMethod · 0.65
OpenMethod · 0.45

Tested by

no test coverage detected