Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
Seanaye/feat/slow query telemetry (#6717)
Sean Aye4b7da9f12e98
diff --git a/Cargo.lock b/Cargo.lockindex 92b59a0fc5..04426045ec 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -3118,7 +3118,7 @@ dependencies = [[[package]]name = "cache-wasm"-version = "0.6.17"+version = "0.6.18"dependencies = ["async-lock","cache-core",diff --git a/apps/web/src/lib/graphql-cache/telemetry-otel.test.ts b/apps/web/src/lib/graphql-cache/telemetry-otel.test.tsindex de17d075ef..7896118257 100644--- a/apps/web/src/lib/graphql-cache/telemetry-otel.test.ts+++ b/apps/web/src/lib/graphql-cache/telemetry-otel.test.ts@@ -1,10 +1,15 @@import { beforeEach, describe, expect, it, vi } from 'vitest';-const span = vi.hoisted(() => ({ setAttr: vi.fn(), end: vi.fn() }));+const span = vi.hoisted(() => ({+ setAttr: vi.fn(),+ end: vi.fn(),+ run: vi.fn((callback: () => void) => callback()),+}));const anonymousSpan = vi.hoisted(() => vi.fn(() => span));+const warn = vi.hoisted(() => vi.fn());vi.mock('@macro-inc/observability', () => ({- Telemetry: { anonymousSpan },+ Telemetry: { anonymousSpan, warn },}));import { createOtelCacheTelemetrySink } from './telemetry-otel';@@ -55,6 +60,53 @@ describe('cache OpenTelemetry sink', () => {expect(span.end).toHaveBeenCalledOnce();});+ it('exports a slow execution as a correlated warning without SQL or parameters', () => {+ createOtelCacheTelemetrySink().emit({+ browserFamily: 'firefox',+ browserVersion: '151',+ appVersion: '0.1',+ backend: 'turso-wasm-opfs',+ rolloutCohort: 'unknown',+ name: 'graphql_cache.slow_query',+ operationCategory: 'storage',+ queryFingerprint: 'a430d84680aabd0b',+ durationMs: 201,+ outcome: 'success',+ });++ expect(anonymousSpan).toHaveBeenCalledWith('graphql_cache.slow_query');+ expect(span.run).toHaveBeenCalledOnce();+ expect(warn).toHaveBeenCalledExactlyOnceWith('graphql_cache.slow_query', {+ 'db.system.name': 'sqlite',+ 'db.query.fingerprint': 'a430d84680aabd0b',+ 'cache.backend': 'turso-wasm-opfs',+ 'cache.duration_ms': 201,+ 'cache.outcome': 'success',+ 'cache.slow_query_threshold_ms': 200,+ });+ expect(span.end).toHaveBeenCalledOnce();+ });++ it('ends the slow-query span even if logging throws', () => {+ warn.mockImplementationOnce(() => {+ throw new Error('logger unavailable');+ });+ expect(() =>+ createOtelCacheTelemetrySink().emit({+ browserFamily: 'other',+ browserVersion: 'unknown',+ appVersion: 'unknown',+ backend: 'turso-wasm-opfs',+ rolloutCohort: 'unknown',+ name: 'graphql_cache.slow_query',+ operationCategory: 'storage',+ durationMs: 201,+ outcome: 'error',+ })+ ).not.toThrow();+ expect(span.end).toHaveBeenCalledOnce();+ });+it('isolates Telemetry failures', () => {anonymousSpan.mockImplementationOnce(() => {throw new Error('provider unavailable');diff --git a/apps/web/src/lib/graphql-cache/telemetry-otel.ts b/apps/web/src/lib/graphql-cache/telemetry-otel.tsindex 996ccd440f..985321cb17 100644--- a/apps/web/src/lib/graphql-cache/telemetry-otel.ts+++ b/apps/web/src/lib/graphql-cache/telemetry-otel.ts@@ -76,7 +76,27 @@ export function createOtelCacheTelemetrySink(): CacheTelemetrySink {if (event.oldestAgeMs !== undefined) {span.setAttr('cache.oldest_age_ms', event.oldestAgeMs);}- span.end();+ try {+ if (event.name === 'graphql_cache.slow_query') {+ const attributes = {+ 'db.system.name': 'sqlite',+ 'db.query.fingerprint': event.queryFingerprint,+ 'cache.backend': event.backend,+ 'cache.duration_ms': event.durationMs,+ 'cache.outcome': event.outcome,+ 'cache.slow_query_threshold_ms': 200,+ };+ for (const [key, value] of Object.entries(attributes)) {+ if (value !== undefined) span.setAttr(key, value);+ }+ // Preserve the cache privacy contract on the log as well as the span.+ span.run(() =>+ Telemetry.warn('graphql_cache.slow_query', attributes)+ );+ }+ } finally {+ span.end();+ }} catch {// OTel availability and exporter failures never affect cache behavior.}diff --git a/apps/web/src/lib/graphql-cache/telemetry.test.ts b/apps/web/src/lib/graphql-cache/telemetry.test.tsindex b978b7e637..3c554b8a8b 100644--- a/apps/web/src/lib/graphql-cache/telemetry.test.ts+++ b/apps/web/src/lib/graphql-cache/telemetry.test.ts@@ -168,6 +168,51 @@ describe('cache telemetry privacy contract', () => {});describe('cache telemetry sampling and failure isolation', () => {+ it('never samples or aggregates slow SQL executions and strips query payloads', () => {+ const events: CacheTelemetryEnvelope[] = [];+ const recorder = new CacheTelemetryRecorder(+ new CacheTelemetryReporter(browserCacheTelemetryContext('treatment'), {+ emit: (event) => events.push(event),+ })+ );+ for (let i = 0; i < 3; i++) {+ recorder.record({+ name: 'graphql_cache.slow_query',+ operationCategory: 'storage',+ durationMs: 201,+ queryFingerprint: 'a430d84680aabd0b',+ outcome: i === 0 ? 'error' : 'success',+ sql: 'SELECT private-data',+ parameters: ['secret'],+ result: ['secret'],+ } as unknown as CacheTelemetryObservation);+ }+ recorder.flush();+ expect(events).toHaveLength(3);+ for (const event of events) {+ expect(event).toMatchObject({+ name: 'graphql_cache.slow_query',+ durationMs: 201,+ queryFingerprint: 'a430d84680aabd0b',+ });+ expect(+ isCacheTelemetryObservation({+ name: event.name,+ operationCategory: event.operationCategory,+ queryFingerprint: event.queryFingerprint,+ durationMs: event.durationMs,+ })+ ).toBe(true);+ }+ expect(JSON.stringify(events)).not.toMatch(/private-data|secret/);+ recorder.record({+ name: 'graphql_cache.slow_query',+ operationCategory: 'storage',+ queryFingerprint: 'SELECT private-data',+ });+ expect(events[3]).not.toHaveProperty('queryFingerprint');+ });+it('uses weighted aggregates, independent event sampling, and unsampled errors', () => {const observations: CacheTelemetryObservation[] = [];const recorder = new CacheTelemetryRecorder(diff --git a/apps/web/src/lib/graphql-cache/telemetry.ts b/apps/web/src/lib/graphql-cache/telemetry.tsindex 952021ce1f..f6cf2f21dc 100644--- a/apps/web/src/lib/graphql-cache/telemetry.ts+++ b/apps/web/src/lib/graphql-cache/telemetry.ts@@ -14,6 +14,7 @@ export const CACHE_TELEMETRY_EVENT_NAMES = ['graphql_cache.engine_request','graphql_cache.transaction','graphql_cache.read',+ 'graphql_cache.slow_query','graphql_cache.owner','graphql_cache.stale_drop','graphql_cache.lock_wait',@@ -158,6 +159,8 @@ export type CacheTelemetryObservation = {revisionCategory?: CacheRevisionCategory;resetAttempt?: 'wipe-before-open';durationMs?: number;+ /** FNV-1a of the unexpanded SQL template; never SQL text or bound values. */+ queryFingerprint?: string;bytes?: number;highWaterBytes?: number;usageBytes?: number;@@ -268,6 +271,11 @@ function sanitizeObservation(...(boundedNumber(input.durationMs, 3_600_000) !== undefined? { durationMs: boundedNumber(input.durationMs, 3_600_000) }: {}),+ ...(input.name === 'graphql_cache.slow_query' &&+ typeof input.queryFingerprint === 'string' &&+ /^[0-9a-f]{16}$/.test(input.queryFingerprint)+ ? { queryFingerprint: input.queryFingerprint }+ : {}),...(boundedNumber(input.bytes) !== undefined? { bytes: boundedNumber(input.bytes) }: {}),@@ -633,6 +641,7 @@ export function isCacheTelemetryObservation('revisionCategory','resetAttempt','durationMs',+ 'queryFingerprint','bytes','highWaterBytes','usageBytes',diff --git a/apps/web/src/lib/graphql-cache/worker/wasm-module.ts b/apps/web/src/lib/graphql-cache/worker/wasm-module.tsindex 01357e7286..5e3cf3a2e1 100644--- a/apps/web/src/lib/graphql-cache/worker/wasm-module.ts+++ b/apps/web/src/lib/graphql-cache/worker/wasm-module.ts@@ -191,6 +191,14 @@ export interface CacheWasmModule {hotCapacity?: number): Promise<CacheOpenResult>;destroyCache(scope: string): Promise<void>;+ /** Optional while older cached WASM artifacts are still in circulation. */+ setSlowQueryCallback?(+ callback: (+ fingerprint: string,+ durationMs: number,+ success: boolean+ ) => void+ ): void;schemaHash(): string;/** Read-only binary metadata; optional so fixtures can diagnose stale artifacts. */cacheBuildInfo?(): unknown;@@ -279,6 +287,15 @@ export function loadCacheWasm(): Promise<CacheWasmModule> {throw new Error('cache WASM did not export its linear memory');}wasmMemory = exports.memory;+ mod.setSlowQueryCallback?.((queryFingerprint, durationMs, success) => {+ telemetry.record({+ name: 'graphql_cache.slow_query',+ operationCategory: 'storage',+ queryFingerprint,+ durationMs,+ outcome: success ? 'success' : 'error',+ });+ });telemetry.record({name: 'graphql_cache.wasm_instantiate',operationCategory: 'initialization',diff --git a/crates/client/README.md b/crates/client/README.mdindex b2e450a34f..16088b7d1b 100644--- a/crates/client/README.md+++ b/crates/client/README.md@@ -75,6 +75,26 @@ CHROMEDRIVER="$chromedriver" \WASM_BINDGEN_TEST_ONLY_WEB=1 cargo test --target wasm32-unknown-unknown -p turso-opfs --lib```+## Slow SQL telemetry++The browser WASM driver reports each SQL statement execution **over 200 ms** as+`graphql_cache.slow_query` through the existing page telemetry relay and OTLP+pipeline to Datadog. These events bypass cache sampling/aggregation. Exporting+still requires the existing browser telemetry enablement and exporter config.++Search Datadog logs for `service:web-app "graphql_cache.slow_query"`:++- `cache.duration_ms`: binding, stepping/OPFS I/O, row collection, and cleanup;+ excludes statement preparation and time queued in the worker.+- `db.query.fingerprint`: 16-digit lowercase FNV-1a hash of the exact unexpanded+ SQL template, allowing repeated executions to be grouped without exporting SQL.+- `cache.outcome`: `success` or `error`; slow failures are reported too.+- `cache.slow_query_threshold_ms`: `200`.++Parameters, results, raw SQL, and user identity are not exported. Logging uses+an anonymous span context. A throwing JS telemetry callback cannot change query+results. Native hosts are unchanged unless they install their own telemetry sink.+## Key policy**Presence-of-id convention**: an output object type with an `id: ID!`diff --git a/crates/client/cache-turso/src/driver.rs b/crates/client/cache-turso/src/driver.rsindex b89041428d..0dffc0346c 100644--- a/crates/client/cache-turso/src/driver.rs+++ b/crates/client/cache-turso/src/driver.rs@@ -88,6 +88,9 @@ pub(crate) fn query_prepared(}fn run_prepared(statement: &mut Statement, values: Values) -> Result<(Rows, i64), RunFailure> {+ // Time binding, stepping/I/O, row collection, and cleanup, not worker queue+ // wait or statement preparation. Reused statements are timed per execution.+ let timer = crate::query_telemetry::QueryTimer::start();let execution = (|| {if statement.parameters_count() != values.len() {return Err(TursoStorageError::Database);@@ -144,11 +147,15 @@ fn run_prepared(statement: &mut Statement, values: Values) -> Result<(Rows, i64)// surrounding transaction unusable, regardless of the underlying class.let reset = checked_reset(statement);statement.clear_bindings();- match (execution, reset) {+ let result = match (execution, reset) {(Ok(value), Ok(())) => Ok(value),(Err(error), Ok(())) => Err(RunFailure::execution(error)),(execution, Err(_)) => Err(RunFailure::cleanup(execution.is_ok())),+ };+ if let Some(timer) = timer {+ timer.finish(statement.get_sql(), result.is_ok());}+ result}fn checked_reset(statement: &mut Statement) -> Result<(), TursoStorageError> {diff --git a/crates/client/cache-turso/src/lib.rs b/crates/client/cache-turso/src/lib.rsindex 1b1ee01e75..4bb4b3f4cf 100644--- a/crates/client/cache-turso/src/lib.rs+++ b/crates/client/cache-turso/src/lib.rs@@ -12,6 +12,7 @@mod driver;mod error;mod key;+pub mod query_telemetry;mod storage;pub use error::{PhysicalResetReason, TursoStorageError};diff --git a/crates/client/cache-turso/src/query_telemetry.rs b/crates/client/cache-turso/src/query_telemetry.rsnew file mode 100644index 0000000000..155eb02d41--- /dev/null+++ b/crates/client/cache-turso/src/query_telemetry.rs@@ -0,0 +1,63 @@+//! Optional, thread-local instrumentation for the synchronous SQL driver.+//!+//! The browser composition root supplies a monotonic clock and an exception-safe+//! sink. Neither SQL text, bound values, nor rows cross this boundary.++use std::{cell::RefCell, rc::Rc};++/// Queries must strictly exceed this execution time to be reported.+pub const SLOW_QUERY_THRESHOLD_MS: f64 = 200.0;++/// Host-provided instrumentation for the current cache engine thread.+pub trait QueryTelemetry {+ /// Monotonic milliseconds. Return NaN when the clock is unavailable.+ fn now_ms(&self) -> f64;+ /// Report a slow execution. Implementations must not panic or throw.+ fn slow_query(&self, fingerprint: &str, duration_ms: f64, success: bool);+}++thread_local! {+ static TELEMETRY: RefCell<Option<Rc<dyn QueryTelemetry>>> = const { RefCell::new(None) };+}++/// Install (or disable) instrumentation on the current cache engine thread.+/// Native hosts remain uninstrumented unless they explicitly install a sink.+pub fn set_query_telemetry(telemetry: Option<Rc<dyn QueryTelemetry>>) {+ TELEMETRY.with(|slot| *slot.borrow_mut() = telemetry);+}++pub(crate) struct QueryTimer {+ telemetry: Rc<dyn QueryTelemetry>,+ started_at: f64,+}++impl QueryTimer {+ pub(crate) fn start() -> Option<Self> {+ let telemetry = TELEMETRY.with(|slot| slot.borrow().clone())?;+ let started_at = telemetry.now_ms();+ Some(Self {+ telemetry,+ started_at,+ })+ }++ pub(crate) fn finish(self, sql: &str, success: bool) {+ let duration_ms = self.telemetry.now_ms() - self.started_at;+ if duration_ms.is_finite() && duration_ms > SLOW_QUERY_THRESHOLD_MS {+ self.telemetry+ .slow_query(&fingerprint(sql), duration_ms, success);+ }+ }+}++// Stable FNV-1a of the unexpanded statement template, never of bound values.+// Hash only slow queries, keeping the hot path to two clock reads.+fn fingerprint(sql: &str) -> String {+ let hash = sql.bytes().fold(0xcbf29ce484222325_u64, |hash, byte| {+ (hash ^ u64::from(byte)).wrapping_mul(0x100000001b3)+ });+ format!("{hash:016x}")+}++#[cfg(test)]+mod test;diff --git a/crates/client/cache-turso/src/query_telemetry/test.rs b/crates/client/cache-turso/src/query_telemetry/test.rsnew file mode 100644index 0000000000..b09fd6aeef--- /dev/null+++ b/crates/client/cache-turso/src/query_telemetry/test.rs@@ -0,0 +1,101 @@+use super::*;+use std::cell::Cell;++#[derive(Default)]+struct TestTelemetry {+ now: Cell<f64>,+ events: RefCell<Vec<(String, f64, bool)>>,+}++impl QueryTelemetry for TestTelemetry {+ fn now_ms(&self) -> f64 {+ self.now.get()+ }++ fn slow_query(&self, fingerprint: &str, duration_ms: f64, success: bool) {+ self.events+ .borrow_mut()+ .push((fingerprint.to_owned(), duration_ms, success));+ }+}++struct ResetTelemetry;++impl Drop for ResetTelemetry {+ fn drop(&mut self) {+ set_query_telemetry(None);+ }+}++#[test]+fn reports_only_executions_strictly_over_200_ms_with_finite_clocks() {+ let telemetry = Rc::new(TestTelemetry::default());+ set_query_telemetry(Some(telemetry.clone()));+ let _reset = ResetTelemetry;+ for duration in [0.0, 199.0, 200.0, 200.1, -1.0, f64::NAN, f64::INFINITY] {+ telemetry.now.set(10.0);+ let timer = QueryTimer::start().unwrap();+ telemetry.now.set(10.0 + duration);+ timer.finish("SELECT ?", true);+ }+ assert_eq!(+ telemetry.events.borrow().as_slice(),+ [(fingerprint("SELECT ?"), 200.1, true)]+ );+}++#[test]+fn fingerprint_is_stable_and_does_not_export_sql() {+ // Known FNV-1a vector, also fixes the representation across Rust versions.+ assert_eq!(fingerprint("hello"), "a430d84680aabd0b");+ assert_eq!(fingerprint("SELECT ?").len(), 16);+ assert_ne!(fingerprint("SELECT ?"), fingerprint("DELETE FROM records"));+}++#[cfg(not(target_arch = "wasm32"))]+#[test]+fn driver_times_reused_statements_and_failures_without_bound_values() {+ use std::sync::Arc;+ use turso_core::{Database, MemoryIO, OpenOptions, SqliteDialect, Value};++ struct AdvancingTelemetry(TestTelemetry);+ impl QueryTelemetry for AdvancingTelemetry {+ fn now_ms(&self) -> f64 {+ let now = self.0.now.get();+ self.0.now.set(now + 201.0);+ now+ }+ fn slow_query(&self, fingerprint: &str, duration_ms: f64, success: bool) {+ self.0.slow_query(fingerprint, duration_ms, success);+ }+ }++ let database = Database::open(+ Arc::new(MemoryIO::new()),+ "query-telemetry.db",+ OpenOptions::new(Arc::new(SqliteDialect)),+ )+ .unwrap();+ let connection = database.connect().unwrap();+ let mut statement = crate::driver::prepare(&connection, "SELECT ?").unwrap();+ let telemetry = Rc::new(AdvancingTelemetry(TestTelemetry::default()));+ set_query_telemetry(Some(telemetry.clone()));+ let _reset = ResetTelemetry;++ for value in ["private-first", "private-second"] {+ let rows = crate::driver::query_prepared(&mut statement, vec![Value::from_text(value)]);+ assert!(rows.is_ok());+ }+ assert!(crate::driver::query_prepared(&mut statement, vec![]).is_err());+ assert_eq!(+ telemetry.0.events.borrow().as_slice(),+ [+ (fingerprint("SELECT ?"), 201.0, true),+ (fingerprint("SELECT ?"), 201.0, true),+ (fingerprint("SELECT ?"), 201.0, false),+ ]+ );++ set_query_telemetry(None);+ assert!(QueryTimer::start().is_none());+}diff --git a/crates/client/cache-wasm/Cargo.toml b/crates/client/cache-wasm/Cargo.tomlindex fa0acf0459..0359d83a13 100644--- a/crates/client/cache-wasm/Cargo.toml+++ b/crates/client/cache-wasm/Cargo.toml@@ -2,8 +2,8 @@edition = "2024"name = "cache-wasm"publish = false-# Rebuild for indexed predicate sort-fact lookups on large cached mailboxes.-version = "0.6.17"+# Rebuild for the browser's slow SQL execution telemetry callback.+version = "0.6.18"[features]default = []diff --git a/crates/client/cache-wasm/src/lib.rs b/crates/client/cache-wasm/src/lib.rsindex d3b9f98587..c47224adf6 100644--- a/crates/client/cache-wasm/src/lib.rs+++ b/crates/client/cache-wasm/src/lib.rs@@ -15,8 +15,13 @@//! engine without collisions; they're interned to the engine's `u64` ids//! internally.+#[cfg(target_arch = "wasm32")]+mod query_telemetry;#[cfg(target_arch = "wasm32")]mod shell;+#[cfg(target_arch = "wasm32")]+pub use query_telemetry::set_slow_query_callback;+#[cfg(target_arch = "wasm32")]pub use shell::*;diff --git a/crates/client/cache-wasm/src/query_telemetry.rs b/crates/client/cache-wasm/src/query_telemetry.rsnew file mode 100644index 0000000000..3d769802c7--- /dev/null+++ b/crates/client/cache-wasm/src/query_telemetry.rs@@ -0,0 +1,36 @@+use std::rc::Rc;++use cache_turso::query_telemetry::{QueryTelemetry, set_query_telemetry};+use js_sys::Function;+use wasm_bindgen::prelude::*;++#[wasm_bindgen]+extern "C" {+ #[wasm_bindgen(catch, js_namespace = performance, js_name = now)]+ fn performance_now() -> Result<f64, JsValue>;+}++struct BrowserQueryTelemetry(Function);++impl QueryTelemetry for BrowserQueryTelemetry {+ fn now_ms(&self) -> f64 {+ performance_now().unwrap_or(f64::NAN)+ }++ fn slow_query(&self, fingerprint: &str, duration_ms: f64, success: bool) {+ // A broken/throwing JS telemetry sink must never change SQL results.+ let _ = self.0.call3(+ &JsValue::UNDEFINED,+ &JsValue::from_str(fingerprint),+ &JsValue::from_f64(duration_ms),+ &JsValue::from_bool(success),+ );+ }+}++/// Install the worker's payload-free slow SQL callback before opening the cache.+/// Called only for statement executions strictly longer than 200 milliseconds.+#[wasm_bindgen(js_name = setSlowQueryCallback)]+pub fn set_slow_query_callback(callback: Function) {+ set_query_telemetry(Some(Rc::new(BrowserQueryTelemetry(callback))));+}diff --git a/packages/observability/src/logging.test.ts b/packages/observability/src/logging.test.tsnew file mode 100644index 0000000000..931635c8e3--- /dev/null+++ b/packages/observability/src/logging.test.ts@@ -0,0 +1,51 @@+// @vitest-environment jsdom+import { context, ROOT_CONTEXT } from '@opentelemetry/api';+import { logs } from '@opentelemetry/api-logs';+import { resourceFromAttributes } from '@opentelemetry/resources';+import type { InMemoryLogRecordExporter } from '@opentelemetry/sdk-logs';+import { afterEach, expect, test, vi } from 'vitest';+import { Logging } from './logging';+import { suppressUserId } from './privacy';+import { ZoneContextManager } from './zone';++const exporters: InMemoryLogRecordExporter[] = [];+vi.mock('@opentelemetry/exporter-logs-otlp-http', async () => {+ const { InMemoryLogRecordExporter } = await import('@opentelemetry/sdk-logs');+ return {+ OTLPLogExporter: class extends InMemoryLogRecordExporter {+ constructor() {+ super();+ exporters.push(this);+ }+ },+ };+});++afterEach(() => {+ logs.disable();+ context.disable();+});++test('the production log provider suppresses identity only in anonymous contexts', async () => {+ context.setGlobalContextManager(new ZoneContextManager().enable());+ const logging = new Logging();+ logging.init(+ {+ serviceName: 'test',+ environment: 'test',+ enabled: async () => true,+ logsUrl: 'http://localhost/v1/logs',+ },+ resourceFromAttributes({}),+ () => 'private-user'+ );++ logging.warn('regular');+ context.with(suppressUserId(ROOT_CONTEXT), () => logging.warn('anonymous'));+ await logging.flush();++ const records = exporters[0].getFinishedLogRecords();+ expect(records[0].attributes['usr.id']).toBe('private-user');+ expect(records[1].attributes).not.toHaveProperty('usr.id');+ await logging.shutdown();+});diff --git a/packages/observability/src/logging.ts b/packages/observability/src/logging.tsindex 969b581133..300288f858 100644--- a/packages/observability/src/logging.ts+++ b/packages/observability/src/logging.ts@@ -1,115 +1,117 @@-import type { Attributes as OtelAttributes } from "@opentelemetry/api";-import { type Logger, logs, SeverityNumber } from "@opentelemetry/api-logs";-import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";-import type { Resource } from "@opentelemetry/resources";+import type { Attributes as OtelAttributes } from '@opentelemetry/api';+import { type Logger, logs, SeverityNumber } from '@opentelemetry/api-logs';+import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http';+import type { Resource } from '@opentelemetry/resources';import {- BatchLogRecordProcessor,- LoggerProvider,-} from "@opentelemetry/sdk-logs";+ BatchLogRecordProcessor,+ LoggerProvider,+} from '@opentelemetry/sdk-logs';import {- ATTR_EXCEPTION_MESSAGE,- ATTR_EXCEPTION_STACKTRACE,- ATTR_EXCEPTION_TYPE,-} from "@opentelemetry/semantic-conventions";-import type { LogAttributes, TelemetryInitConfig } from "./config";+ ATTR_EXCEPTION_MESSAGE,+ ATTR_EXCEPTION_STACKTRACE,+ ATTR_EXCEPTION_TYPE,+} from '@opentelemetry/semantic-conventions';+import type { LogAttributes, TelemetryInitConfig } from './config';import {- ATTR_USER_ID,- CONSOLE_PREFIX,- INSTRUMENTATION_SCOPE_NAME,-} from "./constants";+ ATTR_USER_ID,+ CONSOLE_PREFIX,+ INSTRUMENTATION_SCOPE_NAME,+} from './constants';+import { userIdSuppressed } from './privacy';-type LogLevel = "debug" | "info" | "warn" | "error";+type LogLevel = 'debug' | 'info' | 'warn' | 'error';export class Logging {- #provider: LoggerProvider | undefined;- readonly #logger: Logger = logs.getLogger(INSTRUMENTATION_SCOPE_NAME);+ #provider: LoggerProvider | undefined;+ readonly #logger: Logger = logs.getLogger(INSTRUMENTATION_SCOPE_NAME);- init(- config: TelemetryInitConfig,- resource: Resource,- getUserId: () => string | undefined,- ): void {- if (!config.logsUrl) return;- this.#provider = new LoggerProvider({- resource,- processors: [- {- onEmit: (record) => {- const userId = getUserId();- if (userId !== undefined) record.setAttribute(ATTR_USER_ID, userId);- },- forceFlush: () => Promise.resolve(),- shutdown: () => Promise.resolve(),- },- new BatchLogRecordProcessor({- exporter: new OTLPLogExporter({ url: config.logsUrl }),- }),- ],- });- logs.setGlobalLoggerProvider(this.#provider);- }+ init(+ config: TelemetryInitConfig,+ resource: Resource,+ getUserId: () => string | undefined+ ): void {+ if (!config.logsUrl) return;+ this.#provider = new LoggerProvider({+ resource,+ processors: [+ {+ onEmit: (record, context) => {+ if (context && userIdSuppressed(context)) return;+ const userId = getUserId();+ if (userId !== undefined) record.setAttribute(ATTR_USER_ID, userId);+ },+ forceFlush: () => Promise.resolve(),+ shutdown: () => Promise.resolve(),+ },+ new BatchLogRecordProcessor({+ exporter: new OTLPLogExporter({ url: config.logsUrl }),+ }),+ ],+ });+ logs.setGlobalLoggerProvider(this.#provider);+ }- debug(message: string, attributes?: LogAttributes): void {- this.#emit("debug", message, attributes);- }+ debug(message: string, attributes?: LogAttributes): void {+ this.#emit('debug', message, attributes);+ }- info(message: string, attributes?: LogAttributes): void {- this.#emit("info", message, attributes);- }+ info(message: string, attributes?: LogAttributes): void {+ this.#emit('info', message, attributes);+ }- warn(message: string, attributes?: LogAttributes): void {- this.#emit("warn", message, attributes);- }+ warn(message: string, attributes?: LogAttributes): void {+ this.#emit('warn', message, attributes);+ }- error(error: unknown, attributes?: LogAttributes): void {- const normalized = Logging.#normalizeError(error);- this.#emit(- "error",- typeof normalized === "string"- ? normalized- : normalized.message || normalized.name,- {- ...attributes,- ...(normalized instanceof Error && {- [ATTR_EXCEPTION_TYPE]: normalized.name,- [ATTR_EXCEPTION_MESSAGE]: normalized.message,- [ATTR_EXCEPTION_STACKTRACE]: normalized.stack,- }),- },- );- }+ error(error: unknown, attributes?: LogAttributes): void {+ const normalized = Logging.#normalizeError(error);+ this.#emit(+ 'error',+ typeof normalized === 'string'+ ? normalized+ : normalized.message || normalized.name,+ {+ ...attributes,+ ...(normalized instanceof Error && {+ [ATTR_EXCEPTION_TYPE]: normalized.name,+ [ATTR_EXCEPTION_MESSAGE]: normalized.message,+ [ATTR_EXCEPTION_STACKTRACE]: normalized.stack,+ }),+ }+ );+ }- async flush(): Promise<void> {- await this.#provider?.forceFlush();- }+ async flush(): Promise<void> {+ await this.#provider?.forceFlush();+ }- async shutdown(): Promise<void> {- await this.#provider?.shutdown();- this.#provider = undefined;- }+ async shutdown(): Promise<void> {+ await this.#provider?.shutdown();+ this.#provider = undefined;+ }- #emit(level: LogLevel, message: string, attributes?: LogAttributes): void {- const severity = {- debug: SeverityNumber.DEBUG,- info: SeverityNumber.INFO,- warn: SeverityNumber.WARN,- error: SeverityNumber.ERROR,- }[level];- this.#logger.emit({- severityNumber: severity,- severityText: level,- body: message,- attributes: attributes as OtelAttributes,- });- const consoleMethod = level === "info" ? "log" : level;- console[consoleMethod](CONSOLE_PREFIX, message, attributes ?? {});- }+ #emit(level: LogLevel, message: string, attributes?: LogAttributes): void {+ const severity = {+ debug: SeverityNumber.DEBUG,+ info: SeverityNumber.INFO,+ warn: SeverityNumber.WARN,+ error: SeverityNumber.ERROR,+ }[level];+ this.#logger.emit({+ severityNumber: severity,+ severityText: level,+ body: message,+ attributes: attributes as OtelAttributes,+ });+ const consoleMethod = level === 'info' ? 'log' : level;+ console[consoleMethod](CONSOLE_PREFIX, message, attributes ?? {});+ }- static #normalizeError(error: unknown): Error | string {- return error instanceof Error ||- typeof error === "string" ||- (typeof error === "object" && error !== null && "message" in error)- ? (error as Error | string)- : String(error);- }+ static #normalizeError(error: unknown): Error | string {+ return error instanceof Error ||+ typeof error === 'string' ||+ (typeof error === 'object' && error !== null && 'message' in error)+ ? (error as Error | string)+ : String(error);+ }}