Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
feat(client): openplait connectors framework and signal routing (#1453)
Aman Agarwale215952c20a6
diff --git a/.gitignore b/.gitignoreindex fad85a5b..c4a88537 100644--- a/.gitignore+++ b/.gitignore@@ -9,6 +9,11 @@ path/__pycache__/**bun.lockblogs/+# Keep OpenLIT log-signal facade + API routes tracked (logs/ would ignore them).+!src/client/src/lib/platform/logs/+!src/client/src/lib/platform/logs/**+!src/client/src/app/api/telemetry/logs/+!src/client/src/app/api/telemetry/logs/**# Secrets / local env (never commit).env**/.env@@ -23,3 +28,4 @@ logs/**certs/index.txt**.csrsdk/typescript-test/+tmp/diff --git a/docs/docs.json b/docs/docs.jsonindex ee1931ef..e353ce08 100644--- a/docs/docs.json+++ b/docs/docs.json@@ -4,28 +4,11 @@"name": "OpenLIT","description": "OpenLIT is the leading open-source AI observability platform with zero-code instrumentation for LLMs, vector databases, and AI frameworks. Monitor OpenAI, Anthropic, LangChain, LlamaIndex with OpenTelemetry. Features include cost tracking, performance metrics, prompt management, and enterprise-grade security for production AI applications.","colors": {- "primary": "#FFA500",- "light": "#FFA500",- "dark": "#FFA500"+ "primary": "#F36C06",+ "light": "#FF8A3D",+ "dark": "#D45A05"},"favicon": "favicon.png",- "seo": {- "indexing": "navigable",- "metatags": {- "og:image": "/images/docs-main-banner.jpeg"- },- "organization": {- "name": "OpenLIT",- "url": "https://openlit.io",- "logo": "https://docs.openlit.io/images/rectangular-light-bg-logo.jpg",- "sameAs": [- "https://github.com/openlit/",- "https://twitter.com/openlit_io",- "https://www.linkedin.com/company/openlit/",- "https://join.slack.com/t/openlit/shared_invite/zt-2etnfttwg-TjP_7BZXfYg84oAukY8QRQ"- ]- }- },"feedback": {"thumbsRating": true,"suggestEdit": true@@ -65,147 +48,72 @@"pages": ["latest/openlit/quickstart-ai-observability","latest/openlit/quickstart-evals",- "latest/openlit/prompts-experiments/prompt-hub/overview"- ]- },- "latest/openlit/configuration"- ]- },- {- "group": "Monitor",- "pages": [- {- "group": "Telemetry",- "icon": "wave-pulse",- "pages": [- "latest/openlit/observability/telemetry/overview",- "latest/openlit/observability/telemetry/traces",- "latest/openlit/observability/telemetry/ai-analysis",- "latest/openlit/observability/telemetry/metrics",- "latest/openlit/observability/telemetry/logs"+ "latest/openlit/quickstart-guard",+ "latest/openlit/quickstart-gpu",+ "latest/openlit/quickstart-vectordb-observability",+ "latest/openlit/quickstart-mcp-observability"]},+ "latest/openlit/configuration",+ "latest/openlit/oauth",{- "group": "Agents",- "icon": "robot",+ "group": "Organisation",+ "icon": "building","pages": [- "latest/openlit/observability/agents/overview",- "latest/openlit/observability/agents/setup",- "latest/openlit/observability/agents/analytics",- "latest/openlit/observability/agents/monitoring",- "latest/openlit/observability/agents/definition",- "latest/openlit/observability/agents/configuration",- "latest/openlit/observability/agents/versioning"- ]- },- {- "group": "Coding Agents",- "icon": "code",- "pages": [- "latest/openlit/coding-agents/overview",- "latest/openlit/coding-agents/setup-and-configure",- "latest/openlit/coding-agents/analytics",- "latest/openlit/coding-agents/sessions",- "latest/openlit/coding-agents/users",- "latest/openlit/coding-agents/privacy-and-governance",- "latest/openlit/coding-agents/troubleshooting"- ]- },- {- "group": "Evaluations",- "icon": "display",- "pages": [- "latest/openlit/evaluations/overview",- "latest/openlit/evaluations/analytics",- "latest/openlit/evaluations/evaluators",- "latest/openlit/evaluations/configuration",+ "latest/openlit/organisation/overview",{- "group": "Evaluation Methods",- "icon": "ruler",+ "group": "Projects",+ "icon": "folder","pages": [- "latest/openlit/evaluations/llm-as-a-judge",- "latest/openlit/evaluations/programmatic-evals",- "latest/openlit/evaluations/manual-feedback"+ "latest/openlit/organisation/projects",+ "latest/openlit/organisation/environments",+ "latest/openlit/organisation/database-config",+ "latest/openlit/organisation/connectors",+ "latest/openlit/organisation/signal-routing",+ "latest/openlit/organisation/telemetry-sources"]}]- },- {- "group": "Costs",- "icon": "circle-dollar",- "pages": [- "latest/openlit/costs/overview",- "latest/openlit/costs/analytics",- {- "group": "Manage models",- "icon": "sliders",- "pages": [- "latest/openlit/costs/manage-models/overview",- "latest/openlit/costs/manage-models/providers",- "latest/openlit/costs/manage-models/models",- "latest/openlit/costs/manage-models/import-and-export",- "latest/openlit/costs/manage-models/sdk-pricing"- ]- },- "latest/openlit/costs/configuration"- ]}]},{- "group": "Develop",+ "group": "Observability","pages": [+ "latest/openlit/observability/tracing",+ "latest/openlit/observability/metrics",+ "latest/openlit/observability/error",+ "latest/openlit/observability/fleet-hub",{- "group": "Prompt Hub",- "icon": "shapes",- "pages": [- "latest/openlit/prompts-experiments/prompt-hub/overview",- "latest/openlit/prompts-experiments/prompt-hub/manage-prompts",- "latest/openlit/prompts-experiments/prompt-hub/versioning",- "latest/openlit/prompts-experiments/prompt-hub/ai-improvement",- "latest/openlit/prompts-experiments/prompt-hub/rules",- "latest/openlit/prompts-experiments/prompt-hub/retrieve-a-prompt"- ]- },- {- "group": "Vault",- "icon": "book",- "pages": [- "latest/openlit/developer-resources/vault/overview",- "latest/openlit/developer-resources/vault/manage-secrets",- "latest/openlit/developer-resources/vault/security",- "latest/openlit/developer-resources/vault/retrieve-secrets"- ]- },- {- "group": "Context",- "icon": "book-open",+ "group": "Agents",+ "icon": "radar","pages": [- "latest/openlit/prompts-experiments/context/overview",- "latest/openlit/prompts-experiments/context/manage-contexts",- "latest/openlit/prompts-experiments/context/link-rules",- "latest/openlit/prompts-experiments/context/retrieve-context"+ "latest/controller/overview",+ "latest/controller/quickstart",+ "latest/controller/architecture",+ "latest/controller/configuration"]},{- "group": "Rule Engine",- "icon": "sliders",+ "group": "Coding Agents",+ "icon": "code","pages": [- "latest/openlit/prompts-experiments/rule-engine/overview",- "latest/openlit/prompts-experiments/rule-engine/create-and-manage-rules",- "latest/openlit/prompts-experiments/rule-engine/conditions",- "latest/openlit/prompts-experiments/rule-engine/preview",- "latest/openlit/prompts-experiments/rule-engine/linked-entities",- "latest/openlit/prompts-experiments/rule-engine/evaluate-api"+ "latest/openlit/coding-agents/onboarding",+ "latest/openlit/coding-agents/architecture"]- },+ }+ ]+ },+ {+ "group": "Evaluations",+ "pages": [+ "latest/openlit/evaluations/overview",{- "group": "OpenGround",- "icon": "display",+ "group": "Evaluation Methods",+ "icon": "ruler","pages": [- "latest/openlit/prompts-experiments/openground/overview",- "latest/openlit/prompts-experiments/openground/run-a-comparison",- "latest/openlit/prompts-experiments/openground/results-and-history"+ "latest/openlit/evaluations/llm-as-a-judge",+ "latest/openlit/evaluations/programmatic-evals"]}]@@ -229,7 +137,7 @@"pages": ["latest/openlit/dashboards/widgets/overview","latest/openlit/dashboards/widgets/area-chart-widget",- "latest/openlit/dashboards/widgets/bar-chart-widget",+ "latest/openlit/dashboards/widgets/bar-chart-widget","latest/openlit/dashboards/widgets/line-chart-widget","latest/openlit/dashboards/widgets/pie-chart-widget","latest/openlit/dashboards/widgets/stat-widget",@@ -254,31 +162,29 @@{"group": "Otter (AI Copilot)","pages": [- "latest/openlit/chat/overview",- "latest/openlit/chat/conversations",- "latest/openlit/chat/settings",- "latest/openlit/chat/usage"+ "latest/openlit/chat"]},{- "group": "Platform",+ "group": "Pricing & Models","pages": [- {- "group": "Administration",- "icon": "user-shield",- "pages": [- "latest/openlit/oauth",- {- "group": "Organisation",- "icon": "building",- "pages": [- "latest/openlit/organisation/overview",- "latest/openlit/organisation/projects",- "latest/openlit/organisation/database-config"- ]- }- ]- },+ "latest/openlit/pricing/manage-models",+ "latest/openlit/pricing/cost-recalculation"+ ]+ },+ {+ "group": "Prompts and Experiments",+ "pages": [+ "latest/openlit/prompts-experiments/prompt-hub",+ "latest/openlit/prompts-experiments/openground",+ "latest/openlit/prompts-experiments/context",+ "latest/openlit/prompts-experiments/rule-engine"+ ]+ },+ {+ "group": "Developer Resources",+ "pages": [+ "latest/openlit/developer-resources/vault","latest/openlit/developer-resources/multiple-db","latest/openlit/developer-resources/anonymous-telemetry",{@@ -303,28 +209,10 @@"pages": ["latest/openlit/developer-resources/api-reference/endpoint/rule-engine/evaluate"]- },- {- "group": "Manage Models",- "pages": [- "latest/openlit/developer-resources/api-reference/endpoint/manage-models/models",- "latest/openlit/developer-resources/api-reference/endpoint/manage-models/providers",- "latest/openlit/developer-resources/api-reference/endpoint/manage-models/export",- "latest/openlit/developer-resources/api-reference/endpoint/manage-models/import",- "latest/openlit/developer-resources/api-reference/endpoint/manage-models/public-pricing"- ]}]}]- },- {- "group": "Resources",- "pages": [- "latest/glossary/what-is-ai-observability",- "latest/glossary/what-is-ai-evaluation",- "latest/glossary/what-is-prompt-engineering"- ]}]},@@ -361,7 +249,6 @@"latest/sdk/features/pricing","latest/sdk/features/evaluations","latest/sdk/features/guardrails",- "latest/sdk/features/custom-attributes","latest/sdk/features/rule-engine"]},@@ -374,8 +261,10 @@"icon": "robot","pages": ["latest/sdk/integrations/openai",+ "latest/sdk/integrations/go-openai","latest/sdk/integrations/ollama","latest/sdk/integrations/anthropic",+ "latest/sdk/integrations/go-anthropic","latest/sdk/integrations/deepseek","latest/sdk/integrations/gpt4all","latest/sdk/integrations/cohere",@@ -386,7 +275,6 @@"latest/sdk/integrations/azure-ai-inference","latest/sdk/integrations/huggingface","latest/sdk/integrations/bedrock",- "latest/sdk/integrations/oci","latest/sdk/integrations/vertexai","latest/sdk/integrations/google-ai-studio","latest/sdk/integrations/groq",@@ -447,10 +335,7 @@"latest/sdk/integrations/strands","latest/sdk/integrations/google-adk","latest/sdk/integrations/claude-agent-sdk",- "latest/sdk/integrations/vercel-ai",- "latest/sdk/integrations/smolagents",- "latest/sdk/integrations/agent-framework",- "latest/sdk/integrations/mcp"+ "latest/sdk/integrations/vercel-ai"]},{@@ -534,170 +419,9 @@]}]- },- {- "tab": "CLI",- "groups": [- {- "group": "Getting Started",- "pages": [- "latest/cli/overview",- "latest/cli/installation",- "latest/cli/configuration"- ]- },- {- "group": "Reference",- "pages": [- "latest/cli/commands"- ]- }- ]}]},- "redirects": [- {- "source": "/latest/openlit/prompts-experiments/prompt-hub",- "destination": "/latest/openlit/prompts-experiments/prompt-hub/overview",- "permanent": true- },- {- "source": "/latest/openlit/developer-resources/vault",- "destination": "/latest/openlit/developer-resources/vault/overview",- "permanent": true- },- {- "source": "/latest/openlit/prompts-experiments/context",- "destination": "/latest/openlit/prompts-experiments/context/overview",- "permanent": true- },- {- "source": "/latest/openlit/prompts-experiments/rule-engine",- "destination": "/latest/openlit/prompts-experiments/rule-engine/overview",- "permanent": true- },- {- "source": "/latest/openlit/prompts-experiments/openground",- "destination": "/latest/openlit/prompts-experiments/openground/overview",- "permanent": true- },- {- "source": "/latest/openlit/observability/tracing",- "destination": "/latest/openlit/observability/telemetry/traces",- "permanent": true- },- {- "source": "/latest/openlit/observability/metrics",- "destination": "/latest/openlit/observability/telemetry/metrics",- "permanent": true- },- {- "source": "/latest/openlit/observability/error",- "destination": "/latest/openlit/observability/telemetry/traces",- "permanent": true- },- {- "source": "/latest/openlit/observability/telemetry",- "destination": "/latest/openlit/observability/telemetry/overview",- "permanent": true- },- {- "source": "/latest/openlit/observability/agents",- "destination": "/latest/openlit/observability/agents/overview",- "permanent": true- },- {- "source": "/latest/openlit/coding-agents/onboarding",- "destination": "/latest/openlit/coding-agents/setup-and-configure",- "permanent": true- },- {- "source": "/latest/openlit/coding-agents/architecture",- "destination": "/latest/openlit/coding-agents/overview",- "permanent": true- },- {- "source": "/latest/openlit/observability/agents/agent-detail",- "destination": "/latest/openlit/observability/agents/overview",- "permanent": true- },- {- "source": "/latest/openlit/observability/agents/definition-and-versions",- "destination": "/latest/openlit/observability/agents/versioning",- "permanent": true- },- {- "source": "/latest/openlit/observability/agents/coding-agents",- "destination": "/latest/openlit/coding-agents/overview",- "permanent": true- },- {- "source": "/latest/openlit/costs/manage-models",- "destination": "/latest/openlit/costs/manage-models/overview",- "permanent": true- },- {- "source": "/latest/openlit/chat",- "destination": "/latest/openlit/chat/overview",- "permanent": true- },- {- "source": "/latest/openlit/pricing/cost-recalculation",- "destination": "/latest/openlit/costs/configuration",- "permanent": true- },- {- "source": "/latest/openlit/pricing/manage-models",- "destination": "/latest/openlit/costs/manage-models/overview",- "permanent": true- },- {- "source": "/latest/openlit/pricing/manage-models/overview",- "destination": "/latest/openlit/costs/manage-models/overview",- "permanent": true- },- {- "source": "/latest/openlit/pricing/manage-models/providers",- "destination": "/latest/openlit/costs/manage-models/providers",- "permanent": true- },- {- "source": "/latest/openlit/pricing/manage-models/models",- "destination": "/latest/openlit/costs/manage-models/models",- "permanent": true- },- {- "source": "/latest/openlit/pricing/manage-models/import-and-export",- "destination": "/latest/openlit/costs/manage-models/import-and-export",- "permanent": true- },- {- "source": "/latest/openlit/pricing/manage-models/sdk-pricing",- "destination": "/latest/openlit/costs/manage-models/sdk-pricing",- "permanent": true- },- {- "source": "/latest/openlit/evaluations/core-concepts",- "destination": "/latest/openlit/evaluations/evaluators",- "permanent": true- },- {- "source": "/latest/openlit/evaluations/auto-evaluation",- "destination": "/latest/openlit/evaluations/configuration",- "permanent": true- },- {- "source": "/latest/openlit/evaluations/settings",- "destination": "/latest/openlit/evaluations/configuration",- "permanent": true- },- {- "source": "/latest/openlit/evaluations/types",- "destination": "/latest/openlit/evaluations/evaluators",- "permanent": true- }- ],"logo": {"light": "/images/rectangular-light-bg-logo.jpg","dark": "/images/rectangular-dark-bg-logo.jpg",@@ -706,8 +430,23 @@"appearance": {"default": "dark"},+ "fonts": {+ "family": "Inter"+ },+ "background": {+ "color": {+ "light": "#FAFAF9",+ "dark": "#0C0A09"+ }+ },"styling": {- "eyebrows": "breadcrumbs"+ "eyebrows": "breadcrumbs",+ "codeblocks": {+ "theme": {+ "light": "github-light",+ "dark": "github-dark"+ }+ }},"navbar": {"primary": {@@ -733,10 +472,6 @@}},"contextual": {- "options": [- "copy",- "chatgpt",- "claude"- ]+ "options": ["copy", "chatgpt", "claude"]}}diff --git a/docs/images/organisation/add-database-config.webm b/docs/images/organisation/add-database-config.webmnew file mode 100644index 00000000..0e625909Binary files /dev/null and b/docs/images/organisation/add-database-config.webm differdiff --git a/docs/images/organisation/connectors-list.png b/docs/images/organisation/connectors-list.pngnew file mode 100644index 00000000..8f37d883Binary files /dev/null and b/docs/images/organisation/connectors-list.png differdiff --git a/docs/images/organisation/openlit-jaeger-connector.png b/docs/images/organisation/openlit-jaeger-connector.pngnew file mode 100644index 00000000..53659dd0Binary files /dev/null and b/docs/images/organisation/openlit-jaeger-connector.png differdiff --git a/docs/images/organisation/project-header.png b/docs/images/organisation/project-header.pngnew file mode 100644index 00000000..72af717fBinary files /dev/null and b/docs/images/organisation/project-header.png differdiff --git a/docs/images/organisation/videos/b8c36210756d8ab8d1af2b035b0d227b.webm b/docs/images/organisation/videos/b8c36210756d8ab8d1af2b035b0d227b.webmnew file mode 100644index 00000000..0e625909Binary files /dev/null and b/docs/images/organisation/videos/b8c36210756d8ab8d1af2b035b0d227b.webm differdiff --git a/docs/latest/openlit/configuration.mdx b/docs/latest/openlit/configuration.mdxindex 0f8484d2..d3fdb87e 100644--- a/docs/latest/openlit/configuration.mdx+++ b/docs/latest/openlit/configuration.mdx@@ -4,12 +4,29 @@ description: 'Configuring Options for OpenLIT'icon: 'sliders'----This guide covers all the available environment variables to fine-tune OpenLIT according to your needs.+This guide covers environment variables for installing and fine-tuning OpenLIT.++<Info>+**ClickHouse connection at runtime is a [Database Config](/latest/openlit/organisation/database-config), not a live env lookup.**+`INIT_DB_*` variables **seed** the first Database Config when OpenLIT is first set up. Day-2 host, password, or environment changes belong in **Organisation → Database Config / Connectors**. See also [Connectors](/latest/openlit/organisation/connectors) and [Signal routing](/latest/openlit/organisation/signal-routing).+</Info>++## ClickHouse bootstrap (`INIT_DB_*`)++Used once at seed/install time to create the default Database Config. After that, the UI (and APIs under `/api/db-config`) own the connection.++| Env var | Database Config field |+| --- | --- |+| `INIT_DB_HOST` | Host |+| `INIT_DB_PORT` | Port |+| `INIT_DB_DATABASE` | Database |+| `INIT_DB_USERNAME` | Username |+| `INIT_DB_PASSWORD` | Password |## Environment variables<ResponseField name="INIT_DB_HOST" required>- Sets the host address of the ClickHouse server for OpenLIT to connect+ Seed-only: host address written into the default Database Config on first setup. Not read on every request after the config exists.**Example**:```yaml@@ -18,7 +35,7 @@ This guide covers all the available environment variables to fine-tune OpenLIT a</ResponseField><ResponseField name="INIT_DB_PORT" required>- Sets the port on which ClickHouse listens+ Seed-only: ClickHouse HTTP port for the default Database Config (commonly `8123`).**Example**:```yaml@@ -27,7 +44,7 @@ This guide covers all the available environment variables to fine-tune OpenLIT a</ResponseField><ResponseField name="INIT_DB_DATABASE" required>- Sets the name of the database in Clickhouse to be used by OpenLIT+ Seed-only: ClickHouse database name for the default Database Config.**Example**:```yaml@@ -36,7 +53,7 @@ This guide covers all the available environment variables to fine-tune OpenLIT a</ResponseField><ResponseField name="INIT_DB_USERNAME" required>- Sets the username for authenticating with ClickHouse+ Seed-only: username stored on the default Database Config.**Example**:```yaml@@ -45,7 +62,7 @@ This guide covers all the available environment variables to fine-tune OpenLIT a</ResponseField><ResponseField name="INIT_DB_PASSWORD" required>- Sets the password for authenticating with ClickHouse+ Seed-only: password stored on the default Database Config. Rotate later via the Database Config UI, not by relying on this env var alone.**Example**:```yaml@@ -347,7 +364,8 @@ A restart is also all you need for the cron-driven features (Auto Evaluation, Au## Sample environment file (.env)```.env.example .env-# Database Configuration+# ClickHouse bootstrap ONLY — seeds the first Database Config at install.+# Day-2 host/password/environment changes: Organisation → Database Config.INIT_DB_HOST="127.0.0.1"INIT_DB_PORT="8123"INIT_DB_DATABASE="default"diff --git a/docs/latest/openlit/developer-resources/multiple-db.mdx b/docs/latest/openlit/developer-resources/multiple-db.mdxindex 0b16652a..e3c7da12 100644--- a/docs/latest/openlit/developer-resources/multiple-db.mdx+++ b/docs/latest/openlit/developer-resources/multiple-db.mdx@@ -1,66 +1,46 @@----title: "Connect Multiple Databases"-sidebarTitle: "Connect Multiple Databases"-description: "Connect multiple ClickHouse database configurations in one OpenLIT project and switch which one powers traces, dashboards, Prompt Hub, and Vault"-icon: "database"+title: 'Connect Multiple Databases'+description: 'Add and switch between multiple ClickHouse Database Configs per project and environment in OpenLIT'+sidebarTitle: 'Connect Multiple Databases'----OpenLIT lets a single project hold **multiple database configurations** - each one is a ClickHouse connection (host, port, database, credentials) that you can select as the active source for observability and platform data. The active configuration powers traces, dashboards, Prompt Hub, Vault, Manage Models, and the rest of the app for your session.+OpenLIT lets you attach multiple **Database Configs** (ClickHouse connections) inside a project, each tagged with an [environment](/latest/openlit/organisation/environments). Use this to separate staging vs production data, tenants, or retention tiers without redeploying `INIT_DB_*` env vars.-Managing those connections lives under **Organisation → Project → Database configurations**. There is no standalone “database configuration” settings page - `/settings/database-config` redirects to Organisation.+Day-2 connection changes belong in **Organisation → Project → Database configurations / Connectors**, not in live container env vars. See [Database Config](/latest/openlit/organisation/database-config).-## When to use multiple databases+## When to use this feature-- **Environments** - keep development, staging, and production data on separate ClickHouse databases and switch between them without changing projects.-- **Isolation** - give different tenants or teams their own database configurations inside the same organisation structure.-- **One OpenLIT, many stores** - analyze several ClickHouse backends from a single OpenLIT deployment.+- **Different environments** — keep `production` and `staging` ClickHouse clusters on separate configs+- **Multi-tenancy** — isolate datasets per customer while sharing one OpenLIT deployment+- **Centralized management** — switch active Database Config from the header **Databases** control-Search interest for niche phrases like "multi-tenant observability platform" is too thin and noisy to lean on - the product job is simply: connect and switch multiple databases from one place.+## Get started-## Add and switch configurations+<Frame>+ <img src="/images/organisation/database-config-list.png" alt="OpenLIT database configuration list on a project" />+</Frame>-<Steps>- <Step title="Open Organisation">- Select **Organisation** (or **Projects**) from the sidebar under Settings.- </Step>- <Step title="Open a project">- Open the project that should own the configuration, then switch to the **Database configurations** tab. Configurations are project-scoped - one created under project A is never selectable from project B.- </Step>- <Step title="Add New Config">- Click **Add New Config** (or **Add database config** from the empty state). Fill in **Config Name**, **Environment**, **Username**, **Password**, **Host**, **Port**, **Database**, and optional **Query params**, then **Save**.- </Step>- <Step title="Mark it active">- Use the checkbox on the list row (tooltip: **Mark … as the active database config**) or pick the config from the header **Databases** switcher. Only one is active for your user in that project at a time - teammates can select a different one.- </Step>-</Steps>+1. **Log in** to OpenLIT (`[email protected]` / `openlituser` on local seed installs).+2. Select **organisation**, **project**, and **environment** in the header.+3. Open **Organisation → Project → Database configurations** (or **Connectors → ClickHouse**).+4. Click **Add**, enter host, port, database, credentials, and environment.+5. **Save**, then mark the config **active** for your user.+6. Dashboards, requests, Prompt Hub, and Vault use the active Database Config for that project.-There is no **Test connection** button on the form - OpenLIT checks connectivity after a config is set active. The active database configuration drives what you see across the product, including dashboards, Telemetry, Prompt Hub, and Vault.+<video controls playsInline preload="metadata" src="/images/organisation/add-database-config.webm" style={{ width: "100%", borderRadius: "8px" }} />-## Sharing and project boundaries--You can share a database configuration with specific teammates by email, with separate edit / delete / re-share permissions. For the full field list, selection behavior, and FAQs, see [Database Config](/latest/openlit/organisation/database-config) under Organisation.--## Frequently asked questions--<AccordionGroup>- <Accordion title="Can two projects share one database configuration?">- No. A configuration belongs to the project it was created in. Point another project at the same ClickHouse instance by creating a new configuration there.- </Accordion>- <Accordion title="Do I need a new ClickHouse cluster for every configuration?">- No. Multiple configurations can target the same cluster with different database names, or entirely different clusters.- </Accordion>- <Accordion title="Where did the Database Config settings page go?">- `/settings/database-config` redirects to Organisation - open a project and use the **Database configurations** tab.- </Accordion>-</AccordionGroup>+Pair each environment’s Database Config with [signal routing](/latest/openlit/organisation/signal-routing) when traces/logs/metrics live on Tempo, Loki, Prometheus, or Jaeger.----<CardGroup cols={2}>+<CardGroup cols={3}><Card title="Database Config" href="/latest/openlit/organisation/database-config" icon="database">- Field-level guide for adding and selecting ClickHouse configs+ Env vars → Database Config migration and field reference+ </Card>+ <Card title="Environments" href="/latest/openlit/organisation/environments" icon="layer-group">+ How environments partition configs and connectors</Card>- <Card title="Organisation" href="/latest/openlit/organisation/overview" icon="building">- Organisations, projects, and how database configs fit the hierarchy+ <Card title="Connectors" href="/latest/openlit/organisation/connectors" icon="plug">+ External data-source connectors for OpenLIT</Card></CardGroup>diff --git a/docs/latest/openlit/organisation/connectors.mdx b/docs/latest/openlit/organisation/connectors.mdxnew file mode 100644index 00000000..c8a3e4f1--- /dev/null+++ b/docs/latest/openlit/organisation/connectors.mdx@@ -0,0 +1,173 @@+---+title: 'Connectors'+sidebarTitle: 'Connectors'+description: 'Connect OpenLIT to ClickHouse and OpenPlait-backed observability backends — atomic connectors, actions, and the roadmap for a unified connector registry'+icon: 'plug'+---++**Connectors** are how OpenLIT attaches to data systems. Each connector is an **atomic** integration: one backend, one credential set, and a clear set of **actions** (test connection, validate AI telemetry, bind signals, manage secrets).++Open the connectors experience from **Organisation → Project → Connectors** (Data sources). Always select the correct [project](/latest/openlit/organisation/projects) and [environment](/latest/openlit/organisation/environments) first — connectors belong to the project, not the organisation root.++<Frame>+ <img src="/images/organisation/connectors-list.png" alt="OpenLIT Connectors page showing configured connectors and the connector catalog" />+</Frame>++<Info>+Connectors are the long-term integration point in OpenLIT: **data-source connectors** today, with the same registry model expanding to notifications and other action types over time. Several read paths use portable [@openplait](https://github.com/openlit/openplait) adapters so query behavior stays consistent across backends.+</Info>++## Mental model++```mermaid+flowchart LR+ subgraph org [Organisation]+ proj[Project]+ end+ subgraph env [Environment]+ bindT[traces binding]+ bindL[logs binding]+ bindM[metrics binding]+ end+ proj --> env+ bindT --> C1[Connector A]+ bindL --> C2[Connector B]+ bindM --> C3[Connector C]+ C1 --> Tempo[Tempo]+ C2 --> Loki[Loki]+ C3 --> Prom[Prometheus]+```++- **Atomic connectors** — never a multi-backend blob. One Tempo instance, one Loki instance, one Prometheus endpoint.+- **Signal routing** — each of traces / logs / metrics is bound independently. See [Signal routing](/latest/openlit/organisation/signal-routing).+- **Database Config** — ClickHouse lives as a Database Config and appears as the built-in connector. See [Database Config](/latest/openlit/organisation/database-config).++## Supported connectors (OpenLIT + OpenPlait)++These are the connectors available in open-source OpenLIT. OpenPlait packages power the portable query adapters for ClickHouse, Tempo, Loki, Prometheus, and Jaeger.++### Built-in app store++| Connector | Package / implementation | Signals | What it’s for |+| --- | --- | --- | --- |+| **ClickHouse** | Database Config + `@openplait/adapter-clickhouse` | traces, logs, metrics (+ intelligence) | Default store; full correlation, raw SQL, evals metadata, vault |++### Data-source connectors++| Connector | Package / implementation | Signals | What it’s for |+| --- | --- | --- | --- |+| **Grafana Tempo** | `@openplait/adapter-tempo` | traces | TraceQL search, trace tree, span events |+| **Grafana Loki** | `@openplait/adapter-loki` | logs | LogQL logs; correlate by trace id / service |+| **Prometheus** | `@openplait/adapter-prometheus` | metrics | PromQL HTTP API (also works with Prometheus-compatible endpoints such as Mimir when you point at their query URL) |+| **Jaeger** | `@openplait/adapter-jaeger` | traces | Jaeger Query HTTP API; sampled in-process aggregates |++<Tip>+Prometheus-compatible APIs (for example Grafana Mimir’s PromQL endpoint) use the **Prometheus** connector — there is no separate Mimir connector type in open-source OpenLIT.+</Tip>++## Capability matrix++| Connector | Signals | Trace tree | Span events | Server aggregation | Raw SQL | Cross-signal correlation |+| --- | --- | --- | --- | --- | --- | --- |+| ClickHouse | traces, logs, metrics | Yes | Yes | Yes | Yes | Full |+| Tempo | traces | Yes | Yes | No* | No | trace / span / service |+| Loki | logs | — | — | No | No | trace id, service |+| Prometheus | metrics | — | — | Yes | No | — |+| Jaeger | traces | Yes | Yes | No* | No | trace / span / service |++\* Aggregate graphs are reconstructed in-process from a bounded sample of full traces when the backend cannot aggregate server-side.++## Add a Jaeger connector in OpenLIT++<Frame>+ <img src="/images/organisation/openlit-jaeger-connector.png" alt="Add source dialog with the connector type menu open, including Jaeger" />+</Frame>++1. Select a [project](/latest/openlit/organisation/projects) and environment in the header.+2. Open **Connectors** and choose **Add connector** / **Add source**.+3. Pick **Jaeger** from the connector type list.+4. Set the Query URL (local all-in-one: `http://localhost:16686`) and auth if needed.+5. Bind **traces** to the connector under [signal routing](/latest/openlit/organisation/signal-routing).++<Tip>+Local demo credentials for seeded installs: email `[email protected]`, password `openlituser`. Jaeger all-in-one + seed script live in the OpenPlait `adapter-jaeger` integration folder.+</Tip>++## Actions you can take on a connector++The connector registry is designed so **actions** attach to a connector type. Today’s data-source actions:++| Action | When to use it |+| --- | --- |+| **Add / edit connector** | Point OpenLIT at a new endpoint or rotate settings |+| **Store credentials in Vault** | API keys and tokens stay encrypted; decrypted only server-side |+| **Health check / test connection** | Confirm the endpoint is reachable with current auth |+| **Validate AI signal** | Confirm recent AI telemetry exists (`gen_ai.*` / OpenLIT markers) |+| **Bind signal** | Route traces, logs, or metrics to this connector for an environment |+| **Unbind / rebind** | Move a signal to another connector without deleting history in the backend |+| **Mark Database Config active** | Choose which ClickHouse app store the UI uses |+| **Share Database Config** | Grant edit / delete / re-share on a ClickHouse connection |++Enterprise audit logs record connector create/update/delete/test plus bind/unbind (with signal, environment, and previous source when switching). RBAC permissions are `connectors:read|create|update|delete|test|bind` (owner/admin by default).++### Future connector categories++The same registry will grow beyond datasources (for example notification and workflow connectors). Each new category still follows: **atomic connector → credentials → actions → optional bindings**. Data-source connectors remain the foundation for telemetry reads.++## How OpenLIT finds AI telemetry++Backends hold all telemetry — not only AI. OpenLIT applies a layered **AI selector** so reads stay focused:++- Resource `telemetry.sdk.name = openlit`+- Resource `telemetry.distro.name = openlit-cli`+- Any `gen_ai.*` attribute+- `coding_agent.session.id`, or Claude Code session markers+- Known `coding_agent.*` span names++## Authentication cheat sheet++| Backend | Typical auth |+| --- | --- |+| **Grafana Cloud** (Tempo / Loki) | Basic: instance ID + access policy token (`traces:read` / `logs:read`). Use **query** URLs from the Cloud Portal. |+| **Self-hosted Tempo / Loki / Prometheus / Jaeger** | None, Basic, or Bearer; optional tenant / `X-Scope-OrgID` |+| **ClickHouse** | Username / password on the Database Config |++## Security and reliability++- Endpoints validated (`http`/`https` only); credentials in URLs rejected; private/metadata SSRF targets blocked where applicable.+- Secrets live in Vault, redacted from errors, never logged.+- Per-source concurrency caps, query budgets, short-lived cache + in-flight de-dupe, and backoff on transient `429`/`5xx`.++## OpenPlait packages++OpenLIT wraps these npm packages for portable reads:++| npm package | Used for |+| --- | --- |+| `@openplait/core` | Query IR and normalized results |+| `@openplait/adapter-sdk` | Adapter contracts |+| `@openplait/adapter-clickhouse` | ClickHouse / OTel |+| `@openplait/adapter-tempo` | Tempo / TraceQL |+| `@openplait/adapter-loki` | Loki / LogQL |+| `@openplait/adapter-prometheus` | Prometheus / PromQL |+| `@openplait/adapter-jaeger` | Jaeger Query HTTP API |+| `@openplait/runtime` | Planning and execution helpers |++See the [OpenPlait repository](https://github.com/openlit/openplait) for publishing and adapter docs.++## Related++<CardGroup cols={2}>+ <Card title="Signal routing" href="/latest/openlit/organisation/signal-routing" icon="route">+ Bind traces, logs, and metrics independently.+ </Card>+ <Card title="Environments" href="/latest/openlit/organisation/environments" icon="layer-group">+ Partition configs and connectors by environment.+ </Card>+ <Card title="Database Config" href="/latest/openlit/organisation/database-config" icon="database">+ Env vars → ClickHouse Database Config.+ </Card>+ <Card title="Projects" href="/latest/openlit/organisation/projects" icon="folder">+ How connectors are scoped to a project.+ </Card>+</CardGroup>diff --git a/docs/latest/openlit/organisation/database-config.mdx b/docs/latest/openlit/organisation/database-config.mdxindex 15db27ba..daf8db8c 100644--- a/docs/latest/openlit/organisation/database-config.mdx+++ b/docs/latest/openlit/organisation/database-config.mdx@@ -1,59 +1,132 @@---title: 'Database Config'-description: 'Add and manage ClickHouse database configurations for an OpenLIT project'+sidebarTitle: 'Database Config'+description: 'Create ClickHouse Database Configs on a project — seeded from INIT_DB_* then managed in the OpenLIT UI'icon: 'database'----A database configuration is the ClickHouse connection - host, port, database, and credentials - that a project reads and writes data through. Every project needs at least one before it can show traces, dashboards, or any other data.+Database Configs live **under a project**. Create or select a [project](/latest/openlit/organisation/projects) first, then add ClickHouse connections for that project's environments. They are not a free-floating organisation-wide setting.++A **Database Config** is the ClickHouse connection OpenLIT uses as its **app store** and default telemetry backend: host, port, database, credentials, environment label, and optional query params. Every project needs at least one before traces, dashboards, evals, or vault-backed features can load.++<Info>+**Environment variables are for bootstrap only.** `INIT_DB_HOST`, `INIT_DB_PORT`, `INIT_DB_DATABASE`, `INIT_DB_USERNAME`, and `INIT_DB_PASSWORD` seed the first Database Config at install time. After that, OpenLIT reads and writes ClickHouse through the **Database Config stored in the product database** — not those env vars on every request.+</Info>++## From env vars to Database Config++| Before (deploy-time env) | After (product Database Config) |+| --- | --- |+| `INIT_DB_HOST` | **Host** |+| `INIT_DB_PORT` | **Port** |+| `INIT_DB_DATABASE` | **Database** |+| `INIT_DB_USERNAME` | **Username** |+| `INIT_DB_PASSWORD` | **Password** |+| (often a single shared connection) | **Config name** + **Environment** per project |++### What still uses env vars++| Variable | Role today |+| --- | --- |+| `INIT_DB_*` | **Seed only** — when the app first starts (or is seeded), OpenLIT creates/updates a default Database Config if host and port are set. Changing these later does **not** automatically rewrite existing configs. |+| `SQLITE_DATABASE_URL` | Location of the OpenLIT control-plane SQLite DB (users, projects, Database Config rows, bindings). |+| `OPENLIT_DB_*` / Compose secrets | Credentials for the **ClickHouse container** itself in Docker/K8s — infrastructure, not the UI connection picker. |++### What to change when ClickHouse moves++1. Open **Organisation → Project → Database configurations** (or **Connectors → ClickHouse**).+2. Edit or add a Database Config with the new host/port/database/credentials.+3. Mark it **active** for your user in that project (header **Databases** switcher).+4. Optionally keep `INIT_DB_*` in Compose only so **new** installs still seed a default — do not rely on them for day-2 connection changes.++<Warning>+OpenLIT still requires a Database Config even when raw traces/logs/metrics are routed to Tempo, Loki, Prometheus, Datadog, or other [connectors](/latest/openlit/organisation/connectors). Evaluations, boards metadata, rules, vault secrets, and agent summaries stay in ClickHouse.+</Warning>## Add a database configuration-Configs live on the project page under the **Database configurations** tab (`/organisation/project/[projectId]?tab=database`). `/settings/database-config` redirects to Organisation.+Configs live on the **current project**. OpenLIT’s Organisation → **Projects** tab states this explicitly: select a project before managing its Database Config. In Connectors, ClickHouse also appears as the built-in connector.++<Frame>+ <img src="/images/organisation/projects-list.png" alt="Organisation Projects tab — Manage DB Config per project" />+</Frame>++<video controls playsInline preload="metadata" src="/images/organisation/add-database-config.webm" style={{ width: "100%", borderRadius: "8px" }} /><Steps>- <Step title="Open Organisation">- Select **Organisation** or **Projects** from the sidebar under Settings.+ <Step title="Select a project">+ In the header, choose the [project](/latest/openlit/organisation/projects) (and [environment](/latest/openlit/organisation/environments)) that should own the configuration.</Step>- <Step title="Open a project">- Choose the project that should own the database configuration, then open the **Database configurations** tab.+ <Step title="Open Database configurations">+ From **Organisation → Projects**, use **Manage DB Config**, or open **Connectors → ClickHouse**.</Step>- <Step title="Add New Config">- Click **Add New Config** and enter **Config Name**, **Environment**, **Username**, **Password**, **Host**, **Port**, **Database**, and optional **Query params**. Click **Save** (or **Update** when editing).+ <Step title="Add config">+ Enter **Config Name**, **Environment**, **Username**, **Password**, **Host**, **Port**, **Database**, and optional **Query params**.</Step>- <Step title="Mark it active">- Check the list row to **Mark … as the active database config**, or choose it from the header **Databases** switcher. Connectivity is checked after the config becomes active - there is no separate test-connection button on the form.+ <Step title="Save and activate">+ Save, then mark the config active (list checkbox or header **Databases** switcher). Connectivity is verified when the config becomes active.</Step></Steps>+## Fields++| Field | Description |+| --- | --- |+| **Config Name** | Friendly label (unique per project + environment). |+| **Environment** | Logical env (for example `production`, `staging`) used with [signal routing](/latest/openlit/organisation/signal-routing). |+| **Host / Port** | ClickHouse HTTP endpoint (commonly port `8123`). |+| **Database** | ClickHouse database name OpenLIT queries. |+| **Username / Password** | ClickHouse credentials. Password is masked after save. |+| **Query params** | Optional URL query string appended to the ClickHouse HTTP client. |+## Selection behavior-The active database configuration is scoped to the current user and project - it's not a global setting, so two people can be looking at different database configurations inside the same project at the same time. Switching projects updates the list of available database configurations to only those that belong to the newly selected project; a configuration created under one project is never selectable from another. If a project has no database configuration yet, OpenLIT guides you through onboarding before showing any data pages.+- Active Database Config is scoped to the **current user + project** — teammates can select different configs in the same project.+- Switching projects changes the available configs; a config never crosses projects.+- Creating a config seeds default **signal bindings** (traces, logs, metrics, intelligence) to that ClickHouse for the environment when none exist yet.-## Multiple databases+## Multiple databases and environments-A project can have multiple database configurations - for example, one per ClickHouse cluster or one per data retention tier. Use multiple configurations when you need to switch between them without changing projects or organisations.+Use several Database Configs when you need separate ClickHouse clusters, retention tiers, or staging vs production without changing organisation. Pair each config’s **Environment** with connector bindings so telemetry routing stays consistent. See [Environments](/latest/openlit/organisation/environments).-<Card title="Multiple database guide" href="/latest/openlit/developer-resources/multiple-db" icon="database">- Learn more about using multiple ClickHouse database configurations in OpenLIT.-</Card>+<CardGroup cols={2}>+ <Card title="Environments" href="/latest/openlit/organisation/environments" icon="layer-group">+ How environments replace a single global connection.+ </Card>+ <Card title="Multiple databases" href="/latest/openlit/developer-resources/multiple-db" icon="database">+ Switch between ClickHouse configs inside a project.+ </Card>+ <Card title="Signal routing" href="/latest/openlit/organisation/signal-routing" icon="route">+ Bind traces, logs, and metrics per environment.+ </Card>+</CardGroup>-## Sharing a database configuration+## Sharing-A database configuration can be shared directly with specific teammates by email, independent of broader project membership - each share grants its own **edit**, **delete**, and **re-share** permissions, so you can give someone read-only-equivalent access to a connection without letting them modify or share it further.+A Database Config can be shared with teammates by email with separate **edit**, **delete**, and **re-share** permissions — useful when someone needs the connection without full project admin rights.## Frequently asked questions<AccordionGroup>- <Accordion title="Can I use the same database configuration across two projects?">- No. A database configuration belongs to the project it was created in and can't be selected from a different project - create a new configuration (pointing at the same or a different ClickHouse instance) in each project that needs one.+ <Accordion title="I changed INIT_DB_HOST in Docker — why is OpenLIT still on the old host?">+ Runtime uses the Database Config row. Update the config in the UI (or create a new one and activate it). `INIT_DB_*` only seeds on first setup.</Accordion>- <Accordion title="Do I need a new ClickHouse instance for every database configuration?">- No. Multiple configurations can point at the same ClickHouse cluster using different database names, or at entirely different clusters - whichever fits your infrastructure.+ <Accordion title="Can one Database Config be used by two projects?">+ No. Create a config in each project (they can point at the same ClickHouse cluster or database name).</Accordion>- <Accordion title="What happens if I open a project with no database configuration?">- OpenLIT detects the missing configuration and walks you through adding one before showing any data pages for that project.+ <Accordion title="Do I need ClickHouse if I only use Tempo + Loki + Prometheus?">+ Yes. External connectors serve raw telemetry reads. ClickHouse remains the app store for derived intelligence and vault-backed connector secrets.</Accordion>- <Accordion title="Is my ClickHouse password visible after I save a configuration?">- No. The password is masked in the API and UI after saving - only entering a new value overwrites it.+ <Accordion title="Is the password visible after save?">+ No. It is masked in the API and UI; only submitting a new value overwrites it.</Accordion></AccordionGroup>++<CardGroup cols={2}>+ <Card title="Connectors" href="/latest/openlit/organisation/connectors" icon="plug">+ Catalog of data-source connectors and actions.+ </Card>+ <Card title="Configuration reference" href="/latest/openlit/configuration" icon="sliders">+ Full list of bootstrap and OAuth environment variables.+ </Card>+</CardGroup>diff --git a/docs/latest/openlit/organisation/environments.mdx b/docs/latest/openlit/organisation/environments.mdxnew file mode 100644index 00000000..a80cc6d2--- /dev/null+++ b/docs/latest/openlit/organisation/environments.mdx@@ -0,0 +1,79 @@+---+title: 'Environments'+sidebarTitle: 'Environments'+description: 'Project environments partition Database Configs, connectors, and signal routing — replacing a single global ClickHouse env-var connection'+icon: 'layer-group'+---++An **environment** is a named partition inside a project (for example `production`, `staging`, or `development`). Database Configs, data-source connectors, and signal bindings are all scoped to an environment so you can point the same OpenLIT project at different backends without redeploying with new env vars.++```text+Organisation+ └── Project+ └── Environment (production | staging | …)+ ├── Database Config (ClickHouse app store)+ ├── Connectors (Tempo, Loki, Prometheus, Jaeger, …)+ └── Signal routing (traces / logs / metrics bindings)+```++<Info>+Day-2 connection changes happen in the UI under the selected environment — not by editing `INIT_DB_*` on the running container. See [Database Config](/latest/openlit/organisation/database-config).+</Info>++## Why environments exist++| Before | After |+| --- | --- |+| One ClickHouse connection from deploy-time `INIT_DB_*` | Many Database Configs, each tagged with an **environment** |+| Changing host meant redeploying OpenLIT | Edit or activate a Database Config for that environment |+| Staging and production mixed in one connection | Switch environment in the header; routing and connectors follow |++## Switch environment++Use the **environment** control in the top navigation (next to organisation and project). Switching environment refreshes:++- Available Database Configs for that environment+- Connectors created in that environment+- Signal bindings for traces, logs, and metrics++## Create or use an environment++Environments are created when you add a Database Config or connector with a new environment name, or from project environment management in Organisation settings. Names must be lowercase letters, numbers, dots, hyphens, or underscores (for example `production`, `us-east.staging`).++<Steps>+ <Step title="Select project">+ Choose the project in the header.+ </Step>+ <Step title="Pick or create environment">+ Select an existing environment, or enter a new name when adding a Database Config / connector.+ </Step>+ <Step title="Attach resources">+ Add a [Database Config](/latest/openlit/organisation/database-config) and optional [connectors](/latest/openlit/organisation/connectors) for that environment.+ </Step>+ <Step title="Bind signals">+ Use [signal routing](/latest/openlit/organisation/signal-routing) so traces, logs, and metrics resolve to the right connector for this environment.+ </Step>+</Steps>++## Relation to Database Config and connectors++- **Database Config** — ClickHouse for this environment (app store + default telemetry). Seeded once from `INIT_DB_*`, then managed in the UI.+- **Connectors** — external backends (Tempo, Loki, Prometheus, Jaeger) also carry an environment.+- **Signal routing** — bindings are unique per **project + signal + environment**.++## Related++<CardGroup cols={2}>+ <Card title="Database Config" href="/latest/openlit/organisation/database-config" icon="database">+ Env vars → ClickHouse Database Config per environment.+ </Card>+ <Card title="Connectors" href="/latest/openlit/organisation/connectors" icon="plug">+ Data-source connectors available in OpenLIT.+ </Card>+ <Card title="Signal routing" href="/latest/openlit/organisation/signal-routing" icon="route">+ Bind traces, logs, and metrics per environment.+ </Card>+ <Card title="Projects" href="/latest/openlit/organisation/projects" icon="folder">+ Project boundaries that own environments.+ </Card>+</CardGroup>diff --git a/docs/latest/openlit/organisation/overview.mdx b/docs/latest/openlit/organisation/overview.mdxindex a66b2cd3..0c8a61f7 100644--- a/docs/latest/openlit/organisation/overview.mdx+++ b/docs/latest/openlit/organisation/overview.mdx@@ -1,74 +1,58 @@---title: 'Organisation Overview'-description: 'Understand how organisations, projects, and database configurations fit together in OpenLIT'+description: 'Organisation → Project → Environment → Database Config and connectors in OpenLIT'icon: 'building'----An organisation in OpenLIT is a shared workspace for a team. It owns projects, and each project owns one or more ClickHouse database configurations - this three-level hierarchy is how OpenLIT separates teams, environments, and data without running separate installations.+Organisations in OpenLIT are shared workspaces for teams. Inside an organisation, **projects** own everything you configure for telemetry backends: environments, Database Configs, connectors, and signal bindings.-```mermaid-flowchart TB;- Org[Organisation] -->|owns| Proj1[Project: production];- Org -->|owns| Proj2[Project: staging];- Proj1 -->|connects to| DB1[ClickHouse database config];- Proj2 -->|connects to| DB2[ClickHouse database config];+```text+Organisation+ └── Project ← create / select a project first+ └── Environment+ ├── Database Config (ClickHouse app store)+ ├── Connectors (Tempo, Loki, Prometheus, Jaeger)+ └── Signal routing (traces / logs / metrics)```-Use organisations as the membership boundary - who can log in and see this workspace at all. Use projects inside an organisation when you need separate database boundaries for the same team, such as production, staging, or per-customer environments.+Use organisations for membership and workspace boundaries. Use [projects](/latest/openlit/organisation/projects) when you need separate data boundaries inside the same organisation. Database Config and Connectors are **project resources** — they appear in the sidebar as documentation topics, but in the product you create them under a project.-## Creating an organisation+## What belongs where-New users are prompted to either create an organisation (just a name) or accept a pending invitation during onboarding. Skipping this step creates a personal organisation automatically, so every user always lands in exactly one organisation - onboarding never leaves you without a workspace.+| Layer | Owns |+| --- | --- |+| **Organisation** | Workspace name, members, invitations; billing/licensing where applicable |+| **Project** | Environments, Database Configs, connectors, bindings |+| **Environment** | Partition for configs, connectors, and per-signal routing |+| **Database Config** | ClickHouse connection on the project (seeded from `INIT_DB_*`, then managed in UI) |+| **Connector** | External backend + credentials + actions on the project |-Every organisation gets a default project the moment it's created, and existing installations are migrated into a default project automatically, so upgrading never breaks an existing database configuration.--## Membership and roles--Every organisation member has one of three roles:--| Role | Description |-|---|---|-| **Owner** | The user who created the organisation. Owner status can't be reassigned or changed. |-| **Admin** | Can invite members, manage projects, and change other members' roles (except the owner's). |-| **Member** | Can use the organisation's projects and data; can't manage membership. |--Invite teammates by email from the Organisation page - each invite is scoped to that organisation, and the invited user accepts or declines it from their own onboarding screen. Removing a member revokes their access to every project in that organisation immediately.--## What belongs to an organisation--- Organisation identity, such as the workspace name.-- Members, their roles, and pending invitations.-- Projects owned by the organisation, including the default project.-- Billing and licensing state where applicable.--## Switching and deleting organisations--If you belong to multiple organisations, use the organisation selector in the top navigation to change the active workspace - project and database configuration selectors refresh to match the newly selected organisation. Deleting an organisation removes every project and database configuration that belongs to it, so it's only available to the owner.+Every organisation has a default project. Existing installations are mapped to a default project and `production` environment so upgrades keep working.## Common tasks<CardGroup cols={2}>- <Card title="Manage projects" href="/latest/openlit/organisation/projects" icon="folder">- Create projects, switch project context, and review project-level structure.+ <Card title="Projects" href="/latest/openlit/organisation/projects" icon="folder">+ Create a project first — Database Configs and connectors live here.+ </Card>+ <Card title="Environments" href="/latest/openlit/organisation/environments" icon="layer-group">+ Partition Database Configs and connectors by environment.</Card>- <Card title="Configure databases" href="/latest/openlit/organisation/database-config" icon="database">- Add, select, and update ClickHouse database configurations inside a project.+ <Card title="Database Config" href="/latest/openlit/organisation/database-config" icon="database">+ Add ClickHouse on a project (seeded from INIT_DB_* then managed in the UI).+ </Card>+ <Card title="Connectors" href="/latest/openlit/organisation/connectors" icon="plug">+ Attach Tempo, Loki, Prometheus, or Jaeger on a project.+ </Card>+ <Card title="Signal routing" href="/latest/openlit/organisation/signal-routing" icon="route">+ Bind traces, logs, and metrics independently per environment.</Card></CardGroup>-## Frequently asked questions+<Frame>+ <img src="/images/organisation/projects-list.png" alt="OpenLIT Organisation Projects tab — manage DB Config per project" />+</Frame>++## Workspace switching-<AccordionGroup>- <Accordion title="What's the difference between an organisation and a project?">- An organisation is the membership boundary - it controls who can access a workspace at all. A project is a database boundary inside an organisation - it controls which ClickHouse connection a given environment or team uses. One organisation can have many projects.- </Accordion>- <Accordion title="What happens if I skip creating an organisation during onboarding?">- OpenLIT creates a personal organisation for you automatically, with a default project already set up, so you're never left without a workspace.- </Accordion>- <Accordion title="Can I change who owns an organisation?">- No. The owner is permanently the user who created the organisation. Admins can manage members and projects, but ownership itself can't be reassigned.- </Accordion>- <Accordion title="What happens to my data if I delete an organisation?">- Deleting an organisation deletes every project and database configuration that belongs to it. Only the owner can delete an organisation.- </Accordion>-</AccordionGroup>+If you belong to multiple organisations, use the organisation selector in the top navigation. Project, environment, database, and connector selectors update based on the selected organisation — the same pattern as the OpenLIT dashboard header.diff --git a/docs/latest/openlit/organisation/projects.mdx b/docs/latest/openlit/organisation/projects.mdxindex 364302c8..08338693 100644--- a/docs/latest/openlit/organisation/projects.mdx+++ b/docs/latest/openlit/organisation/projects.mdx@@ -1,66 +1,86 @@---title: 'Projects'-description: 'Create and manage project boundaries inside an OpenLIT organisation'+description: 'Create projects — they own environments, Database Configs, connectors, and signal routing'+sidebarTitle: 'Projects'icon: 'folder'----A project is a database boundary inside an organisation. Every project connects to its own ClickHouse database configuration, so one organisation can run several isolated environments - production, staging, per-customer, or per-team - without needing separate organisations or separate OpenLIT installations.+Everything under **Organisation → Projects** is project-scoped. You create a project first; then that project owns environments, ClickHouse **Database Configs**, data-source **connectors** (Tempo, Loki, Prometheus, Jaeger), and **signal routing**.++```text+Organisation+ └── Project ← create this first+ ├── Environments+ ├── Database Configs (ClickHouse)+ ├── Connectors (Tempo / Loki / Prometheus / Jaeger)+ └── Signal routing+```++<Info>+Database Config is not a separate top-level product area. It is a resource you create **inside a project** (same for connectors and bindings).+</Info>## When to create a projectCreate a project for:-| Scenario | Why a project helps |-|---|---|-| Production and staging | Keep live traces and test traces in separate ClickHouse databases. |-| Multiple teams, one organisation | Each team gets its own project without splitting membership across organisations. |-| Customer-specific deployments | Isolate a customer's data in its own database configuration while keeping shared org-level billing and membership. |-| Different ClickHouse clusters | Route one workload to a different ClickHouse host or cluster than another, under the same organisation. |+- Separate teams that share the same organisation+- Customer-specific deployments+- Workloads that should use different ClickHouse Database Configs++Use **environments** (`production`, `staging`, …) inside a project when you need multiple backend targets without splitting projects. See [Environments](/latest/openlit/organisation/environments).## Create a project+<Frame>+ <img src="/images/organisation/projects-list.png" alt="OpenLIT projects list under Organisation" />+</Frame>+<Steps><Step title="Open Organisation">Select **Organisation** from the sidebar.</Step><Step title="Review existing projects">- Use the projects list to see the projects already available in the organisation.+ Use the projects list to see projects already available in the organisation.</Step><Step title="Create project">- Click **Create project**, enter a project name, and submit the form.+ Click **Create project**, enter a project name, and submit.</Step><Step title="Select project">- Use the project selector in the top navigation to make the project active.+ Use the project selector in the top navigation to make the project active. Then pick an [environment](/latest/openlit/organisation/environments).</Step></Steps>-Every organisation always has exactly one **default project**, created automatically the moment the organisation itself is created. If an organisation has no other database configuration set up yet, OpenLIT falls back to the default project so there's always a working project to land in.+## What a project contains-## Project details+| Resource | Description |+| --- | --- |+| **Environments** | Named partitions (`production`, `staging`, …) |+| **Database Configs** | ClickHouse app-store connections per environment |+| **Connectors** | Tempo, Loki, Prometheus, Jaeger (and ClickHouse as built-in) |+| **Signal bindings** | Which connector serves traces / logs / metrics per environment |-Project details show the project name, status, and database configurations attached to that project. Open a project from the Organisation page to review or update its configuration.+<Frame>+ <img src="/images/organisation/project-header.png" alt="OpenLIT header with organisation, project, environment, and database selectors" />+</Frame>-## Switching projects+## Member access-The project selector in the top navigation scopes everything you see - traces, dashboards, prompts, evaluations - to the currently active project's database configuration. Switching projects re-fetches the active database configuration for the new project; if that project has none configured yet, OpenLIT walks you through adding one before showing data pages.+Project access controls which organisation members can use a project. Existing organisation members are assigned to existing projects during migration so current installations continue to work. New members can be invited to the organisation first and then granted access to specific projects as needed.-## Member access+## Related-Project access controls which organisation members can use a project. Existing organisation members are assigned to existing projects during migration so current installations continue to work. New members can be invited to the organisation first and then granted access to specific projects as needed. A member without access to a project can't select it or see its database configurations, even if they're a member of the parent organisation.--## Frequently asked questions--<AccordionGroup>- <Accordion title="Do I need multiple projects if I only have one environment?">- No. Every organisation ships with a default project, and most single-environment setups never need to create another one.- </Accordion>- <Accordion title="Can a project use more than one database configuration?">- Yes. See [Database Config](/latest/openlit/organisation/database-config) for adding and switching between multiple ClickHouse configurations inside the same project.- </Accordion>- <Accordion title="What happens if I switch to a project with no database configured?">- OpenLIT detects the missing configuration and guides you through adding one before showing any data pages for that project.- </Accordion>- <Accordion title="Can every organisation member see every project?">- No. Project access is assigned per member. A member only sees and can select the projects they've been granted access to.- </Accordion>-</AccordionGroup>+<CardGroup cols={2}>+ <Card title="Environments" href="/latest/openlit/organisation/environments" icon="layer-group">+ How environments partition configs and routing.+ </Card>+ <Card title="Database Config" href="/latest/openlit/organisation/database-config" icon="database">+ Add ClickHouse connections on the project.+ </Card>+ <Card title="Connectors" href="/latest/openlit/organisation/connectors" icon="plug">+ Attach Tempo, Loki, Prometheus, or Jaeger.+ </Card>+ <Card title="Signal routing" href="/latest/openlit/organisation/signal-routing" icon="route">+ Bind traces, logs, and metrics per environment.+ </Card>+</CardGroup>diff --git a/docs/latest/openlit/organisation/signal-routing.mdx b/docs/latest/openlit/organisation/signal-routing.mdxnew file mode 100644index 00000000..1988f574--- /dev/null+++ b/docs/latest/openlit/organisation/signal-routing.mdx@@ -0,0 +1,125 @@+---+title: 'Signal routing'+sidebarTitle: 'Signal routing'+description: 'Route traces, logs, and metrics to different OpenLIT connectors per project and environment'+icon: 'route'+---++**Signal routing** decides which [connector](/latest/openlit/organisation/connectors) serves each telemetry signal for a project [environment](/latest/openlit/organisation/environments). Bindings are independent: traces can come from Tempo while logs come from Loki and metrics from Prometheus — in the same OpenLIT project.++## Signals++| Signal | Typical connectors | Notes |+| --- | --- | --- |+| **traces** | ClickHouse, Tempo, Jaeger | Powers request views, agent materialization, eval candidate discovery |+| **logs** | ClickHouse, Loki | Correlated when the backend supports trace id / service |+| **metrics** | ClickHouse, Prometheus | PromQL backends support server aggregation |+| **intelligence** | ClickHouse (Database Config) | Derived store — evals, boards metadata, rules, vault — not rebound to external vendors |++<Warning>+Changing a binding changes **where OpenLIT reads**. It does not migrate historical data between backends. Ensure the new connector already holds the telemetry you expect.+</Warning>++## Bind a signal++<Steps>+ <Step title="Open Connectors">+ Go to **Organisation → Project → Connectors** (data sources).+ </Step>+ <Step title="Choose environment">+ Bindings are unique per **project + signal + environment**. Match the environment on your Database Config and connectors.+ </Step>+ <Step title="Add or select a connector">+ Create an atomic connector from the supported catalog, then run **health check** / **AI validate** as needed.+ </Step>+ <Step title="Bind the signal">+ Assign **traces**, **logs**, and/or **metrics** to connectors that declare those capabilities. A metrics-only connector cannot be bound to traces.+ </Step>+</Steps>++When you add a Database Config, OpenLIT can seed default bindings so all signals point at that ClickHouse until you rebind them.++## Resolution order++For every read, OpenLIT resolves the source in this order and **never** returns a connector that does not serve the requested signal:++<Steps>+ <Step title="Explicit override">+ Widget or API `sourceId` (mixed dashboards) wins when present and project-scoped. Use `builtin:<databaseConfigId>` for ClickHouse.+ </Step>+ <Step title="Per-signal binding">+ The binding for this project, signal, and environment.+ </Step>+ <Step title="Built-in ClickHouse">+ Active [Database Config](/latest/openlit/organisation/database-config) for the environment (when no explicit environment fail-closed applies).+ </Step>+</Steps>++If nothing serves the signal, OpenLIT shows a first-class **no source for this signal** state instead of silently reading the wrong backend.++```mermaid+flowchart TD+ req[Read traces/logs/metrics] --> ovr{Widget sourceId?}+ ovr -->|yes| adapter[Create adapter]+ ovr -->|no| bind{Signal binding?}+ bind -->|yes| adapter+ bind -->|no| ch{Database Config?}+ ch -->|yes| adapter+ ch -->|no| empty[No source state]+ adapter --> backend[Vendor or ClickHouse]+```++## Mixed dashboards++Each dashboard widget can optionally target its own connector and signal. One board can render Tempo traces next to Prometheus metrics while evaluation results still come from ClickHouse intelligence.++## Correlation across backends++Only ClickHouse holds every signal in one store for **full** correlation. When signals are split:++- Joins use declared keys (`service.name`, trace/span id, `coding_agent.session.id`).+- Features that need correlatable logs/metrics no-op gracefully when the bound source cannot join.+- The UI asks you to connect a correlatable source instead of returning incorrect merges.++## Natural-language / raw SQL++Raw ClickHouse SQL (NL chat and raw-SQL widgets) requires the built-in ClickHouse path. On external connectors, use structured Telemetry, trace, and dashboard views.++## What never leaves ClickHouse++Regardless of signal routing:++- Evaluation results and scoring metadata+- Dashboard / board definitions and widget layout metadata+- Rules, vault secrets materialization, controller-derived intelligence++Raw OTLP **write** paths (SDK exporters, collectors) are separate from these **read** connectors. To mirror only AI telemetry into ClickHouse, see the [OTel Collector AI filter recipe](https://github.com/openlit/openlit/blob/main/assets/otel-collector-ai-filter.yaml).++## Permissions and audit++Enterprise roles control connector management:++| Permission | Typical use |+| --- | --- |+| `connectors:read` | View connectors and current signal bindings |+| `connectors:create` / `update` / `delete` | Add, edit, or remove connectors |+| `connectors:test` | Run health check / AI validation |+| `connectors:bind` | Bind, rebind, or clear a signal for an environment |++Owner and admin built-in roles include the full connector set. Members can read observability data but do not manage connectors by default.++Binding and unbinding are written to the organisation **audit log** (`connectors.connector_bound` / `connectors.connector_unbound`) with signal, environment, next connector, and previous source when switching.++## Related++<CardGroup cols={2}>+ <Card title="Connectors catalog" href="/latest/openlit/organisation/connectors" icon="plug">+ Supported OpenLIT + OpenPlait connectors and actions.+ </Card>+ <Card title="Environments" href="/latest/openlit/organisation/environments" icon="layer-group">+ How bindings are partitioned by environment.+ </Card>+ <Card title="Database Config" href="/latest/openlit/organisation/database-config" icon="database">+ ClickHouse app store and INIT_DB_* bootstrap.+ </Card>+</CardGroup>diff --git a/docs/latest/openlit/organisation/telemetry-sources.mdx b/docs/latest/openlit/organisation/telemetry-sources.mdxnew file mode 100644index 00000000..0e8a504c--- /dev/null+++ b/docs/latest/openlit/organisation/telemetry-sources.mdx@@ -0,0 +1,29 @@+---+title: 'Telemetry Sources'+sidebarTitle: 'Telemetry Sources'+description: 'Telemetry Sources is the earlier name for OpenLIT Connectors — atomic backends, signal bindings, and AI read paths'+icon: 'plug'+---++**Telemetry Sources** is the same product surface as **Connectors**. Prefer the connectors and signal-routing guides below — they match the OpenLIT dashboard language (connectors, actions, and per-signal routing).++<CardGroup cols={2}>+ <Card title="Connectors" href="/latest/openlit/organisation/connectors" icon="plug">+ Catalog of supported OpenLIT + OpenPlait data-source connectors and actions.+ </Card>+ <Card title="Signal routing" href="/latest/openlit/organisation/signal-routing" icon="route">+ How traces, logs, and metrics are bound per project and environment.+ </Card>+ <Card title="Environments" href="/latest/openlit/organisation/environments" icon="layer-group">+ How Database Configs and connectors are partitioned.+ </Card>+ <Card title="Database Config" href="/latest/openlit/organisation/database-config" icon="database">+ ClickHouse app store and how INIT_DB_* env vars became Database Config.+ </Card>+</CardGroup>++## Quick summary++OpenLIT reads raw traces, logs, and metrics from the built-in ClickHouse store by default. You can attach external backends supported in open-source OpenLIT — **Tempo**, **Loki**, **Prometheus**, and **Jaeger** — and bind each signal independently per [environment](/latest/openlit/organisation/environments). Derived intelligence always stays in ClickHouse via [Database Config](/latest/openlit/organisation/database-config).++For auth tables, AI selectors, correlation limits, and reliability hardening, see the [Connectors](/latest/openlit/organisation/connectors) page — that document is the canonical reference.diff --git a/docs/style.css b/docs/style.cssnew file mode 100644index 00000000..12a2ce89--- /dev/null+++ b/docs/style.css@@ -0,0 +1,43 @@+/**+ * OpenLIT docs visual language — aligned with the dashboard+ * (stone surfaces, #F36C06 primary, Inter, compact cards).+ */++:root {+ --openlit-primary: #f36c06;+ --openlit-primary-hover: #d45a05;+ --openlit-stone-50: #fafaf9;+ --openlit-stone-100: #f5f5f4;+ --openlit-stone-200: #e7e5e4;+ --openlit-stone-700: #44403c;+ --openlit-stone-800: #292524;+ --openlit-stone-900: #1c1917;+ --openlit-stone-950: #0c0a09;+}++html.dark body,+html[data-theme="dark"] body {+ background-color: var(--openlit-stone-950);+}++#navbar {+ border-bottom-color: color-mix(in srgb, var(--openlit-stone-200) 70%, transparent);+}++html.dark #navbar,+html[data-theme="dark"] #navbar {+ border-bottom-color: color-mix(in srgb, var(--openlit-stone-800) 80%, transparent);+}++/* Card-like surfaces closer to dashboard settings panels */+[data-component-name="card"],+.card {+ border-radius: 0.5rem;+}++/* Soften callouts to stone + primary accent */+.tip,+.note,+.info {+ border-radius: 0.5rem;+}diff --git a/external_o11y.md b/external_o11y.mdnew file mode 100644index 00000000..88b21dad--- /dev/null+++ b/external_o11y.md@@ -0,0 +1,575 @@+# External Observability Data Sources — Branch Handoff++**Audience:** developers picking up this branch.+**Goal:** make raw AI telemetry pluggable (Grafana LGTM today; any OTLP vendor later) while OpenLIT keeps derived/eval/agent data in its own ClickHouse app store.++This document describes **what landed on this branch**, how the pieces fit together, and where to extend safely. For a short “add a vendor” checklist, see `src/client/src/lib/platform/datasource/README.md`.++---++## Executive summary++Before this branch, ~286 call sites executed raw ClickHouse SQL via `dataCollector()`. Observability reads were implicitly tied to the project’s `DatabaseConfig` ClickHouse.++This branch introduces:++1. A **normalized datasource layer** (`src/client/src/lib/platform/datasource/`) with a `DataSourceAdapter` contract, vendor adapters, and a **QueryPlanner** (L0 native / L1 sample / L2 rollup).+2. **Project-scoped telemetry source config** in Postgres (`TelemetrySource`, `TelemetrySourceBinding`) with secrets in the vault.+3. **Read facades** per signal (`traces/read.ts`, `logs/read.ts`, `metrics/read.ts`) that route built-in ClickHouse unchanged and external vendors through adapters + denormalization back to the row shapes the UI already expects.+4. **Grafana-parity performance**: push-down aggregation, pixel-bounded downsampling, query budgets, per-source concurrency/retry, short TTL caches, stale-while-revalidate polling, and L2 rollups for dashboard KPIs.+5. **Descriptor-driven configuration**: each vendor’s `describe()` returns `configFields`, `authStyle`, optional `stackTemplate` — no per-vendor UI switches.+6. **Surface migrations**: Telemetry, Agents (materializer + graph + monitoring tab), Evals (auto-eval candidates), Dashboards (structured query builder + per-widget `sourceId`), LLM dashboard widgets (external path via `llm/external.ts`).++**Important:** configuring an external source does **not** automatically reroute every legacy SQL path. Only surfaces that call the facades / `getTelemetryAdapter` / `runWidgetQuery` with structured queries are adapter-aware. Built-in ClickHouse behavior is preserved.++---++## Architectural split (unchanged principle, now enforced)++| Store | Role | What lives here |+|-------|------|-----------------|+| **App / derived store** | OpenLIT-owned ClickHouse (`DatabaseConfig`) | `openlit_agents_summary`, `openlit_agent_versions`, `openlit_evaluation`, dashboards, vault, controller tables, **and new L2 rollup tables** |+| **Telemetry source** | Pluggable per project (and per widget) | Raw `otel_*` when built-in; vendor APIs when external |++Only **raw telemetry reads** go through adapters. Derived writes/reads stay on OpenLIT ClickHouse. Moving `openlit_*` to Postgres is explicitly out of scope.++### L2 rollup tables (new on this branch)++Created by ClickHouse migrations (`create-telemetry-rollups-migration.ts`, `alter-telemetry-rollups-dimensions-migration.ts`):++| Table | Purpose |+|-------|---------|+| `openlit_signal_buckets` | Time-bucketed request/cost/token rollups per `source_id` + service + environment |+| `openlit_llm_rollups` | Grouped LLM dimensions (model, provider, application, …) for dashboard KPIs |+| `openlit_external_span_cache` | Short-lived recent-span hot cache for list views (TTL ~2h, row cap) |++Rollups are **not** a full vendor mirror. Materialization samples via adapters and writes bounded aggregates. QueryPlanner prefers L2 when fresh (`ROLLUP_FRESHNESS_MS` = 5m) and filters allow (`rollup-policy.ts` — version-scoped queries still fall back to L1).++---++## End-to-end data flow (as built)++```mermaid+flowchart TD+ subgraph ui [UI surfaces]+ Tel[Telemetry pages]+ AgMon[Agent monitoring tab]+ AgMat[Agents materializer]+ Ev[Evals auto-eval]+ Dash[Dashboard widgets]+ LLM[LLM dashboard widgets]+ end++ subgraph config [Configuration]+ TS[TelemetrySource Postgres]+ Bind[TelemetrySourceBinding per signal]+ Vault[openlit_vault secrets]+ Desc[SourceTypeDescriptor.describe]+ end++ subgraph resolve [Resolution]+ Resolver[telemetry-source.ts]+ CRUD[telemetry-source-crud.ts]+ end++ subgraph read [Read path]+ Facade[traces/logs/metrics read.ts]+ Widget[widget.ts runWidgetQuery]+ LLMExt[llm/external.ts]+ end++ subgraph plan [Compute tiers]+ QP[query-planner.ts]+ L0[L0 native vendor aggregation]+ L1[L1 sample-fetch + sample-aggregate]+ L2[L2 rollups.ts]+ end++ subgraph ds [Datasource module]+ Reg[registry + bootstrap]+ Adapters[ClickHouse + vendor adapters]+ Safe[safe-fetch + limits + cache]+ end++ ui --> Facade+ ui --> Widget+ ui --> LLMExt+ Facade --> Resolver+ Widget --> Resolver+ LLMExt --> Resolver+ Resolver --> TS+ Resolver --> Bind+ TS --> Vault+ CRUD --> Desc+ Facade --> QP+ Widget --> QP+ LLMExt --> QP+ QP --> L0+ QP --> L1+ QP --> L2+ L0 --> Adapters+ L1 --> Adapters+ L2 --> CH[(OpenLIT ClickHouse app store)]+ Adapters --> Safe+ Safe --> Vend[(External vendor APIs)]+ Adapters --> CH+ Reg --> Adapters+```++---++## Configuration model++### Postgres (`prisma/schema.prisma`)++**`TelemetrySource`** (project-scoped):++- `type` — free string (`tempo`, `loki`, `mimir`, `datadog`, …). **No enum** — new vendors need no migration.+- `signals` — comma-separated subset of `traces,logs,metrics`.+- `settings` — JSON text (URLs, site, region, `allowHttp`, tenant, …). **Never secrets.**+- `secretRef` — vault id for API keys / basic auth.+- `isDefault` — optional project default source.++**`TelemetrySourceBinding`** — Grafana-style **per-signal routing**:++- At most one binding per `(projectId, signal)`.+- Lets traces come from Tempo while logs come from Loki and metrics from Mimir.++Migrations: `20260709130000_telemetry_source`, `20260709150000_telemetry_source_binding`.++### Built-in ClickHouse source++Every project still has an implicit built-in source from `DatabaseConfig`:++- Descriptor id: `builtin:<dbConfigId>`+- `isBuiltIn: true`, `type: "clickhouse"`+- Used when no external binding exists for a signal.++### Resolution precedence (`telemetry-source.ts`)++For a read with optional overrides:++1. Explicit `sourceId` (dashboard widget, query-builder metadata) — must belong to current project.+2. `TelemetrySourceBinding` for the requested signal.+3. Project default `TelemetrySource` (`isDefault`).+4. Built-in ClickHouse (`DatabaseConfig`).++`getTelemetryAdapter({ signal, sourceId })` returns a bound `DataSourceAdapter` instance.++### Stack umbrellas (Grafana / Victoria)++`stacks.ts` defines **internal descriptor-only** factories (`internal: true` + `stackTemplate`):++- **Grafana stack** → Tempo + Loki + Mimir slots.+- **Victoria stack** → VictoriaLogs + VictoriaMetrics slots.++`createSourceStack()` in `telemetry-source-crud.ts` expands a template into atomic per-signal `TelemetrySource` rows + bindings. Umbrellas are **not** stored as a single adapter row.++### Management UI++- **Project settings → Data Sources** (`data-sources-page.tsx`, embedded in `project-page.tsx`).+- Forms render **`configFields` from `SourceTypeDescriptor`** — no `type === "datadog"` switches.+- `authStyle` / `authHelp` / `docsUrl` drive credential hints and 401 troubleshooting.+- Stack builder uses the same descriptor-driven fields per slot.++### API routes++| Route | Purpose |+|-------|---------|+| `GET/POST /api/telemetry-source` | List/create sources; GET includes `signalCapabilities` |+| `GET/PATCH/DELETE /api/telemetry-source/[id]` | CRUD + update |+| `POST /api/telemetry-source/[id]/health` | Adapter health check |+| `GET/PUT/DELETE /api/telemetry-source/binding` | Per-signal bindings |+| `POST /api/telemetry-source/stack` | Create multi-signal stack from template |++---++## Datasource module — file map++```+src/client/src/lib/platform/datasource/+├── types.ts # OpenLITQuery, DataFrame, SourceCapabilities, SourceTypeDescriptor, FieldDef+├── registry.ts # Factory registration; listSourceTypeDescriptors({ includeInternal })+├── bootstrap.ts # Registers ClickHouse + all CE vendor factories + stack umbrellas+├── enterprise.ts # getExternalDataSourceAdapters() → [] in CE (EE hook for extra vendors)+├── base-adapter.ts # BaseExternalAdapter (auth, network opts, capability throws)+├── facade.ts # resolveSignalReadContext, facadeErrorMessage (+ columnar follow-up note)+├── ai-selector.ts # AITelemetrySelector → vendor-native filters+├── selector-match.ts # Post-filter when vendor can't express full selector+├── config-fields.ts # httpVendorFields(), endpointField(), tenantField(), …+├── stacks.ts # grafana / victoria umbrella descriptors+├── query-planner.ts # planAndAggregateSpans, planAndSpanTimeSeries, planAndDistinctValues+├── l1-compute.ts # compute*L1 wrappers over sample-fetch + sample-aggregate+├── rollup-policy.ts # shouldPreferRollup (version filters → skip L2)+├── downsample.ts # Grafana-parity maxDataPoints → interval/step math+├── query-observability.ts # debug logging per adapter read (OPENLIT_QUERY_OBSERVABILITY)+├── otlp-json.ts # OTLP trace/log JSON normalization (Tempo/Jaeger)+├── clickhouse/+│ ├── adapter.ts # Reference DataSourceAdapter (delegates to existing SQL)+│ ├── query-map.ts # MetricParams → OpenLITQuery+│ └── normalize.ts # Normalized* → ClickHouse-shaped rows (facade denormalization)+├── datadog/ # Traces + logs + metrics+├── newrelic/ # NRQL via NerdGraph+├── grafana/+│ ├── tempo.ts # TraceQL + TraceQL metrics aggregation + span index+│ ├── loki.ts # LogQL+│ └── prometheus.ts # PromQL (Prometheus + Mimir factories)+├── jaeger/ # L1-heavy traces+├── victoria/ # VictoriaLogs + VictoriaMetrics+├── graph/+│ ├── sample-fetch.ts # Bounded trace sampling + multi-service stratification+│ ├── sample-aggregate.ts+│ ├── map-pool.ts # Bounded parallel trace downloads+│ └── aggregate-dag.ts # Agent graph DAG from normalized spans+└── http/+ ├── safe-fetch.ts # SSRF default-deny, redirect re-validation, timeouts+ ├── limits.ts # clampQueryBudget, concurrency, retry+ ├── cache.ts # Per-source query cache (range aligned to step)+ ├── secret.ts # Vault decrypt for adapter auth+ └── auth-headers.ts # Basic / Bearer / tenant headers+```++**Related (outside `datasource/`):**++| Path | Role |+|------|------|+| `lib/telemetry-source.ts` | Descriptor resolution, adapter factory binding |+| `lib/telemetry-source-crud.ts` | CRUD, bindings, stack create, `resolveProjectSignalCapabilities()` |+| `lib/platform/traces/read.ts` | Traces facade (list, detail, hierarchy, summary, grouped, config) |+| `lib/platform/logs/read.ts` | Logs facade |+| `lib/platform/metrics/read.ts` | Metrics facade |+| `lib/platform/telemetry/rollups.ts` | L2 write/read + hot cache |+| `lib/platform/llm/external.ts` | LLM dashboard widgets via QueryPlanner for external traces |+| `lib/platform/manage-dashboard/widget.ts` | `runWidgetQuery` + structured execution + observability logging |+| `utils/hooks/useFetchWrapper.ts` | Stale-while-revalidate + monotonic requestId |+| `utils/telemetry-request-cache.ts` | Browser-side short TTL cache for telemetry POSTs |+| `utils/hooks/useSignalCapabilities.ts` | Client capability gating from `/api/telemetry-source` |++---++## Normalized query contract++### `Signal`++`"traces" | "logs" | "metrics"`++### `AITelemetrySelector`++Shared “only AI telemetry” predicate (`ai-selector.ts`): `gen_ai.*`, `coding_agent.*`, `gen_ai.agent.*`, and/or `telemetry.sdk.name = openlit`. Each adapter translates to TraceQL / LogQL / PromQL / NRQL / Datadog query string / Jaeger tags.++### `OpenLITQuery`++Vendor-agnostic query:++- `signal`, `timeRange`, `filters` (normalized attribute predicates)+- `groupBy`, `aggregations`, `sort`, `limit`, `offset`+- `aiSelector` — boolean or structured selector+- `interval` — optional explicit bucket+- `maxDataPoints` — pixel-bounded downsampling (Grafana `maxDataPoints` parity)++Built from UI filters via `metricParamsToOpenLITQuery()` in `clickhouse/query-map.ts`.++### `DataFrame` / `DataFrameMeta`++Columnar result: `fields`, `rows`, `meta`:++- `truncated`, `latencyMs`, `rowsScanned`, `freshness` (`live` | `sampled` | `accelerated`)+- `degraded` — e.g. `serverAggregation`, `rollup`++Facades **denormalize** to existing ClickHouse row shapes so React tables did not need a wholesale rewrite. **Follow-up:** migrate UI to consume `DataFrame` directly (noted in `facade.ts`).++### `DataSourceAdapter`++Methods gated by `capabilities()`:++| Area | Methods |+|------|---------|+| Traces | `listSpans`, `getSpan`, `getTraceSpans`, `aggregateSpans`, `spanTimeSeries`, `distinctValues`, `attributeKeys`, `discoverServices`, `aggregateByService`, `sampleTracesForGraph` |+| Logs | `listLogs`, `getLog`, `logTimeSeries` |+| Metrics | `listMetricSeries`, `metricTimeSeries`, `metricNames` |+| Ops | `healthCheck`, `validateAISignal` |++Unsupported methods throw `UnsupportedCapabilityError` → UI shows honest “not supported” states.++### `SourceCapabilities` (per instance)++| Flag | Meaning |+|------|---------|+| `traceTree` | Full parent/child hierarchy |+| `spanEvents` | Prompt/completion payloads (chat view) |+| `serverAggregation` | Native group-by + aggregates (L0) |+| `distinctValues` | Filter dropdown enumeration |+| `crossTraceSession` | Multi-trace session stitching |+| `rawQuery` | ClickHouse SQL escape hatch |+| `maxLookbackMs` | Vendor retention clamp (used in `clampQueryBudget`) |++---++## Registered adapters (CE bootstrap)++| Type | Signals | Aggregation tier | Notes |+|------|---------|------------------|-------|+| `clickhouse` | all | L0 (SQL) | Built-in reference; full capabilities |+| `tempo` | traces | L0 metrics + L1 lists | TraceQL; process-wide span index keyed by `sourceId` (FIFO cap 5k) |+| `loki` | logs | push-down volume | LogQL; `meta.truncated` on list cap |+| `prometheus` / `mimir` | metrics | L0 range/instant | PromQL; step from `downsample.ts` |+| `datadog` | all | L0 | Site + API/app keys |+| `newrelic` | all | L0 NRQL | Region + account + API key |+| `jaeger` | traces | L1 primary | Service-stratified sampling |+| `victorialogs` | logs | list + volume | LogsQL NDJSON |+| `victoriametrics` | metrics | PromQL-compatible | |+| `grafana` / `victoria` | — | — | Internal stack templates only |++EE may add more via `getExternalDataSourceAdapters()` without CE importing `@/ee/**`.++---++## QueryPlanner — L0 / L1 / L2++**Never branch on vendor name in UI or facades.** Use `query-planner.ts`:++```+L0 native → adapter.capabilities().serverAggregation+ (ClickHouse SQL, Datadog, NRQL, Tempo TraceQL metrics, PromQL)++L2 rollup → openlit_signal_buckets / openlit_llm_rollups when fresh+ (dashboard KPIs, agent widgets without version scope)++L1 sample → sample-fetch (bounded traces) + sample-aggregate (in-process)+ (Jaeger, Tempo list/summary when native agg unavailable)+```++### L1 sampling highlights (`graph/sample-fetch.ts`)++- **Caps:** 100 traces (list), 200 (aggregate), shared 45s TTL cache per source.+- **Multi-service stratification:** discovers services, fetches per-service samples so one high-volume app cannot dominate the telemetry table (fixes “only OpenAI app traces” after agent scoping/polling).+- **Jaeger** marks `samplesAreServiceStratified` to avoid double-stratification.++### Query budgets (`http/limits.ts`)++Every structured external query passes `clampQueryBudget()`:++- `maxRows` (default 5000)+- `maxRangeMs` (default 30d)+- `maxLookbackMs` from adapter capabilities (vendor retention)++Widget path: `widget.ts` → `executeStructuredWidgetQuery`.++---++## Performance & UX (Grafana parity)++| Mechanism | Where | What it does |+|-----------|-------|--------------|+| **Push-down aggregation** | Tempo TraceQL metrics, PromQL, NRQL, CH SQL | Never pull raw spans to aggregate in Node when L0 exists |+| **Pixel downsampling** | `downsample.ts`, Tempo/Prometheus step calc, `query-map.ts` | `intervalMs = max(range/maxDataPoints, minInterval)`, nice-step rounding |+| **Range alignment** | `alignRangeToStep`, `http/cache.ts` | Identical poll windows dedupe cache keys |+| **Per-source concurrency + retry** | `safe-fetch` + `limits.ts` | `concurrencyKey`, exponential backoff on 429/5xx |+| **Per-query timeouts** | Adapter `safeFetch` opts | e.g. Loki 20s, New Relic 35s |+| **X-Query-Tags** | Loki, Tempo, New Relic | Ops guardrails (`source=openlit,type=…`) |+| **Query observability** | `query-observability.ts` | `console.debug` line: source, signal, rows, latency, freshness |+| **Server L1 sample cache** | `sample-fetch.ts` | 45s TTL shared across summary/grouped/config |+| **Browser telemetry cache** | `telemetry-request-cache.ts` | 45s TTL, quantized time keys, coalesced in-flight |+| **Stale-while-revalidate** | `useFetchWrapper.ts` | Keep last data during poll; skeleton only on first load; monotonic `requestId` drops stale responses |+| **L2 rollups + hot cache** | `telemetry/rollups.ts` | Dashboard/agent KPI acceleration |++---++## Per-surface migration status++### Telemetry (`/telemetry`, `/api/metrics/**`, `/api/telemetry/**`)++| Operation | Facade | External path |+|-----------|--------|---------------|+| Trace list | `traces/read.ts` → `listTraceRecords` | Hot cache → stratified `fetchSpansForList` |+| Trace detail / hierarchy | `getTraceSpanRecord`, `getTraceHierarchy` | `getTraceSpans` + tree build |+| Summary charts | `getTracesSummary` | `planAndSpanTimeSeries` + L2 |+| Filter config | `getTraceFilterConfig` | `discoverServices` + shared L1 sample |+| Attribute keys | `getTraceAttributeKeys` | `adapter.attributeKeys` |+| Logs list | `logs/read.ts` | `listLogs` + truncation-aware total |+| Metrics list | `metrics/read.ts` | `listMetricSeries` → grouped rows |++Routes updated to call facades instead of raw `lib/platform/request` / `observability` for these paths.++**Capability gating (UI):**++- `useSignalCapabilities()` ← `resolveProjectSignalCapabilities()` on GET `/api/telemetry-source`+- `span-hierarchy-explorer.tsx` — chat view gated on `spanEvents`+- `structured-query-builder.tsx` — `capabilityAware` uses live attribute keys; static AI fallback only for built-in CH++### Agents++| Component | Change |+|-----------|--------|+| `materialize.ts` | Reads via adapter (`discoverServices`, sampling); **writes** still to `openlit_agents_summary` |+| `aggregate-graph.ts` | External DAG via `sampleTracesForGraph` + `aggregate-dag.ts` |+| `agent-monitoring-tab.tsx` | Full multi-signal observability (traces/exceptions/metrics/logs) scoped by `AgentScopeProvider` |+| `agent-scope-provider.tsx` | Continuous scope lock (service + optional version); `scopeReady` gate prevents unscoped widget fetch; restores on unmount |++**Agent scoping fixes on this branch:**++- Global telemetry page strips stale agent scope when not under `AgentScopeProvider`.+- Widget/dashboard queries respect `serviceNames` / `versionFilter` in `selectedConfig`.+- Multi-service trace list stratification prevents one agent poll from narrowing the global telemetry view.++### Evals (`evaluation/index.ts`)++- Auto-eval candidate fetch uses traces adapter `listSpans` when external; excludes ids already in `openlit_evaluation`.+- Eval **results** still stored in OpenLIT ClickHouse.++### Dashboards++| Feature | Implementation |+|---------|----------------|+| Structured query builder | `structured-query-builder.tsx` — signal/mode/filters/groupBy/aggregations |+| Per-widget source | `edit-widget-sheet.tsx` — `sourceId` on widget config |+| Metadata fetches | Query builder passes `sourceId` to `/api/metrics/request/config` and `attribute-keys` |+| Query execution | `runWidgetQuery` → `executeStructuredWidgetQuery` → QueryPlanner |+| Legacy SQL widgets | ClickHouse raw SQL still works; external project traces infer structured query via `widget-sql-bridge.ts` |+| LLM seeded dashboards | `llm/*.ts` delegates to `llm/external.ts` when traces binding is external |++### Data Sources admin UI++`data-sources-page.tsx` (~900 lines): list, add/edit, test connection, bindings, stack wizard — all descriptor-driven.++---++## Security++### SSRF (`http/safe-fetch.ts`)++- **Default deny:** https-only, no credentials-in-URL, metadata IPs/hostnames always blocked.+- **Self-hosted opt-in:** `allowHttp` + `allowPrivateNetwork` from source settings (for Tempo/Loki beside OpenLIT).+- **Redirects:** manual follow with re-validation on every hop (blocks redirect-to-internal bypass).+- **Secrets:** decrypted server-side only; redacted in error messages.++### CE/EE boundary++- All vendor adapters above ship in **CE** (`bootstrap.ts`).+- `getExternalDataSourceAdapters()` in `enterprise.ts` returns `[]` in CE — EE adds private vendors without `@/ee/**` imports in shared code.+- User strings in `constants/messages/en.ts` (not hard-coded in adapters/UI).++---++## Denormalization contract (important for new adapters)++External adapters produce **normalized** types (`NormalizedSpan`, `NormalizedLog`, `NormalizedMetricPoint`).++Facades convert to **ClickHouse-shaped rows** via `clickhouse/normalize.ts` so existing React components keep working. When adding a vendor:++1. Implement normalized adapter methods.+2. Ensure denormalizers map to the same field names the UI expects (`SpanName`, `ServiceName`, `Body`, …).+3. Long-term: UI should consume `DataFrame` directly (documented follow-up in `facade.ts`).++---++## Known limitations & follow-ups++| Item | Status |+|------|--------|+| Dash0 adapter | **Not built** — extensibility proven via fictional descriptor test + README checklist |+| Version-scoped L2 rollups | Version filters skip L2 (`rollup-policy.ts`); L1 only |+| Sub-trace span paging | Not implemented (Grafana also loads full trace on open) |+| Grafana Live / WebSockets | Out of scope; polling + chunked fetch instead |+| Legacy SQL call sites | Not all ~286 `dataCollector` sites migrated — only observability/agent/dashboard/eval paths on facades |+| `tsc` in test files | Some strict-test typing uses `expect.arrayContaining` patterns to avoid `unknown` index errors |+| Columnar end-to-end | Facades still denormalize; adapters should not assume CH row shapes internally |++### Operational env vars++| Variable | Effect |+|----------|--------|+| `OPENLIT_QUERY_OBSERVABILITY=1` | Enable query debug lines in production |+| Default (non-production) | Query observability on |++---++## Local development & testing++### Run validation (from `src/client`)++```bash+npm test -- --runInBand+npm run lint+npx prisma validate --schema prisma/schema.prisma+npx tsc --noEmit+rg -n "@/ee/" src/client/src # must be comments/docs only+rg -n '"[a-z_]+:[a-z_]+"' src/client/src # review matches; no RBAC literals+```++### LGTM stack smoke test++1. Configure Grafana Cloud (or local LGTM) sources in **Project → Data Sources**.+2. Create a **Grafana stack** (Tempo + Loki + Mimir) or bind signals individually.+3. Set per-signal bindings (traces → Tempo, logs → Loki, metrics → Mimir).+4. Verify: Telemetry (all services visible after polling), Agent detail monitoring tab, LLM dashboard widgets, structured dashboard widgets with `sourceId`.++### Rebuild client container++```bash+cd src+docker compose -f dev-docker-compose.yml build openlit+docker compose -f dev-docker-compose.yml up -d --no-deps --force-recreate openlit+```++---++## Adding a new datasource (summary)++Full checklist: `src/client/src/lib/platform/datasource/README.md`.++1. `datasource/<vendor>/adapter.ts` extending `BaseExternalAdapter`.+2. `describe()` with `configFields`, `authStyle`, honest `capabilities`.+3. `selector.ts` for AI selector → vendor query language.+4. Register in `bootstrap.ts` (or EE hook).+5. Mocked HTTP unit tests (see `grafana.test.ts`, `datadog.test.ts` patterns).+6. **Do not** edit shared forms, CRUD templates, or Prisma for a new `type` string.++**Extensibility regression test:** `registry.test.ts` registers a fictional `dash0` type with only `configFields` and asserts the descriptor resolves for the form — no UI edits required.++---++## Branch scale (orientation)++~139 files touched, ~17k insertions. Largest new areas:++- `datasource/grafana/tempo.ts` — Tempo adapter + span index+- `structured-query-builder.tsx` — Grafana-style widget query UI+- `data-sources-page.tsx` — source admin UI+- `telemetry-source-crud.ts` / `telemetry-source.ts` — config + resolution+- `traces/read.ts` — traces facade+- Vendor adapters (Datadog, New Relic, Jaeger, Victoria, …)+- Tests under `src/client/src/__tests__/lib/platform/datasource/` and facade tests++---++## Original design notes (still valid)++The feasibility verdict from the initial design remains: vendors differ wildly in query languages and aggregation power, so **capability negotiation + explicit degradation** is mandatory. The agents materializer **cannot** write into vendor stores — it reads via adapters and writes derived rows to OpenLIT ClickHouse. Query pushdown, caching, rate limits, and SSRF protections are not optional for production external sources.++What changed from the early design doc:++- **Vendor adapters ship in CE** (not only EE); EE adds extra vendors via hook.+- **Descriptor-driven forms and stack templates** landed (no hardcoded `TELEMETRY_STACK_TEMPLATES`).+- **L2 rollups + hot cache** landed for dashboard/agent KPI acceleration.+- **Grafana-parity downsampling + SWR polling** landed.+- **Per-signal bindings** landed (not just per-widget override).+- **Data Sources UI** is in CE project settings (not deferred).++---++## Quick debugging guide++| Symptom | Likely cause | Where to look |+|---------|--------------|---------------|+| Only one service in telemetry after agent visit | Stale scope or unstratified list | `agent-scope-provider.tsx`, `sample-fetch.ts` stratification |+| Widget shows 200 total requests | L1 sample cap / truncated list | `listTraceRecords`, adapter `meta.truncated` |+| Page blanks on poll | Missing SWR | `useFetchWrapper.ts`, `telemetry-request-cache.ts` |+| 401 on source test | Auth style mismatch | descriptor `authStyle`, `auth-headers.ts` |+| “Not supported” on chat view | Source lacks `spanEvents` | `useSignalCapabilities`, Tempo/Jaeger capabilities |+| Slow dashboards | L1 re-sample every poll | L2 rollups freshness, `shouldPreferRollup` |+| SSRF error on local Tempo | Private network blocked | source `allowPrivateNetwork` setting |++---++*Last updated: branch handoff for external observability + datasource-agnostic layer.*diff --git a/src/Dockerfile b/src/Dockerfileindex 6ddee5a1..4da317a8 100644--- a/src/Dockerfile+++ b/src/Dockerfile@@ -1,10 +1,7 @@-# Node.js builder - pinned to alpine 3.21 with digest-FROM alpine:3.21@sha256:c3f8e73fdb79deaebaa2037150150191b9dcbfba68b4a46d70103204c53f4709 AS builder+# Node.js builder - Node 20 Alpine (OpenPlait adapters require >=20)+FROM node:20-alpine@sha256:fb4cd12c85ee03686f6af5362a0b0d56d50c58a04632e6c0fb8363f609372293 AS builder-# Installing node and npm-RUN apk add --no-cache nodejs npm--# Installing extra packages (openssl needed for Prisma engine detection on Alpine 3.21+)+# Installing extra packages (openssl needed for Prisma engine detection on Alpine)RUN apk add --no-cache rust cargo build-base openssl# Set the working directory in the container@@ -82,9 +79,8 @@ RUN echo "Downloading binaries for architecture: ${TARGETARCH}" && \echo "opampsupervisor binary info:" && file opampsupervisor-# Final runtime image - Use node:18-bullseye-slim pinned with digest-# This replaces the unsafe `curl | bash` NodeSource install pattern-FROM node:18-bullseye-slim@sha256:d69fb189fa7765636655db043a6a9e6be5ddf94bd1a8dc33fd0bcf4664606a9e+# Final runtime image - Node 20 (OpenPlait adapters declare engines.node >=20)+FROM node:20-bullseye-slim@sha256:65ef49f7d24aefd012a7fc6f9a2b734bcc19e424976a81f60c86b47266ef5b28# Accept build arguments to pass version and architecture info# TARGETARCH is automatically provided by Docker buildx based on --platformdiff --git a/src/client/jest.config.js b/src/client/jest.config.jsindex b38cdc51..bd6e96df 100644--- a/src/client/jest.config.js+++ b/src/client/jest.config.js@@ -10,7 +10,14 @@ const config = {setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],moduleNameMapper: {'^@/(.*)$': '<rootDir>/src/$1',+ // OpenPlait packages are ESM-only and expose their entry points through+ // package export maps. Resolve them explicitly so Jest's CommonJS resolver+ // can hand the files to Next's transformer.+ '^@openplait/([^/]+)$': '<rootDir>/node_modules/@openplait/$1/dist/src/index.js',},+ transformIgnorePatterns: [+ '/node_modules/(?!(?:@openplait)/)',+ ],collectCoverageFrom: ['src/**/*.{ts,tsx}','!src/**/*.d.ts',diff --git a/src/client/jest.setup.ts b/src/client/jest.setup.tsindex 0e5ceac8..bb1c4815 100644--- a/src/client/jest.setup.ts+++ b/src/client/jest.setup.ts@@ -1,2 +1,9 @@import '@testing-library/jest-dom';import './jest.response-polyfill';++// jsdom does not currently expose Node's structuredClone implementation.+// OpenPlait adapters use it to isolate server-side connection configuration.+if (typeof globalThis.structuredClone !== 'function') {+ globalThis.structuredClone = <T>(value: T): T =>+ JSON.parse(JSON.stringify(value)) as T;+}diff --git a/src/client/next.config.js b/src/client/next.config.jsindex 995be769..5e523a41 100644--- a/src/client/next.config.js+++ b/src/client/next.config.js@@ -16,6 +16,16 @@ const contentSecurityPolicy = [const nextConfig = {output: "standalone",poweredByHeader: false,+ transpilePackages: [+ "@openplait/adapter-clickhouse",+ "@openplait/adapter-jaeger",+ "@openplait/adapter-loki",+ "@openplait/adapter-prometheus",+ "@openplait/adapter-tempo",+ "@openplait/adapter-sdk",+ "@openplait/core",+ "@openplait/runtime",+ ],images: {remotePatterns: [{diff --git a/src/client/package-lock.json b/src/client/package-lock.jsonindex 6648c943..2276cf54 100644--- a/src/client/package-lock.json+++ b/src/client/package-lock.json@@ -14,12 +14,20 @@"@ai-sdk/mistral": "^3.0.4","@ai-sdk/openai": "^3.0.4","@anthropic-ai/sdk": "^0.21.1",- "@clickhouse/client": "^1.1.0",+ "@clickhouse/client": "^1.23.1","@dhmk/zustand-lens": "^4.0.0","@kubernetes/client-node": "^1.4.0","@mistralai/mistralai": "^0.4.0","@monaco-editor/react": "^4.7.0","@next-auth/prisma-adapter": "^1.0.7",+ "@openplait/adapter-clickhouse": "1.0.0",+ "@openplait/adapter-jaeger": "1.0.0",+ "@openplait/adapter-loki": "1.0.0",+ "@openplait/adapter-prometheus": "1.0.0",+ "@openplait/adapter-sdk": "1.0.0",+ "@openplait/adapter-tempo": "1.0.0",+ "@openplait/core": "1.0.0",+ "@openplait/runtime": "1.0.0","@prisma/client": "^5.15.0","@radix-ui/react-accordion": "^1.2.1","@radix-ui/react-alert-dialog": "^1.1.15",@@ -102,6 +110,9 @@"prisma": "^5.15.0","tailwindcss": "^3.4.4","typescript": "^5.4.4"+ },+ "engines": {+ "node": ">=20"}},"node_modules/@adobe/css-tools": {@@ -1702,21 +1713,14 @@"license": "MIT"},"node_modules/@clickhouse/client": {- "version": "1.1.0",- "resolved": "https://registry.npmjs.org/@clickhouse/client/-/client-1.1.0.tgz",- "integrity": "sha512-fDG0dUU2nzcsE4XLDL3qUydbHJfuVCHnLuBTEmQfDrNQB9z6/qOtAUoGfAatZ07eEakZ4JAzKWMTU4MW4m0BJg==",- "dependencies": {- "@clickhouse/client-common": "1.1.0"- },+ "version": "1.23.1",+ "resolved": "https://registry.npmjs.org/@clickhouse/client/-/client-1.23.1.tgz",+ "integrity": "sha512-vs3/Zc1dHvT171btW5nMoPsPCJ6QVJ5pp7obxzO5sjqwFx/jjz9wwCAqcFOdc2DhprugDBaVn+4dVY8hG3A9nw==",+ "license": "Apache-2.0","engines": {- "node": ">=16"+ "node": ">=20"}},- "node_modules/@clickhouse/client-common": {- "version": "1.1.0",- "resolved": "https://registry.npmjs.org/@clickhouse/client-common/-/client-common-1.1.0.tgz",- "integrity": "sha512-saD6vNg+gIZACT5OSCWn0evLBP+Cs5bBR2Ol24yOQc8AXcw94b1qcDjQEVxms1xzO6heFhZRuPf+YefMNrL3Ig=="- },"node_modules/@csstools/color-helpers": {"version": "5.1.0","resolved": "https://registry.npmjs.org/@csstools/color-helpers/-/color-helpers-5.1.0.tgz",@@ -3811,6 +3815,148 @@"node": ">= 8"}},+ "node_modules/@openplait/adapter-clickhouse": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/@openplait/adapter-clickhouse/-/adapter-clickhouse-1.0.0.tgz",+ "integrity": "sha512-oXJEyF47oEOJ8FlQHjcWJ1MuyzZsbuK32/4J/DJgA6gnCVwCm6ZQoIY6R8q0NzkCM7rvFqr7aL+g3D6tpAu36g==",+ "license": "Apache-2.0",+ "dependencies": {+ "@clickhouse/client": "^1.23.1",+ "@openplait/adapter-sdk": "1.0.0",+ "@openplait/core": "1.0.0"+ },+ "engines": {+ "node": ">=20.19"+ }+ },+ "node_modules/@openplait/adapter-jaeger": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/@openplait/adapter-jaeger/-/adapter-jaeger-1.0.0.tgz",+ "integrity": "sha512-d+UuDcjPjK/d/9J5RJ7S5dK2lAGTMVSJ6iHT+kUVVFceXs2eYWB/Ied9PayEwadyOPR1pQKI+9XxXNQIEdbP2g==",+ "license": "Apache-2.0",+ "dependencies": {+ "@openplait/adapter-sdk": "1.0.0",+ "@openplait/core": "1.0.0"+ },+ "engines": {+ "node": ">=20.19"+ }+ },+ "node_modules/@openplait/adapter-loki": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/@openplait/adapter-loki/-/adapter-loki-1.0.0.tgz",+ "integrity": "sha512-XRf4VkwmWcx1wX++du3ZFAu3gN7Ebf/tklKXF42b1NKXjY6MD/ngfJ0r37ioeDIxHoItyLyuMMt2/gW9mqBBVw==",+ "license": "Apache-2.0",+ "dependencies": {+ "@openplait/adapter-sdk": "1.0.0",+ "@openplait/core": "1.0.0"+ },+ "engines": {+ "node": ">=20.19"+ }+ },+ "node_modules/@openplait/adapter-prometheus": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/@openplait/adapter-prometheus/-/adapter-prometheus-1.0.0.tgz",+ "integrity": "sha512-YVsKptvjexdUraugP0e0UYF1i4thiVOoxqocJWDENbmTT7QPZ0us2ngXUh4vtG0Z6FH0PgfkL/D00X2JyjvgQA==",+ "license": "Apache-2.0",+ "dependencies": {+ "@openplait/adapter-sdk": "1.0.0",+ "@openplait/core": "1.0.0"+ },+ "engines": {+ "node": ">=20.19"+ }+ },+ "node_modules/@openplait/adapter-sdk": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/@openplait/adapter-sdk/-/adapter-sdk-1.0.0.tgz",+ "integrity": "sha512-PxM1/GxPk2iq1ASV2tKphuoATxnf99ZqTdBcvCqOTDBN8YZjycq+WYUe1SUwjc7bDpIxOzwuZutzPkYr/U/auQ==",+ "license": "Apache-2.0",+ "dependencies": {+ "@openplait/core": "1.0.0"+ },+ "engines": {+ "node": ">=20.19"+ }+ },+ "node_modules/@openplait/adapter-tempo": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/@openplait/adapter-tempo/-/adapter-tempo-1.0.0.tgz",+ "integrity": "sha512-dW5TbP451KlLt6tYO8HTIZEDScWSg6vOi8jpA2Oi/WjBgOkFJFVaxh1y4jRqyK5u2ecPif3AfnvrunGcJMbbNw==",+ "license": "Apache-2.0",+ "dependencies": {+ "@openplait/adapter-sdk": "1.0.0",+ "@openplait/core": "1.0.0"+ },+ "engines": {+ "node": ">=20.19"+ }+ },+ "node_modules/@openplait/core": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/@openplait/core/-/core-1.0.0.tgz",+ "integrity": "sha512-BMlxx0bTi2IssfarTkb9zHpIx6OJ7IYRi0M0CxYRFeBiaDndAG5BwCsnmSt/odPyBAJDRyAQEOsQ+9tR78p2JQ==",+ "license": "Apache-2.0",+ "dependencies": {+ "ajv": "^8.17.0",+ "ajv-formats": "^3.0.0",+ "yaml": "^2.8.0"+ },+ "engines": {+ "node": ">=20.19"+ }+ },+ "node_modules/@openplait/core/node_modules/ajv": {+ "version": "8.20.0",+ "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.20.0.tgz",+ "integrity": "sha512-Thbli+OlOj+iMPYFBVBfJ3OmCAnaSyNn4M1vz9T6Gka5Jt9ba/HIR56joy65tY6kx/FCF5VXNB819Y7/GUrBGA==",+ "license": "MIT",+ "dependencies": {+ "fast-deep-equal": "^3.1.3",+ "fast-uri": "^3.0.1",+ "json-schema-traverse": "^1.0.0",+ "require-from-string": "^2.0.2"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/epoberezkin"+ }+ },+ "node_modules/@openplait/core/node_modules/json-schema-traverse": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",+ "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==",+ "license": "MIT"+ },+ "node_modules/@openplait/core/node_modules/yaml": {+ "version": "2.9.0",+ "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.9.0.tgz",+ "integrity": "sha512-2AvhNX3mb8zd6Zy7INTtSpl1F15HW6Wnqj0srWlkKLcpYl/gMIMJiyuGq2KeI2YFxUPjdlB+3Lc10seMLtL4cA==",+ "license": "ISC",+ "bin": {+ "yaml": "bin.mjs"+ },+ "engines": {+ "node": ">= 14.6"+ },+ "funding": {+ "url": "https://github.com/sponsors/eemeli"+ }+ },+ "node_modules/@openplait/runtime": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/@openplait/runtime/-/runtime-1.0.0.tgz",+ "integrity": "sha512-vGExDp4scIYvpYVSh4WNQf5g/cfLRhzXzXXhkUvy2LC6HW0TKNXUBRkc3WK5W6oIQ5sW3PovlQy7FR4e+R5iSg==",+ "license": "Apache-2.0",+ "dependencies": {+ "@openplait/adapter-sdk": "1.0.0",+ "@openplait/core": "1.0.0"+ },+ "engines": {+ "node": ">=20.19"+ }+ },"node_modules/@opentelemetry/api": {"version": "1.9.0","resolved": "https://registry.npmjs.org/@opentelemetry/api/-/api-1.9.0.tgz",@@ -8401,6 +8547,45 @@"url": "https://github.com/sponsors/epoberezkin"}},+ "node_modules/ajv-formats": {+ "version": "3.0.1",+ "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-3.0.1.tgz",+ "integrity": "sha512-8iUql50EUR+uUcdRQ3HDqa6EVyo3docL8g5WJ3FNcWmu62IbkGUue/pEyLBW8VGKKucTPgqeks4fIU1DA4yowQ==",+ "license": "MIT",+ "dependencies": {+ "ajv": "^8.0.0"+ },+ "peerDependencies": {+ "ajv": "^8.0.0"+ },+ "peerDependenciesMeta": {+ "ajv": {+ "optional": true+ }+ }+ },+ "node_modules/ajv-formats/node_modules/ajv": {+ "version": "8.20.0",+ "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.20.0.tgz",+ "integrity": "sha512-Thbli+OlOj+iMPYFBVBfJ3OmCAnaSyNn4M1vz9T6Gka5Jt9ba/HIR56joy65tY6kx/FCF5VXNB819Y7/GUrBGA==",+ "license": "MIT",+ "dependencies": {+ "fast-deep-equal": "^3.1.3",+ "fast-uri": "^3.0.1",+ "json-schema-traverse": "^1.0.0",+ "require-from-string": "^2.0.2"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/epoberezkin"+ }+ },+ "node_modules/ajv-formats/node_modules/json-schema-traverse": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",+ "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==",+ "license": "MIT"+ },"node_modules/ansi-escapes": {"version": "4.3.2","resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-4.3.2.tgz",@@ -10962,8 +11147,7 @@"node_modules/fast-deep-equal": {"version": "3.1.3","resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",- "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==",- "dev": true+ "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="},"node_modules/fast-equals": {"version": "5.0.1",@@ -11017,6 +11201,22 @@"integrity": "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==","dev": true},+ "node_modules/fast-uri": {+ "version": "3.1.5",+ "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.1.5.tgz",+ "integrity": "sha512-gHwA1O9LDIcKunMKhObS/HimwtehO1nPUECKAu5TpKgaO19fcWEl4bliWe1jWxVFvIXztJjjQ4L8XQ1EU9f7Jw==",+ "funding": [+ {+ "type": "github",+ "url": "https://github.com/sponsors/fastify"+ },+ {+ "type": "opencollective",+ "url": "https://opencollective.com/fastify"+ }+ ],+ "license": "BSD-3-Clause"+ },"node_modules/fast-xml-parser": {"version": "4.2.5","resolved": "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-4.2.5.tgz",@@ -16264,6 +16464,15 @@"node": ">=0.10.0"}},+ "node_modules/require-from-string": {+ "version": "2.0.2",+ "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz",+ "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==",+ "license": "MIT",+ "engines": {+ "node": ">=0.10.0"+ }+ },"node_modules/resize-observer-polyfill": {"version": "1.5.1","resolved": "https://registry.npmjs.org/resize-observer-polyfill/-/resize-observer-polyfill-1.5.1.tgz",diff --git a/src/client/package.json b/src/client/package.jsonindex 2629752c..2b5dfe29 100644--- a/src/client/package.json+++ b/src/client/package.json@@ -20,12 +20,20 @@"@ai-sdk/mistral": "^3.0.4","@ai-sdk/openai": "^3.0.4","@anthropic-ai/sdk": "^0.21.1",- "@clickhouse/client": "^1.1.0",+ "@clickhouse/client": "^1.23.1","@dhmk/zustand-lens": "^4.0.0","@kubernetes/client-node": "^1.4.0","@mistralai/mistralai": "^0.4.0","@monaco-editor/react": "^4.7.0","@next-auth/prisma-adapter": "^1.0.7",+ "@openplait/adapter-clickhouse": "1.0.0",+ "@openplait/adapter-jaeger": "1.0.0",+ "@openplait/adapter-loki": "1.0.0",+ "@openplait/adapter-prometheus": "1.0.0",+ "@openplait/adapter-sdk": "1.0.0",+ "@openplait/adapter-tempo": "1.0.0",+ "@openplait/core": "1.0.0",+ "@openplait/runtime": "1.0.0","@prisma/client": "^5.15.0","@radix-ui/react-accordion": "^1.2.1","@radix-ui/react-alert-dialog": "^1.1.15",@@ -111,5 +119,8 @@},"prisma": {"seed": "node prisma/seed.js"+ },+ "engines": {+ "node": ">=20"}}diff --git a/src/client/prisma/migrations/20260709130000_telemetry_source/migration.sql b/src/client/prisma/migrations/20260709130000_telemetry_source/migration.sqlnew file mode 100644index 00000000..3ae374d8--- /dev/null+++ b/src/client/prisma/migrations/20260709130000_telemetry_source/migration.sql@@ -0,0 +1,18 @@+-- CreateTable+CREATE TABLE "telemetry_source" (+ "id" TEXT NOT NULL PRIMARY KEY,+ "project_id" TEXT,+ "name" TEXT NOT NULL,+ "type" TEXT NOT NULL DEFAULT 'clickhouse',+ "signals" TEXT NOT NULL DEFAULT 'traces,logs,metrics',+ "settings" TEXT,+ "secret_ref" TEXT,+ "is_default" BOOLEAN NOT NULL DEFAULT false,+ "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,+ "updatedAt" DATETIME NOT NULL,+ "user_id" TEXT,+ CONSTRAINT "telemetry_source_project_id_fkey" FOREIGN KEY ("project_id") REFERENCES "projects" ("id") ON DELETE CASCADE ON UPDATE CASCADE+);++-- CreateIndex+CREATE UNIQUE INDEX "telemetry_source_name_project_id_key" ON "telemetry_source"("name", "project_id");diff --git a/src/client/prisma/migrations/20260709150000_telemetry_source_binding/migration.sql b/src/client/prisma/migrations/20260709150000_telemetry_source_binding/migration.sqlnew file mode 100644index 00000000..94b7f045--- /dev/null+++ b/src/client/prisma/migrations/20260709150000_telemetry_source_binding/migration.sql@@ -0,0 +1,14 @@+-- CreateTable+CREATE TABLE "telemetry_source_binding" (+ "id" TEXT NOT NULL PRIMARY KEY,+ "project_id" TEXT NOT NULL,+ "signal" TEXT NOT NULL,+ "source_id" TEXT NOT NULL,+ "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,+ "updatedAt" DATETIME NOT NULL,+ CONSTRAINT "telemetry_source_binding_project_id_fkey" FOREIGN KEY ("project_id") REFERENCES "projects" ("id") ON DELETE CASCADE ON UPDATE CASCADE,+ CONSTRAINT "telemetry_source_binding_source_id_fkey" FOREIGN KEY ("source_id") REFERENCES "telemetry_source" ("id") ON DELETE CASCADE ON UPDATE CASCADE+);++-- CreateIndex+CREATE UNIQUE INDEX "telemetry_source_binding_project_id_signal_key" ON "telemetry_source_binding"("project_id", "signal");diff --git a/src/client/prisma/migrations/20260724100000_connector_framework/migration.sql b/src/client/prisma/migrations/20260724100000_connector_framework/migration.sqlnew file mode 100644index 00000000..6fea9cb4--- /dev/null+++ b/src/client/prisma/migrations/20260724100000_connector_framework/migration.sql@@ -0,0 +1,32 @@+CREATE TABLE "connector_instances" (+ "id" TEXT NOT NULL PRIMARY KEY,+ "category" TEXT NOT NULL,+ "type" TEXT NOT NULL,+ "name" TEXT NOT NULL,+ "organisation_id" TEXT,+ "project_id" TEXT,+ "settings" TEXT NOT NULL DEFAULT '{}',+ "secret_ref" TEXT,+ "status" TEXT NOT NULL DEFAULT 'active',+ "metadata" TEXT NOT NULL DEFAULT '{}',+ "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,+ "updatedAt" DATETIME NOT NULL,+ CONSTRAINT "connector_instances_organisation_id_fkey" FOREIGN KEY ("organisation_id") REFERENCES "organisations" ("id") ON DELETE CASCADE ON UPDATE CASCADE,+ CONSTRAINT "connector_instances_project_id_fkey" FOREIGN KEY ("project_id") REFERENCES "projects" ("id") ON DELETE CASCADE ON UPDATE CASCADE+);++CREATE TABLE "connector_bindings" (+ "id" TEXT NOT NULL PRIMARY KEY,+ "connector_id" TEXT NOT NULL,+ "consumer_type" TEXT NOT NULL,+ "consumer_key" TEXT NOT NULL,+ "metadata" TEXT NOT NULL DEFAULT '{}',+ "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,+ "updatedAt" DATETIME NOT NULL,+ CONSTRAINT "connector_bindings_connector_id_fkey" FOREIGN KEY ("connector_id") REFERENCES "connector_instances" ("id") ON DELETE CASCADE ON UPDATE CASCADE+);++CREATE UNIQUE INDEX "connector_instances_name_organisation_id_project_id_key" ON "connector_instances"("name", "organisation_id", "project_id");+CREATE INDEX "connector_instances_category_type_idx" ON "connector_instances"("category", "type");+CREATE UNIQUE INDEX "connector_bindings_connector_id_consumer_type_consumer_key_key" ON "connector_bindings"("connector_id", "consumer_type", "consumer_key");+CREATE INDEX "connector_bindings_consumer_type_consumer_key_idx" ON "connector_bindings"("consumer_type", "consumer_key");diff --git a/src/client/prisma/migrations/20260724113000_connector_environments/migration.sql b/src/client/prisma/migrations/20260724113000_connector_environments/migration.sqlnew file mode 100644index 00000000..f1e8f8a0--- /dev/null+++ b/src/client/prisma/migrations/20260724113000_connector_environments/migration.sql@@ -0,0 +1,9 @@+ALTER TABLE "telemetry_source" ADD COLUMN "environment" TEXT NOT NULL DEFAULT 'production';+ALTER TABLE "connector_instances" ADD COLUMN "environment" TEXT NOT NULL DEFAULT 'production';+ALTER TABLE "connector_bindings" ADD COLUMN "environment" TEXT NOT NULL DEFAULT 'production';++DROP INDEX IF EXISTS "connector_instances_name_organisation_id_project_id_key";+DROP INDEX IF EXISTS "connector_bindings_connector_id_consumer_type_consumer_key_key";++CREATE UNIQUE INDEX "connector_instances_name_organisation_id_project_id_environment_key" ON "connector_instances"("name", "organisation_id", "project_id", "environment");+CREATE UNIQUE INDEX "connector_bindings_connector_id_consumer_type_consumer_key_environment_key" ON "connector_bindings"("connector_id", "consumer_type", "consumer_key", "environment");diff --git a/src/client/prisma/migrations/20260724113001_telemetry_connector_environments/migration.sql b/src/client/prisma/migrations/20260724113001_telemetry_connector_environments/migration.sqlnew file mode 100644index 00000000..145dfb91--- /dev/null+++ b/src/client/prisma/migrations/20260724113001_telemetry_connector_environments/migration.sql@@ -0,0 +1,7 @@+ALTER TABLE "telemetry_source_binding" ADD COLUMN "environment" TEXT NOT NULL DEFAULT 'production';++DROP INDEX IF EXISTS "telemetry_source_name_project_id_key";+DROP INDEX IF EXISTS "telemetry_source_binding_project_id_signal_key";++CREATE UNIQUE INDEX "telemetry_source_name_project_id_environment_key" ON "telemetry_source"("name", "project_id", "environment");+CREATE UNIQUE INDEX "telemetry_source_binding_project_id_signal_environment_key" ON "telemetry_source_binding"("project_id", "signal", "environment");diff --git a/src/client/prisma/migrations/20260724113002_telemetry_binding_clickhouse_targets/migration.sql b/src/client/prisma/migrations/20260724113002_telemetry_binding_clickhouse_targets/migration.sqlnew file mode 100644index 00000000..5661ff64--- /dev/null+++ b/src/client/prisma/migrations/20260724113002_telemetry_binding_clickhouse_targets/migration.sql@@ -0,0 +1,25 @@+PRAGMA foreign_keys=OFF;++CREATE TABLE "new_telemetry_source_binding" (+ "id" TEXT NOT NULL PRIMARY KEY,+ "project_id" TEXT NOT NULL,+ "signal" TEXT NOT NULL,+ "environment" TEXT NOT NULL DEFAULT 'production',+ "source_id" TEXT,+ "database_config_id" TEXT,+ "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,+ "updatedAt" DATETIME NOT NULL,+ CONSTRAINT "telemetry_source_binding_project_id_fkey" FOREIGN KEY ("project_id") REFERENCES "projects" ("id") ON DELETE CASCADE ON UPDATE CASCADE,+ CONSTRAINT "telemetry_source_binding_source_id_fkey" FOREIGN KEY ("source_id") REFERENCES "telemetry_source" ("id") ON DELETE CASCADE ON UPDATE CASCADE,+ CONSTRAINT "telemetry_source_binding_database_config_id_fkey" FOREIGN KEY ("database_config_id") REFERENCES "databaseconfig" ("id") ON DELETE CASCADE ON UPDATE CASCADE+);++INSERT INTO "new_telemetry_source_binding" ("id", "project_id", "signal", "environment", "source_id", "createdAt", "updatedAt")+SELECT "id", "project_id", "signal", "environment", "source_id", "createdAt", "updatedAt"+FROM "telemetry_source_binding";++DROP TABLE "telemetry_source_binding";+ALTER TABLE "new_telemetry_source_binding" RENAME TO "telemetry_source_binding";++CREATE UNIQUE INDEX "telemetry_source_binding_project_id_signal_environment_key" ON "telemetry_source_binding"("project_id", "signal", "environment");+PRAGMA foreign_keys=ON;diff --git a/src/client/prisma/migrations/20260724140000_project_environments/migration.sql b/src/client/prisma/migrations/20260724140000_project_environments/migration.sqlnew file mode 100644index 00000000..d2540398--- /dev/null+++ b/src/client/prisma/migrations/20260724140000_project_environments/migration.sql@@ -0,0 +1,33 @@+CREATE TABLE IF NOT EXISTS "project_environments" (+ "id" TEXT NOT NULL PRIMARY KEY,+ "project_id" TEXT NOT NULL,+ "name" TEXT NOT NULL,+ "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,+ "updatedAt" DATETIME NOT NULL,+ CONSTRAINT "project_environments_project_id_fkey" FOREIGN KEY ("project_id") REFERENCES "projects" ("id") ON DELETE CASCADE ON UPDATE CASCADE+);++CREATE UNIQUE INDEX IF NOT EXISTS "project_environments_project_id_name_key" ON "project_environments"("project_id", "name");++INSERT OR IGNORE INTO "project_environments" ("id", "project_id", "name", "createdAt", "updatedAt")+SELECT 'env_' || lower(hex(randomblob(16))), "id", 'production', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP+FROM "projects"+WHERE "id" IS NOT NULL;++INSERT OR IGNORE INTO "project_environments" ("id", "project_id", "name", "createdAt", "updatedAt")+SELECT 'env_' || lower(hex(randomblob(16))), "project_id", "environment", CURRENT_TIMESTAMP, CURRENT_TIMESTAMP+FROM "databaseconfig"+WHERE "project_id" IS NOT NULL AND "environment" IS NOT NULL AND trim("environment") <> ''+ AND EXISTS (SELECT 1 FROM "projects" p WHERE p."id" = "databaseconfig"."project_id");++INSERT OR IGNORE INTO "project_environments" ("id", "project_id", "name", "createdAt", "updatedAt")+SELECT 'env_' || lower(hex(randomblob(16))), "project_id", "environment", CURRENT_TIMESTAMP, CURRENT_TIMESTAMP+FROM "telemetry_source"+WHERE "project_id" IS NOT NULL AND "environment" IS NOT NULL AND trim("environment") <> ''+ AND EXISTS (SELECT 1 FROM "projects" p WHERE p."id" = "telemetry_source"."project_id");++INSERT OR IGNORE INTO "project_environments" ("id", "project_id", "name", "createdAt", "updatedAt")+SELECT 'env_' || lower(hex(randomblob(16))), "project_id", "environment", CURRENT_TIMESTAMP, CURRENT_TIMESTAMP+FROM "telemetry_source_binding"+WHERE "project_id" IS NOT NULL AND "environment" IS NOT NULL AND trim("environment") <> ''+ AND EXISTS (SELECT 1 FROM "projects" p WHERE p."id" = "telemetry_source_binding"."project_id");diff --git a/src/client/prisma/migrations/20260724141000_database_config_environment_unique/migration.sql b/src/client/prisma/migrations/20260724141000_database_config_environment_unique/migration.sqlnew file mode 100644index 00000000..d4f176b0--- /dev/null+++ b/src/client/prisma/migrations/20260724141000_database_config_environment_unique/migration.sql@@ -0,0 +1,3 @@+DROP INDEX IF EXISTS "databaseconfig_name_project_id_key";+CREATE UNIQUE INDEX IF NOT EXISTS "databaseconfig_name_project_id_environment_key"+ON "databaseconfig"("name", "project_id", "environment");diff --git a/src/client/prisma/migrations/20260727100000_backfill_environment_database_bindings/migration.sql b/src/client/prisma/migrations/20260727100000_backfill_environment_database_bindings/migration.sqlnew file mode 100644index 00000000..f2f0bafb--- /dev/null+++ b/src/client/prisma/migrations/20260727100000_backfill_environment_database_bindings/migration.sql@@ -0,0 +1,37 @@+-- Ensure every project environment has a deterministic built-in database target.+-- Existing external bindings are preserved; only missing signal routes are added.+INSERT OR IGNORE INTO "project_environments" ("id", "project_id", "name", "createdAt", "updatedAt")+SELECT 'env_' || lower(hex(randomblob(16))), "project_id", "environment", CURRENT_TIMESTAMP, CURRENT_TIMESTAMP+FROM "databaseconfig"+WHERE "project_id" IS NOT NULL AND trim("environment") <> '';++WITH signals(signal) AS (VALUES ('traces'), ('logs'), ('metrics'))+INSERT INTO "telemetry_source_binding"+ ("id", "project_id", "signal", "environment", "source_id", "database_config_id", "createdAt", "updatedAt")+SELECT+ 'binding_' || lower(hex(randomblob(16))),+ db."project_id",+ signals.signal,+ db."environment",+ NULL,+ db."id",+ CURRENT_TIMESTAMP,+ CURRENT_TIMESTAMP+FROM "databaseconfig" db+CROSS JOIN signals+WHERE db."project_id" IS NOT NULL+ AND db."id" = (+ SELECT candidate."id"+ FROM "databaseconfig" candidate+ WHERE candidate."project_id" = db."project_id"+ AND candidate."environment" = db."environment"+ ORDER BY candidate."createdAt" ASC, candidate."id" ASC+ LIMIT 1+ )+ AND NOT EXISTS (+ SELECT 1+ FROM "telemetry_source_binding" binding+ WHERE binding."project_id" = db."project_id"+ AND binding."signal" = signals.signal+ AND binding."environment" = db."environment"+ );diff --git a/src/client/prisma/migrations/20260727101000_normalize_empty_environments/migration.sql b/src/client/prisma/migrations/20260727101000_normalize_empty_environments/migration.sqlnew file mode 100644index 00000000..c257b016--- /dev/null+++ b/src/client/prisma/migrations/20260727101000_normalize_empty_environments/migration.sql@@ -0,0 +1,34 @@+-- Normalize pre-environment rows created with an empty environment.+-- Keep an existing production binding when both rows exist.+DELETE FROM "telemetry_source_binding"+WHERE trim("environment") = ''+ AND EXISTS (+ SELECT 1 FROM "telemetry_source_binding" production+ WHERE production."project_id" = "telemetry_source_binding"."project_id"+ AND production."signal" = "telemetry_source_binding"."signal"+ AND production."environment" = 'production'+ );++UPDATE "databaseconfig"+SET "environment" = 'production'+WHERE trim("environment") = ''+ AND NOT EXISTS (+ SELECT 1 FROM "databaseconfig" production+ WHERE production."project_id" = "databaseconfig"."project_id"+ AND production."name" = "databaseconfig"."name"+ AND production."environment" = 'production'+ );++UPDATE "telemetry_source"+SET "environment" = 'production'+WHERE trim("environment") = ''+ AND NOT EXISTS (+ SELECT 1 FROM "telemetry_source" production+ WHERE production."project_id" = "telemetry_source"."project_id"+ AND production."name" = "telemetry_source"."name"+ AND production."environment" = 'production'+ );++UPDATE "telemetry_source_binding"+SET "environment" = 'production'+WHERE trim("environment") = '';diff --git a/src/client/prisma/migrations/20260728120000_split_legacy_databases_by_environment/migration.sql b/src/client/prisma/migrations/20260728120000_split_legacy_databases_by_environment/migration.sqlnew file mode 100644index 00000000..a71adb3b--- /dev/null+++ b/src/client/prisma/migrations/20260728120000_split_legacy_databases_by_environment/migration.sql@@ -0,0 +1,65 @@+-- Legacy installations stored multiple project databases as production-only.+-- Preserve the oldest database as production and give subsequent legacy+-- targets deterministic environments so the global environment selector can+-- switch the complete ClickHouse-backed feature scope.+CREATE TEMP TABLE "_legacy_database_environment_map" (+ "database_config_id" TEXT PRIMARY KEY,+ "project_id" TEXT NOT NULL,+ "environment" TEXT NOT NULL+);++INSERT INTO "_legacy_database_environment_map" ("database_config_id", "project_id", "environment")+SELECT+ ranked."id",+ ranked."project_id",+ CASE ranked."row_number"+ WHEN 1 THEN 'production'+ WHEN 2 THEN 'staging'+ WHEN 3 THEN 'development'+ ELSE 'legacy-' || ranked."row_number"+ END+FROM (+ SELECT+ "id",+ "project_id",+ ROW_NUMBER() OVER (PARTITION BY "project_id" ORDER BY "createdAt" ASC, "id" ASC) AS "row_number",+ COUNT(*) OVER (PARTITION BY "project_id") AS "project_database_count"+ FROM "databaseconfig"+ WHERE "project_id" IS NOT NULL AND "environment" = 'production'+) ranked+WHERE ranked."project_database_count" > 1;++INSERT OR IGNORE INTO "project_environments" ("id", "project_id", "name", "createdAt", "updatedAt")+SELECT 'env_' || lower(hex(randomblob(16))), "project_id", "environment", CURRENT_TIMESTAMP, CURRENT_TIMESTAMP+FROM "_legacy_database_environment_map";++UPDATE "databaseconfig"+SET "environment" = (+ SELECT map."environment"+ FROM "_legacy_database_environment_map" map+ WHERE map."database_config_id" = "databaseconfig"."id"+)+WHERE "id" IN (SELECT "database_config_id" FROM "_legacy_database_environment_map");++WITH signals(signal) AS (VALUES ('traces'), ('logs'), ('metrics'))+INSERT INTO "telemetry_source_binding"+ ("id", "project_id", "signal", "environment", "source_id", "database_config_id", "createdAt", "updatedAt")+SELECT+ 'binding_' || lower(hex(randomblob(16))),+ map."project_id",+ signals.signal,+ map."environment",+ NULL,+ map."database_config_id",+ CURRENT_TIMESTAMP,+ CURRENT_TIMESTAMP+FROM "_legacy_database_environment_map" map+CROSS JOIN signals+WHERE NOT EXISTS (+ SELECT 1 FROM "telemetry_source_binding" binding+ WHERE binding."project_id" = map."project_id"+ AND binding."signal" = signals.signal+ AND binding."environment" = map."environment"+);++DROP TABLE "_legacy_database_environment_map";diff --git a/src/client/prisma/migrations/20260728150000_one_clickhouse_per_environment/migration.sql b/src/client/prisma/migrations/20260728150000_one_clickhouse_per_environment/migration.sqlnew file mode 100644index 00000000..f3453eb7--- /dev/null+++ b/src/client/prisma/migrations/20260728150000_one_clickhouse_per_environment/migration.sql@@ -0,0 +1,46 @@+-- A project environment has exactly one ClickHouse connector.+-- Move duplicate legacy targets to deterministic legacy environments before+-- adding the database-level uniqueness constraint.+CREATE TEMP TABLE "_duplicate_database_environment_map" (+ "database_config_id" TEXT PRIMARY KEY,+ "project_id" TEXT NOT NULL,+ "environment" TEXT NOT NULL+);++INSERT INTO "_duplicate_database_environment_map" ("database_config_id", "project_id", "environment")+SELECT ranked."id", ranked."project_id", ranked."environment" || '-legacy-' || ranked."row_number"+FROM (+ SELECT+ "id",+ "project_id",+ "environment",+ ROW_NUMBER() OVER (PARTITION BY "project_id", "environment" ORDER BY "createdAt" ASC, "id" ASC) AS "row_number"+ FROM "databaseconfig"+ WHERE "project_id" IS NOT NULL+) ranked+WHERE ranked."row_number" > 1;++INSERT OR IGNORE INTO "project_environments" ("id", "project_id", "name", "createdAt", "updatedAt")+SELECT 'env_' || lower(hex(randomblob(16))), "project_id", "environment", CURRENT_TIMESTAMP, CURRENT_TIMESTAMP+FROM "_duplicate_database_environment_map";++UPDATE "telemetry_source_binding"+SET "environment" = (+ SELECT map."environment"+ FROM "_duplicate_database_environment_map" map+ WHERE map."database_config_id" = "telemetry_source_binding"."database_config_id"+)+WHERE "database_config_id" IN (SELECT "database_config_id" FROM "_duplicate_database_environment_map");++UPDATE "databaseconfig"+SET "environment" = (+ SELECT map."environment"+ FROM "_duplicate_database_environment_map" map+ WHERE map."database_config_id" = "databaseconfig"."id"+)+WHERE "id" IN (SELECT "database_config_id" FROM "_duplicate_database_environment_map");++DROP TABLE "_duplicate_database_environment_map";++CREATE UNIQUE INDEX "databaseconfig_project_id_environment_key"+ON "databaseconfig"("project_id", "environment");diff --git a/src/client/prisma/migrations/20260803120000_allow_multiple_clickhouse_per_environment/migration.sql b/src/client/prisma/migrations/20260803120000_allow_multiple_clickhouse_per_environment/migration.sqlnew file mode 100644index 00000000..3d0d4d29--- /dev/null+++ b/src/client/prisma/migrations/20260803120000_allow_multiple_clickhouse_per_environment/migration.sql@@ -0,0 +1,4 @@+-- Allow multiple ClickHouse connectors in the same project environment.+-- The name/environment uniqueness constraint remains, so connector names must+-- still be unique within an environment.+DROP INDEX IF EXISTS "databaseconfig_project_id_environment_key";diff --git a/src/client/prisma/migrations/20260813120000_backfill_intelligence_bindings/migration.sql b/src/client/prisma/migrations/20260813120000_backfill_intelligence_bindings/migration.sqlnew file mode 100644index 00000000..af3a099b--- /dev/null+++ b/src/client/prisma/migrations/20260813120000_backfill_intelligence_bindings/migration.sql@@ -0,0 +1,34 @@+-- Backfill missing `intelligence` signal bindings for every project environment.+-- Prior backfill only seeded traces/logs/metrics; intelligence fail-closes when+-- an explicit environment is requested without a binding (Rule Engine, NL chat,+-- coding-agent ClickHouse fallback).+WITH signals(signal) AS (VALUES ('intelligence'))+INSERT INTO "telemetry_source_binding"+ ("id", "project_id", "signal", "environment", "source_id", "database_config_id", "createdAt", "updatedAt")+SELECT+ 'binding_' || lower(hex(randomblob(16))),+ db."project_id",+ signals.signal,+ db."environment",+ NULL,+ db."id",+ CURRENT_TIMESTAMP,+ CURRENT_TIMESTAMP+FROM "databaseconfig" db+CROSS JOIN signals+WHERE db."project_id" IS NOT NULL+ AND db."id" = (+ SELECT candidate."id"+ FROM "databaseconfig" candidate+ WHERE candidate."project_id" = db."project_id"+ AND candidate."environment" = db."environment"+ ORDER BY candidate."createdAt" ASC, candidate."id" ASC+ LIMIT 1+ )+ AND NOT EXISTS (+ SELECT 1+ FROM "telemetry_source_binding" binding+ WHERE binding."project_id" = db."project_id"+ AND binding."signal" = signals.signal+ AND binding."environment" = db."environment"+ );diff --git a/src/client/prisma/schema.prisma b/src/client/prisma/schema.prismaindex ff1d74bd..c2711ef3 100644--- a/src/client/prisma/schema.prisma+++ b/src/client/prisma/schema.prisma@@ -87,6 +87,7 @@ model Organisation {members OrganisationUser[]invitedUsers OrganisationInvitedUser[]projects Project[]+ connectorInstances ConnectorInstance[]@@map("organisations")}@@ -101,6 +102,10 @@ model Project {createdAt DateTime @default(now())updatedAt DateTime @updatedAtdatabaseConfigs DatabaseConfig[]+ telemetrySources TelemetrySource[]+ telemetrySourceBindings TelemetrySourceBinding[]+ connectorInstances ConnectorInstance[]+ environments ProjectEnvironment[]currentForUsers OrganisationUser[] @relation("CurrentOrganisationProject")members ProjectUser[]@@ -108,6 +113,48 @@ model Project {@@map("projects")}+// Generic project/organisation connector records. Existing datasource and+// alert tables remain compatible while their repositories migrate behind the+// connector abstraction.+model ConnectorInstance {+ id String @id @default(cuid())+ category String+ type String+ name String+ environment String @default("production")+ organisationId String? @map("organisation_id")+ organisation Organisation? @relation(fields: [organisationId], references: [id], onDelete: Cascade)+ projectId String? @map("project_id")+ project Project? @relation(fields: [projectId], references: [id], onDelete: Cascade)+ settings String @default("{}")+ secretRef String? @map("secret_ref")+ status String @default("active")+ metadata String @default("{}")+ createdAt DateTime @default(now())+ updatedAt DateTime @updatedAt+ bindings ConnectorBinding[]++ @@unique([name, organisationId, projectId, environment])+ @@index([category, type])+ @@map("connector_instances")+}++model ConnectorBinding {+ id String @id @default(cuid())+ connectorId String @map("connector_id")+ connector ConnectorInstance @relation(fields: [connectorId], references: [id], onDelete: Cascade)+ consumerType String @map("consumer_type")+ consumerKey String @map("consumer_key")+ environment String @default("production")+ metadata String @default("{}")+ createdAt DateTime @default(now())+ updatedAt DateTime @updatedAt++ @@unique([connectorId, consumerType, consumerKey, environment])+ @@index([consumerType, consumerKey])+ @@map("connector_bindings")+}+model OrganisationUser {id String @id @default(cuid())organisationId String @map("organisation_id")@@ -174,11 +221,80 @@ model DatabaseConfig {dbInvitedUsers DatabaseConfigInvitedUser[]apiKeys APIKeys[]opengrounds OpenGround[]+ telemetryBindings TelemetrySourceBinding[]+ environmentRef ProjectEnvironment? @relation("ProjectEnvironmentDatabaseConfigs", fields: [projectId, environment], references: [projectId, name], onDelete: Restrict)- @@unique([name, projectId])+ @@unique([name, projectId, environment])@@map("databaseconfig")}+// A pluggable raw-telemetry source for a project. The implicit built-in+// source is the project's ClickHouse (DatabaseConfig) and is NOT stored here;+// rows in this table describe external observability backends (Datadog,+// Tempo/Loki/Mimir, New Relic, Jaeger, Victoria, ...). Secrets live in the+// vault (secretRef); only non-secret settings are stored inline as JSON text.+model TelemetrySource {+ id String @id @default(cuid())+ projectId String? @map("project_id")+ project Project? @relation(fields: [projectId], references: [id], onDelete: Cascade)+ name String+ environment String @default("production")+ // Source type, e.g. "clickhouse" | "datadog" | "tempo" | "loki" | "mimir" |+ // "prometheus" | "newrelic" | "jaeger" | "victoriametrics" | ...+ type String @default("clickhouse")+ // Comma-separated subset of "traces,logs,metrics" this source serves.+ signals String @default("traces,logs,metrics")+ // JSON text of non-secret settings (URLs, site, orgId, headers).+ settings String?+ // Vault secret id holding credentials (API/app keys), if any.+ secretRef String? @map("secret_ref")+ isDefault Boolean @default(false) @map("is_default")+ createdAt DateTime @default(now())+ updatedAt DateTime @updatedAt+ createdByUserId String? @map("user_id")+ bindings TelemetrySourceBinding[]+ environmentRef ProjectEnvironment? @relation("ProjectEnvironmentTelemetrySources", fields: [projectId, environment], references: [projectId, name], onDelete: Restrict)++ @@unique([name, projectId, environment])+ @@map("telemetry_source")+}++model ProjectEnvironment {+ id String @id @default(cuid())+ projectId String @map("project_id")+ project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)+ name String+ createdAt DateTime @default(now())+ updatedAt DateTime @updatedAt+ databaseConfigs DatabaseConfig[] @relation("ProjectEnvironmentDatabaseConfigs")+ telemetrySources TelemetrySource[] @relation("ProjectEnvironmentTelemetrySources")++ @@unique([projectId, name])+ @@map("project_environments")+}++// Per-signal routing: which TelemetrySource serves a given signal in a project+// (Grafana-style per-signal datasource binding). At most one binding per+// (project, signal). Cascades on both project and source deletion so routing+// never dangles.+model TelemetrySourceBinding {+ id String @id @default(cuid())+ projectId String @map("project_id")+ project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)+ // One of "traces" | "logs" | "metrics".+ signal String+ environment String @default("production")+ sourceId String? @map("source_id")+ source TelemetrySource? @relation(fields: [sourceId], references: [id], onDelete: Cascade)+ databaseConfigId String? @map("database_config_id")+ databaseConfig DatabaseConfig? @relation(fields: [databaseConfigId], references: [id], onDelete: Cascade)+ createdAt DateTime @default(now())+ updatedAt DateTime @updatedAt++ @@unique([projectId, signal, environment])+ @@map("telemetry_source_binding")+}+model DatabaseConfigUser {databaseConfigId StringuserId String @map("user_id")diff --git a/src/client/prisma/seed-lib.js b/src/client/prisma/seed-lib.jsnew file mode 100644index 00000000..794b8d7c--- /dev/null+++ b/src/client/prisma/seed-lib.js@@ -0,0 +1,350 @@+/**+ * Seed helpers shared by prisma/seed.js and unit tests.+ * Covers fresh INIT_DB_* bootstrap and upgrade-safe orphan migration.+ */++const DEFAULT_ENVIRONMENT = "production";+const DEFAULT_DB_NAME = "Default DB";+const DEFAULT_SIGNALS = ["traces", "logs", "metrics", "intelligence"];++function databaseConnectorId(id) {+ return `database:${id}`;+}++function readInitDbConfig(env = process.env) {+ return {+ username: env.INIT_DB_USERNAME || "default",+ password: env.INIT_DB_PASSWORD || "",+ host: env.INIT_DB_HOST,+ port: env.INIT_DB_PORT,+ database: env.INIT_DB_DATABASE || "default",+ };+}++function hasInitDb(config) {+ return Boolean(config && config.host && config.port);+}++async function ensureProjectEnvironment(prisma, projectId, name = DEFAULT_ENVIRONMENT) {+ return prisma.projectEnvironment.upsert({+ where: { projectId_name: { projectId, name } },+ create: { projectId, name },+ update: {},+ });+}++async function ensureEnvironmentDatabaseBindings(+ prisma,+ projectId,+ databaseConfigId,+ environment = DEFAULT_ENVIRONMENT,+ signals = DEFAULT_SIGNALS+) {+ for (const signal of signals) {+ await prisma.telemetrySourceBinding.upsert({+ where: {+ projectId_signal_environment: { projectId, signal, environment },+ },+ create: {+ projectId,+ signal,+ environment,+ databaseConfigId,+ },+ // Preserve day-2 routing changes made in the UI.+ update: {},+ });+ }+}++async function syncDatabaseConfigConnector(prisma, config) {+ if (!config?.id || !prisma.connectorInstance?.upsert) return null;+ const settings = JSON.stringify({+ username: config.username,+ host: config.host,+ port: config.port,+ database: config.database,+ query: config.query || "",+ });+ return prisma.connectorInstance.upsert({+ where: { id: databaseConnectorId(config.id) },+ create: {+ id: databaseConnectorId(config.id),+ category: "datasource",+ type: "clickhouse",+ name: config.name,+ environment: config.environment || DEFAULT_ENVIRONMENT,+ projectId: config.projectId,+ settings,+ status: "active",+ metadata: JSON.stringify({+ legacyKind: "database-config",+ legacyId: config.id,+ }),+ },+ update: {+ name: config.name,+ environment: config.environment || DEFAULT_ENVIRONMENT,+ projectId: config.projectId,+ settings,+ status: "active",+ },+ });+}++/**+ * Move pre-project "Default DB" rows into the default project before upsert.+ */+async function migrateOrphanedDefaultDb(prisma, defaultProjectId) {+ const orphanedDefaultDBConfigs = await prisma.databaseConfig.findMany({+ where: { name: DEFAULT_DB_NAME, projectId: null },+ });+ if (orphanedDefaultDBConfigs.length === 0) return;++ const existingDefaultDB = await prisma.databaseConfig.findUnique({+ where: {+ name_projectId_environment: {+ name: DEFAULT_DB_NAME,+ projectId: defaultProjectId,+ environment: DEFAULT_ENVIRONMENT,+ },+ },+ });++ if (existingDefaultDB) {+ for (const orphanedConfig of orphanedDefaultDBConfigs) {+ const hasReferences = await prisma.databaseConfigUser.findFirst({+ where: { databaseConfigId: orphanedConfig.id },+ });+ if (!hasReferences) {+ await prisma.databaseConfig.delete({ where: { id: orphanedConfig.id } });+ } else {+ await prisma.databaseConfig.update({+ where: { id: orphanedConfig.id },+ data: {+ name: `Default DB (${orphanedConfig.id.slice(0, 8)})`,+ projectId: defaultProjectId,+ environment: orphanedConfig.environment || DEFAULT_ENVIRONMENT,+ },+ });+ }+ }+ return;+ }++ await prisma.databaseConfig.updateMany({+ where: { name: DEFAULT_DB_NAME, projectId: null },+ data: { projectId: defaultProjectId, environment: DEFAULT_ENVIRONMENT },+ });+}++/**+ * Fresh install: create Default DB from INIT_DB_*.+ * Upgrade/restart: do not overwrite connection fields already stored in SQLite+ * (day-2 UI edits must survive container recreates with the same volume).+ */+async function upsertDefaultDatabaseConfig(prisma, { userId, projectId, initDb }) {+ if (!hasInitDb(initDb)) return null;++ await migrateOrphanedDefaultDb(prisma, projectId);++ const dbConfig = await prisma.databaseConfig.upsert({+ where: {+ name_projectId_environment: {+ name: DEFAULT_DB_NAME,+ projectId,+ environment: DEFAULT_ENVIRONMENT,+ },+ },+ // Preserve existing host/user/password/database on upgrades.+ update: {},+ create: {+ environment: DEFAULT_ENVIRONMENT,+ name: DEFAULT_DB_NAME,+ username: initDb.username,+ password: initDb.password,+ host: initDb.host,+ port: initDb.port,+ database: initDb.database,+ createdByUserId: userId,+ projectId,+ },+ });++ await prisma.databaseConfigUser.upsert({+ where: {+ databaseConfigId_userId: {+ userId,+ databaseConfigId: dbConfig.id,+ },+ },+ update: {},+ create: {+ userId,+ databaseConfigId: dbConfig.id,+ isCurrent: true,+ canEdit: true,+ canDelete: true,+ canShare: true,+ },+ });++ await ensureProjectEnvironment(prisma, projectId, DEFAULT_ENVIRONMENT);+ await ensureEnvironmentDatabaseBindings(+ prisma,+ projectId,+ dbConfig.id,+ DEFAULT_ENVIRONMENT+ );+ await syncDatabaseConfigConnector(prisma, dbConfig);++ return dbConfig;+}++/**+ * Upgrade path: attach every orphaned databaseConfig to the default project.+ */+async function migrateExistingData(prisma, defaultOrgId, defaultProjectId, seedUserId) {+ const allOrphanedConfigs = await prisma.databaseConfig.findMany({+ where: { projectId: null },+ select: { id: true, name: true, environment: true },+ });++ let migratedConfigCount = 0;+ for (const config of allOrphanedConfigs) {+ const environment = config.environment || DEFAULT_ENVIRONMENT;+ const existingConfigWithName = await prisma.databaseConfig.findUnique({+ where: {+ name_projectId_environment: {+ name: config.name,+ projectId: defaultProjectId,+ environment,+ },+ },+ });++ if (existingConfigWithName) {+ await prisma.databaseConfig.update({+ where: { id: config.id },+ data: {+ name: `${config.name} (${config.id.slice(0, 8)})`,+ projectId: defaultProjectId,+ environment,+ },+ });+ } else {+ await prisma.databaseConfig.update({+ where: { id: config.id },+ data: { projectId: defaultProjectId, environment },+ });+ }+ migratedConfigCount++;+ }++ if (migratedConfigCount > 0) {+ console.log(+ `Migrated ${migratedConfigCount} orphaned database configs to default project`+ );+ }++ const orgConfigs = await prisma.databaseConfig.findMany({+ where: { projectId: defaultProjectId },+ select: { id: true, name: true, username: true, host: true, port: true, database: true, query: true, environment: true, projectId: true },+ });+ if (orgConfigs.length === 0) return { migratedConfigCount: 0 };++ await ensureProjectEnvironment(prisma, defaultProjectId, DEFAULT_ENVIRONMENT);++ for (const config of orgConfigs) {+ const environment = config.environment || DEFAULT_ENVIRONMENT;+ await ensureProjectEnvironment(prisma, defaultProjectId, environment);+ await syncDatabaseConfigConnector(prisma, config);+ await ensureEnvironmentDatabaseBindings(+ prisma,+ defaultProjectId,+ config.id,+ environment+ );+ }++ const orgConfigIds = orgConfigs.map((c) => c.id);+ const sharedUserLinks = await prisma.databaseConfigUser.findMany({+ where: {+ databaseConfigId: { in: orgConfigIds },+ userId: { not: seedUserId },+ },+ select: { userId: true },+ distinct: ["userId"],+ });++ for (const { userId } of sharedUserLinks) {+ const existingMembership = await prisma.organisationUser.findUnique({+ where: {+ organisationId_userId: {+ organisationId: defaultOrgId,+ userId,+ },+ },+ });++ const hasCurrentOrg = await prisma.organisationUser.findFirst({+ where: { userId, isCurrent: true },+ });++ if (!existingMembership) {+ await prisma.organisationUser.create({+ data: {+ organisationId: defaultOrgId,+ userId,+ isCurrent: !hasCurrentOrg,+ currentProjectId: defaultProjectId,+ },+ });+ } else if (!hasCurrentOrg) {+ await prisma.organisationUser.update({+ where: {+ organisationId_userId: {+ organisationId: defaultOrgId,+ userId,+ },+ },+ data: {+ isCurrent: true,+ currentProjectId: defaultProjectId,+ },+ });+ } else if (!existingMembership.currentProjectId) {+ await prisma.organisationUser.update({+ where: {+ organisationId_userId: {+ organisationId: defaultOrgId,+ userId,+ },+ },+ data: { currentProjectId: defaultProjectId },+ });+ }++ await prisma.user.update({+ where: { id: userId },+ data: { hasCompletedOnboarding: true },+ });+ }++ return { migratedConfigCount };+}++module.exports = {+ DEFAULT_ENVIRONMENT,+ DEFAULT_DB_NAME,+ DEFAULT_SIGNALS,+ databaseConnectorId,+ readInitDbConfig,+ hasInitDb,+ ensureProjectEnvironment,+ ensureEnvironmentDatabaseBindings,+ syncDatabaseConfigConnector,+ migrateOrphanedDefaultDb,+ upsertDefaultDatabaseConfig,+ migrateExistingData,+};diff --git a/src/client/prisma/seed.js b/src/client/prisma/seed.jsindex 6b545e82..446d497f 100644--- a/src/client/prisma/seed.js+++ b/src/client/prisma/seed.js@@ -1,8 +1,16 @@const { PrismaClient } = require("@prisma/client");+const {+ readInitDbConfig,+ hasInitDb,+ ensureProjectEnvironment,+ upsertDefaultDatabaseConfig,+ migrateExistingData,+} = require("./seed-lib");+const prisma = new PrismaClient();+async function main() {console.log("Seeding Start.....");- // const defaultPassword = "openlituser"; ⤵const hashedPassword ="$2a$10$gh6Odw7fhLRrE1A1OxaHfeWOWKiZEEQpkOAhhCQ.RHx8VWOngwlHO";const user = await prisma.user.upsert({@@ -18,7 +26,6 @@ async function main() {},});- // Create default organisationconst defaultOrg = await prisma.organisation.upsert({where: { slug: "default" },update: {},@@ -29,11 +36,6 @@ async function main() {},});- // Link user to default organisation as the owner. The `role` column- // defaults to `member` at the schema level; the seed user is the- // org creator and must have elevated permissions or downstream- // privacy gates (e.g. coding-agents COHORT_K_FLOOR) silently- // suppress their own data. Existing rows are upgraded too.await prisma.organisationUser.upsert({where: {organisationId_userId: {@@ -83,238 +85,25 @@ async function main() {},});- const environmentDBConfig = {- username: process.env.INIT_DB_USERNAME || "default",- password: process.env.INIT_DB_PASSWORD || "",- host: process.env.INIT_DB_HOST,- port: process.env.INIT_DB_PORT,- database: process.env.INIT_DB_DATABASE || "default",- };-- if (environmentDBConfig.host && environmentDBConfig.port) {- // First, migrate any orphaned "Default DB" configs to the default project.- // This must happen before the upsert so existing installs keep references.- const orphanedDefaultDBConfigs = await prisma.databaseConfig.findMany({- where: {- name: "Default DB",- projectId: null,- },- });-- if (orphanedDefaultDBConfigs.length > 0) {- // Check if a "Default DB" config already exists in the default project.- const existingDefaultDB = await prisma.databaseConfig.findUnique({- where: {- name_projectId: {- name: "Default DB",- projectId: defaultProject.id,- },- },- });-- if (existingDefaultDB) {- // If one exists, we need to handle the orphaned configs- // Delete only orphaned configs that have no databaseconfiguser references- for (const orphanedConfig of orphanedDefaultDBConfigs) {- const hasReferences = await prisma.databaseConfigUser.findFirst({- where: { databaseConfigId: orphanedConfig.id },- });-- if (!hasReferences) {- // Safe to delete - no foreign key constraints- await prisma.databaseConfig.delete({- where: { id: orphanedConfig.id },- });- } else {- // Has references - migrate it by updating to a unique name.- await prisma.databaseConfig.update({- where: { id: orphanedConfig.id },- data: {- name: `Default DB (${orphanedConfig.id.slice(0, 8)})`,- projectId: defaultProject.id,- },- });- }- }- } else {- // No existing "Default DB" in default project - safe to migrate all orphaned ones.- await prisma.databaseConfig.updateMany({- where: {- name: "Default DB",- projectId: null,- },- data: { projectId: defaultProject.id },- });- }- }-- const dbConfig = await prisma.databaseConfig.upsert({- where: {- name_projectId: {- name: "Default DB",- projectId: defaultProject.id,- },- },- update: environmentDBConfig,- create: {- environment: "production",- name: "Default DB",- ...environmentDBConfig,- createdByUserId: user.id,- projectId: defaultProject.id,- },- });+ // Fresh and upgraded installs always get a production environment row so+ // connector/environment UIs and bindings have a stable target.+ await ensureProjectEnvironment(prisma, defaultProject.id, "production");- await prisma.databaseConfigUser.upsert({- where: {- databaseConfigId_userId: {- userId: user.id,- databaseConfigId: dbConfig.id,- },- },- update: {},- create: {- userId: user.id,- databaseConfigId: dbConfig.id,- isCurrent: true,- canEdit: true,- canDelete: true,- canShare: true,- },+ const initDb = readInitDbConfig(process.env);+ if (hasInitDb(initDb)) {+ await upsertDefaultDatabaseConfig(prisma, {+ userId: user.id,+ projectId: defaultProject.id,+ initDb,});}- // Migrate existing data if needed (for upgrades)- await migrateExistingData(defaultOrg.id, defaultProject.id, user.id);+ // Upgrade path: attach orphaned configs and backfill connectors/bindings.+ await migrateExistingData(prisma, defaultOrg.id, defaultProject.id, user.id);console.log("Seeding End.....");}-// Migration logic for existing installations.-// Migrates ALL orphaned configs into the default project, then ensures-// all users sharing configs in the default org are also members.-async function migrateExistingData(defaultOrgId, defaultProjectId, seedUserId) {- // Step 1: Move ALL orphaned DB configs into the default project.- // This ensures no user loses access to their configs when they join/create an org- const allOrphanedConfigs = await prisma.databaseConfig.findMany({- where: {- projectId: null,- },- select: { id: true, name: true },- });-- if (allOrphanedConfigs.length > 0) {- let migratedConfigCount = 0;-- for (const config of allOrphanedConfigs) {- const existingConfigWithName = await prisma.databaseConfig.findUnique({- where: {- name_projectId: {- name: config.name,- projectId: defaultProjectId,- },- },- });-- if (existingConfigWithName) {- await prisma.databaseConfig.update({- where: { id: config.id },- data: {- name: `${config.name} (${config.id.slice(0, 8)})`,- projectId: defaultProjectId,- },- });- } else {- await prisma.databaseConfig.update({- where: { id: config.id },- data: { projectId: defaultProjectId },- });- }-- migratedConfigCount++;- }-- console.log(- `Migrated ${migratedConfigCount} orphaned database configs to default project`- );- }-- // Step 2: Find ALL configs now in the default project and ensure shared users are members.- const orgConfigs = await prisma.databaseConfig.findMany({- where: { projectId: defaultProjectId },- select: { id: true },- });-- if (orgConfigs.length === 0) return;-- const orgConfigIds = orgConfigs.map((c) => c.id);-- // Find users who have access to these configs but aren't in the default org- const sharedUserLinks = await prisma.databaseConfigUser.findMany({- where: {- databaseConfigId: { in: orgConfigIds },- userId: { not: seedUserId },- },- select: { userId: true },- distinct: ["userId"],- });-- for (const { userId } of sharedUserLinks) {- const existingMembership = await prisma.organisationUser.findUnique({- where: {- organisationId_userId: {- organisationId: defaultOrgId,- userId,- },- },- });-- // Check if the user already has any current org- const hasCurrentOrg = await prisma.organisationUser.findFirst({- where: { userId, isCurrent: true },- });-- if (!existingMembership) {- await prisma.organisationUser.create({- data: {- organisationId: defaultOrgId,- userId,- isCurrent: !hasCurrentOrg,- currentProjectId: defaultProjectId,- },- });- } else if (!hasCurrentOrg) {- // Existing membership but no current org — fix it- await prisma.organisationUser.update({- where: {- organisationId_userId: {- organisationId: defaultOrgId,- userId,- },- },- data: {- isCurrent: true,- currentProjectId: defaultProjectId,- },- });- } else if (!existingMembership.currentProjectId) {- await prisma.organisationUser.update({- where: {- organisationId_userId: {- organisationId: defaultOrgId,- userId,- },- },- data: { currentProjectId: defaultProjectId },- });- }-- await prisma.user.update({- where: { id: userId },- data: { hasCompletedOnboarding: true },- });- }-}main().then(async () => {await prisma.$disconnect();@@ -322,4 +111,5 @@ main().catch(async (e) => {console.error(e);await prisma.$disconnect();+ process.exitCode = 1;});diff --git a/src/client/public/images/connectors/clickhouse.svg b/src/client/public/images/connectors/clickhouse.svgnew file mode 100644index 00000000..8b1340b1--- /dev/null+++ b/src/client/public/images/connectors/clickhouse.svg@@ -0,0 +1 @@+<svg fill="#FFCC00" role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>ClickHouse</title><path d="M21.333 10H24v4h-2.667ZM16 1.335h2.667v21.33H16Zm-5.333 0h2.666v21.33h-2.666ZM0 22.665V1.335h2.667v21.33zm5.333-21.33H8v21.33H5.333Z"/></svg>\ No newline at end of filediff --git a/src/client/public/images/connectors/grafana.svg b/src/client/public/images/connectors/grafana.svgnew file mode 100644index 00000000..c99695a1--- /dev/null+++ b/src/client/public/images/connectors/grafana.svg@@ -0,0 +1 @@+<svg fill="#F46800" role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Grafana</title><path d="M23.02 10.59a8.578 8.578 0 0 0-.862-3.034 8.911 8.911 0 0 0-1.789-2.445c.337-1.342-.413-2.505-.413-2.505-1.292-.08-2.113.4-2.416.62-.052-.02-.102-.044-.154-.064-.22-.089-.446-.172-.677-.247-.231-.073-.47-.14-.711-.197a9.867 9.867 0 0 0-.875-.161C14.557.753 12.94 0 12.94 0c-1.804 1.145-2.147 2.744-2.147 2.744l-.018.093c-.098.029-.2.057-.298.088-.138.042-.275.094-.413.143-.138.055-.275.107-.41.166a8.869 8.869 0 0 0-1.557.87l-.063-.029c-2.497-.955-4.716.195-4.716.195-.203 2.658.996 4.33 1.235 4.636a11.608 11.608 0 0 0-.607 2.635C1.636 12.677.953 15.014.953 15.014c1.926 2.214 4.171 2.351 4.171 2.351.003-.002.006-.002.006-.005.285.509.615.994.986 1.446.156.19.32.371.488.548-.704 2.009.099 3.68.099 3.68 2.144.08 3.553-.937 3.849-1.173a9.784 9.784 0 0 0 3.164.501h.08l.055-.003.107-.002.103-.005.003.002c1.01 1.44 2.788 1.646 2.788 1.646 1.264-1.332 1.337-2.653 1.337-2.94v-.058c0-.02-.003-.039-.003-.06.265-.187.52-.387.758-.6a7.875 7.875 0 0 0 1.415-1.7c1.43.083 2.437-.885 2.437-.885-.236-1.49-1.085-2.216-1.264-2.354l-.018-.013-.016-.013a.217.217 0 0 1-.031-.02c.008-.092.016-.18.02-.27.011-.162.016-.323.016-.48v-.253l-.005-.098-.008-.135a1.891 1.891 0 0 0-.01-.13c-.003-.042-.008-.083-.013-.125l-.016-.124-.018-.122a6.215 6.215 0 0 0-2.032-3.73 6.015 6.015 0 0 0-3.222-1.46 6.292 6.292 0 0 0-.85-.048l-.107.002h-.063l-.044.003-.104.008a4.777 4.777 0 0 0-3.335 1.695c-.332.4-.592.84-.768 1.297a4.594 4.594 0 0 0-.312 1.817l.003.091c.005.055.007.11.013.164a3.615 3.615 0 0 0 .698 1.82 3.53 3.53 0 0 0 1.827 1.282c.33.098.66.14.971.137.039 0 .078 0 .114-.002l.063-.003c.02 0 .041-.003.062-.003.034-.002.065-.007.099-.01.007 0 .018-.003.028-.003l.031-.005.06-.008a1.18 1.18 0 0 0 .112-.02c.036-.008.072-.013.109-.024a2.634 2.634 0 0 0 .914-.415c.028-.02.056-.041.085-.065a.248.248 0 0 0 .039-.35.244.244 0 0 0-.309-.06l-.078.042c-.09.044-.184.083-.283.116a2.476 2.476 0 0 1-.475.096c-.028.003-.054.006-.083.006l-.083.002c-.026 0-.054 0-.08-.002l-.102-.006h-.012l-.024.006c-.016-.003-.031-.003-.044-.006-.031-.002-.06-.007-.091-.01a2.59 2.59 0 0 1-.724-.213 2.557 2.557 0 0 1-.667-.438 2.52 2.52 0 0 1-.805-1.475 2.306 2.306 0 0 1-.029-.444l.006-.122v-.023l.002-.031c.003-.021.003-.04.005-.06a3.163 3.163 0 0 1 1.352-2.29 3.12 3.12 0 0 1 .937-.43 2.946 2.946 0 0 1 .776-.101h.06l.07.002.045.003h.026l.07.005a4.041 4.041 0 0 1 1.635.49 3.94 3.94 0 0 1 1.602 1.662 3.77 3.77 0 0 1 .397 1.414l.005.076.003.075c.002.026.002.05.002.075 0 .024.003.052 0 .07v.065l-.002.073-.008.174a6.195 6.195 0 0 1-.08.639 5.1 5.1 0 0 1-.267.927 5.31 5.31 0 0 1-.624 1.13 5.052 5.052 0 0 1-3.237 2.014 4.82 4.82 0 0 1-.649.066l-.039.003h-.287a6.607 6.607 0 0 1-1.716-.265 6.776 6.776 0 0 1-3.4-2.274 6.75 6.75 0 0 1-.746-1.15 6.616 6.616 0 0 1-.714-2.596l-.005-.083-.002-.02v-.056l-.003-.073v-.096l-.003-.104v-.07l.003-.163c.008-.22.026-.45.054-.678a8.707 8.707 0 0 1 .28-1.355c.128-.444.286-.872.473-1.277a7.04 7.04 0 0 1 1.456-2.1 5.925 5.925 0 0 1 .953-.763c.169-.111.343-.213.524-.306.089-.05.182-.091.273-.135.047-.02.093-.042.138-.062a7.177 7.177 0 0 1 .714-.267l.145-.045c.049-.015.098-.026.148-.041.098-.029.197-.052.296-.076.049-.013.1-.02.15-.033l.15-.032.151-.028.076-.013.075-.01.153-.024c.057-.01.114-.013.171-.023l.169-.021c.036-.003.073-.008.106-.01l.073-.008.036-.003.042-.002c.057-.003.114-.008.171-.01l.086-.006h.023l.037-.003.145-.007a7.999 7.999 0 0 1 1.708.125 7.917 7.917 0 0 1 2.048.68 8.253 8.253 0 0 1 1.672 1.09l.09.077.089.078c.06.052.114.107.171.159.057.052.112.106.166.16.052.055.107.107.159.164a8.671 8.671 0 0 1 1.41 1.978c.012.026.028.052.04.078l.04.078.075.156c.023.051.05.1.07.153l.065.15a8.848 8.848 0 0 1 .45 1.34.19.19 0 0 0 .201.142.186.186 0 0 0 .172-.184c.01-.246.002-.532-.024-.856z"/></svg>\ No newline at end of filediff --git a/src/client/public/images/connectors/jaeger.svg b/src/client/public/images/connectors/jaeger.svgnew file mode 100644index 00000000..74155a60--- /dev/null+++ b/src/client/public/images/connectors/jaeger.svg@@ -0,0 +1 @@+<svg fill="#66CFE3" role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Jaeger</title><path d="M14.816 22.3774c0 .0724-.1283.1311-.2865.1311-.1581 0-.2864-.0587-.2864-.131 0-.0725.1283-.1311.2864-.1311.1582 0 .2865.0586.2865.131Zm-.738.1554c0 .075-.126.136-.2815.136-.1555 0-.2816-.061-.2816-.136 0-.075.126-.136.2816-.136.1555 0 .2816.061.2816.136Zm-.7427.1408c0 .0697-.1739.1262-.3884.1262-.2145 0-.3884-.0565-.3884-.1262 0-.0697.174-.1263.3884-.1263.2145 0 .3884.0566.3884.1263Zm-1.136.1116c0 .0885-.1587.1602-.3544.1602-.1957 0-.3544-.0717-.3544-.1602 0-.0885.1587-.1602.3544-.1602.1957 0 .3544.0717.3544.1602Zm-1.1943.0389c0 .0965-.2369.1747-.5291.1747-.2923 0-.5292-.0782-.5292-.1747 0-.0966.237-.1748.5292-.1748s.5291.0782.5291.1748Zm1.107-.8787c.6116-.0292.7718-.1214.7912-.3253 0 0 .1214-.1505-.2913-.233-.4126-.0826-.9952-.0826-.8932-.4127.102-.335 1.1554-.3932 1.7865-.3932s1.3447-.0388 1.2525.301c-.0874.3398-.5486.3447-.5097.5728.0388.2282.4175.1602.3495.5195-.068.3592-1.7428.4903-2.9274.5923-1.1797.102-1.9321-.0195-1.7428-.3399.1893-.3204 1.573-.2524 2.1846-.2815Zm-9.9693 1.0755c.0066.045-.128.102-.3004.127-.1724.0251-.3176.0089-.3241-.0362-.0066-.0452.128-.102.3004-.1271.1725-.025.3176-.0088.3241.0363Zm.0918.226c.0077.053-.1474.1195-.3464.1484-.199.029-.3665.0094-.3742-.0437-.0078-.053.1473-.1195.3463-.1485.199-.0289.3666-.0093.3743.0438Zm.1648.204c.0077.053-.1473.1194-.3463.1484-.199.0289-.3666.0093-.3743-.0437-.0077-.0531.1473-.1196.3463-.1485.199-.029.3666-.0094.3743.0437Zm.7387.114c.0086.0638-.1501.138-.3547.166-.2046.0278-.3774-.0013-.3861-.065-.0087-.0638.15-.138.3546-.166.2046-.0278.3775.0013.3862.065Zm1.1803.1309c.0066.0828-.2394.17-.5494.1947s-.5667-.0225-.5733-.1053c-.0066-.0829.2394-.17.5494-.1947s.5667.0224.5733.1053Zm1.1926.0011c.0182.0838-.2006.2026-.4888.2653-.2882.0627-.5366.0456-.5548-.0382-.0182-.0839.2006-.2027.4888-.2654.2882-.0627.5366-.0455.5548.0383Zm1.2463-2.0331c.3544-.0777 1.9856-.0388 2.0292.2476.0534.2913-2.67.5437-3.2866.631-.6165.0875-.6408.2622-.6408.2622 0 .0146-.0534.1554.3495.1505.5632-.0097.9564.1068.7234.3787-.233.2719-2.8109.199-3.34 0-.5292-.199-.3204-.398-.0486-.5.2719-.0971 1.0438-.1894 1.937-.3593.8933-.1699 1.9274-.7282 2.2769-.8107Zm6.7917-9.7337a.6457.6457 0 1 1-1.2914 0 .6457.6457 0 0 1 1.2914 0ZM7.3155 13.44a.6457.6457 0 1 1-1.2914 0 .6457.6457 0 0 1 1.2914 0ZM4.325 20.023c-.3544 0-.602-.1409-.7573-.4176-.0631-.1068-.102-.2379-.1311-.3738-.0631-.335-.0437-.7282.0291-1.0972.0825-.4126.2282-.7961.3835-1.0389.1408-.2233.2719-.369.3884-.466a50.61 50.61 0 0 0 .5195 1.3205c.2378.5874.4563 1.1068.6165 1.4758-.0097.0194-.0243.034-.0388.0534-.233.3107-.5583.5437-1.0098.5437Zm15.0155-2.1944c0 .0146.0049.0388.0195.0728.0534.2913.233 1.0778.3543 1.5292-1.8642.2088-4.5051.3399-7.4325.3399-2.4759 0-4.7479-.0923-6.515-.2476a86.46 86.46 0 0 1-.6748-1.607c-.597-1.4563-1.3302-3.3933-1.5583-4.5779-.2088-1.0923-.4612-2.4176.1165-3.6119A28.5682 28.5682 0 0 1 3.32 9.697c-1.369-.1456-1.5535-.4127-1.6263-.5243-.0777-.1117-.0923-.2427-.0389-.3738.1214-.3059.534-.5826 1.0146-.8108a5.028 5.028 0 0 0 .136.2816c-.437.2088-.7768.432-.8593.6457-.0194.0437-.0097.0631.0049.0874.068.097.4611.398 2.6846.4709a.6316.6316 0 0 1-.5-.505.634.634 0 0 1 .0582-.3931c-.2039.1068-.4078.233-.602.364 0 0-1.6554-2.1748-.4709-2.8933.136-.0826.267-.1214.3981-.1214.9904 0 1.7234 2.1603 1.7234 2.1603s-.1456.0534-.3592.1505c.2621.0243.7816.1457 1.8302.568 1.068.4321 1.4952.4612 1.6118.4612A.625.625 0 0 1 9 9.3426l.034.034a22.885 22.885 0 0 0 .4563-.0389c.0485-.0048.1116-.0194.1942-.0388a.6277.6277 0 0 1 .2864-.5826.6193.6193 0 0 1 .733.0437.434.434 0 0 0 .0874-.034c.2719-.1359 1.2331-1.1602 1.9565-2.034a.616.616 0 0 1 .6117-.2137c.1941-.6553.9709-2.9759 2.1554-2.9759 1.3788 0 .5777 2.7283.4418 2.9225 0 0-.3204-.0242-.7573-.0242-.4855 0-1.1166.029-1.6312.165.0146.0097.034.0243.0534.0389.267.2184.301.6117.0825.8787-.1165.1359-.267.3204-.4417.5194.3592-.1456.733-.3058 1.1068-.466.6263-.2768 1.2089-.539 1.7234-.7914h.0098l.0048-.0097c.4418-.2136.8399-.4175 1.1748-.6068 1.17-.6554 1.3157-.9418 1.3302-1.0146.0097-.0486 0-.0631-.0048-.068-.0097-.0194-.0923-.097-.4612-.097-.403 0-.9661.0922-1.5535.2184a3.849 3.849 0 0 0 .0291-.3253c.5729-.1165 1.1214-.2039 1.5292-.2039.369 0 .5923.068.7088.2136.0534.0631.1068.1748.068.335-.0437.1893-.2428.5194-1.4807 1.2137a8.6623 8.6623 0 0 1-.3738.2039c-.0922.0534-.1942.102-.2961.1553.5582.471.9758 1.1748 1.2379 2.1943.5292 2.039.937 4.4275 1.2185 6.3208-.4612.0583-.7427.2767-.801.6311-.0631.3836.136.5923.3447.8156.2233.2379.4806.505.5631 1.1069Zm-4.84-12.331c0 .0244.0048.0195.0048.0195s.0242-.0437.063-.1165c.0438-.0728.102-.1748.17-.2961.0728-.1214.1553-.2573.2573-.3787.0485-.0583.1068-.1165.165-.1602.0632-.0437.1214-.0728.1845-.0825.0194 0 .034-.0049.0486-.0049.0145.0049.034 0 .0485.0049.0146.0048.034.0048.0486.0097l.0388.0097.0097.0048c0 .0049.0049.0049.0049.0097.0097.0146.0145.0437.0194.0777 0 .068-.0049.1505-.0194.2233-.0097.0729-.0292.1457-.0437.2136-.034.136-.0631.2476-.0826.3302-.0242.0825-.034.131-.034.131s.034-.034.0874-.102c.0534-.063.1214-.165.1942-.2912.034-.0631.0729-.136.102-.2185.0291-.0825.0582-.1747.068-.2815.0048-.0534.0048-.1165-.0195-.1845-.0048-.0194-.0145-.034-.0242-.0534l-.0146-.0243-.0194-.0243c-.0194-.029-.0971-.0776-.102-.0776l-.0097-.0049h-.0048l-.0146-.0048h-.0049l-.0194-.0097-.0437-.0146c-.0242-.0097-.0582-.0146-.0874-.0194-.029-.0049-.063-.0049-.0922-.0049-.0291.0049-.0631.0049-.0922.0097a.6844.6844 0 0 0-.3204.1554c-.0874.0728-.1505.1553-.204.2379-.1019.165-.1601.33-.199.4709-.034.1456-.0534.267-.0582.3495-.0049.0388-.0049.0728-.0049.097Zm-4.2868 3.578c-.0874.0534-.1116.165-.063.2524.0145.0195.0922.1311.2572.1942a.707.707 0 0 0 .2525.0437c.1456 0 .3058-.0388.4709-.1116.3592-.1602 1.2379-1.0487 2.2428-2.2575.0631-.0776.0534-.1942-.0242-.2573-.0437-.034-.0874-.0437-.1166-.0437a.1794.1794 0 0 0-.1407.068c-.4321.5195-1.6555 1.9225-2.0924 2.1458l-.0194.0097c-.0534.0194-.102.0388-.1505.0534l-.2233.0631-.1797-.1456c-.0291-.0292-.0728-.0437-.1165-.0437a.175.175 0 0 0-.097.029ZM9.17 14.304c.0582.335.5874.5243 1.1845.4272.5971-.097 1.039-.4515.9807-.7865-.0583-.335-.5875-.5243-1.1846-.4272-.5971.102-1.034.4564-.9806.7865ZM4.4172 7.9493a2.001 2.001 0 0 0 .0729.0922s-.0049-.0145-.0049-.034c-.0049-.0194-.0049-.0485-.0146-.0873-.0097-.0777-.0388-.1845-.0776-.3107a2.4088 2.4088 0 0 0-.1748-.4175 1.8868 1.8868 0 0 0-.1408-.2233c-.0534-.0729-.1165-.1457-.2039-.2136-.0485-.0292-.097-.0632-.165-.0826-.0146-.0048-.034-.0048-.0535-.0097h-.0728c-.0097 0-.0291.0049-.034.0049l-.029.0097-.0195.0048-.0049.0049-.0097.0048a.3371.3371 0 0 0-.1602.136.4473.4473 0 0 0-.0534.1747c-.0097.1068.0146.1942.034.272.0243.0776.0534.1504.0825.2087a1.493 1.493 0 0 0 .17.2718c.0242.0291.0436.0534.0582.068l.0243.0243s-.0097-.0437-.0243-.1166c-.0146-.0728-.0388-.1796-.0582-.301a2.2904 2.2904 0 0 1-.0243-.1941c-.0049-.068-.0049-.136.0097-.1894.0048-.0242.0146-.0437.0243-.0534.0097-.0048.0145-.0097.029-.0145h.0098l.0146-.0049c.0048 0 0 .0049 0 .0049h.0048a.2045.2045 0 0 1 .0631.034c.0486.0388.102.0922.1505.1504.0534.0632.102.1214.1457.1845.0873.1262.1747.2476.2476.3496a5.199 5.199 0 0 0 .1844.2524Zm.3544 1.0777.034.0049c.131.0146.5534.0922 1.5923.5097.8545.3447 1.5001.5195 1.9128.5195.3107 0 .3932-.0971.4223-.131.0437-.0535.0486-.1069.0437-.136-.0048-.0291-.0145-.0825-.068-.1262-.0436-.034-.0873-.0437-.1164-.0437-.0292 0-.0583.0048-.0826.0194l-.0874.0437h-.1165c-.2476 0-.7622-.0825-1.767-.4903-1.204-.4855-1.6458-.539-1.7769-.539-.0291 0-.0388 0-.0582.005-.0632.0145-.102.0533-.1166.0776-.0145.0194-.0388.0728-.029.136.0145.0873.0922.1504.1795.1504h.034Zm.0437 3.6945c0 1.1262.9127 2.0438 2.039 2.039 1.1263 0 2.039-.9128 2.039-2.039 0-1.1263-.9127-2.039-2.039-2.039s-2.039.9127-2.039 2.039Zm5.5246 3.3643c-.267.102-.5.2475-.7039.3738.0486.2184.199.7088.6311.5825.3544-.1068.1505-.733.0728-.9563Zm.6214.7816c.1166.0437.5778.0825.534-.4758-.0145-.1942-.029-.3301-.0388-.4224-.0437-.0048-.0825-.0048-.1213-.0048-.136 0-.2622-.0049-.369-.0049-.1117 0-.17.0049-.1942.0097a9.3046 9.3046 0 0 0-.1553.0292c.0388.2524.1456.7961.3446.869Zm1.7866-1.3788c.432-.1942.432-.5243.4078-.6942-.0292-.1796-.2962-.5583-1.3788-.568 0 0-.335.7816-1.2428.9273-.9078.1505-1.2962-.0243-1.4515-.2185 0 0-.8739.2573-.9467.8107-.0728.5535.6603.7477 1.0098.5535.3495-.1942.8738-.6166 1.573-.7088.3737-.0486 1.5971.0922 2.0292-.102Zm2.471-4.2333c0-1.1262-.9127-2.039-2.039-2.039s-2.039.9128-2.039 2.039c0 1.1263.9127 2.039 2.039 2.039s2.039-.9175 2.039-2.039ZM3.1162 18.1975c-.0437.267-.0728.6117-.0243.9564-.6602-.1408-1.034-.3058-1.034-.4757 0-.17.3835-.335 1.0583-.4807Zm18.4866.0389c.5729.136.8884.2864.8836.4418 0 .2476-.767.4757-2.0633.6553.233-.063.4952-.233.7962-.5146.1505-.1407.267-.3107.3495-.5a.5922.5922 0 0 0 .034-.0825Zm-.3495-.0728a1.0453 1.0453 0 0 1-.2767.3883c-.3884.369-.6214.4467-.7525.4467-.2573 0-.4466-.3593-.5583-1.0632-.0049-.0486-.0146-.0971-.0194-.1456-.0923-.6991-.4127-1.039-.6457-1.2817-.2039-.2136-.301-.3301-.2621-.5486.0534-.3155.4369-.3835.7476-.3835.1456 0 .2573.0146.2573.0146h.0194c.5728.0097 1.17.5291 1.4515 1.2525.1554.398.2185.903.0389 1.3205ZM4.7085 6.3618c-.1068-.1456-.2136-.267-.3253-.3592-.0097-.3981-.0097-.7574.0098-.8981.0388-.2622.6893-1.0244 1.2962-1.607.5097-.4903 1.4855-1.335 2.301-1.5.1263-.0243.272-.0389.4419-.0389 1.5535 0 4.6993 1.1651 4.8546 1.3205l.0097.0097c.0389-.3787.0777-.9078.0098-1.0583 0 0 .296.0922.3495.2767 0 0 .733-.738.6602-1.6554 0 0 .165 0 .1845.296 0 0 .5292-1.2524.9175-1.1408.3884.1117-.1262 1.8594-.4029 2.1895 0 0 .4418-.1068.4952-.403 0 0 .0534.2574-.0194.403 0 0 .9952-.8107.9563-1.1214 0 0 .1311.3155.0923.5534 0 0 .8253-.6602.8253-.937 0 0 .1262 1.6021-1.471 2.1895 0 0 .4224-.0534.6263-.0922 0 0-.1748.2281-.4418.5534-.1602-.102-.3496-.1505-.5632-.1505-.6408 0-1.2282.4855-1.7476 1.4419.0776-.4175-.0292-.7574-.1845-.7914-.0049-.0048-.0146-.0048-.0243-.0048-.1602 0-.3884.2767-.4806.6894-.0923.4369.0146.8058.1748.8398.0582.0146.131-.0194.2039-.0874-.1505.3593-.267.6942-.3447.9321-3.976 1.7914-6.5053 1.7527-7.656 1.573-.0825-.2137-.233-.5778-.4417-.9419-.0583-.097-.1166-.1941-.1748-.2815 2.7769.7476 6.4761-.6506 7.9568-1.2962-.0777-.2185-.0874-.5-.0194-.806.0631-.296.1942-.5485.3544-.7184-.0291-.1165-.0534-.199-.0728-.2427-.3399-.1845-3.1556-1.2331-4.6265-1.2331-.1457 0-.272.0145-.3787.034-1.301.267-3.277 2.5292-3.3497 2.8497-.0243.1553-.0146.6796.0048 1.2136Z"/></svg>\ No newline at end of filediff --git a/src/client/src/__tests__/app/api/manage-dashboard/query-run-route.test.ts b/src/client/src/__tests__/app/api/manage-dashboard/query-run-route.test.tsnew file mode 100644index 00000000..972c35fa--- /dev/null+++ b/src/client/src/__tests__/app/api/manage-dashboard/query-run-route.test.ts@@ -0,0 +1,89 @@+jest.mock("@/lib/session", () => ({+ getCurrentUser: jest.fn(),+}));+jest.mock("@/lib/platform/manage-dashboard/widget", () => ({+ runWidgetQuery: jest.fn(),+}));+jest.mock("@/lib/posthog", () => ({+ __esModule: true,+ default: { fireEvent: jest.fn() },+}));++import { POST } from "@/app/api/manage-dashboard/query/run/route";+import { getCurrentUser } from "@/lib/session";+import { runWidgetQuery } from "@/lib/platform/manage-dashboard/widget";++(globalThis as unknown as { Response: { json: unknown } }).Response = {+ json: (body: unknown, init?: ResponseInit) => ({+ status: init?.status ?? 200,+ json: async () => body,+ }),+};++function jsonRequest(body: unknown) {+ return {+ json: jest.fn().mockResolvedValue(body),+ } as unknown as Request;+}++function invalidJsonRequest() {+ return {+ json: jest.fn().mockRejectedValue(new Error("bad json")),+ } as unknown as Request;+}++describe("manage-dashboard query/run route", () => {+ beforeEach(() => {+ jest.clearAllMocks();+ (getCurrentUser as jest.Mock).mockResolvedValue({ id: "user1" });+ (runWidgetQuery as jest.Mock).mockResolvedValue({ data: [] });+ });++ it("requires authentication", async () => {+ (getCurrentUser as jest.Mock).mockResolvedValue(null);++ const response = await POST(+ jsonRequest({+ widgetId: "w1",+ filter: {},+ }) as never+ );++ expect(response.status).toBe(401);+ expect(runWidgetQuery).not.toHaveBeenCalled();+ });++ it("rejects invalid JSON", async () => {+ const response = await POST(invalidJsonRequest() as never);+ expect(response.status).toBe(400);+ expect(runWidgetQuery).not.toHaveBeenCalled();+ });++ it("rejects a missing widgetId", async () => {+ const response = await POST(jsonRequest({ filter: {} }) as never);+ expect(response.status).toBe(400);+ expect(runWidgetQuery).not.toHaveBeenCalled();+ });++ it("runs the widget query when authenticated", async () => {+ const response = await POST(+ jsonRequest({+ widgetId: "w1",+ filter: { timeLimit: { start: "", end: "" } },+ sourceId: "src-1",+ signal: "traces",+ }) as never+ );++ expect(response.status).toBe(200);+ expect(runWidgetQuery).toHaveBeenCalledWith("w1", {+ userQuery: undefined,+ filter: {+ timeLimit: { start: "", end: "" },+ environment: undefined,+ },+ sourceId: "src-1",+ signal: "traces",+ });+ });+});\ No newline at end of filediff --git a/src/client/src/__tests__/app/api/route-access-wraps.test.ts b/src/client/src/__tests__/app/api/route-access-wraps.test.tsnew file mode 100644index 00000000..1a44e75d--- /dev/null+++ b/src/client/src/__tests__/app/api/route-access-wraps.test.ts@@ -0,0 +1,66 @@+/**+ * Ensures new telemetry / connector / agents route surfaces export access+ * wrappers so EE path aliases can enforce RBAC + audit without CE forks.+ */+import { readdirSync, readFileSync, statSync } from "fs";+import { join } from "path";++const API_ROOT = join(__dirname, "../../../app/api");++function collectRouteFiles(dir: string, acc: string[] = []): string[] {+ for (const name of readdirSync(dir)) {+ const full = join(dir, name);+ if (statSync(full).isDirectory()) {+ collectRouteFiles(full, acc);+ } else if (name === "route.ts") {+ acc.push(full);+ }+ }+ return acc;+}++describe("new implementation route access wraps", () => {+ it("wraps connector mutation routes with connector access/audit hooks", () => {+ const files = [+ "connectors/route.ts",+ "connectors/[id]/route.ts",+ "connectors/[id]/health/route.ts",+ "connectors/bindings/route.ts",+ "project/environment/route.ts",+ ];+ for (const rel of files) {+ const source = readFileSync(join(API_ROOT, rel), "utf8");+ expect(source).toMatch(/withConnectorAccess/);+ if (rel.includes("bindings") || rel.includes("[id]") || rel.includes("environment")) {+ expect(source).toMatch(/withConnectorAudit|withConnectorAccess/);+ }+ }+ });++ it("wraps telemetry query routes with withRouteAccess", () => {+ const roots = ["metrics", "telemetry", "observability"].map((d) =>+ join(API_ROOT, d)+ );+ const files = roots.flatMap((root) => collectRouteFiles(root));+ expect(files.length).toBeGreaterThan(20);+ for (const file of files) {+ const source = readFileSync(file, "utf8");+ expect(source).toMatch(/withRouteAccess/);+ expect(source).not.toMatch(/"[a-z_]+:[a-z_]+"/);+ }+ });++ it("wraps agents and coding-agents routes with neutral access keys", () => {+ const agentFiles = collectRouteFiles(join(API_ROOT, "agents")).filter(+ (f) => !f.includes("materialize")+ );+ const codingFiles = collectRouteFiles(join(API_ROOT, "coding-agents"));+ expect(agentFiles.length).toBeGreaterThan(0);+ expect(codingFiles.length).toBeGreaterThan(0);+ for (const file of [...agentFiles, ...codingFiles]) {+ const source = readFileSync(file, "utf8");+ expect(source).toMatch(/withRouteAccess/);+ expect(source).not.toMatch(/"[a-z_]+:[a-z_]+"/);+ }+ });+});diff --git a/src/client/src/__tests__/components/agent-scope-provider.test.tsx b/src/client/src/__tests__/components/agent-scope-provider.test.tsxnew file mode 100644index 00000000..9fe49a02--- /dev/null+++ b/src/client/src/__tests__/components/agent-scope-provider.test.tsx@@ -0,0 +1,69 @@+import { render } from "@testing-library/react";+import AgentScopeProvider, {+ useIsAgentScoped,+} from "@/components/(playground)/agents/agent-scope-provider";+import { useRootStore } from "@/store";++function ScopeProbe() {+ const scoped = useIsAgentScoped();+ return <div data-testid="probe">{scoped ? "scoped" : "global"}</div>;+}++const serviceNames = (): string[] =>+ (useRootStore.getState().filter.details.selectedConfig?.serviceNames as+ | string[]+ | undefined) ?? [];++describe("AgentScopeProvider scope lock", () => {+ beforeEach(() => {+ // Reset any leftover scope between tests.+ useRootStore+ .getState()+ .filter.updateFilter("selectedConfig", {}, { clearFilter: true });+ });++ it("locks serviceNames on mount and clears the lock on unmount", () => {+ expect(serviceNames()).toEqual([]);++ const { getByTestId, unmount } = render(+ <AgentScopeProvider serviceName="demo-openai-app">+ <ScopeProbe />+ </AgentScopeProvider>+ );++ // Lock applied synchronously (layout effect) so children render scoped.+ expect(getByTestId("probe")).toHaveTextContent("scoped");+ expect(serviceNames()).toEqual(["demo-openai-app"]);++ // Restore runs in a layout-effect cleanup, so the agent scope is gone the+ // moment the page unmounts — before the next route (e.g. Telemetry) can+ // issue a query. This is what keeps the global list showing every service.+ unmount();+ expect(serviceNames()).toEqual([]);+ });++ it("does not leak one agent's scope into the next", () => {+ const first = render(+ <AgentScopeProvider serviceName="demo-openai-app">+ <ScopeProbe />+ </AgentScopeProvider>+ );+ expect(serviceNames()).toEqual(["demo-openai-app"]);+ first.unmount();+ expect(serviceNames()).toEqual([]);++ const second = render(+ <AgentScopeProvider serviceName="demo-anthropic-app">+ <ScopeProbe />+ </AgentScopeProvider>+ );+ expect(serviceNames()).toEqual(["demo-anthropic-app"]);+ second.unmount();+ expect(serviceNames()).toEqual([]);+ });++ it("useIsAgentScoped is false outside any provider", () => {+ const { getByTestId } = render(<ScopeProbe />);+ expect(getByTestId("probe")).toHaveTextContent("global");+ });+});diff --git a/src/client/src/__tests__/components/filter-time-capabilities.test.ts b/src/client/src/__tests__/components/filter-time-capabilities.test.tsnew file mode 100644index 00000000..a1ba73a9--- /dev/null+++ b/src/client/src/__tests__/components/filter-time-capabilities.test.ts@@ -0,0 +1,37 @@+import {+ clampDateRangeToLimit,+ effectiveRangeLimit,+ isPresetRangeSupported,+} from "@/components/(playground)/filter";++describe("connector-driven telemetry time ranges", () => {+ it("uses the tighter of a connector's query and retention limits", () => {+ expect(effectiveRangeLimit({+ capabilities: { maxTimeRangeMs: 7_200_000, maxLookbackMs: 3_600_000 },+ })).toBe(3_600_000);+ expect(effectiveRangeLimit({ capabilities: {} })).toBeUndefined();+ });++ it("keeps all presets available when the datasource advertises no limit", () => {+ expect(isPresetRangeSupported("3M")).toBe(true);+ expect(isPresetRangeSupported("CUSTOM")).toBe(true);+ });++ it("disables only presets wider than the selected datasource supports", () => {+ const sevenDays = 7 * 24 * 60 * 60 * 1000;+ expect(isPresetRangeSupported("24H", sevenDays)).toBe(true);+ expect(isPresetRangeSupported("7D", sevenDays)).toBe(true);+ expect(isPresetRangeSupported("1M", sevenDays)).toBe(false);+ });++ it("clamps a custom selection at the datasource boundary", () => {+ const end = new Date("2026-08-06T00:00:00.000Z");+ const start = new Date("2026-07-01T00:00:00.000Z");+ const max = 7 * 24 * 60 * 60 * 1000;+ expect(clampDateRangeToLimit(start, end, max)).toEqual({+ start: new Date("2026-07-30T00:00:00.000Z"),+ end,+ clamped: true,+ });+ });+});diff --git a/src/client/src/__tests__/components/manage-dashboard/formatters.test.ts b/src/client/src/__tests__/components/manage-dashboard/formatters.test.tsnew file mode 100644index 00000000..05bb5aef--- /dev/null+++ b/src/client/src/__tests__/components/manage-dashboard/formatters.test.ts@@ -0,0 +1,17 @@+import { formatCompactNumber } from "@/components/(playground)/manage-dashboard/board-creator/utils/formatters";++describe("formatCompactNumber", () => {+ it("adds compact suffixes to large stat values", () => {+ expect(formatCompactNumber(1_250)).toBe("1.25K");+ expect(formatCompactNumber(3_400_000)).toBe("3.4M");+ });++ it("rounds normal values without losing small decimal values", () => {+ expect(formatCompactNumber(12.3456)).toBe("12.35");+ expect(formatCompactNumber(0.0001234)).toBe("0.0001");+ });++ it("preserves non-numeric values", () => {+ expect(formatCompactNumber("healthy")).toBe("healthy");+ });+});diff --git a/src/client/src/__tests__/components/manage-dashboard/structured-query-builder.test.tsx b/src/client/src/__tests__/components/manage-dashboard/structured-query-builder.test.tsxnew file mode 100644index 00000000..278a3bc1--- /dev/null+++ b/src/client/src/__tests__/components/manage-dashboard/structured-query-builder.test.tsx@@ -0,0 +1,151 @@+import { fireEvent, render, screen } from "@testing-library/react";+import { StructuredQueryBuilder } from "@/components/(playground)/manage-dashboard/board-creator/components/structured-query-builder";+import {+ WIDGET_STRUCTURED_ADD_FILTER,+ WIDGET_STRUCTURED_FIELD_PLACEHOLDER,+ WIDGET_STRUCTURED_GROUP_BY_PLACEHOLDER,+ WIDGET_STRUCTURED_SORT_FIELD_PLACEHOLDER,+} from "@/constants/messages/en";++const fireRequest = jest.fn();++class ResizeObserverMock {+ observe() {}+ unobserve() {}+ disconnect() {}+}++Object.defineProperty(global, "ResizeObserver", {+ writable: true,+ value: ResizeObserverMock,+});+Object.defineProperty(HTMLElement.prototype, "scrollIntoView", {+ writable: true,+ value: jest.fn(),+});++jest.mock("@/utils/hooks/useFetchWrapper", () => ({+ __esModule: true,+ default: () => ({ fireRequest }),+}));++describe("StructuredQueryBuilder", () => {+ beforeEach(() => jest.clearAllMocks());++ it("keeps an incomplete filter as a UI draft when Add Filter is clicked", () => {+ const onChange = jest.fn();+ render(+ <StructuredQueryBuilder+ signals={["traces"]}+ value={{ mode: "timeseries", query: { signal: "traces" } }}+ onChange={onChange}+ />+ );++ fireEvent.click(+ screen.getByRole("button", { name: WIDGET_STRUCTURED_ADD_FILTER })+ );++ expect(onChange).toHaveBeenCalledTimes(1);+ const next = onChange.mock.calls[0][0];+ expect(next.draftFilters).toEqual([+ {+ target: "attribute",+ scope: "span",+ key: "",+ op: "eq",+ value: "",+ },+ ]);+ expect(next.query.filters).toBeUndefined();+ });++ it("uses a searchable rule-engine style combobox for the sort field", () => {+ const onChange = jest.fn();+ render(+ <StructuredQueryBuilder+ signals={["traces"]}+ value={{ mode: "timeseries", query: { signal: "traces" } }}+ onChange={onChange}+ />+ );++ fireEvent.click(+ screen.getByRole("combobox", {+ name: WIDGET_STRUCTURED_SORT_FIELD_PLACEHOLDER,+ })+ );+ fireEvent.click(screen.getByRole("option", { name: "service.name" }));++ expect(onChange).toHaveBeenCalledWith(+ expect.objectContaining({+ query: expect.objectContaining({+ sort: [{ field: "service.name", direction: "asc" }],+ }),+ })+ );+ });++ it("uses the searchable combobox for group-by properties", () => {+ const onChange = jest.fn();+ render(+ <StructuredQueryBuilder+ signals={["traces"]}+ value={{ mode: "aggregate", query: { signal: "traces" } }}+ onChange={onChange}+ />+ );++ fireEvent.click(+ screen.getByRole("combobox", {+ name: WIDGET_STRUCTURED_GROUP_BY_PLACEHOLDER,+ })+ );+ fireEvent.click(screen.getByRole("option", { name: "service.name" }));++ expect(onChange).toHaveBeenCalledWith(+ expect.objectContaining({+ query: expect.objectContaining({ groupBy: ["service.name"] }),+ })+ );+ });++ it("uses the searchable combobox for aggregation properties", () => {+ const onChange = jest.fn();+ render(+ <StructuredQueryBuilder+ signals={["traces"]}+ value={{+ mode: "aggregate",+ query: {+ signal: "traces",+ aggregations: [{ fn: "avg" }],+ },+ }}+ onChange={onChange}+ />+ );++ fireEvent.click(+ screen.getByRole("combobox", {+ name: WIDGET_STRUCTURED_FIELD_PLACEHOLDER,+ })+ );+ fireEvent.click(+ screen.getByRole("option", { name: "gen_ai.request.model" })+ );++ expect(onChange).toHaveBeenCalledWith(+ expect.objectContaining({+ query: expect.objectContaining({+ aggregations: [+ expect.objectContaining({+ fn: "avg",+ field: "gen_ai.request.model",+ }),+ ],+ }),+ })+ );+ });+});diff --git a/src/client/src/__tests__/components/project-page-header.test.tsx b/src/client/src/__tests__/components/project-page-header.test.tsxindex 28a98db6..326c76d0 100644--- a/src/client/src/__tests__/components/project-page-header.test.tsx+++ b/src/client/src/__tests__/components/project-page-header.test.tsx@@ -14,13 +14,13 @@ jest.mock("@/components/(playground)/feature-page-header", () => ({}));describe("ProjectPageHeader", () => {- it("renders project identity, left back control, and status badges", () => {+ it("renders project identity, back control, and route actions", () => {render(<ProjectPageHeader project={{ name: "Production", isCurrent: true, isDefault: true }} />);expect(screen.getByRole("heading", { name: "Production" })).toBeInTheDocument();expect(screen.getByRole("link", { name: "Back to Organisation" })).toHaveAttribute("href", "/organisation");- expect(screen.getByText("Current")).toBeInTheDocument();- expect(screen.getByText("Default Project")).toBeInTheDocument();+ expect(screen.getByRole("link", { name: /Overview/ })).toBeInTheDocument();+ expect(screen.getByRole("link", { name: /Manage Environments/ })).toBeInTheDocument();});it("uses the loading title when no project is available", () => {diff --git a/src/client/src/__tests__/helpers/client/connector-analytics.test.ts b/src/client/src/__tests__/helpers/client/connector-analytics.test.tsnew file mode 100644index 00000000..2bd1ebcc--- /dev/null+++ b/src/client/src/__tests__/helpers/client/connector-analytics.test.ts@@ -0,0 +1,51 @@+import {+ BUILTIN_ROUTING_VALUE,+ classifySignalRoutingChange,+ connectorCreateEventProps,+ signalRoutingChangedEventProps,+} from "@/helpers/client/connector-analytics";++describe("connector analytics helpers", () => {+ it("classifies first bind, switch, and clear", () => {+ expect(classifySignalRoutingChange(null, "src-1")).toBe("bound");+ expect(classifySignalRoutingChange("src-1", "src-2")).toBe("switched");+ expect(classifySignalRoutingChange("src-1", BUILTIN_ROUTING_VALUE)).toBe(+ "cleared"+ );+ expect(classifySignalRoutingChange("src-1", "")).toBe("cleared");+ });++ it("treats rebinding the same source as bound (idempotent)", () => {+ expect(classifySignalRoutingChange("src-1", "src-1")).toBe("bound");+ });++ it("builds connector create props without secrets", () => {+ expect(+ connectorCreateEventProps({ type: "tempo", environment: "Staging" })+ ).toEqual({+ connector_type: "tempo",+ environment: "staging",+ });+ });++ it("builds signal routing change props for PostHog", () => {+ expect(+ signalRoutingChangedEventProps({+ signal: "traces",+ environment: "production",+ previousSourceId: "src-a",+ nextSourceId: "src-b",+ previousConnectorType: "tempo",+ nextConnectorType: "jaeger",+ })+ ).toEqual({+ signal: "traces",+ environment: "production",+ change: "switched",+ previous_source_id: "src-a",+ next_source_id: "src-b",+ previous_connector_type: "tempo",+ next_connector_type: "jaeger",+ });+ });+});diff --git a/src/client/src/__tests__/helpers/client/database-config.test.ts b/src/client/src/__tests__/helpers/client/database-config.test.tsindex 5aa5d447..a3cdd750 100644--- a/src/client/src/__tests__/helpers/client/database-config.test.ts+++ b/src/client/src/__tests__/helpers/client/database-config.test.ts@@ -59,6 +59,14 @@ describe('fetchDatabaseConfigList', () => {await fetchDatabaseConfigList(successCb);expect(successCb).toHaveBeenCalledWith([]);});++ it('coerces non-array payloads to an empty list', async () => {+ (asaw as jest.Mock).mockResolvedValue([null, '<html>login</html>']);+ const successCb = jest.fn();+ await fetchDatabaseConfigList(successCb);+ expect(successCb).toHaveBeenCalledWith([]);+ expect(mockSetList).toHaveBeenCalledWith([]);+ });});describe('pingActiveDatabaseConfig', () => {diff --git a/src/client/src/__tests__/helpers/client/observability-registry.test.ts b/src/client/src/__tests__/helpers/client/observability-registry.test.tsindex 53cc7e02..783ca80f 100644--- a/src/client/src/__tests__/helpers/client/observability-registry.test.ts+++ b/src/client/src/__tests__/helpers/client/observability-registry.test.ts@@ -14,7 +14,7 @@ describe("observability signal registry", () => {const traces = getSignalConfig("traces");expect(traces.key).toBe("traces");- expect(traces.listUrl).toBe("/api/telemetry/trace");+ expect(traces.listUrl).toBe("/api/telemetry/request");expect(traces.getRowId({ spanId: "span-1" })).toBe("span-1");expect(traces.getDetailHref({ spanId: "span-1" }, "/agents/a?tab=monitoring")).toBe("/telemetry/traces/span-1?from=%2Fagents%2Fa%3Ftab%3Dmonitoring"diff --git a/src/client/src/__tests__/helpers/client/observability.test.ts b/src/client/src/__tests__/helpers/client/observability.test.tsindex 93fd93af..e1fd85de 100644--- a/src/client/src/__tests__/helpers/client/observability.test.ts+++ b/src/client/src/__tests__/helpers/client/observability.test.ts@@ -6,25 +6,39 @@ import { create } from "zustand";const createStore = () => create<any>()(withLenses({ filter: filterStoreSlice }));describe("prepareObservabilitySignalChange", () => {- it("clears available-options config and selected filters on signal change", () => {+ it("clears grouping, sorting, offset, and selected filters", () => {const updateConfig = jest.fn();const updateFilter = jest.fn();+ const updateAttributeKeys = jest.fn();- prepareObservabilitySignalChange(updateConfig, updateFilter);+ prepareObservabilitySignalChange(+ updateConfig,+ updateFilter,+ updateAttributeKeys+ );expect(updateConfig).toHaveBeenCalledWith(undefined);- expect(updateFilter).toHaveBeenCalledWith("selectedConfig", {}, {- clearFilter: true,- });expect(updateFilter).toHaveBeenCalledWith("groupBy", null);+ expect(updateFilter).toHaveBeenCalledWith(+ "selectedConfig",+ {},+ { clearFilter: true }+ );+ expect(updateAttributeKeys).toHaveBeenCalledWith({+ spanAttributeKeys: [],+ resourceAttributeKeys: [],+ logAttributeKeys: [],+ scopeAttributeKeys: [],+ metricAttributeKeys: [],+ });});- it("wipes selectedConfig so Traces filters cannot empty Exceptions", () => {+ it("wipes selectedConfig so logs/metrics filters cannot leak across tabs", () => {const store = createStore();store.getState().filter.updateFilter("selectedConfig", {- models: ["gpt-4o-mini"],- providers: ["openai"],+ metricNames: ["up"],+ severities: ["error"],services: ["api"],});store.getState().filter.updateFilter("groupBy", "serviceName");@@ -40,21 +54,29 @@ describe("prepareObservabilitySignalChange", () => {// E3: sort key applied on the previous tab gets reset so the// new tab doesn't try to ORDER BY a column it doesn't have.- it("resets sorting and offset when changing signal", () => {+ it("preserves agent scope fields when clearing signal filters", () => {const store = createStore();- store.getState().filter.updateFilter("sorting", {- type: "Tokens",- direction: "asc",+ store.getState().filter.updateFilter("selectedConfig", {+ serviceNames: ["demo-openai-app"],+ environments: ["production"],+ metricNames: ["up"],+ severities: ["error"],});- store.getState().filter.updateFilter("offset", 50);prepareObservabilitySignalChange(store.getState().filter.updateConfig,- store.getState().filter.updateFilter+ store.getState().filter.updateFilter,+ undefined,+ {+ serviceNames: ["demo-openai-app"],+ environments: ["production"],+ });- expect(store.getState().filter.details.sorting).toEqual(DEFAULT_SORTING);- expect(store.getState().filter.details.offset).toBe(0);+ expect(store.getState().filter.details.selectedConfig).toEqual({+ serviceNames: ["demo-openai-app"],+ environments: ["production"],+ });});});diff --git a/src/client/src/__tests__/helpers/server/trace.test.ts b/src/client/src/__tests__/helpers/server/trace.test.tsindex 6e64efba..bcfa70da 100644--- a/src/client/src/__tests__/helpers/server/trace.test.ts+++ b/src/client/src/__tests__/helpers/server/trace.test.ts@@ -67,4 +67,26 @@ describe('buildHierarchy', () => {const result = buildHierarchy(data) as any;expect(result.SpanId).toBe('root');});++ it('promotes orphan tops when the true root is missing', () => {+ const data = [+ { SpanId: 'a', ParentSpanId: 'missing', Timestamp: '2026-01-01T00:00:02.000Z' },+ { SpanId: 'b', ParentSpanId: 'missing', Timestamp: '2026-01-01T00:00:01.000Z' },+ { SpanId: 'c', ParentSpanId: 'a', Timestamp: '2026-01-01T00:00:03.000Z' },+ ];+ const result = buildHierarchy(data) as any;+ expect(result.SpanId).toBe('b');+ expect(result.children.map((child: any) => child.SpanId)).toEqual(['a']);+ expect(result.children[0].children.map((child: any) => child.SpanId)).toEqual(['c']);+ });++ it('treats all-zero parent ids as roots', () => {+ const data = [+ { SpanId: 'root', ParentSpanId: '0000000000000000' },+ { SpanId: 'child', ParentSpanId: 'root' },+ ];+ const result = buildHierarchy(data) as any;+ expect(result.SpanId).toBe('root');+ expect(result.children).toHaveLength(1);+ });});diff --git a/src/client/src/__tests__/lib/access/connector-entitlement.test.ts b/src/client/src/__tests__/lib/access/connector-entitlement.test.tsnew file mode 100644index 00000000..ae86aab7--- /dev/null+++ b/src/client/src/__tests__/lib/access/connector-entitlement.test.ts@@ -0,0 +1,22 @@+import {+ assertPremiumConnectorAllowed,+ getConnectorCatalogLocks,+} from "@/lib/access/connector-entitlement";++describe("CE connector entitlement fallback", () => {+ it("allows any connector type without checking entitlements", async () => {+ await expect(+ assertPremiumConnectorAllowed("datadog")+ ).resolves.toBeUndefined();+ await expect(+ assertPremiumConnectorAllowed("tempo")+ ).resolves.toBeUndefined();+ });++ it("returns an unlocked catalog with no premium types", async () => {+ await expect(getConnectorCatalogLocks()).resolves.toEqual({+ entitled: true,+ premiumTypes: new Set(),+ });+ });+});diff --git a/src/client/src/__tests__/lib/db-config.test.ts b/src/client/src/__tests__/lib/db-config.test.tsindex d226c4f2..42d83015 100644--- a/src/client/src/__tests__/lib/db-config.test.ts+++ b/src/client/src/__tests__/lib/db-config.test.ts@@ -29,6 +29,10 @@ jest.mock('@/lib/prisma', () => ({user: {findUnique: jest.fn(),},+ connectorInstance: {+ upsert: jest.fn(),+ deleteMany: jest.fn(),+ },},}));jest.mock('@/lib/session', () => ({@@ -69,6 +73,7 @@ import {getDBConfigById,getDBConfigByIdInternal,getDBConfigByIdForUser,+ getDBConfigByIdForBackground,upsertDBConfig,deleteDBConfig,setCurrentDBConfig,@@ -79,6 +84,7 @@ import prisma from '@/lib/prisma';import { getCurrentUser } from '@/lib/session';import { getCurrentOrganisation, getCurrentProjectForOrganisation } from '../../lib/organisation';import asaw from '@/utils/asaw';+import migrations from '@/clickhouse/migrations';const mockUser = { id: 'u1', email: '[email protected]' };const mockOrg = { id: 'org1', name: 'Test Org' };@@ -106,6 +112,8 @@ beforeEach(() => {(prisma.databaseConfig.findFirst as jest.Mock).mockResolvedValue(null);(prisma.databaseConfig.findUnique as jest.Mock).mockResolvedValue(null);(prisma.databaseConfigUser.count as jest.Mock).mockResolvedValue(0);+ (prisma.connectorInstance.upsert as jest.Mock).mockResolvedValue({});+ (prisma.connectorInstance.deleteMany as jest.Mock).mockResolvedValue({ count: 1 });});describe('getDBConfigByUser', () => {@@ -207,6 +215,33 @@ describe('getDBConfigByIdInternal', () => {});});+describe('getDBConfigByIdForBackground', () => {+ it('uses the internal lookup when there is no user session', async () => {+ (getCurrentUser as jest.Mock).mockResolvedValue(null);+ (prisma.databaseConfig.findUnique as jest.Mock).mockResolvedValue(mockDbConfig);+ const result = await getDBConfigByIdForBackground({ id: 'db1' });+ expect(result).toEqual(mockDbConfig);+ expect(prisma.databaseConfig.findUnique).toHaveBeenCalledWith({ where: { id: 'db1' } });+ expect(prisma.databaseConfigUser.findFirst).not.toHaveBeenCalled();+ });++ it('keeps user and project scoping when a session exists', async () => {+ (prisma.databaseConfigUser.findFirst as jest.Mock).mockResolvedValue({+ databaseConfig: mockDbConfig,+ });+ const result = await getDBConfigByIdForBackground({ id: 'db1' });+ expect(result).toEqual(mockDbConfig);+ expect(prisma.databaseConfigUser.findFirst).toHaveBeenCalledWith(+ expect.objectContaining({+ where: expect.objectContaining({+ userId: 'u1',+ databaseConfigId: 'db1',+ }),+ })+ );+ });+});+describe('getDBConfigByIdForUser', () => {it('returns null when the current organisation has no accessible project', async () => {(getCurrentProjectForOrganisation as jest.Mock).mockResolvedValue(null);@@ -291,6 +326,7 @@ describe('upsertDBConfig', () => {const result = await upsertDBConfig(validConfig);expect(result).toBe('Added db details successfully');+ expect(migrations).toHaveBeenCalledWith('new-db');});it('creates config without project (no-project create path)', async () => {diff --git a/src/client/src/__tests__/lib/platform/agents/aggregate-graph.test.ts b/src/client/src/__tests__/lib/platform/agents/aggregate-graph.test.tsindex 46160508..3d50e495 100644--- a/src/client/src/__tests__/lib/platform/agents/aggregate-graph.test.ts+++ b/src/client/src/__tests__/lib/platform/agents/aggregate-graph.test.ts@@ -10,9 +10,23 @@jest.mock("@/lib/platform/common", () => ({dataCollector: jest.fn(),+ intelligenceDataCollector: jest.fn(),OTEL_TRACES_TABLE_NAME: "otel_traces",OTEL_LOGS_TABLE_NAME: "otel_logs",}));+jest.mock("@/lib/platform/connectors/datasource/facade", () => ({+ resolveSignalReadContext: jest.fn(),+}));+jest.mock("@/lib/platform/coding-agents/source", () => ({+ resolveCodingAgentsClickHouseDbConfigId: jest.fn(),+}));+jest.mock("@/lib/platform/agents/logger", () => ({+ agentsLogger: {+ error: jest.fn(),+ info: jest.fn(),+ warn: jest.fn(),+ },+}));import {assignDepths,@@ -20,9 +34,16 @@ import {type AggregateEdge,type AggregateNode,} from "@/lib/platform/agents/aggregate-graph";-import { dataCollector } from "@/lib/platform/common";+import { intelligenceDataCollector } from "@/lib/platform/common";+import { resolveSignalReadContext } from "@/lib/platform/connectors/datasource/facade";+import { resolveCodingAgentsClickHouseDbConfigId } from "@/lib/platform/coding-agents/source";+import { agentsLogger } from "@/lib/platform/agents/logger";-const mockDataCollector = jest.mocked(dataCollector);+const mockIntelligenceDataCollector = jest.mocked(intelligenceDataCollector);+const mockResolveSignalReadContext = jest.mocked(resolveSignalReadContext);+const mockResolveClickHouseId = jest.mocked(+ resolveCodingAgentsClickHouseDbConfigId+);function node(id: string): AggregateNode {return {@@ -55,8 +76,6 @@ describe("assignDepths", () => {});it("uses the longest path when a node has multiple parents", () => {- // root -> mid -> leaf- // \-----------> leaf (still depth=2 because mid is the longer path)const nodes = [node("root"), node("mid"), node("leaf")];const edges = [edge("root", "mid"), edge("mid", "leaf"), edge("root", "leaf")];assignDepths(nodes, edges);@@ -74,7 +93,6 @@ describe("assignDepths", () => {});it("clamps cycles to depth 0 instead of looping forever", () => {- // Real production traces shouldn't contain cycles; guard anyway.const nodes = [node("a"), node("b")];assignDepths(nodes, [edge("a", "b"), edge("b", "a")]);expect(Number.isFinite(nodes[0].depth)).toBe(true);@@ -83,7 +101,6 @@ describe("assignDepths", () => {it("ignores edges that reference missing nodes", () => {const nodes = [node("root")];- // `phantom` doesn't exist in nodes — depth assignment should ignore it.assignDepths(nodes, [edge("root", "phantom"), edge("phantom", "root")]);expect(nodes[0].depth).toBe(0);});@@ -111,11 +128,22 @@ describe("assignDepths", () => {describe("getAggregateGraph", () => {beforeEach(() => {jest.clearAllMocks();- mockDataCollector.mockResolvedValue({ err: null, data: [] });+ mockIntelligenceDataCollector.mockResolvedValue({ err: null, data: [] });+ mockResolveSignalReadContext.mockResolvedValue({+ isBuiltIn: true,+ adapter: {} as never,+ descriptor: {+ id: "builtin",+ type: "clickhouse",+ isBuiltIn: true,+ name: "ClickHouse",+ },+ });+ mockResolveClickHouseId.mockResolvedValue("db-routed");});it("normalizes span, edge, tool, and trace-count rows", async () => {- mockDataCollector.mockResolvedValueOnce({+ mockIntelligenceDataCollector.mockResolvedValueOnce({err: null,data: [{@@ -243,11 +271,15 @@ describe("getAggregateGraph", () => {]),});- const { query } = mockDataCollector.mock.calls[0][0] as { query: string };+ const { query } = mockIntelligenceDataCollector.mock.calls[0][0] as {+ query: string;+ };expect(query).toContain("ServiceName = 'api'");- expect(query).toContain("ResourceAttributes['deployment.environment'] = 'production'");+ expect(query).toContain(+ "ResourceAttributes['deployment.environment'] = 'production'"+ );expect(query).toContain("LIMIT 50");- expect(mockDataCollector).toHaveBeenCalledWith(+ expect(mockIntelligenceDataCollector).toHaveBeenCalledWith({ query: expect.any(String) },"query","db-1"@@ -257,7 +289,9 @@ describe("getAggregateGraph", () => {it("uses default environment and 24h fallback without a version filter", async () => {await getAggregateGraph({ serviceName: "api" });- const { query } = mockDataCollector.mock.calls[0][0] as { query: string };+ const { query } = mockIntelligenceDataCollector.mock.calls[0][0] as {+ query: string;+ };expect(query).toContain("ResourceAttributes['deployment.environment'] IN ('default', 'local', 'default_environment', '')");@@ -265,9 +299,65 @@ describe("getAggregateGraph", () => {expect(query).toContain("LIMIT 500");});+ it("falls back to ClickHouse when Tempo sampling returns no traces", async () => {+ mockResolveSignalReadContext.mockResolvedValue({+ isBuiltIn: false,+ adapter: {+ sampleTracesForGraph: jest.fn().mockResolvedValue([]),+ } as never,+ descriptor: {+ id: "tempo-1",+ type: "tempo",+ isBuiltIn: false,+ name: "Tempo",+ },+ });+ mockIntelligenceDataCollector.mockResolvedValueOnce({+ err: null,+ data: [+ {+ kind: "node",+ source: "root",+ target: "",+ edge_count: "2",+ p50_ms: "5",+ error_rate: "0",+ },+ {+ kind: "trace_count",+ source: "",+ target: "",+ edge_count: "2",+ p50_ms: 0,+ error_rate: 0,+ },+ ],+ });++ const graph = await getAggregateGraph({+ serviceName: "weather-agent",+ environment: "production",+ });++ expect(graph.traceCount).toBe(2);+ expect(graph.nodes).toEqual([+ expect.objectContaining({ id: "root", spanCount: 2 }),+ ]);+ expect(mockResolveClickHouseId).toHaveBeenCalledWith({+ environment: "production",+ });+ expect(mockIntelligenceDataCollector).toHaveBeenCalledWith(+ expect.anything(),+ "query",+ "db-routed"+ );+ });+it("logs query failures and returns an empty graph", async () => {- const errorSpy = jest.spyOn(console, "error").mockImplementation();- mockDataCollector.mockResolvedValueOnce({ err: "boom", data: [] });+ mockIntelligenceDataCollector.mockResolvedValueOnce({+ err: "boom",+ data: [],+ });await expect(getAggregateGraph({ serviceName: "api" })).resolves.toEqual({nodes: [],@@ -276,9 +366,9 @@ describe("getAggregateGraph", () => {spanCount: 0,});- expect(errorSpy).toHaveBeenCalledWith(- expect.stringContaining("aggregate_graph_query_failed")+ expect(agentsLogger.error).toHaveBeenCalledWith(+ "aggregate_graph_query_failed",+ expect.objectContaining({ err: "boom" }));- errorSpy.mockRestore();});});diff --git a/src/client/src/__tests__/lib/platform/agents/cache.test.ts b/src/client/src/__tests__/lib/platform/agents/cache.test.tsindex 88a0822c..7deb6d2d 100644--- a/src/client/src/__tests__/lib/platform/agents/cache.test.ts+++ b/src/client/src/__tests__/lib/platform/agents/cache.test.ts@@ -94,4 +94,31 @@ describe("swr cache", () => {invalidatePrefix("agents:list:db1:");expect(_cacheSizeForTests()).toBe(1);});++ it("uses policyFor so empty results leave the long fresh window", async () => {+ let calls = 0;+ const loader = jest.fn(async () => {+ calls += 1;+ return { nodes: [] as string[], edges: [] as string[], n: calls };+ });+ const longPolicy = { freshMs: 60_000, staleMs: 60_000 };+ const emptyPolicy = { freshMs: 1, staleMs: 1 };++ await swr("graph-empty", longPolicy, loader, {+ policyFor: (value) =>+ value.nodes.length === 0 && value.edges.length === 0+ ? emptyPolicy+ : longPolicy,+ });+ await new Promise((r) => setTimeout(r, 15));+ const second = await swr("graph-empty", longPolicy, loader, {+ policyFor: (value) =>+ value.nodes.length === 0 && value.edges.length === 0+ ? emptyPolicy+ : longPolicy,+ });++ expect(loader).toHaveBeenCalledTimes(2);+ expect(second.n).toBe(2);+ });});diff --git a/src/client/src/__tests__/lib/platform/agents/index.test.ts b/src/client/src/__tests__/lib/platform/agents/index.test.tsindex dfb7c080..5ace1ce8 100644--- a/src/client/src/__tests__/lib/platform/agents/index.test.ts+++ b/src/client/src/__tests__/lib/platform/agents/index.test.ts@@ -2,10 +2,14 @@* Unified agent listing query + computeAgentKey helper.*/-jest.mock("@/lib/platform/common", () => ({- dataCollector: jest.fn(),- OTEL_TRACES_TABLE_NAME: "otel_traces",-}));+jest.mock("@/lib/platform/common", () => {+ const collector = jest.fn();+ return {+ dataCollector: collector,+ intelligenceDataCollector: collector,+ OTEL_TRACES_TABLE_NAME: "otel_traces",+ };+});jest.mock("@/lib/platform/agents/cache", () => ({swr: jest.fn(<T,>(_key: string, _policy: unknown, loader: () => Promise<T>) =>loader()@@ -18,7 +22,7 @@ jest.mock("@/lib/platform/agents/cache", () => ({POLICY_TOOLS: {},}));-import { dataCollector } from "@/lib/platform/common";+import { intelligenceDataCollector } from "@/lib/platform/common";import {computeAgentKey,deploymentEnvironmentSqlPredicate,@@ -29,7 +33,9 @@ import {} from "@/lib/platform/agents";import { invalidate as invalidateCache } from "@/lib/platform/agents/cache";-const mockedDataCollector = dataCollector as jest.MockedFunction<typeof dataCollector>;+const mockedDataCollector = intelligenceDataCollector as jest.MockedFunction<+ typeof intelligenceDataCollector+>;beforeEach(() => {mockedDataCollector.mockReset();diff --git a/src/client/src/__tests__/lib/platform/agents/materialize.test.ts b/src/client/src/__tests__/lib/platform/agents/materialize.test.tsindex 391e808d..36953297 100644--- a/src/client/src/__tests__/lib/platform/agents/materialize.test.ts+++ b/src/client/src/__tests__/lib/platform/agents/materialize.test.ts@@ -13,10 +13,14 @@* `(cluster, env, service_name)` for SDK-only / controller-only rows.*/-jest.mock("@/lib/platform/common", () => ({- dataCollector: jest.fn(),- OTEL_TRACES_TABLE_NAME: "otel_traces",-}));+jest.mock("@/lib/platform/common", () => {+ const collector = jest.fn();+ return {+ dataCollector: collector,+ intelligenceDataCollector: collector,+ OTEL_TRACES_TABLE_NAME: "otel_traces",+ };+});jest.mock("@/lib/platform/agents/cache", () => ({swr: jest.fn(<T,>(_k: string, _p: unknown, loader: () => Promise<T>) =>loader()@@ -35,24 +39,29 @@ jest.mock("@/lib/platform/agents/snapshot", () => ({upsertVersion: jest.fn(),getLatestVersionsBatch: jest.fn().mockResolvedValue(new Map()),}));+const mockGetTelemetryAdapterForDbConfig = jest.fn();+jest.mock("@/lib/telemetry-source", () => ({+ isSignalServedByBuiltInClickHouse: jest.fn().mockResolvedValue(true),+ getTelemetryAdapterForDbConfig: (...args: unknown[]) =>+ mockGetTelemetryAdapterForDbConfig(...args),+}));+const mockGetDBConfigByIdInternal = jest.fn();+jest.mock("@/lib/db-config", () => ({+ getDBConfigByIdInternal: (...args: unknown[]) =>+ mockGetDBConfigByIdInternal(...args),+}));+const mockResolveCodingAgentsClickHouseDbConfigId = jest.fn();+jest.mock("@/lib/platform/coding-agents/source", () => ({+ resolveCodingAgentsClickHouseDbConfigId: (...args: unknown[]) =>+ mockResolveCodingAgentsClickHouseDbConfigId(...args),+}));-import { dataCollector } from "@/lib/platform/common";-import {- materializeAgents,- recomputeCodingAgentsForWindow,-} from "@/lib/platform/agents/materialize";+import { intelligenceDataCollector } from "@/lib/platform/common";+import { materializeAgents } from "@/lib/platform/agents/materialize";import { computeAgentKey } from "@/lib/platform/agents";-import {- deriveSnapshot,- getLatestVersionsBatch,- upsertVersion,-} from "@/lib/platform/agents/snapshot";-const mockedDC = dataCollector as jest.MockedFunction<typeof dataCollector>;-const mockedDerive = deriveSnapshot as jest.MockedFunction<typeof deriveSnapshot>;-const mockedUpsert = upsertVersion as jest.MockedFunction<typeof upsertVersion>;-const mockedGetLatest = getLatestVersionsBatch as jest.MockedFunction<- typeof getLatestVersionsBatch+const mockedDC = intelligenceDataCollector as jest.MockedFunction<+ typeof intelligenceDataCollector>;interface RecordedInsert {@@ -100,11 +109,73 @@ function queueDiscovery(beforeEach(() => {mockedDC.mockReset();- mockedDerive.mockReset();- mockedUpsert.mockReset();- mockedDerive.mockResolvedValue(null);- mockedUpsert.mockResolvedValue({ versionNumber: 1, isNewVersion: false });- mockedGetLatest.mockResolvedValue(new Map());+ mockGetTelemetryAdapterForDbConfig.mockReset();+ mockGetDBConfigByIdInternal.mockReset();+ mockGetDBConfigByIdInternal.mockResolvedValue({+ id: "db-1",+ projectId: "p1",+ environment: "production",+ });+ mockResolveCodingAgentsClickHouseDbConfigId.mockReset();+ mockResolveCodingAgentsClickHouseDbConfigId.mockResolvedValue("db-1");+});++describe("materializeAgents — connector routing purity", () => {+ it("fails closed instead of reading ClickHouse traces when the routed external adapter cannot resolve", async () => {+ mockGetTelemetryAdapterForDbConfig.mockRejectedValue(+ new Error("Tempo credentials unavailable")+ );+ await expect(materializeAgents({ dbConfigId: "db-1" })).rejects.toThrow(+ "Tempo credentials unavailable"+ );+ const sdkOrControllerQueries = mockedDC.mock.calls.filter(([cfg]) => {+ const q = String(cfg?.query || "");+ return (+ q.includes("sdk_seen") || q.includes("openlit_controller_services")+ );+ });+ expect(sdkOrControllerQueries).toHaveLength(0);+ });++ it("keeps coding-agent discovery on ClickHouse SQL when traces are bound to Tempo", async () => {+ const sampleTracesForGraph = jest.fn();+ mockGetTelemetryAdapterForDbConfig.mockResolvedValue({+ isBuiltIn: false,+ adapter: {+ discoverServices: jest.fn().mockResolvedValue([]),+ sampleTracesForGraph,+ listSpans: jest.fn(),+ },+ descriptor: { type: "tempo" },+ });+ mockResolveCodingAgentsClickHouseDbConfigId.mockResolvedValue("db-intel");++ const queries: string[] = [];+ mockedDC.mockImplementation(async (config: any, op?: string) => {+ if (op === "query" || op === undefined) {+ queries.push(String(config.query || ""));+ return { data: [] } as any;+ }+ return { data: [] } as any;+ });++ await materializeAgents({ dbConfigId: "db-intel" });++ expect(sampleTracesForGraph).not.toHaveBeenCalled();+ expect(queries.some((q) => q.includes("coding_agent.session.id"))).toBe(+ true+ );+ expect(mockResolveCodingAgentsClickHouseDbConfigId).toHaveBeenCalledWith(+ expect.objectContaining({+ dbConfigId: "db-intel",+ projectId: "p1",+ })+ );+ const codingCall = mockedDC.mock.calls.find(([cfg]) =>+ String(cfg?.query || "").includes("coding_agent.session.id")+ );+ expect(codingCall?.[2]).toBe("db-intel");+ });});describe("materializeAgents — workload_key dedup", () => {@@ -420,229 +491,3 @@ describe("materializeAgents — workload_key dedup", () => {expect(ctrlQuery!).toMatch(/latest\.llm_providers\s+AS\s+llm_providers/);});});--describe("recomputeCodingAgentsForWindow", () => {- it("builds a 24h default window and maps vendor rows", async () => {- mockedDC.mockResolvedValueOnce({- data: [- {- vendor: "cursor",- client_version: "1.2.3",- first_seen: "2026-05-11 20:00:00",- last_seen: "2026-05-11 22:00:00",- session_count_24h: 5,- cost_usd_24h: 1.5,- active_users_24h: 2,- lines_added_24h: 10,- lines_removed_24h: 1,- lines_accepted_24h: 8,- lines_rejected_24h: 0,- edit_accept_24h: 3,- edit_reject_24h: 0,- commit_count_24h: 1,- pr_count_24h: 0,- },- { vendor: "" },- ],- } as any);-- const rows = await recomputeCodingAgentsForWindow({});- expect(rows).toHaveLength(1);- expect(rows[0]).toMatchObject({- service_name: "cursor",- source: "coding",- cluster_id: "coding",- coding_session_count_24h: 5,- coding_cost_usd_24h: 1.5,- sdk_version: "1.2.3",- });- const query = String((mockedDC.mock.calls[0][0] as any).query);- expect(query).toContain("Timestamp >= now() - INTERVAL 24 HOUR");- });-- it("escapes custom window bounds and returns [] on error", async () => {- mockedDC.mockResolvedValueOnce({ data: [] } as any);- await recomputeCodingAgentsForWindow({- timeStart: "2026-05-11T00:00:00Z",- timeEnd: "2026-05-11 23:59:59",- });- const query = String((mockedDC.mock.calls[0][0] as any).query);- expect(query).toContain(- "parseDateTimeBestEffort('2026-05-11T00:00:00Z')"- );- expect(query).toContain("parseDateTimeBestEffort('2026-05-11 23:59:59')");-- mockedDC.mockResolvedValueOnce({- err: new Error("coding discovery failed"),- data: [],- } as any);- await expect(- recomputeCodingAgentsForWindow({- timeStart: "2026-05-11 00:00:00",- })- ).resolves.toEqual([]);- });-- it("escapes quotes in window timestamps", async () => {- mockedDC.mockResolvedValueOnce({ data: [] } as any);- await recomputeCodingAgentsForWindow({- timeStart: "2026-05-11 00:00:00'; DROP TABLE x; --",- });- const query = String((mockedDC.mock.calls[0][0] as any).query);- expect(query).toContain("\\'");- });-});--describe("materializeAgents — scope / filter / coding", () => {- it("returns processed:0 when scoped refresh finds no matching agent", async () => {- mockedDC- .mockResolvedValueOnce({ data: [] } as any) // SDK- .mockResolvedValueOnce({ data: [] } as any) // coding- .mockResolvedValueOnce({ data: [] } as any); // controller-- await expect(- materializeAgents({- scope: {- serviceName: "cursor",- clusterId: "coding",- environment: "default",- },- })- ).resolves.toEqual({ processed: 0, newVersions: 0, errors: 0 });- });-- it("materializes a coding agent matched by agentKeyFilter without snapshot upsert", async () => {- const codingKey = computeAgentKey("coding", "default", "cursor");- mockedDC- // discoverAgents: SDK then controller (sequential)- .mockResolvedValueOnce({ data: [] } as any)- .mockResolvedValueOnce({ data: [] } as any)- // discoverCodingAgents- .mockResolvedValueOnce({- data: [- {- vendor: "cursor",- client_version: "9.0.0",- first_seen: "2026-05-11 20:00:00",- last_seen: "2026-05-11 22:00:00",- session_count_24h: 2,- cost_usd_24h: 0.4,- active_users_24h: 1,- lines_added_24h: 0,- lines_removed_24h: 0,- lines_accepted_24h: 0,- lines_rejected_24h: 0,- edit_accept_24h: 0,- edit_reject_24h: 0,- commit_count_24h: 0,- pr_count_24h: 0,- },- ],- } as any);-- const inserts: RecordedInsert[] = [];- mockedDC.mockImplementation(async (c: any, op?: string) => {- if (op === "insert") {- inserts.push({- table: String(c.table),- values: (c.values || []) as any,- });- return { data: [] } as any;- }- return { data: [] } as any;- });-- const result = await materializeAgents({ agentKeyFilter: codingKey });- expect(result.processed).toBe(1);- expect(mockedDerive).not.toHaveBeenCalled();- expect(mockedUpsert).not.toHaveBeenCalled();- expect(inserts[0].values[0]).toMatchObject({- agent_key: codingKey,- source: "coding",- service_name: "cursor",- coding_agent_vendor: "cursor",- });- });-- it("returns empty when agentKeyFilter does not match", async () => {- mockedDC- .mockResolvedValueOnce({ data: [] } as any)- .mockResolvedValueOnce({ data: [] } as any)- .mockResolvedValueOnce({ data: [] } as any);-- await expect(- materializeAgents({ agentKeyFilter: "nope" })- ).resolves.toEqual({ processed: 0, newVersions: 0, errors: 0 });- });-- it("tolerates SDK/controller discovery errors", async () => {- mockedDC- .mockResolvedValueOnce({ err: new Error("sdk boom"), data: [] } as any)- .mockResolvedValueOnce({ data: [] } as any) // coding- .mockResolvedValueOnce({ err: new Error("ctrl boom"), data: [] } as any);-- await expect(materializeAgents()).resolves.toEqual({- processed: 0,- newVersions: 0,- errors: 0,- });- });-- it("materializes scoped SDK matches and upserts versions when snapshot exists", async () => {- mockedDerive.mockResolvedValueOnce({- agent_key: computeAgentKey("default", "default", "scoped-app"),- service_name: "scoped-app",- environment: "default",- cluster_id: "default",- system_prompt: "hi",- tools: [],- primary_model: "gpt-4o",- models: ["gpt-4o"],- providers: ["openai"],- runtime_config: {},- request_count: 3,- first_seen: "2026-05-11 22:00:00",- last_seen: "2026-05-11 22:10:00",- version_hash: "snap-hash",- } as any);- mockedUpsert.mockResolvedValueOnce({ versionNumber: 2, isNewVersion: true });-- mockedDC- .mockResolvedValueOnce({- data: [- {- service_name: "scoped-app",- environment: "default",- cluster_id: "default",- workload_key: "",- sdk_version: "1.0.0",- sdk_language: "python",- first_seen: "2026-05-11 22:00:00",- last_seen: "2026-05-11 22:10:00",- },- ],- } as any)- .mockResolvedValueOnce({ data: [] } as any) // coding- .mockResolvedValueOnce({ data: [] } as any); // controller-- const inserts: RecordedInsert[] = [];- mockedDC.mockImplementation(async (c: any, op?: string) => {- if (op === "insert") {- inserts.push({- table: String(c.table),- values: (c.values || []) as any,- });- return { data: [] } as any;- }- return { data: [] } as any;- });-- const result = await materializeAgents({- scope: { serviceName: "scoped-app", environment: "default" },- });- expect(result.processed).toBe(1);- expect(result.newVersions).toBe(1);- expect(mockedUpsert).toHaveBeenCalled();- expect(inserts[0].values[0].current_version_hash).toBe("snap-hash");- });-});diff --git a/src/client/src/__tests__/lib/platform/agents/snapshot.test.ts b/src/client/src/__tests__/lib/platform/agents/snapshot.test.tsindex 2f8ca320..a544bd26 100644--- a/src/client/src/__tests__/lib/platform/agents/snapshot.test.ts+++ b/src/client/src/__tests__/lib/platform/agents/snapshot.test.ts@@ -5,13 +5,17 @@* decision logic (new version vs same-version bump) without a real DB.*/-jest.mock("@/lib/platform/common", () => ({- dataCollector: jest.fn(),- OTEL_TRACES_TABLE_NAME: "otel_traces",- OTEL_LOGS_TABLE_NAME: "otel_logs",-}));+jest.mock("@/lib/platform/common", () => {+ const collector = jest.fn();+ return {+ dataCollector: collector,+ intelligenceDataCollector: collector,+ OTEL_TRACES_TABLE_NAME: "otel_traces",+ OTEL_LOGS_TABLE_NAME: "otel_logs",+ };+});-import { dataCollector } from "@/lib/platform/common";+import { intelligenceDataCollector } from "@/lib/platform/common";import {upsertVersion,deriveSnapshot,@@ -23,7 +27,9 @@ import {_internals,} from "@/lib/platform/agents/snapshot";-const mockedDataCollector = dataCollector as jest.MockedFunction<typeof dataCollector>;+const mockedDataCollector = intelligenceDataCollector as jest.MockedFunction<+ typeof intelligenceDataCollector+>;beforeEach(() => {mockedDataCollector.mockReset();@@ -290,6 +296,40 @@ describe("deriveSnapshot", () => {// Exactly one dataCollector call — the logs fallback was correctly skipped.expect(mockedDataCollector).toHaveBeenCalledTimes(1);});++ it("skips the logs enrichment when logs are not correlatable (external backend)", async () => {+ // Correlation boundary: trace tool defs empty AND tools_fallback empty,+ // but logs live in a different backend -> the otel_logs query must NOT be+ // issued; the snapshot degrades gracefully with no tools.+ mockedDataCollector.mockResolvedValueOnce({+ data: [+ {+ system_prompt: "Agent",+ tool_definitions_json: "",+ tools_fallback: [],+ primary_model: "gpt-4o",+ models: ["gpt-4o"],+ providers: ["openai"],+ temperature: 0.5,+ top_p: 1,+ max_tokens: 512,+ request_count: 3,+ first_seen: "2026-05-11 22:00:00",+ last_seen: "2026-05-11 22:10:00",+ },+ ],+ });++ const result = await deriveSnapshot({+ serviceName: "external-logs-agent",+ environment: "prod",+ clusterId: "default",+ logsCorrelatable: false,+ });+ expect(result?.tools).toHaveLength(0);+ // Only the trace query ran; the correlation boundary blocked the logs query.+ expect(mockedDataCollector).toHaveBeenCalledTimes(1);+ });});describe("snapshot internals", () => {diff --git a/src/client/src/__tests__/lib/platform/agents/version-filter.test.ts b/src/client/src/__tests__/lib/platform/agents/version-filter.test.tsindex 547a992d..73d1c377 100644--- a/src/client/src/__tests__/lib/platform/agents/version-filter.test.ts+++ b/src/client/src/__tests__/lib/platform/agents/version-filter.test.ts@@ -3,10 +3,14 @@ const mockGetVersion = jest.fn();const mockGetAgent = jest.fn();const mockLoggerError = jest.fn();-jest.mock("@/lib/platform/common", () => ({- dataCollector: (...args: unknown[]) => mockDataCollector(...args),- OTEL_TRACES_TABLE_NAME: "otel_traces",-}));+jest.mock("@/lib/platform/common", () => {+ const collector = (...args: unknown[]) => mockDataCollector(...args);+ return {+ dataCollector: collector,+ intelligenceDataCollector: collector,+ OTEL_TRACES_TABLE_NAME: "otel_traces",+ };+});jest.mock("@/lib/platform/agents/cache", () => ({POLICY_VERSIONS: { ttlMs: 1000, staleMs: 2000 },diff --git a/src/client/src/__tests__/lib/platform/chat/config.test.ts b/src/client/src/__tests__/lib/platform/chat/config.test.tsindex d98cda7c..086ab5d8 100644--- a/src/client/src/__tests__/lib/platform/chat/config.test.ts+++ b/src/client/src/__tests__/lib/platform/chat/config.test.ts@@ -1,4 +1,7 @@-jest.mock('@/lib/platform/common', () => ({ dataCollector: jest.fn() }));+jest.mock('@/lib/platform/common', () => {+ const collector = jest.fn();+ return { dataCollector: collector, intelligenceDataCollector: collector };+});jest.mock('@/lib/platform/chat/table-details', () => ({OPENLIT_CHAT_CONFIG_TABLE: 'openlit_chat_config',}));diff --git a/src/client/src/__tests__/lib/platform/chat/conversation.test.ts b/src/client/src/__tests__/lib/platform/chat/conversation.test.tsindex 0e53e769..169d69c5 100644--- a/src/client/src/__tests__/lib/platform/chat/conversation.test.ts+++ b/src/client/src/__tests__/lib/platform/chat/conversation.test.ts@@ -1,4 +1,7 @@-jest.mock('@/lib/platform/common', () => ({ dataCollector: jest.fn() }));+jest.mock('@/lib/platform/common', () => {+ const collector = jest.fn();+ return { dataCollector: collector, intelligenceDataCollector: collector };+});jest.mock('@/lib/platform/chat/table-details', () => ({OPENLIT_CHAT_CONVERSATION_TABLE: 'openlit_chat_conversation',OPENLIT_CHAT_MESSAGE_TABLE: 'openlit_chat_message',diff --git a/src/client/src/__tests__/lib/platform/chat/improvement.test.ts b/src/client/src/__tests__/lib/platform/chat/improvement.test.tsindex d9df182a..bb9c21ea 100644--- a/src/client/src/__tests__/lib/platform/chat/improvement.test.ts+++ b/src/client/src/__tests__/lib/platform/chat/improvement.test.ts@@ -29,7 +29,28 @@ Object.assign(global, {jest.mock("@/lib/platform/chat/stream", () => ({ getModelInstance: jest.fn() }));jest.mock("@/lib/platform/chat/config", () => ({ getChatConfigWithApiKey: jest.fn() }));jest.mock("@/lib/platform/request", () => ({ getHeirarchyViaSpanId: jest.fn() }));-jest.mock("@/lib/platform/common", () => ({ dataCollector: jest.fn() }));+jest.mock("@/lib/platform/traces/read", () => ({+ getTraceHierarchy: (...args: unknown[]) =>+ require("@/lib/platform/request").getHeirarchyViaSpanId(...args),+}));+jest.mock("@/lib/telemetry-source", () => ({+ resolveTelemetrySourceDescriptor: jest.fn().mockResolvedValue({+ type: "clickhouse",+ id: "builtin:test",+ isBuiltIn: true,+ }),+ isNativeSqlChatAvailable: jest.fn().mockResolvedValue({+ available: true,+ sourceType: "clickhouse",+ sourceName: "Test ClickHouse",+ databaseConfigId: "db-intelligence",+ }),+ getTelemetryAdapter: jest.fn(),+}));+jest.mock("@/lib/platform/common", () => {+ const collector = jest.fn();+ return { dataCollector: collector, intelligenceDataCollector: collector };+});jest.mock("@/lib/platform/evaluation/rule-engine-context", () => ({getContextFromRuleEngineForTrace: jest.fn(),}));diff --git a/src/client/src/__tests__/lib/platform/chat/otter-runs.test.ts b/src/client/src/__tests__/lib/platform/chat/otter-runs.test.tsindex 716d8b4d..09a401b1 100644--- a/src/client/src/__tests__/lib/platform/chat/otter-runs.test.ts+++ b/src/client/src/__tests__/lib/platform/chat/otter-runs.test.ts@@ -1,5 +1,8 @@jest.mock("crypto", () => ({ randomUUID: jest.fn(() => "run-1") }));-jest.mock("@/lib/platform/common", () => ({ dataCollector: jest.fn() }));+jest.mock("@/lib/platform/common", () => {+ const collector = jest.fn();+ return { dataCollector: collector, intelligenceDataCollector: collector };+});jest.mock("@/lib/platform/chat/table-details", () => ({OPENLIT_OTTER_RUNS_TABLE: "openlit_otter_runs",}));diff --git a/src/client/src/__tests__/lib/platform/chat/save-widget.test.ts b/src/client/src/__tests__/lib/platform/chat/save-widget.test.tsindex 664fbcf8..855759b8 100644--- a/src/client/src/__tests__/lib/platform/chat/save-widget.test.ts+++ b/src/client/src/__tests__/lib/platform/chat/save-widget.test.ts@@ -1,7 +1,10 @@jest.mock('@/lib/platform/manage-dashboard/widget', () => ({createWidget: jest.fn(),}));-jest.mock('@/lib/platform/common', () => ({ dataCollector: jest.fn() }));+jest.mock('@/lib/platform/common', () => {+ const collector = jest.fn();+ return { dataCollector: collector, intelligenceDataCollector: collector };+});jest.mock('@/lib/platform/manage-dashboard/table-details', () => ({OPENLIT_BOARD_WIDGET_TABLE_NAME: 'openlit_board_widget',}));diff --git a/src/client/src/__tests__/lib/platform/chat/stream.test.ts b/src/client/src/__tests__/lib/platform/chat/stream.test.tsindex 2405ab59..5bc79251 100644--- a/src/client/src/__tests__/lib/platform/chat/stream.test.ts+++ b/src/client/src/__tests__/lib/platform/chat/stream.test.ts@@ -37,17 +37,36 @@ jest.mock('@/lib/platform/chat/sql-validator', () => ({validateSQL: jest.fn(),extractSQLFromResponse: jest.fn(() => []),}));-jest.mock('@/lib/platform/common', () => ({- dataCollector: jest.fn(),-}));+jest.mock('@/lib/platform/common', () => {+ const collector = jest.fn();+ return {+ dataCollector: collector,+ intelligenceDataCollector: collector,+ };+});jest.mock('@/lib/platform/chat/tools', () => ({getChatTools: jest.fn(() => ({})),}));+jest.mock('@/lib/telemetry-source', () => ({+ isNativeSqlChatAvailable: jest.fn().mockResolvedValue({+ available: true,+ sourceType: 'clickhouse',+ sourceName: 'ClickHouse',+ databaseConfigId: 'db1',+ }),+}));+jest.mock('@/lib/platform/chat/telemetry-sql-routing', () => ({+ authorizeTelemetrySQLRouting: jest.fn().mockResolvedValue({+ allowed: true,+ blockedSignals: [],+ }),+}));import { streamText, generateText } from 'ai';import { dataCollector } from '@/lib/platform/common';import { addMessage, getConversationMessages, updateConversation } from '@/lib/platform/chat/conversation';import { extractSQLFromResponse, validateSQL } from '@/lib/platform/chat/sql-validator';+import { authorizeTelemetrySQLRouting } from '@/lib/platform/chat/telemetry-sql-routing';async function* streamParts(parts: any[]) {for (const part of parts) {@@ -249,6 +268,35 @@ describe('streamChatMessage', () => {});});+ it('does not execute generated ClickHouse telemetry SQL when signal routing blocks it', async () => {+ const response = '```sql\nSELECT * FROM otel_traces LIMIT 10\n```';+ (extractSQLFromResponse as jest.Mock).mockReturnValue(['SELECT * FROM otel_traces LIMIT 10']);+ (validateSQL as jest.Mock).mockReturnValue({ valid: true, query: 'SELECT * FROM otel_traces LIMIT 10' });+ (authorizeTelemetrySQLRouting as jest.Mock).mockResolvedValueOnce({+ allowed: false,+ blockedSignals: [{ signal: 'traces', sourceName: 'Tempo', sourceType: 'tempo' }],+ error: 'ClickHouse telemetry query blocked by signal routing: traces -> Tempo (tempo).',+ });+ (streamText as jest.Mock).mockImplementation(({ onFinish }) => {+ onFinish({ text: response, usage: {}, steps: [] });+ return { fullStream: streamParts([{ type: 'text-delta', text: response }]) };+ });++ const result = await streamChatMessage({+ conversationId: 'c1',+ content: 'Show traces',+ provider: 'openai',+ apiKey: 'sk-test',+ model: 'gpt-4o',+ userId: 'u1',+ dbConfigId: 'db1',+ environment: 'production',+ });++ expect(dataCollector).not.toHaveBeenCalled();+ expect(result.responseText).toContain('blocked by signal routing');+ });+it('saves a sanitized error response when streaming fails before text arrives', async () => {(streamText as jest.Mock).mockImplementation(({ onError }) => {onError({ error: { statusCode: 401 } });diff --git a/src/client/src/__tests__/lib/platform/chat/telemetry-sql-routing.test.ts b/src/client/src/__tests__/lib/platform/chat/telemetry-sql-routing.test.tsnew file mode 100644index 00000000..b187cc57--- /dev/null+++ b/src/client/src/__tests__/lib/platform/chat/telemetry-sql-routing.test.ts@@ -0,0 +1,83 @@+const mockResolveSignalSource = jest.fn();++jest.mock("@/lib/telemetry-source", () => ({+ resolveSignalSource: (...args: unknown[]) => mockResolveSignalSource(...args),+}));++import {+ authorizeTelemetrySQLRouting,+ telemetrySignalsReferencedBySQL,+} from "@/lib/platform/chat/telemetry-sql-routing";++describe("Otter telemetry SQL routing", () => {+ beforeEach(() => jest.clearAllMocks());++ it("detects the signal behind each raw OTel table", () => {+ expect(+ telemetrySignalsReferencedBySQL(+ "SELECT * FROM otel_traces JOIN otel_metrics_sum ON 1 = 1"+ )+ ).toEqual(["traces", "metrics"]);+ });++ it("blocks ClickHouse trace reads when traces are routed to Tempo", async () => {+ mockResolveSignalSource.mockResolvedValue({+ hasSource: true,+ descriptor: {+ id: "tempo-1",+ name: "Production Tempo",+ type: "tempo",+ isBuiltIn: false,+ },+ });++ const result = await authorizeTelemetrySQLRouting(+ "SELECT * FROM otel_traces LIMIT 10",+ { environment: "production", databaseConfigId: "clickhouse-1" }+ );++ expect(result.allowed).toBe(false);+ expect(result.error).toContain("traces -> Production Tempo (tempo)");+ expect(mockResolveSignalSource).toHaveBeenCalledWith("traces", {+ environment: "production",+ });+ });++ it("allows a telemetry table only when its signal uses the execution database", async () => {+ mockResolveSignalSource.mockResolvedValue({+ hasSource: true,+ descriptor: {+ id: "builtin:clickhouse-1",+ name: "ClickHouse",+ type: "clickhouse",+ isBuiltIn: true,+ dbConfigId: "clickhouse-1",+ },+ });++ await expect(+ authorizeTelemetrySQLRouting("SELECT * FROM otel_traces LIMIT 10", {+ databaseConfigId: "clickhouse-1",+ })+ ).resolves.toEqual({ allowed: true, blockedSignals: [] });+ });++ it("blocks a different ClickHouse database to avoid cross-source reads", async () => {+ mockResolveSignalSource.mockResolvedValue({+ hasSource: true,+ descriptor: {+ id: "builtin:clickhouse-2",+ name: "Other ClickHouse",+ type: "clickhouse",+ isBuiltIn: true,+ dbConfigId: "clickhouse-2",+ },+ });++ const result = await authorizeTelemetrySQLRouting(+ "SELECT * FROM otel_traces LIMIT 10",+ { databaseConfigId: "clickhouse-1" }+ );+ expect(result.allowed).toBe(false);+ });+});diff --git a/src/client/src/__tests__/lib/platform/chat/tools.test.ts b/src/client/src/__tests__/lib/platform/chat/tools.test.tsindex bb58c12d..2f009c48 100644--- a/src/client/src/__tests__/lib/platform/chat/tools.test.ts+++ b/src/client/src/__tests__/lib/platform/chat/tools.test.ts@@ -3,6 +3,10 @@ jest.mock("ai", () => ({jsonSchema: jest.fn((schema) => schema),}));+const mockGetLogs = jest.fn();+const mockListMetricRecords = jest.fn();+const mockResolveSignalSource = jest.fn();+import { TextDecoder, TextEncoder } from "util";Object.assign(global, { TextDecoder, TextEncoder });@@ -56,8 +60,28 @@ jest.mock("@/lib/platform/chat/improvement", () => ({streamTraceImprovementAnalysis: jest.fn(),}));-jest.mock("@/lib/platform/common", () => ({- dataCollector: jest.fn(),+jest.mock("@/lib/platform/common", () => {+ const collector = jest.fn();+ return {+ dataCollector: collector,+ intelligenceDataCollector: collector,+ };+});++jest.mock("@/lib/platform/traces/read", () => ({+ listTraceRecords: jest.fn(),+}));++jest.mock("@/lib/platform/logs/read", () => ({+ getLogs: (...args: unknown[]) => mockGetLogs(...args),+}));++jest.mock("@/lib/platform/metrics/read", () => ({+ listMetricRecords: (...args: unknown[]) => mockListMetricRecords(...args),+}));++jest.mock("@/lib/telemetry-source", () => ({+ resolveSignalSource: (...args: unknown[]) => mockResolveSignalSource(...args),}));jest.mock("@/utils/sanitizer", () => ({@@ -95,6 +119,7 @@ import {updateCustomModel,} from "@/lib/platform/providers/models-service";import { dataCollector } from "@/lib/platform/common";+import { listTraceRecords } from "@/lib/platform/traces/read";import {getTraceImprovement,streamTraceImprovementAnalysis,@@ -140,6 +165,8 @@ describe("getChatTools", () => {"get_trace_analysis","analyze_trace_batch","analyze_traces_by_attribute",+ "get_telemetry_routing",+ "query_telemetry",]));expect(tools.create_rule.inputSchema.required).toEqual(["name"]);@@ -149,6 +176,50 @@ describe("getChatTools", () => {]);});+ it("reads Otter trace telemetry through the selected-source facade", async () => {+ (listTraceRecords as jest.Mock).mockResolvedValue({+ err: null,+ records: [{ TraceId: "tempo-trace", SpanId: "tempo-span" }],+ total: 1,+ });+ const tools = getChatTools("user-1", "db-1", "production") as any;++ const result = await tools.query_telemetry.execute({+ signal: "traces",+ limit: 5,+ });++ expect(result).toMatchObject({ success: true, signal: "traces", total: 1 });+ expect(listTraceRecords).toHaveBeenCalledWith(+ expect.objectContaining({ environment: "production", limit: 5 })+ );+ expect(dataCollector).not.toHaveBeenCalled();+ });++ it("reports Tempo as Otter's routed trace connector", async () => {+ mockResolveSignalSource.mockImplementation(async (signal: string) => ({+ hasSource: true,+ via: "binding",+ descriptor: {+ name: signal === "traces" ? "Production Tempo" : "ClickHouse",+ type: signal === "traces" ? "tempo" : "clickhouse",+ isBuiltIn: signal !== "traces",+ environment: "production",+ },+ }));+ const tools = getChatTools("user-1", "db-1", "production") as any;++ const result = await tools.get_telemetry_routing.execute({});++ expect(result.routing).toContainEqual(+ expect.objectContaining({+ signal: "traces",+ connector: "Production Tempo",+ connectorType: "tempo",+ })+ );+ });+it("creates a rule and adds condition groups when provided", async () => {(createRule as jest.Mock).mockResolvedValue({ id: "rule-1" });const tools = getChatTools("user-1", "db-1") as any;@@ -164,12 +235,15 @@ describe("getChatTools", () => {message: "Rule created",}));- expect(createRule).toHaveBeenCalledWith({- name: "Latency rule",- description: "",- group_operator: "AND",- status: "ACTIVE",- });+ expect(createRule).toHaveBeenCalledWith(+ {+ name: "Latency rule",+ description: "",+ group_operator: "AND",+ status: "ACTIVE",+ },+ { databaseConfigId: "db-1" }+ );});it("returns rule operation errors without throwing", async () => {@@ -289,17 +363,24 @@ describe("getChatTools", () => {entities: [{ id: "entity-1" }],});- expect(updateRule).toHaveBeenCalledWith("rule-1", {- name: "Updated",- description: undefined,- group_operator: undefined,- status: undefined,- });- expect(addRuleEntity).toHaveBeenCalledWith({- rule_id: "rule-1",- entity_type: "context",- entity_id: "ctx-1",- });+ expect(updateRule).toHaveBeenCalledWith(+ "rule-1",+ {+ name: "Updated",+ description: undefined,+ group_operator: undefined,+ status: undefined,+ },+ { databaseConfigId: "db-1" }+ );+ expect(addRuleEntity).toHaveBeenCalledWith(+ {+ rule_id: "rule-1",+ entity_type: "context",+ entity_id: "ctx-1",+ },+ { databaseConfigId: "db-1" }+ );});it("executes context tools and serializes tags", async () => {@@ -627,8 +708,12 @@ describe("getChatTools", () => {});it("analyzes traces by span attribute and returns trace refs", async () => {- (dataCollector as jest.Mock).mockResolvedValue({- data: [{ traceId: "trace-abc", spanId: "span-abc", spanCount: 3 }],+ (listTraceRecords as jest.Mock).mockResolvedValue({+ records: [+ { TraceId: "trace-abc", SpanId: "span-abc" },+ { TraceId: "trace-abc", SpanId: "span-def" },+ { TraceId: "trace-abc", SpanId: "span-ghi" },+ ],err: null,});(getTraceImprovement as jest.Mock).mockResolvedValue({@@ -678,14 +763,11 @@ describe("getChatTools", () => {attribute_value: "session-1",});- expect(dataCollector).toHaveBeenCalledWith(- expect.objectContaining({- query: expect.stringContaining("SpanAttributes['session.id'] = 'session-1'"),- enable_readonly: true,- }),- "query",- "db-1"- );+ expect(listTraceRecords).toHaveBeenCalledWith(expect.objectContaining({+ selectedConfig: {+ customFilters: [{ key: "session.id", value: "session-1", scope: "span" }],+ },+ }));expect(result.success).toBe(true);expect(result.details).toContain("```trace-refs");expect(result.matchedTraceCount).toBe(1);@@ -1126,7 +1208,7 @@ describe("getChatTools", () => {error: "Attribute key and value are required",});- (dataCollector as jest.Mock).mockResolvedValueOnce({ err: "query failed" });+ (listTraceRecords as jest.Mock).mockResolvedValueOnce({ err: "query failed" });await expect(tools.analyze_traces_by_attribute.execute({attribute_key: "session.id",@@ -1134,8 +1216,8 @@ describe("getChatTools", () => {})).resolves.toEqual({ success: false, error: "query failed" });- (dataCollector as jest.Mock).mockResolvedValueOnce({- data: [{ traceId: "t1", spanId: null }],+ (listTraceRecords as jest.Mock).mockResolvedValueOnce({+ records: [{ TraceId: "t1", SpanId: null }],err: null,});await expect(@@ -1242,7 +1324,7 @@ describe("getChatTools", () => {error: "Failed to run batch trace analysis",});- (dataCollector as jest.Mock).mockRejectedValueOnce(emptyError);+ (listTraceRecords as jest.Mock).mockRejectedValueOnce(emptyError);await expect(tools.analyze_traces_by_attribute.execute({attribute_key: "k",diff --git a/src/client/src/__tests__/lib/platform/chat/usage.test.ts b/src/client/src/__tests__/lib/platform/chat/usage.test.tsindex 943928a5..1898bb11 100644--- a/src/client/src/__tests__/lib/platform/chat/usage.test.ts+++ b/src/client/src/__tests__/lib/platform/chat/usage.test.ts@@ -1,4 +1,7 @@-jest.mock("@/lib/platform/common", () => ({ dataCollector: jest.fn() }));+jest.mock("@/lib/platform/common", () => {+ const collector = jest.fn();+ return { dataCollector: collector, intelligenceDataCollector: collector };+});jest.mock("@/lib/platform/chat/table-details", () => ({OPENLIT_CHAT_CONVERSATION_TABLE: "openlit_chat_conversation",OPENLIT_CHAT_MESSAGE_TABLE: "openlit_chat_message",diff --git a/src/client/src/__tests__/lib/platform/coding-agents/queries.test.ts b/src/client/src/__tests__/lib/platform/coding-agents/queries.test.tsindex 9d288438..ccee51bb 100644--- a/src/client/src/__tests__/lib/platform/coding-agents/queries.test.ts+++ b/src/client/src/__tests__/lib/platform/coding-agents/queries.test.ts@@ -20,9 +20,13 @@ import {import { buildSessionsHaving, escape } from "@/lib/platform/coding-agents/query-builders";import type { CodingAgentAuth } from "@/lib/platform/coding-agents/auth";-jest.mock("@/lib/platform/common", () => ({- dataCollector: jest.fn(),-}));+jest.mock("@/lib/platform/common", () => {+ const collector = jest.fn();+ return {+ dataCollector: collector,+ intelligenceDataCollector: collector,+ };+});jest.mock("@/clickhouse/migrations/create-coding-agents-audit-migration", () => ({CODING_AGENT_AUDIT_LOG_TABLE: "coding_agent_audit_log",@@ -636,7 +640,8 @@ describe("coding agent query service", () => {}),],}),- "insert"+ "insert",+ undefined);});diff --git a/src/client/src/__tests__/lib/platform/coding-agents/query-service.test.ts b/src/client/src/__tests__/lib/platform/coding-agents/query-service.test.tsindex 4154e44f..0a086784 100644--- a/src/client/src/__tests__/lib/platform/coding-agents/query-service.test.ts+++ b/src/client/src/__tests__/lib/platform/coding-agents/query-service.test.ts@@ -9,9 +9,13 @@ import {} from "@/lib/platform/coding-agents/queries";import type { CodingAgentAuth } from "@/lib/platform/coding-agents/auth";-jest.mock("@/lib/platform/common", () => ({- dataCollector: jest.fn(),-}));+jest.mock("@/lib/platform/common", () => {+ const collector = jest.fn();+ return {+ dataCollector: collector,+ intelligenceDataCollector: collector,+ };+});jest.mock("@/clickhouse/migrations/create-coding-agents-audit-migration", () => ({CODING_AGENT_AUDIT_LOG_TABLE: "coding_agent_audit_log",@@ -207,7 +211,8 @@ describe("coding agent query service", () => {}),],}),- "insert"+ "insert",+ undefined);});diff --git a/src/client/src/__tests__/lib/platform/coding-agents/source.test.ts b/src/client/src/__tests__/lib/platform/coding-agents/source.test.tsnew file mode 100644index 00000000..56950331--- /dev/null+++ b/src/client/src/__tests__/lib/platform/coding-agents/source.test.ts@@ -0,0 +1,243 @@+jest.mock("@/lib/telemetry-source", () => ({+ resolveSignalSource: jest.fn(),+}));+jest.mock("@/lib/db-config", () => ({+ getDBConfigByUser: jest.fn(),+}));+jest.mock("@/lib/platform/coding-agents/auth", () => ({+ requireCodingAgentAuth: jest.fn(),+}));++import { resolveSignalSource } from "@/lib/telemetry-source";+import { getDBConfigByUser } from "@/lib/db-config";+import { requireCodingAgentAuth } from "@/lib/platform/coding-agents/auth";+import {+ isCodingAgentClickHouseSql,+ resolveCodingAgentsClickHouseDbConfigId,+ resolveCodingAgentsDatabaseConfigId,+ requireCodingAgentQueryContext,+} from "@/lib/platform/coding-agents/source";++function requestWithContext({+ environment,+ databaseConfigId,+}: {+ environment?: string;+ databaseConfigId?: string;+} = {}): Request {+ return {+ headers: {+ get: jest.fn((name: string) => {+ if (name === "x-openlit-environment") return environment || null;+ if (name === "x-openlit-database-config-id") {+ return databaseConfigId || null;+ }+ return null;+ }),+ },+ } as unknown as Request;+}++describe("isCodingAgentClickHouseSql", () => {+ it("detects coding-agent seed SQL", () => {+ expect(+ isCodingAgentClickHouseSql(+ "SELECT countIf(SpanName = 'coding_agent.tool.call') AS total_tools FROM otel_traces"+ )+ ).toBe(true);+ expect(+ isCodingAgentClickHouseSql(+ "SELECT count() FROM otel_traces WHERE SpanAttributes['gen_ai.system'] = 'openai'"+ )+ ).toBe(false);+ });+});++describe("resolveCodingAgentsClickHouseDbConfigId", () => {+ beforeEach(() => jest.clearAllMocks());++ it("returns intelligence ClickHouse when traces is Tempo", async () => {+ (resolveSignalSource as jest.Mock)+ .mockResolvedValueOnce({+ hasSource: true,+ descriptor: { type: "tempo" },+ })+ .mockResolvedValueOnce({+ hasSource: true,+ descriptor: { type: "clickhouse", dbConfigId: "db-intel" },+ });++ await expect(+ resolveCodingAgentsClickHouseDbConfigId({ environment: "production" })+ ).resolves.toBe("db-intel");+ });++ it("forwards project and dbConfig scope so sessionless callers skip getCurrentUser", async () => {+ (resolveSignalSource as jest.Mock)+ .mockResolvedValueOnce({+ hasSource: true,+ descriptor: { type: "tempo" },+ })+ .mockResolvedValueOnce({+ hasSource: true,+ descriptor: { type: "clickhouse", dbConfigId: "db-intel" },+ });++ await expect(+ resolveCodingAgentsClickHouseDbConfigId({+ environment: "production",+ projectId: "proj-1",+ dbConfigId: "db-intel",+ })+ ).resolves.toBe("db-intel");+ expect(resolveSignalSource).toHaveBeenCalledWith("traces", {+ environment: "production",+ projectId: "proj-1",+ dbConfigId: "db-intel",+ });+ expect(resolveSignalSource).toHaveBeenNthCalledWith(2, "intelligence", {+ environment: "production",+ projectId: "proj-1",+ dbConfigId: "db-intel",+ });+ });++ it("returns null when no ClickHouse binding exists", async () => {+ (resolveSignalSource as jest.Mock)+ .mockResolvedValueOnce({ hasSource: true, descriptor: { type: "tempo" } })+ .mockResolvedValueOnce({ hasSource: false, descriptor: { type: "clickhouse" } });++ await expect(+ resolveCodingAgentsClickHouseDbConfigId({ environment: "production" })+ ).resolves.toBeNull();+ });+});++describe("resolveCodingAgentsDatabaseConfigId", () => {+ beforeEach(() => jest.clearAllMocks());++ it("uses the selected environment's traces ClickHouse binding", async () => {+ (resolveSignalSource as jest.Mock).mockResolvedValueOnce({+ hasSource: true,+ descriptor: { type: "clickhouse", dbConfigId: "db-traces" },+ });++ await expect(+ resolveCodingAgentsDatabaseConfigId(+ requestWithContext({ environment: "production" })+ )+ ).resolves.toBe("db-traces");+ expect(resolveSignalSource).toHaveBeenCalledWith("traces", {+ environment: "production",+ });+ });++ it("falls back to intelligence ClickHouse when traces is external", async () => {+ (resolveSignalSource as jest.Mock)+ .mockResolvedValueOnce({+ hasSource: true,+ descriptor: { type: "tempo", dbConfigId: undefined },+ })+ .mockResolvedValueOnce({+ hasSource: true,+ descriptor: { type: "clickhouse", dbConfigId: "db-intel" },+ });++ await expect(+ resolveCodingAgentsDatabaseConfigId(+ requestWithContext({ environment: "production" })+ )+ ).resolves.toBe("db-intel");+ expect(resolveSignalSource).toHaveBeenNthCalledWith(2, "intelligence", {+ environment: "production",+ });+ });++ it("prefers signal-routed ClickHouse over the ambient database header", async () => {+ (resolveSignalSource as jest.Mock).mockResolvedValueOnce({+ hasSource: true,+ descriptor: { type: "clickhouse", dbConfigId: "db-traces" },+ });+ (getDBConfigByUser as jest.Mock).mockResolvedValue([+ { id: "db-selected" },+ { id: "db-traces" },+ ]);++ await expect(+ resolveCodingAgentsDatabaseConfigId(+ requestWithContext({+ environment: "production",+ databaseConfigId: "db-selected",+ })+ )+ ).resolves.toBe("db-traces");+ expect(getDBConfigByUser).not.toHaveBeenCalled();+ });++ it("falls back to the project database header when signal routing has no ClickHouse", async () => {+ (resolveSignalSource as jest.Mock)+ .mockResolvedValueOnce({+ hasSource: true,+ descriptor: { type: "tempo" },+ })+ .mockResolvedValueOnce({+ hasSource: false,+ descriptor: { type: "clickhouse" },+ });+ (getDBConfigByUser as jest.Mock).mockResolvedValue([{ id: "db-selected" }]);++ await expect(+ resolveCodingAgentsDatabaseConfigId(+ requestWithContext({+ environment: "production",+ databaseConfigId: "db-selected",+ })+ )+ ).resolves.toBe("db-selected");+ });++ it("fails closed when neither traces nor intelligence is ClickHouse and no header", async () => {+ (resolveSignalSource as jest.Mock)+ .mockResolvedValueOnce({+ hasSource: true,+ descriptor: { type: "tempo" },+ })+ .mockResolvedValueOnce({+ hasSource: false,+ descriptor: { type: "clickhouse" },+ });++ await expect(+ resolveCodingAgentsDatabaseConfigId(+ requestWithContext({ environment: "staging" })+ )+ ).rejects.toThrow(/ClickHouse/);+ });+});++describe("requireCodingAgentQueryContext", () => {+ it("attaches the routed dbConfigId to auth", async () => {+ (requireCodingAgentAuth as jest.Mock).mockResolvedValue({+ userId: "u1",+ organizationId: "o1",+ role: "admin",+ rawRole: "owner",+ });+ (resolveSignalSource as jest.Mock).mockResolvedValue({+ hasSource: true,+ descriptor: { type: "clickhouse", dbConfigId: "db-1" },+ });++ await expect(+ requireCodingAgentQueryContext(+ requestWithContext({ environment: "production" })+ )+ ).resolves.toEqual({+ userId: "u1",+ organizationId: "o1",+ role: "admin",+ rawRole: "owner",+ dbConfigId: "db-1",+ });+ });+});diff --git a/src/client/src/__tests__/lib/platform/common.test.ts b/src/client/src/__tests__/lib/platform/common.test.tsindex 41cc2ed3..126d7ae2 100644--- a/src/client/src/__tests__/lib/platform/common.test.ts+++ b/src/client/src/__tests__/lib/platform/common.test.ts@@ -1,21 +1,22 @@jest.mock('@/lib/db-config', () => ({getDBConfigByUser: jest.fn(),+ getDBConfigById: jest.fn(),getDBConfigByIdInternal: jest.fn(),- getDBConfigByIdForUser: jest.fn(),-}));-jest.mock('@/lib/session', () => ({- getCurrentUser: jest.fn(),+ getDBConfigByIdForBackground: jest.fn(),}));jest.mock('@/lib/platform/clickhouse/clickhouse-client', () => ({__esModule: true,default: jest.fn(),}));jest.mock('@/utils/asaw', () => jest.fn());+jest.mock('@/lib/platform/openplait', () => ({+ executeOpenPlaitRead: jest.fn(),+}));-import { dataCollector } from '@/lib/platform/common';+import { dataCollector, intelligenceDataCollector } from '@/lib/platform/common';import createClickhousePool from '@/lib/platform/clickhouse/clickhouse-client';import asaw from '@/utils/asaw';-import { getCurrentUser } from '@/lib/session';+import { executeOpenPlaitRead } from '@/lib/platform/openplait';const mockDbConfig = { id: 'db-1', host: 'localhost', port: '8123' };@@ -42,24 +43,11 @@ describe('dataCollector', () => {expect(result.data).toEqual([]);});- it('uses internal DB config lookup when dbConfigId is provided without a session', async () => {- (asaw as jest.Mock)- .mockResolvedValueOnce([new Error('No session'), null])- .mockResolvedValueOnce(['DB config error', null]);+ it('uses getDBConfigByIdForBackground when dbConfigId is provided', async () => {+ (asaw as jest.Mock).mockResolvedValue(['DB config error', null]);const result = await dataCollector({ query: 'SELECT 1' }, 'query', 'db-config-id');expect(result.err).toBe('DB config error');});-- it('checks user scoped db config access when dbConfigId is provided with a session', async () => {- (asaw as jest.Mock)- .mockResolvedValueOnce([null, { id: 'user-1' }])- .mockResolvedValueOnce(['DB config denied', null]);-- const result = await dataCollector({ query: 'SELECT 1' }, 'query', 'db-config-id');-- expect(result.err).toBe('DB config denied');- expect(getCurrentUser).toHaveBeenCalled();- });});describe('query mode', () => {@@ -75,39 +63,34 @@ describe('dataCollector', () => {expect(result.err).toBe('No query specified!');});- it('returns error when pool client is null', async () => {- const mockPool = { acquire: jest.fn(), release: jest.fn() };- (createClickhousePool as jest.Mock).mockReturnValue(mockPool);+ it('does not acquire the legacy pool for read queries', async () => {(asaw as jest.Mock)- .mockResolvedValueOnce([null, mockDbConfig]) // getDBConfigByUser- .mockResolvedValueOnce([null, null]); // pool.acquire returns null+ .mockResolvedValueOnce([null, mockDbConfig])+ .mockResolvedValueOnce([null, []]);const result = await dataCollector({ query: 'SELECT 1' });- // null client: either 'not available' or 'ClickHouse Query Error' from catch- expect(result.err).toBeDefined();+ expect(result).toEqual({ err: null, data: [] });+ expect(createClickhousePool).not.toHaveBeenCalled();+ expect(executeOpenPlaitRead).toHaveBeenCalledWith({+ query: 'SELECT 1',+ dbConfig: mockDbConfig,+ });});- it('returns err when pool.acquire fails', async () => {- const mockPool = { acquire: jest.fn(), release: jest.fn() };- (createClickhousePool as jest.Mock).mockReturnValue(mockPool);+ it('returns OpenPlait execution errors', async () => {(asaw as jest.Mock).mockResolvedValueOnce([null, mockDbConfig])- .mockResolvedValueOnce(['acquire failed', null]);+ .mockResolvedValueOnce(['query failed', null]);const result = await dataCollector({ query: 'SELECT 1' });- expect(result.err).toBe('acquire failed');+ expect(result.err).toBe('query failed');+ expect(result.data).toEqual([]);});it('executes query and returns json data on success', async () => {- const mockClient = makeClient();- const mockPool = { acquire: jest.fn(), release: jest.fn() };- (createClickhousePool as jest.Mock).mockReturnValue(mockPool);- const mockResultObj = { json: jest.fn() };(asaw as jest.Mock)- .mockResolvedValueOnce([null, mockDbConfig]) // getDBConfigByUser- .mockResolvedValueOnce([null, mockClient]) // pool.acquire- .mockResolvedValueOnce([null, mockResultObj]) // client.query- .mockResolvedValueOnce([null, [{ col: 1 }]]); // result.json+ .mockResolvedValueOnce([null, mockDbConfig])+ .mockResolvedValueOnce([null, [{ col: 1 }]]);const result = await dataCollector({ query: 'SELECT 1' });expect(result.data).toEqual([{ col: 1 }]);@@ -115,14 +98,8 @@ describe('dataCollector', () => {});it('applies readonly setting when enable_readonly is true', async () => {- const mockClient = makeClient();- const mockPool = { acquire: jest.fn(), release: jest.fn() };- (createClickhousePool as jest.Mock).mockReturnValue(mockPool);- const mockResultObj = { json: jest.fn() };(asaw as jest.Mock).mockResolvedValueOnce([null, mockDbConfig])- .mockResolvedValueOnce([null, mockClient])- .mockResolvedValueOnce([null, mockResultObj]).mockResolvedValueOnce([null, []]);const result = await dataCollector({ query: 'SELECT 1', enable_readonly: true });@@ -130,13 +107,9 @@ describe('dataCollector', () => {});it('returns error fallback when client.query call fails', async () => {- const mockClient = makeClient();- const mockPool = { acquire: jest.fn(), release: jest.fn() };- (createClickhousePool as jest.Mock).mockReturnValue(mockPool);(asaw as jest.Mock).mockResolvedValueOnce([null, mockDbConfig])- .mockResolvedValueOnce([null, mockClient])- .mockResolvedValueOnce(['query failed', null]); // client.query fails, result is null+ .mockResolvedValueOnce(['query failed', null]);const result = await dataCollector({ query: 'SELECT 1' });expect(result.err).toBeTruthy();@@ -289,7 +262,10 @@ describe('dataCollector', () => {throw new Error('Connection refused');});- const result = await dataCollector({ query: 'SELECT 1' });+ const result = await dataCollector(+ { table: 'test_table', values: [{ col: 1 }] },+ 'insert'+ );expect(result.err).toContain('ClickHouse Query Error');expect(result.err).toContain('Connection refused');});@@ -312,3 +288,26 @@ describe('dataCollector', () => {});});});++describe('intelligenceDataCollector', () => {+ it('uses the native ClickHouse client instead of OpenPlait for intelligence reads', async () => {+ const json = jest.fn();+ const resultSet = { json };+ const mockClient = makeClient({ query: jest.fn().mockResolvedValue(resultSet) });+ const mockPool = { acquire: jest.fn(), release: jest.fn() };+ (createClickhousePool as jest.Mock).mockReturnValue(mockPool);+ (asaw as jest.Mock)+ .mockResolvedValueOnce([null, mockDbConfig])+ .mockResolvedValueOnce([null, mockClient])+ .mockResolvedValueOnce([null, resultSet])+ .mockResolvedValueOnce([null, [{ total: 1 }]]);++ const result = await intelligenceDataCollector({ query: 'SELECT count() AS total FROM openlit_agents_summary' });++ expect(result).toEqual({ err: null, data: [{ total: 1 }] });+ expect(createClickhousePool).toHaveBeenCalledWith(mockDbConfig);+ expect(mockClient.query).toHaveBeenCalled();+ expect(executeOpenPlaitRead).not.toHaveBeenCalled();+ expect(mockPool.release).toHaveBeenCalledWith(mockClient);+ });+});diff --git a/src/client/src/__tests__/lib/platform/connectors/coverage.test.ts b/src/client/src/__tests__/lib/platform/connectors/coverage.test.tsnew file mode 100644index 00000000..9212069c--- /dev/null+++ b/src/client/src/__tests__/lib/platform/connectors/coverage.test.ts@@ -0,0 +1,30 @@+import { ensureAdaptersRegistered, __resetBootstrapForTests } from "@/lib/platform/connectors/datasource/bootstrap";+import { __resetRegistryForTests, getAdapterFactory, listSourceTypeDescriptors } from "@/lib/platform/connectors/datasource/registry";+import { listConnectorTypes } from "@/lib/platform/connectors/registry";++jest.mock("@/lib/session", () => ({ getCurrentUser: jest.fn() }));++describe("connector coverage", () => {+ afterEach(() => {+ __resetBootstrapForTests();+ __resetRegistryForTests();+ });++ it("exposes every atomic collector through the adapter and connector registries", () => {+ ensureAdaptersRegistered();+ const descriptors = listSourceTypeDescriptors();+ const connectorTypes = listConnectorTypes("datasource");++ expect(descriptors.length).toBeGreaterThanOrEqual(3);+ for (const descriptor of descriptors) {+ expect(getAdapterFactory(descriptor.type)).toBeDefined();+ expect(descriptor.declaredSignals.length).toBeGreaterThan(0);+ expect(new Set(descriptor.configFields.map((field) => field.key)).size).toBe(+ descriptor.configFields.length+ );+ }++ const registeredTypes = new Set(connectorTypes.map((descriptor) => descriptor.type));+ for (const descriptor of descriptors) expect(registeredTypes.has(descriptor.type)).toBe(true);+ });+});diff --git a/src/client/src/__tests__/lib/platform/connectors/registry.test.ts b/src/client/src/__tests__/lib/platform/connectors/registry.test.tsnew file mode 100644index 00000000..19caaa85--- /dev/null+++ b/src/client/src/__tests__/lib/platform/connectors/registry.test.ts@@ -0,0 +1,58 @@+import {+ __resetConnectorRegistryForTests,+ createConnectorRuntime,+ getConnectorType,+ listConnectorTypes,+ registerConnectorType,+} from "@/lib/platform/connectors";++describe("connector registry", () => {+ afterEach(() => {+ __resetConnectorRegistryForTests();+ });++ it("registers connector types by category and type", () => {+ registerConnectorType({+ descriptor: {+ type: "test-memory",+ category: "memory",+ displayName: "Test memory",+ scope: "project",+ configFields: [],+ capabilities: { read: true },+ },+ });++ expect(getConnectorType("memory", "test-memory")?.descriptor.displayName).toBe(+ "Test memory"+ );+ expect(listConnectorTypes("memory")).toHaveLength(1);+ expect(listConnectorTypes("datasource")).toHaveLength(0);+ });++ it("creates a runtime from a configured connector instance", async () => {+ registerConnectorType({+ descriptor: {+ type: "test-notification",+ category: "notification",+ displayName: "Test notification",+ scope: "organisation",+ configFields: [],+ capabilities: { notify: true },+ },+ create: () => ({ healthCheck: async () => ({ ok: true }) }),+ });++ const runtime = createConnectorRuntime({+ id: "connector-1",+ type: "test-notification",+ category: "notification",+ name: "Test",+ organisationId: "org-1",+ settings: {},+ });++ expect(await runtime?.healthCheck()).toEqual({ ok: true });+ });+});+diff --git a/src/client/src/__tests__/lib/platform/cron-log/cron-log.test.ts b/src/client/src/__tests__/lib/platform/cron-log/cron-log.test.tsindex 5f9deee7..f992ff54 100644--- a/src/client/src/__tests__/lib/platform/cron-log/cron-log.test.ts+++ b/src/client/src/__tests__/lib/platform/cron-log/cron-log.test.ts@@ -121,8 +121,13 @@ describe('getLastRunCronLogByCronId', () => {data: [{ startedAt: new Date('2024-01-15') }],err: null,});- const result = await getLastRunCronLogByCronId('cron-1');+ const result = await getLastRunCronLogByCronId('cron-1', 'db-1');expect(result).toEqual(new Date('2024-01-15'));+ expect(dataCollector).toHaveBeenCalledWith(+ expect.objectContaining({ query: expect.stringContaining("cron_id = 'cron-1'") }),+ 'query',+ 'db-1'+ );});it('returns null when data is null', async () => {diff --git a/src/client/src/__tests__/lib/platform/datasource/ai-selector.test.ts b/src/client/src/__tests__/lib/platform/datasource/ai-selector.test.tsnew file mode 100644index 00000000..a85c127f--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/ai-selector.test.ts@@ -0,0 +1,149 @@+import {+ AI_SELECTOR_MARKERS,+ aiSelectorToClickHouse,+ buildAITelemetrySelector,+ evalOperationClauseToClickHouse,+ operationTypeClauseToClickHouse,+} from "@/lib/platform/connectors/datasource/ai-selector";+import { CODING_AGENT_SPAN_NAMES } from "@/lib/platform/coding-agents/table-details";+import { SUPPORTED_EVALUATION_OPERATIONS } from "@/constants/traces";++describe("AI telemetry selector", () => {+ describe("buildAITelemetrySelector", () => {+ const selector = buildAITelemetrySelector();++ it("produces a disjunction (OR) of predicates", () => {+ expect(selector.anyOf.length).toBeGreaterThan(0);+ selector.anyOf.forEach((p) => {+ expect(Array.isArray(p.allOf)).toBe(true);+ expect(p.allOf.length).toBeGreaterThan(0);+ });+ });++ it("includes the OpenLIT SDK identity marker (resource telemetry.sdk.name=openlit)", () => {+ const match = selector.anyOf.find(+ (p) =>+ p.allOf.length === 1 &&+ p.allOf[0].scope === "resource" &&+ p.allOf[0].key === AI_SELECTOR_MARKERS.telemetrySdkName &&+ p.allOf[0].op === "eq" &&+ p.allOf[0].value === AI_SELECTOR_MARKERS.openlitSdkValue+ );+ expect(match).toBeDefined();+ });++ it("includes the coding CLI distro marker (openlit-cli)", () => {+ const match = selector.anyOf.find(+ (p) =>+ p.allOf[0].key === AI_SELECTOR_MARKERS.telemetryDistroName &&+ p.allOf[0].value === AI_SELECTOR_MARKERS.openlitCliValue+ );+ expect(match).toBeDefined();+ });++ it("includes generic gen_ai.* existence markers", () => {+ const genAiKeys = selector.anyOf+ .filter((p) => p.allOf.length === 1 && p.allOf[0].op === "exists")+ .map((p) => p.allOf[0].key);+ expect(genAiKeys).toContain(AI_SELECTOR_MARKERS.genAiOperation);+ expect(genAiKeys).toContain(AI_SELECTOR_MARKERS.genAiModel);+ expect(genAiKeys).toContain(AI_SELECTOR_MARKERS.genAiSystem);+ expect(genAiKeys).toContain(AI_SELECTOR_MARKERS.genAiProvider);+ expect(genAiKeys).toContain(AI_SELECTOR_MARKERS.genAiTool);+ });++ it("includes coding_agent.session.id on both span and resource scopes", () => {+ const scopes = selector.anyOf+ .filter(+ (p) =>+ p.allOf.length === 1 &&+ p.allOf[0].key === AI_SELECTOR_MARKERS.codingSessionId+ )+ .map((p) => p.allOf[0].scope);+ expect(scopes).toEqual(expect.arrayContaining(["span", "resource"]));+ });++ it("includes the native Claude Code AND-predicate (service.name + session.id)", () => {+ const claude = selector.anyOf.find(+ (p) =>+ p.allOf.length === 2 &&+ p.allOf.some(+ (c) =>+ c.key === AI_SELECTOR_MARKERS.serviceName &&+ c.value === AI_SELECTOR_MARKERS.claudeCodeValue+ ) &&+ p.allOf.some((c) => c.key === AI_SELECTOR_MARKERS.sessionId)+ );+ expect(claude).toBeDefined();+ });++ it("includes all coding-agent span names", () => {+ const spanNamePredicate = selector.anyOf.find(+ (p) => p.allOf.length === 1 && p.allOf[0].target === "spanName"+ );+ expect(spanNamePredicate).toBeDefined();+ expect(spanNamePredicate!.allOf[0].value).toEqual([+ ...CODING_AGENT_SPAN_NAMES,+ ]);+ });+ });++ describe("aiSelectorToClickHouse", () => {+ const sql = aiSelectorToClickHouse();++ it("wraps the whole selector in parentheses and ORs the groups", () => {+ expect(sql.startsWith("(")).toBe(true);+ expect(sql.endsWith(")")).toBe(true);+ expect(sql).toContain(" OR ");+ });++ it("translates identity markers", () => {+ expect(sql).toContain(+ "ResourceAttributes['telemetry.sdk.name'] = 'openlit'"+ );+ expect(sql).toContain(+ "ResourceAttributes['telemetry.distro.name'] = 'openlit-cli'"+ );+ });++ it("translates existence markers with notEmpty", () => {+ expect(sql).toContain("notEmpty(SpanAttributes['gen_ai.operation.name'])");+ expect(sql).toContain("notEmpty(SpanAttributes['coding_agent.session.id'])");+ expect(sql).toContain(+ "notEmpty(ResourceAttributes['coding_agent.session.id'])"+ );+ });++ it("translates the native Claude Code AND-group", () => {+ expect(sql).toContain(+ "(ResourceAttributes['service.name'] = 'claude-code' AND notEmpty(SpanAttributes['session.id']))"+ );+ });++ it("translates coding-agent span names to a SpanName IN clause", () => {+ CODING_AGENT_SPAN_NAMES.forEach((name) => {+ expect(sql).toContain(`'${name}'`);+ });+ expect(sql).toContain("SpanName IN (");+ });+ });++ describe("sub-selectors", () => {+ it("operationTypeClauseToClickHouse splits llm vs vectordb", () => {+ expect(operationTypeClauseToClickHouse("vectordb")).toBe(+ "SpanAttributes['gen_ai.operation.name'] = 'vectordb'"+ );+ expect(operationTypeClauseToClickHouse("llm")).toBe(+ "SpanAttributes['gen_ai.operation.name'] != 'vectordb'"+ );+ });++ it("evalOperationClauseToClickHouse lists supported eval operations", () => {+ const clause = evalOperationClauseToClickHouse();+ expect(clause).toContain("SpanAttributes['gen_ai.operation.name'] IN (");+ SUPPORTED_EVALUATION_OPERATIONS.forEach((op) => {+ expect(clause).toContain(`'${op}'`);+ });+ });+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/bootstrap.test.ts b/src/client/src/__tests__/lib/platform/datasource/bootstrap.test.tsnew file mode 100644index 00000000..0f6d6c33--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/bootstrap.test.ts@@ -0,0 +1,117 @@+jest.mock("@/lib/platform/connectors/datasource/http/secret", () => ({+ resolveSourceSecret: jest.fn(),+ redactableSecretValues: () => [],+}));+jest.mock("@/lib/platform/common", () => ({+ dataCollector: jest.fn(),+ OTEL_TRACES_TABLE_NAME: "otel_traces",+ OTEL_LOGS_TABLE_NAME: "otel_logs",+}));+jest.mock("@/lib/platform/request", () => ({+ getRequests: jest.fn(),+ getRequestViaSpanId: jest.fn(),+ getAttributeKeys: jest.fn(),+}));+jest.mock("@/lib/platform/observability", () => ({+ getLogs: jest.fn(),+ getLogByRowId: jest.fn(),+ getMetrics: jest.fn(),+ getMetricsConfig: jest.fn(),+ getLogAttributeKeys: jest.fn(),+ getMetricAttributeKeys: jest.fn(),+}));++import {+ __resetBootstrapForTests,+ ensureAdaptersRegistered,+} from "@/lib/platform/connectors/datasource/bootstrap";+import {+ __resetRegistryForTests,+ createAdapter,+ hasAdapterFactory,+ listSourceTypeDescriptors,+} from "@/lib/platform/connectors/datasource/registry";+import { ClickHouseAdapter } from "@/lib/platform/connectors/datasource/clickhouse/adapter";+import type { TelemetrySourceDescriptor } from "@/lib/platform/connectors/datasource/types";++beforeEach(() => {+ __resetRegistryForTests();+ __resetBootstrapForTests();+});++describe("datasource bootstrap", () => {+ it("registers the built-in ClickHouse factory exactly once", () => {+ ensureAdaptersRegistered();+ ensureAdaptersRegistered();+ expect(hasAdapterFactory("clickhouse")).toBe(true);+ });++ it("creates a ClickHouseAdapter from a built-in descriptor", () => {+ ensureAdaptersRegistered();+ const descriptor: TelemetrySourceDescriptor = {+ type: "clickhouse",+ id: "builtin:db-1",+ isBuiltIn: true,+ settings: {},+ dbConfigId: "db-1",+ signals: ["traces", "logs", "metrics"],+ name: "CH",+ };+ expect(createAdapter(descriptor)).toBeInstanceOf(ClickHouseAdapter);+ });++ it("registers only the supported CE connector factories", () => {+ ensureAdaptersRegistered();+ for (const type of [+ "clickhouse",+ "tempo",+ "loki",+ "prometheus",+ "jaeger",+ ]) {+ expect(hasAdapterFactory(type)).toBe(true);+ }+ for (const type of ["datadog", "mimir", "victoriametrics", "victorialogs"]) {+ expect(hasAdapterFactory(type)).toBe(false);+ }+ });++ it("re-registers after the registry Map is cleared (HMR-safe)", () => {+ ensureAdaptersRegistered();+ expect(hasAdapterFactory("loki")).toBe(true);+ // Simulate Next.js HMR reloading registry.ts into an empty Map while+ // bootstrap's module-level `registered` flag remains true.+ __resetRegistryForTests();+ expect(hasAdapterFactory("loki")).toBe(false);+ ensureAdaptersRegistered();+ expect(hasAdapterFactory("loki")).toBe(true);+ expect(hasAdapterFactory("prometheus")).toBe(true);+ });++ it("every registered atomic type exposes a valid config schema", () => {+ ensureAdaptersRegistered();+ const descriptors = listSourceTypeDescriptors({ includeInternal: true });+ expect(descriptors.length).toBeGreaterThan(0);+ for (const d of descriptors) {+ // configFields is the single source of truth for the form — it must+ // always be an array (empty for built-in/internal), and every field+ // must be self-describing (key + label + type).+ expect(Array.isArray(d.configFields)).toBe(true);+ for (const f of d.configFields) {+ expect(typeof f.key).toBe("string");+ expect(f.key.length).toBeGreaterThan(0);+ expect(typeof f.label).toBe("string");+ expect(f.label.length).toBeGreaterThan(0);+ expect(["text", "password", "url", "switch", "select"]).toContain(+ f.kind+ );+ expect(["settings", "credentials"]).toContain(f.group);+ }+ // Atomic (non-internal) vendor sources must declare an auth style so+ // the form can render the right credential hints without per-type code.+ if (!d.internal && d.type !== "clickhouse") {+ expect(d.authStyle).toBeDefined();+ }+ }+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/cache.test.ts b/src/client/src/__tests__/lib/platform/datasource/cache.test.tsnew file mode 100644index 00000000..52daba79--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/cache.test.ts@@ -0,0 +1,67 @@+import {+ __clearCache,+ cachedQuery,+ cacheKey,+} from "@/lib/platform/connectors/datasource/http/cache";++beforeEach(() => __clearCache());++describe("cacheKey", () => {+ it("is stable regardless of key order", () => {+ expect(cacheKey("s1", { b: 1, a: 2 })).toBe(cacheKey("s1", { a: 2, b: 1 }));+ });+ it("differs by source id", () => {+ expect(cacheKey("s1", { a: 1 })).not.toBe(cacheKey("s2", { a: 1 }));+ });+});++describe("cachedQuery", () => {+ it("caches results within TTL", async () => {+ const loader = jest.fn().mockResolvedValue(42);+ const key = cacheKey("s1", { q: 1 });+ expect(await cachedQuery(key, 1000, loader)).toBe(42);+ expect(await cachedQuery(key, 1000, loader)).toBe(42);+ expect(loader).toHaveBeenCalledTimes(1);+ });++ it("coalesces concurrent identical requests", async () => {+ let resolve!: (v: number) => void;+ const loader = jest.fn().mockReturnValue(+ new Promise<number>((r) => {+ resolve = r;+ })+ );+ const key = cacheKey("s1", { q: 2 });+ const p1 = cachedQuery(key, 1000, loader);+ const p2 = cachedQuery(key, 1000, loader);+ resolve(7);+ expect(await p1).toBe(7);+ expect(await p2).toBe(7);+ expect(loader).toHaveBeenCalledTimes(1);+ });++ it("reloads after TTL expiry", async () => {+ const loader = jest.fn().mockResolvedValueOnce(1).mockResolvedValueOnce(2);+ const key = cacheKey("s1", { q: 3 });+ expect(await cachedQuery(key, 0, loader)).toBe(1);+ await new Promise((r) => setTimeout(r, 5));+ expect(await cachedQuery(key, 0, loader)).toBe(2);+ expect(loader).toHaveBeenCalledTimes(2);+ });++ it("evicts oldest entries when the byte budget is exceeded", async () => {+ const { MAX_CACHE_BYTES, __cacheStats } = await import(+ "@/lib/platform/connectors/datasource/http/cache"+ );+ // Force a tiny budget by caching several large payloads under the real+ // MAX_CACHE_BYTES ceiling (skip-single-payload path is > MAX).+ const chunk = "y".repeat(Math.floor(MAX_CACHE_BYTES / 3));+ await cachedQuery(cacheKey("s1", { n: 1 }), 60_000, async () => chunk);+ await cachedQuery(cacheKey("s1", { n: 2 }), 60_000, async () => chunk);+ await cachedQuery(cacheKey("s1", { n: 3 }), 60_000, async () => chunk);+ await cachedQuery(cacheKey("s1", { n: 4 }), 60_000, async () => chunk);+ const stats = __cacheStats();+ expect(stats.entries).toBeLessThan(4);+ expect(stats.bytes).toBeLessThanOrEqual(MAX_CACHE_BYTES);+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/clickhouse-adapter.test.ts b/src/client/src/__tests__/lib/platform/datasource/clickhouse-adapter.test.tsnew file mode 100644index 00000000..8aba9af9--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/clickhouse-adapter.test.ts@@ -0,0 +1,257 @@+const mockDataCollector = jest.fn();+const mockGetRequests = jest.fn();+const mockGetRequestViaSpanId = jest.fn();+const mockGetAttributeKeys = jest.fn();+const mockGetLogs = jest.fn();+const mockGetLogByRowId = jest.fn();+const mockGetMetrics = jest.fn();+const mockGetMetricsConfig = jest.fn();+const mockGetLogAttributeKeys = jest.fn();+const mockGetMetricAttributeKeys = jest.fn();+const mockGetSignalSummary = jest.fn();++jest.mock("@/lib/platform/common", () => ({+ dataCollector: (...args: unknown[]) => mockDataCollector(...args),+ OTEL_TRACES_TABLE_NAME: "otel_traces",+ OTEL_LOGS_TABLE_NAME: "otel_logs",+}));++jest.mock("@/lib/platform/request", () => ({+ getRequests: (...args: unknown[]) => mockGetRequests(...args),+ getRequestViaSpanId: (...args: unknown[]) => mockGetRequestViaSpanId(...args),+ getAttributeKeys: (...args: unknown[]) => mockGetAttributeKeys(...args),+}));++jest.mock("@/lib/platform/observability", () => ({+ getLogs: (...args: unknown[]) => mockGetLogs(...args),+ getLogByRowId: (...args: unknown[]) => mockGetLogByRowId(...args),+ getMetrics: (...args: unknown[]) => mockGetMetrics(...args),+ getMetricsConfig: (...args: unknown[]) => mockGetMetricsConfig(...args),+ getLogAttributeKeys: (...args: unknown[]) => mockGetLogAttributeKeys(...args),+ getMetricAttributeKeys: (...args: unknown[]) =>+ mockGetMetricAttributeKeys(...args),+ getSignalSummary: (...args: unknown[]) => mockGetSignalSummary(...args),+}));++import { ClickHouseAdapter } from "@/lib/platform/connectors/datasource/clickhouse/adapter";+import type {+ OpenLITQuery,+ TelemetrySourceDescriptor,+} from "@/lib/platform/connectors/datasource/types";++const descriptor: TelemetrySourceDescriptor = {+ type: "clickhouse",+ id: "builtin:db-1",+ isBuiltIn: true,+ settings: {},+ dbConfigId: "db-1",+ signals: ["traces", "logs", "metrics"],+ name: "CH",+};++const adapter = new ClickHouseAdapter(descriptor);++const window = {+ start: new Date("2026-07-01T00:00:00.000Z"),+ end: new Date("2026-07-02T00:00:00.000Z"),+};++const baseQuery: OpenLITQuery = {+ signal: "traces",+ timeRange: window,+};++const rawSpan = {+ TraceId: "t1",+ SpanId: "s1",+ ParentSpanId: "p1",+ SpanName: "chat",+ ServiceName: "svc",+ Timestamp: "2026-07-01T10:00:00Z",+ Duration: 1500000,+ StatusCode: "STATUS_CODE_OK",+ SpanAttributes: { "gen_ai.usage.cost": "0.25", "gen_ai.request.model": "gpt-4" },+ ResourceAttributes: { "telemetry.sdk.name": "openlit" },+ Events: [{ Name: "gen_ai.content.prompt", Attributes: { foo: "bar" } }],+};++beforeEach(() => {+ jest.clearAllMocks();+});++describe("ClickHouseAdapter", () => {+ it("advertises the full ClickHouse capability set", () => {+ expect(adapter.capabilities()).toEqual({+ signals: ["traces", "logs", "metrics"],+ traceTree: true,+ spanEvents: true,+ serverAggregation: true,+ spanMutation: true,+ distinctValues: true,+ crossTraceSession: true,+ rawQuery: true,+ });+ });++ it("healthCheck pings the configured db config", async () => {+ mockDataCollector.mockResolvedValue({ data: true });+ const result = await adapter.healthCheck();+ expect(result.ok).toBe(true);+ expect(mockDataCollector).toHaveBeenCalledWith({}, "ping", "db-1");+ });++ it("listSpans normalizes getRequests records into NormalizedSpan", async () => {+ mockGetRequests.mockResolvedValue({ records: [rawSpan], total: 1 });+ const frame = await adapter.listSpans({ ...baseQuery, limit: 10 });+ expect(frame.rows).toHaveLength(1);+ expect(frame.rows[0]).toMatchObject({+ traceId: "t1",+ spanId: "s1",+ parentSpanId: "p1",+ name: "chat",+ serviceName: "svc",+ durationNs: 1500000,+ cost: 0.25,+ });+ expect(frame.meta?.rowsScanned).toBe(1);+ });++ it("getSpan returns null when not found", async () => {+ mockGetRequestViaSpanId.mockResolvedValue({ record: undefined });+ expect(await adapter.getSpan("nope")).toBeNull();+ });++ it("getSpan normalizes a found record and its events", async () => {+ mockGetRequestViaSpanId.mockResolvedValue({ record: rawSpan });+ const span = await adapter.getSpan("s1");+ expect(span?.spanId).toBe("s1");+ expect(span?.events).toEqual([+ { name: "gen_ai.content.prompt", timestamp: undefined, attributes: { foo: "bar" } },+ ]);+ });++ it("getTraceSpans queries by TraceId and targets the db config", async () => {+ mockDataCollector.mockResolvedValue({ data: [rawSpan] });+ const spans = await adapter.getTraceSpans("t1");+ expect(spans).toHaveLength(1);+ const [args, type, dbId] = mockDataCollector.mock.calls[0];+ expect((args as { query: string }).query).toContain("TraceId = 't1'");+ expect(type).toBe("query");+ expect(dbId).toBe("db-1");+ });++ it("getSpansBySession unions coding-agent session + parent ids", async () => {+ mockDataCollector.mockResolvedValue({ data: [] });+ await adapter.getSpansBySession("sess-1");+ const sql = (mockDataCollector.mock.calls[0][0] as { query: string }).query;+ expect(sql).toContain("SpanAttributes['coding_agent.session.id'] = 'sess-1'");+ expect(sql).toContain(+ "ResourceAttributes['coding_agent.agent.parent_id'] = 'sess-1'"+ );+ });++ it("validateAISignal counts spans matching the AI selector", async () => {+ mockDataCollector.mockResolvedValue({ data: [{ c: 42 }] });+ const result = await adapter.validateAISignal(window);+ expect(result.ok).toBe(true);+ expect(result.sampleCount).toBe(42);+ const sql = (mockDataCollector.mock.calls[0][0] as { query: string }).query;+ expect(sql).toContain("ResourceAttributes['telemetry.sdk.name'] = 'openlit'");+ });++ it("aggregateSpans builds group-by + aggregation SQL", async () => {+ mockDataCollector.mockResolvedValue({ data: [{ g0: "gpt-4", total: 5 }] });+ await adapter.aggregateSpans({+ ...baseQuery,+ groupBy: ["gen_ai.request.model"],+ aggregations: [{ fn: "sum", field: "gen_ai.usage.cost", as: "total" }],+ });+ const sql = (mockDataCollector.mock.calls[0][0] as { query: string }).query;+ expect(sql).toContain("SpanAttributes['gen_ai.request.model'] AS g0");+ expect(sql).toContain(+ "sum(toFloat64OrZero(SpanAttributes['gen_ai.usage.cost'])) AS total"+ );+ expect(sql).toContain("GROUP BY g0");+ });++ it("rejects SQL-shaped aggregation aliases", async () => {+ await expect(+ adapter.aggregateSpans({+ ...baseQuery,+ aggregations: [{ fn: "count", as: "x, (SELECT secret FROM vault)" }],+ })+ ).rejects.toThrow("Invalid dashboard aggregation alias");+ expect(mockDataCollector).not.toHaveBeenCalled();+ });++ it("spanTimeSeries buckets by interval", async () => {+ mockGetSignalSummary.mockResolvedValue({ buckets: [{ label: "10:00", count: 2 }] });+ const frame = await adapter.spanTimeSeries({ ...baseQuery, interval: "1h" });+ expect(mockGetSignalSummary).toHaveBeenCalledWith(+ expect.objectContaining({ databaseConfigId: "db-1" }),+ "traces"+ );+ expect(frame.rows).toEqual([{ label: "10:00", count: 2 }]);+ });++ it("distinctValues returns non-empty string values", async () => {+ mockDataCollector.mockResolvedValue({+ data: [{ v: "gpt-4" }, { v: "" }, { v: "claude" }],+ });+ const values = await adapter.distinctValues("gen_ai.request.model", baseQuery);+ expect(values).toEqual(["gpt-4", "claude"]);+ });++ it("distinctValues respects resource-scoped attribute fields", async () => {+ mockDataCollector.mockResolvedValue({ data: [{ v: "production" }] });+ await adapter.distinctValues("resource:deployment.environment", baseQuery);+ const sql = (mockDataCollector.mock.calls[0][0] as { query: string }).query;+ expect(sql).toContain("ResourceAttributes['deployment.environment'] AS v");+ });++ it("attributeKeys(traces) merges span + resource keys", async () => {+ mockGetAttributeKeys.mockResolvedValue({+ spanAttributeKeys: ["a"],+ resourceAttributeKeys: ["b"],+ });+ expect(await adapter.attributeKeys("traces", window)).toEqual(["a", "b"]);+ });++ it("discoverServices maps rollup rows", async () => {+ mockDataCollector.mockResolvedValue({+ data: [+ {+ serviceName: "svc",+ environment: "prod",+ clusterId: "c1",+ sdkName: "openlit",+ firstSeen: "2026-07-01T00:00:00Z",+ lastSeen: "2026-07-01T01:00:00Z",+ },+ ],+ });+ const services = await adapter.discoverServices(window);+ expect(services[0]).toMatchObject({+ serviceName: "svc",+ environment: "prod",+ clusterId: "c1",+ sdkName: "openlit",+ });+ });++ it("sampleTracesForGraph fetches spans for sampled trace ids", async () => {+ mockDataCollector+ .mockResolvedValueOnce({ data: [{ TraceId: "t1" }, { TraceId: "t2" }] })+ .mockResolvedValueOnce({ data: [rawSpan] });+ const spans = await adapter.sampleTracesForGraph(baseQuery, 50);+ expect(spans).toHaveLength(1);+ const secondSql = (mockDataCollector.mock.calls[1][0] as { query: string })+ .query;+ expect(secondSql).toContain("TraceId IN ('t1', 't2')");+ });++ it("sampleTracesForGraph returns [] when no traces match", async () => {+ mockDataCollector.mockResolvedValueOnce({ data: [] });+ expect(await adapter.sampleTracesForGraph(baseQuery, 50)).toEqual([]);+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/downsample.test.ts b/src/client/src/__tests__/lib/platform/datasource/downsample.test.tsnew file mode 100644index 00000000..451a74e6--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/downsample.test.ts@@ -0,0 +1,146 @@+import {+ DEFAULT_MAX_DATA_POINTS,+ MIN_INTERVAL_MS,+ MAX_SERIES_POINTS,+ roundIntervalMs,+ computeIntervalMs,+ rateIntervalMs,+ clampStepMs,+ parseDurationMs,+ intervalMsToLabel,+ intervalMsToSeconds,+ alignRangeToStep,+} from "@/lib/platform/connectors/datasource/downsample";+import type { QueryTimeRange } from "@/lib/platform/connectors/datasource/types";++const range = (ms: number): QueryTimeRange => {+ const end = new Date("2026-07-10T12:00:00Z");+ return { start: new Date(end.getTime() - ms), end };+};++const MIN = 60_000;+const HOUR = 3_600_000;+const DAY = 86_400_000;++describe("roundIntervalMs", () => {+ it("snaps a raw interval up to the nearest nice step", () => {+ expect(roundIntervalMs(7_000)).toBe(10_000);+ expect(roundIntervalMs(12_000)).toBe(30_000);+ expect(roundIntervalMs(45_000)).toBe(MIN);+ expect(roundIntervalMs(4 * MIN)).toBe(5 * MIN);+ });++ it("rounds up to whole days beyond a week", () => {+ const eightDays = 8 * DAY;+ expect(roundIntervalMs(eightDays)).toBe(8 * DAY);+ });+});++describe("computeIntervalMs", () => {+ it("derives interval from range / maxDataPoints (Grafana math)", () => {+ // 1h over 300 points => ~12s raw => snaps to 30s.+ const ms = computeIntervalMs({+ timeRange: range(HOUR),+ maxDataPoints: DEFAULT_MAX_DATA_POINTS,+ });+ expect(ms).toBe(30_000);+ });++ it("never returns below the minimum interval", () => {+ const ms = computeIntervalMs({+ timeRange: range(MIN),+ maxDataPoints: 1000,+ });+ expect(ms).toBeGreaterThanOrEqual(MIN_INTERVAL_MS);+ });++ it("honors an explicit interval label over derived math", () => {+ const ms = computeIntervalMs({+ timeRange: range(DAY),+ maxDataPoints: 300,+ interval: "5m",+ });+ expect(ms).toBe(5 * MIN);+ });++ it("wider ranges produce coarser steps", () => {+ const hour = computeIntervalMs({ timeRange: range(HOUR), maxDataPoints: 300 });+ const week = computeIntervalMs({+ timeRange: range(7 * DAY),+ maxDataPoints: 300,+ });+ expect(week).toBeGreaterThan(hour);+ });+});++describe("rateIntervalMs", () => {+ it("is at least 4x the scrape interval", () => {+ expect(rateIntervalMs(10_000, 15_000)).toBe(60_000);+ });++ it("is step + scrape when that is larger", () => {+ expect(rateIntervalMs(120_000, 15_000)).toBe(135_000);+ });+});++describe("clampStepMs", () => {+ it("widens the step when points would exceed the cap", () => {+ const rangeMs = MAX_SERIES_POINTS * 10_000 * 2; // 2x the cap at 10s steps+ const step = clampStepMs(rangeMs, 10_000);+ expect(Math.ceil(rangeMs / step)).toBeLessThanOrEqual(MAX_SERIES_POINTS);+ });++ it("leaves an under-cap step unchanged", () => {+ expect(clampStepMs(HOUR, MIN)).toBe(MIN);+ });+});++describe("parseDurationMs", () => {+ it("parses compact and named durations", () => {+ expect(parseDurationMs("30s")).toBe(30_000);+ expect(parseDurationMs("5m")).toBe(5 * MIN);+ expect(parseDurationMs("1h")).toBe(HOUR);+ expect(parseDurationMs("1d")).toBe(DAY);+ expect(parseDurationMs("minute")).toBe(MIN);+ expect(parseDurationMs("hour")).toBe(HOUR);+ });++ it("returns 0 for invalid labels", () => {+ expect(parseDurationMs("nonsense")).toBe(0);+ expect(parseDurationMs("")).toBe(0);+ });+});++describe("interval formatting", () => {+ it("formats ms as a compact label", () => {+ expect(intervalMsToLabel(30_000)).toBe("30s");+ expect(intervalMsToLabel(5 * MIN)).toBe("5m");+ expect(intervalMsToLabel(HOUR)).toBe("1h");+ expect(intervalMsToLabel(DAY)).toBe("1d");+ });++ it("converts ms to integer Prometheus seconds", () => {+ expect(intervalMsToSeconds(30_000)).toBe(30);+ expect(intervalMsToSeconds(500)).toBe(1);+ });+});++describe("alignRangeToStep", () => {+ it("floors start and ceils end to the step so windows dedupe", () => {+ const start = new Date(1_000_000_000_123);+ const end = new Date(1_000_000_030_456);+ const aligned = alignRangeToStep({ start, end }, 30_000);+ expect(aligned.start.getTime() % 30_000).toBe(0);+ expect(aligned.end.getTime() % 30_000).toBe(0);+ expect(aligned.start.getTime()).toBeLessThanOrEqual(start.getTime());+ expect(aligned.end.getTime()).toBeGreaterThanOrEqual(end.getTime());+ });++ it("does not mutate the input range", () => {+ const start = new Date(1_000_000_000_123);+ const end = new Date(1_000_000_030_456);+ alignRangeToStep({ start, end }, 30_000);+ expect(start.getTime()).toBe(1_000_000_000_123);+ expect(end.getTime()).toBe(1_000_000_030_456);+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/endpoint-url.test.ts b/src/client/src/__tests__/lib/platform/datasource/endpoint-url.test.tsnew file mode 100644index 00000000..34ffc11c--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/endpoint-url.test.ts@@ -0,0 +1,68 @@+import {+ isEnabledSetting,+ normalizeDatasourceEndpointUrl,+ rewriteLoopbackEndpointForDocker,+} from "@/lib/platform/connectors/datasource/http/endpoint-url";+import { selfHostedNetworkOptions } from "@/lib/platform/connectors/datasource/http/safe-fetch";++describe("normalizeDatasourceEndpointUrl", () => {+ it("repairs collapsed http:/ authority slashes", () => {+ expect(normalizeDatasourceEndpointUrl("http:/localhost:9090")).toBe(+ "http://localhost:9090"+ );+ expect(normalizeDatasourceEndpointUrl("https:/prom.example.com/path/")).toBe(+ "https://prom.example.com/path"+ );+ });++ it("preserves valid URLs and strips trailing slashes", () => {+ expect(normalizeDatasourceEndpointUrl("http://localhost:9090/")).toBe(+ "http://localhost:9090"+ );+ });+});++describe("rewriteLoopbackEndpointForDocker", () => {+ it("rewrites localhost and 127.0.0.1 when enabled", () => {+ expect(+ rewriteLoopbackEndpointForDocker("http://localhost:3100", { enabled: true })+ ).toBe("http://host.docker.internal:3100");+ expect(+ rewriteLoopbackEndpointForDocker("http://127.0.0.1:3100/loki", {+ enabled: true,+ })+ ).toBe("http://host.docker.internal:3100/loki");+ });++ it("leaves non-loopback hosts unchanged", () => {+ expect(+ rewriteLoopbackEndpointForDocker("http://loki:3100", { enabled: true })+ ).toBe("http://loki:3100");+ });++ it("is a no-op when disabled", () => {+ expect(+ rewriteLoopbackEndpointForDocker("http://localhost:3100", { enabled: false })+ ).toBe("http://localhost:3100");+ });+});++describe("isEnabledSetting / selfHostedNetworkOptions", () => {+ it("treats common truthy switch encodings as enabled", () => {+ expect(isEnabledSetting(true)).toBe(true);+ expect(isEnabledSetting("true")).toBe(true);+ expect(isEnabledSetting(1)).toBe(true);+ expect(isEnabledSetting("1")).toBe(true);+ expect(isEnabledSetting(false)).toBe(false);+ expect(isEnabledSetting("false")).toBe(false);+ });++ it("reads allowPrivateNetwork from string switch values", () => {+ expect(+ selfHostedNetworkOptions({ allowPrivateNetwork: "true", allowHttp: "true" })+ ).toEqual({ allowHttp: true, allowPrivateNetwork: true });+ expect(+ selfHostedNetworkOptions({ allowPrivateNetwork: false, allowHttp: false })+ ).toEqual({ allowHttp: false, allowPrivateNetwork: false });+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/field-values.test.ts b/src/client/src/__tests__/lib/platform/datasource/field-values.test.tsnew file mode 100644index 00000000..327bd509--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/field-values.test.ts@@ -0,0 +1,46 @@+const mockResolveSignalReadContext = jest.fn();+const mockPlanAndDistinctValues = jest.fn();++jest.mock("@/lib/platform/connectors/datasource/facade", () => ({+ resolveSignalReadContext: (...args: unknown[]) =>+ mockResolveSignalReadContext(...args),+ facadeErrorMessage: (error: unknown) =>+ error instanceof Error ? error.message : String(error),+}));+jest.mock("@/lib/platform/connectors/datasource/query-planner", () => ({+ planAndDistinctValues: (...args: unknown[]) =>+ mockPlanAndDistinctValues(...args),+}));+jest.mock("@/lib/platform/connectors/datasource/clickhouse/query-map", () => ({+ metricParamsToOpenLITQuery: (_params: unknown, signal: string) => ({+ signal,+ timeRange: {},+ }),+}));++import { getSignalFieldValues } from "@/lib/platform/connectors/datasource/field-values";++describe("getSignalFieldValues", () => {+ it("resolves the selected signal adapter and returns unique sorted values", async () => {+ const adapter = { type: "tempo" };+ mockResolveSignalReadContext.mockResolvedValue({ adapter });+ mockPlanAndDistinctValues.mockResolvedValue(["zeta", "alpha", "alpha"]);++ const result = await getSignalFieldValues(+ "traces",+ "service.name",+ { timeLimit: {} } as any+ );++ expect(mockResolveSignalReadContext).toHaveBeenCalledWith(+ "traces",+ expect.any(Object)+ );+ expect(mockPlanAndDistinctValues).toHaveBeenCalledWith(+ adapter,+ "service.name",+ expect.objectContaining({ signal: "traces" })+ );+ expect(result).toEqual({ err: null, values: ["alpha", "zeta"] });+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/grafana.test.ts b/src/client/src/__tests__/lib/platform/datasource/grafana.test.tsnew file mode 100644index 00000000..02b22529--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/grafana.test.ts@@ -0,0 +1,893 @@+const mockSafeFetch = jest.fn();++jest.mock("@/lib/platform/connectors/datasource/http/safe-fetch", () => ({+ safeFetch: (...args: unknown[]) => mockSafeFetch(...args),+ SourceResponseError: class SourceResponseError extends Error {+ status: number;+ constructor(status: number, message: string) {+ super(message);+ this.status = status;+ }+ },+ selfHostedNetworkOptions: () => ({+ allowHttp: true,+ allowPrivateNetwork: true,+ }),+}));+jest.mock("@/lib/platform/connectors/datasource/http/secret", () => ({+ resolveSourceSecret: jest.fn().mockResolvedValue({+ raw: "tok",+ credentials: { token: "tok" },+ }),+ redactableSecretValues: () => ["tok"],+}));++import { TempoAdapter, tempoAISelectorQuery, buildTempoSearchQuery, __clearTempoSpanIndex } from "@/lib/platform/connectors/datasource/grafana/tempo";+import { SourceResponseError } from "@/lib/platform/connectors/datasource/http/safe-fetch";+import { __clearCache } from "@/lib/platform/connectors/datasource/http/cache";+import { buildAggregateDag } from "@/lib/platform/connectors/datasource/graph/aggregate-dag";+import type {+ NormalizedSpan,+ TelemetrySourceDescriptor,+} from "@/lib/platform/connectors/datasource/types";++const window = {+ start: new Date("2026-07-01T00:00:00.000Z"),+ end: new Date("2026-07-02T00:00:00.000Z"),+};++const TRACE_1 = "0123456789abcdef0123456789abcdef";+const TRACE_2 = "fedcba9876543210fedcba9876543210";+const SPAN_1 = "0123456789abcdef";+const SPAN_2 = "fedcba9876543210";++const otlpTrace = {+ batches: [+ {+ resource: {+ attributes: [+ { key: "service.name", value: { stringValue: "svc" } },+ { key: "telemetry.sdk.name", value: { stringValue: "openlit" } },+ ],+ },+ scopeSpans: [+ {+ spans: [+ {+ traceId: TRACE_1,+ spanId: SPAN_1,+ parentSpanId: "",+ name: "chat",+ startTimeUnixNano: "1719792000000000000",+ endTimeUnixNano: "1719792001000000000",+ status: { code: 1 },+ attributes: [+ { key: "gen_ai.request.model", value: { stringValue: "gpt-4" } },+ ],+ events: [+ {+ name: "gen_ai.content.prompt",+ attributes: [{ key: "gen_ai.prompt", value: { stringValue: "hi" } }],+ },+ ],+ },+ ],+ },+ ],+ },+ ],+};++function otlpForTrace(traceId: string, spanId: string) {+ return {+ batches: [+ {+ resource: otlpTrace.batches[0].resource,+ scopeSpans: [+ {+ spans: [+ {+ ...otlpTrace.batches[0].scopeSpans[0].spans[0],+ traceId,+ spanId,+ },+ ],+ },+ ],+ },+ ],+ };+}++beforeEach(() => {+ jest.clearAllMocks();+ __clearCache();+ __clearTempoSpanIndex();+});++describe("tempoAISelectorQuery", () => {+ const q = tempoAISelectorQuery();+ it("wraps TraceQL in braces and uses span./resource. prefixes", () => {+ expect(q.startsWith("{ ")).toBe(true);+ expect(q).toContain('resource.telemetry.sdk.name = "openlit"');+ expect(q).toContain('span.gen_ai.operation.name != ""');+ expect(q).toContain('name = "coding_agent.session"');+ });+});++describe("buildTempoSearchQuery scoping", () => {+ const window = {+ start: new Date("2026-07-11T00:00:00Z"),+ end: new Date("2026-07-11T01:00:00Z"),+ };++ it("parenthesizes the multi-group AI selector so a service filter scopes the whole selector", () => {+ const q = buildTempoSearchQuery({+ signal: "traces",+ timeRange: window,+ aiSelector: true,+ filters: [+ {+ target: "attribute",+ scope: "resource",+ key: "service.name",+ op: "eq",+ value: "demo-openai-app",+ },+ ],+ });+ // The AI selector (which contains `||`) must be wrapped so the trailing+ // `&& resource.service.name = ...` constrains every OR branch, not just+ // the last one (TraceQL binds && tighter than ||).+ expect(q).toContain('&& resource.service.name = "demo-openai-app"');+ const beforeService = q.slice(0, q.indexOf("&& resource.service.name"));+ // Everything before the service clause is a single parenthesized group.+ expect(beforeService.trim().startsWith("{ (")).toBe(true);+ expect(beforeService).toContain(")");+ });++ it("drops hostile attribute keys instead of interpolating them into TraceQL", () => {+ const q = buildTempoSearchQuery({+ signal: "traces",+ timeRange: window,+ aiSelector: false,+ filters: [+ {+ target: "attribute",+ scope: "span",+ key: 'foo" || true || span.bar',+ op: "eq",+ value: "x",+ },+ {+ target: "attribute",+ scope: "resource",+ key: "service.name",+ op: "eq",+ value: "ok",+ },+ ],+ });+ expect(q).not.toContain("|| true");+ expect(q).toContain('resource.service.name = "ok"');+ });+});++describe("TempoAdapter", () => {+ const descriptor: TelemetrySourceDescriptor = {+ type: "tempo",+ id: "src-tempo",+ isBuiltIn: false,+ settings: { url: "https://tempo.example.com", allowHttp: false },+ signals: ["traces"],+ name: "Tempo",+ };+ const adapter = new TempoAdapter(descriptor);++ it("advertises trace-only, span-events true, no server aggregation", () => {+ expect(adapter.capabilities()).toMatchObject({+ signals: ["traces"],+ traceTree: true,+ spanEvents: true,+ serverAggregation: false,+ distinctValues: true,+ });+ });++ it("retries a 31-day HTTP 400 search with Tempo's compatible 30-day window", async () => {+ const wideAdapter = new TempoAdapter({+ ...descriptor,+ settings: {+ ...descriptor.settings,+ maxTimeRangeDays: 31,+ },+ });+ const end = new Date("2026-08-06T11:24:17.901Z");+ const start = new Date(end.getTime() - 31 * 24 * 60 * 60 * 1000);+ mockSafeFetch+ .mockRejectedValueOnce(+ new SourceResponseError(+ 400,+ "Data source responded 400: query range duration exceeds max duration 720h0m0s"+ )+ )+ .mockResolvedValueOnce({+ traces: [{+ traceID: TRACE_1,+ rootServiceName: "svc",+ rootTraceName: "chat",+ startTimeUnixNano: "1719792000000000000",+ durationMs: 1000,+ }],+ });++ const frame = await wideAdapter.listSpans({+ signal: "traces",+ timeRange: { start, end },+ aiSelector: false,+ limit: 1,+ });++ expect(frame.rows).toHaveLength(1);+ expect(mockSafeFetch).toHaveBeenCalledTimes(2);+ const retriedSearch = new URL(mockSafeFetch.mock.calls[1][0] as string);+ expect(Number(retriedSearch.searchParams.get("start"))).toBe(+ Math.ceil(end.getTime() / 1000) - 30 * 24 * 60 * 60+ );+ });++ it("retries Tempo 400s that report a range ceiling without the word duration", async () => {+ const wideAdapter = new TempoAdapter({+ ...descriptor,+ id: "src-tempo-range-wording",+ settings: {+ ...descriptor.settings,+ maxTimeRangeDays: 31,+ },+ });+ const end = new Date("2026-08-06T11:24:17.901Z");+ const start = new Date(end.getTime() - 31 * 24 * 60 * 60 * 1000);+ mockSafeFetch+ .mockRejectedValueOnce(+ new SourceResponseError(+ 400,+ "range specified by start and end exceeds 168h0m0s"+ )+ )+ .mockResolvedValueOnce({+ traces: [{+ traceID: TRACE_1,+ rootServiceName: "svc",+ rootTraceName: "chat",+ startTimeUnixNano: "1719792000000000000",+ durationMs: 1000,+ }],+ });++ const frame = await wideAdapter.listSpans({+ signal: "traces",+ timeRange: { start, end },+ aiSelector: false,+ limit: 1,+ });++ expect(frame.rows).toHaveLength(1);+ expect(mockSafeFetch).toHaveBeenCalledTimes(2);+ const retriedSearch = new URL(mockSafeFetch.mock.calls[1][0] as string);+ expect(Number(retriedSearch.searchParams.get("start"))).toBe(+ Math.ceil(end.getTime() / 1000) - 7 * 24 * 60 * 60+ );+ });++ it("caps unconfigured Tempo searches at 7 days before the first request", async () => {+ const end = new Date("2026-08-06T11:24:17.901Z");+ const start = new Date(end.getTime() - 30 * 24 * 60 * 60 * 1000);+ mockSafeFetch.mockResolvedValueOnce({+ traces: [{+ traceID: TRACE_1,+ rootServiceName: "svc",+ rootTraceName: "chat",+ startTimeUnixNano: "1719792000000000000",+ durationMs: 1000,+ }],+ });++ await adapter.listSpans({+ signal: "traces",+ timeRange: { start, end },+ aiSelector: false,+ limit: 1,+ });++ expect(mockSafeFetch).toHaveBeenCalledTimes(1);+ const search = new URL(mockSafeFetch.mock.calls[0][0] as string);+ expect(Number(search.searchParams.get("start"))).toBe(+ Math.ceil(end.getTime() / 1000) - 7 * 24 * 60 * 60+ );+ });++ it("does not retry a generic Tempo 400 by weakening the query", async () => {+ mockSafeFetch.mockRejectedValueOnce(+ new SourceResponseError(+ 400,+ "Data source responded 400: invalid TraceQL near resource.service.name"+ )+ );+ await expect(+ adapter.listSpans({+ signal: "traces",+ timeRange: window,+ aiSelector: true,+ filters: [{+ target: "attribute",+ scope: "resource",+ key: "service.name",+ op: "eq",+ value: "svc",+ }],+ limit: 1,+ })+ ).rejects.toThrow("Tempo returned HTTP 400");+ expect(mockSafeFetch).toHaveBeenCalledTimes(1);+ const failedUrl = decodeURIComponent(+ mockSafeFetch.mock.calls[0][0] as string+ ).replace(/\+/g, " ");+ expect(failedUrl).toContain('resource.service.name = "svc"');+ });++ it("detects Tempo 2.8 and enables most_recent only after a successful health inspection", async () => {+ mockSafeFetch+ .mockResolvedValueOnce({ version: "2.8.2" })+ .mockResolvedValueOnce({+ traces: [{+ traceID: TRACE_1,+ rootServiceName: "svc",+ rootTraceName: "chat",+ startTimeUnixNano: "1719792000000000000",+ durationMs: 12,+ }],+ });+ await expect(adapter.healthCheck()).resolves.toMatchObject({ ok: true });+ await adapter.listSpans({+ signal: "traces",+ timeRange: window,+ aiSelector: false,+ limit: 1,+ });+ expect(mockSafeFetch).toHaveBeenCalledTimes(2);+ const searchUrl = decodeURIComponent(+ mockSafeFetch.mock.calls[1][0] as string+ ).replace(/\+/g, " ");+ expect(searchUrl).toContain("with (most_recent=true)");+ });++ it("checks a minimal search when a managed Tempo gateway hides build metadata", async () => {+ mockSafeFetch+ .mockRejectedValueOnce(new SourceResponseError(404, "buildinfo is not exposed"))+ .mockResolvedValueOnce({ traces: [] });++ await expect(adapter.healthCheck()).resolves.toMatchObject({ ok: true });+ expect(mockSafeFetch).toHaveBeenCalledTimes(2);+ const searchUrl = decodeURIComponent(+ mockSafeFetch.mock.calls[1][0] as string+ ).replace(/\+/g, " ");+ expect(searchUrl).toContain("/api/search");+ expect(searchUrl).not.toContain("most_recent");+ });++ it("aggregateSpans totals every matching span via native TraceQL metrics (no sample cap)", async () => {+ // Grafana-style: counts come from `/api/metrics/query_range`, which+ // aggregates over the whole window. Buckets summing to 4200 must surface+ // as 4200 — the old L1 path would have capped this at the 200-trace sample.+ mockSafeFetch.mockResolvedValueOnce({+ series: [+ {+ labels: [],+ samples: [+ { timestampMs: window.start.getTime(), value: 1200 },+ { timestampMs: window.start.getTime() + 3_600_000, value: 3000 },+ ],+ },+ ],+ });+ const frame = await adapter.aggregateSpans({+ signal: "traces",+ timeRange: window,+ aiSelector: true,+ filters: [+ {+ target: "attribute",+ scope: "resource",+ key: "service.name",+ op: "eq",+ value: "svc",+ },+ ],+ aggregations: [{ fn: "count", as: "total_requests" }],+ });+ expect(Number((frame.rows[0] as any)?.total_requests)).toBe(4200);+ expect(frame.meta?.freshness).toBe("live");+ // The service scope + count function must reach the metrics query.+ const metricsUrl = decodeURIComponent(+ mockSafeFetch.mock.calls[0][0] as string+ ).replace(/\+/g, " ");+ expect(metricsUrl).toContain("/api/metrics/query_range");+ expect(metricsUrl).toContain("count_over_time()");+ expect(metricsUrl).toContain('resource.service.name = "svc"');+ });++ it("aggregateSpans falls back to the L1 sample when metrics are unavailable", async () => {+ // Metrics endpoint returns no series -> we fall back to TraceQL search ++ // full-trace fetch and count in-process (degraded / sampled).+ mockSafeFetch+ .mockRejectedValueOnce(+ new SourceResponseError(404, "Data source responded 404: metrics disabled")+ ) // metrics endpoint unavailable+ .mockResolvedValueOnce({ traces: [{ traceID: TRACE_1 }] }) // search+ .mockResolvedValueOnce(otlpTrace); // trace fetch+ const frame = await adapter.aggregateSpans({+ signal: "traces",+ timeRange: window,+ aiSelector: true,+ filters: [+ {+ target: "attribute",+ scope: "resource",+ key: "service.name",+ op: "eq",+ value: "svc",+ },+ ],+ aggregations: [{ fn: "count", as: "total_requests" }],+ });+ expect(+ Number(+ (frame.rows[0] as any)?.total_requests ??+ (frame.rows[0] as any)?.count+ )+ ).toBeGreaterThan(0);+ expect(frame.meta?.degraded).toContain("serverAggregation");+ const searchUrl = decodeURIComponent(+ mockSafeFetch.mock.calls[1][0] as string+ ).replace(/\+/g, " ");+ expect(searchUrl).toContain('resource.service.name = "svc"');+ });++ it("aggregateSpans groups by model via a metrics `by (...)` clause", async () => {+ mockSafeFetch.mockResolvedValueOnce({+ series: [+ {+ labels: [+ { key: "gen_ai.request.model", value: { stringValue: "gpt-4" } },+ ],+ samples: [{ timestampMs: window.start.getTime(), value: 100 }],+ },+ {+ labels: [+ { key: "gen_ai.request.model", value: { stringValue: "gpt-3.5" } },+ ],+ samples: [{ timestampMs: window.start.getTime(), value: 50 }],+ },+ ],+ });+ const frame = await adapter.aggregateSpans({+ signal: "traces",+ timeRange: window,+ aiSelector: true,+ groupBy: ["gen_ai.request.model"],+ aggregations: [{ fn: "count", as: "count" }],+ });+ const byModel = Object.fromEntries(+ frame.rows.map((r: any) => [r.group_value, Number(r.count)])+ );+ expect(byModel["gpt-4"]).toBe(100);+ expect(byModel["gpt-3.5"]).toBe(50);+ const metricsUrl = decodeURIComponent(+ mockSafeFetch.mock.calls[0][0] as string+ ).replace(/\+/g, " ");+ expect(metricsUrl).toContain("by (span.gen_ai.request.model)");+ });++ it("spanTimeSeries merges count + sum buckets from native metrics", async () => {+ const t0 = window.start.getTime();+ const t1 = t0 + 3_600_000;+ mockSafeFetch+ .mockResolvedValueOnce({+ series: [+ {+ labels: [],+ samples: [+ { timestampMs: t0, value: 10 },+ { timestampMs: t1, value: 20 },+ ],+ },+ ],+ })+ .mockResolvedValueOnce({+ series: [+ {+ labels: [],+ samples: [+ { timestampMs: t0, value: 5 },+ { timestampMs: t1, value: 15 },+ ],+ },+ ],+ });+ const frame = await adapter.spanTimeSeries({+ signal: "traces",+ timeRange: window,+ aiSelector: true,+ interval: "1h",+ aggregations: [+ { fn: "count", as: "count" },+ { fn: "sum", field: "gen_ai.usage.cost", as: "cost" },+ ],+ });+ expect(frame.rows).toHaveLength(2);+ expect(frame.rows.map((r: any) => Number(r.count))).toEqual([10, 20]);+ expect(frame.rows.map((r: any) => Number(r.cost))).toEqual([5, 15]);+ });++ it("chunks multi-day TraceQL metrics through OpenPlait and de-duplicates boundary buckets", async () => {+ const start = new Date("2026-07-01T00:00:00.000Z");+ const boundary = new Date("2026-07-02T00:00:00.000Z");+ const end = new Date("2026-07-03T00:00:00.000Z");+ mockSafeFetch+ .mockResolvedValueOnce({+ series: [{ labels: [], samples: [+ { timestampMs: start.getTime(), value: 2 },+ { timestampMs: boundary.getTime(), value: 3 },+ ] }],+ })+ .mockResolvedValueOnce({+ series: [{ labels: [], samples: [+ { timestampMs: boundary.getTime(), value: 3 },+ { timestampMs: end.getTime(), value: 4 },+ ] }],+ });+ const frame = await adapter.spanTimeSeries({+ signal: "traces",+ timeRange: { start, end },+ aiSelector: false,+ interval: "1h",+ aggregations: [{ fn: "count", as: "count" }],+ });+ expect((frame.rows as any[]).map((row) => Number(row.count))).toEqual([2, 3, 4]);+ expect(mockSafeFetch).toHaveBeenCalledTimes(2);+ for (const call of mockSafeFetch.mock.calls) {+ expect(new URL(call[0] as string).pathname).toBe("/api/metrics/query_range");+ }+ });++ it("builds trace summary buckets from one OpenPlait search without fetching trace details", async () => {+ mockSafeFetch.mockResolvedValueOnce({+ traces: [+ {+ traceID: TRACE_1,+ rootServiceName: "checkout",+ rootTraceName: "POST /checkout",+ startTimeUnixNano: "1782864000000000000",+ durationMs: 125,+ },+ {+ traceID: TRACE_2,+ rootServiceName: "payments",+ rootTraceName: "POST /charge",+ startTimeUnixNano: "1782867600000000000",+ durationMs: 75,+ },+ ],+ });++ const frame = await adapter.traceTimeSeries!({+ signal: "traces",+ timeRange: window,+ interval: "1h",+ aiSelector: false,+ aggregations: [+ { fn: "count", as: "count" },+ { fn: "avg", field: "duration", as: "avgDuration" },+ ],+ });++ expect(+ (frame.rows as any[]).reduce(+ (sum: number, row: any) => sum + Number(row.count || 0),+ 0+ )+ ).toBe(2);+ expect(frame.meta).toMatchObject({+ freshness: "live",+ truncated: false,+ rowsScanned: 2,+ });+ expect(mockSafeFetch).toHaveBeenCalledTimes(1);+ const searchUrl = new URL(mockSafeFetch.mock.calls[0][0] as string);+ expect(searchUrl.pathname).toBe("/api/search");+ expect(searchUrl.searchParams.get("limit")).toBe("5001");+ await expect(adapter.countTraces!({+ signal: "traces",+ timeRange: window,+ interval: "1h",+ aiSelector: false,+ })).resolves.toEqual({ total: 2, truncated: false });+ // The list total and summary chart share the same cached Tempo search.+ expect(mockSafeFetch).toHaveBeenCalledTimes(1);+ });++ it("learns Grafana Cloud's Tempo search ceiling and retries the summary", async () => {+ mockSafeFetch+ .mockRejectedValueOnce(+ new SourceResponseError(+ 400,+ "Data source responded 400: limit 5001 exceeds max limit 1000"+ )+ )+ .mockResolvedValueOnce({+ traces: [+ {+ traceID: TRACE_1,+ rootServiceName: "checkout",+ rootTraceName: "POST /checkout",+ startTimeUnixNano: "1782864000000000000",+ durationMs: 125,+ },+ ],+ });++ const frame = await adapter.traceTimeSeries!({+ signal: "traces",+ timeRange: window,+ interval: "1h",+ aiSelector: false,+ aggregations: [{ fn: "count", as: "count" }],+ });++ expect(frame.meta).toMatchObject({ freshness: "live", truncated: false });+ expect(mockSafeFetch).toHaveBeenCalledTimes(2);+ expect(+ new URL(mockSafeFetch.mock.calls[0][0] as string).searchParams.get("limit")+ ).toBe("5001");+ expect(+ new URL(mockSafeFetch.mock.calls[1][0] as string).searchParams.get("limit")+ ).toBe("1000");+ });++ it("marks trace summary buckets sampled only when the 5000-trace cap is exceeded", async () => {+ mockSafeFetch.mockResolvedValueOnce({+ traces: Array.from({ length: 5_001 }, (_, index) => ({+ traceID: index.toString(16).padStart(32, "0"),+ rootServiceName: "checkout",+ rootTraceName: "POST /checkout",+ startTimeUnixNano: "1782864000000000000",+ durationMs: 25,+ })),+ });++ const frame = await adapter.traceTimeSeries!({+ signal: "traces",+ timeRange: window,+ interval: "1h",+ aiSelector: false,+ aggregations: [{ fn: "count", as: "count" }],+ });++ expect(+ (frame.rows as any[]).reduce(+ (sum: number, row: any) => sum + Number(row.count || 0),+ 0+ )+ ).toBe(5_000);+ expect(frame.meta).toMatchObject({+ freshness: "sampled",+ truncated: true,+ rowsScanned: 5_000,+ degraded: ["traceSummaryLimit"],+ });+ await expect(adapter.countTraces!({+ signal: "traces",+ timeRange: window,+ interval: "1h",+ aiSelector: false,+ })).resolves.toEqual({ total: 5_000, truncated: true });+ expect(mockSafeFetch).toHaveBeenCalledTimes(1);+ });++ it("getTraceSpans parses OTLP into normalized spans with events", async () => {+ mockSafeFetch.mockResolvedValue(otlpTrace);+ const spans = await adapter.getTraceSpans(TRACE_1);+ expect(spans).toHaveLength(1);+ expect(spans[0]).toMatchObject({+ traceId: TRACE_1,+ spanId: SPAN_1,+ name: "chat",+ serviceName: "svc",+ statusCode: "STATUS_CODE_OK",+ durationNs: 1000000000,+ });+ expect(spans[0].resourceAttributes["telemetry.sdk.name"]).toBe("openlit");+ expect(spans[0].events?.[0].attributes["gen_ai.prompt"]).toBe("hi");+ });++ it("listSpans uses TraceQL search summaries without downloading full OTLP traces", async () => {+ mockSafeFetch.mockResolvedValueOnce({+ traces: [+ {+ traceID: TRACE_1,+ rootServiceName: "svc-a",+ rootTraceName: "chat",+ startTimeUnixNano: "1719792000000000000",+ durationMs: 12.5,+ },+ {+ traceID: TRACE_2,+ rootServiceName: "svc-b",+ rootTraceName: "embed",+ startTimeUnixNano: "1719792001000000000",+ durationMs: 4,+ },+ ],+ });+ const frame = await adapter.listSpans({+ signal: "traces",+ timeRange: window,+ limit: 5,+ aiSelector: true,+ });+ // One list row per trace summary — no per-trace OTLP fan-out.+ expect(frame.rows).toHaveLength(2);+ expect(frame.rows[0]).toMatchObject({+ traceId: TRACE_1,+ spanId: TRACE_1,+ serviceName: "svc-a",+ name: "chat",+ durationNs: 12_500_000,+ });+ const searchUrl = mockSafeFetch.mock.calls[0][0] as string;+ expect(searchUrl).toContain("/api/search");+ expect(decodeURIComponent(searchUrl)).toContain("telemetry.sdk.name");+ expect(mockSafeFetch.mock.calls[0][1]).toMatchObject({+ headers: expect.objectContaining({+ accept: "application/json",+ authorization: "Bearer tok",+ "x-request-id": expect.stringMatching(/^openlit:tempo:/),+ }),+ allowPrivateNetwork: true,+ });+ expect(frame.meta?.degraded).toContain("serverAggregation");+ expect(mockSafeFetch).toHaveBeenCalledTimes(1);+ });++ it("sampleTracesForGraph skips an oversized Tempo trace instead of failing the whole sample", async () => {+ mockSafeFetch+ .mockResolvedValueOnce({+ traces: [{ traceID: TRACE_1 }, { traceID: TRACE_2 }],+ })+ .mockRejectedValueOnce(+ new SourceResponseError(+ 422,+ "Data source responded 422: trace exceeds max size (max bytes: 5000000)"+ )+ )+ .mockResolvedValueOnce(otlpForTrace(TRACE_2, SPAN_2));++ const spans = await adapter.sampleTracesForGraph(+ {+ signal: "traces",+ timeRange: window,+ aiSelector: false,+ },+ 5+ );++ expect(spans).toHaveLength(1);+ expect(spans[0]).toMatchObject({ traceId: TRACE_2, spanId: SPAN_2 });+ });++ it("getSpan resolves via TraceQL search then a single OTLP download", async () => {+ mockSafeFetch+ .mockResolvedValueOnce({ traces: [{ traceID: TRACE_1 }] })+ .mockResolvedValueOnce(otlpTrace);+ const span = await adapter.getSpan(SPAN_1);+ expect(span?.spanId).toBe(SPAN_1);+ expect(span?.traceId).toBe(TRACE_1);+ expect(mockSafeFetch).toHaveBeenCalledTimes(2);+ });++ it("getSpan returns null when Tempo's matched trace does not contain the requested span", async () => {+ mockSafeFetch+ .mockResolvedValueOnce({ traces: [{ traceID: TRACE_1 }] })+ .mockResolvedValueOnce(otlpTrace);++ await expect(adapter.getSpan(SPAN_2)).resolves.toBeNull();+ expect(mockSafeFetch).toHaveBeenCalledTimes(2);+ });++ it("preserves Tempo's upstream HTTP status through the OpenPlait transport", async () => {+ mockSafeFetch.mockRejectedValueOnce(+ new SourceResponseError(429, "Data source responded 429: rate limited")+ );++ await expect(+ adapter.listSpans({+ signal: "traces",+ timeRange: window,+ limit: 5,+ aiSelector: true,+ })+ ).rejects.toThrow("Tempo returned HTTP 429");+ });++ it("discovers services through OpenPlait's Tempo v2 tag-values path", async () => {+ mockSafeFetch.mockResolvedValueOnce({+ tagValues: [+ { type: "string", value: "checkout" },+ { type: "string", value: "payments" },+ ],+ });++ await expect(adapter.discoverServices(window)).resolves.toEqual([+ {+ serviceName: "checkout",+ environment: "default",+ clusterId: "default",+ },+ {+ serviceName: "payments",+ environment: "default",+ clusterId: "default",+ },+ ]);+ const url = new URL(mockSafeFetch.mock.calls[0][0] as string);+ expect(url.pathname).toBe(+ "/api/v2/search/tag/resource.service.name/values"+ );+ expect(url.searchParams.get("q")).toContain("telemetry.sdk.name");+ expect(url.searchParams.get("start")).toBe(+ String(window.start.getTime() / 1000)+ );+ });+});++describe("buildAggregateDag", () => {+ const span = (over: Partial<NormalizedSpan>): NormalizedSpan => ({+ traceId: "t1",+ spanId: "s",+ parentSpanId: "",+ name: "n",+ serviceName: "svc",+ timestamp: "2026-07-01T00:00:00.000Z",+ durationNs: 1_000_000,+ statusCode: "STATUS_CODE_OK",+ spanAttributes: {},+ resourceAttributes: {},+ ...over,+ });++ it("reconstructs nodes, edges, and quantiles from sampled spans", () => {+ const spans = [+ span({ spanId: "a", name: "agent", durationNs: 2_000_000, cost: 0.01 }),+ span({ spanId: "b", parentSpanId: "a", name: "llm", durationNs: 4_000_000 }),+ span({+ spanId: "c",+ parentSpanId: "a",+ name: "llm",+ durationNs: 6_000_000,+ statusCode: "STATUS_CODE_ERROR",+ }),+ span({ traceId: "t2", spanId: "d", name: "agent", durationNs: 1_000_000 }),+ ];+ const dag = buildAggregateDag(spans);+ expect(dag.sampledTraces).toBe(2);+ expect(dag.sampledSpans).toBe(4);+ const llm = dag.nodes.find((n) => n.name === "llm");+ expect(llm).toMatchObject({ count: 2, errorCount: 1 });+ expect(llm?.p95DurationMs).toBeGreaterThanOrEqual(llm!.p50DurationMs);+ expect(llm?.p50DurationMs).toBe(4);+ const agent = dag.nodes.find((n) => n.name === "agent");+ expect(agent?.totalCost).toBeCloseTo(0.01);+ const edge = dag.edges.find((e) => e.from === "agent" && e.to === "llm");+ expect(edge?.count).toBe(2);+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/jaeger-victoria.test.ts b/src/client/src/__tests__/lib/platform/datasource/jaeger-victoria.test.tsnew file mode 100644index 00000000..99d76265--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/jaeger-victoria.test.ts@@ -0,0 +1,320 @@+const mockSafeFetch = jest.fn();++jest.mock("@/lib/platform/connectors/datasource/http/safe-fetch", () => ({+ safeFetch: (...args: unknown[]) => mockSafeFetch(...args),+ selfHostedNetworkOptions: () => ({+ allowHttp: true,+ allowPrivateNetwork: true,+ }),+}));+jest.mock("@/lib/platform/connectors/datasource/http/secret", () => ({+ resolveSourceSecret: jest.fn().mockResolvedValue({+ raw: "tok",+ credentials: { token: "tok" },+ }),+ redactableSecretValues: () => ["tok"],+}));++import { JaegerAdapter } from "@/lib/platform/connectors/datasource/jaeger/adapter";+import {+ spanMatchesAISelector,+ traceMatchesAISelector,+} from "@/lib/platform/connectors/datasource/selector-match";+import { __clearCache } from "@/lib/platform/connectors/datasource/http/cache";+import type {+ NormalizedSpan,+ TelemetrySourceDescriptor,+} from "@/lib/platform/connectors/datasource/types";++const window = {+ start: new Date("2026-07-01T00:00:00.000Z"),+ end: new Date("2026-07-02T00:00:00.000Z"),+};++const span = (over: Partial<NormalizedSpan>): NormalizedSpan => ({+ traceId: "t",+ spanId: "s",+ parentSpanId: "",+ name: "n",+ serviceName: "svc",+ timestamp: "2026-07-01T00:00:00.000Z",+ durationNs: 1_000_000,+ statusCode: "STATUS_CODE_OK",+ spanAttributes: {},+ resourceAttributes: {},+ ...over,+});++beforeEach(() => {+ jest.clearAllMocks();+ __clearCache();+});++describe("selector-match", () => {+ it("matches openlit SDK identity on resource attributes", () => {+ expect(+ spanMatchesAISelector(+ span({ resourceAttributes: { "telemetry.sdk.name": "openlit" } })+ )+ ).toBe(true);+ });+ it("matches gen_ai span attributes", () => {+ expect(+ spanMatchesAISelector(+ span({ spanAttributes: { "gen_ai.operation.name": "chat" } })+ )+ ).toBe(true);+ });+ it("matches coding-agent span names", () => {+ expect(spanMatchesAISelector(span({ name: "coding_agent.session" }))).toBe(true);+ });+ it("rejects non-AI spans", () => {+ expect(+ spanMatchesAISelector(+ span({ name: "GET /health", spanAttributes: { "http.method": "GET" } })+ )+ ).toBe(false);+ });+ it("keeps a trace when any span is AI-relevant", () => {+ const spans = [+ span({ spanId: "a", name: "GET /x" }),+ span({ spanId: "b", spanAttributes: { "gen_ai.request.model": "gpt-4" } }),+ ];+ expect(traceMatchesAISelector(spans)).toBe(true);+ });+});++describe("JaegerAdapter", () => {+ const descriptor: TelemetrySourceDescriptor = {+ type: "jaeger",+ id: "src-jaeger",+ isBuiltIn: false,+ settings: { url: "https://jaeger.example.com", services: ["svc"] },+ signals: ["traces"],+ name: "Jaeger",+ };+ const adapter = new JaegerAdapter(descriptor);++ const jaegerTrace = {+ data: [+ {+ traceID: "t1",+ processes: {+ p1: {+ serviceName: "svc",+ tags: [{ key: "telemetry.sdk.name", value: "openlit" }],+ },+ },+ spans: [+ {+ traceID: "t1",+ spanID: "s1",+ operationName: "chat",+ references: [],+ startTime: 1782864000000000,+ duration: 12000,+ processID: "p1",+ tags: [+ { key: "gen_ai.request.model", value: "gpt-4" },+ { key: "gen_ai.usage.cost", value: "0.003" },+ ],+ logs: [+ {+ timestamp: 1782864000500000,+ fields: [+ { key: "event", value: "gen_ai.content.prompt" },+ { key: "gen_ai.prompt", value: "hi" },+ ],+ },+ ],+ },+ {+ traceID: "t1",+ spanID: "s2",+ operationName: "GET /health",+ references: [{ refType: "CHILD_OF", spanID: "s1" }],+ startTime: 1782864000100000,+ duration: 500,+ processID: "p1",+ tags: [{ key: "http.method", value: "GET" }],+ },+ ],+ },+ ],+ };++ it("advertises trace-only, span events, no server aggregation", () => {+ expect(adapter.capabilities()).toMatchObject({+ signals: ["traces"],+ traceTree: true,+ spanEvents: true,+ serverAggregation: false,+ });+ });++ it("normalizes native Jaeger spans, maps logs to events, keeps AI traces", async () => {+ mockSafeFetch.mockResolvedValue(jaegerTrace);+ const frame = await adapter.listSpans({+ signal: "traces",+ timeRange: window,+ limit: 100,+ aiSelector: true,+ });+ const url = mockSafeFetch.mock.calls[0][0] as string;+ expect(url).toContain("/api/traces");+ expect(url).toContain(`start=${window.start.getTime() * 1000}`);+ // Explore-style list: one row per trace (root span).+ expect(frame.rows).toHaveLength(1);+ const chat = frame.rows[0]!;+ expect(chat).toMatchObject({+ traceId: "t1",+ spanId: "s1",+ name: "chat",+ serviceName: "svc",+ durationNs: 12_000_000,+ cost: 0.003,+ });+ expect(chat.resourceAttributes["telemetry.sdk.name"]).toBe("openlit");+ expect(chat.events?.[0]).toMatchObject({+ name: "gen_ai.content.prompt",+ });+ expect(chat.events?.[0].attributes["gen_ai.prompt"]).toBe("hi");+ expect(frame.meta?.degraded).toContain("serverAggregation");+ expect(await adapter.getSpan("s1")).toMatchObject({+ spanId: "s1",+ traceId: "t1",+ });+ const tree = await adapter.getTraceSpans("t1");+ expect(tree).toHaveLength(2);+ expect(tree.find((span) => span.name === "GET /health")?.parentSpanId).toBe(+ "s1"+ );+ });++ it("counts unique traces from the Jaeger sample", async () => {+ mockSafeFetch.mockResolvedValue(jaegerTrace);+ await expect(+ adapter.countTraces({+ signal: "traces",+ timeRange: window,+ aiSelector: false,+ })+ ).resolves.toEqual({ total: 1, truncated: false });+ });++ it("budgets list results by traces, not child-span count", async () => {+ const fatChildren = Array.from({ length: 400 }, (_, i) => ({+ traceID: "fat",+ spanID: `c${i}`,+ operationName: `child-${i}`,+ references: [{ refType: "CHILD_OF", spanID: "root" }],+ startTime: 1782864000000000 + i,+ duration: 10,+ processID: "p1",+ tags: [],+ }));+ mockSafeFetch.mockResolvedValue({+ data: [+ {+ traceID: "fat",+ processes: { p1: { serviceName: "svc", tags: [] } },+ spans: [+ {+ traceID: "fat",+ spanID: "root",+ operationName: "session",+ references: [],+ startTime: 1782864000000000,+ duration: 5000,+ processID: "p1",+ tags: [{ key: "gen_ai.request.model", value: "gpt-4" }],+ },+ ...fatChildren,+ ],+ },+ {+ traceID: "thin",+ processes: { p1: { serviceName: "svc", tags: [] } },+ spans: [+ {+ traceID: "thin",+ spanID: "r2",+ operationName: "checkout",+ references: [],+ startTime: 1782864100000000,+ duration: 100,+ processID: "p1",+ tags: [{ key: "gen_ai.request.model", value: "gpt-4" }],+ },+ ],+ },+ ],+ });+ const frame = await adapter.listSpans({+ signal: "traces",+ timeRange: window,+ limit: 25,+ aiSelector: false,+ });+ expect(frame.rows.map((row) => row.traceId).sort()).toEqual(["fat", "thin"]);+ });++ it("loads span names from Jaeger operations API", async () => {+ mockSafeFetch.mockImplementation(async (url: string) => {+ if (String(url).includes("/operations")) {+ return { data: ["checkout", "gen_ai.chat"] };+ }+ return jaegerTrace;+ });+ await expect(+ adapter.distinctValues("SpanName", {+ signal: "traces",+ timeRange: window,+ aiSelector: false,+ })+ ).resolves.toEqual(["checkout", "gen_ai.chat"]);+ });++ it("builds a trace-level time series from search hits", async () => {+ mockSafeFetch.mockResolvedValue(jaegerTrace);+ const frame = await adapter.traceTimeSeries!({+ signal: "traces",+ timeRange: window,+ interval: "1d",+ aiSelector: false,+ aggregations: [{ fn: "count", as: "count" }],+ });+ expect(+ frame.rows.some((row) => Number((row as { count?: unknown }).count) > 0)+ ).toBe(true);+ expect(frame.meta?.freshness).toBe("sampled");+ });++ it("drops traces with no AI-relevant span", async () => {+ mockSafeFetch.mockResolvedValue({+ data: [+ {+ traceID: "t2",+ processes: { p1: { serviceName: "svc", tags: [] } },+ spans: [+ {+ traceID: "t2",+ spanID: "x",+ operationName: "GET /health",+ startTime: 1782864000000000,+ duration: 100,+ processID: "p1",+ tags: [{ key: "http.method", value: "GET" }],+ },+ ],+ },+ ],+ });+ const frame = await adapter.listSpans({+ signal: "traces",+ timeRange: window,+ aiSelector: true,+ });+ expect(frame.rows).toHaveLength(0);+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/l1-query-planner.test.ts b/src/client/src/__tests__/lib/platform/datasource/l1-query-planner.test.tsnew file mode 100644index 00000000..ec221709--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/l1-query-planner.test.ts@@ -0,0 +1,469 @@+import { mapPool } from "@/lib/platform/connectors/datasource/graph/map-pool";+import {+ aggregateSpansInProcess,+ bucketSpansByInterval,+ distinctFromSpans,+ looksLikeRootsOnly,+ spanFieldValue,+} from "@/lib/platform/connectors/datasource/graph/sample-aggregate";+import { fetchSpansForAggregation } from "@/lib/platform/connectors/datasource/graph/sample-fetch";+import {+ computeAggregateSpansL1,+ computeSpanTimeSeriesL1,+} from "@/lib/platform/connectors/datasource/l1-compute";+import {+ intervalFromTimeRange,+ planAndAggregateSpans,+ planAndSpanTimeSeries,+} from "@/lib/platform/connectors/datasource/query-planner";+import { __clearCache } from "@/lib/platform/connectors/datasource/http/cache";+import {+ UnsupportedCapabilityError,+ type DataSourceAdapter,+ type NormalizedSpan,+ type OpenLITQuery,+ type SourceCapabilities,+} from "@/lib/platform/connectors/datasource/types";++function span(partial: Partial<NormalizedSpan> & Pick<NormalizedSpan, "spanId">): NormalizedSpan {+ return {+ traceId: partial.traceId || "t1",+ spanId: partial.spanId,+ parentSpanId: partial.parentSpanId ?? "",+ name: partial.name || "chat",+ serviceName: partial.serviceName ?? "api",+ timestamp: partial.timestamp || "2026-07-11T12:30:00.000Z",+ durationNs: partial.durationNs ?? 1_500_000_000,+ statusCode: partial.statusCode || "STATUS_CODE_OK",+ spanAttributes: partial.spanAttributes || {},+ resourceAttributes: partial.resourceAttributes || {},+ cost: partial.cost,+ };+}++const windowQuery: OpenLITQuery = {+ signal: "traces",+ timeRange: {+ start: new Date("2026-07-11T00:00:00.000Z"),+ end: new Date("2026-07-11T23:59:59.000Z"),+ },+ limit: 50,+};++beforeEach(() => {+ __clearCache();+});++describe("mapPool", () => {+ it("preserves order under concurrency cap", async () => {+ const out = await mapPool([1, 2, 3, 4, 5], 2, async (n) => {+ await new Promise((r) => setTimeout(r, 5 - n));+ return n * 10;+ });+ expect(out).toEqual([10, 20, 30, 40, 50]);+ });+});++describe("spanFieldValue", () => {+ it("converts duration ns to seconds", () => {+ const s = span({ spanId: "a", durationNs: 2_000_000_000 });+ expect(spanFieldValue(s, "duration")).toBe(2);+ });++ it("resolves cost and tokens from attributes", () => {+ const s = span({+ spanId: "a",+ spanAttributes: {+ "gen_ai.usage.cost": "0.42",+ "gen_ai.usage.total_tokens": "120",+ },+ });+ expect(spanFieldValue(s, "cost")).toBe(0.42);+ expect(spanFieldValue(s, "tokens")).toBe(120);+ });++ it("resolves service.name and SpanName", () => {+ const s = span({+ spanId: "a",+ name: "openai.chat",+ serviceName: "",+ resourceAttributes: { "service.name": "worker" },+ });+ expect(spanFieldValue(s, "SpanName")).toBe("openai.chat");+ expect(spanFieldValue(s, "service.name")).toBe("worker");+ });+});++describe("sample-aggregate", () => {+ const spans = [+ span({+ spanId: "1",+ traceId: "t1",+ timestamp: "2026-07-11T10:15:00.000Z",+ durationNs: 1e9,+ spanAttributes: { "gen_ai.usage.cost": "1", "gen_ai.request.model": "gpt-4" },+ }),+ span({+ spanId: "2",+ traceId: "t1",+ timestamp: "2026-07-11T10:45:00.000Z",+ durationNs: 3e9,+ spanAttributes: { "gen_ai.usage.cost": "2", "gen_ai.request.model": "gpt-4" },+ }),+ span({+ spanId: "3",+ traceId: "t2",+ timestamp: "2026-07-11T11:10:00.000Z",+ durationNs: 2e9,+ spanAttributes: { "gen_ai.usage.cost": "3", "gen_ai.request.model": "gpt-3.5" },+ }),+ ];++ it("bucketSpansByInterval groups by hour with request_time ISO", () => {+ const frame = bucketSpansByInterval(spans, "1h", [+ { fn: "count", as: "total" },+ { fn: "sum", field: "cost", as: "cost" },+ ]);+ expect(frame.rows).toHaveLength(2);+ expect(frame.rows[0]).toMatchObject({+ bucket: "2026-07-11T10:00:00.000Z",+ request_time: "2026-07-11T10:00:00.000Z",+ total: 2,+ cost: 3,+ });+ expect(frame.rows[1]).toMatchObject({ total: 1, cost: 3 });+ });++ it("bucketSpansByInterval zero-fills the requested time range", () => {+ const frame = bucketSpansByInterval(+ [+ span({+ spanId: "1",+ timestamp: "2026-07-11T10:15:00.000Z",+ }),+ ],+ "1h",+ [{ fn: "count", as: "total" }],+ {+ start: new Date("2026-07-11T09:00:00.000Z"),+ end: new Date("2026-07-11T11:00:00.000Z"),+ }+ );+ expect(frame.rows).toHaveLength(3);+ expect(frame.rows.map((r) => (r as { total: number }).total)).toEqual([+ 0, 1, 0,+ ]);+ });++ it("bucketSpansByInterval preserves 90-day data in weekly buckets", () => {+ const frame = bucketSpansByInterval(+ [+ span({ spanId: "may", timestamp: "2026-05-10T10:00:00.000Z" }),+ span({ spanId: "jul", timestamp: "2026-07-29T10:00:00.000Z" }),+ ],+ "1w",+ [{ fn: "count", as: "total" }],+ {+ start: new Date("2026-05-08T00:00:00.000Z"),+ end: new Date("2026-08-06T00:00:00.000Z"),+ }+ );++ expect(frame.rows).toHaveLength(14);+ expect(+ frame.rows.reduce<number>(+ (sum, row) => sum + Number((row as { total: number }).total),+ 0+ )+ ).toBe(2);+ expect(frame.rows[0]).toMatchObject({+ bucket: "2026-05-04T00:00:00.000Z",+ label: "2026/05/04",+ });+ });++ it("aggregateSpansInProcess groups and exposes group_value", () => {+ const frame = aggregateSpansInProcess(+ spans,+ ["gen_ai.request.model"],+ [{ fn: "count", as: "n" }, { fn: "p50", field: "duration", as: "p50" }]+ );+ expect(frame.rows).toHaveLength(2);+ const gpt4 = frame.rows.find(+ (r) => (r as { group_value: string }).group_value === "gpt-4"+ ) as { n: number; p50: number };+ expect(gpt4.n).toBe(2);+ expect(gpt4.p50).toBe(1);+ });++ it("aggregateSpansInProcess returns one row when groupBy empty", () => {+ const frame = aggregateSpansInProcess(spans, [], [{ fn: "count", as: "n" }]);+ expect(frame.rows).toEqual([{ n: 3 }]);+ });++ it("distinctFromSpans returns sorted unique values", () => {+ expect(distinctFromSpans(spans, "gen_ai.request.model")).toEqual([+ "gpt-3.5",+ "gpt-4",+ ]);+ });++ it("looksLikeRootsOnly detects one root per trace", () => {+ expect(+ looksLikeRootsOnly([+ span({ spanId: "a", traceId: "t1", parentSpanId: "" }),+ span({ spanId: "b", traceId: "t2", parentSpanId: "0".repeat(16) }),+ ])+ ).toBe(true);+ expect(+ looksLikeRootsOnly([+ span({ spanId: "a", traceId: "t1", parentSpanId: "" }),+ span({ spanId: "b", traceId: "t1", parentSpanId: "a" }),+ ])+ ).toBe(false);+ });+});++describe("fetchSpansForAggregation", () => {+ it("prefers sampleTracesForGraph", async () => {+ const spans = [span({ spanId: "1" })];+ const result = await fetchSpansForAggregation(+ {+ sampleTracesForGraph: async () => spans,+ listSpans: async () => {+ throw new Error("should not list");+ },+ },+ windowQuery,+ { skipCache: true }+ );+ expect(result.spans).toBe(spans);+ });++ it("expands roots-only listSpans via getTraceSpans", async () => {+ const root = span({ spanId: "root", traceId: "t1", parentSpanId: "" });+ const child = span({ spanId: "child", traceId: "t1", parentSpanId: "root" });+ const result = await fetchSpansForAggregation(+ {+ listSpans: async () => ({ fields: [], rows: [root] }),+ getTraceSpans: async () => [root, child],+ },+ windowQuery,+ { skipCache: true }+ );+ expect(result.spans).toHaveLength(2);+ });++ it("stratifies across discovered services", async () => {+ const calls: string[] = [];+ const result = await fetchSpansForAggregation(+ {+ discoverServices: async () => [+ { serviceName: "demo-openai-app", environment: "default", clusterId: "default" },+ { serviceName: "demo-anthropic-app", environment: "default", clusterId: "default" },+ ],+ sampleTracesForGraph: async (query) => {+ const service = String(+ query.filters?.find((f) => f.key === "service.name")?.value || ""+ );+ calls.push(service);+ return [+ span({+ spanId: `${service}-1`,+ traceId: `${service}-t`,+ serviceName: service,+ }),+ ];+ },+ },+ windowQuery,+ { skipCache: true }+ );+ expect(calls.sort()).toEqual(["demo-anthropic-app", "demo-openai-app"]);+ expect(result.spans.map((s) => s.serviceName).sort()).toEqual([+ "demo-anthropic-app",+ "demo-openai-app",+ ]);+ expect(result.truncated).toBe(false);+ });++ it("keeps service stratification within the global trace budget", async () => {+ const requested: number[] = [];+ const result = await fetchSpansForAggregation(+ {+ discoverServices: async () =>+ ["a", "b", "c", "d"].map((serviceName) => ({+ serviceName,+ environment: "default",+ clusterId: "default",+ })),+ sampleTracesForGraph: async (query, limit) => {+ requested.push(limit);+ const service = String(+ query.filters?.find((f) => f.key === "service.name")?.value || ""+ );+ return Array.from({ length: limit }, (_, index) =>+ span({+ spanId: `${service}-${index}`,+ traceId: `${service}-trace-${index}`,+ serviceName: service,+ })+ );+ },+ },+ windowQuery,+ { maxTraces: 3, skipCache: true }+ );++ expect(requested.reduce((sum, value) => sum + value, 0)).toBe(3);+ expect(result.spans).toHaveLength(3);+ });++ it("does not re-stratify adapters that already fan out per service", async () => {+ let calls = 0;+ const spans = [span({ spanId: "1", serviceName: "a" })];+ const result = await fetchSpansForAggregation(+ {+ samplesAreServiceStratified: true,+ discoverServices: async () => [+ { serviceName: "a", environment: "default", clusterId: "default" },+ { serviceName: "b", environment: "default", clusterId: "default" },+ ],+ sampleTracesForGraph: async () => {+ calls += 1;+ return spans;+ },+ },+ windowQuery,+ { skipCache: true }+ );+ expect(calls).toBe(1);+ expect(result.spans).toBe(spans);+ });+});++describe("l1-compute", () => {+ it("sets degraded serverAggregation meta", async () => {+ const spans = [+ span({+ spanId: "1",+ timestamp: "2026-07-11T10:00:00.000Z",+ spanAttributes: { "gen_ai.usage.cost": "1" },+ }),+ ];+ const source = {+ sampleTracesForGraph: async () => spans,+ };+ const agg = await computeAggregateSpansL1(source, {+ ...windowQuery,+ aggregations: [{ fn: "sum", field: "cost", as: "cost" }],+ });+ expect(agg.meta?.degraded).toEqual(["serverAggregation"]);+ expect(agg.meta?.rowsScanned).toBe(1);+ expect(agg.rows[0]).toMatchObject({ cost: 1 });++ const ts = await computeSpanTimeSeriesL1(source, {+ ...windowQuery,+ interval: "1h",+ aggregations: [{ fn: "count", as: "total" }],+ });+ expect(ts.meta?.degraded).toContain("serverAggregation");+ const nonEmpty = ts.rows.find((r) => Number((r as any).total) > 0);+ expect(nonEmpty).toMatchObject({ total: 1 });+ expect(ts.rows.length).toBeGreaterThan(1);+ });+});++describe("query-planner", () => {+ it("intervalFromTimeRange maps dateTrunc units", () => {+ expect(+ intervalFromTimeRange(+ new Date("2026-07-11T00:00:00.000Z"),+ new Date("2026-07-11T12:00:00.000Z")+ )+ ).toBe("1h");+ expect(+ intervalFromTimeRange(+ new Date("2026-07-01T00:00:00.000Z"),+ new Date("2026-07-10T00:00:00.000Z")+ )+ ).toBe("1d");+ });++ it("preferRollup uses readRollup when present", async () => {+ const frame = await planAndAggregateSpans(+ {} as DataSourceAdapter,+ windowQuery,+ {+ preferRollup: true,+ readRollup: async () => ({+ fields: [],+ rows: [{ n: 9 }],+ }),+ }+ );+ expect(frame.rows).toEqual([{ n: 9 }]);+ expect(frame.meta?.degraded).toContain("rollup");+ });++ it("falls back to L1 on UnsupportedCapabilityError", async () => {+ const spans = [span({ spanId: "1", spanAttributes: { "gen_ai.usage.cost": "5" } })];+ const adapter = {+ capabilities: (): SourceCapabilities => ({+ signals: ["traces"],+ traceTree: true,+ spanEvents: true,+ serverAggregation: false,+ spanMutation: false,+ distinctValues: false,+ crossTraceSession: false,+ rawQuery: false,+ }),+ aggregateSpans: async () => {+ throw new UnsupportedCapabilityError("tempo", "aggregateSpans");+ },+ spanTimeSeries: async () => {+ throw new UnsupportedCapabilityError("tempo", "spanTimeSeries");+ },+ sampleTracesForGraph: async () => spans,+ } as unknown as DataSourceAdapter;++ const agg = await planAndAggregateSpans(adapter, {+ ...windowQuery,+ aggregations: [{ fn: "sum", field: "cost", as: "cost" }],+ });+ expect(agg.rows[0]).toMatchObject({ cost: 5 });+ expect(agg.meta?.degraded).toContain("serverAggregation");++ const ts = await planAndSpanTimeSeries(adapter, {+ ...windowQuery,+ interval: "1h",+ aggregations: [{ fn: "count", as: "total" }],+ });+ const nonEmpty = ts.rows.find((r) => Number((r as { total?: number }).total) > 0);+ expect(nonEmpty).toMatchObject({ total: 1 });+ expect(ts.rows.length).toBeGreaterThan(1);+ });++ it("uses native adapter method when serverAggregation is true", async () => {+ const adapter = {+ capabilities: (): SourceCapabilities => ({+ signals: ["traces"],+ traceTree: true,+ spanEvents: true,+ serverAggregation: true,+ spanMutation: false,+ distinctValues: true,+ crossTraceSession: false,+ rawQuery: false,+ }),+ aggregateSpans: async () => ({+ fields: [],+ rows: [{ native: true }],+ }),+ } as unknown as DataSourceAdapter;++ const frame = await planAndAggregateSpans(adapter, windowQuery);+ expect(frame.rows).toEqual([{ native: true }]);+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/limits.test.ts b/src/client/src/__tests__/lib/platform/datasource/limits.test.tsnew file mode 100644index 00000000..5d403032--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/limits.test.ts@@ -0,0 +1,193 @@+import {+ clampQueryBudget,+ clampQueryToSource,+ DEFAULT_QUERY_BUDGET,+ Semaphore,+ withSourceConcurrency,+ withRetry,+ defaultIsRetryable,+ __resetConcurrencyForTests,+} from "@/lib/platform/connectors/datasource/http/limits";+import type { OpenLITQuery } from "@/lib/platform/connectors/datasource/types";+import { SourceResponseError } from "@/lib/platform/connectors/datasource/http/safe-fetch";++beforeEach(() => {+ __resetConcurrencyForTests();+});++const baseQuery = (over: Partial<OpenLITQuery> = {}): OpenLITQuery => ({+ signal: "traces",+ timeRange: { start: new Date("2026-07-01T00:00:00Z"), end: new Date("2026-07-02T00:00:00Z") },+ ...over,+});++describe("clampQueryBudget", () => {+ it("clamps a limit over the budget and reports it", () => {+ const { query, clamped } = clampQueryBudget(baseQuery({ limit: 999999 }));+ expect(query.limit).toBe(DEFAULT_QUERY_BUDGET.maxRows);+ expect(clamped).toContain("limit");+ });++ it("defaults a missing limit to the budget max without flagging", () => {+ const { query, clamped } = clampQueryBudget(baseQuery());+ expect(query.limit).toBe(DEFAULT_QUERY_BUDGET.maxRows);+ expect(clamped).not.toContain("limit");+ });++ it("clamps an over-wide time range by moving start forward", () => {+ const end = new Date("2026-07-02T00:00:00Z");+ const start = new Date(end.getTime() - 90 * 24 * 60 * 60 * 1000);+ const { query, clamped } = clampQueryBudget(baseQuery({ timeRange: { start, end } }));+ expect(clamped).toContain("timeRange");+ expect(query.timeRange.end).toEqual(end);+ expect(end.getTime() - query.timeRange.start.getTime()).toBe(+ DEFAULT_QUERY_BUDGET.maxRangeMs+ );+ });++ it("leaves an in-budget query untouched", () => {+ const { query, clamped } = clampQueryBudget(baseQuery({ limit: 100 }));+ expect(query.limit).toBe(100);+ expect(clamped).toHaveLength(0);+ });++ it("clamps start to the vendor lookback window when tighter than maxRangeMs", () => {+ const end = new Date("2026-07-10T00:00:00Z");+ const start = new Date(end.getTime() - 20 * 24 * 60 * 60 * 1000); // 20d+ const maxLookbackMs = 7 * 24 * 60 * 60 * 1000; // 7d retention+ const { query, clamped } = clampQueryBudget(+ baseQuery({ timeRange: { start, end } }),+ { ...DEFAULT_QUERY_BUDGET, maxLookbackMs }+ );+ expect(query.timeRange.end).toEqual(end);+ expect(end.getTime() - query.timeRange.start.getTime()).toBe(maxLookbackMs);+ // Range was inside maxRangeMs (30d) but outside the 7d lookback.+ expect(clamped).toContain("maxLookback");+ expect(clamped).not.toContain("timeRange");+ });++ it("keeps an in-lookback range untouched", () => {+ const end = new Date("2026-07-10T00:00:00Z");+ const start = new Date(end.getTime() - 3 * 24 * 60 * 60 * 1000); // 3d+ const maxLookbackMs = 7 * 24 * 60 * 60 * 1000;+ const { query, clamped } = clampQueryBudget(+ baseQuery({ timeRange: { start, end } }),+ { ...DEFAULT_QUERY_BUDGET, maxLookbackMs }+ );+ expect(query.timeRange.start).toEqual(start);+ expect(clamped).not.toContain("maxLookback");+ });++ it("uses the selected datasource's maximum query window", () => {+ const end = new Date("2026-07-10T00:00:00Z");+ const start = new Date(end.getTime() - 20 * 24 * 60 * 60 * 1000);+ const maxTimeRangeMs = 3 * 24 * 60 * 60 * 1000;+ const source = {+ capabilities: () => ({ maxTimeRangeMs }),+ };++ const { query, clamped } = clampQueryToSource(+ source,+ baseQuery({ timeRange: { start, end } })+ );++ expect(end.getTime() - query.timeRange.start.getTime()).toBe(+ maxTimeRangeMs+ );+ expect(clamped).toContain("timeRange");+ });+});++describe("Semaphore", () => {+ it("caps concurrent holders and releases in order", async () => {+ const sem = new Semaphore(2);+ const r1 = await sem.acquire();+ const r2 = await sem.acquire();+ let thirdAcquired = false;+ const p3 = sem.acquire().then((rel) => {+ thirdAcquired = true;+ return rel;+ });+ await Promise.resolve();+ expect(thirdAcquired).toBe(false);+ r1();+ const r3 = await p3;+ expect(thirdAcquired).toBe(true);+ r2();+ r3();+ });+});++describe("withSourceConcurrency", () => {+ it("never exceeds the cap for a key", async () => {+ let active = 0;+ let maxActive = 0;+ const task = () =>+ withSourceConcurrency("src-1", 2, async () => {+ active++;+ maxActive = Math.max(maxActive, active);+ await new Promise((r) => setTimeout(r, 5));+ active--;+ });+ await Promise.all([task(), task(), task(), task(), task()]);+ expect(maxActive).toBeLessThanOrEqual(2);+ });+});++describe("defaultIsRetryable", () => {+ it("retries 429 and 5xx SourceResponseError", () => {+ expect(defaultIsRetryable(new SourceResponseError(429, "rate"))).toBe(true);+ expect(defaultIsRetryable(new SourceResponseError(503, "down"))).toBe(true);+ expect(defaultIsRetryable(new SourceResponseError(400, "bad"))).toBe(false);+ });++ it("retries network/timeout errors", () => {+ expect(defaultIsRetryable(new Error("Data source request timed out"))).toBe(true);+ expect(defaultIsRetryable(new Error("fetch failed"))).toBe(true);+ expect(defaultIsRetryable(new Error("bad request"))).toBe(false);+ });+});++describe("withRetry", () => {+ it("retries transient failures then succeeds", async () => {+ let calls = 0;+ const result = await withRetry(+ async () => {+ calls++;+ if (calls < 3) throw new SourceResponseError(503, "down");+ return "ok";+ },+ { retries: 3, baseDelayMs: 1, sleep: async () => {} }+ );+ expect(result).toBe("ok");+ expect(calls).toBe(3);+ });++ it("does not retry non-retryable errors", async () => {+ let calls = 0;+ await expect(+ withRetry(+ async () => {+ calls++;+ throw new SourceResponseError(400, "bad");+ },+ { retries: 3, sleep: async () => {} }+ )+ ).rejects.toBeInstanceOf(SourceResponseError);+ expect(calls).toBe(1);+ });++ it("gives up after exhausting retries", async () => {+ let calls = 0;+ await expect(+ withRetry(+ async () => {+ calls++;+ throw new SourceResponseError(500, "down");+ },+ { retries: 2, baseDelayMs: 1, sleep: async () => {} }+ )+ ).rejects.toBeInstanceOf(SourceResponseError);+ expect(calls).toBe(3);+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/loki-prometheus.test.ts b/src/client/src/__tests__/lib/platform/datasource/loki-prometheus.test.tsnew file mode 100644index 00000000..184f1b90--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/loki-prometheus.test.ts@@ -0,0 +1,420 @@+const mockSafeFetch = jest.fn();++jest.mock("@/lib/platform/connectors/datasource/http/safe-fetch", () => ({+ safeFetch: (...args: unknown[]) => mockSafeFetch(...args),+ SourceResponseError: class SourceResponseError extends Error {+ status: number;+ constructor(status: number, message: string) {+ super(message);+ this.status = status;+ }+ },+ selfHostedNetworkOptions: () => ({ allowHttp: true, allowPrivateNetwork: true }),+}));+jest.mock("@/lib/platform/connectors/datasource/http/secret", () => ({+ resolveSourceSecret: jest.fn().mockResolvedValue({ raw: "", credentials: {} }),+ redactableSecretValues: () => [],+}));+jest.mock("@/utils/log", () => ({ consoleLog: jest.fn() }));++import {+ LokiAdapter,+ __resetLokiLearningForTests,+ parseLokiDurationMs,+ reportedLokiMaxQueryRangeMs,+} from "@/lib/platform/connectors/datasource/grafana/loki";+import { PrometheusAdapter, prometheusSelector } from "@/lib/platform/connectors/datasource/prometheus/adapter";+import type { TelemetrySourceDescriptor } from "@/lib/platform/connectors/datasource/types";+import { facadeErrorMessage } from "@/lib/platform/connectors/datasource/facade";+import { AdapterError } from "@openplait/adapter-sdk";++const window = {+ start: new Date("2026-08-05T00:00:00Z"),+ end: new Date("2026-08-05T01:00:00Z"),+};++function descriptor(+ type: "loki" | "prometheus",+ url: string,+ settings: Record<string, unknown> = {}+): TelemetrySourceDescriptor {+ return {+ type,+ id: `source-${type}`,+ isBuiltIn: false,+ settings: { url, allowHttp: true, allowPrivateNetwork: true, ...settings },+ signals: [type === "loki" ? "logs" : "metrics"],+ name: type,+ };+}++beforeEach(() => {+ mockSafeFetch.mockReset();+ __resetLokiLearningForTests();+});++describe("OpenPlait Loki integration", () => {+ it("uses the selected Loki endpoint and normalizes log streams", async () => {+ mockSafeFetch.mockResolvedValue({+ status: "success",+ data: {+ resultType: "streams",+ result: [+ {+ stream: { service_name: "checkout", trace_id: "trace-1" },+ values: [["1785888000000000000", "failed"]],+ },+ ],+ },+ });+ const adapter = new LokiAdapter(descriptor("loki", "http://loki:3100"));+ const frame = await adapter.listLogs({+ signal: "logs",+ timeRange: window,+ filters: [+ { target: "attribute", key: "service.name", op: "eq", value: "checkout" },+ ],+ limit: 25,+ });+ const url = new URL(mockSafeFetch.mock.calls[0][0]);+ expect(url.origin).toBe("http://loki:3100");+ expect(url.pathname).toBe("/loki/api/v1/query_range");+ expect(url.searchParams.get("query")).toBe('{service_name="checkout"}');+ expect(frame.rows[0]).toMatchObject({+ body: "failed",+ traceId: "trace-1",+ serviceName: "checkout",+ });+ });++ it("clamps oversized summary ranges before querying Loki", async () => {+ mockSafeFetch.mockResolvedValue({+ status: "success",+ data: { resultType: "matrix", result: [] },+ });+ const adapter = new LokiAdapter(descriptor("loki", "http://loki:3100"));+ await adapter.logTimeSeries({+ signal: "logs",+ timeRange: {+ start: new Date("2024-01-01T00:00:00Z"),+ end: new Date("2026-08-07T00:00:00Z"),+ },+ });+ const url = new URL(mockSafeFetch.mock.calls[0][0]);+ const startNs = BigInt(url.searchParams.get("start") || "0");+ const endNs = BigInt(url.searchParams.get("end") || "0");+ const rangeMs = Number((endNs - startNs) / BigInt(1_000_000));+ expect(rangeMs).toBeLessThanOrEqual(30 * 24 * 60 * 60 * 1_000);+ expect(url.searchParams.get("query")).toContain("count_over_time");+ expect(url.searchParams.get("query")).not.toContain("[auto]");+ expect(Number(url.searchParams.get("step"))).toBeGreaterThan(0);+ });++ it("learns Loki max query length from 400 bodies and retries", async () => {+ const { SourceResponseError } = jest.requireMock(+ "@/lib/platform/connectors/datasource/http/safe-fetch"+ ) as {+ SourceResponseError: new (status: number, message: string) => Error & {+ status: number;+ };+ };+ mockSafeFetch+ .mockRejectedValueOnce(+ new SourceResponseError(+ 400,+ "Data source responded 400: the query time range exceeds the limit (query length: 40d, limit: 7d)"+ )+ )+ .mockResolvedValueOnce({+ status: "success",+ data: { resultType: "matrix", result: [] },+ });++ const adapter = new LokiAdapter(+ descriptor("loki", "http://loki:3100", {+ // Force first attempt above the learned 7d ceiling.+ maxTimeRangeMs: 40 * 24 * 60 * 60 * 1_000,+ })+ );+ await adapter.logTimeSeries({+ signal: "logs",+ timeRange: {+ start: new Date("2026-06-01T00:00:00Z"),+ end: new Date("2026-08-07T00:00:00Z"),+ },+ });+ expect(mockSafeFetch).toHaveBeenCalledTimes(2);+ const retryUrl = new URL(mockSafeFetch.mock.calls[1][0]);+ const startNs = BigInt(retryUrl.searchParams.get("start") || "0");+ const endNs = BigInt(retryUrl.searchParams.get("end") || "0");+ const rangeMs = Number((endNs - startNs) / BigInt(1_000_000));+ expect(rangeMs).toBeLessThanOrEqual(7 * 24 * 60 * 60 * 1_000);+ });+});++describe("Loki duration parsing", () => {+ it("parses Loki limit durations and error bodies", () => {+ expect(parseLokiDurationMs("30d1h")).toBe(30 * 24 * 60 * 60 * 1_000 + 60 * 60 * 1_000);+ expect(parseLokiDurationMs("7d")).toBe(7 * 24 * 60 * 60 * 1_000);+ expect(+ reportedLokiMaxQueryRangeMs(+ "the query time range exceeds the limit (query length: 22788h3m24s, limit: 30d1h)"+ )+ ).toBe(30 * 24 * 60 * 60 * 1_000 + 60 * 60 * 1_000);+ });+});++describe("facadeErrorMessage", () => {+ it("includes Loki upstream bodies for generic HTTP failures", () => {+ const error = new AdapterError("EXECUTION_FAILED", "Loki returned HTTP 400.", {+ details: {+ status: 400,+ body: "the query time range exceeds the limit (query length: 40d, limit: 30d1h)",+ },+ });+ expect(facadeErrorMessage(error)).toContain("limit: 30d1h");+ });+});++describe("rethrowIfSourceFailure", () => {+ it("rethrows AdapterError", () => {+ const { rethrowIfSourceFailure } = require("@/lib/platform/connectors/datasource/facade");+ const error = new AdapterError("EXECUTION_FAILED", "down");+ expect(() => rethrowIfSourceFailure(error)).toThrow(AdapterError);+ });++ it("rethrows connection-like errors", () => {+ const { rethrowIfSourceFailure } = require("@/lib/platform/connectors/datasource/facade");+ expect(() =>+ rethrowIfSourceFailure(new Error("fetch failed"))+ ).toThrow("fetch failed");+ });++ it("does not rethrow unrelated errors", () => {+ const { rethrowIfSourceFailure } = require("@/lib/platform/connectors/datasource/facade");+ expect(() =>+ rethrowIfSourceFailure(new Error("span not found"))+ ).not.toThrow();+ });+});++describe("Loki discovery", () => {+ it("discovers Loki filter labels with start/end nanoseconds", async () => {+ mockSafeFetch.mockResolvedValue({+ status: "success",+ data: ["openlit-demo", "openplait-integration"],+ });+ const adapter = new LokiAdapter(descriptor("loki", "http://loki:3100"));+ const values = await adapter.distinctValues("service.name", {+ signal: "logs",+ timeRange: window,+ });+ const url = new URL(mockSafeFetch.mock.calls[0][0]);+ expect(url.pathname).toBe("/loki/api/v1/label/service_name/values");+ expect(url.searchParams.get("start")).toBe(`${window.start.getTime()}000000`);+ expect(url.searchParams.get("end")).toBe(`${window.end.getTime()}000000`);+ expect(values).toEqual(["openlit-demo", "openplait-integration"]);+ });++ it("discovers Loki attribute keys for custom filters", async () => {+ mockSafeFetch.mockResolvedValue({+ status: "success",+ data: ["service_name", "level", "job"],+ });+ const adapter = new LokiAdapter(descriptor("loki", "http://loki:3100"));+ const keys = await adapter.attributeKeys("logs", window);+ expect(keys).toEqual(["service_name", "level", "job"]);+ const url = new URL(mockSafeFetch.mock.calls[0][0]);+ expect(url.pathname).toBe("/loki/api/v1/labels");+ expect(url.searchParams.get("start")).toBeTruthy();+ });++ it("fails loud when Loki label discovery returns an upstream error", async () => {+ const { SourceResponseError } = jest.requireMock(+ "@/lib/platform/connectors/datasource/http/safe-fetch"+ ) as { SourceResponseError: new (status: number, message: string) => Error };+ mockSafeFetch.mockRejectedValue(new SourceResponseError(503, "loki down"));+ const adapter = new LokiAdapter(descriptor("loki", "http://loki:3100"));+ await expect(adapter.attributeKeys("logs", window)).rejects.toMatchObject({+ status: 503,+ });+ });++ it("getLog returns a log from the warm cache after listLogs", async () => {+ mockSafeFetch.mockResolvedValue({+ status: "success",+ data: {+ resultType: "streams",+ result: [+ {+ stream: {+ service_name: "checkout",+ level: "error",+ trace_id: "trace-1",+ },+ values: [["1785888000000000000", "payment failed"]],+ },+ ],+ },+ });+ const adapter = new LokiAdapter(descriptor("loki", "http://loki:3100"));+ const frame = await adapter.listLogs({+ signal: "logs",+ timeRange: window,+ limit: 25,+ });+ const { logStableRowId } = await import(+ "@/lib/platform/connectors/datasource/clickhouse/normalize"+ );+ const rowId = logStableRowId(frame.rows[0]);+ mockSafeFetch.mockClear();+ const detail = await adapter.getLog(rowId);+ expect(detail).toMatchObject({+ body: "payment failed",+ serviceName: "checkout",+ severityText: "error",+ traceId: "trace-1",+ });+ expect(mockSafeFetch).not.toHaveBeenCalled();+ });++ it("getLog falls back to a recent Loki scan when the index misses", async () => {+ mockSafeFetch.mockResolvedValue({+ status: "success",+ data: {+ resultType: "streams",+ result: [+ {+ stream: { service_name: "checkout", level: "info" },+ values: [["1785888000000000000", "hello from fallback"]],+ },+ ],+ },+ });+ const adapter = new LokiAdapter(descriptor("loki", "http://loki:3100"));+ const { logStableRowId } = await import(+ "@/lib/platform/connectors/datasource/clickhouse/normalize"+ );+ const expectedId = logStableRowId({+ timestamp: new Date(1785888000000).toISOString(),+ traceId: "",+ spanId: "",+ severityText: "info",+ body: "hello from fallback",+ });+ const detail = await adapter.getLog(expectedId);+ expect(detail).toMatchObject({+ body: "hello from fallback",+ serviceName: "checkout",+ });+ expect(mockSafeFetch).toHaveBeenCalled();+ });+});++describe("OpenPlait Prometheus integration", () => {+ it("builds regex matchers for multiple metric names", () => {+ expect(+ prometheusSelector({+ signal: "metrics",+ timeRange: window,+ filters: [+ {+ target: "spanName",+ op: "in",+ value: ["up", "go_goroutines"],+ },+ {+ target: "attribute",+ key: "service.name",+ op: "in",+ value: ["prometheus-local", "api"],+ },+ ],+ })+ ).toBe(+ '{__name__=~"up|go_goroutines",service_name=~"prometheus-local|api"}'+ );+ });++ it("escapes regex metacharacters in multi-value PromQL matchers", () => {+ expect(+ prometheusSelector({+ signal: "metrics",+ timeRange: window,+ filters: [+ {+ target: "spanName",+ op: "in",+ value: ["http.server.duration", "rpc.client.duration"],+ },+ ],+ })+ ).toBe(+ // escapeRegex → \. then escape() doubles backslashes for PromQL quotes+ '{__name__=~"http\\\\.server\\\\.duration|rpc\\\\.client\\\\.duration"}'+ );+ });++ it("uses the selected Prometheus endpoint and normalizes metric samples", async () => {+ mockSafeFetch.mockResolvedValue({+ status: "success",+ data: {+ resultType: "matrix",+ result: [+ {+ metric: { __name__: "up", service_name: "checkout" },+ values: [[1785888000, "1"]],+ },+ ],+ },+ });+ const adapter = new PrometheusAdapter(descriptor("prometheus", "http://prometheus:9090"));+ const frame = await adapter.metricTimeSeries({+ signal: "metrics",+ timeRange: window,+ filters: [{ target: "spanName", op: "eq", value: "up" }],+ });+ const url = new URL(mockSafeFetch.mock.calls[0][0]);+ expect(url.origin).toBe("http://prometheus:9090");+ expect(url.pathname).toBe("/api/v1/query_range");+ expect(url.searchParams.get("query")).toBe('{__name__="up"}');+ expect(frame.rows[0]).toMatchObject({+ metricName: "up",+ value: 1,+ serviceName: "checkout",+ });+ });++ it("discovers Prometheus filter labels within the selected time window", async () => {+ mockSafeFetch.mockResolvedValue({+ status: "success",+ data: ["prometheus-local", "checkout"],+ });+ const adapter = new PrometheusAdapter(descriptor("prometheus", "http://prometheus:9090"));+ const values = await adapter.distinctValues("service.name", {+ signal: "metrics",+ timeRange: window,+ });+ const url = new URL(mockSafeFetch.mock.calls[0][0]);+ expect(url.pathname).toBe("/api/v1/label/service_name/values");+ expect(url.searchParams.get("start")).toBe(String(Math.floor(window.start.getTime() / 1000)));+ expect(url.searchParams.get("end")).toBe(String(Math.ceil(window.end.getTime() / 1000)));+ expect(values).toEqual(["prometheus-local", "checkout"]);+ });++ it("fails loud when Prometheus label discovery returns an upstream error", async () => {+ const { SourceResponseError } = jest.requireMock(+ "@/lib/platform/connectors/datasource/http/safe-fetch"+ ) as { SourceResponseError: new (status: number, message: string) => Error };+ mockSafeFetch.mockRejectedValue(new SourceResponseError(502, "prom down"));+ const adapter = new PrometheusAdapter(+ descriptor("prometheus", "http://prometheus:9090")+ );+ await expect(+ adapter.distinctValues("service.name", {+ signal: "metrics",+ timeRange: window,+ })+ ).rejects.toMatchObject({ status: 502 });+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/otlp-json.test.ts b/src/client/src/__tests__/lib/platform/datasource/otlp-json.test.tsnew file mode 100644index 00000000..cd7ce0a4--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/otlp-json.test.ts@@ -0,0 +1,44 @@+import {+ normalizeOtlpId,+ parseOtlpTrace,+} from "@/lib/platform/connectors/datasource/otlp-json";++describe("normalizeOtlpId", () => {+ it("lowercases hex ids", () => {+ expect(normalizeOtlpId("E0B1E39EFD34CE35")).toBe("e0b1e39efd34ce35");+ });++ it("decodes Tempo base64 OTLP ids to hex", () => {+ expect(normalizeOtlpId("4LHjnv00zjU=")).toBe("e0b1e39efd34ce35");+ });+});++describe("parseOtlpTrace", () => {+ it("normalizes base64 span and trace ids to hex", () => {+ const spans = parseOtlpTrace({+ batches: [+ {+ resource: { attributes: [] },+ scopeSpans: [+ {+ spans: [+ {+ traceId: Buffer.from("0123456789abcdef0123456789abcdef", "hex").toString(+ "base64"+ ),+ spanId: "4LHjnv00zjU=",+ parentSpanId: "",+ name: "chat",+ startTimeUnixNano: "1",+ endTimeUnixNano: "2",+ },+ ],+ },+ ],+ },+ ],+ });+ expect(spans[0].spanId).toBe("e0b1e39efd34ce35");+ expect(spans[0].traceId).toBe("0123456789abcdef0123456789abcdef");+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/query-map.test.ts b/src/client/src/__tests__/lib/platform/datasource/query-map.test.tsnew file mode 100644index 00000000..02b1a834--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/query-map.test.ts@@ -0,0 +1,389 @@+import {+ metricParamsToOpenLITQuery,+ toMetricParams,+} from "@/lib/platform/connectors/datasource/clickhouse/query-map";+import {+ denormalizeLogToClickHouseRow,+ denormalizeMetricPointsToListRows,+ denormalizeSpanToTraceRow,+} from "@/lib/platform/connectors/datasource/clickhouse/normalize";+import type {+ NormalizedLog,+ NormalizedMetricPoint,+ NormalizedSpan,+} from "@/lib/platform/connectors/datasource/types";++describe("metricParamsToOpenLITQuery", () => {+ it("maps time, pagination, status, and selectedConfig filters", () => {+ const start = new Date("2026-07-01T00:00:00.000Z");+ const end = new Date("2026-07-01T01:00:00.000Z");+ const query = metricParamsToOpenLITQuery({+ timeLimit: { start, end, type: "CUSTOM" },+ limit: 25,+ offset: 50,+ statusCode: ["STATUS_CODE_ERROR"],+ sorting: { type: "Timestamp", direction: "desc" },+ selectedConfig: {+ models: ["gpt-4o"],+ providers: ["openai"],+ spanNames: ["chat"],+ serviceNames: ["api"],+ environments: ["production"],+ versionFilter: {+ versionHash: "v1",+ firstSeen: "2026-07-01T00:00:00.000Z",+ lastSeen: "2026-07-01T01:00:00.000Z",+ },+ customFilters: [+ { key: "gen_ai.operation.name", value: "chat", operator: "eq" },+ ],+ },+ });++ expect(query.signal).toBe("traces");+ expect(query.aiSelector).toBe(true);+ expect(query.limit).toBe(25);+ expect(query.offset).toBe(50);+ expect(query.timeRange).toEqual({ start, end });+ expect(query.sort).toEqual([{ field: "Timestamp", direction: "desc" }]);+ expect(query.filters).toEqual(+ expect.arrayContaining([+ expect.objectContaining({ target: "status", op: "in" }),+ expect.objectContaining({+ key: "gen_ai.request.model",+ op: "in",+ value: ["gpt-4o"],+ }),+ expect.objectContaining({+ key: "gen_ai.system",+ value: ["openai"],+ }),+ expect.objectContaining({ target: "spanName", value: ["chat"] }),+ expect.objectContaining({+ key: "service.name",+ scope: "resource",+ }),+ expect.objectContaining({+ key: "deployment.environment",+ scope: "resource",+ value: ["production"],+ }),+ expect.objectContaining({+ key: "openlit.agent.version_hash",+ value: "v1",+ }),+ expect.objectContaining({+ key: "gen_ai.operation.name",+ value: "chat",+ }),+ ])+ );+ });++ it("maps operationType llm/vectordb onto gen_ai.operation.name", () => {+ const start = new Date("2026-07-01T00:00:00.000Z");+ const end = new Date("2026-07-01T01:00:00.000Z");+ const llm = metricParamsToOpenLITQuery({+ timeLimit: { start, end, type: "CUSTOM" },+ operationType: "llm",+ } as any);+ expect(llm.filters).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ key: "gen_ai.operation.name",+ op: "neq",+ value: "vectordb",+ }),+ ])+ );+ const vectordb = metricParamsToOpenLITQuery({+ timeLimit: { start, end, type: "CUSTOM" },+ operationType: "vectordb",+ } as any);+ expect(vectordb.filters).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ key: "gen_ai.operation.name",+ op: "eq",+ value: "vectordb",+ }),+ ])+ );+ });++ it("maps the remaining trace selectedConfig filters", () => {+ const query = metricParamsToOpenLITQuery({+ timeLimit: { start: new Date("2026-07-01"), end: new Date("2026-07-02"), type: "CUSTOM" },+ selectedConfig: {+ traceTypes: ["chat"],+ applicationNames: ["my-app"],+ maxCost: 0.25,+ customFilters: [+ { attributeType: "ResourceAttributes", key: "cluster", value: "prod" },+ { attributeType: "SpanAttributes", key: "tenant.id", value: "tenant-1" },+ ],+ },+ } as any);++ expect(query.filters).toEqual(+ expect.arrayContaining([+ expect.objectContaining({ key: "gen_ai.operation.name", value: ["chat"] }),+ expect.objectContaining({ scope: "resource", key: "service.name", value: ["my-app"] }),+ expect.objectContaining({ key: "gen_ai.usage.cost", op: "lte", value: 0.25 }),+ expect.objectContaining({ scope: "resource", key: "cluster" }),+ expect.objectContaining({ scope: "span", key: "tenant.id" }),+ ])+ );+ });++ it("maps log-signal filters (services, severities, custom log attrs)", () => {+ const start = new Date("2026-07-01T00:00:00.000Z");+ const end = new Date("2026-07-01T01:00:00.000Z");+ const query = metricParamsToOpenLITQuery(+ {+ timeLimit: { start, end, type: "CUSTOM" },+ selectedConfig: {+ services: ["api"],+ severities: ["ERROR"],+ customFilters: [+ { key: "user.id", value: "u1", attributeType: "LogAttributes" },+ ],+ },+ },+ "logs"+ );+ expect(query.signal).toBe("logs");+ expect(query.aiSelector).toBe(false);+ expect(query.filters).toEqual(+ expect.arrayContaining([+ expect.objectContaining({ scope: "resource", key: "service.name" }),+ expect.objectContaining({ scope: "log", key: "severity", value: ["ERROR"] }),+ expect.objectContaining({ scope: "log", key: "user.id", value: "u1" }),+ ])+ );+ });++ it("maps metric-signal filters (metricNames -> spanName target)", () => {+ const start = new Date("2026-07-01T00:00:00.000Z");+ const end = new Date("2026-07-01T01:00:00.000Z");+ const query = metricParamsToOpenLITQuery(+ {+ timeLimit: { start, end, type: "CUSTOM" },+ selectedConfig: {+ metricNames: ["gen_ai.client.token.usage"],+ services: ["api"],+ },+ },+ "metrics"+ );+ expect(query.signal).toBe("metrics");+ expect(query.filters).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ target: "spanName",+ value: ["gen_ai.client.token.usage"],+ }),+ expect.objectContaining({ scope: "resource", key: "service.name" }),+ ])+ );+ });++ it("scopes logs and metrics by agent serviceNames lock", () => {+ const start = new Date("2026-07-01T00:00:00.000Z");+ const end = new Date("2026-07-01T01:00:00.000Z");+ const selectedConfig = {+ serviceNames: ["demo-openai-app"],+ environments: ["production"],+ };++ const logs = metricParamsToOpenLITQuery(+ { timeLimit: { start, end, type: "CUSTOM" }, selectedConfig },+ "logs"+ );+ expect(logs.filters).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ key: "service.name",+ value: ["demo-openai-app"],+ }),+ expect.objectContaining({+ key: "deployment.environment",+ value: ["production"],+ }),+ ])+ );++ const metrics = metricParamsToOpenLITQuery(+ { timeLimit: { start, end, type: "CUSTOM" }, selectedConfig },+ "metrics"+ );+ expect(metrics.filters).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ key: "service.name",+ value: ["demo-openai-app"],+ }),+ expect.objectContaining({+ key: "deployment.environment",+ value: ["production"],+ }),+ ])+ );+ });++ it("does not emit a synthetic default deployment.environment filter", () => {+ const start = new Date("2026-07-01T00:00:00.000Z");+ const end = new Date("2026-07-01T01:00:00.000Z");+ const selectedConfig = {+ serviceNames: ["demo-openai-app"],+ environments: ["default"],+ };++ for (const signal of ["traces", "logs", "metrics"] as const) {+ const query = metricParamsToOpenLITQuery(+ { timeLimit: { start, end, type: "CUSTOM" }, selectedConfig },+ signal+ );+ expect(query.filters || []).not.toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ key: "deployment.environment",+ value: ["default"],+ }),+ ])+ );+ expect(query.filters).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ key: "service.name",+ value: ["demo-openai-app"],+ }),+ ])+ );+ }+ });++ it("round-trips time/limit through toMetricParams", () => {+ const start = new Date("2026-07-01T00:00:00.000Z");+ const end = new Date("2026-07-01T01:00:00.000Z");+ const query = metricParamsToOpenLITQuery({+ timeLimit: { start, end, type: "CUSTOM" },+ limit: 10,+ statusCode: ["Error"],+ });+ const back = toMetricParams(query);+ expect(back.limit).toBe(10);+ expect(back.statusCode).toEqual(["Error"]);+ expect(back.timeLimit.type).toBe("CUSTOM");+ expect(back.timeLimit.start).toBe("2026-07-01T00:00:00.000Z");+ expect(back.timeLimit.end).toBe("2026-07-01T01:00:00.000Z");+ });+});++describe("denormalizeSpanToTraceRow", () => {+ it("produces ClickHouse-shaped columns for the Telemetry UI", () => {+ const span: NormalizedSpan = {+ traceId: "t1",+ spanId: "s1",+ parentSpanId: "p1",+ name: "chat",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:00.000Z",+ durationNs: 2_000_000,+ statusCode: "STATUS_CODE_OK",+ spanAttributes: { "gen_ai.request.model": "gpt-4o" },+ resourceAttributes: { "service.name": "api" },+ events: [+ {+ name: "gen_ai.content.prompt",+ timestamp: "2026-07-01T00:00:00.000Z",+ attributes: { "gen_ai.prompt": "hi" },+ },+ ],+ cost: 0.01,+ };+ expect(denormalizeSpanToTraceRow(span)).toMatchObject({+ TraceId: "t1",+ SpanId: "s1",+ ParentSpanId: "p1",+ SpanName: "chat",+ ServiceName: "api",+ Duration: 2_000_000,+ Cost: 0.01,+ SpanAttributes: expect.objectContaining({+ "gen_ai.request.model": "gpt-4o",+ "gen_ai.usage.cost": "0.01",+ }),+ Events: [+ expect.objectContaining({+ Name: "gen_ai.content.prompt",+ Attributes: { "gen_ai.prompt": "hi" },+ }),+ ],+ });+ });+});++describe("denormalizeLogToClickHouseRow", () => {+ it("produces ClickHouse-shaped log columns with a stable rowId", () => {+ const log: NormalizedLog = {+ timestamp: "2026-07-01T00:00:00.000Z",+ traceId: "t1",+ spanId: "s1",+ severityText: "ERROR",+ severityNumber: 17,+ body: "boom",+ serviceName: "api",+ logAttributes: { "user.id": "u1" },+ resourceAttributes: { "service.name": "api" },+ };+ const row = denormalizeLogToClickHouseRow(log);+ expect(row).toMatchObject({+ Timestamp: "2026-07-01T00:00:00.000Z",+ TraceId: "t1",+ SpanId: "s1",+ SeverityText: "ERROR",+ SeverityNumber: 17,+ Body: "boom",+ ServiceName: "api",+ LogAttributes: { "user.id": "u1" },+ });+ expect(typeof row.rowId).toBe("string");+ // Deterministic: same input -> same rowId.+ expect(denormalizeLogToClickHouseRow(log).rowId).toBe(row.rowId);+ });+});++describe("denormalizeMetricPointsToListRows", () => {+ it("folds points into grouped list rows per metric + service", () => {+ const points: NormalizedMetricPoint[] = [+ {+ metricName: "m",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:00.000Z",+ value: 10,+ attributes: {},+ resourceAttributes: {},+ },+ {+ metricName: "m",+ serviceName: "api",+ timestamp: "2026-07-01T00:01:00.000Z",+ value: 20,+ attributes: {},+ resourceAttributes: {},+ },+ ];+ const rows = denormalizeMetricPointsToListRows(points);+ expect(rows).toHaveLength(1);+ expect(rows[0]).toMatchObject({+ metricName: "m",+ serviceName: "api",+ latestValue: 20,+ avgValue: 15,+ minValue: 10,+ maxValue: 20,+ pointCount: 2,+ });+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/registry.test.ts b/src/client/src/__tests__/lib/platform/datasource/registry.test.tsnew file mode 100644index 00000000..7803dbab--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/registry.test.ts@@ -0,0 +1,166 @@+import {+ __resetRegistryForTests,+ createAdapter,+ getAdapterFactory,+ getSourceTypeDescriptor,+ hasAdapterFactory,+ listAdapterTypes,+ listSourceTypeDescriptors,+ registerAdapterFactory,+} from "@/lib/platform/connectors/datasource/registry";+import { getExternalDataSourceAdapters } from "@/lib/platform/connectors/datasource/enterprise";+import type {+ DataSourceAdapter,+ DataSourceAdapterFactory,+ SourceTypeDescriptor,+ TelemetrySourceDescriptor,+} from "@/lib/platform/connectors/datasource/types";++const fakeAdapter = { type: "clickhouse" } as unknown as DataSourceAdapter;++const fakeTypeDescriptor: SourceTypeDescriptor = {+ type: "clickhouse",+ displayName: "ClickHouse",+ declaredSignals: ["traces", "logs", "metrics"],+ capabilities: {+ traceTree: true,+ spanEvents: true,+ serverAggregation: true,+ spanMutation: true,+ distinctValues: true,+ crossTraceSession: true,+ rawQuery: true,+ },+ correlation: { crossSignal: true, keys: ["traceId", "spanId", "service", "session"] },+ configFields: [],+};++const internalTypeDescriptor: SourceTypeDescriptor = {+ ...fakeTypeDescriptor,+ type: "grafana",+ displayName: "Grafana stack",+ internal: true,+};++const fakeFactory: DataSourceAdapterFactory = {+ type: "clickhouse",+ create: () => fakeAdapter,+ describe: () => fakeTypeDescriptor,+};++const internalFactory: DataSourceAdapterFactory = {+ type: "grafana",+ create: () => fakeAdapter,+ describe: () => internalTypeDescriptor,+};++const descriptor: TelemetrySourceDescriptor = {+ type: "clickhouse",+ id: "builtin:db-1",+ isBuiltIn: true,+ settings: {},+ signals: ["traces", "logs", "metrics"],+ name: "CH",+};++beforeEach(() => {+ __resetRegistryForTests();+});++describe("datasource registry", () => {+ it("CE enterprise hook returns no external adapters", () => {+ expect(getExternalDataSourceAdapters()).toEqual([]);+ });++ it("registers and retrieves a factory by type", () => {+ registerAdapterFactory(fakeFactory);+ expect(hasAdapterFactory("clickhouse")).toBe(true);+ expect(getAdapterFactory("clickhouse")).toBe(fakeFactory);+ expect(listAdapterTypes()).toContain("clickhouse");+ });++ it("creates an adapter from a descriptor", () => {+ registerAdapterFactory(fakeFactory);+ expect(createAdapter(descriptor)).toBe(fakeAdapter);+ });++ it("returns undefined for an unregistered type (EE type on CE)", () => {+ expect(getAdapterFactory("datadog")).toBeUndefined();+ expect(createAdapter({ ...descriptor, type: "datadog" })).toBeUndefined();+ });++ it("registers a fictional descriptor-only type end to end (extensibility proof)", () => {+ // A future OTLP vendor (e.g. dash0) should plug in with just a factory+ // whose describe() returns self-describing configFields — no shared form,+ // planner, schema, or UI edits. This proves the form's single source of+ // truth (the descriptor) resolves for a type the codebase has never seen.+ const dash0Descriptor: SourceTypeDescriptor = {+ type: "dash0",+ displayName: "dash0",+ declaredSignals: ["traces", "logs", "metrics"],+ capabilities: {+ traceTree: true,+ spanEvents: true,+ serverAggregation: true,+ spanMutation: false,+ distinctValues: true,+ crossTraceSession: false,+ rawQuery: false,+ },+ correlation: { crossSignal: true, keys: ["traceId", "spanId"] },+ authStyle: "api-key",+ authHelp: "Create a dash0 auth token in Settings → Auth Tokens.",+ docsUrl: "https://www.dash0.com/documentation",+ configFields: [+ {+ key: "url",+ label: "Endpoint",+ kind: "url",+ group: "settings",+ placeholder: "https://api.dash0.com",+ },+ {+ key: "apiKey",+ label: "Auth token",+ kind: "password",+ group: "credentials",+ },+ ],+ };+ const dash0Factory: DataSourceAdapterFactory = {+ type: "dash0",+ create: () => fakeAdapter,+ describe: () => dash0Descriptor,+ };++ registerAdapterFactory(dash0Factory);++ // It appears in the atomic picker list the shared form renders from...+ const listed = listSourceTypeDescriptors().find((d) => d.type === "dash0");+ expect(listed).toBeDefined();+ // ...and the form can read its fields + auth hints straight off the+ // descriptor with zero per-vendor code.+ const resolved = getSourceTypeDescriptor("dash0");+ expect(resolved?.authStyle).toBe("api-key");+ expect(resolved?.docsUrl).toContain("dash0");+ expect(resolved?.configFields.map((f) => f.key)).toEqual(["url", "apiKey"]);+ expect(resolved?.configFields.find((f) => f.key === "apiKey")?.kind).toBe(+ "password"+ );+ });++ it("exposes registered connector descriptors", () => {+ registerAdapterFactory(fakeFactory);+ registerAdapterFactory(internalFactory);+ expect(getSourceTypeDescriptor("clickhouse")?.declaredSignals).toEqual([+ "traces",+ "logs",+ "metrics",+ ]);+ const atomic = listSourceTypeDescriptors().map((d) => d.type);+ expect(atomic).toContain("clickhouse");+ expect(atomic).not.toContain("grafana");+ const all = listSourceTypeDescriptors({ includeInternal: true }).map((d) => d.type);+ expect(all).toContain("clickhouse");+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/rollup-policy.test.ts b/src/client/src/__tests__/lib/platform/datasource/rollup-policy.test.tsnew file mode 100644index 00000000..7a31d71c--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/rollup-policy.test.ts@@ -0,0 +1,52 @@+import {+ shouldPreferRollup,+ queryHasScopeFilters,+} from "@/lib/platform/connectors/datasource/rollup-policy";++describe("rollup-policy", () => {+ it("prefers rollups for unscoped and service/env scoped reads", () => {+ expect(shouldPreferRollup({} as any)).toBe(true);+ expect(+ shouldPreferRollup({+ selectedConfig: { serviceNames: ["demo-openai-app"] },+ } as any)+ ).toBe(true);+ expect(+ shouldPreferRollup({+ selectedConfig: { environments: ["prod"] },+ } as any)+ ).toBe(true);+ });++ it("skips rollups when version-hash scope is present", () => {+ expect(+ shouldPreferRollup({+ selectedConfig: { versionFilter: { versionHash: "abc" } },+ } as any)+ ).toBe(false);+ });++ it("detects scope filters on OpenLITQuery", () => {+ expect(+ queryHasScopeFilters({+ signal: "traces",+ timeRange: { start: new Date(), end: new Date() },+ filters: [+ {+ target: "attribute",+ scope: "resource",+ key: "service.name",+ op: "eq",+ value: "api",+ },+ ],+ })+ ).toBe(true);+ expect(+ queryHasScopeFilters({+ signal: "traces",+ timeRange: { start: new Date(), end: new Date() },+ })+ ).toBe(false);+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/safe-fetch.test.ts b/src/client/src/__tests__/lib/platform/datasource/safe-fetch.test.tsnew file mode 100644index 00000000..820a88c8--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/safe-fetch.test.ts@@ -0,0 +1,312 @@+import {+ assertPublicUrl,+ isPrivateAddress,+ redact,+ safeFetch,+ SsrfError,+} from "@/lib/platform/connectors/datasource/http/safe-fetch";++describe("isPrivateAddress", () => {+ it("flags IPv4 private / loopback / link-local / CGNAT ranges", () => {+ expect(isPrivateAddress("127.0.0.1")).toBe(true);+ expect(isPrivateAddress("10.0.0.5")).toBe(true);+ expect(isPrivateAddress("172.16.3.4")).toBe(true);+ expect(isPrivateAddress("172.32.0.1")).toBe(false);+ expect(isPrivateAddress("192.168.1.1")).toBe(true);+ expect(isPrivateAddress("169.254.169.254")).toBe(true);+ expect(isPrivateAddress("100.64.0.1")).toBe(true);+ expect(isPrivateAddress("0.0.0.0")).toBe(true);+ expect(isPrivateAddress("8.8.8.8")).toBe(false);+ });++ it("flags IPv6 loopback / link-local / ULA / mapped", () => {+ expect(isPrivateAddress("::1")).toBe(true);+ expect(isPrivateAddress("fe80::1")).toBe(true);+ expect(isPrivateAddress("fd00::1")).toBe(true);+ expect(isPrivateAddress("::ffff:127.0.0.1")).toBe(true);+ expect(isPrivateAddress("2606:4700:4700::1111")).toBe(false);+ });+});++describe("assertPublicUrl", () => {+ const lookup = async (host: string) => {+ if (host === "public.example.com") return [{ address: "8.8.8.8" }];+ if (host === "evil.example.com") return [{ address: "10.0.0.1" }];+ if (host === "mixed.example.com")+ return [{ address: "8.8.8.8" }, { address: "127.0.0.1" }];+ return [];+ };++ it("rejects non-https by default", async () => {+ await expect(+ assertPublicUrl("http://public.example.com", { lookup })+ ).rejects.toThrow(SsrfError);+ });++ it("allows http when explicitly enabled (self-hosted OSS backends)", async () => {+ const url = await assertPublicUrl("http://public.example.com", {+ allowHttp: true,+ lookup,+ });+ expect(url.hostname).toBe("public.example.com");+ });++ it("rejects javascript: and invalid URLs", async () => {+ await expect(assertPublicUrl("javascript:alert(1)")).rejects.toThrow(+ SsrfError+ );+ await expect(assertPublicUrl("not a url")).rejects.toThrow(SsrfError);+ });++ it("rejects embedded credentials", async () => {+ await expect(+ assertPublicUrl("https://user:[email protected]", { lookup })+ ).rejects.toThrow(/Credentials/);+ });++ it("allows private RFC1918 targets when allowPrivateNetwork is set", async () => {+ const url = await assertPublicUrl("http://10.0.0.5:3200", {+ allowHttp: true,+ allowPrivateNetwork: true,+ });+ expect(url.hostname).toBe("10.0.0.5");+ });++ it("allows localhost when allowPrivateNetwork is set", async () => {+ const url = await assertPublicUrl("http://localhost:3200", {+ allowHttp: true,+ allowPrivateNetwork: true,+ });+ expect(url.hostname).toBe("localhost");+ });++ it("still blocks cloud metadata even with allowPrivateNetwork", async () => {+ await expect(+ assertPublicUrl("http://169.254.169.254/latest/meta-data", {+ allowHttp: true,+ allowPrivateNetwork: true,+ })+ ).rejects.toThrow(SsrfError);+ await expect(+ assertPublicUrl("http://metadata.google.internal/", {+ allowHttp: true,+ allowPrivateNetwork: true,+ })+ ).rejects.toThrow(SsrfError);+ });++ it("rejects private targets by default", async () => {+ await expect(+ assertPublicUrl("http://10.0.0.5:3200", { allowHttp: true })+ ).rejects.toThrow(/private or localhost endpoints/i);+ });++ it("blocks hostnames that resolve to private addresses", async () => {+ await expect(+ assertPublicUrl("https://evil.example.com", { lookup })+ ).rejects.toThrow(/private or localhost endpoints/i);+ });++ it("blocks when ANY resolved address is private", async () => {+ await expect(+ assertPublicUrl("https://mixed.example.com", { lookup })+ ).rejects.toThrow(/private or localhost endpoints/i);+ });++ it("normalizes collapsed http:/ URLs before validation", async () => {+ const url = await assertPublicUrl("http:/localhost:9090", {+ allowHttp: true,+ allowPrivateNetwork: true,+ });+ expect(url.origin).toBe("http://localhost:9090");+ });++ it("explains how to allow localhost when private network is disabled", async () => {+ await expect(+ assertPublicUrl("http://localhost:9090", { allowHttp: true })+ ).rejects.toThrow(/Allow private or localhost endpoints/i);+ });++ it("allows a hostname that resolves only to public addresses", async () => {+ const url = await assertPublicUrl("https://public.example.com/path", {+ lookup,+ });+ expect(url.pathname).toBe("/path");+ });++ it("allows a public literal IP and blocks a private literal IP", async () => {+ await expect(assertPublicUrl("https://8.8.8.8")).resolves.toBeInstanceOf(URL);+ await expect(assertPublicUrl("https://10.1.2.3")).rejects.toThrow(SsrfError);+ });+});++describe("redact", () => {+ it("removes secret substrings", () => {+ expect(redact("token=abcd1234 failed", ["abcd1234"])).toBe(+ "token=[REDACTED] failed"+ );+ });+ it("ignores short secrets", () => {+ expect(redact("x=ab", ["ab"])).toBe("x=ab");+ });+});++describe("safeFetch", () => {+ const lookup = async () => [{ address: "8.8.8.8" }];++ it("returns parsed JSON on success", async () => {+ const fetchImpl = jest.fn().mockResolvedValue({+ ok: true,+ status: 200,+ text: async () => JSON.stringify({ hello: "world" }),+ });+ const result = await safeFetch("https://api.example.com", {+ lookup,+ fetchImpl: fetchImpl as unknown as typeof fetch,+ });+ expect(result).toEqual({ hello: "world" });+ });++ it("throws a redacted error on non-2xx", async () => {+ const fetchImpl = jest.fn().mockResolvedValue({+ ok: false,+ status: 403,+ text: async () => "forbidden secret-xyz-123",+ });+ await expect(+ safeFetch("https://api.example.com", {+ lookup,+ fetchImpl: fetchImpl as unknown as typeof fetch,+ redactValues: ["secret-xyz-123"],+ })+ ).rejects.toThrow(/\[REDACTED\]/);+ });++ it("does not call fetch for an SSRF-blocked URL", async () => {+ const fetchImpl = jest.fn();+ await expect(+ safeFetch("https://10.0.0.1", {+ fetchImpl: fetchImpl as unknown as typeof fetch,+ })+ ).rejects.toThrow(SsrfError);+ expect(fetchImpl).not.toHaveBeenCalled();+ });++ it("throws a typed SourceResponseError carrying the status", async () => {+ const fetchImpl = jest.fn().mockResolvedValue({+ ok: false,+ status: 429,+ text: async () => "rate limited",+ });+ await expect(+ safeFetch("https://api.example.com", {+ lookup,+ fetchImpl: fetchImpl as unknown as typeof fetch,+ })+ ).rejects.toMatchObject({ name: "SourceResponseError", status: 429 });+ });++ it("blocks a redirect that points at an internal address", async () => {+ const rebindLookup = async (host: string) => {+ if (host === "api.example.com") return [{ address: "8.8.8.8" }];+ if (host === "evil.internal") return [{ address: "169.254.169.254" }];+ return [];+ };+ const fetchImpl = jest.fn().mockResolvedValue({+ status: 302,+ headers: { get: (h: string) => (h === "location" ? "https://evil.internal/steal" : null) },+ text: async () => "",+ });+ await expect(+ safeFetch("https://api.example.com", {+ lookup: rebindLookup,+ fetchImpl: fetchImpl as unknown as typeof fetch,+ })+ ).rejects.toThrow(SsrfError);+ expect(fetchImpl).toHaveBeenCalledTimes(1);+ });++ it("follows a redirect to another public URL", async () => {+ const twoHopLookup = async () => [{ address: "8.8.8.8" }];+ const fetchImpl = jest+ .fn()+ .mockResolvedValueOnce({+ status: 302,+ headers: { get: (h: string) => (h === "location" ? "https://cdn.example.com/data" : null) },+ text: async () => "",+ })+ .mockResolvedValueOnce({+ ok: true,+ status: 200,+ headers: { get: () => null },+ text: async () => JSON.stringify({ ok: true }),+ });+ const result = await safeFetch("https://api.example.com", {+ lookup: twoHopLookup,+ fetchImpl: fetchImpl as unknown as typeof fetch,+ });+ expect(result).toEqual({ ok: true });+ expect(fetchImpl).toHaveBeenCalledTimes(2);+ });++ it("retries a transient 429 then succeeds when retry is enabled", async () => {+ const fetchImpl = jest+ .fn()+ .mockResolvedValueOnce({+ ok: false,+ status: 429,+ text: async () => "rate limited",+ })+ .mockResolvedValueOnce({+ ok: true,+ status: 200,+ headers: { get: () => null },+ text: async () => JSON.stringify({ ok: true }),+ });+ const result = await safeFetch("https://api.example.com", {+ lookup,+ fetchImpl: fetchImpl as unknown as typeof fetch,+ retry: { retries: 2, baseDelayMs: 1 },+ });+ expect(result).toEqual({ ok: true });+ expect(fetchImpl).toHaveBeenCalledTimes(2);+ });++ it("rejects oversized responses via Content-Length before reading body", async () => {+ const fetchImpl = jest.fn().mockResolvedValue({+ ok: true,+ status: 200,+ headers: {+ get: (h: string) =>+ h.toLowerCase() === "content-length" ? String(40 * 1024 * 1024) : null,+ },+ text: async () => {+ throw new Error("text() should not be called");+ },+ });+ await expect(+ safeFetch("https://api.example.com", {+ lookup,+ fetchImpl: fetchImpl as unknown as typeof fetch,+ maxResponseBytes: 1024,+ })+ ).rejects.toThrow(/MiB safety limit/i);+ });++ it("rejects oversized responses after reading when Content-Length is absent", async () => {+ const big = "x".repeat(2048);+ const fetchImpl = jest.fn().mockResolvedValue({+ ok: true,+ status: 200,+ headers: { get: () => null },+ text: async () => big,+ });+ await expect(+ safeFetch("https://api.example.com", {+ lookup,+ fetchImpl: fetchImpl as unknown as typeof fetch,+ maxResponseBytes: 1024,+ })+ ).rejects.toThrow(/MiB safety limit/i);+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/sample-fetch-list.test.ts b/src/client/src/__tests__/lib/platform/datasource/sample-fetch-list.test.tsnew file mode 100644index 00000000..bb97ef60--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/sample-fetch-list.test.ts@@ -0,0 +1,138 @@+import {+ collapseToRootSpans,+ fetchSpansForList,+ pickRootSpan,+} from "@/lib/platform/connectors/datasource/graph/sample-fetch";+import type { NormalizedSpan, OpenLITQuery } from "@/lib/platform/connectors/datasource/types";++function span(partial: Partial<NormalizedSpan>): NormalizedSpan {+ return {+ traceId: "t1",+ spanId: "s1",+ parentSpanId: "",+ name: "root",+ serviceName: "svc",+ timestamp: "2026-07-11T12:00:00.000Z",+ durationNs: 1,+ statusCode: "STATUS_CODE_OK",+ spanAttributes: {},+ resourceAttributes: {},+ ...partial,+ };+}++const windowQuery: OpenLITQuery = {+ signal: "traces",+ timeRange: {+ start: new Date("2026-07-11T00:00:00.000Z"),+ end: new Date("2026-07-11T12:00:00.000Z"),+ },+ aiSelector: true,+};++describe("sample-fetch list stratification", () => {+ it("picks root span and collapses one root per trace", () => {+ const root = span({+ spanId: "root",+ parentSpanId: "",+ timestamp: "2026-07-11T12:00:00.000Z",+ });+ const child = span({+ spanId: "child",+ parentSpanId: "root",+ timestamp: "2026-07-11T12:00:01.000Z",+ });+ expect(pickRootSpan([child, root])?.spanId).toBe("root");+ expect(collapseToRootSpans([root, child, span({ traceId: "t2", spanId: "r2" })])).toHaveLength(+ 2+ );+ });++ it("fans out across discoverServices when unscoped", async () => {+ const calls: string[] = [];+ const source = {+ discoverServices: async () => [+ { serviceName: "demo-openai-app", environment: "default", clusterId: "default" },+ { serviceName: "demo-anthropic-app", environment: "default", clusterId: "default" },+ ],+ sampleTracesForGraph: async (q: OpenLITQuery) => {+ const svc =+ (q.filters || []).find((f) => f.key === "service.name")?.value ||+ "unknown";+ calls.push(String(svc));+ return [+ span({+ traceId: `t-${svc}`,+ spanId: `s-${svc}`,+ serviceName: String(svc),+ }),+ ];+ },+ };+ const result = await fetchSpansForList(source, windowQuery, {+ maxRows: 10,+ skipCache: true,+ });+ expect(calls.sort()).toEqual(["demo-anthropic-app", "demo-openai-app"]);+ expect(result.spans.map((s) => s.serviceName).sort()).toEqual([+ "demo-anthropic-app",+ "demo-openai-app",+ ]);+ });++ it("skips stratification when service.name filter is already set", async () => {+ let calls = 0;+ const source = {+ discoverServices: async () => {+ throw new Error("should not discover");+ },+ sampleTracesForGraph: async () => {+ calls += 1;+ return [span({ serviceName: "only-one" })];+ },+ };+ const result = await fetchSpansForList(+ source,+ {+ ...windowQuery,+ filters: [+ {+ target: "attribute",+ scope: "resource",+ key: "service.name",+ op: "eq",+ value: "only-one",+ },+ ],+ },+ { maxRows: 10, skipCache: true }+ );+ expect(calls).toBe(1);+ expect(result.spans[0]?.serviceName).toBe("only-one");+ });++ it("does not use AI-only service discovery for a flat trace list", async () => {+ let discovered = false;+ let sampled = false;+ const source = {+ discoverServices: async () => {+ discovered = true;+ return [];+ },+ sampleTracesForGraph: async () => {+ sampled = true;+ return [span({ traceId: "flat-list-trace" })];+ },+ };++ const result = await fetchSpansForList(+ source,+ { ...windowQuery, aiSelector: false },+ { maxRows: 10, skipCache: true }+ );++ expect(discovered).toBe(false);+ expect(sampled).toBe(true);+ expect(result.spans).toHaveLength(1);+ });+});diff --git a/src/client/src/__tests__/lib/platform/datasource/secret.test.ts b/src/client/src/__tests__/lib/platform/datasource/secret.test.tsnew file mode 100644index 00000000..ac4486fd--- /dev/null+++ b/src/client/src/__tests__/lib/platform/datasource/secret.test.ts@@ -0,0 +1,101 @@+import {+ __resetSourceSecretCacheForTests,+ resolveSourceSecret,+} from "@/lib/platform/connectors/datasource/http/secret";++jest.mock("@/lib/platform/vault", () => ({+ getSecretById: jest.fn(),+}));++import { getSecretById } from "@/lib/platform/vault";++describe("resolveSourceSecret", () => {+ beforeEach(() => {+ jest.clearAllMocks();+ __resetSourceSecretCacheForTests();+ });++ it("parses JSON credentials from a decrypted vault value", async () => {+ (getSecretById as jest.Mock).mockResolvedValue({+ data: [+ {+ value: JSON.stringify({+ username: "1628208",+ password: "glc_token",+ }),+ },+ ],+ });+ const secret = await resolveSourceSecret("sec-1");+ expect(secret.credentials).toEqual({+ username: "1628208",+ password: "glc_token",+ });+ });++ it("fails closed when decryption leaves ciphertext in place", async () => {+ (getSecretById as jest.Mock).mockResolvedValue({+ data: [{ value: "enc:v1:iv:tag:ciphertext" }],+ });+ await expect(resolveSourceSecret("sec-1")).rejects.toThrow(+ /could not be decrypted/i+ );+ });++ it("returns empty credentials when no secretRef is set", async () => {+ const secret = await resolveSourceSecret(null);+ expect(secret).toEqual({ raw: "", credentials: {} });+ expect(getSecretById).not.toHaveBeenCalled();+ });++ it("forwards the background database and project scope to the vault lookup", async () => {+ (getSecretById as jest.Mock).mockResolvedValue({+ data: [{ value: '{"token":"abc"}' }],+ });++ await resolveSourceSecret("sec-1", "db-1", "project-1");++ expect(getSecretById).toHaveBeenCalledWith("sec-1", "db-1", false, {+ logDecryptErrors: false,+ projectId: "project-1",+ });+ });++ it("fails locally when the credential vault query fails", async () => {+ (getSecretById as jest.Mock).mockResolvedValue({+ err: new Error("read ECONNRESET"),+ });++ await expect(resolveSourceSecret("sec-1", "db-1", "project-1"))+ .rejects.toThrow(/could not be loaded from the OpenLIT vault/i);+ });++ it("treats a non-JSON vault value as a bearer token", async () => {+ (getSecretById as jest.Mock).mockResolvedValue({+ data: [{ value: "opaque-token" }],+ });++ const secret = await resolveSourceSecret("sec-opaque");+ expect(secret.credentials).toEqual({ token: "opaque-token" });+ });++ it("serves a short stale secret window when vault reads fail after a hit", async () => {+ const now = Date.now();+ const dateNow = jest.spyOn(Date, "now").mockReturnValue(now);+ (getSecretById as jest.Mock).mockResolvedValueOnce({+ data: [{ value: JSON.stringify({ token: "fresh" }) }],+ });+ const first = await resolveSourceSecret("sec-stale");+ expect(first.credentials).toEqual({ token: "fresh" });++ // Past fresh TTL (2m), still inside stale window (5m).+ dateNow.mockReturnValue(now + 3 * 60_000);+ (getSecretById as jest.Mock).mockResolvedValueOnce({+ err: new Error("vault blip"),+ });+ const second = await resolveSourceSecret("sec-stale");+ expect(second.credentials).toEqual({ token: "fresh" });+ expect(getSecretById).toHaveBeenCalledTimes(2);+ dateNow.mockRestore();+ });+});diff --git a/src/client/src/__tests__/lib/platform/evaluation/evaluation.test.ts b/src/client/src/__tests__/lib/platform/evaluation/evaluation.test.tsindex fca05de7..1c87395e 100644--- a/src/client/src/__tests__/lib/platform/evaluation/evaluation.test.ts+++ b/src/client/src/__tests__/lib/platform/evaluation/evaluation.test.ts@@ -45,11 +45,25 @@ jest.mock('@/lib/platform/cron-log', () => ({insertCronLog: jest.fn(),}));jest.mock('@/lib/db-config', () => ({+ getDBConfigById: jest.fn(),getDBConfigByIdInternal: jest.fn(),}));+jest.mock('@/lib/telemetry-source', () => ({+ getTelemetryAdapterForDbConfig: jest.fn().mockResolvedValue({+ isBuiltIn: true,+ descriptor: { type: 'clickhouse', isBuiltIn: true },+ adapter: {},+ }),+}));jest.mock('@/lib/platform/request', () => ({getRequestViaSpanId: jest.fn(),}));+// Evals fetch the span via the traces facade; delegate to the request mock so+// existing `getRequestViaSpanId` expectations keep driving the eval flows.+jest.mock('@/lib/platform/traces/read', () => ({+ getTraceSpanRecord: (...args: unknown[]) =>+ require('@/lib/platform/request').getRequestViaSpanId(...args),+}));jest.mock('@/helpers/server/platform', () => ({getFilterPreviousParams: jest.fn((p) => ({ ...p, timeLimit: { start: '2024-01-01', end: '2024-01-07' } })),}));@@ -60,8 +74,6 @@ jest.mock('@/helpers/server/trace', () => ({jest.mock('@/lib/platform/evaluation/rule-engine-context', () => ({getContextFromRuleEngineForTrace: jest.fn().mockResolvedValue({ contextContents: [], matchingRuleIds: [], contextEntityIds: [] }),getContextFromRulesWithPriority: jest.fn().mockResolvedValue({ contextContents: [], matchingRuleIds: [], contextEntityIds: [] }),- getContextFromFields: jest.fn().mockResolvedValue({ contextContents: [], matchingRuleIds: [], contextEntityIds: [] }),- getContextFromRulesWithPriorityForFields: jest.fn().mockResolvedValue({ contextContents: [], matchingRuleIds: [], contextEntityIds: [] }),}));jest.mock('@/constants/traces', () => ({SUPPORTED_EVALUATION_OPERATIONS: ['llm', 'chat'],@@ -77,11 +89,13 @@ jest.mock('@/lib/platform/evaluation/evaluation-type-defaults', () => ({getEvaluationTypeDefaultPrompt: jest.fn().mockResolvedValue(undefined),}));-import { getEvaluationsForSpanId, getEvaluationDetectedByType, autoEvaluate, setEvaluationsForSpanId, getEvaluationSummaryForSpanId, storeManualFeedback, runOfflineEvaluation } from '@/lib/platform/evaluation/index';+import { getEvaluationsForSpanId, getEvaluationDetectedByType, autoEvaluate, setEvaluationsForSpanId, getEvaluationSummaryForSpanId, storeManualFeedback, extractEvalPromptCompletion } from '@/lib/platform/evaluation/index';import { dataCollector } from '@/lib/platform/common';import { getCurrentUser } from '@/lib/session';import { getEvaluationConfig, getEvaluationConfigById } from '@/lib/platform/evaluation/config';import { getLastRunCronLogByCronId, getLastFailureCronLogBySpanId, insertCronLog } from '@/lib/platform/cron-log';+import { getDBConfigById } from '@/lib/db-config';+import { getTelemetryAdapterForDbConfig } from '@/lib/telemetry-source';import { getRequestViaSpanId } from '@/lib/platform/request';import asaw from '@/utils/asaw';import { runEvaluation } from '@/lib/platform/evaluation/run-evaluation';@@ -99,6 +113,11 @@ beforeEach(() => {(insertCronLog as jest.Mock).mockResolvedValue({ err: null });(getLastRunCronLogByCronId as jest.Mock).mockResolvedValue(null);(getRequestViaSpanId as jest.Mock).mockResolvedValue({ record: { SpanId: 'span-1' } });+ (getTelemetryAdapterForDbConfig as jest.Mock).mockResolvedValue({+ isBuiltIn: true,+ descriptor: { type: 'clickhouse', isBuiltIn: true },+ adapter: {},+ });// Default runEvaluation success(runEvaluation as jest.Mock).mockResolvedValue({ success: true, result: [] });});@@ -551,6 +570,43 @@ describe('autoEvaluate', () => {const [{ query }] = (dataCollector as jest.Mock).mock.calls[0];expect(query).toContain("meta['source'] IN ('auto', 'auto_skipped')");});++ it('does not fall back to otel_traces when the traces adapter cannot be resolved', async () => {+ (asaw as jest.Mock)+ .mockResolvedValueOnce([null, { id: 'eval-cfg-1', databaseConfigId: 'db-1', provider: 'openai', model: 'gpt-4', secret: {} }])+ .mockResolvedValueOnce([null, { id: 'db-1' }]);+ (getTelemetryAdapterForDbConfig as jest.Mock).mockRejectedValue(+ new Error('Unauthorized user!')+ );++ const result = await autoEvaluate(autoEvalConfig as any);+ expect(result.success).toBe(false);+ expect(String(result.err)).toContain('Unauthorized user!');+ expect(dataCollector).not.toHaveBeenCalled();+ });++ it('lists candidate spans from the routed traces connector', async () => {+ const listSpans = jest.fn().mockResolvedValue({+ rows: [{ spanId: 'jaeger-span', spanAttributes: {}, resourceAttributes: {} }],+ });+ (asaw as jest.Mock)+ .mockResolvedValueOnce([null, { id: 'eval-cfg-1', databaseConfigId: 'db-1', provider: 'openai', model: 'gpt-4', secret: {} }])+ .mockResolvedValueOnce([null, { id: 'db-1' }]);+ (getTelemetryAdapterForDbConfig as jest.Mock).mockResolvedValue({+ isBuiltIn: false,+ descriptor: { type: 'jaeger', isBuiltIn: false },+ adapter: { listSpans },+ });+ (dataCollector as jest.Mock).mockResolvedValue({ data: [], err: null });++ const result = await autoEvaluate(autoEvalConfig as any);+ expect(result.success).toBe(true);+ expect(listSpans).toHaveBeenCalled();+ const otelQueries = (dataCollector as jest.Mock).mock.calls.filter(+ ([params]) => typeof params?.query === 'string' && params.query.includes('otel_traces')+ );+ expect(otelQueries).toHaveLength(0);+ });});describe('setEvaluationsForSpanId', () => {@@ -831,395 +887,58 @@ describe('setEvaluationsForSpanId — evaluationTypes branches', () => {'db-1');});-- it('applies per-type thresholdScore when storing dashboard/manual evaluation verdicts', async () => {- (getRequestViaSpanId as jest.Mock).mockResolvedValue({- record: { SpanId: 'span-1', SpanAttributes: {} },- });- (getEvaluationConfig as jest.Mock).mockResolvedValue({- id: 'cfg-1',- provider: 'openai',- model: 'gpt-4',- secret: { value: 'sk-1' },- databaseConfigId: 'db-1',- evaluationTypes: [- { id: 'toxicity', enabled: true, label: 'Toxicity', thresholdScore: 0.8 },- ],- });- // Raw LLM verdict would be "yes" under the default 0.5 threshold; the- // per-type threshold of 0.8 must recompute it to "no" before storage.- (runEvaluation as jest.Mock).mockResolvedValue({- success: true,- result: [- {- evaluation: 'toxicity',- score: 0.6,- verdict: 'yes',- classification: 'mild',- explanation: 'borderline',- },- ],- });- (dataCollector as jest.Mock).mockResolvedValue({ data: true, err: null });-- const result = await setEvaluationsForSpanId('span-1');- expect(result).toMatchObject({ success: true });-- const insertCall = (dataCollector as jest.Mock).mock.calls.find(- (call) => call[1] === 'insert'- );- expect(insertCall).toBeDefined();- const stored = insertCall![0].values[0];- // storeEvaluation remaps ids to configured labels (main normalizeEvaluationResults).- expect(stored['evaluationData.evaluation']).toEqual(['Toxicity']);- expect(stored['evaluationData.verdict']).toEqual(['no']);- expect(stored.scores).toEqual({ Toxicity: 0.6 });- });});-describe('runOfflineEvaluation', () => {- const baseConfig = {- id: 'cfg-1',- provider: 'openai',- model: 'gpt-4',- secret: { value: 'sk-1' },- databaseConfigId: 'db-1',- };-- beforeEach(() => {- const { getContextFromFields, getContextFromRulesWithPriorityForFields } =- require('@/lib/platform/evaluation/rule-engine-context');- (getContextFromFields as jest.Mock).mockResolvedValue({ contextContents: [], matchingRuleIds: [], contextEntityIds: [] });- (getContextFromRulesWithPriorityForFields as jest.Mock).mockResolvedValue({ contextContents: [], matchingRuleIds: [], contextEntityIds: [] });- });-- it('rejects unknown eval types without calling runEvaluation', async () => {- const config = {- ...baseConfig,- evaluationTypes: [{ id: 'hallucination', enabled: true, label: 'Hallucination' }],+describe('extractEvalPromptCompletion (attribute-first extraction)', () => {+ it('prefers span attributes (gen_ai.input/output.messages)', () => {+ const trace = {+ SpanAttributes: {+ 'gen_ai.input.messages': 'attr-prompt',+ 'gen_ai.output.messages': 'attr-response',+ },+ // events would resolve via the mocked SpanAttributes.<key> path+ prompt: 'event-prompt',+ response: 'event-response',};-- const result = await runOfflineEvaluation(- { prompt: 'p', response: 'r', evalTypes: ['bogus'] },- config as any,- 'db-1'- );-- expect(result.success).toBe(false);- expect(result.error).toBe('Unknown eval types: bogus');- expect(runEvaluation).not.toHaveBeenCalled();- });-- it('rejects explicitly requested but disabled eval types without calling runEvaluation', async () => {- const config = {- ...baseConfig,- evaluationTypes: [- { id: 'hallucination', enabled: true, label: 'Hallucination' },- { id: 'bias', enabled: false, label: 'Bias' },- ],- };-- const result = await runOfflineEvaluation(- { prompt: 'p', response: 'r', evalTypes: ['bias'] },- config as any,- 'db-1'- );-- expect(result.success).toBe(false);- expect(result.error).toBe('Disabled eval types: bias');- expect(runEvaluation).not.toHaveBeenCalled();- });-- it('reports unknown types even when other requested types are valid', async () => {- const config = {- ...baseConfig,- evaluationTypes: [{ id: 'hallucination', enabled: true, label: 'Hallucination' }],- };-- const result = await runOfflineEvaluation(- { prompt: 'p', response: 'r', evalTypes: ['hallucination', 'bogus'] },- config as any,- 'db-1'- );-- expect(result.success).toBe(false);- expect(result.error).toBe('Unknown eval types: bogus');- expect(runEvaluation).not.toHaveBeenCalled();- });-- it('runs only the requested enabled types, excluding others from context', async () => {- const config = {- ...baseConfig,- evaluationTypes: [- { id: 'hallucination', enabled: true, label: 'Hallucination', defaultPrompt: 'hallucination ctx' },- { id: 'bias', enabled: true, label: 'Bias', defaultPrompt: 'bias ctx' },- ],- };- (runEvaluation as jest.Mock).mockResolvedValue({- success: true,- result: [{ evaluation: 'Hallucination', score: 0.1, classification: 'none', explanation: 'ok', verdict: 'no' }],+ expect(extractEvalPromptCompletion(trace)).toEqual({+ prompt: 'attr-prompt',+ response: 'attr-response',});-- const result = await runOfflineEvaluation(- { prompt: 'p', response: 'r', evalTypes: ['hallucination'] },- config as any,- 'db-1'- );-- expect(result.success).toBe(true);- expect(result.metadata?.evalTypesRun).toEqual(['hallucination']);- const [{ contexts }] = (runEvaluation as jest.Mock).mock.calls[0];- expect(contexts).toContain('hallucination ctx');- expect(contexts).not.toContain('bias ctx');});- it('defaults to all enabled types when eval_types is omitted', async () => {- const config = {- ...baseConfig,- evaluationTypes: [- { id: 'hallucination', enabled: true, label: 'Hallucination' },- { id: 'bias', enabled: false, label: 'Bias' },- { id: 'relevance', enabled: true, label: 'Relevance' },- ],+ it('falls back through legacy attribute keys', () => {+ const trace = {+ SpanAttributes: {+ 'gen_ai.prompt': 'legacy-prompt',+ 'gen_ai.completion': 'legacy-completion',+ },};- (runEvaluation as jest.Mock).mockResolvedValue({ success: true, result: [] });-- const result = await runOfflineEvaluation(- { prompt: 'p', response: 'r' },- config as any,- 'db-1'- );-- expect(result.success).toBe(true);- expect([...(result.metadata?.evalTypesRun || [])].sort()).toEqual(['hallucination', 'relevance']);- });-- it('falls back to hallucination/bias/toxicity defaults when nothing is enabled and no eval_types requested', async () => {- const config = {- ...baseConfig,- evaluationTypes: [- { id: 'hallucination', enabled: false, label: 'Hallucination' },- { id: 'bias', enabled: false, label: 'Bias' },- { id: 'toxicity', enabled: false, label: 'Toxicity' },- { id: 'relevance', enabled: false, label: 'Relevance' },- ],- };- (runEvaluation as jest.Mock).mockResolvedValue({ success: true, result: [] });-- const result = await runOfflineEvaluation(- { prompt: 'p', response: 'r' },- config as any,- 'db-1'- );-- expect([...(result.metadata?.evalTypesRun || [])].sort()).toEqual(['bias', 'hallucination', 'toxicity']);- });-- it('overrides verdict to "no" when the per-type threshold is stricter than the raw score', async () => {- const config = {- ...baseConfig,- evaluationTypes: [{ id: 'toxicity', enabled: true, label: 'Toxicity', thresholdScore: 0.8 }],- };- // Score of 0.6 would be 'yes' under the default 0.5 threshold, but the- // per-type threshold of 0.8 should keep it 'no'.- (runEvaluation as jest.Mock).mockResolvedValue({- success: true,- result: [{ evaluation: 'Toxicity', score: 0.6, classification: 'mild', explanation: 'x', verdict: 'yes' }],+ expect(extractEvalPromptCompletion(trace)).toEqual({+ prompt: 'legacy-prompt',+ response: 'legacy-completion',});-- const result = await runOfflineEvaluation(- { prompt: 'p', response: 'r', evalTypes: ['toxicity'] },- config as any,- 'db-1'- );-- expect(result.evaluations?.[0].verdict).toBe('no');});- it('overrides verdict to "yes" when the score exceeds a stricter per-type threshold', async () => {- const config = {- ...baseConfig,- evaluationTypes: [{ id: 'toxicity', enabled: true, label: 'Toxicity', thresholdScore: 0.3 }],+ it('falls back to span events when no span attributes are present', () => {+ // The mocked getTraceMappingKeyFullPath returns `SpanAttributes.<key>`,+ // so the events fallback resolves trace.SpanAttributes.prompt/.response.+ const trace = {+ SpanAttributes: { prompt: 'event-prompt', response: 'event-response' },};- (runEvaluation as jest.Mock).mockResolvedValue({- success: true,- result: [{ evaluation: 'Toxicity', score: 0.5, classification: 'mild', explanation: 'x', verdict: 'no' }],+ expect(extractEvalPromptCompletion(trace)).toEqual({+ prompt: 'event-prompt',+ response: 'event-response',});-- const result = await runOfflineEvaluation(- { prompt: 'p', response: 'r', evalTypes: ['toxicity'] },- config as any,- 'db-1'- );-- expect(result.evaluations?.[0].verdict).toBe('yes');});- it('falls back to the request thresholdScore when the type has no per-type threshold configured', async () => {- const config = {- ...baseConfig,- evaluationTypes: [{ id: 'toxicity', enabled: true, label: 'Toxicity' }],- };- (runEvaluation as jest.Mock).mockResolvedValue({- success: true,- result: [{ evaluation: 'Toxicity', score: 0.9, classification: 'severe', explanation: 'x', verdict: 'no' }],- });-- const result = await runOfflineEvaluation(- { prompt: 'p', response: 'r', evalTypes: ['toxicity'], thresholdScore: 0.95 },- config as any,- 'db-1'- );-- // 0.9 does not exceed the request-level 0.95 threshold- expect(result.evaluations?.[0].verdict).toBe('no');- });-- it('falls back to the request threshold when the returned evaluation label matches no configured type', async () => {- const config = {- ...baseConfig,- evaluationTypes: [- { id: 'sensitivity', enabled: true, label: 'PII - Financial', thresholdScore: 0.9 },- ],- };- // The model echoes back a label that doesn't match the configured id or- // label exactly (e.g. a custom prompt without the matching context header).- (runEvaluation as jest.Mock).mockResolvedValue({- success: true,- result: [{ evaluation: 'Pii Financial Data', score: 0.6, classification: 'leak', explanation: 'x', verdict: 'yes' }],- });-- const result = await runOfflineEvaluation(- { prompt: 'p', response: 'r', evalTypes: ['sensitivity'], thresholdScore: 0.5 },- config as any,- 'db-1'- );-- // The unmatched result still comes back (no crash, no dropped row), and- // since its label didn't match, the 0.9 per-type threshold is NOT applied —- // it falls back to the request-level 0.5 threshold instead.- expect(result.success).toBe(true);- expect(result.evaluations).toHaveLength(1);- expect(result.evaluations?.[0].verdict).toBe('yes');- });-- it('returns failure when runEvaluation fails', async () => {- const config = {- ...baseConfig,- evaluationTypes: [{ id: 'hallucination', enabled: true, label: 'Hallucination' }],- };- (runEvaluation as jest.Mock).mockResolvedValue({ success: false, error: 'Model error' });-- const result = await runOfflineEvaluation(- { prompt: 'p', response: 'r' },- config as any,- 'db-1'- );-- expect(result.success).toBe(false);- expect(result.error).toBe('Model error');- });-- describe('rule-priority context consistency with the real-time path', () => {- it('collects rules from enabled types and resolves context via getContextFromRulesWithPriorityForFields, matching the real-time path', async () => {- const { getContextFromRulesWithPriorityForFields, getContextFromFields } =- require('@/lib/platform/evaluation/rule-engine-context');- (getContextFromRulesWithPriorityForFields as jest.Mock).mockResolvedValue({- contextContents: ['linked rule context'],- matchingRuleIds: ['r1'],- contextEntityIds: ['e1'],- });- const config = {- ...baseConfig,- evaluationTypes: [- { id: 'hallucination', enabled: true, rules: [{ ruleId: 'r1', priority: 5 }] },- ],- };- (runEvaluation as jest.Mock).mockResolvedValue({ success: true, result: [] });-- const result = await runOfflineEvaluation(- { prompt: 'p', response: 'r', attributes: { 'service.name': 'my-app' } },- config as any,- 'db-1'- );-- expect(getContextFromRulesWithPriorityForFields).toHaveBeenCalledWith(- { 'service.name': 'my-app' },- [{ ruleId: 'r1', priority: 5 }],- 'db-1'- );- expect(getContextFromFields).not.toHaveBeenCalled();- expect(result.contextApplied?.matchingRuleIds).toEqual(['r1']);- });-- it('collects legacy ruleId/priority fields into rulesWithPriority, same as the real-time path', async () => {- const { getContextFromRulesWithPriorityForFields } =- require('@/lib/platform/evaluation/rule-engine-context');- (getContextFromRulesWithPriorityForFields as jest.Mock).mockResolvedValue({- contextContents: [], matchingRuleIds: [], contextEntityIds: [],- });- const config = {- ...baseConfig,- evaluationTypes: [- { id: 'hallucination', enabled: true, ruleId: 'r-legacy', priority: 2 },- ],- };- (runEvaluation as jest.Mock).mockResolvedValue({ success: true, result: [] });-- await runOfflineEvaluation(- { prompt: 'p', response: 'r', attributes: { 'service.name': 'my-app' } },- config as any,- 'db-1'- );-- expect(getContextFromRulesWithPriorityForFields).toHaveBeenCalledWith(- { 'service.name': 'my-app' },- [{ ruleId: 'r-legacy', priority: 2 }],- 'db-1'- );- });-- it('falls back to getContextFromFields when no enabled type has linked rules', async () => {- const { getContextFromFields, getContextFromRulesWithPriorityForFields } =- require('@/lib/platform/evaluation/rule-engine-context');- (getContextFromFields as jest.Mock).mockResolvedValue({- contextContents: ['generic context'], matchingRuleIds: [], contextEntityIds: [],- });- const config = {- ...baseConfig,- evaluationTypes: [{ id: 'hallucination', enabled: true }],- };- (runEvaluation as jest.Mock).mockResolvedValue({ success: true, result: [] });-- await runOfflineEvaluation(- { prompt: 'p', response: 'r', attributes: { 'service.name': 'my-app' } },- config as any,- 'db-1'- );-- expect(getContextFromFields).toHaveBeenCalledWith({ 'service.name': 'my-app' }, 'db-1');- expect(getContextFromRulesWithPriorityForFields).not.toHaveBeenCalled();+ it('returns empty strings when nothing is available', () => {+ expect(extractEvalPromptCompletion({ SpanAttributes: {} })).toEqual({+ prompt: '',+ response: '',});-- it('does not call the rule engine at all when no attributes are provided', async () => {- const { getContextFromFields, getContextFromRulesWithPriorityForFields } =- require('@/lib/platform/evaluation/rule-engine-context');- const config = {- ...baseConfig,- evaluationTypes: [- { id: 'hallucination', enabled: true, rules: [{ ruleId: 'r1', priority: 5 }] },- ],- };- (runEvaluation as jest.Mock).mockResolvedValue({ success: true, result: [] });-- await runOfflineEvaluation(- { prompt: 'p', response: 'r' },- config as any,- 'db-1'- );-- expect(getContextFromFields).not.toHaveBeenCalled();- expect(getContextFromRulesWithPriorityForFields).not.toHaveBeenCalled();+ expect(extractEvalPromptCompletion(undefined)).toEqual({+ prompt: '',+ response: '',});});});diff --git a/src/client/src/__tests__/lib/platform/gpu/gpu.test.ts b/src/client/src/__tests__/lib/platform/gpu/gpu.test.tsindex 95f82377..22491c77 100644--- a/src/client/src/__tests__/lib/platform/gpu/gpu.test.ts+++ b/src/client/src/__tests__/lib/platform/gpu/gpu.test.ts@@ -3,6 +3,15 @@ jest.mock('@/lib/platform/common', () => ({OTEL_GPUS_TABLE_NAME: 'otel_metrics_gauge',}));+jest.mock('@/lib/platform/gpu/external', () => ({+ externalAverageUtilization: jest.fn().mockResolvedValue(null),+ externalUtilizationParamsPerTime: jest.fn().mockResolvedValue(null),+ externalAveragePowerDraw: jest.fn().mockResolvedValue(null),+ externalPowerParamsPerTime: jest.fn().mockResolvedValue(null),+ externalAverageMemoryUsage: jest.fn().mockResolvedValue(null),+ externalMemoryParamsPerTime: jest.fn().mockResolvedValue(null),+}));+import { getAverageUtilization, getUtilizationParamsPerTime } from '@/lib/platform/gpu/utilization';import { getAveragePowerDraw, getPowerParamsPerTime } from '@/lib/platform/gpu/power';import { getMemoryParamsPerTime, getAverageMemoryUsage } from '@/lib/platform/gpu/memory';diff --git a/src/client/src/__tests__/lib/platform/gpu/temperature-fanspeed.test.ts b/src/client/src/__tests__/lib/platform/gpu/temperature-fanspeed.test.tsindex 3fe12dae..f52ca893 100644--- a/src/client/src/__tests__/lib/platform/gpu/temperature-fanspeed.test.ts+++ b/src/client/src/__tests__/lib/platform/gpu/temperature-fanspeed.test.ts@@ -3,6 +3,12 @@ jest.mock('@/lib/platform/common', () => ({OTEL_GPUS_TABLE_NAME: 'otel_metrics_gauge',}));+jest.mock('@/lib/platform/gpu/external', () => ({+ externalAverageTemperature: jest.fn().mockResolvedValue(null),+ externalAverageTemperatureParamsPerTime: jest.fn().mockResolvedValue(null),+ externalFanspeedParamsPerTime: jest.fn().mockResolvedValue(null),+}));+import { getAverageTemperature, getAverageTemperatureParamsPerTime } from '@/lib/platform/gpu/temperature';import { getFanspeedParamsPerTime } from '@/lib/platform/gpu/fanspeed';import { dataCollector } from '@/lib/platform/common';diff --git a/src/client/src/__tests__/lib/platform/llm/category.test.ts b/src/client/src/__tests__/lib/platform/llm/category.test.tsindex 700d9aea..8b937d3a 100644--- a/src/client/src/__tests__/lib/platform/llm/category.test.ts+++ b/src/client/src/__tests__/lib/platform/llm/category.test.ts@@ -2,6 +2,22 @@ jest.mock('@/lib/platform/common', () => ({dataCollector: jest.fn().mockResolvedValue({ data: [] }),OTEL_TRACES_TABLE_NAME: 'otel_traces',}));+// Isolate the built-in ClickHouse SQL path: the external-source router pulls in+// the telemetry-source/next-auth chain (ESM `jose`) which Jest cannot parse.+// Returning null routes every helper to the built-in SQL branch under test.+jest.mock('@/lib/platform/llm/external', () => ({+ externalTotalCost: jest.fn().mockResolvedValue(null),+ externalAverageCost: jest.fn().mockResolvedValue(null),+ externalCostPerTime: jest.fn().mockResolvedValue(null),+ externalAverageTokens: jest.fn().mockResolvedValue(null),+ externalTokensPerTime: jest.fn().mockResolvedValue(null),+ externalGenerationByCategories: jest.fn().mockResolvedValue(null),+ externalGenerationByProvider: jest.fn().mockResolvedValue(null),+ externalTopModels: jest.fn().mockResolvedValue(null),+ externalCostByApplication: jest.fn().mockResolvedValue(null),+ externalCostByEnvironment: jest.fn().mockResolvedValue(null),+ externalModelsPerTime: jest.fn().mockResolvedValue(null),+}));import { getResultGenerationByCategories } from '@/lib/platform/llm/category';import { dataCollector } from '@/lib/platform/common';diff --git a/src/client/src/__tests__/lib/platform/llm/cost.test.ts b/src/client/src/__tests__/lib/platform/llm/cost.test.tsindex ea356c26..63416b14 100644--- a/src/client/src/__tests__/lib/platform/llm/cost.test.ts+++ b/src/client/src/__tests__/lib/platform/llm/cost.test.ts@@ -19,6 +19,13 @@ jest.mock("@/helpers/server/trace", () => ({getTraceMappingKeyFullPaths: jest.fn(() => ["gen_ai.provider.name", "gen_ai.system"]),}));+jest.mock("@/lib/platform/llm/external", () => ({+ externalTotalCost: jest.fn(async () => null),+ externalAverageCost: jest.fn(async () => null),+ externalCostByApplication: jest.fn(async () => null),+ externalCostByEnvironment: jest.fn(async () => null),+}));+import { dataCollector } from "@/lib/platform/common";import {getCostByApplication,@@ -80,7 +87,7 @@ describe("llm cost analytics queries", () => {await getCostPerTime(params);expect(mockedDataCollector).toHaveBeenCalledWith(expect.objectContaining({- query: expect.stringContaining("request_time"),+ query: expect.stringContaining("AS request_time"),}));});diff --git a/src/client/src/__tests__/lib/platform/llm/endpoint.test.ts b/src/client/src/__tests__/lib/platform/llm/endpoint.test.tsindex 282b5b7a..5518ba02 100644--- a/src/client/src/__tests__/lib/platform/llm/endpoint.test.ts+++ b/src/client/src/__tests__/lib/platform/llm/endpoint.test.ts@@ -2,6 +2,22 @@ jest.mock('@/lib/platform/common', () => ({dataCollector: jest.fn().mockResolvedValue({ data: [] }),OTEL_TRACES_TABLE_NAME: 'otel_traces',}));+// Isolate the built-in ClickHouse SQL path: the external-source router pulls in+// the telemetry-source/next-auth chain (ESM `jose`) which Jest cannot parse.+// Returning null routes every helper to the built-in SQL branch under test.+jest.mock('@/lib/platform/llm/external', () => ({+ externalTotalCost: jest.fn().mockResolvedValue(null),+ externalAverageCost: jest.fn().mockResolvedValue(null),+ externalCostPerTime: jest.fn().mockResolvedValue(null),+ externalAverageTokens: jest.fn().mockResolvedValue(null),+ externalTokensPerTime: jest.fn().mockResolvedValue(null),+ externalGenerationByCategories: jest.fn().mockResolvedValue(null),+ externalGenerationByProvider: jest.fn().mockResolvedValue(null),+ externalTopModels: jest.fn().mockResolvedValue(null),+ externalCostByApplication: jest.fn().mockResolvedValue(null),+ externalCostByEnvironment: jest.fn().mockResolvedValue(null),+ externalModelsPerTime: jest.fn().mockResolvedValue(null),+}));import { getResultGenerationByEndpoint } from '@/lib/platform/llm/endpoint';import { dataCollector } from '@/lib/platform/common';diff --git a/src/client/src/__tests__/lib/platform/llm/model.test.ts b/src/client/src/__tests__/lib/platform/llm/model.test.tsindex 3c6cd8fb..92fa87c3 100644--- a/src/client/src/__tests__/lib/platform/llm/model.test.ts+++ b/src/client/src/__tests__/lib/platform/llm/model.test.ts@@ -2,6 +2,22 @@ jest.mock('@/lib/platform/common', () => ({dataCollector: jest.fn().mockResolvedValue({ data: [] }),OTEL_TRACES_TABLE_NAME: 'otel_traces',}));+// Isolate the built-in ClickHouse SQL path: the external-source router pulls in+// the telemetry-source/next-auth chain (ESM `jose`) which Jest cannot parse.+// Returning null routes every helper to the built-in SQL branch under test.+jest.mock('@/lib/platform/llm/external', () => ({+ externalTotalCost: jest.fn().mockResolvedValue(null),+ externalAverageCost: jest.fn().mockResolvedValue(null),+ externalCostPerTime: jest.fn().mockResolvedValue(null),+ externalAverageTokens: jest.fn().mockResolvedValue(null),+ externalTokensPerTime: jest.fn().mockResolvedValue(null),+ externalGenerationByCategories: jest.fn().mockResolvedValue(null),+ externalGenerationByProvider: jest.fn().mockResolvedValue(null),+ externalTopModels: jest.fn().mockResolvedValue(null),+ externalCostByApplication: jest.fn().mockResolvedValue(null),+ externalCostByEnvironment: jest.fn().mockResolvedValue(null),+ externalModelsPerTime: jest.fn().mockResolvedValue(null),+}));import { getTopModels, getModelsPerTime } from '@/lib/platform/llm/model';import { dataCollector } from '@/lib/platform/common';diff --git a/src/client/src/__tests__/lib/platform/llm/token.test.ts b/src/client/src/__tests__/lib/platform/llm/token.test.tsindex de79c151..7f7a5530 100644--- a/src/client/src/__tests__/lib/platform/llm/token.test.ts+++ b/src/client/src/__tests__/lib/platform/llm/token.test.ts@@ -2,6 +2,22 @@ jest.mock('@/lib/platform/common', () => ({dataCollector: jest.fn().mockResolvedValue({ data: [] }),OTEL_TRACES_TABLE_NAME: 'otel_traces',}));+// Isolate the built-in ClickHouse SQL path: the external-source router pulls in+// the telemetry-source/next-auth chain (ESM `jose`) which Jest cannot parse.+// Returning null routes every helper to the built-in SQL branch under test.+jest.mock('@/lib/platform/llm/external', () => ({+ externalTotalCost: jest.fn().mockResolvedValue(null),+ externalAverageCost: jest.fn().mockResolvedValue(null),+ externalCostPerTime: jest.fn().mockResolvedValue(null),+ externalAverageTokens: jest.fn().mockResolvedValue(null),+ externalTokensPerTime: jest.fn().mockResolvedValue(null),+ externalGenerationByCategories: jest.fn().mockResolvedValue(null),+ externalGenerationByProvider: jest.fn().mockResolvedValue(null),+ externalTopModels: jest.fn().mockResolvedValue(null),+ externalCostByApplication: jest.fn().mockResolvedValue(null),+ externalCostByEnvironment: jest.fn().mockResolvedValue(null),+ externalModelsPerTime: jest.fn().mockResolvedValue(null),+}));import { getAverageTokensPerRequest, getTokensPerTime } from '@/lib/platform/llm/token';import { dataCollector } from '@/lib/platform/common';diff --git a/src/client/src/__tests__/lib/platform/manage-dashboard/board-format.test.ts b/src/client/src/__tests__/lib/platform/manage-dashboard/board-format.test.tsnew file mode 100644index 00000000..a0f442aa--- /dev/null+++ b/src/client/src/__tests__/lib/platform/manage-dashboard/board-format.test.ts@@ -0,0 +1,112 @@+import {+ CURRENT_DASHBOARD_FORMAT_VERSION,+ DASHBOARD_FORMAT_VERSION_V1,+ DASHBOARD_FORMAT_VERSION_V2,+ detectDashboardFormatVersion,+ normalizeDashboardTags,+ normalizeImportedDashboard,+ toExportDashboardPayload,+} from "@/lib/platform/manage-dashboard/board-format";++describe("board-format", () => {+ it("treats missing formatVersion as v1", () => {+ expect(detectDashboardFormatVersion({ title: "Legacy" })).toBe(+ DASHBOARD_FORMAT_VERSION_V1+ );+ });++ it("detects v2 format versions", () => {+ expect(detectDashboardFormatVersion({ formatVersion: 2 })).toBe(+ DASHBOARD_FORMAT_VERSION_V2+ );+ expect(detectDashboardFormatVersion({ formatVersion: "v2" })).toBe(+ DASHBOARD_FORMAT_VERSION_V2+ );+ });++ it("normalizes tags from arrays and JSON strings", () => {+ expect(normalizeDashboardTags(["a", " b "])).toEqual(["a", "b"]);+ expect(normalizeDashboardTags('["llm","prod"]')).toEqual(["llm", "prod"]);+ expect(normalizeDashboardTags("")).toEqual([]);+ expect(normalizeDashboardTags(undefined)).toEqual([]);+ });++ it("imports legacy v1 dashboard JSON with tags as arrays", () => {+ const result = normalizeImportedDashboard({+ title: "LLM dashboard",+ description: "legacy",+ tags: ["llm"],+ widgets: {+ "w-1": {+ id: "w-1",+ title: "Total",+ type: "STAT_CARD",+ properties: {},+ config: { query: "SELECT 1" },+ },+ },+ layouts: {+ lg: [{ i: "w-1", x: 0, y: 0, w: 2, h: 2 }],+ },+ });++ expect("err" in result).toBe(false);+ if ("err" in result) return;+ expect(result.data.formatVersion).toBe(DASHBOARD_FORMAT_VERSION_V1);+ expect(result.data.tags).toEqual(["llm"]);+ expect(result.data.widgets["w-1"].title).toBe("Total");+ expect(result.data.layouts.lg).toEqual([+ { i: "w-1", x: 0, y: 0, w: 2, h: 2 },+ ]);+ });++ it("rejects imports without a title", () => {+ expect(normalizeImportedDashboard({ widgets: {} })).toEqual({+ err: "Dashboard import requires a title.",+ });+ });++ it("synthesizes layouts when missing", () => {+ const result = normalizeImportedDashboard({+ title: "Broken layouts",+ widgets: {+ "w-1": { id: "w-1", title: "A", type: "STAT_CARD", properties: {}, config: {} },+ "w-2": { id: "w-2", title: "B", type: "STAT_CARD", properties: {}, config: {} },+ },+ });+ expect("err" in result).toBe(false);+ if ("err" in result) return;+ expect(result.data.layouts.lg).toHaveLength(2);+ expect(result.data.layouts.lg.map((item) => item.i).sort()).toEqual([+ "w-1",+ "w-2",+ ]);+ });++ it("exports v2 payloads with normalized tags", () => {+ const exported = toExportDashboardPayload({+ title: "Ops",+ description: "desc",+ isPinned: true,+ isMainDashboard: false,+ tags: '["ops"]',+ layouts: { lg: [{ i: "w-1", x: 0, y: 0, w: 1, h: 1 }] },+ widgets: {+ "w-1": {+ id: "w-1",+ title: "Count",+ description: "",+ type: "STAT_CARD",+ properties: {},+ config: { signal: "metrics", sourceId: "src-prom" },+ createdAt: "",+ updatedAt: "",+ },+ },+ });++ expect(exported.formatVersion).toBe(CURRENT_DASHBOARD_FORMAT_VERSION);+ expect(exported.tags).toEqual(["ops"]);+ expect((exported.widgets as any)["w-1"].config.sourceId).toBe("src-prom");+ });+});diff --git a/src/client/src/__tests__/lib/platform/manage-dashboard/widget-sql-bridge.test.ts b/src/client/src/__tests__/lib/platform/manage-dashboard/widget-sql-bridge.test.tsnew file mode 100644index 00000000..280c9261--- /dev/null+++ b/src/client/src/__tests__/lib/platform/manage-dashboard/widget-sql-bridge.test.ts@@ -0,0 +1,164 @@+import {+ inferStructuredFromClickHouseSql,+ isLegacyOtelTracesSql,+ percentChange,+ stripSyntheticDefaultEnvironment,+} from "@/lib/platform/manage-dashboard/widget-sql-bridge";+import type { OpenLITQuery } from "@/lib/platform/connectors/datasource/types";++describe("widget-sql-bridge", () => {+ it("detects otel_traces SQL and skips evaluation tables", () => {+ expect(isLegacyOtelTracesSql("SELECT count() FROM otel_traces")).toBe(true);+ expect(+ isLegacyOtelTracesSql(+ "SELECT count() FROM openlit_evaluation WHERE score > 0"+ )+ ).toBe(false);+ });++ it("returns null percent change when previous period is zero", () => {+ expect(percentChange(14200, 0)).toBeNull();+ expect(percentChange(0, 0)).toBeNull();+ expect(percentChange(150, 100)).toBe(50);+ expect(percentChange(50, 100)).toBe(-50);+ });++ it("does not send the synthetic default environment to external sources", () => {+ const query = stripSyntheticDefaultEnvironment({+ signal: "traces",+ timeRange: { start: new Date("2026-07-28"), end: new Date("2026-07-29") },+ filters: [+ {+ target: "attribute",+ scope: "resource",+ key: "deployment.environment",+ op: "eq",+ value: "default",+ },+ ],+ } satisfies OpenLITQuery);++ expect(query.filters).toBeUndefined();+ });++ it("infers total-request previous-period aggregate", () => {+ const inferred = inferStructuredFromClickHouseSql(`+ WITH prev_start_time AS x+ SELECT CAST(countIf(Timestamp >= start_time) AS INTEGER) AS total_request,+ CAST(countIf(Timestamp >= prev_start_time) AS INTEGER) AS total_request_previous,+ 1 AS rate+ FROM otel_traces+ `);+ expect(inferred).toMatchObject({+ mode: "aggregate",+ includePrevious: true,+ primaryAlias: "total_request",+ previousAlias: "total_request_previous",+ });+ expect(inferred?.aggregations[0]).toEqual({+ fn: "count",+ as: "total_request",+ });+ });++ it("infers timeseries request counts", () => {+ const inferred = inferStructuredFromClickHouseSql(`+ SELECT CAST(COUNT(*) AS INTEGER) AS total,+ formatDateTime(DATE_TRUNC('hour', Timestamp), '%Y/%m/%d %R') AS request_time+ FROM otel_traces+ GROUP BY request_time+ `);+ expect(inferred).toMatchObject({+ mode: "timeseries",+ includePrevious: false,+ primaryAlias: "total",+ });+ });++ it("infers provider group-by", () => {+ const inferred = inferStructuredFromClickHouseSql(`+ SELECT SpanAttributes['gen_ai.system'] AS provider, CAST(COUNT(*) AS INTEGER) AS count+ FROM otel_traces+ GROUP BY provider+ `);+ expect(inferred?.groupBy).toEqual(["gen_ai.system"]);+ expect(inferred?.aggregations[0].fn).toBe("count");+ });++ it("infers models-per-time with total_model_count primary alias", () => {+ const inferred = inferStructuredFromClickHouseSql(`+ SELECT+ ARRAY_AGG(model) AS models,+ ARRAY_AGG(model_count) AS model_counts,+ CAST(SUM(model_count) AS INTEGER) AS total_model_count,+ formatDateTime(DATE_TRUNC('hour', Timestamp), '%Y/%m/%d %R') AS request_time+ FROM (+ SELECT+ SpanAttributes['gen_ai.request.model'] AS model,+ COUNT(*) AS model_count,+ formatDateTime(DATE_TRUNC('hour', Timestamp), '%Y/%m/%d %R') AS request_time+ FROM otel_traces+ GROUP BY model, request_time+ ) AS sub+ GROUP BY request_time+ `);+ expect(inferred).toMatchObject({+ mode: "timeseries",+ primaryAlias: "total_model_count",+ groupBy: ["gen_ai.request.model"],+ });+ expect(inferred?.aggregations[0]).toEqual({+ fn: "count",+ as: "model_count",+ });+ });++ it("infers token usage timeseries with prompt and completion series", () => {+ const inferred = inferStructuredFromClickHouseSql(`+ SELECT+ CAST(SUM(toInt64OrZero(SpanAttributes['gen_ai.usage.total_tokens'])) AS INTEGER) AS total_tokens,+ CAST(SUM(toInt64OrZero(SpanAttributes['gen_ai.usage.input_tokens'])) AS INTEGER) AS prompt_tokens,+ CAST(SUM(toInt64OrZero(SpanAttributes['gen_ai.usage.output_tokens'])) AS INTEGER) AS completion_tokens,+ formatDateTime(DATE_TRUNC('day', Timestamp), '%Y/%m/%d %R') AS request_time+ FROM otel_traces+ GROUP BY request_time+ `);+ expect(inferred?.mode).toBe("timeseries");+ expect(inferred?.aggregations).toEqual(+ expect.arrayContaining([+ { fn: "sum", field: "gen_ai.usage.total_tokens", as: "total_tokens" },+ { fn: "sum", field: "gen_ai.usage.input_tokens", as: "prompt_tokens" },+ { fn: "sum", field: "gen_ai.usage.output_tokens", as: "completion_tokens" },+ ])+ );+ });++ it("round-trips structured query to ClickHouse SQL", () => {+ const { openLITQueryToClickHouseSql, inferredToStructuredQuery } =+ require("@/lib/platform/manage-dashboard/widget-sql-bridge") as typeof import("@/lib/platform/manage-dashboard/widget-sql-bridge");+ const inferred = inferStructuredFromClickHouseSql(`+ SELECT SpanAttributes['gen_ai.request.model'] AS model, CAST(COUNT(*) AS INTEGER) AS count+ FROM otel_traces+ GROUP BY model+ `);+ expect(inferred).not.toBeNull();+ const structured = inferredToStructuredQuery(inferred!);+ const sql = openLITQueryToClickHouseSql(+ structured.query as any,+ structured.mode+ );+ expect(sql).toContain("FROM otel_traces");+ expect(sql).toContain("gen_ai.request.model");+ expect(sql).toContain("count()");+ });++ it("does not treat prototype keys as aggregation aliases", () => {+ const inferred = inferStructuredFromClickHouseSql(`+ SELECT avg(SpanAttributes['gen_ai.usage.input_tokens']) AS constructor+ FROM otel_traces+ `);+ expect(inferred?.primaryAlias).not.toBe("constructor");+ expect(inferred?.primaryAlias).not.toBe("__proto__");+ expect(inferred?.primaryAlias).toBe("count");+ });+});diff --git a/src/client/src/__tests__/lib/platform/manage-dashboard/widget.test.ts b/src/client/src/__tests__/lib/platform/manage-dashboard/widget.test.tsindex 71cf3502..7c05dce7 100644--- a/src/client/src/__tests__/lib/platform/manage-dashboard/widget.test.ts+++ b/src/client/src/__tests__/lib/platform/manage-dashboard/widget.test.ts@@ -10,8 +10,23 @@ jest.mock("@/constants/messages", () => ({default: jest.fn(() => ({WIDGET_FETCH_FAILED: "Widget fetch failed",WIDGET_RUN_FAILED: "Widget run failed",+ WIDGET_STRUCTURED_QUERY_FAILED: "Structured widget query failed.",+ WIDGET_NO_STRUCTURED_QUERY: "No structured query.",+ WIDGET_RAW_SQL_SOURCE_ONLY: (source: string) => `raw-sql-only:${source}`,})),}));+const mockResolveDescriptor = jest.fn();+const mockSourceSupportsNativeSql = jest.fn();+const mockGetTelemetryAdapter = jest.fn();+jest.mock("@/lib/telemetry-source", () => ({+ resolveTelemetrySourceDescriptor: (...a: unknown[]) => mockResolveDescriptor(...a),+ sourceSupportsNativeSql: (...a: unknown[]) => mockSourceSupportsNativeSql(...a),+ getTelemetryAdapter: (...a: unknown[]) => mockGetTelemetryAdapter(...a),+}));+jest.mock("@/lib/platform/coding-agents/source", () => ({+ isCodingAgentClickHouseSql: () => false,+ resolveCodingAgentsClickHouseDbConfigId: jest.fn(),+}));jest.mock("@/utils/sanitizer", () => ({__esModule: true,default: {@@ -42,6 +57,12 @@ import { escapeSingleQuotes } from "@/helpers/server/widget";beforeEach(() => {jest.clearAllMocks();(dataCollector as jest.Mock).mockResolvedValue({ data: [], err: null });+ mockResolveDescriptor.mockResolvedValue({+ type: "clickhouse",+ name: "Built-in",+ isBuiltIn: true,+ });+ mockSourceSupportsNativeSql.mockReturnValue(true);});describe("runWidgetQuery", () => {@@ -137,7 +158,9 @@ describe("runWidgetQuery", () => {expect(result).toEqual({ data: [{ count: 1 }] });expect(dataCollector).toHaveBeenLastCalledWith(- { query: "SELECT count() FROM otel_traces", enable_readonly: true }+ { query: "SELECT count() FROM otel_traces", enable_readonly: true },+ "query",+ undefined);});@@ -160,10 +183,14 @@ describe("runWidgetQuery", () => {const result = await runWidgetQuery("w1", { filter: {} as any });expect(result).toEqual({ data: [{ provider: "openai", count: 5 }] });- expect(dataCollector).toHaveBeenLastCalledWith({- query,- enable_readonly: true,- });+ expect(dataCollector).toHaveBeenLastCalledWith(+ {+ query,+ enable_readonly: true,+ },+ "query",+ undefined+ );});it("does not flag blocklisted keywords that appear only inside string literals", async () => {@@ -250,10 +277,14 @@ describe("runWidgetQuery", () => {});expect(result).toEqual({ data: [{ n: 1 }] });- expect(dataCollector).toHaveBeenLastCalledWith({- query: "SELECT 1 WHERE ts >= '2024-01-01' AND env = 'prod'",- enable_readonly: true,- });+ expect(dataCollector).toHaveBeenLastCalledWith(+ {+ query: "SELECT 1 WHERE ts >= '2024-01-01' AND env = 'prod'",+ enable_readonly: true,+ },+ "query",+ undefined+ );});it("ignores non-filter mustache-like tags so they cannot run as code", async () => {@@ -270,13 +301,200 @@ describe("runWidgetQuery", () => {});expect(result).toEqual({ data: [] });- expect(dataCollector).toHaveBeenLastCalledWith({- query: "SELECT '{{#evil}}{{/evil}}' AS x, 'safe' AS y",- enable_readonly: true,- });+ expect(dataCollector).toHaveBeenLastCalledWith(+ {+ query: "SELECT '{{#evil}}{{/evil}}' AS x, 'safe' AS y",+ enable_readonly: true,+ },+ "query",+ undefined+ );});it("mock escapeSingleQuotes escapes backslashes before quotes", () => {expect(escapeSingleQuotes("a\\b'c")).toBe("a\\\\b\\'c");});});++describe("runWidgetQuery source routing", () => {+ it("rejects raw SQL on an external source", async () => {+ (dataCollector as jest.Mock).mockResolvedValueOnce({+ data: [+ {+ id: "w1",+ config: JSON.stringify({+ query: "SELECT name FROM system.tables",+ sourceId: "src-dd",+ }),+ },+ ],+ err: null,+ });+ mockResolveDescriptor.mockResolvedValue({ type: "datadog", name: "Prod DD" });+ mockSourceSupportsNativeSql.mockReturnValue(false);++ const result = await runWidgetQuery("w1", {+ userQuery: "SELECT name FROM system.tables",+ filter: {} as any,+ });++ expect(result).toEqual({ err: "raw-sql-only:Prod DD" });+ // Only the widget fetch hit ClickHouse; no query execution.+ expect(dataCollector).toHaveBeenCalledTimes(1);+ });++ it("executes a structured query against the external adapter", async () => {+ (dataCollector as jest.Mock).mockResolvedValueOnce({+ data: [+ {+ id: "w1",+ config: JSON.stringify({+ sourceId: "src-tempo",+ structuredQuery: {+ mode: "timeseries",+ query: { signal: "traces" },+ },+ }),+ },+ ],+ err: null,+ });+ mockResolveDescriptor.mockResolvedValue({ type: "tempo", name: "Prod Tempo" });+ mockSourceSupportsNativeSql.mockReturnValue(false);+ const spanTimeSeries = jest+ .fn()+ .mockResolvedValue({ fields: [], rows: [{ bucket: "t0", agg0: 5 }] });+ mockGetTelemetryAdapter.mockResolvedValue({+ type: "tempo",+ capabilities: () => ({ serverAggregation: true }),+ spanTimeSeries,+ });++ const result = await runWidgetQuery("w1", {+ filter: { timeLimit: { start: "2026-07-01", end: "2026-07-02" } } as any,+ });++ expect(result).toEqual({ data: [{ bucket: "t0", agg0: 5 }] });+ expect(spanTimeSeries).toHaveBeenCalledTimes(1);+ expect(mockResolveDescriptor).toHaveBeenCalledWith(+ expect.objectContaining({ sourceId: "src-tempo", signal: "traces" })+ );+ });++ it("follows project binding when a signal widget has no sourceId", async () => {+ (dataCollector as jest.Mock).mockResolvedValueOnce({+ data: [+ {+ id: "w1",+ config: JSON.stringify({+ structuredQuery: {+ mode: "timeseries",+ query: { signal: "traces" },+ },+ }),+ },+ ],+ err: null,+ });+ mockResolveDescriptor.mockResolvedValue({ type: "tempo", name: "Prod Tempo" });+ mockSourceSupportsNativeSql.mockReturnValue(false);+ const spanTimeSeries = jest+ .fn()+ .mockResolvedValue({ fields: [], rows: [{ bucket: "t0", agg0: 5 }] });+ mockGetTelemetryAdapter.mockResolvedValue({+ type: "tempo",+ capabilities: () => ({ serverAggregation: true }),+ spanTimeSeries,+ });++ await runWidgetQuery("w1", {+ filter: { timeLimit: { start: "2026-07-01", end: "2026-07-02" } } as any,+ });++ expect(mockResolveDescriptor).toHaveBeenCalledWith(+ expect.objectContaining({ sourceId: null, signal: "traces" })+ );+ });++ it("threads the resolved dbConfigId for a built-in source override", async () => {+ (dataCollector as jest.Mock)+ .mockResolvedValueOnce({+ data: [+ {+ id: "w1",+ config: JSON.stringify({+ query: "SELECT count() FROM otel_traces",+ sourceId: "src-ch2",+ }),+ },+ ],+ err: null,+ })+ .mockResolvedValueOnce({ data: [{ count: 2 }], err: null });+ mockResolveDescriptor.mockResolvedValue({+ type: "clickhouse",+ name: "Other CH",+ dbConfigId: "db-9",+ });+ mockSourceSupportsNativeSql.mockReturnValue(true);++ const result = await runWidgetQuery("w1", { filter: {} as any });++ expect(result).toEqual({ data: [{ count: 2 }] });+ expect(dataCollector).toHaveBeenLastCalledWith(+ { query: "SELECT count() FROM otel_traces", enable_readonly: true },+ "query",+ "db-9"+ );+ });++ it("bridges legacy otel_traces SQL to the project external traces source", async () => {+ (dataCollector as jest.Mock).mockResolvedValueOnce({+ data: [+ {+ id: "w1",+ config: JSON.stringify({+ query: `SELECT CAST(countIf(Timestamp >= start_time) AS INTEGER) AS total_request,+ CAST(countIf(Timestamp >= prev_start_time) AS INTEGER) AS total_request_previous+ FROM otel_traces WHERE 1=1`,+ }),+ },+ ],+ err: null,+ });+ mockResolveDescriptor.mockResolvedValue({+ type: "tempo",+ name: "Grafana Tempo",+ isBuiltIn: false,+ });+ mockSourceSupportsNativeSql.mockReturnValue(false);+ const aggregateSpans = jest.fn().mockResolvedValue({+ fields: [],+ rows: [{ total_request: 12, count: 12 }],+ });+ mockGetTelemetryAdapter.mockResolvedValue({ aggregateSpans });++ const result = await runWidgetQuery("w1", {+ filter: {+ timeLimit: {+ start: "2026-07-01T00:00:00.000Z",+ end: "2026-07-02T00:00:00.000Z",+ },+ selectedConfig: { serviceNames: ["demo-openai-app"] },+ } as any,+ });++ expect(result.err).toBeUndefined();+ expect(result.data).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ total_request: expect.any(Number),+ total_request_previous: expect.any(Number),+ }),+ ])+ );+ // Widget fetch only — no ClickHouse SQL execution for the metric.+ expect(dataCollector).toHaveBeenCalledTimes(1);+ expect(mockGetTelemetryAdapter).toHaveBeenCalled();+ });+});diff --git a/src/client/src/__tests__/lib/platform/metrics/read.test.ts b/src/client/src/__tests__/lib/platform/metrics/read.test.tsnew file mode 100644index 00000000..0e4d498b--- /dev/null+++ b/src/client/src/__tests__/lib/platform/metrics/read.test.ts@@ -0,0 +1,245 @@+const mockGetMetrics = jest.fn();+const mockGetMetricsConfig = jest.fn();+const mockGetMetricAttributeKeys = jest.fn();+const mockGetMetricDetail = jest.fn();+const mockGetSignalSummary = jest.fn();+const mockResolveCtx = jest.fn();++jest.mock("@/lib/platform/observability", () => ({+ getMetrics: (...a: unknown[]) => mockGetMetrics(...a),+ getMetricsConfig: (...a: unknown[]) => mockGetMetricsConfig(...a),+ getMetricAttributeKeys: (...a: unknown[]) => mockGetMetricAttributeKeys(...a),+ getMetricDetail: (...a: unknown[]) => mockGetMetricDetail(...a),+ getSignalSummary: (...a: unknown[]) => mockGetSignalSummary(...a),+ getSummaryBucket: () => "hour",+}));++jest.mock("@/lib/platform/connectors/datasource/facade", () => ({+ resolveSignalReadContext: (...a: unknown[]) => mockResolveCtx(...a),+ facadeErrorMessage: (e: unknown) => (e instanceof Error ? e.message : String(e)),+ rethrowIfSourceFailure: (e: unknown) => {+ const { AdapterError } = require("@openplait/adapter-sdk");+ if (e instanceof AdapterError) throw e;+ },+}));++import { listMetricRecords, getMetricsSummary, getMetricsFilterConfig, getMetricAttributeKeysRecord } from "@/lib/platform/metrics/read";+import { AdapterError } from "@openplait/adapter-sdk";++const params = {+ timeLimit: {+ start: new Date("2026-07-01T00:00:00.000Z"),+ end: new Date("2026-07-01T01:00:00.000Z"),+ type: "CUSTOM",+ },+ limit: 25,+ offset: 0,+ selectedConfig: {},+} as never;++beforeEach(() => jest.clearAllMocks());++describe("listMetricRecords", () => {+ it("uses the same adapter contract for the built-in ClickHouse source", async () => {+ const listMetricSeries = jest.fn().mockResolvedValue({+ fields: [],+ rows: [+ {+ metricName: "m",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:00.000Z",+ value: 10,+ attributes: {},+ resourceAttributes: {},+ },+ ],+ });+ mockResolveCtx.mockResolvedValue({+ adapter: { listMetricSeries },+ descriptor: { dbConfigId: "db-1" },+ isBuiltIn: true,+ });++ const res = await listMetricRecords(params);+ expect(listMetricSeries).toHaveBeenCalled();+ expect(res).toMatchObject({+ err: null,+ records: [expect.objectContaining({ metricName: "m", latestValue: 10 })],+ });+ expect(mockGetMetrics).not.toHaveBeenCalled();+ });++ it("folds external metric points into grouped list rows", async () => {+ const listMetricSeries = jest.fn().mockResolvedValue({+ fields: [],+ rows: [+ {+ metricName: "m",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:00.000Z",+ value: 10,+ attributes: {},+ resourceAttributes: {},+ },+ {+ metricName: "m",+ serviceName: "api",+ timestamp: "2026-07-01T00:01:00.000Z",+ value: 20,+ attributes: {},+ resourceAttributes: {},+ },+ ],+ });+ mockResolveCtx.mockResolvedValue({+ adapter: { listMetricSeries },+ descriptor: {},+ isBuiltIn: false,+ });++ const res = await listMetricRecords(params);+ expect(listMetricSeries).toHaveBeenCalled();+ expect(res.err).toBeNull();+ expect(res.records).toHaveLength(1);+ expect(res.records).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ metricName: "m",+ latestValue: 20,+ avgValue: 15,+ }),+ ])+ );+ expect(mockGetMetrics).not.toHaveBeenCalled();+ });++ it("rethrows AdapterError so routes can return 503", async () => {+ const listMetricSeries = jest+ .fn()+ .mockRejectedValue(new AdapterError("EXECUTION_FAILED", "prometheus down"));+ mockResolveCtx.mockResolvedValue({+ adapter: { listMetricSeries },+ descriptor: {},+ isBuiltIn: false,+ });++ await expect(listMetricRecords(params)).rejects.toBeInstanceOf(AdapterError);+ });+});++describe("getMetricsSummary", () => {+ it("requests a count aggregation and maps value to count", async () => {+ const metricTimeSeries = jest.fn().mockResolvedValue({+ fields: [],+ rows: [+ { timestamp: "2026-07-01T00:00:00.000Z", value: 12 },+ { timestamp: "2026-07-01T00:00:00.000Z", value: 3 },+ { timestamp: "2026-07-01T01:00:00.000Z", value: 8 },+ ],+ });+ mockResolveCtx.mockResolvedValue({+ adapter: { metricTimeSeries },+ descriptor: {},+ isBuiltIn: false,+ });++ const res = await getMetricsSummary(params);+ expect(metricTimeSeries).toHaveBeenCalledWith(+ expect.objectContaining({+ aggregations: [{ fn: "count", field: "value" }],+ })+ );+ expect(res).toMatchObject({+ err: null,+ total: 23,+ peak: 15,+ buckets: [+ { label: "2026-07-01T00:00:00.000Z", count: 15 },+ { label: "2026-07-01T01:00:00.000Z", count: 8 },+ ],+ });+ });++ it("keeps ClickHouse-style count buckets", async () => {+ const metricTimeSeries = jest.fn().mockResolvedValue({+ fields: [],+ rows: [{ label: "2026-07-01T00:00:00.000Z", count: 4, metrics: 2, services: 1 }],+ });+ mockResolveCtx.mockResolvedValue({+ adapter: { metricTimeSeries },+ descriptor: { dbConfigId: "db-1" },+ isBuiltIn: true,+ });++ const res = await getMetricsSummary(params);+ expect(res).toMatchObject({+ err: null,+ total: 4,+ peak: 4,+ buckets: [{ label: "2026-07-01T00:00:00.000Z", count: 4 }],+ });+ });+});++describe("getMetricsFilterConfig", () => {+ it("returns services and metric names for the filter bar", async () => {+ mockResolveCtx.mockResolvedValue({+ adapter: {+ capabilities: () => ({ distinctValues: true }),+ metricNames: jest.fn().mockResolvedValue(["up", "go_goroutines"]),+ distinctValues: jest.fn().mockResolvedValue(["prometheus-local"]),+ },+ descriptor: {},+ isBuiltIn: false,+ });++ const res = await getMetricsFilterConfig(params);+ expect(res).toEqual({+ err: null,+ data: [+ {+ services: ["prometheus-local"],+ metricNames: ["go_goroutines", "up"],+ metricTypes: [],+ totalRows: 0,+ },+ ],+ });+ });++ it("falls back to job when service.name is empty", async () => {+ const distinctValues = jest.fn(async (key: string) =>+ key === "service.name" ? [] : key === "job" ? ["prometheus"] : []+ );+ mockResolveCtx.mockResolvedValue({+ adapter: {+ capabilities: () => ({ distinctValues: true }),+ metricNames: jest.fn().mockResolvedValue(["up"]),+ distinctValues,+ },+ descriptor: {},+ isBuiltIn: false,+ });++ const res = await getMetricsFilterConfig(params);+ expect(res.data?.[0]?.services).toEqual(["prometheus"]);+ });+});++describe("getMetricAttributeKeysRecord", () => {+ it("exposes Prometheus labels under metric and resource attribute keys", async () => {+ mockResolveCtx.mockResolvedValue({+ adapter: {+ attributeKeys: jest.fn().mockResolvedValue(["service_name", "job"]),+ },+ descriptor: {},+ isBuiltIn: false,+ });++ const res = await getMetricAttributeKeysRecord(params);+ expect(res).toMatchObject({+ metricAttributeKeys: ["service_name", "job"],+ resourceAttributeKeys: ["service_name", "job"],+ });+ });+});diff --git a/src/client/src/__tests__/lib/platform/openplait/openplait.test.ts b/src/client/src/__tests__/lib/platform/openplait/openplait.test.tsnew file mode 100644index 00000000..41d053ec--- /dev/null+++ b/src/client/src/__tests__/lib/platform/openplait/openplait.test.ts@@ -0,0 +1,57 @@+import {+ DIRECT_INTELLIGENCE_READ_FEATURES,+ OPENPLAIT_CLICKHOUSE_READ_FEATURES,+} from "@/lib/platform/openplait/features";+import { openPlaitFramesToRows } from "@/lib/platform/openplait/frames";+import { normalizeOpenPlaitReadStatement } from "@/lib/platform/openplait/native";++describe("OpenPlait ClickHouse integration", () => {+ it("normalizes a trailing SQL terminator for OpenPlait native mode", () => {+ expect(normalizeOpenPlaitReadStatement(" SELECT 1; ")).toBe("SELECT 1");+ expect(normalizeOpenPlaitReadStatement("SELECT ';' AS value")).toBe(+ "SELECT ';' AS value"+ );+ });++ it("converts columnar OpenPlait frames to legacy OpenLIT rows", () => {+ expect(+ openPlaitFramesToRows([+ {+ name: "telemetry",+ length: 2,+ fields: [+ { name: "TraceId", type: "string", values: ["a", "b"] },+ { name: "Duration", type: "number", values: [10, 20] },+ ],+ },+ ])+ ).toEqual([+ { TraceId: "a", Duration: 10 },+ { TraceId: "b", Duration: 20 },+ ]);+ });++ it("separates routed product reads from direct intelligence reads", () => {+ expect(OPENPLAIT_CLICKHOUSE_READ_FEATURES).toEqual(+ expect.arrayContaining([+ "traces",+ "logs",+ "metrics",+ "telemetry",+ "openground",+ "rule-engine",+ "evaluations",+ "dashboards",+ "alerts",+ ])+ );+ expect(DIRECT_INTELLIGENCE_READ_FEATURES).toEqual(+ expect.arrayContaining([+ "otter",+ "ai-analysis",+ "coding-agents",+ "telemetry-rollups",+ ])+ );+ });+});diff --git a/src/client/src/__tests__/lib/platform/pricing/pricing.test.ts b/src/client/src/__tests__/lib/platform/pricing/pricing.test.tsindex 5105883d..b39a47d1 100644--- a/src/client/src/__tests__/lib/platform/pricing/pricing.test.ts+++ b/src/client/src/__tests__/lib/platform/pricing/pricing.test.ts@@ -17,6 +17,22 @@ jest.mock('@/lib/platform/common', () => ({jest.mock('@/lib/platform/request', () => ({getRequestViaSpanId: jest.fn(),}));+jest.mock('@/lib/platform/traces/read', () => ({+ getTraceSpanRecord: (...args: unknown[]) =>+ require('@/lib/platform/request').getRequestViaSpanId(...args),+}));+jest.mock('@/lib/telemetry-source', () => ({+ resolveTelemetrySourceDescriptor: jest.fn().mockResolvedValue({+ type: 'clickhouse',+ id: 'builtin:test',+ isBuiltIn: true,+ }),+ getTelemetryAdapterForDbConfig: jest.fn().mockResolvedValue({+ isBuiltIn: true,+ descriptor: { type: 'clickhouse', isBuiltIn: true },+ adapter: {},+ }),+}));jest.mock('@/lib/platform/providers/provider-registry', () => ({ProviderRegistry: {getModel: jest.fn(),@@ -111,6 +127,18 @@ beforeEach(() => {// Re-apply prisma mock for setPricingForSpanId's dynamic importconst prisma = require('@/lib/prisma').default;prisma.pricingConfigs.findFirst.mockResolvedValue({ databaseConfigId: 'db-1' });++ const { resolveTelemetrySourceDescriptor, getTelemetryAdapterForDbConfig } = require('@/lib/telemetry-source');+ (resolveTelemetrySourceDescriptor as jest.Mock).mockResolvedValue({+ type: 'clickhouse',+ id: 'builtin:test',+ isBuiltIn: true,+ });+ (getTelemetryAdapterForDbConfig as jest.Mock).mockResolvedValue({+ isBuiltIn: true,+ descriptor: { type: 'clickhouse', isBuiltIn: true },+ adapter: {},+ });});describe('setPricingForSpanId', () => {@@ -512,4 +540,57 @@ describe('autoUpdatePricing', () => {'db-1');});++ it('reads candidates from the routed traces connector and does not write otel_traces', async () => {+ const listSpans = jest.fn().mockResolvedValue({+ rows: [+ {+ spanId: 'jaeger-span',+ timestamp: '2026-01-01',+ spanAttributes: {+ 'gen_ai.system': 'openai',+ 'gen_ai.request.model': 'gpt-4o',+ 'gen_ai.usage.input_tokens': '100',+ 'gen_ai.usage.output_tokens': '200',+ },+ },+ ],+ });+ const { getTelemetryAdapterForDbConfig } = require('@/lib/telemetry-source');+ (getTelemetryAdapterForDbConfig as jest.Mock).mockResolvedValue({+ isBuiltIn: false,+ descriptor: { type: 'jaeger', isBuiltIn: false },+ adapter: { listSpans },+ });+ (getPricingConfigById as jest.Mock).mockResolvedValue({+ id: 'pc-1',+ databaseConfigId: 'db-1',+ });+ (asaw as jest.Mock).mockResolvedValue([null, { id: 'db-1' }]);+ (ProviderRegistry.getModel as jest.Mock).mockResolvedValue({+ id: 'gpt-4o',+ inputPricePerMToken: 2.5,+ outputPricePerMToken: 10.0,+ });++ const result = await autoUpdatePricing({+ pricingConfigId: 'pc-1',+ cronId: 'cron-1',+ });++ expect(result.success).toBe(true);+ expect(listSpans).toHaveBeenCalled();+ const writes = (dataCollector as jest.Mock).mock.calls.filter(+ ([params, mode]) =>+ mode === 'exec' ||+ (typeof params?.query === 'string' && params.query.includes('ALTER TABLE'))+ );+ expect(writes).toHaveLength(0);+ expect(insertCronLog).toHaveBeenCalledWith(+ expect.objectContaining({+ meta: expect.objectContaining({ totalUpdated: 0 }),+ }),+ 'db-1'+ );+ });});diff --git a/src/client/src/__tests__/lib/platform/request/request.test.ts b/src/client/src/__tests__/lib/platform/request/request.test.tsindex 6d2d2d9b..a85aec22 100644--- a/src/client/src/__tests__/lib/platform/request/request.test.ts+++ b/src/client/src/__tests__/lib/platform/request/request.test.ts@@ -101,6 +101,27 @@ describe('getRequestsConfig', () => {});describe('getRequests', () => {+ it('keeps the selected database configuration on every ClickHouse read', async () => {+ (dataCollector as jest.Mock)+ .mockResolvedValueOnce({ data: [{ total: 1 }], err: null })+ .mockResolvedValueOnce({ data: [], err: null });++ await getRequests({ ...baseParams, databaseConfigId: 'db-1' });++ expect(dataCollector).toHaveBeenNthCalledWith(+ 1,+ expect.any(Object),+ 'query',+ 'db-1'+ );+ expect(dataCollector).toHaveBeenNthCalledWith(+ 2,+ expect.any(Object),+ 'query',+ 'db-1'+ );+ });+it('calls dataCollector twice (count + data) and returns records', async () => {(dataCollector as jest.Mock).mockResolvedValueOnce({ data: [{ total: 42 }], err: null })@@ -231,7 +252,13 @@ describe('getHeirarchyViaSpanId', () => {const result = await getHeirarchyViaSpanId('orphan');- expect(result).toEqual({ err: 'Error building hierarchy', record: {} });+ expect(result.err).toBeNull();+ expect(result.record).toMatchObject({+ SpanId: 'orphan',+ ParentSpanId: 'missing-parent',+ TraceId: 't1',+ children: [],+ });});it('unions spans across subagent sessions when source span is a subagent', async () => {diff --git a/src/client/src/__tests__/lib/platform/rule-engine/rule-engine.test.ts b/src/client/src/__tests__/lib/platform/rule-engine/rule-engine.test.tsindex ce17dc3d..bea742c4 100644--- a/src/client/src/__tests__/lib/platform/rule-engine/rule-engine.test.ts+++ b/src/client/src/__tests__/lib/platform/rule-engine/rule-engine.test.ts@@ -183,6 +183,12 @@ describe('createRule', () => {const [{ query }] = (dataCollector as jest.Mock).mock.calls[0];expect(query).toContain('INSERT INTO openlit_rules');});++ it('routes creation to the selected database config', async () => {+ (dataCollector as jest.Mock).mockResolvedValue({ err: null, data: {} });+ await createRule({ name: 'r' }, { databaseConfigId: 'db-2' });+ expect(dataCollector).toHaveBeenCalledWith(expect.any(Object), 'exec', 'db-2');+ });});// ---------------------------------------------------------------------------@@ -420,4 +426,9 @@ describe('getRuleEntities', () => {const result = await getRuleEntities({ rule_id: 'r1' });expect(result).toEqual({ data: [{ id: 'e1' }], err: null });});++ it('routes reads to the selected database config', async () => {+ await getRuleEntities({ rule_id: 'r1' }, 'db-2');+ expect(dataCollector).toHaveBeenCalledWith(expect.any(Object), 'query', 'db-2');+ });});diff --git a/src/client/src/__tests__/lib/platform/rule-engine/source.test.ts b/src/client/src/__tests__/lib/platform/rule-engine/source.test.tsnew file mode 100644index 00000000..ed7adb1a--- /dev/null+++ b/src/client/src/__tests__/lib/platform/rule-engine/source.test.ts@@ -0,0 +1,86 @@+jest.mock("@/lib/telemetry-source", () => ({+ resolveSignalSource: jest.fn(),+}));+jest.mock("@/lib/db-config", () => ({+ getDBConfigByUser: jest.fn(),+}));++import { resolveSignalSource } from "@/lib/telemetry-source";+import { getDBConfigByUser } from "@/lib/db-config";+import { resolveRuleEngineDatabaseConfigId } from "@/lib/platform/rule-engine/source";++function requestWithContext({+ environment,+ databaseConfigId,+}: {+ environment?: string;+ databaseConfigId?: string;+} = {}): Request {+ return {+ headers: {+ get: jest.fn((name: string) => {+ if (name === "x-openlit-environment") return environment || null;+ if (name === "x-openlit-database-config-id") {+ return databaseConfigId || null;+ }+ return null;+ }),+ },+ } as unknown as Request;+}++describe("resolveRuleEngineDatabaseConfigId", () => {+ beforeEach(() => jest.clearAllMocks());++ it("uses the selected environment's intelligence ClickHouse binding", async () => {+ (resolveSignalSource as jest.Mock).mockResolvedValue({+ hasSource: true,+ descriptor: { type: "clickhouse", dbConfigId: "db-production" },+ });++ await expect(+ resolveRuleEngineDatabaseConfigId(requestWithContext({ environment: "production" }))+ ).resolves.toBe("db-production");+ expect(resolveSignalSource).toHaveBeenCalledWith("intelligence", {+ environment: "production",+ });+ });++ it("prefers the explicitly selected project ClickHouse", async () => {+ (getDBConfigByUser as jest.Mock).mockResolvedValue([+ { id: "db-selected" },+ { id: "db-other" },+ ]);++ await expect(+ resolveRuleEngineDatabaseConfigId(+ requestWithContext({+ environment: "production",+ databaseConfigId: "db-selected",+ })+ )+ ).resolves.toBe("db-selected");+ expect(resolveSignalSource).not.toHaveBeenCalled();+ });++ it("rejects an explicit ClickHouse outside the current project", async () => {+ (getDBConfigByUser as jest.Mock).mockResolvedValue([{ id: "db-allowed" }]);++ await expect(+ resolveRuleEngineDatabaseConfigId(+ requestWithContext({ databaseConfigId: "db-forged" })+ )+ ).rejects.toThrow("not available in the current project");+ });++ it("fails closed when the selected environment has no ClickHouse", async () => {+ (resolveSignalSource as jest.Mock).mockResolvedValue({+ hasSource: false,+ descriptor: { type: "clickhouse", dbConfigId: undefined },+ });++ await expect(+ resolveRuleEngineDatabaseConfigId(requestWithContext({ environment: "staging" }))+ ).rejects.toThrow("requires a ClickHouse datasource");+ });+});diff --git a/src/client/src/__tests__/lib/platform/rule-engine/telemetry.test.ts b/src/client/src/__tests__/lib/platform/rule-engine/telemetry.test.tsnew file mode 100644index 00000000..fd162bb2--- /dev/null+++ b/src/client/src/__tests__/lib/platform/rule-engine/telemetry.test.ts@@ -0,0 +1,41 @@+jest.mock("@/lib/platform/traces/read", () => ({+ listTraceRecords: jest.fn(),+}));++import { listTraceRecords } from "@/lib/platform/traces/read";+import {+ getRuleTraceFieldValue,+ listRecentRuleTraces,+} from "@/lib/platform/rule-engine/telemetry";++describe("listRecentRuleTraces", () => {+ it("uses ClickHouse-compatible ISO timestamps for preview", async () => {+ (listTraceRecords as jest.Mock).mockResolvedValue({+ err: null,+ records: [],+ });++ await listRecentRuleTraces(100, "production");++ const params = (listTraceRecords as jest.Mock).mock.calls[0][0];+ expect(params.timeLimit.start).toMatch(/^\d{4}-\d{2}-\d{2}T.*Z$/);+ expect(params.timeLimit.end).toMatch(/^\d{4}-\d{2}-\d{2}T.*Z$/);+ expect(+ new Date(params.timeLimit.end).getTime() -+ new Date(params.timeLimit.start).getTime()+ ).toBe(30 * 24 * 60 * 60 * 1000);+ expect(params.environment).toBe("production");+ });++ it("reads deployment.environment from the OTel resource attribute", () => {+ expect(+ getRuleTraceFieldValue(+ {+ ResourceAttributes: { "deployment.environment": "production" },+ SpanAttributes: { "gen_ai.environment": "legacy" },+ },+ "deployment.environment"+ )+ ).toBe("production");+ });+});diff --git a/src/client/src/__tests__/lib/platform/telemetry/rollups.test.ts b/src/client/src/__tests__/lib/platform/telemetry/rollups.test.tsnew file mode 100644index 00000000..b8b1f7ab--- /dev/null+++ b/src/client/src/__tests__/lib/platform/telemetry/rollups.test.ts@@ -0,0 +1,92 @@+const mockDataCollector = jest.fn();+const mockLogError = jest.fn();++jest.mock("@/lib/platform/common", () => ({+ intelligenceDataCollector: (...args: unknown[]) => mockDataCollector(...args),+}));++jest.mock("@/lib/platform/agents/logger", () => ({+ agentsLogger: { error: (...args: unknown[]) => mockLogError(...args) },+}));++import { materializeTelemetryRollups } from "@/lib/platform/telemetry/rollups";++const rootSpan = {+ traceId: "trace-1",+ spanId: "span-1",+ parentSpanId: "",+ name: "chat",+ serviceName: "checkout",+ timestamp: new Date().toISOString(),+ durationNs: 1_000_000_000,+ statusCode: "STATUS_CODE_OK",+ spanAttributes: {+ "gen_ai.request.model": "gpt-4o",+ "gen_ai.system": "openai",+ "gen_ai.operation.name": "chat",+ "gen_ai.usage.cost": "0.01",+ "gen_ai.usage.total_tokens": "25",+ "deployment.environment": "production",+ },+ resourceAttributes: {+ "service.name": "checkout",+ "deployment.environment": "production",+ },+};++describe("external telemetry rollup materialization", () => {+ beforeEach(() => {+ jest.clearAllMocks();+ mockDataCollector.mockResolvedValue({ data: [], err: null });+ });++ it("reuses one connector sample for buckets, dimensions, and hot cache", async () => {+ const sampleTracesForGraph = jest.fn().mockResolvedValue([rootSpan]);+ const adapter = {+ sampleCacheKey: `rollup-success-${Date.now()}`,+ sampleTracesForGraph,+ discoverServices: jest+ .fn()+ .mockResolvedValue([+ { serviceName: "checkout", environment: "production", clusterId: "default" },+ ]),+ };++ const result = await materializeTelemetryRollups({+ adapter: adapter as never,+ sourceId: "tempo-1",+ dbConfigId: "db-1",+ });++ expect(sampleTracesForGraph).toHaveBeenCalledTimes(1);+ expect(result.buckets).toBeGreaterThan(0);+ expect(result.llmRows).toBe(5);+ expect(result.hotCacheRows).toBe(1);+ expect(mockDataCollector).toHaveBeenCalledTimes(7);+ });++ it("stops the cycle after one rejected connector sample", async () => {+ const sampleTracesForGraph = jest+ .fn()+ .mockRejectedValue(new Error("Tempo returned HTTP 429"));+ const adapter = {+ sampleCacheKey: `rollup-failure-${Date.now()}`,+ sampleTracesForGraph,+ };++ await expect(+ materializeTelemetryRollups({+ adapter: adapter as never,+ sourceId: "tempo-1",+ dbConfigId: "db-1",+ })+ ).resolves.toEqual({ buckets: 0, llmRows: 0, hotCacheRows: 0 });++ expect(sampleTracesForGraph).toHaveBeenCalledTimes(1);+ expect(mockDataCollector).not.toHaveBeenCalled();+ expect(mockLogError).toHaveBeenCalledWith(+ "telemetry_materialization_sample_failed",+ expect.any(Object)+ );+ });+});diff --git a/src/client/src/__tests__/lib/platform/traces/read.test.ts b/src/client/src/__tests__/lib/platform/traces/read.test.tsnew file mode 100644index 00000000..da1b16e2--- /dev/null+++ b/src/client/src/__tests__/lib/platform/traces/read.test.ts@@ -0,0 +1,539 @@+const mockGetRequests = jest.fn();+const mockGetRequestViaSpanId = jest.fn();+const mockGetRequestViaTraceId = jest.fn();+const mockGetHeirarchyViaSpanId = jest.fn();+const mockResolveDescriptor = jest.fn();+const mockGetAdapter = jest.fn();++const mockGetRequestsConfig = jest.fn();+const mockGetGroupedRequests = jest.fn();+const mockGetAttributeKeys = jest.fn();+const mockGetSignalSummary = jest.fn();++jest.mock("@/lib/platform/request", () => ({+ getRequests: (...a: unknown[]) => mockGetRequests(...a),+ getRequestViaSpanId: (...a: unknown[]) => mockGetRequestViaSpanId(...a),+ getRequestViaTraceId: (...a: unknown[]) => mockGetRequestViaTraceId(...a),+ getHeirarchyViaSpanId: (...a: unknown[]) => mockGetHeirarchyViaSpanId(...a),+ getRequestsConfig: (...a: unknown[]) => mockGetRequestsConfig(...a),+ getGroupedRequests: (...a: unknown[]) => mockGetGroupedRequests(...a),+ getAttributeKeys: (...a: unknown[]) => mockGetAttributeKeys(...a),+ getTotalRequests: jest.fn(),+ getRequestPerTime: jest.fn(),+ getAverageRequestDuration: jest.fn(),+ getRequestExist: jest.fn(),+}));++jest.mock("@/helpers/server/platform", () => ({+ getFilterPreviousParams: (p: unknown) => p,+ dateTruncGroupingLogic: () => "hour",+}));++jest.mock("@/lib/platform/observability", () => ({+ getSignalSummary: (...a: unknown[]) => mockGetSignalSummary(...a),+ getSummaryBucket: () => "hour",+}));++jest.mock("@/lib/telemetry-source", () => ({+ resolveTelemetrySourceDescriptor: (...a: unknown[]) =>+ mockResolveDescriptor(...a),+ getTelemetryAdapter: (...a: unknown[]) => mockGetAdapter(...a),+}));++jest.mock("@/helpers/server/trace", () => ({+ buildHierarchy: (rows: unknown[]) =>+ rows.length ? { SpanId: (rows[0] as { SpanId: string }).SpanId, children: [] } : null,+}));++jest.mock("@/lib/platform/connectors/datasource/http/cache", () => ({+ cacheKey: (...parts: unknown[]) => parts.join(":"),+ cachedQuery: (_key: string, _ttl: number, loader: () => unknown) => loader(),+ __clearCache: jest.fn(),+}));++jest.mock("@/lib/platform/telemetry/rollups", () => ({+ readSignalBucketRollup: jest.fn().mockResolvedValue(null),+ readLlmRollup: jest.fn().mockResolvedValue(null),+ readSpanHotCache: jest.fn().mockResolvedValue(null),+ materializeTelemetryRollups: jest.fn(),+ SIGNAL_BUCKETS_TABLE: "openlit_signal_buckets",+ LLM_ROLLUPS_TABLE: "openlit_llm_rollups",+ SPAN_HOT_CACHE_TABLE: "openlit_external_span_cache",+ ROLLUP_FRESHNESS_MS: 300000,+}));++import {+ getTraceAverageDuration,+ getTraceExist,+ getTraceHierarchy,+ getTraceRecordByTraceId,+ getTraceRequestPerTime,+ getTraceSummary,+ getTraceSpanRecord,+ getTraceTotalRequests,+ listTraceRecords,+} from "@/lib/platform/traces/read";+import { AdapterError } from "@openplait/adapter-sdk";++const builtin = {+ type: "clickhouse",+ id: "builtin:db-1",+ isBuiltIn: true,+ settings: {},+ signals: ["traces", "logs", "metrics"],+ name: "CH",+ dbConfigId: "db-1",+};++const tempo = {+ type: "tempo",+ id: "src-tempo",+ isBuiltIn: false,+ settings: { url: "https://tempo.example.com" },+ signals: ["traces"],+ name: "Tempo",+};++const params = {+ timeLimit: {+ start: new Date("2026-07-01T00:00:00.000Z"),+ end: new Date("2026-07-01T01:00:00.000Z"),+ type: "CUSTOM",+ },+ limit: 10,+ offset: 0,+ selectedConfig: { models: ["gpt-4o"] },+};++beforeEach(() => {+ jest.clearAllMocks();+});++describe("listTraceRecords", () => {+ it("uses the same adapter contract for the built-in ClickHouse source", async () => {+ mockResolveDescriptor.mockResolvedValue(builtin);+ const listSpans = jest.fn().mockResolvedValue({+ rows: [+ {+ traceId: "t1",+ spanId: "s1",+ parentSpanId: "",+ name: "chat",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:00.000Z",+ durationNs: 1,+ statusCode: "OK",+ spanAttributes: {},+ resourceAttributes: {},+ },+ ],+ });+ mockGetAdapter.mockResolvedValue({+ type: "clickhouse",+ sampleCacheKey: "builtin-db-1",+ listSpans,+ });++ const res = await listTraceRecords(params as never);+ expect(listSpans).toHaveBeenCalled();+ expect(res).toMatchObject({+ err: null,+ records: [expect.objectContaining({ SpanId: "s1", TraceId: "t1" })],+ });+ expect(mockGetRequests).not.toHaveBeenCalled();+ expect(mockGetAdapter).toHaveBeenCalledWith(+ expect.objectContaining({+ signal: "traces",+ descriptor: expect.objectContaining({ id: "builtin:db-1" }),+ })+ );+ });++ it("lists via stratified sample (not raw listSpans) and denormalizes rows", async () => {+ mockResolveDescriptor.mockResolvedValue(tempo);+ const listSpans = jest.fn().mockResolvedValue({ rows: [+ {+ traceId: "t1",+ spanId: "s1",+ parentSpanId: "",+ name: "chat",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:00.000Z",+ durationNs: 1_000_000,+ statusCode: "STATUS_CODE_OK",+ spanAttributes: { "gen_ai.request.model": "gpt-4o" },+ resourceAttributes: { "service.name": "api" },+ },+ ] });+ mockGetAdapter.mockResolvedValue({ listSpans });++ const res = await listTraceRecords(params as never);+ expect(listSpans).toHaveBeenCalled();+ expect(res.err).toBeNull();+ expect((res as { records?: unknown[] }).records?.[0]).toMatchObject({+ TraceId: "t1",+ SpanId: "s1",+ SpanName: "chat",+ ServiceName: "api",+ });+ expect(mockGetRequests).not.toHaveBeenCalled();+ });++ it("uses the backend trace count so pagination total does not grow with offset", async () => {+ mockResolveDescriptor.mockResolvedValue(tempo);+ let releaseCount!: () => void;+ const countStarted = new Promise<void>((resolve) => {+ releaseCount = resolve;+ });+ const countTraces = jest.fn().mockImplementation(async () => {+ releaseCount();+ return { total: 32, truncated: false };+ });+ // listSpans blocks until countTraces has started so a serial+ // list-then-count path would deadlock this test.+ const listSpansBlocked = jest.fn().mockImplementation(async () => {+ await countStarted;+ return {+ rows: [+ {+ traceId: "t1",+ spanId: "s1",+ parentSpanId: "",+ name: "chat",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:00.000Z",+ durationNs: 1_000_000,+ statusCode: "STATUS_CODE_OK",+ spanAttributes: {},+ resourceAttributes: { "service.name": "api" },+ },+ ],+ };+ });+ mockGetAdapter.mockResolvedValue({+ listSpans: listSpansBlocked,+ countTraces,+ });++ const res = await listTraceRecords({ ...params, offset: 25 } as never);++ expect(countTraces).toHaveBeenCalledTimes(1);+ expect(listSpansBlocked).toHaveBeenCalledTimes(1);+ expect(res).toMatchObject({ total: 32, freshness: "live" });+ });++ it("rethrows AdapterError so list routes can return 503", async () => {+ mockResolveDescriptor.mockResolvedValue(tempo);+ mockGetAdapter.mockResolvedValue({+ listSpans: jest+ .fn()+ .mockRejectedValue(new AdapterError("EXECUTION_FAILED", "tempo down")),+ });++ await expect(listTraceRecords(params as never)).rejects.toBeInstanceOf(+ AdapterError+ );+ });+});++describe("getTraceSpanRecord", () => {+ it("uses getSpan on external sources", async () => {+ mockResolveDescriptor.mockResolvedValue(tempo);+ mockGetAdapter.mockResolvedValue({+ getSpan: jest.fn().mockResolvedValue({+ traceId: "t1",+ spanId: "s1",+ parentSpanId: "",+ name: "chat",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:00.000Z",+ durationNs: 1,+ statusCode: "OK",+ spanAttributes: {},+ resourceAttributes: {},+ }),+ });++ const res = await getTraceSpanRecord("s1");+ expect(res.record).toMatchObject({ SpanId: "s1", TraceId: "t1" });+ });++ it("falls back to getTraceSpans when TraceId is provided and getSpan misses", async () => {+ mockResolveDescriptor.mockResolvedValue(tempo);+ const getSpan = jest.fn().mockResolvedValue(null);+ const getTraceSpans = jest.fn().mockResolvedValue([+ {+ traceId: "t1",+ spanId: "s1",+ parentSpanId: "",+ name: "chat",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:00.000Z",+ durationNs: 1,+ statusCode: "OK",+ spanAttributes: {},+ resourceAttributes: {},+ },+ ]);+ mockGetAdapter.mockResolvedValue({ getSpan, getTraceSpans });++ const res = await getTraceSpanRecord("s1", { traceId: "t1" });+ expect(getTraceSpans).toHaveBeenCalledWith("t1");+ expect(res.record).toMatchObject({ SpanId: "s1", TraceId: "t1" });+ });++ it("fails closed when the requested span is absent from the selected Tempo trace", async () => {+ mockResolveDescriptor.mockResolvedValue(tempo);+ const getSpan = jest.fn();+ const getTraceSpans = jest.fn().mockResolvedValue([+ {+ traceId: "t1",+ spanId: "different-span",+ parentSpanId: "",+ name: "root",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:00.000Z",+ durationNs: 1,+ statusCode: "OK",+ spanAttributes: {},+ resourceAttributes: {},+ },+ ]);+ mockGetAdapter.mockResolvedValue({ getSpan, getTraceSpans });++ const res = await getTraceSpanRecord("clickhouse-only-span", {+ traceId: "t1",+ });++ expect(res.record).toBeUndefined();+ expect(res.err).toContain("selected trace and telemetry source");+ expect(getSpan).not.toHaveBeenCalled();+ expect(mockGetRequestViaSpanId).not.toHaveBeenCalled();+ });++ it("opens the Tempo root span when list reused the trace id as spanId", async () => {+ mockResolveDescriptor.mockResolvedValue(tempo);+ const getSpan = jest.fn();+ const getTraceSpans = jest.fn().mockResolvedValue([+ {+ traceId: "t1",+ spanId: "root-span",+ parentSpanId: "",+ name: "root",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:00.000Z",+ durationNs: 1,+ statusCode: "OK",+ spanAttributes: {},+ resourceAttributes: {},+ },+ {+ traceId: "t1",+ spanId: "child-span",+ parentSpanId: "root-span",+ name: "child",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:01.000Z",+ durationNs: 1,+ statusCode: "OK",+ spanAttributes: {},+ resourceAttributes: {},+ },+ ]);+ mockGetAdapter.mockResolvedValue({ getSpan, getTraceSpans });++ const res = await getTraceSpanRecord("t1", { traceId: "t1" });++ expect(getTraceSpans).toHaveBeenCalledWith("t1");+ expect(res.err).toBeNull();+ expect(res.record).toMatchObject({ SpanId: "root-span", TraceId: "t1" });+ expect(getSpan).not.toHaveBeenCalled();+ });+});++describe("getTraceRecordByTraceId", () => {+ it("returns the first span from getTraceSpans", async () => {+ mockResolveDescriptor.mockResolvedValue(tempo);+ mockGetAdapter.mockResolvedValue({+ getTraceSpans: jest.fn().mockResolvedValue([+ {+ traceId: "t1",+ spanId: "root",+ parentSpanId: "",+ name: "root",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:00.000Z",+ durationNs: 1,+ statusCode: "OK",+ spanAttributes: {},+ resourceAttributes: {},+ },+ ]),+ });++ const res = await getTraceRecordByTraceId("t1");+ expect(res.record).toMatchObject({ SpanId: "root" });+ });+});++describe("getTraceHierarchy", () => {+ it("builds a ParentSpanId tree from external getTraceSpans", async () => {+ mockResolveDescriptor.mockResolvedValue(tempo);+ mockGetAdapter.mockResolvedValue({+ capabilities: () => ({ crossTraceSession: false }),+ getSpan: jest.fn().mockResolvedValue({+ traceId: "t1",+ spanId: "s1",+ parentSpanId: "",+ name: "root",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:00.000Z",+ durationNs: 1,+ statusCode: "OK",+ spanAttributes: {},+ resourceAttributes: {},+ }),+ getTraceSpans: jest.fn().mockResolvedValue([+ {+ traceId: "t1",+ spanId: "s1",+ parentSpanId: "",+ name: "root",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:00.000Z",+ durationNs: 1,+ statusCode: "OK",+ spanAttributes: {},+ resourceAttributes: {},+ },+ ]),+ });++ const res = await getTraceHierarchy("s1");+ expect(res.err).toBeNull();+ expect(res.record).toMatchObject({ SpanId: "s1" });+ expect(mockGetHeirarchyViaSpanId).not.toHaveBeenCalled();+ });++ it("builds ClickHouse hierarchy through the same adapter contract", async () => {+ mockResolveDescriptor.mockResolvedValue(builtin);+ const span = {+ traceId: "t1",+ spanId: "s1",+ parentSpanId: "",+ name: "root",+ serviceName: "api",+ timestamp: "2026-07-01T00:00:00.000Z",+ durationNs: 1,+ statusCode: "OK",+ spanAttributes: {},+ resourceAttributes: {},+ };+ mockGetAdapter.mockResolvedValue({+ capabilities: () => ({ crossTraceSession: false }),+ getSpan: jest.fn().mockResolvedValue(span),+ getTraceSpans: jest.fn().mockResolvedValue([span]),+ });++ const res = await getTraceHierarchy("s1");+ expect(mockGetHeirarchyViaSpanId).not.toHaveBeenCalled();+ expect(res.record).toMatchObject({ SpanId: "s1" });+ });++ it("does not build a hierarchy for a span absent from the selected Tempo trace", async () => {+ mockResolveDescriptor.mockResolvedValue(tempo);+ const getSpan = jest.fn();+ mockGetAdapter.mockResolvedValue({+ getSpan,+ getTraceSpans: jest.fn().mockResolvedValue([]),+ });++ const res = await getTraceHierarchy("clickhouse-only-span", {+ traceId: "tempo-trace",+ });++ expect(res.record).toEqual({});+ expect(res.err).toContain("selected trace and telemetry source");+ expect(getSpan).not.toHaveBeenCalled();+ expect(mockGetHeirarchyViaSpanId).not.toHaveBeenCalled();+ });+});++describe("dashboard graph facades", () => {+ it("uses a backend trace-summary series for the external trace volume", async () => {+ mockResolveDescriptor.mockResolvedValue(tempo);+ const traceTimeSeries = jest.fn().mockResolvedValue({+ rows: [+ { label: "00:00", count: 12 },+ { label: "01:00", count: 8 },+ ],+ meta: { freshness: "live", truncated: false },+ });+ const spanTimeSeries = jest.fn();+ mockGetAdapter.mockResolvedValue({ traceTimeSeries, spanTimeSeries });++ const res = await getTraceSummary(params as any);++ expect(traceTimeSeries).toHaveBeenCalledTimes(1);+ expect(spanTimeSeries).not.toHaveBeenCalled();+ expect(res).toMatchObject({+ total: 20,+ peak: 12,+ freshness: "live",+ truncated: false,+ });+ });++ it("aggregates total requests via the external adapter", async () => {+ mockResolveDescriptor.mockResolvedValue(tempo);+ mockGetAdapter.mockResolvedValue({+ aggregateSpans: jest+ .fn()+ .mockResolvedValueOnce({ rows: [{ total_requests: 12 }] })+ .mockResolvedValueOnce({ rows: [{ total_requests: 4 }] }),+ });++ const res = await getTraceTotalRequests(params as any);+ expect(res.err).toBeNull();+ expect(res.data).toEqual([+ { total_requests: 12, previous_total_requests: 4 },+ ]);+ });++ it("builds request-per-time series via spanTimeSeries", async () => {+ mockResolveDescriptor.mockResolvedValue(tempo);+ mockGetAdapter.mockResolvedValue({+ spanTimeSeries: jest.fn().mockResolvedValue({+ rows: [{ total: 3, request_time: "2026/07/01 00:00" }],+ }),+ });++ const res = await getTraceRequestPerTime(params as any);+ expect(res.data).toEqual([{ total: 3, request_time: "2026/07/01 00:00" }]);+ });++ it("aggregates average duration via the external adapter", async () => {+ mockResolveDescriptor.mockResolvedValue(tempo);+ mockGetAdapter.mockResolvedValue({+ aggregateSpans: jest+ .fn()+ .mockResolvedValueOnce({ rows: [{ average_duration: 1.5 }] })+ .mockResolvedValueOnce({ rows: [{ average_duration: 0.5 }] }),+ });++ const res = await getTraceAverageDuration(params as any);+ expect(res.data).toEqual([+ { average_duration: 1.5, previous_average_duration: 0.5 },+ ]);+ });++ it("probes existence via listSpans(limit=1)", async () => {+ mockResolveDescriptor.mockResolvedValue(tempo);+ mockGetAdapter.mockResolvedValue({+ listSpans: jest.fn().mockResolvedValue({ rows: [{ spanId: "s1" }] }),+ });++ const res = await getTraceExist();+ expect(res.data).toEqual([{ total_requests: 1 }]);+ });+});diff --git a/src/client/src/__tests__/lib/platform/vault/vault.test.ts b/src/client/src/__tests__/lib/platform/vault/vault.test.tsindex 74e98f22..951bf27e 100644--- a/src/client/src/__tests__/lib/platform/vault/vault.test.ts+++ b/src/client/src/__tests__/lib/platform/vault/vault.test.ts@@ -29,7 +29,18 @@ jest.mock('@/utils/sanitizer', () => ({__esModule: true,default: {sanitizeValue: jest.fn((v: string) => v),- sanitizeObject: jest.fn((o: object) => o),+ // Mimic sqlString.escape corrupting JSON quotes — upsertSecret must+ // preserve the raw value before this runs.+ sanitizeObject: jest.fn((o: object) => {+ const out: Record<string, unknown> = { ...o };+ for (const [k, v] of Object.entries(out)) {+ if (typeof v === 'string' && v.includes('"')) {+ // Escape backslashes before quotes (complete string escaping).+ out[k] = v.replace(/\\/g, '\\\\').replace(/"/g, '\\"');+ }+ }+ return out;+ }),},}));jest.mock('@/helpers/server/vault', () => ({@@ -55,6 +66,8 @@ import { getSecretByName, checkNameValidity, deleteSecret, getSecrets, getSecretimport { dataCollector } from '@/lib/platform/common';import { getCurrentUser } from '@/lib/session';import { getAPIKeyInfo } from '@/lib/platform/api-keys';+import { encryptValue } from '@/utils/crypto';+import { verifySecretInput, normalizeSecretDataForSDK } from '@/helpers/server/vault';beforeEach(() => {jest.clearAllMocks();@@ -164,15 +177,15 @@ describe('getSecrets', () => {await expect(getSecrets({ key: 'my-key' })).rejects.toThrow('Unauthorized');});- it('bypasses auth check when databaseConfigId is provided', async () => {+ it('requires an authenticated owner even when databaseConfigId is provided', async () => {await expect(getSecrets({ databaseConfigId: 'db-1' })).resolves.toBeDefined();- expect(getCurrentUser).not.toHaveBeenCalled();+ expect(getCurrentUser).toHaveBeenCalled();});- it('does not add session ownership filters for databaseConfigId API-key reads', async () => {+ it('keeps session ownership filters for databaseConfigId reads', async () => {await getSecrets({ databaseConfigId: 'db-1' });const [{ query }] = (dataCollector as jest.Mock).mock.calls[0];- expect(query).not.toContain('created_by');+ expect(query).toContain("v.created_by = '[email protected]'");});});@@ -191,8 +204,6 @@ describe('getSecretById', () => {});});-import { verifySecretInput, normalizeSecretDataForSDK } from '@/helpers/server/vault';-describe('upsertSecret', () => {describe('INSERT path (no id provided)', () => {it('inserts a new secret and returns success message', async () => {@@ -219,6 +230,21 @@ describe('upsertSecret', () => {});});+ it('preserves JSON credential payloads that SQL sanitization would corrupt', async () => {+ (dataCollector as jest.Mock)+ .mockResolvedValueOnce({ data: [], err: null })+ .mockResolvedValueOnce({ err: null });+ const json = JSON.stringify({+ username: '1628208',+ password: 'glc_token',+ });+ await upsertSecret({ key: 'telemetry-source/tempo/x', value: json });+ expect(encryptValue).toHaveBeenCalledWith(json);+ const [insertParams] = (dataCollector as jest.Mock).mock.calls[1];+ expect(insertParams.values[0].value).toBe(`enc:v1:${json}`);+ expect(insertParams.values[0].value).not.toContain('\\"');+ });+it('throws UNAUTHORIZED_USER when no user', async () => {(getCurrentUser as jest.Mock).mockResolvedValue(null);await expect(upsertSecret({ key: 'k', value: 'v' })).rejects.toThrow('Unauthorized');diff --git a/src/client/src/__tests__/lib/platform/vector/vector.test.ts b/src/client/src/__tests__/lib/platform/vector/vector.test.tsindex b0d0b393..953e63f3 100644--- a/src/client/src/__tests__/lib/platform/vector/vector.test.ts+++ b/src/client/src/__tests__/lib/platform/vector/vector.test.ts@@ -3,6 +3,13 @@ jest.mock('@/lib/platform/common', () => ({OTEL_TRACES_TABLE_NAME: 'otel_traces',}));+jest.mock('@/lib/platform/vector/external', () => ({+ externalResultGenerationByApplication: jest.fn().mockResolvedValue(null),+ externalResultGenerationByEnvironment: jest.fn().mockResolvedValue(null),+ externalResultGenerationByOperation: jest.fn().mockResolvedValue(null),+ externalResultGenerationBySystem: jest.fn().mockResolvedValue(null),+}));+import { getResultGenerationByApplication } from '@/lib/platform/vector/application';import { getResultGenerationByEnvironment } from '@/lib/platform/vector/environment';import { getResultGenerationByOperation } from '@/lib/platform/vector/operation';diff --git a/src/client/src/__tests__/lib/telemetry-source-crud.test.ts b/src/client/src/__tests__/lib/telemetry-source-crud.test.tsnew file mode 100644index 00000000..025eaa26--- /dev/null+++ b/src/client/src/__tests__/lib/telemetry-source-crud.test.ts@@ -0,0 +1,549 @@+const mockFindMany = jest.fn();+const mockFindFirst = jest.fn();+const mockCreate = jest.fn();+const mockUpdate = jest.fn();+const mockUpdateMany = jest.fn();+const mockDelete = jest.fn();+const mockConnectorUpsert = jest.fn();+const mockGetCurrentOrganisation = jest.fn();+const mockGetCurrentProjectForOrganisation = jest.fn();+const mockHasAdapterFactory = jest.fn();+const mockGetSourceTypeDescriptor = jest.fn();+const mockListSourceTypeDescriptors = jest.fn();+const mockCreateAdapter = jest.fn();++const mockBindingFindMany = jest.fn();+const mockBindingFindUnique = jest.fn();+const mockBindingFindFirst = jest.fn();+const mockBindingUpsert = jest.fn();+const mockBindingDeleteMany = jest.fn();+const mockUpsertSecret = jest.fn();+const mockGetSecretById = jest.fn();++const mockTxBindingUpsert = jest.fn();++const txClient = {+ telemetrySource: {+ updateMany: (...a: unknown[]) => mockUpdateMany(...a),+ create: (...a: unknown[]) => mockCreate(...a),+ update: (...a: unknown[]) => mockUpdate(...a),+ delete: (...a: unknown[]) => mockDelete(...a),+ },+ telemetrySourceBinding: {+ upsert: (...a: unknown[]) => mockTxBindingUpsert(...a),+ },+ connectorInstance: {+ upsert: (...a: unknown[]) => mockConnectorUpsert(...a),+ },+};++jest.mock("@/lib/prisma", () => ({+ __esModule: true,+ default: {+ telemetrySource: {+ findMany: (...a: unknown[]) => mockFindMany(...a),+ findFirst: (...a: unknown[]) => mockFindFirst(...a),+ delete: (...a: unknown[]) => mockDelete(...a),+ },+ telemetrySourceBinding: {+ findMany: (...a: unknown[]) => mockBindingFindMany(...a),+ findUnique: (...a: unknown[]) => mockBindingFindUnique(...a),+ findFirst: (...a: unknown[]) => mockBindingFindFirst(...a),+ upsert: (...a: unknown[]) => mockBindingUpsert(...a),+ deleteMany: (...a: unknown[]) => mockBindingDeleteMany(...a),+ },+ $transaction: (fn: (tx: unknown) => unknown) => fn(txClient),+ },+}));++jest.mock("@/lib/organisation", () => ({+ getCurrentOrganisation: (...a: unknown[]) => mockGetCurrentOrganisation(...a),+ getCurrentProjectForOrganisation: (...a: unknown[]) =>+ mockGetCurrentProjectForOrganisation(...a),+}));++jest.mock("@/lib/db-config", () => ({+ getDBConfigByUser: jest.fn(),+ getDBConfigById: jest.fn(),+}));++jest.mock("@/lib/platform/connectors/datasource/bootstrap", () => ({+ ensureAdaptersRegistered: jest.fn(),+}));++jest.mock("@/lib/platform/connectors/datasource/registry", () => ({+ hasAdapterFactory: (...a: unknown[]) => mockHasAdapterFactory(...a),+ getSourceTypeDescriptor: (...a: unknown[]) => mockGetSourceTypeDescriptor(...a),+ listSourceTypeDescriptors: (...a: unknown[]) =>+ mockListSourceTypeDescriptors(...a),+ createAdapter: (...a: unknown[]) => mockCreateAdapter(...a),+}));++jest.mock("@/lib/platform/vault", () => ({+ upsertSecret: (...a: unknown[]) => mockUpsertSecret(...a),+ getSecretById: (...a: unknown[]) => mockGetSecretById(...a),+}));++jest.mock("@/lib/platform/connectors/instances", () => ({+ syncTelemetrySourceConnector: jest.fn().mockResolvedValue(undefined),+ removeLegacyConnector: jest.fn().mockResolvedValue(undefined),+}));++jest.mock("@/lib/project-environment", () => ({+ createProjectEnvironment: jest.fn().mockResolvedValue({ id: "env-1" }),+}));++jest.mock("@/utils/log", () => ({ consoleLog: jest.fn() }));++import {+ availableSourceTypes,+ createTelemetrySource,+ deleteTelemetrySource,+ deleteTelemetrySourceBinding,+ healthCheckTelemetrySource,+ listTelemetrySources,+ listTelemetrySourceBindings,+ setTelemetrySourceBinding,+ updateTelemetrySource,+ validateTelemetrySourceAISignal,+} from "@/lib/telemetry-source-crud";++const row = (over: Record<string, unknown> = {}) => ({+ id: "src-1",+ projectId: "proj-1",+ name: "Prod DD",+ type: "datadog",+ signals: "traces,logs,metrics",+ settings: "{}",+ secretRef: "vault-1",+ isDefault: false,+ createdAt: new Date(),+ updatedAt: new Date(),+ createdByUserId: "u1",+ ...over,+});++beforeEach(() => {+ jest.clearAllMocks();+ mockGetCurrentOrganisation.mockResolvedValue({ id: "org-1" });+ mockGetCurrentProjectForOrganisation.mockResolvedValue({ id: "proj-1" });+ mockUpsertSecret.mockResolvedValue({ id: "vault-new" });+ mockGetSecretById.mockResolvedValue({ data: [{ id: "vault-1" }] });+ mockHasAdapterFactory.mockReturnValue(true);+ mockFindFirst.mockResolvedValue(null);+ mockBindingFindUnique.mockResolvedValue(null);+ mockBindingFindFirst.mockResolvedValue(null);+ mockConnectorUpsert.mockResolvedValue({});+ mockGetSourceTypeDescriptor.mockImplementation((type: string) => ({+ type,+ displayName: type,+ declaredSignals:+ type === "tempo"+ ? ["traces"]+ : type === "prometheus" || type === "loki"+ ? type === "loki"+ ? ["logs"]+ : ["metrics"]+ : ["traces", "logs", "metrics"],+ capabilities: {+ traceTree: true,+ spanEvents: false,+ serverAggregation: true,+ spanMutation: false,+ distinctValues: true,+ crossTraceSession: false,+ rawQuery: false,+ },+ correlation: { crossSignal: true, keys: ["traceId", "service"] },+ }));+ mockListSourceTypeDescriptors.mockImplementation(+ (opts: { includeInternal?: boolean } = {}) => {+ return [+ { type: "clickhouse" },+ { type: "tempo" },+ { type: "jaeger" },+ ];+ }+ );+});++describe("listTelemetrySources", () => {+ it("scopes to the current project and strips the secret ref", async () => {+ mockFindMany.mockResolvedValue([row(), row({ id: "src-2", secretRef: null })]);+ const sources = await listTelemetrySources();+ expect(mockFindMany).toHaveBeenCalledWith({+ where: { projectId: "proj-1" },+ orderBy: [{ isDefault: "desc" }, { createdAt: "asc" }],+ });+ expect(sources[0]).not.toHaveProperty("secretRef");+ expect(sources[0].hasSecret).toBe(true);+ expect(sources[1].hasSecret).toBe(false);+ });++ it("denies when the user has no current project (membership)", async () => {+ mockGetCurrentProjectForOrganisation.mockResolvedValue(null);+ await expect(listTelemetrySources()).rejects.toThrow();+ expect(mockFindMany).not.toHaveBeenCalled();+ });+});++describe("availableSourceTypes", () => {+ it("returns the registered adapter types", () => {+ expect(availableSourceTypes()).toEqual(["clickhouse", "tempo", "jaeger"]);+ });+});++describe("createTelemetrySource", () => {+ it("creates a project-scoped source and normalizes signals/settings", async () => {+ mockCreate.mockResolvedValue(row());+ await createTelemetrySource({+ name: "Prod DD",+ type: "datadog",+ signals: ["traces", "logs"],+ settings: { site: "datadoghq.com" },+ });+ const arg = mockCreate.mock.calls[0][0];+ expect(arg.data).toMatchObject({+ projectId: "proj-1",+ name: "Prod DD",+ type: "datadog",+ signals: "traces,logs",+ settings: '{"site":"datadoghq.com"}',+ isDefault: false,+ });+ });++ it("rejects a blank name", async () => {+ await expect(+ createTelemetrySource({ name: " ", type: "datadog" })+ ).rejects.toThrow();+ expect(mockCreate).not.toHaveBeenCalled();+ });++ it("rejects a duplicate name in the same environment", async () => {+ mockFindFirst.mockResolvedValue({ id: "src-existing" });+ await expect(+ createTelemetrySource({+ name: "prod-loki",+ type: "loki",+ environment: "production",+ })+ ).rejects.toThrow(/already exists/);+ expect(mockCreate).not.toHaveBeenCalled();+ });++ it("syncs the connector instance inside the create transaction", async () => {+ mockCreate.mockResolvedValue(row({ id: "src-loki", type: "loki", name: "local-loki", signals: "logs" }));+ await createTelemetrySource({+ name: "local-loki",+ type: "loki",+ environment: "production",+ settings: { url: "http://localhost:3100" },+ });+ expect(mockConnectorUpsert).toHaveBeenCalledWith(+ expect.objectContaining({+ where: { id: "telemetry:src-loki" },+ create: expect.objectContaining({+ type: "loki",+ name: "local-loki",+ }),+ })+ );+ });++ it("normalizes collapsed endpoint URLs before persist", async () => {+ mockCreate.mockResolvedValue(row({ id: "src-prom", type: "prometheus", name: "local-prometheus", signals: "metrics" }));+ await createTelemetrySource({+ name: "local-prometheus",+ type: "prometheus",+ environment: "production",+ settings: { url: "http:/localhost:9090", allowPrivateNetwork: true },+ });+ expect(mockCreate.mock.calls[0][0].data.settings).toContain(+ '"url":"http://localhost:9090"'+ );+ });++ it("rejects an unknown source type", async () => {+ mockHasAdapterFactory.mockReturnValue(false);+ await expect(+ createTelemetrySource({ name: "X", type: "splunk" })+ ).rejects.toThrow();+ });++ it("rejects non-object settings", async () => {+ await expect(+ createTelemetrySource({ name: "X", type: "datadog", settings: "[1,2]" })+ ).rejects.toThrow();+ });++ it("rejects a signal the source type cannot serve", async () => {+ await expect(+ createTelemetrySource({ name: "T", type: "tempo", signals: ["logs"] })+ ).rejects.toThrow();+ expect(mockCreate).not.toHaveBeenCalled();+ });++ it("defaults to the type's declared signals when none are given", async () => {+ mockCreate.mockResolvedValue(row({ type: "tempo", signals: "traces" }));+ await createTelemetrySource({ name: "T", type: "tempo" });+ expect(mockCreate.mock.calls[0][0].data.signals).toBe("traces");+ });++ it("persists inline credentials to the vault and stores only the secret id", async () => {+ mockCreate.mockResolvedValue(row());+ await createTelemetrySource({+ name: "Prod DD",+ type: "datadog",+ credentials: { apiKey: "dd-key", appKey: "dd-app", empty: " " },+ });+ expect(mockUpsertSecret).toHaveBeenCalledTimes(1);+ const secretArg = mockUpsertSecret.mock.calls[0][0];+ // Blank values are stripped; only real credentials are persisted.+ expect(JSON.parse(secretArg.value)).toEqual({+ apiKey: "dd-key",+ appKey: "dd-app",+ });+ // The vault secret id is stored, never the raw credentials.+ expect(mockCreate.mock.calls[0][0].data.secretRef).toBe("vault-new");+ });++ it("does not touch the vault when no non-empty credentials are given", async () => {+ mockCreate.mockResolvedValue(row());+ await createTelemetrySource({+ name: "Prod DD",+ type: "datadog",+ credentials: { apiKey: " " },+ secretRef: "explicit-ref",+ });+ expect(mockUpsertSecret).not.toHaveBeenCalled();+ expect(mockCreate.mock.calls[0][0].data.secretRef).toBe("explicit-ref");+ });++ it("rejects a vault secret that is not owned by the current user", async () => {+ mockGetSecretById.mockResolvedValue({ data: [] });+ await expect(+ createTelemetrySource({+ name: "Untrusted source",+ type: "datadog",+ secretRef: "secret-from-another-user",+ })+ ).rejects.toThrow("not owned by the current user");+ expect(mockCreate).not.toHaveBeenCalled();+ });++ it("unsets the previous default when creating a new default", async () => {+ mockCreate.mockResolvedValue(row({ isDefault: true }));+ await createTelemetrySource({+ name: "Prod DD",+ type: "datadog",+ isDefault: true,+ });+ expect(mockUpdateMany).toHaveBeenCalledWith({+ where: { projectId: "proj-1", isDefault: true },+ data: { isDefault: false },+ });+ });+});++describe("updateTelemetrySource", () => {+ it("denies updating a source outside the current project", async () => {+ mockFindFirst.mockResolvedValue(null);+ await expect(+ updateTelemetrySource("src-other", { name: "hacked" })+ ).rejects.toThrow();+ expect(mockUpdate).not.toHaveBeenCalled();+ expect(mockFindFirst).toHaveBeenCalledWith({+ where: { id: "src-other", projectId: "proj-1" },+ });+ });++ it("updates only provided fields for an owned source", async () => {+ mockFindFirst.mockResolvedValue(row());+ mockUpdate.mockResolvedValue(row({ name: "Renamed" }));+ await updateTelemetrySource("src-1", { name: "Renamed" });+ expect(mockUpdate).toHaveBeenCalledWith({+ where: { id: "src-1" },+ data: { name: "Renamed" },+ });+ });++ it("promotes to default and demotes siblings", async () => {+ mockFindFirst.mockResolvedValue(row());+ mockUpdate.mockResolvedValue(row({ isDefault: true }));+ await updateTelemetrySource("src-1", { isDefault: true });+ expect(mockUpdateMany).toHaveBeenCalledWith({+ where: { projectId: "proj-1", isDefault: true, NOT: { id: "src-1" } },+ data: { isDefault: false },+ });+ expect(mockUpdate.mock.calls[0][0].data.isDefault).toBe(true);+ });+});++describe("deleteTelemetrySource", () => {+ it("denies deleting a source outside the current project", async () => {+ mockFindFirst.mockResolvedValue(null);+ await expect(deleteTelemetrySource("src-other")).rejects.toThrow();+ expect(mockDelete).not.toHaveBeenCalled();+ });++ it("deletes an owned source", async () => {+ mockFindFirst.mockResolvedValue(row());+ await deleteTelemetrySource("src-1");+ expect(mockDelete).toHaveBeenCalledWith({ where: { id: "src-1" } });+ });+});++describe("healthCheckTelemetrySource", () => {+ it("binds the adapter and returns its health", async () => {+ mockFindFirst.mockResolvedValue(row());+ mockCreateAdapter.mockReturnValue({+ healthCheck: jest.fn().mockResolvedValue({ ok: true }),+ });+ expect(await healthCheckTelemetrySource("src-1")).toEqual({ ok: true });+ });++ it("returns not-ok when no adapter is registered for the type", async () => {+ mockFindFirst.mockResolvedValue(row({ type: "unknownvendor" }));+ mockCreateAdapter.mockReturnValue(undefined);+ const res = await healthCheckTelemetrySource("src-1");+ expect(res.ok).toBe(false);+ });+});++describe("validateTelemetrySourceAISignal", () => {+ it("delegates to the adapter's validateAISignal", async () => {+ mockFindFirst.mockResolvedValue(row());+ mockCreateAdapter.mockReturnValue({+ validateAISignal: jest+ .fn()+ .mockResolvedValue({ ok: true, sampleCount: 3, missingAttributes: [] }),+ });+ const res = await validateTelemetrySourceAISignal("src-1", {+ start: new Date("2026-07-01"),+ end: new Date("2026-07-02"),+ });+ expect(res).toMatchObject({ ok: true, sampleCount: 3, supported: true });+ });++ it("soft-succeeds when the adapter does not support AI validation", async () => {+ const { UnsupportedCapabilityError } = jest.requireActual(+ "@/lib/platform/connectors/datasource/types"+ ) as typeof import("@/lib/platform/connectors/datasource/types");+ mockFindFirst.mockResolvedValue(row({ type: "loki" }));+ mockCreateAdapter.mockReturnValue({+ validateAISignal: jest+ .fn()+ .mockRejectedValue(new UnsupportedCapabilityError("loki", "validateAISignal")),+ });+ const res = await validateTelemetrySourceAISignal("src-1", {+ start: new Date("2026-07-01"),+ end: new Date("2026-07-02"),+ });+ expect(res.supported).toBe(false);+ expect(res.ok).toBe(true);+ });+});++describe("telemetry source bindings", () => {+ it("lists the current project's bindings", async () => {+ mockBindingFindMany.mockResolvedValue([+ {+ id: "b1",+ signal: "traces",+ sourceId: "src-1",+ source: { name: "Prod DD", type: "datadog" },+ },+ ]);+ const bindings = await listTelemetrySourceBindings();+ expect(mockBindingFindMany).toHaveBeenCalledWith({+ where: { projectId: "proj-1", environment: "production" },+ include: { source: true, databaseConfig: true },+ orderBy: { signal: "asc" },+ });+ expect(bindings[0]).toMatchObject({+ signal: "traces",+ sourceId: "src-1",+ sourceName: "Prod DD",+ sourceType: "datadog",+ });+ });++ it("binds a signal to a source that serves it (project-scoped)", async () => {+ mockFindFirst.mockResolvedValue(row({ signals: "traces,logs,metrics" }));+ mockBindingFindUnique.mockResolvedValue({+ sourceId: "src-old",+ databaseConfigId: null,+ });+ mockBindingUpsert.mockResolvedValue({+ id: "b1",+ signal: "traces",+ sourceId: "src-1",+ environment: "production",+ });+ const result = await setTelemetrySourceBinding("traces", "src-1");+ expect(result).toMatchObject({+ sourceId: "src-1",+ previousSourceId: "src-old",+ environment: "production",+ });+ expect(mockFindFirst).toHaveBeenCalledWith({+ where: { id: "src-1", projectId: "proj-1" },+ });+ expect(mockBindingUpsert).toHaveBeenCalledWith({+ where: { projectId_signal_environment: { projectId: "proj-1", signal: "traces", environment: "production" } },+ create: { projectId: "proj-1", signal: "traces", environment: "production", sourceId: "src-1", databaseConfigId: null },+ update: { sourceId: "src-1", databaseConfigId: null },+ });+ });++ it("accepts connector-registry telemetry: prefixed source ids", async () => {+ mockFindFirst.mockResolvedValue(row({ signals: "logs" }));+ mockBindingUpsert.mockResolvedValue({+ id: "b2",+ signal: "logs",+ sourceId: "src-1",+ });+ await setTelemetrySourceBinding("logs", "telemetry:src-1");+ expect(mockFindFirst).toHaveBeenCalledWith({+ where: { id: "src-1", projectId: "proj-1" },+ });+ });++ it("rejects binding a signal the source does not serve", async () => {+ mockFindFirst.mockResolvedValue(row({ signals: "metrics" }));+ await expect(+ setTelemetrySourceBinding("traces", "src-1")+ ).rejects.toThrow();+ expect(mockBindingUpsert).not.toHaveBeenCalled();+ });++ it("rejects binding to a source outside the current project", async () => {+ mockFindFirst.mockResolvedValue(null);+ await expect(+ setTelemetrySourceBinding("traces", "src-other")+ ).rejects.toThrow();+ });++ it("rejects an unknown signal", async () => {+ await expect(+ setTelemetrySourceBinding("bogus", "src-1")+ ).rejects.toThrow();+ });++ it("deletes a signal binding (project-scoped)", async () => {+ mockBindingFindFirst.mockResolvedValue({+ sourceId: "src-9",+ databaseConfigId: null,+ });+ mockBindingDeleteMany.mockResolvedValue({ count: 1 });+ await expect(deleteTelemetrySourceBinding("logs")).resolves.toEqual({+ signal: "logs",+ environment: "production",+ previousSourceId: "src-9",+ });+ expect(mockBindingDeleteMany).toHaveBeenCalledWith({+ where: { projectId: "proj-1", signal: "logs", environment: "production" },+ });+ });+});diff --git a/src/client/src/__tests__/lib/telemetry-source-form-fields.test.ts b/src/client/src/__tests__/lib/telemetry-source-form-fields.test.tsnew file mode 100644index 00000000..68a2aab2--- /dev/null+++ b/src/client/src/__tests__/lib/telemetry-source-form-fields.test.ts@@ -0,0 +1,106 @@+jest.mock("@/lib/platform/connectors/datasource/http/safe-fetch", () => ({+ safeFetch: jest.fn(),+ selfHostedNetworkOptions: () => ({ allowHttp: true, allowPrivateNetwork: true }),+}));+jest.mock("@/lib/platform/connectors/datasource/http/secret", () => ({+ resolveSourceSecret: jest.fn(),+ redactableSecretValues: () => [],+}));++import { applyHttpAuthCredentials } from "@/lib/platform/connectors/datasource/http/auth-headers";+import { tempoAdapterFactory } from "@/lib/platform/connectors/datasource/grafana/tempo";+import { lokiAdapterFactory } from "@/lib/platform/connectors/datasource/grafana/loki";+import { prometheusAdapterFactory } from "@/lib/platform/connectors/datasource/prometheus/adapter";++describe("applyHttpAuthCredentials", () => {+ it("prefers Basic auth when username is set (Grafana Cloud path)", () => {+ const headers = applyHttpAuthCredentials({+ username: "1676120",+ password: "glc_token",+ token: "should-not-win",+ });+ expect(headers.Authorization).toBe(+ `Basic ${Buffer.from("1676120:glc_token").toString("base64")}`+ );+ });++ it("uses Bearer when only token is set", () => {+ const headers = applyHttpAuthCredentials({ token: "abc" });+ expect(headers.Authorization).toBe("Bearer abc");+ });++ it("follows the explicitly selected authentication type", () => {+ expect(+ applyHttpAuthCredentials(+ { username: "instance", password: "token" },+ { authType: "none" }+ )+ ).not.toHaveProperty("Authorization");+ expect(+ applyHttpAuthCredentials(+ { token: "abc" },+ { authType: "bearer" }+ ).Authorization+ ).toBe("Bearer abc");+ });++ it("adds X-Scope-OrgID tenant header when requested", () => {+ const headers = applyHttpAuthCredentials(+ { username: "u", password: "p", tenant: "team-a" },+ { tenantHeader: "X-Scope-OrgID" }+ );+ expect(headers["X-Scope-OrgID"]).toBe("team-a");+ });++ it("adds AccountID tenant header for VictoriaLogs", () => {+ const headers = applyHttpAuthCredentials(+ { token: "t", tenant: "1" },+ { tenantHeader: "AccountID" }+ );+ expect(headers.AccountID).toBe("1");+ expect(headers.Authorization).toBe("Bearer t");+ });+});++describe("descriptor configFields (descriptor-driven forms)", () => {+ it("exposes basic + bearer for Tempo", () => {+ for (const factory of [tempoAdapterFactory, lokiAdapterFactory, prometheusAdapterFactory]) {+ const d = factory.describe();+ const keys = d.configFields.map((f) => f.key);+ expect(keys).toEqual(+ expect.arrayContaining([+ "url",+ "allowHttp",+ "allowPrivateNetwork",+ "username",+ "password",+ "token",+ "tenant",+ ])+ );+ expect(d.configFields.find((f) => f.key === "authType")).toMatchObject({+ kind: "select",+ defaultValue: "none",+ });+ const keysInOrder = d.configFields.map((f) => f.key);+ const tenantIdx = keysInOrder.indexOf("tenant");+ const usernameIdx = keysInOrder.indexOf("username");+ if (tenantIdx >= 0 && usernameIdx >= 0) {+ expect(tenantIdx).toBeLessThan(usernameIdx);+ }+ expect(d.authStyle).toBe("http");+ }+ expect(+ tempoAdapterFactory.describe().configFields.find((f) => f.key === "tenant")+ ).toBeDefined();+ });++ it("uses type-specific endpoint placeholders", () => {+ const urlField = (type: { describe: () => { configFields: { key: string; placeholder?: string }[] } }) =>+ type.describe().configFields.find((f) => f.key === "url")?.placeholder ?? "";+ expect(urlField(tempoAdapterFactory)).toContain("tempo");+ expect(urlField(lokiAdapterFactory)).toContain("3100");+ expect(urlField(prometheusAdapterFactory)).toContain("9090");+ });++});diff --git a/src/client/src/__tests__/lib/telemetry-source.test.ts b/src/client/src/__tests__/lib/telemetry-source.test.tsnew file mode 100644index 00000000..a619f518--- /dev/null+++ b/src/client/src/__tests__/lib/telemetry-source.test.ts@@ -0,0 +1,588 @@+const mockFindUnique = jest.fn();+const mockFindFirst = jest.fn();+const mockFindMany = jest.fn();+const mockBindingFindUnique = jest.fn();+const mockDatabaseConfigFindFirst = jest.fn();+const mockGetDBConfigByUser = jest.fn();+const mockGetDBConfigById = jest.fn();+const mockGetDBConfigByIdInternal = jest.fn();+const mockGetCurrentOrganisation = jest.fn();+const mockGetCurrentProjectForOrganisation = jest.fn();++jest.mock("@/lib/prisma", () => ({+ __esModule: true,+ default: {+ telemetrySource: {+ findUnique: (...args: unknown[]) => mockFindUnique(...args),+ findFirst: (...args: unknown[]) => mockFindFirst(...args),+ findMany: (...args: unknown[]) => mockFindMany(...args),+ },+ telemetrySourceBinding: {+ findUnique: (...args: unknown[]) => mockBindingFindUnique(...args),+ },+ databaseConfig: {+ findFirst: (...args: unknown[]) => mockDatabaseConfigFindFirst(...args),+ },+ },+}));++jest.mock("@/lib/db-config", () => ({+ getDBConfigByUser: (...args: unknown[]) => mockGetDBConfigByUser(...args),+ getDBConfigById: (...args: unknown[]) => mockGetDBConfigById(...args),+ getDBConfigByIdInternal: (...args: unknown[]) =>+ mockGetDBConfigByIdInternal(...args),+}));++jest.mock("@/lib/organisation", () => ({+ getCurrentOrganisation: (...args: unknown[]) =>+ mockGetCurrentOrganisation(...args),+ getCurrentProjectForOrganisation: (...args: unknown[]) =>+ mockGetCurrentProjectForOrganisation(...args),+}));++jest.mock("@/lib/platform/connectors/datasource/http/secret", () => ({+ resolveSourceSecret: jest.fn(),+ redactableSecretValues: () => [],+}));++jest.mock("@/utils/log", () => ({+ consoleLog: jest.fn(),+}));++import {+ builtInDescriptor,+ getTelemetryAdapter,+ getTelemetryAdapterForDbConfig,+ isSignalServedByBuiltInClickHouse,+ isNativeSqlChatAvailable,+ parseSettings,+ parseSignals,+ resolveSignalSource,+ resolveTelemetrySourceDescriptor,+ sourceSupportsNativeSql,+ toDescriptor,+} from "@/lib/telemetry-source";++const srcRow = (over: Record<string, unknown> = {}) => ({+ id: "src-1",+ projectId: "proj-1",+ name: "Source",+ type: "clickhouse",+ signals: "traces,logs,metrics",+ settings: "{}",+ secretRef: null,+ isDefault: false,+ createdAt: new Date(),+ updatedAt: new Date(),+ createdByUserId: null,+ ...over,+});++const dbConfig = {+ id: "db-1",+ name: "Primary CH",+ projectId: "proj-1",+} as any;++beforeEach(() => {+ jest.clearAllMocks();+});++describe("parseSignals", () => {+ it("defaults to all signals when empty", () => {+ expect(parseSignals(undefined)).toEqual(["traces", "logs", "metrics", "intelligence"]);+ expect(parseSignals("")).toEqual(["traces", "logs", "metrics", "intelligence"]);+ });++ it("parses and trims valid signals", () => {+ expect(parseSignals("traces, metrics")).toEqual(["traces", "metrics"]);+ });++ it("drops invalid signals and falls back if none valid", () => {+ expect(parseSignals("bogus")).toEqual(["traces", "logs", "metrics", "intelligence"]);+ expect(parseSignals("logs,bogus")).toEqual(["logs"]);+ });+});++describe("parseSettings", () => {+ it("returns {} for empty or invalid JSON", () => {+ expect(parseSettings(undefined)).toEqual({});+ expect(parseSettings("not-json")).toEqual({});+ expect(parseSettings("123")).toEqual({});+ });++ it("parses JSON objects", () => {+ expect(parseSettings('{"site":"datadoghq.eu"}')).toEqual({+ site: "datadoghq.eu",+ });+ });+});++describe("builtInDescriptor", () => {+ it("builds a clickhouse built-in descriptor from a DatabaseConfig", () => {+ const d = builtInDescriptor(dbConfig);+ expect(d).toMatchObject({+ type: "clickhouse",+ id: "builtin:db-1",+ isBuiltIn: true,+ dbConfigId: "db-1",+ projectId: "proj-1",+ name: "Primary CH",+ });+ expect(d.signals).toEqual(["traces", "logs", "metrics", "intelligence"]);+ });+});++describe("toDescriptor", () => {+ it("maps a TelemetrySource row to a descriptor", () => {+ const row = {+ id: "src-1",+ projectId: "proj-1",+ name: "Prod Datadog",+ type: "datadog",+ signals: "traces,logs",+ settings: '{"site":"datadoghq.com"}',+ secretRef: "vault-1",+ isDefault: true,+ createdAt: new Date(),+ updatedAt: new Date(),+ createdByUserId: "u1",+ } as any;+ expect(toDescriptor(row)).toEqual({+ type: "datadog",+ id: "src-1",+ isBuiltIn: false,+ settings: { site: "datadoghq.com" },+ secretRef: "vault-1",+ signals: ["traces", "logs"],+ projectId: "proj-1",+ name: "Prod Datadog",+ });+ });+});++describe("resolveTelemetrySourceDescriptor precedence", () => {+ it("1. resolves an explicit sourceId override within the current project", async () => {+ mockGetCurrentOrganisation.mockResolvedValue({ id: "org-1" });+ mockGetCurrentProjectForOrganisation.mockResolvedValue({ id: "proj-1" });+ // First findFirst is the project-scoped sourceId lookup; no default needed.+ mockFindFirst.mockResolvedValueOnce({+ id: "src-1",+ projectId: "proj-1",+ name: "Prod Tempo",+ type: "tempo",+ signals: "traces",+ settings: "{}",+ secretRef: null,+ isDefault: false,+ createdAt: new Date(),+ updatedAt: new Date(),+ createdByUserId: null,+ });++ const d = await resolveTelemetrySourceDescriptor({ sourceId: "src-1" });+ expect(d.type).toBe("tempo");+ expect(d.id).toBe("src-1");+ expect(mockFindFirst).toHaveBeenCalledWith({+ where: { id: "src-1", projectId: "proj-1", environment: "production" },+ });+ expect(mockFindUnique).not.toHaveBeenCalled();+ });++ it("denies a sourceId that belongs to another project (IDOR)", async () => {+ mockGetCurrentOrganisation.mockResolvedValue({ id: "org-1" });+ mockGetCurrentProjectForOrganisation.mockResolvedValue({ id: "proj-1" });+ // Project-scoped lookup misses (source lives in proj-2).+ mockFindFirst+ .mockResolvedValueOnce(null)+ .mockResolvedValueOnce({+ id: "src-default",+ projectId: "proj-1",+ name: "Default NR",+ type: "newrelic",+ signals: "traces,logs,metrics",+ settings: "{}",+ secretRef: "vault-2",+ isDefault: true,+ createdAt: new Date(),+ updatedAt: new Date(),+ createdByUserId: null,+ });++ const d = await resolveTelemetrySourceDescriptor({+ sourceId: "foreign-src",+ });+ expect(d.id).toBe("src-default");+ expect(d.type).toBe("newrelic");+ expect(mockFindFirst).toHaveBeenNthCalledWith(1, {+ where: { id: "foreign-src", projectId: "proj-1", environment: "production" },+ });+ });++ it("falls through to project default when sourceId is missing", async () => {+ mockFindFirst.mockResolvedValueOnce(null).mockResolvedValueOnce({+ id: "src-default",+ projectId: "proj-1",+ name: "Default NR",+ type: "newrelic",+ signals: "traces,logs,metrics",+ settings: "{}",+ secretRef: "vault-2",+ isDefault: true,+ createdAt: new Date(),+ updatedAt: new Date(),+ createdByUserId: null,+ });+ mockGetCurrentOrganisation.mockResolvedValue({ id: "org-1" });+ mockGetCurrentProjectForOrganisation.mockResolvedValue({ id: "proj-1" });++ const d = await resolveTelemetrySourceDescriptor({ sourceId: "nope" });+ expect(d.type).toBe("newrelic");+ expect(d.id).toBe("src-default");+ });++ it("2. resolves the current project's default TelemetrySource", async () => {+ mockGetCurrentOrganisation.mockResolvedValue({ id: "org-1" });+ mockGetCurrentProjectForOrganisation.mockResolvedValue({ id: "proj-1" });+ mockFindFirst.mockResolvedValue({+ id: "src-default",+ projectId: "proj-1",+ name: "Default DD",+ type: "datadog",+ signals: "traces,logs,metrics",+ settings: "{}",+ secretRef: "vault-2",+ isDefault: true,+ createdAt: new Date(),+ updatedAt: new Date(),+ createdByUserId: null,+ });++ const d = await resolveTelemetrySourceDescriptor();+ expect(d.type).toBe("datadog");+ expect(mockFindFirst).toHaveBeenCalledWith({+ where: { projectId: "proj-1", isDefault: true },+ orderBy: { createdAt: "asc" },+ });+ });++ it("3. falls back to the built-in ClickHouse source (current db config)", async () => {+ mockGetCurrentOrganisation.mockResolvedValue({ id: "org-1" });+ mockGetCurrentProjectForOrganisation.mockResolvedValue({ id: "proj-1" });+ mockFindFirst.mockResolvedValue(null);+ mockGetDBConfigByUser.mockResolvedValue(dbConfig);++ const d = await resolveTelemetrySourceDescriptor();+ expect(d).toMatchObject({+ type: "clickhouse",+ id: "builtin:db-1",+ isBuiltIn: true,+ });+ expect(mockGetDBConfigByUser).toHaveBeenCalledWith(true);+ });++ it("resolves the built-in source by explicit dbConfigId", async () => {+ mockGetCurrentOrganisation.mockResolvedValue(null);+ mockGetDBConfigById.mockResolvedValue({+ id: "db-2",+ name: "Other CH",+ projectId: null,+ });++ const d = await resolveTelemetrySourceDescriptor({ dbConfigId: "db-2" });+ expect(d).toMatchObject({+ type: "clickhouse",+ id: "builtin:db-2",+ dbConfigId: "db-2",+ });+ });++ it("returns a safe placeholder when no ClickHouse is configured", async () => {+ mockGetCurrentOrganisation.mockResolvedValue(null);+ mockGetDBConfigByUser.mockResolvedValue(undefined);++ const d = await resolveTelemetrySourceDescriptor();+ expect(d).toMatchObject({+ type: "clickhouse",+ id: "builtin:none",+ isBuiltIn: true,+ });+ });+});++describe("resolveSignalSource (signal-aware routing)", () => {+ beforeEach(() => {+ mockGetCurrentOrganisation.mockResolvedValue({ id: "org-1" });+ mockGetCurrentProjectForOrganisation.mockResolvedValue({ id: "proj-1" });+ });++ it("uses the per-signal binding when the bound source serves the signal", async () => {+ mockBindingFindUnique.mockResolvedValue({+ source: srcRow({ id: "tempo-1", type: "tempo", signals: "traces" }),+ });+ const res = await resolveSignalSource("traces");+ expect(res.via).toBe("binding");+ expect(res.descriptor.id).toBe("tempo-1");+ expect(res.servesSignal).toBe(true);+ });++ it("keeps the active vault DatabaseConfig on an environment-routed external source", async () => {+ mockGetDBConfigByUser.mockResolvedValueOnce({+ ...dbConfig,+ environment: "production",+ });+ mockBindingFindUnique.mockResolvedValue({+ source: srcRow({ id: "tempo-1", type: "tempo", signals: "traces" }),+ });++ const res = await resolveSignalSource("traces", {+ environment: "production",+ });++ expect(res.descriptor).toMatchObject({+ id: "tempo-1",+ dbConfigId: "db-1",+ });+ expect(mockGetDBConfigByUser).toHaveBeenCalledWith(true);+ });++ it("ignores a binding that does not serve the signal and picks a capable source", async () => {+ mockBindingFindUnique.mockResolvedValue({+ source: srcRow({ id: "prom-1", type: "prometheus", signals: "metrics" }),+ });+ mockFindMany.mockResolvedValue([+ srcRow({ id: "prom-1", type: "prometheus", signals: "metrics" }),+ srcRow({ id: "tempo-1", type: "tempo", signals: "traces" }),+ ]);+ const res = await resolveSignalSource("traces");+ expect(res.via).toBe("none");+ });++ it("never routes a signal to a source that lacks it; falls back to built-in", async () => {+ mockBindingFindUnique.mockResolvedValue(null);+ mockFindMany.mockResolvedValue([+ srcRow({ id: "prom-1", type: "prometheus", signals: "metrics" }),+ ]);+ mockGetDBConfigByUser.mockResolvedValue(dbConfig);+ const res = await resolveSignalSource("traces");+ expect(res.via).toBe("builtin");+ expect(res.descriptor.type).toBe("clickhouse");+ expect(res.servesSignal).toBe(true);+ });++ it("returns a typed no-source state when nothing serves the signal", async () => {+ mockBindingFindUnique.mockResolvedValue(null);+ mockFindMany.mockResolvedValue([]);+ mockGetDBConfigByUser.mockResolvedValue(undefined);+ const res = await resolveSignalSource("traces");+ expect(res.via).toBe("none");+ expect(res.hasSource).toBe(false);+ expect(res.servesSignal).toBe(false);+ });++ it("routes through resolveTelemetrySourceDescriptor when signal is set", async () => {+ mockBindingFindUnique.mockResolvedValue({+ source: srcRow({ id: "loki-1", type: "loki", signals: "logs" }),+ });+ const d = await resolveTelemetrySourceDescriptor({ signal: "logs" });+ expect(d.id).toBe("loki-1");+ });++ it("denies a signal override sourceId from another project", async () => {+ mockFindFirst.mockResolvedValue(null);+ mockBindingFindUnique.mockResolvedValue(null);+ mockFindMany.mockResolvedValue([]);+ mockGetDBConfigByUser.mockResolvedValue(dbConfig);++ const res = await resolveSignalSource("traces", {+ sourceId: "foreign-src",+ });+ expect(res.via).toBe("builtin");+ expect(res.descriptor.type).toBe("clickhouse");+ expect(mockFindFirst).toHaveBeenCalledWith({+ where: { id: "foreign-src", projectId: "proj-1", environment: "production" },+ });+ expect(mockFindUnique).not.toHaveBeenCalled();+ });++ it("honors builtin:dbConfigId overrides for signal widgets", async () => {+ mockDatabaseConfigFindFirst.mockResolvedValue({+ id: "db-9",+ name: "Staging CH",+ projectId: "proj-1",+ environment: "staging",+ });++ const res = await resolveSignalSource("metrics", {+ sourceId: "builtin:db-9",+ environment: "staging",+ });++ expect(res.via).toBe("override");+ expect(res.descriptor).toMatchObject({+ id: "builtin:db-9",+ type: "clickhouse",+ dbConfigId: "db-9",+ name: "Staging CH",+ });+ expect(mockDatabaseConfigFindFirst).toHaveBeenCalledWith({+ where: {+ id: "db-9",+ projectId: "proj-1",+ environment: "staging",+ },+ });+ });+});++describe("getTelemetryAdapter fail-closed", () => {+ it("throws when an external source type has no registered adapter", async () => {+ mockGetCurrentOrganisation.mockResolvedValue({ id: "org-1" });+ mockGetCurrentProjectForOrganisation.mockResolvedValue({ id: "proj-1" });+ mockFindFirst.mockResolvedValue({+ id: "src-unknown",+ projectId: "proj-1",+ name: "Unknown Vendor",+ type: "not-a-real-adapter-type",+ signals: "traces",+ settings: "{}",+ secretRef: null,+ isDefault: true,+ createdAt: new Date(),+ updatedAt: new Date(),+ createdByUserId: null,+ });++ await expect(getTelemetryAdapter()).rejects.toThrow(+ /No adapter is registered/+ );+ });+});++describe("getTelemetryAdapterForDbConfig", () => {+ it("binds the materializer DatabaseConfig to an external source for vault access", async () => {+ mockGetDBConfigByIdInternal.mockResolvedValue({+ ...dbConfig,+ environment: "production",+ });+ mockBindingFindUnique.mockResolvedValue({+ source: srcRow({+ id: "tempo-1",+ type: "tempo",+ signals: "traces",+ secretRef: "vault-tempo",+ environment: "production",+ }),+ });++ const result = await getTelemetryAdapterForDbConfig("db-1", "traces");++ expect(result.descriptor).toMatchObject({+ id: "tempo-1",+ type: "tempo",+ projectId: "proj-1",+ secretRef: "vault-tempo",+ dbConfigId: "db-1",+ });+ expect(result.isBuiltIn).toBe(false);+ expect(mockGetDBConfigByIdInternal).toHaveBeenCalledWith({ id: "db-1" });+ expect(mockGetDBConfigById).not.toHaveBeenCalled();+ });++ it("resolves without a user session so cron materializer ticks can run", async () => {+ mockGetDBConfigById.mockRejectedValue(new Error("Unauthorized user!"));+ mockGetDBConfigByIdInternal.mockResolvedValue({+ ...dbConfig,+ environment: "production",+ });+ mockBindingFindUnique.mockResolvedValue({+ source: srcRow({+ id: "jaeger-1",+ type: "jaeger",+ signals: "traces",+ secretRef: "vault-jaeger",+ environment: "production",+ }),+ });++ await expect(+ getTelemetryAdapterForDbConfig("db-1", "traces")+ ).resolves.toMatchObject({+ isBuiltIn: false,+ descriptor: { id: "jaeger-1", type: "jaeger", dbConfigId: "db-1" },+ });+ expect(mockGetDBConfigById).not.toHaveBeenCalled();+ });+});++describe("isSignalServedByBuiltInClickHouse", () => {+ it("returns false for the built-in-shaped no-source placeholder", async () => {+ mockGetCurrentOrganisation.mockResolvedValueOnce({ id: "org-1" });+ mockGetCurrentProjectForOrganisation.mockResolvedValueOnce({ id: "proj-1" });+ mockGetDBConfigByUser.mockResolvedValueOnce(undefined);+ mockBindingFindUnique.mockResolvedValueOnce(null);++ await expect(+ isSignalServedByBuiltInClickHouse("logs", { environment: "production" })+ ).resolves.toBe(false);+ });++ it("returns false when the signal is routed to Tempo", async () => {+ mockGetCurrentOrganisation.mockResolvedValueOnce({ id: "org-1" });+ mockGetCurrentProjectForOrganisation.mockResolvedValueOnce({ id: "proj-1" });+ mockGetDBConfigByUser.mockResolvedValueOnce(dbConfig);+ mockBindingFindUnique.mockResolvedValueOnce({+ source: srcRow({ id: "tempo-1", type: "tempo", signals: "traces" }),+ });++ await expect(isSignalServedByBuiltInClickHouse("traces")).resolves.toBe(false);+ });++ it("returns true only for a real routed built-in ClickHouse", async () => {+ mockGetCurrentOrganisation.mockResolvedValueOnce({ id: "org-1" });+ mockGetCurrentProjectForOrganisation.mockResolvedValueOnce({ id: "proj-1" });+ mockGetDBConfigByUser.mockResolvedValueOnce(dbConfig);+ mockBindingFindUnique.mockResolvedValueOnce({ databaseConfig: dbConfig });++ await expect(isSignalServedByBuiltInClickHouse("logs")).resolves.toBe(true);+ });+});+++describe("isNativeSqlChatAvailable", () => {+ it("is available on the built-in ClickHouse source", async () => {+ mockFindUnique.mockResolvedValue(null);+ mockGetCurrentOrganisation.mockResolvedValue({ id: "org-1" });+ mockGetCurrentProjectForOrganisation.mockResolvedValue({ id: "proj-1" });+ mockFindFirst.mockResolvedValue(null);+ mockGetDBConfigByUser.mockResolvedValue(dbConfig);++ const res = await isNativeSqlChatAvailable();+ expect(res).toMatchObject({ available: true, sourceType: "clickhouse" });+ });++ it("is unavailable when the project default is an external source", async () => {+ mockGetCurrentOrganisation.mockResolvedValue({ id: "org-1" });+ mockGetCurrentProjectForOrganisation.mockResolvedValue({ id: "proj-1" });+ mockFindFirst.mockResolvedValue({+ id: "src-default",+ projectId: "proj-1",+ name: "Prod Datadog",+ type: "datadog",+ signals: "traces,logs,metrics",+ settings: "{}",+ secretRef: "vault-2",+ isDefault: true,+ createdAt: new Date(),+ updatedAt: new Date(),+ createdByUserId: null,+ });++ const res = await isNativeSqlChatAvailable();+ expect(res).toMatchObject({+ available: false,+ sourceType: "datadog",+ sourceName: "Prod Datadog",+ });+ });+});diff --git a/src/client/src/__tests__/middleware/middleware.test.ts b/src/client/src/__tests__/middleware/middleware.test.tsindex e46aeb29..a22c34d4 100644--- a/src/client/src/__tests__/middleware/middleware.test.ts+++ b/src/client/src/__tests__/middleware/middleware.test.ts@@ -54,6 +54,30 @@ describe('middleware', () => {expect(config.matcher).toContain('/agents/:path*');});+ // Regression: pages omitted from the matcher skip auth entirely and+ // render the playground shell while unauthenticated (APIs still 307).+ it('matches organisation, connectors, costs, and evaluations routes', () => {+ expect(config.matcher).toContain('/organisation');+ expect(config.matcher).toContain('/organisation/:path*');+ expect(config.matcher).toContain('/connectors');+ expect(config.matcher).toContain('/costs');+ expect(config.matcher).toContain('/evaluations');+ expect(config.matcher).toContain('/evaluations/:path*');+ });++ it('matches nested playground routes that previously bypassed auth', () => {+ expect(config.matcher).toContain('/openground/:path*');+ expect(config.matcher).toContain('/prompt-hub/:path*');+ expect(config.matcher).toContain('/fleet-hub/:path*');+ expect(config.matcher).toContain('/rule-engine');+ expect(config.matcher).toContain('/rule-engine/:path*');+ expect(config.matcher).toContain('/context');+ expect(config.matcher).toContain('/context/:path*');+ expect(config.matcher).toContain('/coding-agents/:path*');+ expect(config.matcher).toContain('/pricing');+ expect(config.matcher).toContain('/manage-models');+ });+// Regression guard for the `^/.*$` fallback: Next.js can't statically// analyze a spread of an imported binding in `config.matcher`, so it// silently matches every route — which made middleware run on staticdiff --git a/src/client/src/__tests__/prisma/seed-lib.test.ts b/src/client/src/__tests__/prisma/seed-lib.test.tsnew file mode 100644index 00000000..ba680d72--- /dev/null+++ b/src/client/src/__tests__/prisma/seed-lib.test.ts@@ -0,0 +1,377 @@+/**+ * @jest-environment node+ *+ * Fresh-install vs upgrade/backward-compat coverage for image seeding:+ * connectors, environments, and signal bindings.+ */++const {+ DEFAULT_ENVIRONMENT,+ DEFAULT_DB_NAME,+ DEFAULT_SIGNALS,+ databaseConnectorId,+ readInitDbConfig,+ hasInitDb,+ ensureProjectEnvironment,+ ensureEnvironmentDatabaseBindings,+ syncDatabaseConfigConnector,+ migrateOrphanedDefaultDb,+ upsertDefaultDatabaseConfig,+ migrateExistingData,+} = require("../../../prisma/seed-lib");++function createPrismaMock() {+ return {+ databaseConfig: {+ findMany: jest.fn().mockResolvedValue([]),+ findUnique: jest.fn().mockResolvedValue(null),+ findFirst: jest.fn().mockResolvedValue(null),+ upsert: jest.fn(),+ update: jest.fn(),+ updateMany: jest.fn(),+ delete: jest.fn(),+ },+ databaseConfigUser: {+ findFirst: jest.fn().mockResolvedValue(null),+ findMany: jest.fn().mockResolvedValue([]),+ upsert: jest.fn(),+ },+ projectEnvironment: {+ upsert: jest.fn(),+ },+ telemetrySourceBinding: {+ upsert: jest.fn(),+ },+ connectorInstance: {+ upsert: jest.fn(),+ },+ organisationUser: {+ findUnique: jest.fn().mockResolvedValue(null),+ findFirst: jest.fn().mockResolvedValue(null),+ create: jest.fn(),+ update: jest.fn(),+ },+ user: {+ update: jest.fn(),+ },+ };+}++describe("seed-lib INIT_DB helpers", () => {+ it("reads INIT_DB_* for fresh compose installs", () => {+ const config = readInitDbConfig({+ INIT_DB_HOST: "clickhouse",+ INIT_DB_PORT: "8123",+ INIT_DB_USERNAME: "default",+ INIT_DB_PASSWORD: "OPENLIT",+ INIT_DB_DATABASE: "openlit",+ });+ expect(hasInitDb(config)).toBe(true);+ expect(config).toEqual({+ username: "default",+ password: "OPENLIT",+ host: "clickhouse",+ port: "8123",+ database: "openlit",+ });+ });++ it("treats missing host/port as no seeded ClickHouse (fresh UI path)", () => {+ expect(hasInitDb(readInitDbConfig({}))).toBe(false);+ expect(hasInitDb(readInitDbConfig({ INIT_DB_HOST: "only-host" }))).toBe(false);+ });+});++describe("fresh user initiation with INIT_DB_*", () => {+ it("creates Default DB, production env, all signal bindings, and connector projection", async () => {+ const prisma = createPrismaMock();+ const created = {+ id: "db-1",+ name: DEFAULT_DB_NAME,+ projectId: "project-1",+ environment: DEFAULT_ENVIRONMENT,+ username: "default",+ password: "OPENLIT",+ host: "clickhouse",+ port: "8123",+ database: "openlit",+ query: "",+ };+ prisma.databaseConfig.upsert.mockResolvedValue(created);+ prisma.databaseConfigUser.upsert.mockResolvedValue({});+ prisma.projectEnvironment.upsert.mockResolvedValue({});+ prisma.telemetrySourceBinding.upsert.mockResolvedValue({});+ prisma.connectorInstance.upsert.mockResolvedValue({});++ const result = await upsertDefaultDatabaseConfig(prisma, {+ userId: "user-1",+ projectId: "project-1",+ initDb: {+ username: "default",+ password: "OPENLIT",+ host: "clickhouse",+ port: "8123",+ database: "openlit",+ },+ });++ expect(result).toEqual(created);+ expect(prisma.databaseConfig.upsert).toHaveBeenCalledWith(+ expect.objectContaining({+ where: {+ name_projectId_environment: {+ name: DEFAULT_DB_NAME,+ projectId: "project-1",+ environment: DEFAULT_ENVIRONMENT,+ },+ },+ // Fresh create uses INIT_DB_*; upgrades must not wipe UI edits.+ update: {},+ create: expect.objectContaining({+ environment: DEFAULT_ENVIRONMENT,+ name: DEFAULT_DB_NAME,+ host: "clickhouse",+ projectId: "project-1",+ createdByUserId: "user-1",+ }),+ })+ );++ expect(prisma.projectEnvironment.upsert).toHaveBeenCalledWith({+ where: { projectId_name: { projectId: "project-1", name: "production" } },+ create: { projectId: "project-1", name: "production" },+ update: {},+ });++ expect(prisma.telemetrySourceBinding.upsert).toHaveBeenCalledTimes(+ DEFAULT_SIGNALS.length+ );+ for (const signal of DEFAULT_SIGNALS) {+ expect(prisma.telemetrySourceBinding.upsert).toHaveBeenCalledWith(+ expect.objectContaining({+ where: {+ projectId_signal_environment: {+ projectId: "project-1",+ signal,+ environment: "production",+ },+ },+ create: expect.objectContaining({+ databaseConfigId: "db-1",+ signal,+ environment: "production",+ }),+ update: {},+ })+ );+ }++ expect(prisma.connectorInstance.upsert).toHaveBeenCalledWith(+ expect.objectContaining({+ where: { id: databaseConnectorId("db-1") },+ create: expect.objectContaining({+ type: "clickhouse",+ environment: "production",+ projectId: "project-1",+ status: "active",+ }),+ })+ );+ });++ it("skips Default DB when INIT_DB_* is absent (fresh user adds connectors later)", async () => {+ const prisma = createPrismaMock();+ const result = await upsertDefaultDatabaseConfig(prisma, {+ userId: "user-1",+ projectId: "project-1",+ initDb: { host: "", port: "" },+ });+ expect(result).toBeNull();+ expect(prisma.databaseConfig.upsert).not.toHaveBeenCalled();+ });+});++describe("backward compatibility / upgrade volume", () => {+ it("migrates orphaned Default DB using name_projectId_environment", async () => {+ const prisma = createPrismaMock();+ prisma.databaseConfig.findMany.mockResolvedValue([+ { id: "orphan-1", name: DEFAULT_DB_NAME, projectId: null },+ ]);+ prisma.databaseConfig.findUnique.mockResolvedValue(null);+ prisma.databaseConfig.updateMany.mockResolvedValue({ count: 1 });++ await migrateOrphanedDefaultDb(prisma, "project-1");++ expect(prisma.databaseConfig.updateMany).toHaveBeenCalledWith({+ where: { name: DEFAULT_DB_NAME, projectId: null },+ data: { projectId: "project-1", environment: DEFAULT_ENVIRONMENT },+ });+ });++ it("renames colliding orphaned configs instead of deleting referenced ones", async () => {+ const prisma = createPrismaMock();+ prisma.databaseConfig.findMany.mockResolvedValue([+ { id: "orphan-abc12345", name: DEFAULT_DB_NAME, projectId: null, environment: "production" },+ ]);+ prisma.databaseConfig.findUnique.mockResolvedValue({ id: "existing-default" });+ prisma.databaseConfigUser.findFirst.mockResolvedValue({ id: "link-1" });+ prisma.databaseConfig.update.mockResolvedValue({});++ await migrateOrphanedDefaultDb(prisma, "project-1");++ expect(prisma.databaseConfig.delete).not.toHaveBeenCalled();+ expect(prisma.databaseConfig.update).toHaveBeenCalledWith({+ where: { id: "orphan-abc12345" },+ data: {+ name: "Default DB (orphan-a)",+ projectId: "project-1",+ environment: "production",+ },+ });+ });++ it("backfills connectors and bindings for every environment on upgrade", async () => {+ const prisma = createPrismaMock();+ prisma.databaseConfig.findMany+ .mockResolvedValueOnce([]) // orphaned+ .mockResolvedValueOnce([+ {+ id: "db-legacy",+ name: "Default DB",+ username: "default",+ host: "clickhouse",+ port: "8123",+ database: "openlit",+ query: "",+ environment: "production",+ projectId: "project-1",+ },+ {+ id: "db-staging",+ name: "Staging DB",+ username: "default",+ host: "clickhouse",+ port: "8123",+ database: "openlit",+ query: "",+ environment: "staging",+ projectId: "project-1",+ },+ ]);+ prisma.projectEnvironment.upsert.mockResolvedValue({});+ prisma.connectorInstance.upsert.mockResolvedValue({});+ prisma.telemetrySourceBinding.upsert.mockResolvedValue({});+ prisma.databaseConfigUser.findMany.mockResolvedValue([]);++ const result = await migrateExistingData(+ prisma,+ "org-1",+ "project-1",+ "seed-user"+ );++ expect(result.migratedConfigCount).toBe(0);+ expect(prisma.connectorInstance.upsert).toHaveBeenCalledTimes(2);+ // production + staging environments × all default signals+ expect(prisma.telemetrySourceBinding.upsert).toHaveBeenCalledTimes(+ DEFAULT_SIGNALS.length * 2+ );+ expect(prisma.projectEnvironment.upsert).toHaveBeenCalledWith(+ expect.objectContaining({+ where: { projectId_name: { projectId: "project-1", name: "staging" } },+ })+ );+ });++ it("moves orphaned configs into the default project on upgrade", async () => {+ const prisma = createPrismaMock();+ prisma.databaseConfig.findMany+ .mockResolvedValueOnce([+ { id: "orphan-2", name: "Legacy CH", environment: null },+ ])+ .mockResolvedValueOnce([+ {+ id: "orphan-2",+ name: "Legacy CH",+ username: "default",+ host: "ch",+ port: "8123",+ database: "default",+ query: "",+ environment: "production",+ projectId: "project-1",+ },+ ]);+ prisma.databaseConfig.findUnique.mockResolvedValue(null);+ prisma.databaseConfig.update.mockResolvedValue({});+ prisma.projectEnvironment.upsert.mockResolvedValue({});+ prisma.connectorInstance.upsert.mockResolvedValue({});+ prisma.telemetrySourceBinding.upsert.mockResolvedValue({});+ prisma.databaseConfigUser.findMany.mockResolvedValue([]);++ const result = await migrateExistingData(+ prisma,+ "org-1",+ "project-1",+ "seed-user"+ );++ expect(result.migratedConfigCount).toBe(1);+ expect(prisma.databaseConfig.update).toHaveBeenCalledWith({+ where: { id: "orphan-2" },+ data: { projectId: "project-1", environment: "production" },+ });+ });+});++describe("environment + connector helpers", () => {+ it("ensures production environment upsert shape", async () => {+ const prisma = createPrismaMock();+ await ensureProjectEnvironment(prisma, "project-1");+ expect(prisma.projectEnvironment.upsert).toHaveBeenCalledWith({+ where: { projectId_name: { projectId: "project-1", name: "production" } },+ create: { projectId: "project-1", name: "production" },+ update: {},+ });+ });++ it("projects clickhouse connector settings without secrets in settings JSON", async () => {+ const prisma = createPrismaMock();+ prisma.connectorInstance.upsert.mockResolvedValue({});+ await syncDatabaseConfigConnector(prisma, {+ id: "db-1",+ name: "Default DB",+ username: "default",+ password: "secret",+ host: "clickhouse",+ port: "8123",+ database: "openlit",+ query: "",+ environment: "production",+ projectId: "project-1",+ });+ const call = prisma.connectorInstance.upsert.mock.calls[0][0];+ const settings = JSON.parse(call.create.settings);+ expect(settings).toEqual({+ username: "default",+ host: "clickhouse",+ port: "8123",+ database: "openlit",+ query: "",+ });+ expect(settings.password).toBeUndefined();+ });++ it("binding helper covers all default signals including intelligence", async () => {+ const prisma = createPrismaMock();+ prisma.telemetrySourceBinding.upsert.mockResolvedValue({});+ await ensureEnvironmentDatabaseBindings(prisma, "p1", "db1");+ expect(prisma.telemetrySourceBinding.upsert).toHaveBeenCalledTimes(4);+ expect(DEFAULT_SIGNALS).toEqual([+ "traces",+ "logs",+ "metrics",+ "intelligence",+ ]);+ });+});diff --git a/src/client/src/__tests__/store/filter.test.ts b/src/client/src/__tests__/store/filter.test.tsindex 039f8ccf..0101c53d 100644--- a/src/client/src/__tests__/store/filter.test.ts+++ b/src/client/src/__tests__/store/filter.test.ts@@ -4,7 +4,7 @@ import {REFRESH_RATE_TYPE,DEFAULT_TIME_RANGE,} from '@/store/filter';-import { addDays, addWeeks, addMonths } from 'date-fns';+import { addDays, addWeeks } from 'date-fns';describe('TIME_RANGE_TYPE', () => {it('has all expected time range keys', () => {@@ -59,15 +59,15 @@ describe('getTimeLimitObject', () => {expect(Math.abs(result.start.getTime() - expectedStart.getTime())).toBeLessThan(TOLERANCE_MS);});- it('returns start ~1 month ago for 1M', () => {+ it('returns a fixed 30-day window for 1M', () => {const result = getTimeLimitObject('1M', '') as { start: Date; end: Date };- const expectedStart = addMonths(new Date(), -1);+ const expectedStart = addDays(new Date(), -30);expect(Math.abs(result.start.getTime() - expectedStart.getTime())).toBeLessThan(TOLERANCE_MS);});- it('returns start ~3 months ago for 3M', () => {+ it('returns a fixed 90-day window for 3M', () => {const result = getTimeLimitObject('3M', '') as { start: Date; end: Date };- const expectedStart = addMonths(new Date(), -3);+ const expectedStart = addDays(new Date(), -90);expect(Math.abs(result.start.getTime() - expectedStart.getTime())).toBeLessThan(TOLERANCE_MS);});diff --git a/src/client/src/__tests__/utils/api.test.ts b/src/client/src/__tests__/utils/api.test.tsindex 66de9072..56348fa2 100644--- a/src/client/src/__tests__/utils/api.test.ts+++ b/src/client/src/__tests__/utils/api.test.ts@@ -8,8 +8,12 @@ jest.mock('@/store', () => ({},}));-const makeFetchResponse = (ok: boolean, body: unknown) => ({+const makeFetchResponse = (ok: boolean, body: unknown, status?: number) => ({ok,+ status: status ?? (ok ? 200 : 400),+ text: jest.fn().mockResolvedValue(+ typeof body === 'string' ? body : JSON.stringify(body ?? null)+ ),json: jest.fn().mockResolvedValue(body),});@@ -102,6 +106,29 @@ describe('getData', () => {expect(options.headers[OPENLIT_CONTEXT_HEADERS.databaseConfigId]).toBe('db-1');});+ it('ignores non-array databaseConfig.list when building context headers', async () => {+ (global.fetch as jest.Mock).mockResolvedValue(makeFetchResponse(true, {}));+ (useRootStore.getState as jest.Mock).mockReturnValue({+ organisation: { current: { id: 'org-1' } },+ project: { current: { id: 'project-1' } },+ databaseConfig: { list: '<html>login</html>' },+ });++ await getData({ url: '/api/test', method: 'GET' });++ const [, options] = (global.fetch as jest.Mock).mock.calls[0];+ expect(options.headers[OPENLIT_CONTEXT_HEADERS.databaseConfigId]).toBeUndefined();+ });++ it('throws when a successful response is non-JSON (e.g. HTML login redirect)', async () => {+ (global.fetch as jest.Mock).mockResolvedValue(+ makeFetchResponse(true, '<!DOCTYPE html><html>login</html>')+ );+ await expect(getData({ url: '/api/db-config', method: 'GET' })).rejects.toThrow(+ /non-JSON/i+ );+ });+it('makes a PUT request when specified', async () => {(global.fetch as jest.Mock).mockResolvedValue(makeFetchResponse(true, {}));await getData({ url: '/api/test', method: 'PUT', data: { x: 1 } });diff --git a/src/client/src/__tests__/utils/log.test.ts b/src/client/src/__tests__/utils/log.test.tsindex 91e9a604..5b4757ff 100644--- a/src/client/src/__tests__/utils/log.test.ts+++ b/src/client/src/__tests__/utils/log.test.ts@@ -16,13 +16,20 @@ describe('consoleLog', () => {expect(consoleSpy).toHaveBeenCalled();});- it('calls console.log with multiple arguments wrapped in an array', () => {+ it('serializes multiple arguments as a JSON array string', () => {consoleLog('a', 'b', 'c');- expect(consoleSpy).toHaveBeenCalledWith(['a', 'b', 'c']);+ expect(consoleSpy).toHaveBeenCalledWith('["a","b","c"]');});it('calls console.log once per invocation', () => {consoleLog('test');expect(consoleSpy).toHaveBeenCalledTimes(1);});++ it('falls back when arguments contain circular references', () => {+ const circular: { self?: unknown } = {};+ circular.self = circular;+ expect(() => consoleLog(circular)).not.toThrow();+ expect(consoleSpy).toHaveBeenCalled();+ });});diff --git a/src/client/src/__tests__/utils/telemetry-request-cache.test.ts b/src/client/src/__tests__/utils/telemetry-request-cache.test.tsnew file mode 100644index 00000000..710f867a--- /dev/null+++ b/src/client/src/__tests__/utils/telemetry-request-cache.test.ts@@ -0,0 +1,71 @@+import {+ __clearTelemetryRequestCache,+ isCacheableTelemetryUrl,+ normalizeTelemetryCacheBody,+ peekTelemetryRequestCache,+ withTelemetryRequestCache,+} from "@/utils/telemetry-request-cache";++describe("telemetry-request-cache", () => {+ beforeEach(() => {+ __clearTelemetryRequestCache();+ });++ it("only caches telemetry/metrics URLs", () => {+ expect(isCacheableTelemetryUrl("/api/telemetry/request/grouped")).toBe(true);+ expect(isCacheableTelemetryUrl("/api/telemetry/summary/traces")).toBe(true);+ expect(isCacheableTelemetryUrl("/api/agents/foo")).toBe(false);+ });++ it("quantizes relative timeLimit ends so remounts share a cache key", () => {+ const a = normalizeTelemetryCacheBody(+ JSON.stringify({+ timeLimit: {+ type: "24HR",+ start: "2026-07-10T12:00:10.000Z",+ end: "2026-07-11T12:00:10.000Z",+ },+ })+ );+ const b = normalizeTelemetryCacheBody(+ JSON.stringify({+ timeLimit: {+ type: "24HR",+ start: "2026-07-10T12:00:25.000Z",+ end: "2026-07-11T12:00:25.000Z",+ },+ })+ );+ expect(a).toBe(b);+ });++ it("seeds peek after a successful cache write", async () => {+ await withTelemetryRequestCache("/api/telemetry/request", "{}", async () => ({+ ok: true,+ }));+ expect(+ peekTelemetryRequestCache("/api/telemetry/request", "{}")+ ).toEqual({ ok: true });+ });++ it("dedupes concurrent loads and serves TTL hits", async () => {+ let loads = 0;+ const loader = async () => {+ loads += 1;+ return { ok: true, loads };+ };+ const a = withTelemetryRequestCache("/api/telemetry/request", "{}", loader);+ const b = withTelemetryRequestCache("/api/telemetry/request", "{}", loader);+ const [ra, rb] = await Promise.all([a, b]);+ expect(ra).toEqual(rb);+ expect(loads).toBe(1);++ const c = await withTelemetryRequestCache(+ "/api/telemetry/request",+ "{}",+ loader+ );+ expect(c).toEqual(ra);+ expect(loads).toBe(1);+ });+});diff --git a/src/client/src/__tests__/utils/validation.test.ts b/src/client/src/__tests__/utils/validation.test.tsindex a9fc0e92..9b6a015f 100644--- a/src/client/src/__tests__/utils/validation.test.ts+++ b/src/client/src/__tests__/utils/validation.test.ts@@ -61,6 +61,12 @@ describe("validateDatabaseHost", () => {expect(validateDatabaseHost("metadata.google.internal").valid).toBe(false);});+ it("allows an explicitly configured local ClickHouse host", () => {+ expect(validateDatabaseHost("127.0.0.1", { allowPrivateNetwork: true }).valid).toBe(true);+ expect(validateDatabaseHost("localhost", { allowPrivateNetwork: true }).valid).toBe(true);+ expect(validateDatabaseHost("169.254.169.254", { allowPrivateNetwork: true }).valid).toBe(false);+ });+it("rejects private IPv6 and invalid host characters", () => {expect(validateDatabaseHost("[::1]").valid).toBe(false);expect(validateDatabaseHost("[fe80::1]").valid).toBe(false);@@ -139,5 +145,12 @@ describe("sanitizeErrorMessage", () => {it("keeps safe user-facing messages", () => {expect(sanitizeErrorMessage("Invalid input")).toBe("Invalid input");+ expect(+ sanitizeErrorMessage(+ 'Error: A connector named "prod-loki" already exists in the production environment. Choose a different name.'+ )+ ).toBe(+ 'A connector named "prod-loki" already exists in the production environment. Choose a different name.'+ );});});diff --git a/src/client/src/app/(playground)/chat/page.tsx b/src/client/src/app/(playground)/chat/page.tsxindex 1db17d28..9da7e643 100644--- a/src/client/src/app/(playground)/chat/page.tsx+++ b/src/client/src/app/(playground)/chat/page.tsx@@ -1,6 +1,6 @@"use client";-import { useEffect } from "react";+import { useEffect, useState } from "react";import { useRouter, useSearchParams } from "next/navigation";import ChatLayout from "@/components/(playground)/chat/chat-layout";import { RequestProvider } from "@/components/(playground)/request/request-context";@@ -11,12 +11,7 @@ import {getProjectIsLoading,getProjectList,} from "@/selectors/project";-import {- getDatabaseConfigList,- getDatabaseConfigListIsLoading,-} from "@/selectors/database-config";import { fetchProjectList } from "@/helpers/client/project";-import { fetchDatabaseConfigList } from "@/helpers/client/database-config";import Loader from "@/components/common/loader";export default function ChatPage() {@@ -27,22 +22,25 @@ export default function ChatPage() {const projects = useRootStore(getProjectList);const currentProject = useRootStore(getCurrentProject);const isProjectLoading = useRootStore(getProjectIsLoading);- const databaseConfigs = useRootStore(getDatabaseConfigList);- const isDatabaseConfigLoading = useRootStore(getDatabaseConfigListIsLoading);+ const [hasDbConfig, setHasDbConfig] = useState<boolean>();const hasProject = Boolean(currentProject?.id && (projects?.length || 0) > 0);- const hasDbConfig = Boolean(databaseConfigs?.length);const isSetupLoading =isProjectLoading ||- isDatabaseConfigLoading ||- projects === undefined ||- (hasProject && databaseConfigs === undefined);+ hasDbConfig === undefined ||+ projects === undefined;useEffect(() => {if (currentOrg?.id) fetchProjectList(currentOrg.id);}, [currentOrg?.id]);useEffect(() => {- if (currentProject?.id) fetchDatabaseConfigList(() => {});+ if (currentProject?.id) {+ setHasDbConfig(undefined);+ fetch("/api/connectors")+ .then((response) => response.ok ? response.json() : { connectors: [] })+ .then((body) => setHasDbConfig((body.connectors || []).some((connector: { type?: string }) => connector.type === "clickhouse")))+ .catch(() => setHasDbConfig(false));+ }}, [currentProject?.id]);useEffect(() => {diff --git a/src/client/src/app/(playground)/connectors/page.tsx b/src/client/src/app/(playground)/connectors/page.tsxnew file mode 100644index 00000000..5af40a0c--- /dev/null+++ b/src/client/src/app/(playground)/connectors/page.tsx@@ -0,0 +1,5 @@+import ConnectorsPage from "@/components/(playground)/connectors/connectors-page";++export default function GlobalConnectorsRoutePage() {+ return <ConnectorsPage />;+}diff --git a/src/client/src/app/(playground)/dashboard/gpu/gpu-metric.tsx b/src/client/src/app/(playground)/dashboard/gpu/gpu-metric.tsxindex c70c67db..91944ce8 100644--- a/src/client/src/app/(playground)/dashboard/gpu/gpu-metric.tsx+++ b/src/client/src/app/(playground)/dashboard/gpu/gpu-metric.tsx@@ -36,7 +36,7 @@ export default function GPUMetric({...getFilterParamsForDashboard(filter),}),requestType: "POST",- url: url ? url : "/api/metrics/gpu",+ url: url ? url : "/api/telemetry/gpu",responseDataKey: "data",});}, [filter]);diff --git a/src/client/src/app/(playground)/dashboard/gpu/index.tsx b/src/client/src/app/(playground)/dashboard/gpu/index.tsxindex 860453ce..756a2b28 100644--- a/src/client/src/app/(playground)/dashboard/gpu/index.tsx+++ b/src/client/src/app/(playground)/dashboard/gpu/index.tsx@@ -8,29 +8,29 @@ export default function GPUDashboard() {<div className="grid-cols-2 grid gap-4"><GPUMetricchartKeys={["utilization", "enc_utilization", "dec_utilization"]}- url="/api/metrics/gpu/utilization/time"+ url="/api/telemetry/gpu/utilization/time"title="Avg Utilization Percentage (%)"/><GPUMetricchartKeys={["temperature"]}- url="/api/metrics/gpu/temperature/time"+ url="/api/telemetry/gpu/temperature/time"title="Avg Temperature (°C)"/><GPUMetricchartKeys={["memory_total", "memory_used", "memory_free"]}- url="/api/metrics/gpu/memory/time"+ url="/api/telemetry/gpu/memory/time"title="Memory (MB)"/><GPUMetricchartKeys={["power.limit", "power.draw"]}- url="/api/metrics/gpu/power/time"+ url="/api/telemetry/gpu/power/time"title="Power (Watt)"/><GPUMetricchartKeys={["fan_speed"]}- url="/api/metrics/gpu/fanspeed/time"+ url="/api/telemetry/gpu/fanspeed/time"title="Fan speed (0-100)"/></div>diff --git a/src/client/src/app/(playground)/dashboard/gpu/number-stats.tsx b/src/client/src/app/(playground)/dashboard/gpu/number-stats.tsxindex fd752e63..534664f9 100644--- a/src/client/src/app/(playground)/dashboard/gpu/number-stats.tsx+++ b/src/client/src/app/(playground)/dashboard/gpu/number-stats.tsx@@ -9,28 +9,28 @@ function NumberStats() {heading="Avg utilization percentage"textSuffix="%"icon={UtilityPole}- url="/api/metrics/gpu/utilization/average"+ url="/api/telemetry/gpu/utilization/average"/><StatCarddataKey="temperature"heading="Avg temperature"icon={Thermometer}textSuffix="°C"- url="/api/metrics/gpu/temperature/average"+ url="/api/telemetry/gpu/temperature/average"/><StatCarddataKey="power_draw"heading="Avg power draw"icon={PowerIcon}textSuffix="W"- url="/api/metrics/gpu/power/average"+ url="/api/telemetry/gpu/power/average"/><StatCarddataKey="memory_used"heading="Average memory used"icon={MemoryStick}textSuffix="MB"- url="/api/metrics/gpu/memory/average"+ url="/api/telemetry/gpu/memory/average"/></div>);diff --git a/src/client/src/app/(playground)/dashboard/llm/data-charts.tsx b/src/client/src/app/(playground)/dashboard/llm/data-charts.tsxindex db315641..1b31a17f 100644--- a/src/client/src/app/(playground)/dashboard/llm/data-charts.tsx+++ b/src/client/src/app/(playground)/dashboard/llm/data-charts.tsx@@ -7,25 +7,25 @@ export default function DataCharts() {categoryKey="count"heading="Generation by categories"indexKey="category"- url="/api/metrics/llm/category"+ url="/api/telemetry/llm/category"/><PieChartCardcategoryKey="count"heading="Generation by provider"indexKey="provider"- url="/api/metrics/llm/endpoint"+ url="/api/telemetry/llm/endpoint"/><PieChartCardcategoryKey="cost"heading="Cost by Environment"indexKey="environment"- url="/api/metrics/llm/cost/environment"+ url="/api/telemetry/llm/cost/environment"/><PieChartCardcategoryKey="cost"heading="Cost by application"indexKey="applicationName"- url="/api/metrics/llm/cost/application"+ url="/api/telemetry/llm/cost/application"/></div>);diff --git a/src/client/src/app/(playground)/dashboard/llm/number-stats.tsx b/src/client/src/app/(playground)/dashboard/llm/number-stats.tsxindex 5beb99df..42925054 100644--- a/src/client/src/app/(playground)/dashboard/llm/number-stats.tsx+++ b/src/client/src/app/(playground)/dashboard/llm/number-stats.tsx@@ -20,7 +20,7 @@ function NumberStats() {}}heading="Total requests"icon={RadioTower}- url="/api/metrics/request/total"+ url="/api/telemetry/request/total"/><StatCarddataKey="average_duration"@@ -33,14 +33,14 @@ function NumberStats() {parser={(value: any) =>integerParser(`${value}`, TraceMapping.requestDuration.offset)}- url="/api/metrics/request/duration/average"+ url="/api/telemetry/request/duration/average"/><StatCarddataKey="total_tokens"extraParams={{ type: "total" }}heading="Avg tokens per request"icon={Braces}- url="/api/metrics/llm/token/request/average"+ url="/api/telemetry/llm/token/request/average"/><StatCarddataKey="total_usage_cost"@@ -48,7 +48,7 @@ function NumberStats() {icon={CircleDollarSign}roundTo={7}textPrefix="$"- url="/api/metrics/llm/cost/total"+ url="/api/telemetry/llm/cost/total"/><StatCarddataKey="average_usage_cost"@@ -56,7 +56,7 @@ function NumberStats() {icon={Banknote}roundTo={7}textPrefix="$"- url="/api/metrics/llm/cost/request/average"+ url="/api/telemetry/llm/cost/request/average"/><StatCarddataKey="total_evaluation_detected"diff --git a/src/client/src/app/(playground)/dashboard/llm/requests-per-time.tsx b/src/client/src/app/(playground)/dashboard/llm/requests-per-time.tsxindex 95f4e6a3..717f9486 100644--- a/src/client/src/app/(playground)/dashboard/llm/requests-per-time.tsx+++ b/src/client/src/app/(playground)/dashboard/llm/requests-per-time.tsx@@ -26,7 +26,7 @@ export default function RequestsPerTime() {fireRequest({body: JSON.stringify(getFilterParamsForDashboard(filter)),requestType: "POST",- url: "/api/metrics/request/time",+ url: "/api/telemetry/request/time",responseDataKey: "data",failureCb: (err?: string) => {toast.error(err || `Cannot connect to server!`, {diff --git a/src/client/src/app/(playground)/dashboard/llm/token-charts.tsx b/src/client/src/app/(playground)/dashboard/llm/token-charts.tsxindex be43fcc2..fe5943e0 100644--- a/src/client/src/app/(playground)/dashboard/llm/token-charts.tsx+++ b/src/client/src/app/(playground)/dashboard/llm/token-charts.tsx@@ -33,7 +33,7 @@ function TopModels() {fireRequest({body: JSON.stringify(getFilterParamsForDashboard(filter)),requestType: "POST",- url: "/api/metrics/llm/model/top",+ url: "/api/telemetry/llm/model/top",responseDataKey: "data",});}, [filter]);@@ -121,7 +121,7 @@ function ModelsPerTime() {fireRequest({body: JSON.stringify(getFilterParamsForDashboard(filter)),requestType: "POST",- url: "/api/metrics/llm/model/time",+ url: "/api/telemetry/llm/model/time",responseDataKey: "data",});}, [filter]);@@ -217,7 +217,7 @@ function TokensPerTime() {fireRequest({body: JSON.stringify(getFilterParamsForDashboard(filter)),requestType: "POST",- url: "/api/metrics/llm/token/time",+ url: "/api/telemetry/llm/token/time",responseDataKey: "data",});}, [filter]);@@ -342,7 +342,7 @@ function TokenCharts() {heading="Avg prompt tokens / request"loadingClass="h-8 w-12"textClass="text-2xl"- url="/api/metrics/llm/token/request/average"+ url="/api/telemetry/llm/token/request/average"/><StatCarddataKey="total_tokens"@@ -350,7 +350,7 @@ function TokenCharts() {heading="Avg completion tokens / request"loadingClass="h-8 w-12"textClass="text-2xl"- url="/api/metrics/llm/token/request/average"+ url="/api/telemetry/llm/token/request/average"/></div><TokensPerTime />diff --git a/src/client/src/app/(playground)/dashboard/vector/number-stats.tsx b/src/client/src/app/(playground)/dashboard/vector/number-stats.tsxindex 19c74664..9f766c22 100644--- a/src/client/src/app/(playground)/dashboard/vector/number-stats.tsx+++ b/src/client/src/app/(playground)/dashboard/vector/number-stats.tsx@@ -15,7 +15,7 @@ function NumberStats() {}}heading="Total requests"icon={RadioTower}- url="/api/metrics/request/total"+ url="/api/telemetry/request/total"/><StatCarddataKey="average_duration"@@ -28,26 +28,26 @@ function NumberStats() {parser={(value: any) =>integerParser(`${value}`, TraceMapping.requestDuration.offset)}- url="/api/metrics/request/duration/average"+ url="/api/telemetry/request/duration/average"/></div><PieChartCardcategoryKey="count"heading="Generation by system"indexKey="system"- url="/api/metrics/vector/system"+ url="/api/telemetry/vector/system"/><PieChartCardcategoryKey="count"heading="Generation by application"indexKey="applicationName"- url="/api/metrics/vector/application"+ url="/api/telemetry/vector/application"/><PieChartCardcategoryKey="count"heading="Generation by Environment"indexKey="environment"- url="/api/metrics/vector/environment"+ url="/api/telemetry/vector/environment"/></div>);diff --git a/src/client/src/app/(playground)/dashboard/vector/operations.tsx b/src/client/src/app/(playground)/dashboard/vector/operations.tsxindex 1f9eaa1e..3f2c425a 100644--- a/src/client/src/app/(playground)/dashboard/vector/operations.tsx+++ b/src/client/src/app/(playground)/dashboard/vector/operations.tsx@@ -25,7 +25,7 @@ export default function Operations() {fireRequest({body: JSON.stringify(getFilterParamsForDashboard(filter)),requestType: "POST",- url: "/api/metrics/vector/operation",+ url: "/api/telemetry/vector/operation",responseDataKey: "data",});}, [filter]);diff --git a/src/client/src/app/(playground)/home/page.tsx b/src/client/src/app/(playground)/home/page.tsxindex 05803e12..0c346791 100644--- a/src/client/src/app/(playground)/home/page.tsx+++ b/src/client/src/app/(playground)/home/page.tsx@@ -15,12 +15,7 @@ import {getProjectIsLoading,getProjectList,} from "@/selectors/project";-import {- getDatabaseConfigList,- getDatabaseConfigListIsLoading,-} from "@/selectors/database-config";import { getFilterParamsForDashboard } from "@/helpers/client/filter";-import { fetchDatabaseConfigList } from "@/helpers/client/database-config";import Loader from "@/components/common/loader";import { usePageHeader } from "@/selectors/page";import { usePostHog } from "posthog-js/react";@@ -35,8 +30,7 @@ export default function DashboardPage() {const projects = useRootStore(getProjectList);const currentProject = useRootStore(getCurrentProject);const isProjectLoading = useRootStore(getProjectIsLoading);- const databaseConfigs = useRootStore(getDatabaseConfigList);- const isDatabaseConfigLoading = useRootStore(getDatabaseConfigListIsLoading);+ const [hasDbConfig, setHasDbConfig] = useState<boolean>();const { fireRequest, isLoading } = useFetchWrapper();const { fireRequest: fireRunQuery } = useFetchWrapper();const [initialConfig, setInitialConfig] = useState<@@ -47,16 +41,18 @@ export default function DashboardPage() {const { setHeader } = usePageHeader();const posthog = usePostHog();const hasProject = Boolean(currentProject?.id && (projects?.length || 0) > 0);- const hasDbConfig = Boolean(databaseConfigs?.length);const isSetupLoading =isProjectLoading ||- isDatabaseConfigLoading ||- projects === undefined ||- (hasProject && databaseConfigs === undefined);+ hasDbConfig === undefined ||+ projects === undefined;useEffect(() => {if (currentProject?.id) {- fetchDatabaseConfigList(() => {});+ setHasDbConfig(undefined);+ fetch("/api/connectors")+ .then((response) => response.ok ? response.json() : { connectors: [] })+ .then((body) => setHasDbConfig((body.connectors || []).some((connector: { type?: string }) => connector.type === "clickhouse")))+ .catch(() => setHasDbConfig(false));}}, [currentProject?.id]);diff --git a/src/client/src/app/(playground)/layout.tsx b/src/client/src/app/(playground)/layout.tsxindex 7d3001d9..c7fa0823 100644--- a/src/client/src/app/(playground)/layout.tsx+++ b/src/client/src/app/(playground)/layout.tsx@@ -6,7 +6,6 @@ import PostHogScope from "@/components/(playground)/posthog-scope";import NavigationEvents from "@/components/common/navigation-events";import AppInit from "@/components/common/app-init";import { PortalProvider } from "@/components/(playground)/header-portal";-import ChatFloatingButton from "@/components/(playground)/chat/chat-floating-button";import PlaygroundShell from "@/components/(playground)/playground-shell";import {EnterpriseFeatureAccessProvider,@@ -34,7 +33,6 @@ export default async function PlaygroundLayout({</EnterpriseFeatureRouteGate></ClickhouseConnectivityWrapper></PlaygroundShell>- <ChatFloatingButton /></PortalProvider></EnterpriseFeatureAccessProvider></TooltipProvider>diff --git a/src/client/src/app/(playground)/onboarding/page.tsx b/src/client/src/app/(playground)/onboarding/page.tsxindex da5b6f62..0229673a 100644--- a/src/client/src/app/(playground)/onboarding/page.tsx+++ b/src/client/src/app/(playground)/onboarding/page.tsx@@ -38,10 +38,6 @@ import {getProjectIsLoading,getProjectList,} from "@/selectors/project";-import {- getDatabaseConfigList,- getDatabaseConfigListIsLoading,-} from "@/selectors/database-config";import {acceptInvitation,declineInvitation,@@ -49,7 +45,6 @@ import {fetchPendingInvitations,} from "@/helpers/client/organisation";import { fetchProjectList } from "@/helpers/client/project";-import { fetchDatabaseConfigList } from "@/helpers/client/database-config";import { postData } from "@/utils/api";import asaw from "@/utils/asaw";import getMessage from "@/constants/messages";@@ -57,6 +52,7 @@ import { DEFAULT_LOGGED_IN_ROUTE } from "@/constants/route";import Link from "next/link";import Loader from "@/components/common/loader";import FeaturePageHeader from "@/components/(playground)/feature-page-header";+import ProjectEnvironmentSwitcher from "@/components/(playground)/organisation/project-environment-switcher";function TimelineStep({active,@@ -134,7 +130,6 @@ function TimelineStep({</div>);}-export default function OnboardingPage() {const posthog = usePostHog();const { update: updateSession } = useSession();@@ -147,6 +142,7 @@ export default function OnboardingPage() {const [hasLoadedOrganisations, setHasLoadedOrganisations] = useState(false);const [projectName, setProjectName] = useState("");const [isCreatingProject, setIsCreatingProject] = useState(false);+ const [onboardingEnvironment, setOnboardingEnvironment] = useState("production");const pendingInvitations = useRootStore(getOrganisationPendingInvitations);const organisationList = useRootStore(getOrganisationList);const currentOrg = useRootStore(getCurrentOrganisation);@@ -154,10 +150,9 @@ export default function OnboardingPage() {const projects = useRootStore(getProjectList);const currentProject = useRootStore(getCurrentProject);const isProjectLoading = useRootStore(getProjectIsLoading);- const databaseConfigs = useRootStore(getDatabaseConfigList);- const isDatabaseConfigLoading = useRootStore(getDatabaseConfigListIsLoading);+ const [connectorLoading, setConnectorLoading] = useState(false);+ const [hasDbConfig, setHasDbConfig] = useState(false);const hasProject = Boolean(currentProject?.id && (projects?.length || 0) > 0);- const hasDbConfig = Boolean(databaseConfigs?.length);const isSetupComplete = Boolean(currentOrg?.id && hasProject && hasDbConfig);const isInitialising =!currentOrg?.id &&@@ -185,7 +180,12 @@ export default function OnboardingPage() {useEffect(() => {if (currentProject?.id) {- fetchDatabaseConfigList(() => {});+ setConnectorLoading(true);+ fetch("/api/connectors")+ .then((response) => response.ok ? response.json() : { connectors: [] })+ .then((body) => setHasDbConfig((body.connectors || []).some((connector: { type?: string }) => connector.type === "clickhouse")))+ .catch(() => setHasDbConfig(false))+ .finally(() => setConnectorLoading(false));}}, [currentProject?.id]);@@ -239,6 +239,21 @@ export default function OnboardingPage() {return true;};+ const setCurrentOrgForSetup = async (orgId: string) => {+ if (isCompleting) return false;+ setIsCompleting(true);+ const [setOrgErr] = await asaw(+ postData({ url: `/api/organisation/current/${orgId}`, data: {} })+ );+ if (setOrgErr) {+ setIsCompleting(false);+ return false;+ }+ await updateSession();+ window.location.href = "/onboarding";+ return true;+ };+const handleCreateOrganisation = async () => {if (!orgName.trim() || isCompleting) return;@@ -252,7 +267,7 @@ export default function OnboardingPage() {return;}- await setCurrentOrgAndComplete(result.id);+ await setCurrentOrgForSetup(result.id);} finally {setIsCreating(false);}@@ -391,24 +406,50 @@ export default function OnboardingPage() {>{hasProject && !hasDbConfig && currentProject?.id ? (<Button asChild size="sm" className="h-9">- <Link href={`/organisation/project/${currentProject.id}?tab=database`}>+ <Link href={`/organisation/project/${currentProject.id}/connectors`}><Database className="mr-1.5 h-3.5 w-3.5" />- {isDatabaseConfigLoading+ {connectorLoading? messages.LOADING: messages.ADD_NEW_CONFIG}</Link></Button>) : null}</TimelineStep>- <TimelineStep- active={isSetupComplete}- complete={isSetupComplete}- description={messages.HOME_SETUP_READY_DESCRIPTION}- icon={<Sparkles className="h-4 w-4" />}- isLast- stepNumber={4}- title={messages.HOME_SETUP_READY_STEP}- />+ <TimelineStep+ active={hasProject && hasDbConfig}+ complete={isSetupComplete}+ description="Choose the environment that owns this project's database and observability connectors."+ icon={<FolderKanban className="h-4 w-4" />}+ stepNumber={4}+ title="Set up an environment"+ >+ {hasProject && hasDbConfig ? (+ <div className="flex flex-wrap items-center gap-2">+ <ProjectEnvironmentSwitcher value={onboardingEnvironment} onChange={setOnboardingEnvironment} />+ <Button asChild size="sm" variant="outline" className="h-9">+ <Link href={`/organisation/project/${currentProject?.id}/connectors`}>+ <Plus className="mr-1.5 h-3.5 w-3.5" />+ Add data connectors+ </Link>+ </Button>+ </div>+ ) : null}+ </TimelineStep>+ <TimelineStep+ active={isSetupComplete}+ complete={isSetupComplete}+ description="Telemetry, dashboards, evaluations, AI analysis, and costing will use this environment's signal routing."+ icon={<Sparkles className="h-4 w-4" />}+ isLast+ stepNumber={5}+ title={messages.HOME_SETUP_READY_STEP}+ >+ {isSetupComplete ? (+ <Button size="sm" className="h-9" onClick={() => currentOrg?.id && setCurrentOrgAndComplete(currentOrg.id)} disabled={isCompleting}>+ {isCompleting ? messages.LOADING : "Finish setup"}+ </Button>+ ) : null}+ </TimelineStep></CardContent></Card>)}diff --git a/src/client/src/app/(playground)/organisation/project/[projectId]/connectors/page.tsx b/src/client/src/app/(playground)/organisation/project/[projectId]/connectors/page.tsxnew file mode 100644index 00000000..630da1d6--- /dev/null+++ b/src/client/src/app/(playground)/organisation/project/[projectId]/connectors/page.tsx@@ -0,0 +1,9 @@+import ProjectConnectorsPage from "@/components/(playground)/organisation/project-connectors-page";++export default function ProjectConnectorsRoutePage({+ params,+}: {+ params: { projectId: string };+}) {+ return <ProjectConnectorsPage projectId={params.projectId} />;+}diff --git a/src/client/src/app/(playground)/organisation/project/[projectId]/environments/page.tsx b/src/client/src/app/(playground)/organisation/project/[projectId]/environments/page.tsxnew file mode 100644index 00000000..18533e17--- /dev/null+++ b/src/client/src/app/(playground)/organisation/project/[projectId]/environments/page.tsx@@ -0,0 +1,9 @@+import ProjectConnectorsPage from "@/components/(playground)/organisation/project-connectors-page";++export default function ProjectEnvironmentsRoutePage({+ params,+}: {+ params: { projectId: string };+}) {+ return <ProjectConnectorsPage projectId={params.projectId} />;+}diff --git a/src/client/src/app/api/agents/[agentKey]/graph/route.ts b/src/client/src/app/api/agents/[agentKey]/graph/route.tsindex 5c0e97fb..372ff6d5 100644--- a/src/client/src/app/api/agents/[agentKey]/graph/route.ts+++ b/src/client/src/app/api/agents/[agentKey]/graph/route.ts@@ -1,8 +1,9 @@import { getAgent } from "@/lib/platform/agents";-import { POLICY_TOOLS, swr } from "@/lib/platform/agents/cache";+import { POLICY_EMPTY_GRAPH, POLICY_TOOLS, swr } from "@/lib/platform/agents/cache";import { getAggregateGraph } from "@/lib/platform/agents/aggregate-graph";import { getVersionWindow } from "@/lib/platform/agents/version-filter";import { withCacheHeaders } from "../../_cache";+import { withRouteAccess } from "@/lib/access/route-access";/*** GET /api/agents/[agentKey]/graph?versionHash=...@@ -11,7 +12,7 @@ import { withCacheHeaders } from "../../_cache";* `versionHash` is omitted, falls back to a 24h aggregate so the canvas* still has something to render before a version is picked.*/-export async function GET(+async function GETHandler(request: Request,{ params }: { params: Promise<{ agentKey: string }> }) {@@ -19,24 +20,48 @@ export async function GET(const url = new URL(request.url);const versionHash = url.searchParams.get("versionHash") || undefined;- const cacheKey = `agents:graph:default:${agentKey}:${versionHash || "all"}`;+ // v3: empty Tempo/Jaeger samples use a short TTL — v2 cached empties for 5m.+ const cacheKey = `agents:graph:v3:${agentKey}:${versionHash || "all"}`;- const result = await swr(cacheKey, POLICY_TOOLS, async () => {- const agent = await getAgent({ agentKey });- if (!agent) return null;- const versionFilter = versionHash- ? await getVersionWindow(agentKey, versionHash)- : null;- const graph = await getAggregateGraph({- serviceName: agent.service_name,- environment: agent.environment,- versionFilter,- });- return { agent, graph, versionFilter };- });+ const result = await swr(+ cacheKey,+ POLICY_TOOLS,+ async () => {+ const agent = await getAgent({ agentKey });+ if (!agent) return null;+ const versionFilter = versionHash+ ? await getVersionWindow(agentKey, versionHash)+ : null;+ const startParam = url.searchParams.get("start");+ const endParam = url.searchParams.get("end");+ const timeRange =+ startParam && endParam+ ? { start: new Date(startParam), end: new Date(endParam) }+ : undefined;+ const graph = await getAggregateGraph({+ serviceName: agent.service_name,+ environment: agent.environment,+ versionFilter,+ timeRange,+ });+ return { agent, graph, versionFilter };+ },+ {+ policyFor: (value) =>+ value?.graph &&+ value.graph.nodes.length === 0 &&+ value.graph.edges.length === 0+ ? POLICY_EMPTY_GRAPH+ : POLICY_TOOLS,+ }+ );if (!result) {return Response.json({ error: "Agent not found" }, { status: 404 });}- return withCacheHeaders({ data: result }, "graph");+ const emptyGraph =+ result.graph.nodes.length === 0 && result.graph.edges.length === 0;+ return withCacheHeaders({ data: result }, emptyGraph ? "graphEmpty" : "graph");}++export const GET = withRouteAccess("observability.read", GETHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/agents/[agentKey]/refresh/route.ts b/src/client/src/app/api/agents/[agentKey]/refresh/route.tsindex 559c50d1..a775d3a8 100644--- a/src/client/src/app/api/agents/[agentKey]/refresh/route.ts+++ b/src/client/src/app/api/agents/[agentKey]/refresh/route.ts@@ -1,5 +1,6 @@import { getAgent } from "@/lib/platform/agents";import { materializeAgents } from "@/lib/platform/agents/materialize";+import { withRouteAccess } from "@/lib/access/route-access";/*** Synchronous materialization for a single agent. Rate-limited to 1 call /@@ -8,7 +9,7 @@ import { materializeAgents } from "@/lib/platform/agents/materialize";const RATE_WINDOW_MS = 10_000;const lastRefreshAt = new Map<string, number>();-export async function POST(+async function POSTHandler(_request: Request,{ params }: { params: Promise<{ agentKey: string }> }) {@@ -43,3 +44,5 @@ export async function POST(return Response.json({ success: true, result });}++export const POST = withRouteAccess("observability.read", POSTHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/agents/[agentKey]/route.ts b/src/client/src/app/api/agents/[agentKey]/route.tsindex a387422b..c8bede0a 100644--- a/src/client/src/app/api/agents/[agentKey]/route.ts+++ b/src/client/src/app/api/agents/[agentKey]/route.ts@@ -1,7 +1,8 @@import { getAgent } from "@/lib/platform/agents";import { withCacheHeaders } from "../_cache";+import { withRouteAccess } from "@/lib/access/route-access";-export async function GET(+async function GETHandler(_request: Request,{ params }: { params: Promise<{ agentKey: string }> }) {@@ -12,3 +13,5 @@ export async function GET(}return withCacheHeaders({ data: agent }, "detail");}++export const GET = withRouteAccess("observability.read", GETHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/agents/[agentKey]/snapshot/route.ts b/src/client/src/app/api/agents/[agentKey]/snapshot/route.tsindex ac361e6c..da76136e 100644--- a/src/client/src/app/api/agents/[agentKey]/snapshot/route.ts+++ b/src/client/src/app/api/agents/[agentKey]/snapshot/route.ts@@ -1,6 +1,7 @@import { getAgent } from "@/lib/platform/agents";import { getLatestVersion } from "@/lib/platform/agents/snapshot";import { withCacheHeaders } from "../../_cache";+import { withRouteAccess } from "@/lib/access/route-access";/*** Returns the agent + its latest version. Backed by `getAgent` and@@ -10,7 +11,7 @@ import { withCacheHeaders } from "../../_cache";* `getAgent` directly. Both routes now share `agents:detail:*` so an* invalidation on materialize / click reflects in both views together.*/-export async function GET(+async function GETHandler(_request: Request,{ params }: { params: Promise<{ agentKey: string }> }) {@@ -24,3 +25,5 @@ export async function GET(}return withCacheHeaders({ data: { agent, version } }, "detail");}++export const GET = withRouteAccess("observability.read", GETHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/agents/[agentKey]/tools/route.ts b/src/client/src/app/api/agents/[agentKey]/tools/route.tsindex 544597c2..7a87f675 100644--- a/src/client/src/app/api/agents/[agentKey]/tools/route.ts+++ b/src/client/src/app/api/agents/[agentKey]/tools/route.ts@@ -1,8 +1,9 @@import { POLICY_TOOLS, swr } from "@/lib/platform/agents/cache";import { getLatestVersion } from "@/lib/platform/agents/snapshot";import { withCacheHeaders } from "../../_cache";+import { withRouteAccess } from "@/lib/access/route-access";-export async function GET(+async function GETHandler(_request: Request,{ params }: { params: Promise<{ agentKey: string }> }) {@@ -14,3 +15,5 @@ export async function GET(});return withCacheHeaders({ data: tools }, "versions");}++export const GET = withRouteAccess("observability.read", GETHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/agents/[agentKey]/versions/[versionHash]/route.ts b/src/client/src/app/api/agents/[agentKey]/versions/[versionHash]/route.tsindex 0d15c1a0..de50ef17 100644--- a/src/client/src/app/api/agents/[agentKey]/versions/[versionHash]/route.ts+++ b/src/client/src/app/api/agents/[agentKey]/versions/[versionHash]/route.ts@@ -1,8 +1,9 @@import { POLICY_VERSIONS, swr } from "@/lib/platform/agents/cache";import { getVersion } from "@/lib/platform/agents/snapshot";import { withCacheHeaders } from "../../../_cache";+import { withRouteAccess } from "@/lib/access/route-access";-export async function GET(+async function GETHandler(_request: Request,{params,@@ -20,3 +21,5 @@ export async function GET(}return withCacheHeaders({ data: version }, "versions");}++export const GET = withRouteAccess("observability.read", GETHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/agents/[agentKey]/versions/[versionHash]/window/route.ts b/src/client/src/app/api/agents/[agentKey]/versions/[versionHash]/window/route.tsindex 61e344ab..6110b9c9 100644--- a/src/client/src/app/api/agents/[agentKey]/versions/[versionHash]/window/route.ts+++ b/src/client/src/app/api/agents/[agentKey]/versions/[versionHash]/window/route.ts@@ -1,5 +1,6 @@import { getVersionWindow } from "@/lib/platform/agents/version-filter";import { withCacheHeaders } from "../../../../_cache";+import { withRouteAccess } from "@/lib/access/route-access";/*** Resolves an agent version into the `VersionFilter` shape expected by@@ -7,7 +8,7 @@ import { withCacheHeaders } from "../../../../_cache";* hasAttributeSpans}`. The page hits this whenever `?versionHash=` changes so* downstream dashboard/requests queries can scope to that version's traffic.*/-export async function GET(+async function GETHandler(_request: Request,{params,@@ -20,3 +21,5 @@ export async function GET(}return withCacheHeaders({ data: filter }, "versions");}++export const GET = withRouteAccess("observability.read", GETHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/agents/[agentKey]/versions/route.ts b/src/client/src/app/api/agents/[agentKey]/versions/route.tsindex 0c937c13..9e3fc09d 100644--- a/src/client/src/app/api/agents/[agentKey]/versions/route.ts+++ b/src/client/src/app/api/agents/[agentKey]/versions/route.ts@@ -1,8 +1,9 @@import { POLICY_VERSIONS, swr } from "@/lib/platform/agents/cache";import { getVersions } from "@/lib/platform/agents/snapshot";import { withCacheHeaders } from "../../_cache";+import { withRouteAccess } from "@/lib/access/route-access";-export async function GET(+async function GETHandler(request: Request,{ params }: { params: Promise<{ agentKey: string }> }) {@@ -17,3 +18,5 @@ export async function GET();return withCacheHeaders({ data: versions }, "versions");}++export const GET = withRouteAccess("observability.read", GETHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/agents/[agentKey]/versions/timeline/route.ts b/src/client/src/app/api/agents/[agentKey]/versions/timeline/route.tsindex 0fe1a3f1..7abde4c1 100644--- a/src/client/src/app/api/agents/[agentKey]/versions/timeline/route.ts+++ b/src/client/src/app/api/agents/[agentKey]/versions/timeline/route.ts@@ -4,6 +4,7 @@ import {_bucketPresets,} from "@/lib/platform/agents/snapshot";import { withCacheHeaders } from "../../../_cache";+import { withRouteAccess } from "@/lib/access/route-access";/*** GET /api/agents/[agentKey]/versions/timeline@@ -20,7 +21,7 @@ import { withCacheHeaders } from "../../../_cache";* the most recent version whose `first_seen` <= bucket — see* `getVersionTimeline` for the hybrid attribution logic.*/-export async function GET(+async function GETHandler(request: Request,{ params }: { params: Promise<{ agentKey: string }> }) {@@ -55,3 +56,5 @@ export async function GET();return withCacheHeaders({ data }, "timeline");}++export const GET = withRouteAccess("observability.read", GETHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/agents/_cache.ts b/src/client/src/app/api/agents/_cache.tsindex cfb2d1e2..2cb57dee 100644--- a/src/client/src/app/api/agents/_cache.ts+++ b/src/client/src/app/api/agents/_cache.ts@@ -39,6 +39,8 @@ export const CACHE_HEADERS = {* shifts as new requests come in.*/graph: "private, max-age=15, s-maxage=60, stale-while-revalidate=300",+ /** Empty DAGs (Tempo/Jaeger ingest lag) must not linger in the browser. */+ graphEmpty: "private, max-age=5, s-maxage=5, stale-while-revalidate=0",/*** Version timeline (bar chart). Stable per-version, refreshes on new* traffic.diff --git a/src/client/src/app/api/agents/materialize/route.ts b/src/client/src/app/api/agents/materialize/route.tsindex 6d3ec446..f9a83c48 100644--- a/src/client/src/app/api/agents/materialize/route.ts+++ b/src/client/src/app/api/agents/materialize/route.ts@@ -1,6 +1,7 @@import prisma from "@/lib/prisma";import asaw from "@/utils/asaw";import { materializeAgents } from "@/lib/platform/agents/materialize";+import { materializeTelemetryRollups } from "@/lib/platform/telemetry/rollups";/*** Cron-triggered endpoint that refreshes openlit_agents_summary +@@ -108,7 +109,30 @@ export async function POST(request: Request) {runningLock.add(globalKey);try {- const [err, configs] = await asaw(prisma.databaseConfig.findMany({}));+ // Materialization is intelligence work. Only the ClickHouse connector+ // currently routed for the intelligence signal is active; probing every+ // historical/database connector makes an unreachable old config fail the+ // cron and produces noisy ECONNREFUSED/ENOTFOUND errors.+ const [bindingErr, activeBindings] = await asaw(+ prisma.telemetrySourceBinding.findMany({+ where: { signal: "intelligence", databaseConfigId: { not: null } },+ select: { databaseConfigId: true },+ })+ );+ if (bindingErr) {+ console.error("[agents materialize] failed to list intelligence bindings", bindingErr);+ return Response.json({ error: "Failed to list active configs" }, { status: 500 });+ }+ const activeConfigIds: string[] = Array.from(+ new Set(+ ((activeBindings as Array<{ databaseConfigId: string | null }> | undefined) || [])+ .map((binding) => binding.databaseConfigId)+ .filter((id): id is string => Boolean(id))+ )+ );+ const [err, configs] = await asaw(+ prisma.databaseConfig.findMany({ where: { id: { in: activeConfigIds } } })+ );if (err) {console.error("[agents materialize] failed to list db configs", err);return Response.json({ error: "Failed to list configs" }, { status: 500 });@@ -139,6 +163,25 @@ export async function POST(request: Request) {try {const result = await materializeAgents({ dbConfigId: config.id });summary[config.id] = result;+ try {+ const { getTelemetryAdapterForDbConfig } = await import(+ "@/lib/telemetry-source"+ );+ const { adapter, descriptor, isBuiltIn } =+ await getTelemetryAdapterForDbConfig(config.id, "traces");+ if (!isBuiltIn && descriptor.type !== "clickhouse") {+ await materializeTelemetryRollups({+ adapter,+ sourceId: descriptor.id,+ dbConfigId: config.id,+ });+ }+ } catch (rollupErr) {+ console.error(+ "[agents materialize] telemetry rollups failed",+ rollupErr+ );+ }} catch (e) {summary[config.id] = { error: String(e) };} finally {@@ -155,3 +198,4 @@ export async function POST(request: Request) {runningLock.delete(globalKey);}}+diff --git a/src/client/src/app/api/agents/route.ts b/src/client/src/app/api/agents/route.tsindex 8ecf4483..bbed7a34 100644--- a/src/client/src/app/api/agents/route.ts+++ b/src/client/src/app/api/agents/route.ts@@ -1,6 +1,7 @@import { listAgents } from "@/lib/platform/agents";import type { AgentListCursor, AgentListFilters, AgentSource } from "@/types/agents";import { withCacheHeaders } from "./_cache";+import { withRouteAccess } from "@/lib/access/route-access";// "coding" is intentionally allowed here so the Coding Agents tab can// pass `?source=coding` and read its rows out of the same endpoint.@@ -22,7 +23,7 @@ function parseCsv(value: string | null): string[] {.filter((v) => v.length > 0);}-export async function GET(request: Request) {+async function GETHandler(request: Request) {const url = new URL(request.url);const sp = url.searchParams;@@ -86,3 +87,5 @@ export async function GET(request: Request) {"list");}++export const GET = withRouteAccess("observability.read", GETHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/chat/improvement/[spanId]/route.ts b/src/client/src/app/api/chat/improvement/[spanId]/route.tsindex 8a747f55..e94acab3 100644--- a/src/client/src/app/api/chat/improvement/[spanId]/route.ts+++ b/src/client/src/app/api/chat/improvement/[spanId]/route.ts@@ -4,10 +4,6 @@ import {getTraceImprovement,streamTraceImprovementAnalysis,} from "@/lib/platform/chat/improvement";-import {- withOtterDbChatAccess,- withOtterDbReadAccess,-} from "@/lib/chat/access";import { getCurrentUser } from "@/lib/session";import PostHogServer from "@/lib/posthog";import asaw from "@/utils/asaw";@@ -30,25 +26,51 @@ function getScope(request: Request) {return scope === "span" ? "span" : "trace";}-async function getHandler(request: Request, context: any) {+function getEnvironment(request: Request) {+ return request.headers.get("x-openlit-environment") || undefined;+}++function getTraceId(request: Request) {+ return new URL(request.url).searchParams.get("traceId") || undefined;+}++export async function GET(request: Request, context: any) {+ const startTimestamp = Date.now();const user = await getCurrentUser();if (!user) {logRoute("get_unauthorized", {});+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.AI_ANALYSIS_GET_FAILURE,+ startTimestamp,+ properties: { reason: "unauthorized" },+ });return Response.json("Unauthorized", { status: 401 });}const { spanId } = context.params || {};if (!spanId) {logRoute("get_missing_span", {});+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.AI_ANALYSIS_GET_FAILURE,+ startTimestamp,+ properties: { reason: "missing_span" },+ });return Response.json("No span id provided", { status: 400 });}const databaseConfigId = await getDatabaseConfigId();const scope = getScope(request);- logRoute("get_start", { spanId, scope, databaseConfigId });- const { data, err } = await getTraceImprovement(spanId, databaseConfigId, scope);+ const environment = getEnvironment(request);+ const traceId = getTraceId(request);+ logRoute("get_start", { spanId, traceId, scope, environment, databaseConfigId });+ const { data, err } = await getTraceImprovement(spanId, databaseConfigId, scope, environment, traceId);if (err) {logRoute("get_failed", { spanId, scope, err });+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.AI_ANALYSIS_GET_FAILURE,+ startTimestamp,+ properties: { spanId, scope, databaseConfigId, error: err },+ });return Response.json(err, { status: 400 });}logRoute("get_done", {@@ -57,11 +79,22 @@ async function getHandler(request: Request, context: any) {rootSpanId: data?.rootSpanId,runCount: data?.runs?.length || 0,});+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.AI_ANALYSIS_GET_SUCCESS,+ startTimestamp,+ properties: {+ spanId,+ scope,+ databaseConfigId,+ rootSpanId: data?.rootSpanId,+ runCount: data?.runs?.length || 0,+ },+ });return Response.json({ data: data || null });}-async function postHandler(request: Request, context: any) {+export async function POST(request: Request, context: any) {const startTimestamp = Date.now();const user = await getCurrentUser();if (!user) {@@ -87,11 +120,15 @@ async function postHandler(request: Request, context: any) {const databaseConfigId = await getDatabaseConfigId();const scope = getScope(request);- logRoute("post_start", { spanId, scope, databaseConfigId });+ const environment = getEnvironment(request);+ const traceId = getTraceId(request);+ logRoute("post_start", { spanId, traceId, scope, environment, databaseConfigId });const { response, err } = await streamTraceImprovementAnalysis(spanId,databaseConfigId,- scope+ scope,+ environment,+ traceId);if (err) {logRoute("post_failed", { spanId, scope, err });@@ -122,6 +159,3 @@ async function postHandler(request: Request, context: any) {return response;}--export const GET = withOtterDbReadAccess(getHandler);-export const POST = withOtterDbChatAccess(postHandler);diff --git a/src/client/src/app/api/chat/message/execute/route.ts b/src/client/src/app/api/chat/message/execute/route.tsindex 8c727057..d24a890b 100644--- a/src/client/src/app/api/chat/message/execute/route.ts+++ b/src/client/src/app/api/chat/message/execute/route.ts@@ -1,7 +1,11 @@import { getCurrentUser } from "@/lib/session";-import { dataCollector } from "@/lib/platform/common";+import { intelligenceDataCollector } from "@/lib/platform/common";import { validateSQL } from "@/lib/platform/chat/sql-validator";import { updateMessage } from "@/lib/platform/chat/conversation";+import { isNativeSqlChatAvailable } from "@/lib/telemetry-source";+import { TELEMETRY_SOURCE_CHAT_NATIVE_ONLY } from "@/constants/messages/en";+import { OPENLIT_CONTEXT_HEADERS } from "@/constants/openlit-context";+import { authorizeTelemetrySQLRouting } from "@/lib/platform/chat/telemetry-sql-routing";import { NextRequest } from "next/server";export async function POST(request: NextRequest) {@@ -10,6 +14,20 @@ export async function POST(request: NextRequest) {return Response.json("Unauthorized", { status: 401 });}+ // Natural-language SQL chat runs raw ClickHouse SQL, so it is only+ // available through the ClickHouse connector routed for intelligence.+ const environment = request.headers.get(OPENLIT_CONTEXT_HEADERS.environment) || undefined;+ const chatSource = await isNativeSqlChatAvailable({+ signal: "intelligence",+ environment,+ });+ if (!chatSource.available) {+ return Response.json(+ { err: TELEMETRY_SOURCE_CHAT_NATIVE_ONLY(chatSource.sourceName) },+ { status: 409 }+ );+ }+const body = await request.json();const { messageId, query } = body;@@ -25,14 +43,21 @@ export async function POST(request: NextRequest) {{ status: 400 });}+ const routing = await authorizeTelemetrySQLRouting(validation.query!, {+ environment,+ databaseConfigId: chatSource.databaseConfigId!,+ });+ if (!routing.allowed) {+ return Response.json({ err: routing.error }, { status: 409 });+ }const startTime = Date.now();// Execute with readonly mode- const { data, err } = await dataCollector({+ const { data, err } = await intelligenceDataCollector({query: validation.query!,enable_readonly: true,- });+ }, "query", chatSource.databaseConfigId);const executionTimeMs = Date.now() - startTime;diff --git a/src/client/src/app/api/chat/message/route.ts b/src/client/src/app/api/chat/message/route.tsindex 7f883dcd..9e801bfa 100644--- a/src/client/src/app/api/chat/message/route.ts+++ b/src/client/src/app/api/chat/message/route.ts@@ -6,6 +6,7 @@ import { streamChatMessage, formatStreamError } from "@/lib/platform/chat/streamimport PostHogServer from "@/lib/posthog";import { NextRequest } from "next/server";import asaw from "@/utils/asaw";+import { OPENLIT_CONTEXT_HEADERS } from "@/constants/openlit-context";export async function POST(request: NextRequest) {const startTimestamp = Date.now();@@ -46,6 +47,7 @@ export async function POST(request: NextRequest) {const [, dbConfig] = await asaw(getDBConfigByUser(true));const dbConfigId = (dbConfig as any)?.id || "";+ const environment = request.headers.get(OPENLIT_CONTEXT_HEADERS.environment) || undefined;try {const encoder = new TextEncoder();@@ -62,6 +64,7 @@ export async function POST(request: NextRequest) {model: config.model,userId: user.id,dbConfigId,+ environment,onDelta: (text) => send({ type: "delta", text }),onStep: (label, status = "active", detail) =>send({ type: "step", status, label, detail }),diff --git a/src/client/src/app/api/coding-agents/classification/dispute/route.ts b/src/client/src/app/api/coding-agents/classification/dispute/route.tsindex 73c1df8d..70997e0a 100644--- a/src/client/src/app/api/coding-agents/classification/dispute/route.ts+++ b/src/client/src/app/api/coding-agents/classification/dispute/route.ts@@ -11,11 +11,8 @@* UI doesn't claim definitive change of state.*/-import {- requireCodingAgentAuth,- CodingAgentUnauthorizedError,-} from "@/lib/platform/coding-agents/auth";-import { withCurrentOrganisationPermission } from "@/lib/rbac/current";+import { CodingAgentUnauthorizedError } from "@/lib/platform/coding-agents/auth";+import { requireCodingAgentQueryContext } from "@/lib/platform/coding-agents/source";import {DisputeError,submitClassificationDispute,@@ -25,13 +22,14 @@ import {validateClassificationDispute,type CodingAgentClassificationDispute,} from "@/lib/platform/coding-agents/classifier";+import { withRouteAccess } from "@/lib/access/route-access";export const dynamic = "force-dynamic";async function POSTHandler(request: Request) {let auth;try {- auth = await requireCodingAgentAuth();+ auth = await requireCodingAgentQueryContext(request);} catch (err) {if (err instanceof CodingAgentUnauthorizedError) {return Response.json({ error: err.message }, { status: 401 });@@ -84,4 +82,4 @@ async function POSTHandler(request: Request) {}}-export const POST = withCurrentOrganisationPermission("coding_agents:dispute", POSTHandler);+export const POST = withRouteAccess("coding_agents.dispute", POSTHandler);diff --git a/src/client/src/app/api/coding-agents/sessions/[sessionId]/digest/route.ts b/src/client/src/app/api/coding-agents/sessions/[sessionId]/digest/route.tsindex 32c4ad06..39856f1e 100644--- a/src/client/src/app/api/coding-agents/sessions/[sessionId]/digest/route.ts+++ b/src/client/src/app/api/coding-agents/sessions/[sessionId]/digest/route.ts@@ -19,22 +19,20 @@* users' aggregate metrics.*/-import {- requireCodingAgentAuth,- CodingAgentUnauthorizedError,-} from "@/lib/platform/coding-agents/auth";-import { withCurrentOrganisationPermission } from "@/lib/rbac/current";+import { CodingAgentUnauthorizedError } from "@/lib/platform/coding-agents/auth";+import { requireCodingAgentQueryContext } from "@/lib/platform/coding-agents/source";import { getCodingSessionDigest } from "@/lib/platform/coding-agents/queries";+import { withRouteAccess } from "@/lib/access/route-access";export const dynamic = "force-dynamic";async function GETHandler(- _request: Request,+ request: Request,context: { params: { sessionId: string } },) {let auth;try {- auth = await requireCodingAgentAuth();+ auth = await requireCodingAgentQueryContext(request);} catch (err) {if (err instanceof CodingAgentUnauthorizedError) {return Response.json({ error: err.message }, { status: 401 });@@ -59,4 +57,4 @@ async function GETHandler(}}-export const GET = withCurrentOrganisationPermission("coding_agents:read", GETHandler);+export const GET = withRouteAccess("coding_agents.read", GETHandler);diff --git a/src/client/src/app/api/coding-agents/sessions/route.ts b/src/client/src/app/api/coding-agents/sessions/route.tsindex 6209bd40..43b33a1e 100644--- a/src/client/src/app/api/coding-agents/sessions/route.ts+++ b/src/client/src/app/api/coding-agents/sessions/route.ts@@ -19,12 +19,10 @@ import {listSessions,type ListSessionsOptions,} from "@/lib/platform/coding-agents/queries";-import {- requireCodingAgentAuth,- CodingAgentUnauthorizedError,-} from "@/lib/platform/coding-agents/auth";-import { withCurrentOrganisationPermission } from "@/lib/rbac/current";+import { CodingAgentUnauthorizedError } from "@/lib/platform/coding-agents/auth";+import { requireCodingAgentQueryContext } from "@/lib/platform/coding-agents/source";import { isCodingAgentClassification } from "@/lib/platform/coding-agents/classifier";+import { withRouteAccess } from "@/lib/access/route-access";export const dynamic = "force-dynamic";@@ -85,7 +83,7 @@ function defaultSince(): Date {async function POSTHandler(request: Request) {let auth;try {- auth = await requireCodingAgentAuth();+ auth = await requireCodingAgentQueryContext(request);} catch (err) {if (err instanceof CodingAgentUnauthorizedError) {return Response.json({ error: err.message }, { status: 401 });@@ -138,4 +136,4 @@ async function POSTHandler(request: Request) {}}-export const POST = withCurrentOrganisationPermission("coding_agents:read", POSTHandler);+export const POST = withRouteAccess("coding_agents.read", POSTHandler);diff --git a/src/client/src/app/api/coding-agents/sessions/summary/route.ts b/src/client/src/app/api/coding-agents/sessions/summary/route.tsindex 5ff3d924..62f0dc2a 100644--- a/src/client/src/app/api/coding-agents/sessions/summary/route.ts+++ b/src/client/src/app/api/coding-agents/sessions/summary/route.ts@@ -10,18 +10,16 @@* consistent with the list route.*/-import {- requireCodingAgentAuth,- CodingAgentUnauthorizedError,-} from "@/lib/platform/coding-agents/auth";-import { withCurrentOrganisationPermission } from "@/lib/rbac/current";-import { dataCollector } from "@/lib/platform/common";+import { CodingAgentUnauthorizedError } from "@/lib/platform/coding-agents/auth";+import { requireCodingAgentQueryContext } from "@/lib/platform/coding-agents/source";+import { intelligenceDataCollector } from "@/lib/platform/common";import {CODING_AGENT_ATTR,CODING_AGENT_SPAN_NAMES,GEN_AI_ATTR,OTEL_TRACES_TABLE,} from "@/lib/platform/coding-agents/table-details";+import { withRouteAccess } from "@/lib/access/route-access";export const dynamic = "force-dynamic";@@ -57,8 +55,9 @@ function pickBucket(start: Date | null, end: Date | null) {}async function POSTHandler(request: Request) {+ let auth;try {- await requireCodingAgentAuth();+ auth = await requireCodingAgentQueryContext(request);} catch (err) {if (err instanceof CodingAgentUnauthorizedError) {return Response.json({ error: err.message }, { status: 401 });@@ -137,7 +136,11 @@ async function POSTHandler(request: Request) {ORDER BY min(Timestamp)`;- const { data, err } = await dataCollector({ query });+ const { data, err } = await intelligenceDataCollector(+ { query },+ "query",+ auth.dbConfigId+ );if (err) {console.error("coding_agent.sessions.summary_failed", err);return Response.json({ error: "Internal error" }, { status: 500 });@@ -150,4 +153,4 @@ async function POSTHandler(request: Request) {return Response.json({ bucket, buckets, total, peak });}-export const POST = withCurrentOrganisationPermission("coding_agents:read", POSTHandler);+export const POST = withRouteAccess("coding_agents.read", POSTHandler);diff --git a/src/client/src/app/api/coding-agents/users/[userId]/route.ts b/src/client/src/app/api/coding-agents/users/[userId]/route.tsindex cbc47a3b..f17363c8 100644--- a/src/client/src/app/api/coding-agents/users/[userId]/route.ts+++ b/src/client/src/app/api/coding-agents/users/[userId]/route.ts@@ -9,12 +9,10 @@* floor is the privacy seal we promise viewer-tier callers.*/-import {- requireCodingAgentAuth,- CodingAgentUnauthorizedError,-} from "@/lib/platform/coding-agents/auth";-import { withCurrentOrganisationPermission } from "@/lib/rbac/current";+import { CodingAgentUnauthorizedError } from "@/lib/platform/coding-agents/auth";+import { requireCodingAgentQueryContext } from "@/lib/platform/coding-agents/source";import { getCodingUserDigest } from "@/lib/platform/coding-agents/queries";+import { withRouteAccess } from "@/lib/access/route-access";export const dynamic = "force-dynamic";@@ -24,7 +22,7 @@ async function GETHandler() {let auth;try {- auth = await requireCodingAgentAuth();+ auth = await requireCodingAgentQueryContext(request);} catch (err) {if (err instanceof CodingAgentUnauthorizedError) {return Response.json({ error: err.message }, { status: 401 });@@ -80,4 +78,4 @@ async function GETHandler(}}-export const GET = withCurrentOrganisationPermission("coding_agents:read", GETHandler);+export const GET = withRouteAccess("coding_agents.read", GETHandler);diff --git a/src/client/src/app/api/coding-agents/users/route.ts b/src/client/src/app/api/coding-agents/users/route.tsindex 0a907b46..2a04acf8 100644--- a/src/client/src/app/api/coding-agents/users/route.ts+++ b/src/client/src/app/api/coding-agents/users/route.ts@@ -11,16 +11,14 @@* `low_cohort` aggregate for non-admin callers. See `listCodingUsers`.*/-import {- requireCodingAgentAuth,- CodingAgentUnauthorizedError,-} from "@/lib/platform/coding-agents/auth";-import { withCurrentOrganisationPermission } from "@/lib/rbac/current";+import { CodingAgentUnauthorizedError } from "@/lib/platform/coding-agents/auth";+import { requireCodingAgentQueryContext } from "@/lib/platform/coding-agents/source";import {listCodingUsers,type CodingUsersSortBy,type ListCodingUsersOptions,} from "@/lib/platform/coding-agents/queries";+import { withRouteAccess } from "@/lib/access/route-access";const VALID_SORT_BY: CodingUsersSortBy[] = ["last_seen",@@ -67,7 +65,7 @@ function defaultSince(): Date {async function GETHandler(request: Request) {let auth;try {- auth = await requireCodingAgentAuth();+ auth = await requireCodingAgentQueryContext(request);} catch (err) {if (err instanceof CodingAgentUnauthorizedError) {return Response.json({ error: err.message }, { status: 401 });@@ -111,7 +109,7 @@ interface UsersListBody {async function POSTHandler(request: Request) {let auth;try {- auth = await requireCodingAgentAuth();+ auth = await requireCodingAgentQueryContext(request);} catch (err) {if (err instanceof CodingAgentUnauthorizedError) {return Response.json({ error: err.message }, { status: 401 });@@ -159,5 +157,5 @@ async function POSTHandler(request: Request) {}}-export const GET = withCurrentOrganisationPermission("coding_agents:read", GETHandler);-export const POST = withCurrentOrganisationPermission("coding_agents:read", POSTHandler);+export const GET = withRouteAccess("coding_agents.read", GETHandler);+export const POST = withRouteAccess("coding_agents.read", POSTHandler);diff --git a/src/client/src/app/api/coding-agents/users/summary/route.ts b/src/client/src/app/api/coding-agents/users/summary/route.tsindex 42bb7e5c..2d63a225 100644--- a/src/client/src/app/api/coding-agents/users/summary/route.ts+++ b/src/client/src/app/api/coding-agents/users/summary/route.ts@@ -10,18 +10,16 @@* any client-side branching.*/-import {- requireCodingAgentAuth,- CodingAgentUnauthorizedError,-} from "@/lib/platform/coding-agents/auth";-import { withCurrentOrganisationPermission } from "@/lib/rbac/current";-import { dataCollector } from "@/lib/platform/common";+import { CodingAgentUnauthorizedError } from "@/lib/platform/coding-agents/auth";+import { requireCodingAgentQueryContext } from "@/lib/platform/coding-agents/source";+import { intelligenceDataCollector } from "@/lib/platform/common";import {CODING_AGENT_ATTR,CODING_AGENT_SPAN_NAMES,GEN_AI_ATTR,OTEL_TRACES_TABLE,} from "@/lib/platform/coding-agents/table-details";+import { withRouteAccess } from "@/lib/access/route-access";export const dynamic = "force-dynamic";@@ -69,8 +67,9 @@ const USER_EXPR = ``;async function POSTHandler(request: Request) {+ let auth;try {- await requireCodingAgentAuth();+ auth = await requireCodingAgentQueryContext(request);} catch (err) {if (err instanceof CodingAgentUnauthorizedError) {return Response.json({ error: err.message }, { status: 401 });@@ -132,7 +131,7 @@ async function POSTHandler(request: Request) {ORDER BY min(Timestamp)`;- const { data, err } = await dataCollector({ query });+ const { data, err } = await intelligenceDataCollector({ query }, "query", auth.dbConfigId);if (err) {console.error("coding_agent.users.summary_failed", err);return Response.json({ error: "Internal error" }, { status: 500 });@@ -145,4 +144,4 @@ async function POSTHandler(request: Request) {return Response.json({ bucket, buckets, total, peak });}-export const POST = withCurrentOrganisationPermission("coding_agents:read", POSTHandler);+export const POST = withRouteAccess("coding_agents.read", POSTHandler);diff --git a/src/client/src/app/api/connectors/[id]/health/route.ts b/src/client/src/app/api/connectors/[id]/health/route.tsnew file mode 100644index 00000000..017f5790--- /dev/null+++ b/src/client/src/app/api/connectors/[id]/health/route.ts@@ -0,0 +1,52 @@+import { getCurrentUser } from "@/lib/session";+import { errorResponse } from "@/utils/api-response";+import asaw from "@/utils/asaw";+import {+ healthCheckTelemetrySource,+ validateTelemetrySourceAISignal,+} from "@/lib/telemetry-source-crud";+import { withConnectorAccess, withConnectorAudit } from "@/lib/access/connector-route";++const DEFAULT_PROBE_MS = 60 * 60 * 1000;++function telemetrySourceId(id: string) {+ return id.startsWith("telemetry:") ? id.slice("telemetry:".length) : id;+}++async function healthAndValidate(id: string) {+ const sourceId = telemetrySourceId(id);+ const [healthErr, health] = await asaw(healthCheckTelemetrySource(sourceId));+ if (healthErr) return errorResponse(healthErr, "Connector health check failed");++ const end = new Date();+ const start = new Date(end.getTime() - DEFAULT_PROBE_MS);+ const [validateErr, validation] = await asaw(+ validateTelemetrySourceAISignal(sourceId, { start, end })+ );+ if (validateErr) {+ return errorResponse(validateErr, "Connector AI validation failed");+ }++ return Response.json({ health, validation });+}++async function GETHandler(+ _request: Request,+ { params }: { params: { id: string } }+) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });+ return healthAndValidate(params.id);+}++async function POSTHandler(+ _request: Request,+ { params }: { params: { id: string } }+) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });+ return healthAndValidate(params.id);+}++export const GET = withConnectorAudit(withConnectorAccess("test", GETHandler));+export const POST = withConnectorAudit(withConnectorAccess("test", POSTHandler));diff --git a/src/client/src/app/api/connectors/[id]/route.ts b/src/client/src/app/api/connectors/[id]/route.tsnew file mode 100644index 00000000..6c551bdf--- /dev/null+++ b/src/client/src/app/api/connectors/[id]/route.ts@@ -0,0 +1,44 @@+import { getCurrentUser } from "@/lib/session";+import { errorResponse } from "@/utils/api-response";+import asaw from "@/utils/asaw";+import {+ deleteTelemetrySource,+ updateTelemetrySource,+} from "@/lib/telemetry-source-crud";+import { NextRequest } from "next/server";+import { withConnectorAccess, withConnectorAudit } from "@/lib/access/connector-route";++function telemetrySourceId(id: string) {+ return id.startsWith("telemetry:") ? id.slice("telemetry:".length) : id;+}++async function PATCHHandler(+ request: NextRequest,+ { params }: { params: { id: string } }+) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });+ let body: Record<string, unknown>;+ try {+ body = (await request.json()) as Record<string, unknown>;+ } catch {+ return Response.json({ err: "Invalid JSON" }, { status: 400 });+ }+ const [err, connector] = await asaw(updateTelemetrySource(telemetrySourceId(params.id), body));+ if (err) return errorResponse(err, "Failed to update connector");+ return Response.json({ ...connector, category: "datasource", scope: "project" });+}++async function DELETEHandler(+ _request: NextRequest,+ { params }: { params: { id: string } }+) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });+ const [err, result] = await asaw(deleteTelemetrySource(telemetrySourceId(params.id)));+ if (err) return errorResponse(err, "Failed to delete connector");+ return Response.json(result);+}++export const PATCH = withConnectorAudit(withConnectorAccess("update", PATCHHandler));+export const DELETE = withConnectorAudit(withConnectorAccess("delete", DELETEHandler));diff --git a/src/client/src/app/api/connectors/bindings/route.ts b/src/client/src/app/api/connectors/bindings/route.tsnew file mode 100644index 00000000..8ca5e86a--- /dev/null+++ b/src/client/src/app/api/connectors/bindings/route.ts@@ -0,0 +1,57 @@+import { getCurrentUser } from "@/lib/session";+import { errorResponse } from "@/utils/api-response";+import asaw from "@/utils/asaw";+import {+ deleteTelemetrySourceBinding,+ listTelemetrySourceBindings,+ setTelemetrySourceBinding,+} from "@/lib/telemetry-source-crud";+import { NextRequest } from "next/server";+import { withConnectorAccess, withConnectorAudit } from "@/lib/access/connector-route";+import { OPENLIT_CONTEXT_HEADERS } from "@/constants/openlit-context";++async function GETHandler(request: NextRequest) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });+ const environment = request.headers.get(OPENLIT_CONTEXT_HEADERS.environment) ||+ new URL(request.url).searchParams.get("environment") || undefined;+ const [err, bindings] = await asaw(listTelemetrySourceBindings(environment));+ if (err) return errorResponse(err, "Failed to list connector bindings");+ return Response.json({ bindings });+}++async function PUTHandler(request: NextRequest) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });+ let body: { signal?: unknown; connectorId?: string; sourceId?: string; environment?: string };+ try {+ body = (await request.json()) as {+ signal?: unknown;+ connectorId?: string;+ sourceId?: string;+ environment?: string;+ };+ } catch {+ return Response.json({ err: "Invalid JSON" }, { status: 400 });+ }+ const connectorId = body.connectorId || body.sourceId;+ if (!connectorId) return Response.json({ err: "connectorId is required" }, { status: 400 });+ const [err, binding] = await asaw(setTelemetrySourceBinding(body.signal, connectorId, body.environment));+ if (err) return errorResponse(err, "Failed to bind connector");+ return Response.json({ ...binding, connectorId: binding?.sourceId });+}++async function DELETEHandler(request: NextRequest) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });+ const signal = new URL(request.url).searchParams.get("signal");+ const environment = request.headers.get(OPENLIT_CONTEXT_HEADERS.environment) ||+ new URL(request.url).searchParams.get("environment") || undefined;+ const [err, result] = await asaw(deleteTelemetrySourceBinding(signal, environment));+ if (err) return errorResponse(err, "Failed to remove connector binding");+ return Response.json(result);+}++export const GET = withConnectorAccess("read", GETHandler);+export const PUT = withConnectorAudit(withConnectorAccess("bind", PUTHandler));+export const DELETE = withConnectorAudit(withConnectorAccess("bind", DELETEHandler));diff --git a/src/client/src/app/api/connectors/route.ts b/src/client/src/app/api/connectors/route.tsnew file mode 100644index 00000000..408eca75--- /dev/null+++ b/src/client/src/app/api/connectors/route.ts@@ -0,0 +1,122 @@+import { getCurrentUser } from "@/lib/session";+import { errorResponse } from "@/utils/api-response";+import asaw from "@/utils/asaw";+import { createTelemetrySource, availableSourceTypeDescriptors } from "@/lib/telemetry-source-crud";+import { upsertDBConfig } from "@/lib/db-config";+import { NextRequest } from "next/server";+import { withConnectorAccess, withConnectorAudit } from "@/lib/access/connector-route";+import { connectorIconPath } from "@/lib/platform/connectors/icons";+import {+ getCurrentOrganisation,+ getCurrentProjectForOrganisation,+} from "@/lib/organisation";+import { listProjectConnectorInstances } from "@/lib/platform/connectors/instances";+import { isVisibleConnectorType } from "@/lib/platform/connectors/visible-types";+import { validateOpenPlaitClickHouseConnection } from "@/lib/platform/openplait";++/**+ * Generic connector endpoint. Datasource instances are exposed through the+ * connector contract while legacy repositories remain compatibility stores+ * for existing platform features.+ */+async function GETHandler() {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });+ const organisation = await getCurrentOrganisation();+ const project = organisation?.id+ ? await getCurrentProjectForOrganisation(organisation.id)+ : null;+ if (!project?.id) return Response.json({ connectors: [] });+ const [err, connectors] = await asaw(listProjectConnectorInstances(project.id));+ if (err) return errorResponse(err, "Failed to list connectors");+ return Response.json({+ connectors: ((connectors || []) as Array<Record<string, unknown>>).filter((connector) => isVisibleConnectorType(connector.type)).map((connector) => {+ const { secretRef: _secretRef, ...safeConnector } = connector;+ return {+ ...safeConnector,+ icon: connectorIconPath(String(connector.type || "")),+ category: "datasource",+ scope: "project",+ };+ }),+ availableTypeDescriptors: availableSourceTypeDescriptors().filter((descriptor) => isVisibleConnectorType(descriptor.type)).map((descriptor) => ({+ ...descriptor,+ icon: connectorIconPath(descriptor.type),+ category: "datasource",+ scope: "project",+ })),+ });+}++async function POSTHandler(request: NextRequest) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });+ let body: Record<string, unknown>;+ try {+ body = (await request.json()) as Record<string, unknown>;+ } catch {+ return Response.json({ err: "Invalid JSON" }, { status: 400 });+ }+ if (body.category && body.category !== "datasource") {+ return Response.json(+ { err: `Connector category is not available in CE: ${String(body.category)}` },+ { status: 400 }+ );+ }+ if (body.type === "clickhouse") {+ const settings = body.settings && typeof body.settings === "object"+ ? body.settings as Record<string, unknown>+ : {};+ const credentials = body.credentials && typeof body.credentials === "object"+ ? body.credentials as Record<string, unknown>+ : {};+ const [validationErr] = await asaw(+ validateOpenPlaitClickHouseConnection({+ host: String(settings.host || ""),+ port: String(settings.port || ""),+ database: String(settings.database || ""),+ username: String(settings.username || ""),+ password: String(credentials.password || ""),+ query: String(settings.query || ""),+ })+ );+ if (validationErr) {+ return errorResponse(+ validationErr,+ "Failed to validate ClickHouse connector through OpenPlait"+ );+ }+ const [dbErr] = await asaw(upsertDBConfig({+ name: body.name,+ environment: body.environment,+ username: settings.username,+ host: settings.host,+ port: settings.port,+ database: settings.database,+ query: settings.query,+ password: credentials.password,+ } as any, typeof body.id === "string" ? body.id : undefined));+ if (dbErr) return errorResponse(dbErr, "Failed to save ClickHouse connector");+ const organisation = await getCurrentOrganisation();+ const project = organisation?.id+ ? await getCurrentProjectForOrganisation(organisation.id)+ : null;+ const refreshed = project?.id ? await listProjectConnectorInstances(project.id) : [];+ const connector = refreshed.find((item) =>+ item.type === "clickhouse" &&+ item.name === String(body.name || "") &&+ (item.environment || "production") === String(body.environment || "production")+ );+ return Response.json({+ ...(connector || { name: body.name, type: "clickhouse", environment: body.environment }),+ category: "datasource",+ scope: "project",+ });+ }+ const [err, source] = await asaw(createTelemetrySource(body));+ if (err) return errorResponse(err, "Failed to create connector");+ return Response.json({ ...source, category: "datasource", scope: "project" });+}++export const GET = withConnectorAccess("read", GETHandler);+export const POST = withConnectorAudit(withConnectorAccess("create", POSTHandler));diff --git a/src/client/src/app/api/connectors/types/route.ts b/src/client/src/app/api/connectors/types/route.tsnew file mode 100644index 00000000..04a1bb4c--- /dev/null+++ b/src/client/src/app/api/connectors/types/route.ts@@ -0,0 +1,22 @@+import { getCurrentUser } from "@/lib/session";+import { availableSourceTypeDescriptors } from "@/lib/telemetry-source-crud";+import { withConnectorAccess } from "@/lib/access/connector-route";+import { connectorIconPath } from "@/lib/platform/connectors/icons";+import { isVisibleConnectorType } from "@/lib/platform/connectors/visible-types";++async function GETHandler() {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });++ return Response.json({+ categories: ["datasource", "notification", "memory", "vector-store", "model-provider"],+ types: availableSourceTypeDescriptors().filter((descriptor) => isVisibleConnectorType(descriptor.type)).map((descriptor) => ({+ ...descriptor,+ icon: connectorIconPath(descriptor.type),+ category: "datasource",+ scope: "project",+ })),+ });+}++export const GET = withConnectorAccess("read", GETHandler);diff --git a/src/client/src/app/api/db-config/route.ts b/src/client/src/app/api/db-config/route.tsindex f456ec7b..501afce1 100644--- a/src/client/src/app/api/db-config/route.ts+++ b/src/client/src/app/api/db-config/route.ts@@ -4,6 +4,7 @@ import { getDBConfigByUser, upsertDBConfig } from "@/lib/db-config";import asaw from "@/utils/asaw";import { DatabaseConfig } from "@prisma/client";import { errorResponse } from "@/utils/api-response";+import { createProjectEnvironment } from "@/lib/project-environment";function stripSensitiveDbFields(config: any) {if (!config) return config;@@ -52,9 +53,19 @@ async function POSTHandler(request: Request) {const [err, res]: any = await asaw(upsertDBConfig(dbConfig, id));- if (err)+ if (err) {+ console.error("[api/db-config] save failed", {+ id: id || null,+ name: formData.name,+ environment: formData.environment,+ host: formData.host,+ port: formData.port,+ error: err,+ });return errorResponse(err, "Failed to save database configuration");+ }+ if (formData.environment) await createProjectEnvironment(formData.environment);return Response.json(res);}diff --git a/src/client/src/app/api/evaluation/[spanId]/feedback/route.ts b/src/client/src/app/api/evaluation/[spanId]/feedback/route.tsindex 3171d367..315d7d5d 100644--- a/src/client/src/app/api/evaluation/[spanId]/feedback/route.ts+++ b/src/client/src/app/api/evaluation/[spanId]/feedback/route.ts@@ -3,6 +3,7 @@ import { withCurrentOrganisationPermission } from "@/lib/rbac/current";import { storeManualFeedback } from "@/lib/platform/evaluation";import { SERVER_EVENTS } from "@/constants/events";import PostHogServer from "@/lib/posthog";+import { OPENLIT_CONTEXT_HEADERS } from "@/constants/openlit-context";async function POSTHandler(request: Request,@@ -23,10 +24,17 @@ async function POSTHandler();}+ const url = new URL(request.url);+ const environment = request.headers.get(OPENLIT_CONTEXT_HEADERS.environment) || undefined;const res: any = await storeManualFeedback(spanId,rating,- comment?.trim() || undefined+ comment?.trim() || undefined,+ undefined,+ {+ traceId: url.searchParams.get("traceId") || undefined,+ environment,+ });if (res?.err) {diff --git a/src/client/src/app/api/evaluation/[spanId]/route.ts b/src/client/src/app/api/evaluation/[spanId]/route.tsindex 2333cd1b..8e530221 100644--- a/src/client/src/app/api/evaluation/[spanId]/route.ts+++ b/src/client/src/app/api/evaluation/[spanId]/route.ts@@ -1,21 +1,30 @@-import { withAudit } from "@/lib/audit/route";-import { withCurrentOrganisationPermission } from "@/lib/rbac/current";import { getEvaluationsForSpanId, setEvaluationsForSpanId } from "@/lib/platform/evaluation";import { SERVER_EVENTS } from "@/constants/events";import PostHogServer from "@/lib/posthog";import { NextRequest } from "next/server";+import { OPENLIT_CONTEXT_HEADERS } from "@/constants/openlit-context";-async function GETHandler(+export async function GET(_: NextRequest,{ params }: { params: { spanId: string } }) {+ const startTimestamp = Date.now();const { spanId } = params;- const res: any = await getEvaluationsForSpanId(spanId);+ const url = new URL(_.url);+ const environment = _.headers.get(OPENLIT_CONTEXT_HEADERS.environment) || undefined;+ const res: any = await getEvaluationsForSpanId(spanId, {+ traceId: url.searchParams.get("traceId") || undefined,+ environment,+ });+ PostHogServer.fireEvent({+ event: res.err ? SERVER_EVENTS.EVALUATION_GET_FAILURE : SERVER_EVENTS.EVALUATION_GET_SUCCESS,+ startTimestamp,+ });return Response.json(res);}-async function POSTHandler(+export async function POST(request: Request,{ params }: { params: { spanId: string } }) {@@ -23,13 +32,33 @@ async function POSTHandler(const { spanId } = params;- const res: any = await setEvaluationsForSpanId(spanId);+ const url = new URL(request.url);+ const environment = request.headers.get(OPENLIT_CONTEXT_HEADERS.environment) || undefined;+ let res: any;+ try {+ res = await setEvaluationsForSpanId(spanId, {+ traceId: url.searchParams.get("traceId") || undefined,+ environment,+ });+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ console.error("[evaluation] run request failed", {+ spanId,+ traceId: url.searchParams.get("traceId") || null,+ error: message,+ });+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.EVALUATION_CREATE_FAILURE,+ startTimestamp,+ });+ return Response.json(+ { err: message },+ { status: message.toLowerCase().includes("trace") ? 404 : 400 }+ );+ }PostHogServer.fireEvent({event: res.err ? SERVER_EVENTS.EVALUATION_CREATE_FAILURE : SERVER_EVENTS.EVALUATION_CREATE_SUCCESS,startTimestamp,});return Response.json(res);}--export const GET = withCurrentOrganisationPermission("evaluation:read", GETHandler);-export const POST = withAudit(withCurrentOrganisationPermission("evaluation:run", POSTHandler));diff --git a/src/client/src/app/api/manage-dashboard/board/[id]/layout/export/route.ts b/src/client/src/app/api/manage-dashboard/board/[id]/layout/export/route.tsindex 8eb1543b..d1382044 100644--- a/src/client/src/app/api/manage-dashboard/board/[id]/layout/export/route.ts+++ b/src/client/src/app/api/manage-dashboard/board/[id]/layout/export/route.ts@@ -1,5 +1,6 @@import { SERVER_EVENTS } from "@/constants/events";import { getBoardLayout } from "@/lib/platform/manage-dashboard/board";+import { toExportDashboardPayload } from "@/lib/platform/manage-dashboard/board-format";import PostHogServer from "@/lib/posthog";import { NextRequest } from "next/server";@@ -11,7 +12,9 @@ export async function GET(const startTimestamp = Date.now();const res = await getBoardLayout(id);PostHogServer.fireEvent({- event: res.err ? SERVER_EVENTS.DASHBOARD_LAYOUT_EXPORT_FAILURE : SERVER_EVENTS.DASHBOARD_LAYOUT_EXPORT_SUCCESS,+ event: res.err+ ? SERVER_EVENTS.DASHBOARD_LAYOUT_EXPORT_FAILURE+ : SERVER_EVENTS.DASHBOARD_LAYOUT_EXPORT_SUCCESS,startTimestamp,});if (res.err) {@@ -20,12 +23,19 @@ export async function GET(headers: { "Content-Type": "application/json" },});}- const json = JSON.stringify(res.data, null, 2);+ const payload = toExportDashboardPayload(+ res.data as unknown as Record<string, unknown>+ );+ const json = JSON.stringify(payload, null, 2);+ const title =+ typeof payload.title === "string" && payload.title+ ? payload.title+ : id;return new Response(json, {status: 200,headers: {"Content-Type": "application/json",- "Content-Disposition": `attachment; filename=openlit-dashboard-${res.data!.title || res.data!.id}-layout.json`,+ "Content-Disposition": `attachment; filename=openlit-dashboard-${title}-layout.json`,},});}diff --git a/src/client/src/app/api/manage-dashboard/board/layout/import/route.ts b/src/client/src/app/api/manage-dashboard/board/layout/import/route.tsindex 98a18c67..8272c791 100644--- a/src/client/src/app/api/manage-dashboard/board/layout/import/route.ts+++ b/src/client/src/app/api/manage-dashboard/board/layout/import/route.ts@@ -1,26 +1,41 @@import { SERVER_EVENTS } from "@/constants/events";+import getMessage from "@/constants/messages";import { importBoardLayout } from "@/lib/platform/manage-dashboard/board";import PostHogServer from "@/lib/posthog";import { NextRequest } from "next/server";export async function POST(request: NextRequest) {- const startTimestamp = Date.now();- const data = await request.json();- const { err, data: boardData } = await importBoardLayout(data);- PostHogServer.fireEvent({- event: err ? SERVER_EVENTS.DASHBOARD_IMPORT_FAILURE : SERVER_EVENTS.DASHBOARD_IMPORT_SUCCESS,- startTimestamp,- });+ const startTimestamp = Date.now();+ let data: unknown;+ try {+ data = await request.json();+ } catch {+ return new Response(+ JSON.stringify({ error: getMessage().BOARD_IMPORT_INVALID_JSON }),+ {+ status: 400,+ headers: { "Content-Type": "application/json" },+ }+ );+ }- if (err) {- return new Response(JSON.stringify({ error: err }), {- status: 400,- headers: { "Content-Type": "application/json" }- });- }+ const { err, data: boardData } = await importBoardLayout(data);+ PostHogServer.fireEvent({+ event: err+ ? SERVER_EVENTS.DASHBOARD_IMPORT_FAILURE+ : SERVER_EVENTS.DASHBOARD_IMPORT_SUCCESS,+ startTimestamp,+ });- return new Response(JSON.stringify({ data: boardData }), {- status: 200,- headers: { "Content-Type": "application/json" }- });+ if (err) {+ return new Response(JSON.stringify({ error: err }), {+ status: 400,+ headers: { "Content-Type": "application/json" },+ });+ }++ return new Response(JSON.stringify({ data: boardData }), {+ status: 200,+ headers: { "Content-Type": "application/json" },+ });}diff --git a/src/client/src/app/api/manage-dashboard/query/run/route.ts b/src/client/src/app/api/manage-dashboard/query/run/route.tsindex 64588897..d8e8e927 100644--- a/src/client/src/app/api/manage-dashboard/query/run/route.ts+++ b/src/client/src/app/api/manage-dashboard/query/run/route.ts@@ -1,24 +1,71 @@+import { SERVER_EVENTS } from "@/constants/events";+import getMessage from "@/constants/messages";import { requireRouteAccess } from "@/lib/access/route-access";import { runWidgetQuery } from "@/lib/platform/manage-dashboard/widget";+import PostHogServer from "@/lib/posthog";+import { getCurrentUser } from "@/lib/session";import asaw from "@/utils/asaw";import { NextRequest } from "next/server";export async function POST(request: NextRequest) {- const [permissionErr] = await asaw(- requireRouteAccess("dashboard.read")- );+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });++ const [permissionErr] = await asaw(requireRouteAccess("dashboard.read"));if (permissionErr) {return Response.json({ err: String(permissionErr) }, { status: 403 });}- const {- widgetId,- userQuery,- filter,- } = await request.json();+ const messages = getMessage();+ let body: {+ widgetId?: string;+ userQuery?: string;+ filter?: unknown;+ sourceId?: string | null;+ signal?: string;+ structuredQuery?: unknown;+ };+ try {+ body = await request.json();+ } catch {+ return Response.json(+ { err: messages.MANAGE_MODELS_INVALID_JSON },+ { status: 400 }+ );+ }++ const { widgetId, userQuery, filter, sourceId, signal, structuredQuery } = body;+ if (!widgetId || typeof widgetId !== "string") {+ return Response.json(+ { err: messages.WIDGET_FETCH_FAILED },+ { status: 400 }+ );+ }++ const startTimestamp = Date.now();+ const environment = request.headers?.get?.("x-openlit-environment") || undefined;+ const routedFilter =+ filter && typeof filter === "object"+ ? {+ ...(filter as Record<string, unknown>),+ environment:+ (filter as Record<string, unknown>).environment || environment,+ }+ : filter;const res = await runWidgetQuery(widgetId, {userQuery,- filter,+ filter: routedFilter as Parameters<typeof runWidgetQuery>[1]["filter"],+ sourceId,+ signal: signal as Parameters<typeof runWidgetQuery>[1]["signal"],+ structuredQuery: structuredQuery as Parameters<+ typeof runWidgetQuery+ >[1]["structuredQuery"],+ });+ PostHogServer.fireEvent({+ event: res.err+ ? SERVER_EVENTS.DASHBOARD_QUERY_RUN_FAILURE+ : SERVER_EVENTS.DASHBOARD_QUERY_RUN_SUCCESS,+ startTimestamp,});return Response.json(res);}diff --git a/src/client/src/app/api/metrics/cost/summary/route.ts b/src/client/src/app/api/metrics/cost/summary/route.tsindex e57f3251..7c92d1e9 100644--- a/src/client/src/app/api/metrics/cost/summary/route.ts+++ b/src/client/src/app/api/metrics/cost/summary/route.ts@@ -1,11 +1,13 @@+import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";import { getCostSummary } from "@/lib/platform/cost/summary";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";-export async function POST(request: Request) {+async function POSTHandler(request: Request) {let formData: Record<string, unknown>;try {formData = await request.json();@@ -17,6 +19,10 @@ export async function POST(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(@@ -32,3 +38,7 @@ export async function POST(request: Request) {const res = await getCostSummary(params);return Response.json(res);}++export const POST = withRouteAccess("metrics.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/metrics/exception/grouped/route.ts b/src/client/src/app/api/metrics/exception/grouped/route.tsindex ef940889..c4c883f1 100644--- a/src/client/src/app/api/metrics/exception/grouped/route.ts+++ b/src/client/src/app/api/metrics/exception/grouped/route.ts@@ -1,10 +1,11 @@-import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getGroupedRequests } from "@/lib/platform/request";+import { getTraceGrouped } from "@/lib/platform/traces/read";+import { withRouteAccess } from "@/lib/access/route-access";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -19,7 +20,11 @@ async function POSTHandler(request: Request) {const params: MetricParams = {timeLimit,selectedConfig,- statusCode: ["STATUS_CODE_ERROR", "Error"],+ statusCode: ["STATUS_CODE_ERROR", "Error", "ERROR"],+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(@@ -30,8 +35,8 @@ async function POSTHandler(request: Request) {if (!validationParam.success)return Response.json(validationParam.err, { status: 400 });- const res = await getGroupedRequests(params, groupBy);+ const res = await getTraceGrouped(params, groupBy);return Response.json(res);}-export const POST = withRouteAccess("metrics.read", POSTHandler, { requireDbConfig: true });+export const POST = withRouteAccess("traces.read", POSTHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/metrics/exception/route.ts b/src/client/src/app/api/metrics/exception/route.tsnew file mode 100644index 00000000..1221a7c8--- /dev/null+++ b/src/client/src/app/api/metrics/exception/route.ts@@ -0,0 +1,51 @@+import { MetricParams, TimeLimit } from "@/lib/platform/common";+import { withRouteAccess } from "@/lib/access/route-access";+import {+ validateMetricsRequest,+ validateMetricsRequestType,+} from "@/helpers/server/platform";+import { listTraceRecords } from "@/lib/platform/traces/read";+import { getRequestEnvironment } from "@/constants/openlit-context";++async function POSTHandler(request: Request) {+ const formData = await request.json();+ const timeLimit = formData.timeLimit as TimeLimit;+ const limit = formData.limit || 10;+ const offset = formData.offset || 0;+ const selectedConfig = formData.selectedConfig || {};+ const sorting = formData.sorting || {};++ const params: MetricParams = {+ timeLimit,+ limit,+ offset,+ selectedConfig,+ sorting,+ statusCode: ["STATUS_CODE_ERROR", "Error", "ERROR"],+ environment: typeof formData.environment === "string" ? formData.environment : getRequestEnvironment(request),+ };++ const validationParam = validateMetricsRequest(+ params,+ validateMetricsRequestType.GET_ALL+ );++ if (!validationParam.success)+ return Response.json(validationParam.err, {+ status: 400,+ });++ try {+ const res: any = await listTraceRecords(params);+ return Response.json(res);+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ return Response.json(+ { err: message, code: "TELEMETRY_SOURCE_UNAVAILABLE" },+ { status: 503 }+ );+ }++}++export const POST = withRouteAccess("traces.read", POSTHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/metrics/gpu/fanspeed/time/route.ts b/src/client/src/app/api/metrics/gpu/fanspeed/time/route.tsindex 97a66f9d..441803cc 100644--- a/src/client/src/app/api/metrics/gpu/fanspeed/time/route.ts+++ b/src/client/src/app/api/metrics/gpu/fanspeed/time/route.ts@@ -1,4 +1,5 @@import { withRouteAccess } from "@/lib/access/route-access";+import { getRequestEnvironment } from "@/constants/openlit-context";import { GPUMetricParams, TimeLimit } from "@/lib/platform/common";import {validateMetricsRequest,@@ -12,6 +13,10 @@ async function POSTHandler(request: Request) {const params: GPUMetricParams = {timeLimit,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/gpu/memory/average/route.ts b/src/client/src/app/api/metrics/gpu/memory/average/route.tsindex 0a41d442..6079d937 100644--- a/src/client/src/app/api/metrics/gpu/memory/average/route.ts+++ b/src/client/src/app/api/metrics/gpu/memory/average/route.ts@@ -1,4 +1,5 @@import { withRouteAccess } from "@/lib/access/route-access";+import { getRequestEnvironment } from "@/constants/openlit-context";import { GPUMetricParams, TimeLimit } from "@/lib/platform/common";import {validateMetricsRequest,@@ -12,6 +13,10 @@ async function POSTHandler(request: Request) {const params: GPUMetricParams = {timeLimit,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/gpu/memory/time/route.ts b/src/client/src/app/api/metrics/gpu/memory/time/route.tsindex 5658986e..5a7dcdf2 100644--- a/src/client/src/app/api/metrics/gpu/memory/time/route.ts+++ b/src/client/src/app/api/metrics/gpu/memory/time/route.ts@@ -1,4 +1,5 @@import { withRouteAccess } from "@/lib/access/route-access";+import { getRequestEnvironment } from "@/constants/openlit-context";import { GPUMetricParams, TimeLimit } from "@/lib/platform/common";import {validateMetricsRequest,@@ -12,6 +13,10 @@ async function POSTHandler(request: Request) {const params: GPUMetricParams = {timeLimit,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/gpu/power/average/route.ts b/src/client/src/app/api/metrics/gpu/power/average/route.tsindex 4877de6e..19e28965 100644--- a/src/client/src/app/api/metrics/gpu/power/average/route.ts+++ b/src/client/src/app/api/metrics/gpu/power/average/route.ts@@ -1,4 +1,5 @@import { withRouteAccess } from "@/lib/access/route-access";+import { getRequestEnvironment } from "@/constants/openlit-context";import { GPUMetricParams, TimeLimit } from "@/lib/platform/common";import {validateMetricsRequest,@@ -12,6 +13,10 @@ async function POSTHandler(request: Request) {const params: GPUMetricParams = {timeLimit,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/gpu/power/time/route.ts b/src/client/src/app/api/metrics/gpu/power/time/route.tsindex 2b78f9c2..50e11bc7 100644--- a/src/client/src/app/api/metrics/gpu/power/time/route.ts+++ b/src/client/src/app/api/metrics/gpu/power/time/route.ts@@ -1,4 +1,5 @@import { withRouteAccess } from "@/lib/access/route-access";+import { getRequestEnvironment } from "@/constants/openlit-context";import { GPUMetricParams, TimeLimit } from "@/lib/platform/common";import {validateMetricsRequest,@@ -12,6 +13,10 @@ async function POSTHandler(request: Request) {const params: GPUMetricParams = {timeLimit,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/gpu/temperature/average/route.ts b/src/client/src/app/api/metrics/gpu/temperature/average/route.tsindex e9d0a4dc..7dd3b235 100644--- a/src/client/src/app/api/metrics/gpu/temperature/average/route.ts+++ b/src/client/src/app/api/metrics/gpu/temperature/average/route.ts@@ -1,4 +1,5 @@import { withRouteAccess } from "@/lib/access/route-access";+import { getRequestEnvironment } from "@/constants/openlit-context";import { GPUMetricParams, TimeLimit } from "@/lib/platform/common";import {validateMetricsRequest,@@ -12,6 +13,10 @@ async function POSTHandler(request: Request) {const params: GPUMetricParams = {timeLimit,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/gpu/temperature/time/route.ts b/src/client/src/app/api/metrics/gpu/temperature/time/route.tsindex 003e9b7e..b786619c 100644--- a/src/client/src/app/api/metrics/gpu/temperature/time/route.ts+++ b/src/client/src/app/api/metrics/gpu/temperature/time/route.ts@@ -1,4 +1,5 @@import { withRouteAccess } from "@/lib/access/route-access";+import { getRequestEnvironment } from "@/constants/openlit-context";import { GPUMetricParams, TimeLimit } from "@/lib/platform/common";import {validateMetricsRequest,@@ -12,6 +13,10 @@ async function POSTHandler(request: Request) {const params: GPUMetricParams = {timeLimit,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/gpu/utilization/average/route.ts b/src/client/src/app/api/metrics/gpu/utilization/average/route.tsindex 86142f4e..1729f812 100644--- a/src/client/src/app/api/metrics/gpu/utilization/average/route.ts+++ b/src/client/src/app/api/metrics/gpu/utilization/average/route.ts@@ -1,4 +1,5 @@import { withRouteAccess } from "@/lib/access/route-access";+import { getRequestEnvironment } from "@/constants/openlit-context";import { GPUMetricParams, TimeLimit } from "@/lib/platform/common";import {validateMetricsRequest,@@ -12,6 +13,10 @@ async function POSTHandler(request: Request) {const params: GPUMetricParams = {timeLimit,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/gpu/utilization/time/route.ts b/src/client/src/app/api/metrics/gpu/utilization/time/route.tsindex c2950fe7..4a77e695 100644--- a/src/client/src/app/api/metrics/gpu/utilization/time/route.ts+++ b/src/client/src/app/api/metrics/gpu/utilization/time/route.ts@@ -1,4 +1,5 @@import { withRouteAccess } from "@/lib/access/route-access";+import { getRequestEnvironment } from "@/constants/openlit-context";import { GPUMetricParams, TimeLimit } from "@/lib/platform/common";import {validateMetricsRequest,@@ -12,6 +13,10 @@ async function POSTHandler(request: Request) {const params: GPUMetricParams = {timeLimit,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/llm/category/route.ts b/src/client/src/app/api/metrics/llm/category/route.tsindex 2f064529..a400dd38 100644--- a/src/client/src/app/api/metrics/llm/category/route.ts+++ b/src/client/src/app/api/metrics/llm/category/route.ts@@ -5,6 +5,7 @@ import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -13,6 +14,10 @@ async function POSTHandler(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/llm/cost/application/route.ts b/src/client/src/app/api/metrics/llm/cost/application/route.tsindex 6ea75009..944edf5f 100644--- a/src/client/src/app/api/metrics/llm/cost/application/route.ts+++ b/src/client/src/app/api/metrics/llm/cost/application/route.ts@@ -5,6 +5,7 @@ import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -13,6 +14,10 @@ async function POSTHandler(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/llm/cost/environment/route.ts b/src/client/src/app/api/metrics/llm/cost/environment/route.tsindex ac6888ca..a55b2280 100644--- a/src/client/src/app/api/metrics/llm/cost/environment/route.ts+++ b/src/client/src/app/api/metrics/llm/cost/environment/route.ts@@ -5,6 +5,7 @@ import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -13,6 +14,10 @@ async function POSTHandler(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/llm/cost/model/route.ts b/src/client/src/app/api/metrics/llm/cost/model/route.tsindex c5c0b9b3..63d57124 100644--- a/src/client/src/app/api/metrics/llm/cost/model/route.ts+++ b/src/client/src/app/api/metrics/llm/cost/model/route.ts@@ -1,11 +1,13 @@+import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";import { getCostByModel } from "@/lib/platform/llm/cost";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";-export async function POST(request: Request) {+async function POSTHandler(request: Request) {let formData: Record<string, unknown>;try {formData = await request.json();@@ -17,6 +19,10 @@ export async function POST(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(@@ -32,3 +38,7 @@ export async function POST(request: Request) {const res = await getCostByModel(params);return Response.json(res);}++export const POST = withRouteAccess("metrics.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/metrics/llm/cost/provider/route.ts b/src/client/src/app/api/metrics/llm/cost/provider/route.tsindex bd33a2ea..2ae56d9f 100644--- a/src/client/src/app/api/metrics/llm/cost/provider/route.ts+++ b/src/client/src/app/api/metrics/llm/cost/provider/route.ts@@ -1,11 +1,13 @@+import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";import { getCostByProvider } from "@/lib/platform/llm/cost";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";-export async function POST(request: Request) {+async function POSTHandler(request: Request) {let formData: Record<string, unknown>;try {formData = await request.json();@@ -17,6 +19,10 @@ export async function POST(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(@@ -32,3 +38,7 @@ export async function POST(request: Request) {const res = await getCostByProvider(params);return Response.json(res);}++export const POST = withRouteAccess("metrics.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/metrics/llm/cost/request/average/route.ts b/src/client/src/app/api/metrics/llm/cost/request/average/route.tsindex af76d0ba..7a74a96b 100644--- a/src/client/src/app/api/metrics/llm/cost/request/average/route.ts+++ b/src/client/src/app/api/metrics/llm/cost/request/average/route.ts@@ -5,6 +5,7 @@ import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -13,6 +14,10 @@ async function POSTHandler(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/llm/cost/time/route.ts b/src/client/src/app/api/metrics/llm/cost/time/route.tsindex 1db46887..bc2cd0e0 100644--- a/src/client/src/app/api/metrics/llm/cost/time/route.ts+++ b/src/client/src/app/api/metrics/llm/cost/time/route.ts@@ -1,11 +1,13 @@+import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";import { getCostPerTime } from "@/lib/platform/llm/cost";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";-export async function POST(request: Request) {+async function POSTHandler(request: Request) {let formData: Record<string, unknown>;try {formData = await request.json();@@ -17,6 +19,10 @@ export async function POST(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(@@ -32,3 +38,7 @@ export async function POST(request: Request) {const res = await getCostPerTime(params);return Response.json(res);}++export const POST = withRouteAccess("metrics.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/metrics/llm/cost/total/route.ts b/src/client/src/app/api/metrics/llm/cost/total/route.tsindex ca8468ec..2e91213e 100644--- a/src/client/src/app/api/metrics/llm/cost/total/route.ts+++ b/src/client/src/app/api/metrics/llm/cost/total/route.ts@@ -5,6 +5,7 @@ import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -13,6 +14,10 @@ async function POSTHandler(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/llm/endpoint/route.ts b/src/client/src/app/api/metrics/llm/endpoint/route.tsindex 7f477e28..fbd6fb07 100644--- a/src/client/src/app/api/metrics/llm/endpoint/route.ts+++ b/src/client/src/app/api/metrics/llm/endpoint/route.ts@@ -6,6 +6,7 @@ import {} from "@/helpers/server/platform";import { getResultGenerationByEndpoint } from "@/lib/platform/llm/endpoint";import { OPERATION_TYPE } from "@/types/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -16,6 +17,10 @@ async function POSTHandler(request: Request) {timeLimit,operationType,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/llm/model/time/route.ts b/src/client/src/app/api/metrics/llm/model/time/route.tsindex fdb01566..685eb3cd 100644--- a/src/client/src/app/api/metrics/llm/model/time/route.ts+++ b/src/client/src/app/api/metrics/llm/model/time/route.ts@@ -5,6 +5,7 @@ import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -13,6 +14,10 @@ async function POSTHandler(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/llm/model/top/route.ts b/src/client/src/app/api/metrics/llm/model/top/route.tsindex 73c8cf6c..764b85a6 100644--- a/src/client/src/app/api/metrics/llm/model/top/route.ts+++ b/src/client/src/app/api/metrics/llm/model/top/route.ts@@ -5,6 +5,7 @@ import {validateMetricsRequestType,} from "@/helpers/server/platform";import { TimeLimit } from "@/lib/platform/common";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -14,6 +15,10 @@ async function POSTHandler(request: Request) {timeLimit,top: formData.top || 3,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/llm/token/request/average/route.ts b/src/client/src/app/api/metrics/llm/token/request/average/route.tsindex 1726a0f8..7a5c7f56 100644--- a/src/client/src/app/api/metrics/llm/token/request/average/route.ts+++ b/src/client/src/app/api/metrics/llm/token/request/average/route.ts@@ -8,6 +8,7 @@ import {validateMetricsRequestType,} from "@/helpers/server/platform";import { TimeLimit } from "@/lib/platform/common";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -17,6 +18,10 @@ async function POSTHandler(request: Request) {timeLimit,type: formData.type,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/llm/token/time/route.ts b/src/client/src/app/api/metrics/llm/token/time/route.tsindex 18d903e5..184068e2 100644--- a/src/client/src/app/api/metrics/llm/token/time/route.ts+++ b/src/client/src/app/api/metrics/llm/token/time/route.ts@@ -5,6 +5,7 @@ import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -13,6 +14,10 @@ async function POSTHandler(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/openground/cost/provider/route.ts b/src/client/src/app/api/metrics/openground/cost/provider/route.tsindex 2e682412..ae1420a0 100644--- a/src/client/src/app/api/metrics/openground/cost/provider/route.ts+++ b/src/client/src/app/api/metrics/openground/cost/provider/route.ts@@ -1,3 +1,4 @@+import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";import { getOpengroundCostByProvider } from "@/lib/platform/openground/cost-analytics";import {@@ -5,7 +6,7 @@ import {validateMetricsRequestType,} from "@/helpers/server/platform";-export async function POST(request: Request) {+async function POSTHandler(request: Request) {let formData: Record<string, unknown>;try {formData = await request.json();@@ -32,3 +33,7 @@ export async function POST(request: Request) {const res = await getOpengroundCostByProvider(params);return Response.json(res);}++export const POST = withRouteAccess("metrics.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/metrics/openground/cost/route.ts b/src/client/src/app/api/metrics/openground/cost/route.tsindex f858c115..717c8834 100644--- a/src/client/src/app/api/metrics/openground/cost/route.ts+++ b/src/client/src/app/api/metrics/openground/cost/route.ts@@ -1,3 +1,4 @@+import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";import { getOpengroundCostAnalytics } from "@/lib/platform/openground/cost-analytics";import {@@ -5,7 +6,7 @@ import {validateMetricsRequestType,} from "@/helpers/server/platform";-export async function POST(request: Request) {+async function POSTHandler(request: Request) {let formData: Record<string, unknown>;try {formData = await request.json();@@ -32,3 +33,7 @@ export async function POST(request: Request) {const res = await getOpengroundCostAnalytics(params);return Response.json(res);}++export const POST = withRouteAccess("metrics.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/metrics/request/attribute-keys/route.ts b/src/client/src/app/api/metrics/request/attribute-keys/route.tsindex d5b3bdb9..8b20bd0f 100644--- a/src/client/src/app/api/metrics/request/attribute-keys/route.ts+++ b/src/client/src/app/api/metrics/request/attribute-keys/route.ts@@ -1,10 +1,11 @@-import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getAttributeKeys } from "@/lib/platform/request";+import { getTraceAttributeKeys } from "@/lib/platform/traces/read";+import { withRouteAccess } from "@/lib/access/route-access";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -14,6 +15,10 @@ async function POSTHandler(request: Request) {timeLimit,selectedConfig: formData.selectedConfig,statusCode: formData.statusCode,+ environment: typeof formData.environment === "string" ? formData.environment : getRequestEnvironment(request),+ ...(typeof formData.sourceId === "string"+ ? { sourceId: formData.sourceId }+ : {}),};const validationParam = validateMetricsRequest(@@ -26,8 +31,8 @@ async function POSTHandler(request: Request) {status: 400,});- const res = await getAttributeKeys(params);+ const res = await getTraceAttributeKeys(params);return Response.json(res);}-export const POST = withRouteAccess("metrics.read", POSTHandler, { requireDbConfig: true });+export const POST = withRouteAccess("traces.read", POSTHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/metrics/request/config/route.ts b/src/client/src/app/api/metrics/request/config/route.tsindex e3fbc893..99805610 100644--- a/src/client/src/app/api/metrics/request/config/route.ts+++ b/src/client/src/app/api/metrics/request/config/route.ts@@ -1,10 +1,11 @@-import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getRequestsConfig } from "@/lib/platform/request";+import { getTraceFilterConfig } from "@/lib/platform/traces/read";+import { withRouteAccess } from "@/lib/access/route-access";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -18,6 +19,10 @@ async function POSTHandler(request: Request) {offset,selectedConfig: formData.selectedConfig,statusCode: formData.statusCode,+ environment: typeof formData.environment === "string" ? formData.environment : getRequestEnvironment(request),+ ...(typeof formData.sourceId === "string"+ ? { sourceId: formData.sourceId }+ : {}),};const validationParam = validateMetricsRequest(@@ -30,8 +35,8 @@ async function POSTHandler(request: Request) {status: 400,});- const res: any = await getRequestsConfig(params);+ const res: any = await getTraceFilterConfig(params);return Response.json(res);}-export const POST = withRouteAccess("metrics.read", POSTHandler, { requireDbConfig: true });+export const POST = withRouteAccess("traces.read", POSTHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/metrics/request/duration/average/route.ts b/src/client/src/app/api/metrics/request/duration/average/route.tsindex 2b43ec48..495e75dd 100644--- a/src/client/src/app/api/metrics/request/duration/average/route.ts+++ b/src/client/src/app/api/metrics/request/duration/average/route.ts@@ -1,11 +1,12 @@-import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getAverageRequestDuration } from "@/lib/platform/request";+import { getTraceAverageDuration } from "@/lib/platform/traces/read";+import { withRouteAccess } from "@/lib/access/route-access";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";import { OPERATION_TYPE } from "@/types/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -16,6 +17,10 @@ async function POSTHandler(request: Request) {timeLimit,operationType,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(@@ -28,8 +33,10 @@ async function POSTHandler(request: Request) {status: 400,});- const res: any = await getAverageRequestDuration(params);+ const res: any = await getTraceAverageDuration(params);return Response.json(res);}-export const POST = withRouteAccess("metrics.read", POSTHandler, { requireDbConfig: true });+export const POST = withRouteAccess("traces.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/metrics/request/exist/route.ts b/src/client/src/app/api/metrics/request/exist/route.tsindex b24f0b61..fcf11df7 100644--- a/src/client/src/app/api/metrics/request/exist/route.ts+++ b/src/client/src/app/api/metrics/request/exist/route.ts@@ -1,8 +1,9 @@+import { getTraceExist } from "@/lib/platform/traces/read";import { withRouteAccess } from "@/lib/access/route-access";-import { getRequestExist } from "@/lib/platform/request";+import { getRequestEnvironment } from "@/constants/openlit-context";-async function POSTHandler() {- const res = await getRequestExist();+async function POSTHandler(request: Request) {+ const res = await getTraceExist(getRequestEnvironment(request));const { data } = res;if ((data as any[])?.[0]?.total_requests > 0) {return Response.json(true);@@ -11,4 +12,4 @@ async function POSTHandler() {return Response.json(false);}-export const POST = withRouteAccess("metrics.read", POSTHandler, { requireDbConfig: true });+export const POST = withRouteAccess("traces.read", POSTHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/metrics/request/grouped/route.ts b/src/client/src/app/api/metrics/request/grouped/route.tsindex 61415f39..0293e3d1 100644--- a/src/client/src/app/api/metrics/request/grouped/route.ts+++ b/src/client/src/app/api/metrics/request/grouped/route.ts@@ -1,11 +1,12 @@-import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getGroupedRequests } from "@/lib/platform/request";+import { getTraceGrouped } from "@/lib/platform/traces/read";+import { withRouteAccess } from "@/lib/access/route-access";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";import { GroupByKey } from "@/types/store/filter";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -20,6 +21,10 @@ async function POSTHandler(request: Request) {const params: MetricParams = {timeLimit,selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(@@ -30,8 +35,10 @@ async function POSTHandler(request: Request) {if (!validationParam.success)return Response.json(validationParam.err, { status: 400 });- const res = await getGroupedRequests(params, groupBy);+ const res = await getTraceGrouped(params, groupBy);return Response.json(res);}-export const POST = withRouteAccess("metrics.read", POSTHandler, { requireDbConfig: true });+export const POST = withRouteAccess("traces.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/metrics/request/route.ts b/src/client/src/app/api/metrics/request/route.tsnew file mode 100644index 00000000..09b6e498--- /dev/null+++ b/src/client/src/app/api/metrics/request/route.ts@@ -0,0 +1,54 @@+import { MetricParams, TimeLimit } from "@/lib/platform/common";+import { listTraceRecords } from "@/lib/platform/traces/read";+import {+ validateMetricsRequest,+ validateMetricsRequestType,+} from "@/helpers/server/platform";+import { withRouteAccess } from "@/lib/access/route-access";+import { getRequestEnvironment } from "@/constants/openlit-context";++async function POSTHandler(request: Request) {+ const formData = await request.json();+ const timeLimit = formData.timeLimit as TimeLimit;+ const limit = formData.limit || 10;+ const offset = formData.offset || 0;+ const selectedConfig = formData.selectedConfig || {};+ const sorting = formData.sorting || {};++ const params: MetricParams = {+ timeLimit,+ limit,+ offset,+ selectedConfig,+ sorting,+ ...(typeof formData.sourceId === "string" ? { sourceId: formData.sourceId } : {}),+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),+ };++ const validationParam = validateMetricsRequest(+ params,+ validateMetricsRequestType.GET_ALL+ );++ if (!validationParam.success)+ return Response.json(validationParam.err, {+ status: 400,+ });++ try {+ const res: any = await listTraceRecords(params);+ return Response.json(res);+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ return Response.json(+ { err: message, code: "TELEMETRY_SOURCE_UNAVAILABLE" },+ { status: 503 }+ );+ }++}++export const POST = withRouteAccess("traces.read", POSTHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/metrics/request/span/[id]/heirarchy/route.ts b/src/client/src/app/api/metrics/request/span/[id]/heirarchy/route.tsnew file mode 100644index 00000000..869f4c03--- /dev/null+++ b/src/client/src/app/api/metrics/request/span/[id]/heirarchy/route.ts@@ -0,0 +1,24 @@+import { getTraceHierarchy } from "@/lib/platform/traces/read";+import { withRouteAccess } from "@/lib/access/route-access";+import { getRequestEnvironment } from "@/constants/openlit-context";++async function GETHandler(request: Request, context: any) {+ const { id } = context.params || {};++ if (!id) {+ return Response.json("No span id provided", {+ status: 400,+ });+ }++ const url = new URL(request.url);+ const traceId = url.searchParams.get("traceId") || undefined;+ const environment =+ url.searchParams.get("environment") || getRequestEnvironment(request);+ const res: any = await getTraceHierarchy(id, { traceId, environment });+ return Response.json(res);+}++export const GET = withRouteAccess("traces.read", GETHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/metrics/request/span/[id]/route.ts b/src/client/src/app/api/metrics/request/span/[id]/route.tsnew file mode 100644index 00000000..ca480810--- /dev/null+++ b/src/client/src/app/api/metrics/request/span/[id]/route.ts@@ -0,0 +1,33 @@+import { getTraceSpanRecord } from "@/lib/platform/traces/read";+import { getEvaluationSummaryForSpanId } from "@/lib/platform/evaluation";+import { withRouteAccess } from "@/lib/access/route-access";+import { getRequestEnvironment } from "@/constants/openlit-context";++async function GETHandler(request: Request, context: any) {+ const { id } = context.params || {};++ if (!id)+ return Response.json("No span id provided", {+ status: 400,+ });++ const url = new URL(request.url);+ const traceId = url.searchParams.get("traceId") || undefined;+ const environment =+ url.searchParams.get("environment") || getRequestEnvironment(request);++ const [spanRes, evalSummary] = await Promise.all([+ getTraceSpanRecord(id, { traceId, environment }),+ getEvaluationSummaryForSpanId(id),+ ]);++ const res: any = { ...spanRes };+ if (evalSummary && evalSummary.runCount > 0) {+ res.evaluationSummary = evalSummary;+ }+ return Response.json(res);+}++export const GET = withRouteAccess("traces.read", GETHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/metrics/request/time/route.ts b/src/client/src/app/api/metrics/request/time/route.tsindex 1842f40c..2534533b 100644--- a/src/client/src/app/api/metrics/request/time/route.ts+++ b/src/client/src/app/api/metrics/request/time/route.ts@@ -1,10 +1,11 @@-import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getRequestPerTime } from "@/lib/platform/request";+import { getTraceRequestPerTime } from "@/lib/platform/traces/read";+import { withRouteAccess } from "@/lib/access/route-access";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -13,6 +14,10 @@ async function POSTHandler(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(@@ -25,8 +30,10 @@ async function POSTHandler(request: Request) {status: 400,});- const res: any = await getRequestPerTime(params);+ const res: any = await getTraceRequestPerTime(params);return Response.json(res);}-export const POST = withRouteAccess("metrics.read", POSTHandler, { requireDbConfig: true });+export const POST = withRouteAccess("traces.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/metrics/request/total/route.ts b/src/client/src/app/api/metrics/request/total/route.tsindex fa23fb71..1bddb5cb 100644--- a/src/client/src/app/api/metrics/request/total/route.ts+++ b/src/client/src/app/api/metrics/request/total/route.ts@@ -1,11 +1,12 @@-import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getTotalRequests } from "@/lib/platform/request";+import { getTraceTotalRequests } from "@/lib/platform/traces/read";+import { withRouteAccess } from "@/lib/access/route-access";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";import { OPERATION_TYPE } from "@/types/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -16,6 +17,10 @@ async function POSTHandler(request: Request) {timeLimit,operationType,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(@@ -28,8 +33,10 @@ async function POSTHandler(request: Request) {status: 400,});- const res: any = await getTotalRequests(params);+ const res: any = await getTraceTotalRequests(params);return Response.json(res);}-export const POST = withRouteAccess("metrics.read", POSTHandler, { requireDbConfig: true });+export const POST = withRouteAccess("traces.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/metrics/request/trace/[id]/route.ts b/src/client/src/app/api/metrics/request/trace/[id]/route.tsnew file mode 100644index 00000000..9e9e5894--- /dev/null+++ b/src/client/src/app/api/metrics/request/trace/[id]/route.ts@@ -0,0 +1,22 @@+import { getTraceRecordByTraceId } from "@/lib/platform/traces/read";+import { withRouteAccess } from "@/lib/access/route-access";+import { getRequestEnvironment } from "@/constants/openlit-context";++async function GETHandler(request: Request, context: any) {+ const { id } = context.params || {};++ if (!id)+ return Response.json("No parent span id provided", {+ status: 400,+ });++ const environment =+ new URL(request.url).searchParams.get("environment") ||+ getRequestEnvironment(request);+ const res: any = await getTraceRecordByTraceId(id, environment);+ return Response.json(res);+}++export const GET = withRouteAccess("traces.read", GETHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/metrics/vector/application/route.ts b/src/client/src/app/api/metrics/vector/application/route.tsindex 3ccfaa9c..b6019a5b 100644--- a/src/client/src/app/api/metrics/vector/application/route.ts+++ b/src/client/src/app/api/metrics/vector/application/route.ts@@ -5,6 +5,7 @@ import {validateMetricsRequestType,} from "@/helpers/server/platform";import { getResultGenerationByApplication } from "@/lib/platform/vector/application";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -13,6 +14,10 @@ async function POSTHandler(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/vector/environment/route.ts b/src/client/src/app/api/metrics/vector/environment/route.tsindex 3919803f..e0fcac28 100644--- a/src/client/src/app/api/metrics/vector/environment/route.ts+++ b/src/client/src/app/api/metrics/vector/environment/route.ts@@ -5,6 +5,7 @@ import {validateMetricsRequestType,} from "@/helpers/server/platform";import { getResultGenerationByEnvironment } from "@/lib/platform/vector/environment";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -13,6 +14,10 @@ async function POSTHandler(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/vector/operation/route.ts b/src/client/src/app/api/metrics/vector/operation/route.tsindex 67d948d7..eef2b4cd 100644--- a/src/client/src/app/api/metrics/vector/operation/route.ts+++ b/src/client/src/app/api/metrics/vector/operation/route.ts@@ -5,6 +5,7 @@ import {validateMetricsRequestType,} from "@/helpers/server/platform";import { getResultGenerationByOperation } from "@/lib/platform/vector/operation";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -13,6 +14,10 @@ async function POSTHandler(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/metrics/vector/system/route.ts b/src/client/src/app/api/metrics/vector/system/route.tsindex 7e7f58c0..6cb99596 100644--- a/src/client/src/app/api/metrics/vector/system/route.ts+++ b/src/client/src/app/api/metrics/vector/system/route.ts@@ -5,6 +5,7 @@ import {validateMetricsRequestType,} from "@/helpers/server/platform";import { getResultGenerationBySystem } from "@/lib/platform/vector/system";+import { getRequestEnvironment } from "@/constants/openlit-context";async function POSTHandler(request: Request) {const formData = await request.json();@@ -13,6 +14,10 @@ async function POSTHandler(request: Request) {const params: MetricParams = {timeLimit,selectedConfig: formData.selectedConfig,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validationParam = validateMetricsRequest(diff --git a/src/client/src/app/api/observability/metrics/[name]/route.ts b/src/client/src/app/api/observability/metrics/[name]/route.tsindex 2734eb65..58eeefe4 100644--- a/src/client/src/app/api/observability/metrics/[name]/route.ts+++ b/src/client/src/app/api/observability/metrics/[name]/route.ts@@ -1,6 +1,11 @@+/**+ * @deprecated Prefer POST /api/telemetry/metrics/[name] — kept for API compatibility.+ */import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getMetricDetail } from "@/lib/platform/observability";+import { getMetricDetailRecord } from "@/lib/platform/metrics/read";+import { getRequestEnvironment } from "@/constants/openlit-context";+import { jsonWithObservabilityDeprecation } from "@/lib/platform/observability-deprecation";async function POSTHandler(request: Request,@@ -13,11 +18,32 @@ async function POSTHandler(const metricParams: MetricParams = {timeLimit: formData.timeLimit as TimeLimit,selectedConfig: formData.selectedConfig || {},+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};- return Response.json(- await getMetricDetail(metricName, metricType, serviceName, metricParams)- );+ try {+ return jsonWithObservabilityDeprecation(+ await getMetricDetailRecord(+ metricName,+ metricType,+ serviceName,+ metricParams+ ),+ `/api/telemetry/metrics/${encodeURIComponent(metricName)}`+ );+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ return jsonWithObservabilityDeprecation(+ { err: message, code: "TELEMETRY_SOURCE_UNAVAILABLE" },+ `/api/telemetry/metrics/${encodeURIComponent(metricName)}`,+ { status: 503 }+ );+ }}-export const POST = withRouteAccess("observability.read", POSTHandler, { requireDbConfig: true });+export const POST = withRouteAccess("observability.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/observability/metrics/attribute-keys/route.ts b/src/client/src/app/api/observability/metrics/attribute-keys/route.tsindex 0de3c3d3..f4581f19 100644--- a/src/client/src/app/api/observability/metrics/attribute-keys/route.ts+++ b/src/client/src/app/api/observability/metrics/attribute-keys/route.ts@@ -1,23 +1,52 @@+/**+ * @deprecated Prefer POST /api/telemetry/metrics/attribute-keys — kept for API compatibility.+ */import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getMetricAttributeKeys } from "@/lib/platform/observability";+import { getMetricAttributeKeysRecord } from "@/lib/platform/metrics/read";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";+import { jsonWithObservabilityDeprecation } from "@/lib/platform/observability-deprecation";async function POSTHandler(request: Request) {const formData = await request.json();const params: MetricParams = {timeLimit: formData.timeLimit as TimeLimit,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validation = validateMetricsRequest(params,validateMetricsRequestType.GET_ALL);- if (!validation.success) return Response.json(validation.err, { status: 400 });+ if (!validation.success) {+ return jsonWithObservabilityDeprecation(+ validation.err,+ "/api/telemetry/metrics/attribute-keys",+ { status: 400 }+ );+ }- return Response.json(await getMetricAttributeKeys(params));+ try {+ return jsonWithObservabilityDeprecation(+ await getMetricAttributeKeysRecord(params),+ "/api/telemetry/metrics/attribute-keys"+ );+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ return jsonWithObservabilityDeprecation(+ { err: message, code: "TELEMETRY_SOURCE_UNAVAILABLE" },+ "/api/telemetry/metrics/attribute-keys",+ { status: 503 }+ );+ }}-export const POST = withRouteAccess("observability.read", POSTHandler, { requireDbConfig: true });+export const POST = withRouteAccess("observability.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/observability/metrics/config/route.ts b/src/client/src/app/api/observability/metrics/config/route.tsindex fc1793cf..21557485 100644--- a/src/client/src/app/api/observability/metrics/config/route.ts+++ b/src/client/src/app/api/observability/metrics/config/route.ts@@ -1,23 +1,52 @@+/**+ * @deprecated Prefer POST /api/telemetry/metrics/config — kept for API compatibility.+ */import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getMetricsConfig } from "@/lib/platform/observability";+import { getMetricsFilterConfig } from "@/lib/platform/metrics/read";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";+import { jsonWithObservabilityDeprecation } from "@/lib/platform/observability-deprecation";async function POSTHandler(request: Request) {const formData = await request.json();const params: MetricParams = {timeLimit: formData.timeLimit as TimeLimit,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validation = validateMetricsRequest(params,validateMetricsRequestType.GET_ALL);- if (!validation.success) return Response.json(validation.err, { status: 400 });+ if (!validation.success) {+ return jsonWithObservabilityDeprecation(+ validation.err,+ "/api/telemetry/metrics/config",+ { status: 400 }+ );+ }- return Response.json(await getMetricsConfig(params));+ try {+ return jsonWithObservabilityDeprecation(+ await getMetricsFilterConfig(params),+ "/api/telemetry/metrics/config"+ );+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ return jsonWithObservabilityDeprecation(+ { err: message, code: "TELEMETRY_SOURCE_UNAVAILABLE" },+ "/api/telemetry/metrics/config",+ { status: 503 }+ );+ }}-export const POST = withRouteAccess("observability.read", POSTHandler, { requireDbConfig: true });+export const POST = withRouteAccess("observability.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/observability/metrics/route.ts b/src/client/src/app/api/observability/metrics/route.tsindex d1316a7b..102edf7c 100644--- a/src/client/src/app/api/observability/metrics/route.ts+++ b/src/client/src/app/api/observability/metrics/route.ts@@ -1,10 +1,15 @@+/**+ * @deprecated Prefer POST /api/telemetry/metrics — kept for API compatibility.+ */import { withRouteAccess } from "@/lib/access/route-access";import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getMetrics } from "@/lib/platform/observability";+import { listMetricRecords } from "@/lib/platform/metrics/read";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";+import { jsonWithObservabilityDeprecation } from "@/lib/platform/observability-deprecation";async function POSTHandler(request: Request) {const formData = await request.json();@@ -14,14 +19,36 @@ async function POSTHandler(request: Request) {offset: formData.offset || 0,selectedConfig: formData.selectedConfig || {},sorting: formData.sorting || {},+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validation = validateMetricsRequest(params,validateMetricsRequestType.GET_ALL);- if (!validation.success) return Response.json(validation.err, { status: 400 });+ if (!validation.success) {+ return jsonWithObservabilityDeprecation(validation.err, "/api/telemetry/metrics", {+ status: 400,+ });+ }- return Response.json(await getMetrics(params));+ try {+ return jsonWithObservabilityDeprecation(+ await listMetricRecords(params),+ "/api/telemetry/metrics"+ );+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ return jsonWithObservabilityDeprecation(+ { err: message, code: "TELEMETRY_SOURCE_UNAVAILABLE" },+ "/api/telemetry/metrics",+ { status: 503 }+ );+ }}-export const POST = withRouteAccess("observability.read", POSTHandler, { requireDbConfig: true });+export const POST = withRouteAccess("observability.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/observability/summary/[signal]/route.ts b/src/client/src/app/api/observability/summary/[signal]/route.tsindex d48eb17c..b7e3751a 100644--- a/src/client/src/app/api/observability/summary/[signal]/route.ts+++ b/src/client/src/app/api/observability/summary/[signal]/route.ts@@ -1,36 +1,98 @@import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getSignalSummary } from "@/lib/platform/observability";+import { getTraceSummary } from "@/lib/platform/traces/read";+import { getLogsSummary } from "@/lib/platform/logs/read";+import { getMetricsSummary } from "@/lib/platform/metrics/read";+import {+ requireRouteAccess,+ withRouteAccess,+ type RouteAccessKey,+} from "@/lib/access/route-access";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";+import asaw from "@/utils/asaw";+import { getRequestEnvironment } from "@/constants/openlit-context";+import { jsonWithObservabilityDeprecation } from "@/lib/platform/observability-deprecation";const VALID_SIGNALS = new Set(["traces", "exceptions", "logs", "metrics"]);+const SIGNAL_ACCESS: Record<string, RouteAccessKey> = {+ traces: "traces.read",+ exceptions: "traces.read",+ logs: "logs.read",+ metrics: "metrics.read",+};++/** Route each signal's summary through its per-signal read facade. */+function summaryForSignal(signal: string, params: MetricParams) {+ if (signal === "logs") return getLogsSummary(params);+ if (signal === "metrics") return getMetricsSummary(params);+ return getTraceSummary(params, signal as "traces" | "exceptions");+}-export async function POST(+/**+ * @deprecated Prefer POST /api/telemetry/summary/[signal] — kept for API compatibility.+ */+async function POSTHandler(request: Request,{ params }: { params: { signal: string } }) {if (!VALID_SIGNALS.has(params.signal)) {- return Response.json({ err: "Invalid signal" }, { status: 400 });+ return jsonWithObservabilityDeprecation(+ { err: "Invalid signal" },+ `/api/telemetry/summary/${params.signal}`,+ { status: 400 }+ );+ }+ const [permissionErr] = await asaw(+ requireRouteAccess(SIGNAL_ACCESS[params.signal])+ );+ if (permissionErr) {+ return jsonWithObservabilityDeprecation(+ { err: String(permissionErr) },+ `/api/telemetry/summary/${params.signal}`,+ { status: 403 }+ );}const formData = await request.json();const metricParams: MetricParams = {timeLimit: formData.timeLimit as TimeLimit,selectedConfig: formData.selectedConfig || {},+ ...(typeof formData.sourceId === "string" ? { sourceId: formData.sourceId } : {}),+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validation = validateMetricsRequest(metricParams,validateMetricsRequestType.GET_ALL);- if (!validation.success) return Response.json(validation.err, { status: 400 });+ if (!validation.success) {+ return jsonWithObservabilityDeprecation(+ validation.err,+ `/api/telemetry/summary/${params.signal}`,+ { status: 400 }+ );+ }- return Response.json(- await getSignalSummary(- metricParams,- params.signal as "traces" | "exceptions" | "logs" | "metrics"- )- );+ try {+ return jsonWithObservabilityDeprecation(+ await summaryForSignal(params.signal, metricParams),+ `/api/telemetry/summary/${params.signal}`+ );+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ return jsonWithObservabilityDeprecation(+ { err: message, code: "TELEMETRY_SOURCE_UNAVAILABLE" },+ `/api/telemetry/summary/${params.signal}`,+ { status: 503 }+ );+ }}++export const POST = withRouteAccess("observability.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/openground/models/debug/route.ts b/src/client/src/app/api/openground/models/debug/route.tsindex 7ea9d0be..85975da0 100644--- a/src/client/src/app/api/openground/models/debug/route.ts+++ b/src/client/src/app/api/openground/models/debug/route.ts@@ -50,8 +50,22 @@ export async function GET(request: NextRequest) {dbConfig.id);- // Try to get table schema- const schemaQuery = `DESCRIBE TABLE ${OPENLIT_PROVIDER_MODELS_TABLE_NAME}`;+ // Use a SELECT-based schema probe so the read stays inside OpenPlait's+ // single-statement native-query safety policy.+ const schemaQuery = `+ SELECT+ name,+ type,+ default_kind AS default_type,+ default_expression,+ comment,+ compression_codec AS codec_expression,+ ttl_expression+ FROM system.columns+ WHERE database = currentDatabase()+ AND table = '${OPENLIT_PROVIDER_MODELS_TABLE_NAME}'+ ORDER BY position+ `;const { data: schemaData, err: schemaErr } = await dataCollector({ query: schemaQuery },"query",diff --git a/src/client/src/app/api/pricing/[spanId]/route.ts b/src/client/src/app/api/pricing/[spanId]/route.tsindex f7f04477..3496969e 100644--- a/src/client/src/app/api/pricing/[spanId]/route.ts+++ b/src/client/src/app/api/pricing/[spanId]/route.ts@@ -2,11 +2,17 @@ import { setPricingForSpanId } from "@/lib/platform/pricing";import asaw from "@/utils/asaw";export async function POST(- _request: Request,+ request: Request,{ params }: { params: { spanId: string } }) {const { spanId } = params;- const [err, res] = await asaw(setPricingForSpanId(spanId));+ const url = new URL(request.url);+ const [err, res] = await asaw(+ setPricingForSpanId(spanId, {+ environment: url.searchParams.get("environment") || undefined,+ traceId: url.searchParams.get("traceId") || undefined,+ })+ );if (err) {return Response.json({ success: false, err: err.message || String(err) }, { status: 400 });diff --git a/src/client/src/app/api/project/environment/route.ts b/src/client/src/app/api/project/environment/route.tsnew file mode 100644index 00000000..7e4bc499--- /dev/null+++ b/src/client/src/app/api/project/environment/route.ts@@ -0,0 +1,22 @@+import { NextRequest } from "next/server";+import asaw from "@/utils/asaw";+import { errorResponse } from "@/utils/api-response";+import { getCurrentUser } from "@/lib/session";+import { createProjectEnvironment, listProjectEnvironments } from "@/lib/project-environment";+import { withConnectorAccess, withConnectorAudit } from "@/lib/access/connector-route";++async function GETHandler() {+ if (!(await getCurrentUser())) return Response.json("Unauthorized", { status: 401 });+ const [err, environments] = await asaw(listProjectEnvironments());+ return err ? errorResponse(err, "Failed to list environments") : Response.json({ environments });+}++async function POSTHandler(request: NextRequest) {+ if (!(await getCurrentUser())) return Response.json("Unauthorized", { status: 401 });+ const body = (await request.json()) as { name?: unknown };+ const [err, environment] = await asaw(createProjectEnvironment(body.name));+ return err ? errorResponse(err, "Failed to create environment") : Response.json({ environment });+}++export const GET = withConnectorAccess("read", GETHandler);+export const POST = withConnectorAudit(withConnectorAccess("create", POSTHandler));diff --git a/src/client/src/app/api/rule-engine/entities/route.ts b/src/client/src/app/api/rule-engine/entities/route.tsindex cdd0d8fb..8d1c5672 100644--- a/src/client/src/app/api/rule-engine/entities/route.ts+++ b/src/client/src/app/api/rule-engine/entities/route.ts@@ -1,5 +1,3 @@-import { withAudit } from "@/lib/audit/route";-import { withCurrentOrganisationPermission } from "@/lib/rbac/current";import { SERVER_EVENTS } from "@/constants/events";import { RuleEntityInput } from "@/types/rule-engine";import { getRuleEntities, addRuleEntity, deleteRuleEntity } from "@/lib/platform/rule-engine";@@ -9,8 +7,11 @@ import {} from "@/lib/platform/evaluation/sync-rule-entities";import PostHogServer from "@/lib/posthog";import asaw from "@/utils/asaw";+import { resolveRuleEngineDatabaseConfigId } from "@/lib/platform/rule-engine/source";-async function GETHandler(request: Request) {+export async function GET(request: Request) {+ const startTimestamp = Date.now();+ const databaseConfigId = await resolveRuleEngineDatabaseConfigId(request);const { searchParams } = new URL(request.url);const filters = {rule_id: searchParams.get("rule_id") || undefined,@@ -18,17 +19,26 @@ async function GETHandler(request: Request) {entity_id: searchParams.get("entity_id") || undefined,};- const { err, data }: any = await getRuleEntities(filters);+ const { err, data }: any = await getRuleEntities(filters, databaseConfigId);if (err) {+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.RULE_ENTITIES_LIST_FAILURE,+ startTimestamp,+ });return Response.json(err, { status: 400 });}+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.RULE_ENTITIES_LIST_SUCCESS,+ startTimestamp,+ });return Response.json(data);}-async function POSTHandler(request: Request) {+export async function POST(request: Request) {const startTimestamp = Date.now();const formData = await request.json();+ const databaseConfigId = await resolveRuleEngineDatabaseConfigId(request);const entityInput: Partial<RuleEntityInput> = {rule_id: formData.rule_id,@@ -36,7 +46,9 @@ async function POSTHandler(request: Request) {entity_id: formData.entity_id,};- const [err, res]: any = await asaw(addRuleEntity(entityInput));+ const [err, res]: any = await asaw(+ addRuleEntity(entityInput, { databaseConfigId })+ );if (err) {PostHogServer.fireEvent({event: SERVER_EVENTS.RULE_ENTITIES_CREATE_FAILURE,@@ -62,8 +74,9 @@ async function POSTHandler(request: Request) {return Response.json(res);}-async function DELETEHandler(request: Request) {+export async function DELETE(request: Request) {const startTimestamp = Date.now();+ const databaseConfigId = await resolveRuleEngineDatabaseConfigId(request);const { searchParams } = new URL(request.url);let id = searchParams.get("id");if (!id) {@@ -76,10 +89,10 @@ async function DELETEHandler(request: Request) {const { err: fetchErr, data: entities } = (await getRuleEntities({id,- })) as { err?: any; data?: Array<{ id: string; rule_id: string; entity_type: string; entity_id: string }> };+ }, databaseConfigId)) as { err?: any; data?: Array<{ id: string; rule_id: string; entity_type: string; entity_id: string }> };const entity = !fetchErr && entities?.[0] ? entities[0] : null;- const [err, res] = await deleteRuleEntity(id);+ const [err, res] = await deleteRuleEntity(id, { databaseConfigId });if (err) {PostHogServer.fireEvent({event: SERVER_EVENTS.RULE_ENTITIES_DELETE_FAILURE,@@ -101,7 +114,3 @@ async function DELETEHandler(request: Request) {});return Response.json(res);}--export const GET = withCurrentOrganisationPermission("rule_engine:read", GETHandler);-export const POST = withAudit(withCurrentOrganisationPermission("rule_engine:configure", POSTHandler));-export const DELETE = withAudit(withCurrentOrganisationPermission("rule_engine:configure", DELETEHandler));diff --git a/src/client/src/app/api/rule-engine/evaluate/route.ts b/src/client/src/app/api/rule-engine/evaluate/route.tsindex e07ceb48..031966b5 100644--- a/src/client/src/app/api/rule-engine/evaluate/route.ts+++ b/src/client/src/app/api/rule-engine/evaluate/route.ts@@ -1,16 +1,39 @@import { SERVER_EVENTS } from "@/constants/events";import { EvaluateInput } from "@/types/rule-engine";import { evaluateRules } from "@/lib/platform/rule-engine/evaluate";+import { getAPIKeyInfo } from "@/lib/platform/api-keys";+import { getCurrentUser } from "@/lib/session";+import getMessage from "@/constants/messages";import PostHogServer from "@/lib/posthog";import asaw from "@/utils/asaw";-import { resolveDbConfigId } from "@/helpers/server/auth";+import { resolveRuleEngineDatabaseConfigId } from "@/lib/platform/rule-engine/source";export async function POST(request: Request) {const startTimestamp = Date.now();// --- Authentication ---- const [authErr, databaseConfigId] = await resolveDbConfigId(request);- if (authErr) {- return Response.json({ err: authErr }, { status: 401 });+ // Prefer Bearer token (external API usage); fall back to session auth.+ const authorizationHeader = request.headers.get("Authorization") || "";+ let databaseConfigId: string | undefined;++ if (authorizationHeader.startsWith("Bearer ")) {+ const apiKey = authorizationHeader.replace(/^Bearer /, "").trim();+ if (!apiKey) {+ return Response.json({ err: getMessage().NO_API_KEY }, { status: 401 });+ }++ const [keyErr, apiInfo] = await getAPIKeyInfo({ apiKey });+ if (keyErr || !apiInfo?.databaseConfigId) {+ return Response.json({ err: getMessage().NO_API_KEY }, { status: 401 });+ }++ databaseConfigId = apiInfo.databaseConfigId;+ } else {+ // Session-based auth (dashboard usage)+ const user = await getCurrentUser();+ if (!user) {+ return Response.json({ err: getMessage().UNAUTHORIZED_USER }, { status: 401 });+ }+ databaseConfigId = await resolveRuleEngineDatabaseConfigId(request);}// --- Parse request body ---diff --git a/src/client/src/app/api/rule-engine/field-values/route.ts b/src/client/src/app/api/rule-engine/field-values/route.tsindex 97f26db2..0009285f 100644--- a/src/client/src/app/api/rule-engine/field-values/route.ts+++ b/src/client/src/app/api/rule-engine/field-values/route.ts@@ -1,50 +1,46 @@import { NextRequest, NextResponse } from "next/server";-import { withCurrentOrganisationPermission } from "@/lib/rbac/current";+import { SERVER_EVENTS } from "@/constants/events";import { getCurrentUser } from "@/lib/session";-import { dataCollector, OTEL_TRACES_TABLE_NAME } from "@/lib/platform/common";--// Maps condition field names to ClickHouse column expressions-const FIELD_COLUMN_MAP: Record<string, string> = {- ServiceName: "ServiceName",- SpanName: "SpanName",- SpanKind: "SpanKind",- StatusCode: "StatusCode",- "deployment.environment": "SpanAttributes['deployment.environment']",- "service.name": "ResourceAttributes['service.name']",- "gen_ai.system": "SpanAttributes['gen_ai.system']",- "gen_ai.request.model": "SpanAttributes['gen_ai.request.model']",-};--async function GETHandler(request: NextRequest) {+import { listRecentRuleTraces, getRuleTraceFieldValue } from "@/lib/platform/rule-engine/telemetry";+import PostHogServer from "@/lib/posthog";+import { getRequestEnvironment } from "@/constants/openlit-context";++const SUPPORTED_FIELDS = new Set([+ "ServiceName", "SpanName", "SpanKind", "StatusCode",+ "deployment.environment", "service.name", "gen_ai.system", "gen_ai.request.model",+ "gen_ai.usage.input_tokens", "gen_ai.usage.output_tokens",+ "gen_ai.usage.total_cost", "gen_ai.request.temperature",+]);++export async function GET(request: NextRequest) {+ const startTimestamp = Date.now();const user = await getCurrentUser();if (!user) {return NextResponse.json({ error: "Unauthorized" }, { status: 401 });}const field = request.nextUrl.searchParams.get("field");- if (!field || !FIELD_COLUMN_MAP[field]) {+ if (!field || !SUPPORTED_FIELDS.has(field)) {return NextResponse.json({ values: [] });}- const columnExpr = FIELD_COLUMN_MAP[field];-- const query = `- SELECT DISTINCT ${columnExpr} AS val- FROM ${OTEL_TRACES_TABLE_NAME}- WHERE notEmpty(toString(${columnExpr}))- ORDER BY val ASC- LIMIT 100;- `;-- const { data, err } = await dataCollector({ query }, "query");- if (err) {+ let traces: Record<string, any>[];+ try {+ traces = await listRecentRuleTraces(1000, getRequestEnvironment(request));+ } catch {+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.RULE_FIELD_VALUES_FAILURE,+ startTimestamp,+ });return NextResponse.json({ values: [] });}- const values = ((data as any[]) || [])- .map((row: any) => String(row.val))- .filter(Boolean);+ const values = Array.from(new Set(+ traces.map((trace) => getRuleTraceFieldValue(trace, field)).filter(Boolean)+ )).sort().slice(0, 100);+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.RULE_FIELD_VALUES_SUCCESS,+ startTimestamp,+ });return NextResponse.json({ values });}--export const GET = withCurrentOrganisationPermission("rule_engine:read", GETHandler);diff --git a/src/client/src/app/api/rule-engine/rules/[id]/conditions/route.ts b/src/client/src/app/api/rule-engine/rules/[id]/conditions/route.tsindex 1317804b..c1fab0bf 100644--- a/src/client/src/app/api/rule-engine/rules/[id]/conditions/route.ts+++ b/src/client/src/app/api/rule-engine/rules/[id]/conditions/route.ts@@ -5,15 +5,19 @@ import { RuleConditionGroupInput } from "@/types/rule-engine";import { addConditionGroupsToRule } from "@/lib/platform/rule-engine";import PostHogServer from "@/lib/posthog";import asaw from "@/utils/asaw";+import { resolveRuleEngineDatabaseConfigId } from "@/lib/platform/rule-engine/source";async function POSTHandler(request: Request, context: any) {const startTimestamp = Date.now();const { id } = context.params;const formData = await request.json();+ const databaseConfigId = await resolveRuleEngineDatabaseConfigId(request);const conditionGroups: RuleConditionGroupInput[] = formData.condition_groups || [];- const [err, res]: any = await asaw(addConditionGroupsToRule(id, conditionGroups));+ const [err, res]: any = await asaw(+ addConditionGroupsToRule(id, conditionGroups, { databaseConfigId })+ );if (err) {PostHogServer.fireEvent({event: SERVER_EVENTS.RULE_CONDITIONS_UPDATE_FAILURE,diff --git a/src/client/src/app/api/rule-engine/rules/[id]/preview/route.ts b/src/client/src/app/api/rule-engine/rules/[id]/preview/route.tsindex 643aeebb..69627e21 100644--- a/src/client/src/app/api/rule-engine/rules/[id]/preview/route.ts+++ b/src/client/src/app/api/rule-engine/rules/[id]/preview/route.ts@@ -1,8 +1,11 @@import { NextRequest, NextResponse } from "next/server";-import { withCurrentOrganisationPermission } from "@/lib/rbac/current";+import { SERVER_EVENTS } from "@/constants/events";import { getCurrentUser } from "@/lib/session";import { getRuleById } from "@/lib/platform/rule-engine";-import { dataCollector, OTEL_TRACES_TABLE_NAME } from "@/lib/platform/common";+import { listRecentRuleTraces, getRuleTraceFieldValue } from "@/lib/platform/rule-engine/telemetry";+import PostHogServer from "@/lib/posthog";+import { getRequestEnvironment } from "@/constants/openlit-context";+import { resolveRuleEngineDatabaseConfigId } from "@/lib/platform/rule-engine/source";type Condition = {field: string;@@ -16,27 +19,6 @@ type ConditionGroup = {conditions: Condition[];};-// Map condition field name → alias column in the SELECT query-function getTraceFieldValue(trace: Record<string, any>, field: string): string {- const map: Record<string, string> = {- ServiceName: "ServiceName",- SpanName: "SpanName",- SpanKind: "SpanKind",- Duration: "Duration",- StatusCode: "StatusCode",- "deployment.environment": "env",- "service.name": "service_name",- "gen_ai.system": "gen_ai_system",- "gen_ai.request.model": "model",- "gen_ai.usage.input_tokens": "input_tokens",- "gen_ai.usage.output_tokens": "output_tokens",- "gen_ai.usage.total_cost": "total_cost",- "gen_ai.request.temperature": "temperature",- };- const col = map[field];- return col ? String(trace[col] ?? "") : "";-}-function evalCondition(cond: Condition, traceValue: string): boolean {const { operator, value, data_type } = cond;@@ -84,7 +66,7 @@ function evaluateRule(const groupResults = groups.map((group) => {if (group.conditions.length === 0) return false;const condResults = group.conditions.map((cond) =>- evalCondition(cond, getTraceFieldValue(trace, cond.field))+ evalCondition(cond, getRuleTraceFieldValue(trace, cond.field)));return group.condition_operator === "AND"? condResults.every(Boolean)@@ -95,10 +77,11 @@ function evaluateRule(: groupResults.some(Boolean);}-async function POSTHandler(+export async function POST(_req: NextRequest,{ params }: { params: { id: string } }) {+ const startTimestamp = Date.now();try {const user = await getCurrentUser();if (!user) {@@ -106,9 +89,10 @@ async function POSTHandler(}const ruleId = params.id;+ const databaseConfigId = await resolveRuleEngineDatabaseConfigId(_req);// getRuleById returns { data: ruleObject } (not an array)- const ruleResult = await getRuleById(ruleId);+ const ruleResult = await getRuleById(ruleId, databaseConfigId);if ((ruleResult as any).err || !ruleResult.data) {return NextResponse.json({ error: "Rule not found" }, { status: 404 });}@@ -117,35 +101,9 @@ async function POSTHandler(return NextResponse.json({ error: "Rule not found" }, { status: 404 });}- // Fetch recent traces to evaluate against the rule conditions- const query = `- SELECT- TraceId,- SpanId,- ServiceName,- SpanName,- SpanKind,- toString(Duration) AS Duration,- StatusCode,- SpanAttributes['deployment.environment'] AS env,- ResourceAttributes['service.name'] AS service_name,- SpanAttributes['gen_ai.system'] AS gen_ai_system,- SpanAttributes['gen_ai.request.model'] AS model,- SpanAttributes['gen_ai.usage.input_tokens'] AS input_tokens,- SpanAttributes['gen_ai.usage.output_tokens'] AS output_tokens,- SpanAttributes['gen_ai.usage.total_cost'] AS total_cost,- SpanAttributes['gen_ai.request.temperature'] AS temperature- FROM ${OTEL_TRACES_TABLE_NAME}- ORDER BY Timestamp DESC- LIMIT 100;- `;-- const { data: tracesData, err: tracesErr } = await dataCollector({ query }, "query");- if (tracesErr) {- return NextResponse.json({ error: "Failed to fetch traces" }, { status: 500 });- }-- const traces = (tracesData as any[]) || [];+ // Fetch through the active traces connector; this is intentionally not a+ // direct ClickHouse query because rules must preview external telemetry.+ const traces = await listRecentRuleTraces(100, getRequestEnvironment(_req));// Build condition groups from the saved rule dataconst groups: ConditionGroup[] = (rule.condition_groups || []).map((g: any) => ({@@ -172,11 +130,17 @@ async function POSTHandler(matched: true,}));+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.RULE_PREVIEW_SUCCESS,+ startTimestamp,+ });return NextResponse.json({ results });} catch (err: any) {+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.RULE_PREVIEW_FAILURE,+ startTimestamp,+ });const message = err?.message || "Internal server error";return NextResponse.json({ error: message }, { status: 500 });}}--export const POST = withCurrentOrganisationPermission("rule_engine:evaluate", POSTHandler);diff --git a/src/client/src/app/api/rule-engine/rules/[id]/route.ts b/src/client/src/app/api/rule-engine/rules/[id]/route.tsindex 29050e6c..38d64b45 100644--- a/src/client/src/app/api/rule-engine/rules/[id]/route.ts+++ b/src/client/src/app/api/rule-engine/rules/[id]/route.ts@@ -1,25 +1,35 @@-import { withAudit } from "@/lib/audit/route";-import { withCurrentOrganisationPermission } from "@/lib/rbac/current";import { SERVER_EVENTS } from "@/constants/events";import { RuleInput } from "@/types/rule-engine";import { getRuleById, updateRule, deleteRule } from "@/lib/platform/rule-engine";import PostHogServer from "@/lib/posthog";import asaw from "@/utils/asaw";+import { resolveRuleEngineDatabaseConfigId } from "@/lib/platform/rule-engine/source";-async function GETHandler(_: Request, context: any) {+export async function GET(request: Request, context: any) {+ const startTimestamp = Date.now();const { id } = context.params;- const { err, data }: any = await getRuleById(id);+ const databaseConfigId = await resolveRuleEngineDatabaseConfigId(request);+ const { err, data }: any = await getRuleById(id, databaseConfigId);if (err) {+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.RULE_GET_FAILURE,+ startTimestamp,+ });return Response.json(err, { status: 400 });}+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.RULE_GET_SUCCESS,+ startTimestamp,+ });return Response.json(data);}-async function PUTHandler(request: Request, context: any) {+export async function PUT(request: Request, context: any) {const startTimestamp = Date.now();const { id } = context.params;const formData = await request.json();+ const databaseConfigId = await resolveRuleEngineDatabaseConfigId(request);const ruleInput: Partial<RuleInput> = {name: formData.name,@@ -28,7 +38,9 @@ async function PUTHandler(request: Request, context: any) {status: formData.status,};- const [err, res]: any = await asaw(updateRule(id, ruleInput));+ const [err, res]: any = await asaw(+ updateRule(id, ruleInput, { databaseConfigId })+ );if (err) {PostHogServer.fireEvent({event: SERVER_EVENTS.RULE_UPDATE_FAILURE,@@ -44,10 +56,11 @@ async function PUTHandler(request: Request, context: any) {return Response.json(res);}-async function DELETEHandler(_: Request, context: any) {+export async function DELETE(request: Request, context: any) {const startTimestamp = Date.now();const { id } = context.params;- const [err, res] = await deleteRule(id);+ const databaseConfigId = await resolveRuleEngineDatabaseConfigId(request);+ const [err, res] = await deleteRule(id, { databaseConfigId });if (err) {PostHogServer.fireEvent({event: SERVER_EVENTS.RULE_DELETE_FAILURE,@@ -62,7 +75,3 @@ async function DELETEHandler(_: Request, context: any) {});return Response.json(res);}--export const GET = withCurrentOrganisationPermission("rule_engine:read", GETHandler);-export const PUT = withAudit(withCurrentOrganisationPermission("rule_engine:configure", PUTHandler));-export const DELETE = withAudit(withCurrentOrganisationPermission("rule_engine:configure", DELETEHandler));diff --git a/src/client/src/app/api/rule-engine/rules/route.ts b/src/client/src/app/api/rule-engine/rules/route.tsindex 4e895dad..82aa63db 100644--- a/src/client/src/app/api/rule-engine/rules/route.ts+++ b/src/client/src/app/api/rule-engine/rules/route.ts@@ -1,30 +1,33 @@-import { withAudit } from "@/lib/audit/route";-import { withCurrentOrganisationPermission } from "@/lib/rbac/current";import { SERVER_EVENTS } from "@/constants/events";import { RuleInput } from "@/types/rule-engine";import { getRules, createRule } from "@/lib/platform/rule-engine";import PostHogServer from "@/lib/posthog";import asaw from "@/utils/asaw";+import { resolveRuleEngineDatabaseConfigId } from "@/lib/platform/rule-engine/source";-import { resolveDbConfigId } from "@/helpers/server/auth";--async function GETHandler(request: Request) {- const [authErr, databaseConfigId] = await resolveDbConfigId(request);- if (authErr) {- return Response.json({ err: authErr }, { status: 401 });- }-+export async function GET(request: Request) {+ const startTimestamp = Date.now();+ const databaseConfigId = await resolveRuleEngineDatabaseConfigId(request);const { err, data }: any = await getRules(databaseConfigId);if (err) {+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.RULE_LIST_FAILURE,+ startTimestamp,+ });return Response.json(err, { status: 400 });}+ PostHogServer.fireEvent({+ event: SERVER_EVENTS.RULE_LIST_SUCCESS,+ startTimestamp,+ });return Response.json(data);}-async function POSTHandler(request: Request) {+export async function POST(request: Request) {const startTimestamp = Date.now();const formData = await request.json();+ const databaseConfigId = await resolveRuleEngineDatabaseConfigId(request);const ruleInput: Partial<RuleInput> = {name: formData.name,@@ -33,7 +36,9 @@ async function POSTHandler(request: Request) {status: formData.status,};- const [err, res]: any = await asaw(createRule(ruleInput));+ const [err, res]: any = await asaw(+ createRule(ruleInput, { databaseConfigId })+ );if (err) {PostHogServer.fireEvent({event: SERVER_EVENTS.RULE_CREATE_FAILURE,@@ -48,6 +53,3 @@ async function POSTHandler(request: Request) {});return Response.json(res);}--export const GET = withCurrentOrganisationPermission("rule_engine:read", GETHandler);-export const POST = withAudit(withCurrentOrganisationPermission("rule_engine:configure", POSTHandler));diff --git a/src/client/src/app/api/telemetry-source/[id]/health/route.ts b/src/client/src/app/api/telemetry-source/[id]/health/route.tsnew file mode 100644index 00000000..5e520a07--- /dev/null+++ b/src/client/src/app/api/telemetry-source/[id]/health/route.ts@@ -0,0 +1,37 @@+import { getCurrentUser } from "@/lib/session";+import asaw from "@/utils/asaw";+import { errorResponse } from "@/utils/api-response";+import {+ healthCheckTelemetrySource,+ validateTelemetrySourceAISignal,+} from "@/lib/telemetry-source-crud";+import { NextRequest } from "next/server";+import { withConnectorAccess, withConnectorAudit } from "@/lib/access/connector-route";++const DEFAULT_PROBE_MS = 60 * 60 * 1000;++async function GETHandler(+ request: NextRequest,+ { params }: { params: { id: string } }+) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });++ const [healthErr, health] = await asaw(+ healthCheckTelemetrySource(params.id)+ );+ if (healthErr)+ return errorResponse(healthErr, "Failed to health-check telemetry source");++ const end = new Date();+ const start = new Date(end.getTime() - DEFAULT_PROBE_MS);+ const [validateErr, validation] = await asaw(+ validateTelemetrySourceAISignal(params.id, { start, end })+ );+ if (validateErr)+ return errorResponse(validateErr, "Failed to validate telemetry source");++ return Response.json({ health, validation });+}++export const GET = withConnectorAudit(withConnectorAccess("test", GETHandler));diff --git a/src/client/src/app/api/telemetry-source/[id]/route.ts b/src/client/src/app/api/telemetry-source/[id]/route.tsnew file mode 100644index 00000000..46517c53--- /dev/null+++ b/src/client/src/app/api/telemetry-source/[id]/route.ts@@ -0,0 +1,46 @@+import { getCurrentUser } from "@/lib/session";+import asaw from "@/utils/asaw";+import { errorResponse } from "@/utils/api-response";+import {+ deleteTelemetrySource,+ updateTelemetrySource,+} from "@/lib/telemetry-source-crud";+import { TELEMETRY_SOURCE_INVALID_JSON } from "@/constants/messages/en";+import { NextRequest } from "next/server";+import { withConnectorAccess, withConnectorAudit } from "@/lib/access/connector-route";++async function PATCHHandler(+ request: NextRequest,+ { params }: { params: { id: string } }+) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });++ let body: unknown;+ try {+ body = await request.json();+ } catch {+ return Response.json({ err: TELEMETRY_SOURCE_INVALID_JSON }, { status: 400 });+ }++ const [err, source] = await asaw(+ updateTelemetrySource(params.id, body as Record<string, unknown>)+ );+ if (err) return errorResponse(err, "Failed to update telemetry source");+ return Response.json(source);+}++async function DELETEHandler(+ _request: NextRequest,+ { params }: { params: { id: string } }+) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });++ const [err, res] = await asaw(deleteTelemetrySource(params.id));+ if (err) return errorResponse(err, "Failed to delete telemetry source");+ return Response.json(res);+}++export const PATCH = withConnectorAudit(withConnectorAccess("update", PATCHHandler));+export const DELETE = withConnectorAudit(withConnectorAccess("delete", DELETEHandler));diff --git a/src/client/src/app/api/telemetry-source/binding/route.ts b/src/client/src/app/api/telemetry-source/binding/route.tsnew file mode 100644index 00000000..5ec5517a--- /dev/null+++ b/src/client/src/app/api/telemetry-source/binding/route.ts@@ -0,0 +1,66 @@+import { getCurrentUser } from "@/lib/session";+import asaw from "@/utils/asaw";+import { errorResponse } from "@/utils/api-response";+import {+ deleteTelemetrySourceBinding,+ listTelemetrySourceBindings,+ setTelemetrySourceBinding,+} from "@/lib/telemetry-source-crud";+import { TELEMETRY_SOURCE_INVALID_JSON } from "@/constants/messages/en";+import { NextRequest } from "next/server";+import { withConnectorAccess, withConnectorAudit } from "@/lib/access/connector-route";+import { OPENLIT_CONTEXT_HEADERS } from "@/constants/openlit-context";++async function GETHandler(request: NextRequest) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });++ const [err, bindings] = await asaw(+ listTelemetrySourceBindings(+ request.headers.get(OPENLIT_CONTEXT_HEADERS.environment) ||+ request.nextUrl.searchParams.get("environment") ||+ undefined+ )+ );+ if (err) return errorResponse(err, "Failed to list telemetry source bindings");+ return Response.json({ bindings });+}++async function PUTHandler(request: NextRequest) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });++ let body: { signal?: unknown; sourceId?: unknown; environment?: unknown };+ try {+ body = await request.json();+ } catch {+ return Response.json({ err: TELEMETRY_SOURCE_INVALID_JSON }, { status: 400 });+ }++ const [err, binding] = await asaw(+ setTelemetrySourceBinding(body?.signal, String(body?.sourceId ?? ""), body?.environment)+ );+ if (err) return errorResponse(err, "Failed to set telemetry source binding");+ return Response.json(binding);+}++async function DELETEHandler(request: NextRequest) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });++ const signal = request.nextUrl.searchParams.get("signal");+ const [err, result] = await asaw(+ deleteTelemetrySourceBinding(+ signal,+ request.headers.get(OPENLIT_CONTEXT_HEADERS.environment) ||+ request.nextUrl.searchParams.get("environment") ||+ undefined+ )+ );+ if (err) return errorResponse(err, "Failed to delete telemetry source binding");+ return Response.json(result);+}++export const GET = withConnectorAccess("read", GETHandler);+export const PUT = withConnectorAudit(withConnectorAccess("bind", PUTHandler));+export const DELETE = withConnectorAudit(withConnectorAccess("bind", DELETEHandler));diff --git a/src/client/src/app/api/telemetry-source/route.ts b/src/client/src/app/api/telemetry-source/route.tsnew file mode 100644index 00000000..737ba82d--- /dev/null+++ b/src/client/src/app/api/telemetry-source/route.ts@@ -0,0 +1,53 @@+import { getCurrentUser } from "@/lib/session";+import asaw from "@/utils/asaw";+import { errorResponse } from "@/utils/api-response";+import {+ availableSourceTypes,+ availableSourceTypeDescriptors,+ createTelemetrySource,+ listTelemetrySources,+ resolveProjectSignalCapabilities,+} from "@/lib/telemetry-source-crud";+import { TELEMETRY_SOURCE_INVALID_JSON } from "@/constants/messages/en";+import { NextRequest } from "next/server";+import { withConnectorAccess, withConnectorAudit } from "@/lib/access/connector-route";+import { isVisibleConnectorType } from "@/lib/platform/connectors/visible-types";++async function GETHandler(request: NextRequest) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });+ const environment = request.nextUrl.searchParams.get("environment") || undefined;++ const [err, sources] = await asaw(listTelemetrySources());+ if (err) return errorResponse(err, "Failed to list telemetry sources");+ const [, signalCapabilities] = await asaw(+ resolveProjectSignalCapabilities(environment)+ );+ return Response.json({+ sources,+ availableTypes: availableSourceTypes().filter(isVisibleConnectorType),+ availableTypeDescriptors: availableSourceTypeDescriptors().filter((descriptor) => isVisibleConnectorType(descriptor.type)),+ signalCapabilities: signalCapabilities ?? null,+ });+}++async function POSTHandler(request: NextRequest) {+ const user = await getCurrentUser();+ if (!user) return Response.json("Unauthorized", { status: 401 });++ let body: unknown;+ try {+ body = await request.json();+ } catch {+ return Response.json({ err: TELEMETRY_SOURCE_INVALID_JSON }, { status: 400 });+ }++ const [err, source] = await asaw(+ createTelemetrySource(body as Record<string, unknown>)+ );+ if (err) return errorResponse(err, "Failed to create telemetry source");+ return Response.json(source);+}++export const GET = withConnectorAccess("read", GETHandler);+export const POST = withConnectorAudit(withConnectorAccess("create", POSTHandler));diff --git a/src/client/src/app/api/telemetry/[...segments]/route.ts b/src/client/src/app/api/telemetry/[...segments]/route.tsnew file mode 100644index 00000000..3438f222--- /dev/null+++ b/src/client/src/app/api/telemetry/[...segments]/route.ts@@ -0,0 +1,70 @@+import type { NextRequest } from "next/server";+import { withRouteAccess } from "@/lib/access/route-access";++type RouteContext = { params: { segments: string[] } };+type RouteHandler = (request: Request, context?: any) => Promise<Response>;++async function post(request: Request, context: RouteContext, path: string): Promise<Response> {+ const modules: Record<string, () => Promise<{ POST: RouteHandler }>> = {+ "exception": () => import("@/app/api/metrics/exception/route"),+ "exception/grouped": () => import("@/app/api/metrics/exception/grouped/route"),+ "request": () => import("@/app/api/metrics/request/route"),+ "request/attribute-keys": () => import("@/app/api/metrics/request/attribute-keys/route"),+ "request/config": () => import("@/app/api/metrics/request/config/route"),+ "request/duration/average": () => import("@/app/api/metrics/request/duration/average/route"),+ "request/exist": () => import("@/app/api/metrics/request/exist/route"),+ "request/grouped": () => import("@/app/api/metrics/request/grouped/route"),+ "request/time": () => import("@/app/api/metrics/request/time/route"),+ "request/total": () => import("@/app/api/metrics/request/total/route"),+ "llm/category": () => import("@/app/api/metrics/llm/category/route"),+ "llm/endpoint": () => import("@/app/api/metrics/llm/endpoint/route"),+ "llm/cost/application": () => import("@/app/api/metrics/llm/cost/application/route"),+ "llm/cost/environment": () => import("@/app/api/metrics/llm/cost/environment/route"),+ "llm/cost/request/average": () => import("@/app/api/metrics/llm/cost/request/average/route"),+ "llm/cost/total": () => import("@/app/api/metrics/llm/cost/total/route"),+ "llm/model/time": () => import("@/app/api/metrics/llm/model/time/route"),+ "llm/model/top": () => import("@/app/api/metrics/llm/model/top/route"),+ "llm/token/time": () => import("@/app/api/metrics/llm/token/time/route"),+ "llm/token/request/average": () => import("@/app/api/metrics/llm/token/request/average/route"),+ "vector/application": () => import("@/app/api/metrics/vector/application/route"),+ "vector/environment": () => import("@/app/api/metrics/vector/environment/route"),+ "vector/operation": () => import("@/app/api/metrics/vector/operation/route"),+ "vector/system": () => import("@/app/api/metrics/vector/system/route"),+ "gpu/fanspeed/time": () => import("@/app/api/metrics/gpu/fanspeed/time/route"),+ "gpu/memory/average": () => import("@/app/api/metrics/gpu/memory/average/route"),+ "gpu/memory/time": () => import("@/app/api/metrics/gpu/memory/time/route"),+ "gpu/power/average": () => import("@/app/api/metrics/gpu/power/average/route"),+ "gpu/power/time": () => import("@/app/api/metrics/gpu/power/time/route"),+ "gpu/temperature/average": () => import("@/app/api/metrics/gpu/temperature/average/route"),+ "gpu/temperature/time": () => import("@/app/api/metrics/gpu/temperature/time/route"),+ "gpu/utilization/average": () => import("@/app/api/metrics/gpu/utilization/average/route"),+ "gpu/utilization/time": () => import("@/app/api/metrics/gpu/utilization/time/route"),+ };+ const loader = modules[path];+ if (!loader) return Response.json({ err: `Unknown telemetry route: ${path}` }, { status: 404 });+ return (await loader()).POST(request, context);+}++async function POSTHandler(request: NextRequest, context: RouteContext) {+ return post(request, context, context.params.segments.join("/"));+}++async function GETHandler(request: NextRequest, context: RouteContext) {+ const segments = context.params.segments;+ if (segments.length === 4 && segments[0] === "request" && segments[1] === "span" && segments[3] === "heirarchy") {+ const handler = (await import("@/app/api/metrics/request/span/[id]/heirarchy/route")).GET;+ return handler(request, { params: { id: segments[2] } });+ }+ if (segments.length === 3 && segments[0] === "request" && segments[1] === "span") {+ const handler = (await import("@/app/api/metrics/request/span/[id]/route")).GET;+ return handler(request, { params: { id: segments[2] } });+ }+ if (segments.length === 3 && segments[0] === "request" && segments[1] === "trace") {+ const handler = (await import("@/app/api/metrics/request/trace/[id]/route")).GET;+ return handler(request, { params: { id: segments[2] } });+ }+ return Response.json({ err: `Unknown telemetry route: ${segments.join("/")}` }, { status: 404 });+}++export const GET = withRouteAccess("observability.read", GETHandler, { requireDbConfig: true });+export const POST = withRouteAccess("observability.read", POSTHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/telemetry/exception/route.ts b/src/client/src/app/api/telemetry/exception/route.tsindex c0c1f53d..04988fbf 100644--- a/src/client/src/app/api/telemetry/exception/route.ts+++ b/src/client/src/app/api/telemetry/exception/route.ts@@ -1,59 +1,56 @@import { MetricParams, TimeLimit } from "@/lib/platform/common";+import { withRouteAccess } from "@/lib/access/route-access";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";-import { getRequests, getRequestsConfig } from "@/lib/platform/request";-import { resolveDbConfigId } from "@/helpers/server/auth";+import { listTraceRecords } from "@/lib/platform/traces/read";+import { getRequestEnvironment } from "@/constants/openlit-context";++async function POSTHandler(request: Request) {+ const formData = await request.json();+ const timeLimit = formData.timeLimit as TimeLimit;+ const limit = formData.limit || 10;+ const offset = formData.offset || 0;+ const selectedConfig = formData.selectedConfig || {};+ const sorting = formData.sorting || {};++ const params: MetricParams = {+ timeLimit,+ limit,+ offset,+ selectedConfig,+ sorting,+ statusCode: ["STATUS_CODE_ERROR", "Error", "ERROR"],+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),+ };++ const validationParam = validateMetricsRequest(+ params,+ validateMetricsRequestType.GET_ALL+ );++ if (!validationParam.success)+ return Response.json(validationParam.err, {+ status: 400,+ });-export async function POST(request: Request) {try {- const [authErr, databaseConfigId] = await resolveDbConfigId(request);- if (authErr) {- return Response.json({ err: authErr }, { status: 401 });- }-- const formData = await request.json();- const timeLimit = formData.timeLimit as TimeLimit;- const limit = formData.limit || 10;- const offset = formData.offset || 0;- const selectedConfig = formData.selectedConfig || {};- const sorting = formData.sorting || {};-- const params: MetricParams = {- timeLimit,- limit,- offset,- selectedConfig,- sorting,- statusCode: ["STATUS_CODE_ERROR", "Error"],- databaseConfigId,- };-- const validationParam = validateMetricsRequest(- params,- validateMetricsRequestType.GET_ALL- );-- if (!validationParam.success)- return Response.json(validationParam.err, {- status: 400,- });-- const res: any = await getRequests(params);-- const { searchParams } = new URL(request.url);- const includeFilters = (searchParams.get("includeFilters") === "true") || (formData.includeFilters === true);-- if (includeFilters && !res.err) {- const configRes = await getRequestsConfig(params);- res.pagination = { limit, offset, total: res.total || 0 };- res.filters = (configRes.data as any)?.[0] || {};- }-+ const res: any = await listTraceRecords(params);return Response.json(res);- } catch (error: any) {- console.error("Error in telemetry exception route:", error);- return Response.json({ err: error.message || "Internal Server Error" }, { status: 500 });+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ return Response.json(+ { err: message, code: "TELEMETRY_SOURCE_UNAVAILABLE" },+ { status: 503 }+ );}+}++export const POST = withRouteAccess("traces.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/telemetry/field-values/route.ts b/src/client/src/app/api/telemetry/field-values/route.tsnew file mode 100644index 00000000..03e93e1b--- /dev/null+++ b/src/client/src/app/api/telemetry/field-values/route.ts@@ -0,0 +1,36 @@+import { getRequestEnvironment } from "@/constants/openlit-context";+import type { MetricParams, TimeLimit } from "@/lib/platform/common";+import { getSignalFieldValues } from "@/lib/platform/connectors/datasource/field-values";+import type { Signal } from "@/lib/platform/connectors/datasource/types";+import { withRouteAccess } from "@/lib/access/route-access";++type TelemetrySignal = Exclude<Signal, "intelligence">;+const SIGNALS = new Set<TelemetrySignal>(["traces", "logs", "metrics"]);+const SAFE_FIELD = /^[A-Za-z0-9_.:-]{1,256}$/;++async function POSTHandler(request: Request) {+ const formData = await request.json();+ const signal = formData.signal as TelemetrySignal;+ const field = typeof formData.field === "string" ? formData.field.trim() : "";++ if (!SIGNALS.has(signal) || !SAFE_FIELD.test(field)) {+ return Response.json({ values: [] }, { status: 400 });+ }++ const params: MetricParams = {+ timeLimit: formData.timeLimit as TimeLimit,+ selectedConfig: formData.selectedConfig || {},+ ...(typeof formData.sourceId === "string"+ ? { sourceId: formData.sourceId }+ : {}),+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),+ };++ const result = await getSignalFieldValues(signal, field, params);+ return Response.json(result, { status: result.err ? 503 : 200 });+}++export const POST = withRouteAccess("observability.read", POSTHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/telemetry/logs/[id]/route.ts b/src/client/src/app/api/telemetry/logs/[id]/route.tsnew file mode 100644index 00000000..6252ecf9--- /dev/null+++ b/src/client/src/app/api/telemetry/logs/[id]/route.ts@@ -0,0 +1,37 @@+import { getLogByRowId } from "@/lib/platform/logs/read";+import { getRequestEnvironment } from "@/constants/openlit-context";+import { withRouteAccess } from "@/lib/access/route-access";++async function GETHandler(+ request: Request,+ { params }: { params: { id: string } }+) {+ const url = new URL(request.url);+ const aroundTimestamp =+ url.searchParams.get("aroundTimestamp") || undefined;+ const sourceId = url.searchParams.get("sourceId") || undefined;+ const environment =+ url.searchParams.get("environment") ||+ getRequestEnvironment(request) ||+ undefined;++ try {+ return Response.json(+ await getLogByRowId(params.id, {+ aroundTimestamp,+ sourceId: sourceId || undefined,+ environment: environment || undefined,+ })+ );+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ return Response.json(+ { err: message, code: "TELEMETRY_SOURCE_UNAVAILABLE" },+ { status: 503 }+ );+ }+}++export const GET = withRouteAccess("logs.read", GETHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/telemetry/logs/attribute-keys/route.ts b/src/client/src/app/api/telemetry/logs/attribute-keys/route.tsnew file mode 100644index 00000000..efe6c8ba--- /dev/null+++ b/src/client/src/app/api/telemetry/logs/attribute-keys/route.ts@@ -0,0 +1,41 @@+import { MetricParams, TimeLimit } from "@/lib/platform/common";+import { getLogAttributeKeys } from "@/lib/platform/logs/read";+import { withRouteAccess } from "@/lib/access/route-access";+import {+ validateMetricsRequest,+ validateMetricsRequestType,+} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";++async function POSTHandler(request: Request) {+ const formData = await request.json();+ const params: MetricParams = {+ timeLimit: formData.timeLimit as TimeLimit,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),+ ...(typeof formData.sourceId === "string"+ ? { sourceId: formData.sourceId }+ : {}),+ };+ const validation = validateMetricsRequest(+ params,+ validateMetricsRequestType.GET_ALL+ );+ if (!validation.success) return Response.json(validation.err, { status: 400 });++ try {+ return Response.json(await getLogAttributeKeys(params));+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ return Response.json(+ { err: message, code: "TELEMETRY_SOURCE_UNAVAILABLE" },+ { status: 503 }+ );+ }+}++export const POST = withRouteAccess("logs.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/telemetry/logs/config/route.ts b/src/client/src/app/api/telemetry/logs/config/route.tsnew file mode 100644index 00000000..fadec374--- /dev/null+++ b/src/client/src/app/api/telemetry/logs/config/route.ts@@ -0,0 +1,41 @@+import { MetricParams, TimeLimit } from "@/lib/platform/common";+import { getLogsConfig } from "@/lib/platform/logs/read";+import { withRouteAccess } from "@/lib/access/route-access";+import {+ validateMetricsRequest,+ validateMetricsRequestType,+} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";++async function POSTHandler(request: Request) {+ const formData = await request.json();+ const params: MetricParams = {+ timeLimit: formData.timeLimit as TimeLimit,+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),+ ...(typeof formData.sourceId === "string"+ ? { sourceId: formData.sourceId }+ : {}),+ };+ const validation = validateMetricsRequest(+ params,+ validateMetricsRequestType.GET_ALL+ );+ if (!validation.success) return Response.json(validation.err, { status: 400 });++ try {+ return Response.json(await getLogsConfig(params));+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ return Response.json(+ { err: message, code: "TELEMETRY_SOURCE_UNAVAILABLE" },+ { status: 503 }+ );+ }+}++export const POST = withRouteAccess("logs.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/telemetry/logs/route.ts b/src/client/src/app/api/telemetry/logs/route.tsnew file mode 100644index 00000000..73deb44a--- /dev/null+++ b/src/client/src/app/api/telemetry/logs/route.ts@@ -0,0 +1,45 @@+import { MetricParams, TimeLimit } from "@/lib/platform/common";+import { getLogs } from "@/lib/platform/logs/read";+import { withRouteAccess } from "@/lib/access/route-access";+import {+ validateMetricsRequest,+ validateMetricsRequestType,+} from "@/helpers/server/platform";+import { getRequestEnvironment } from "@/constants/openlit-context";++async function POSTHandler(request: Request) {+ const formData = await request.json();+ const params: MetricParams = {+ timeLimit: formData.timeLimit as TimeLimit,+ limit: formData.limit || 25,+ offset: formData.offset || 0,+ selectedConfig: formData.selectedConfig || {},+ sorting: formData.sorting || {},+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),+ ...(typeof formData.sourceId === "string"+ ? { sourceId: formData.sourceId }+ : {}),+ };+ const validation = validateMetricsRequest(+ params,+ validateMetricsRequestType.GET_ALL+ );+ if (!validation.success) return Response.json(validation.err, { status: 400 });++ try {+ return Response.json(await getLogs(params));+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ return Response.json(+ { err: message, code: "TELEMETRY_SOURCE_UNAVAILABLE" },+ { status: 503 }+ );+ }+}++export const POST = withRouteAccess("logs.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/telemetry/metrics/[name]/route.ts b/src/client/src/app/api/telemetry/metrics/[name]/route.tsindex 455b935c..a11b15ed 100644--- a/src/client/src/app/api/telemetry/metrics/[name]/route.ts+++ b/src/client/src/app/api/telemetry/metrics/[name]/route.ts@@ -1,16 +1,12 @@import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getMetricDetail } from "@/lib/platform/observability";-import { resolveDbConfigId } from "@/helpers/server/auth";+import { getMetricDetailRecord } from "@/lib/platform/metrics/read";+import { getRequestEnvironment } from "@/constants/openlit-context";+import { withRouteAccess } from "@/lib/access/route-access";-export async function POST(+async function POSTHandler(request: Request,{ params }: { params: { name: string } }) {- const [authErr, databaseConfigId] = await resolveDbConfigId(request);- if (authErr) {- return Response.json({ err: authErr }, { status: 401 });- }-const formData = await request.json();const metricName = decodeURIComponent(params.name);const metricType = formData.metricType as string | undefined;@@ -18,10 +14,12 @@ export async function POST(const metricParams: MetricParams = {timeLimit: formData.timeLimit as TimeLimit,selectedConfig: formData.selectedConfig || {},- databaseConfigId,+ environment: typeof formData.environment === "string" ? formData.environment : getRequestEnvironment(request),};return Response.json(- await getMetricDetail(metricName, metricType, serviceName, metricParams)+ await getMetricDetailRecord(metricName, metricType, serviceName, metricParams));}++export const POST = withRouteAccess("metrics.read", POSTHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/telemetry/metrics/attribute-keys/route.ts b/src/client/src/app/api/telemetry/metrics/attribute-keys/route.tsindex 116030df..e0d19c77 100644--- a/src/client/src/app/api/telemetry/metrics/attribute-keys/route.ts+++ b/src/client/src/app/api/telemetry/metrics/attribute-keys/route.ts@@ -1,21 +1,17 @@import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getMetricAttributeKeys } from "@/lib/platform/observability";+import { getMetricAttributeKeysRecord } from "@/lib/platform/metrics/read";+import { withRouteAccess } from "@/lib/access/route-access";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";-import { resolveDbConfigId } from "@/helpers/server/auth";--export async function POST(request: Request) {- const [authErr, databaseConfigId] = await resolveDbConfigId(request);- if (authErr) {- return Response.json({ err: authErr }, { status: 401 });- }+import { getRequestEnvironment } from "@/constants/openlit-context";+async function POSTHandler(request: Request) {const formData = await request.json();const params: MetricParams = {timeLimit: formData.timeLimit as TimeLimit,- databaseConfigId,+ environment: typeof formData.environment === "string" ? formData.environment : getRequestEnvironment(request),};const validation = validateMetricsRequest(params,@@ -23,5 +19,7 @@ export async function POST(request: Request) {);if (!validation.success) return Response.json(validation.err, { status: 400 });- return Response.json(await getMetricAttributeKeys(params));+ return Response.json(await getMetricAttributeKeysRecord(params));}++export const POST = withRouteAccess("metrics.read", POSTHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/telemetry/metrics/config/route.ts b/src/client/src/app/api/telemetry/metrics/config/route.tsindex 9f2903b8..49ec3ab1 100644--- a/src/client/src/app/api/telemetry/metrics/config/route.ts+++ b/src/client/src/app/api/telemetry/metrics/config/route.ts@@ -1,21 +1,17 @@import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getMetricsConfig } from "@/lib/platform/observability";+import { getMetricsFilterConfig } from "@/lib/platform/metrics/read";+import { withRouteAccess } from "@/lib/access/route-access";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";-import { resolveDbConfigId } from "@/helpers/server/auth";--export async function POST(request: Request) {- const [authErr, databaseConfigId] = await resolveDbConfigId(request);- if (authErr) {- return Response.json({ err: authErr }, { status: 401 });- }+import { getRequestEnvironment } from "@/constants/openlit-context";+async function POSTHandler(request: Request) {const formData = await request.json();const params: MetricParams = {timeLimit: formData.timeLimit as TimeLimit,- databaseConfigId,+ environment: typeof formData.environment === "string" ? formData.environment : getRequestEnvironment(request),};const validation = validateMetricsRequest(params,@@ -23,5 +19,7 @@ export async function POST(request: Request) {);if (!validation.success) return Response.json(validation.err, { status: 400 });- return Response.json(await getMetricsConfig(params));+ return Response.json(await getMetricsFilterConfig(params));}++export const POST = withRouteAccess("metrics.read", POSTHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/telemetry/metrics/route.ts b/src/client/src/app/api/telemetry/metrics/route.tsindex 05782692..405c28cb 100644--- a/src/client/src/app/api/telemetry/metrics/route.ts+++ b/src/client/src/app/api/telemetry/metrics/route.ts@@ -1,50 +1,34 @@import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getMetrics, getMetricsConfig } from "@/lib/platform/observability";+import { listMetricRecords } from "@/lib/platform/metrics/read";+import { withRouteAccess } from "@/lib/access/route-access";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";-import { resolveDbConfigId } from "@/helpers/server/auth";+import { getRequestEnvironment } from "@/constants/openlit-context";-export async function POST(request: Request) {- try {- const [authErr, databaseConfigId] = await resolveDbConfigId(request);- if (authErr) {- return Response.json({ err: authErr }, { status: 401 });- }-- const formData = await request.json();- const limit = formData.limit || 25;- const offset = formData.offset || 0;- const params: MetricParams = {- timeLimit: formData.timeLimit as TimeLimit,- limit,- offset,- selectedConfig: formData.selectedConfig || {},- sorting: formData.sorting || {},- databaseConfigId,- };-- const validation = validateMetricsRequest(- params,- validateMetricsRequestType.GET_ALL- );- if (!validation.success) return Response.json(validation.err, { status: 400 });-- const res: any = await getMetrics(params);+async function POSTHandler(request: Request) {+ const formData = await request.json();+ const params: MetricParams = {+ timeLimit: formData.timeLimit as TimeLimit,+ limit: formData.limit || 25,+ offset: formData.offset || 0,+ selectedConfig: formData.selectedConfig || {},+ sorting: formData.sorting || {},+ environment: typeof formData.environment === "string" ? formData.environment : getRequestEnvironment(request),+ };+ const validation = validateMetricsRequest(+ params,+ validateMetricsRequestType.GET_ALL+ );+ if (!validation.success) return Response.json(validation.err, { status: 400 });- const { searchParams } = new URL(request.url);- const includeFilters = (searchParams.get("includeFilters") === "true") || (formData.includeFilters === true);-- if (includeFilters && !res.err) {- const configRes = await getMetricsConfig(params);- res.pagination = { limit, offset, total: res.total || 0 };- res.filters = (configRes.data as any)?.[0] || {};- }-- return Response.json(res);- } catch (error: any) {- console.error("Error in telemetry metrics route:", error);- return Response.json({ err: error.message || "Internal Server Error" }, { status: 500 });+ try {+ return Response.json(await listMetricRecords(params));+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ return Response.json({ err: message, code: "TELEMETRY_SOURCE_UNAVAILABLE" }, { status: 503 });}}++export const POST = withRouteAccess("metrics.read", POSTHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/telemetry/summary/[signal]/route.ts b/src/client/src/app/api/telemetry/summary/[signal]/route.tsindex 157dc1a6..56a1e0f0 100644--- a/src/client/src/app/api/telemetry/summary/[signal]/route.ts+++ b/src/client/src/app/api/telemetry/summary/[signal]/route.ts@@ -1,31 +1,57 @@import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getSignalSummary } from "@/lib/platform/observability";+import { getTraceSummary } from "@/lib/platform/traces/read";+import { getLogsSummary } from "@/lib/platform/logs/read";+import { getMetricsSummary } from "@/lib/platform/metrics/read";+import {+ requireRouteAccess,+ withRouteAccess,+ type RouteAccessKey,+} from "@/lib/access/route-access";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";-import { resolveDbConfigId } from "@/helpers/server/auth";+import { getRequestEnvironment } from "@/constants/openlit-context";+import asaw from "@/utils/asaw";const VALID_SIGNALS = new Set(["traces", "exceptions", "logs", "metrics"]);+const SIGNAL_ACCESS: Record<string, RouteAccessKey> = {+ traces: "traces.read",+ exceptions: "traces.read",+ logs: "logs.read",+ metrics: "metrics.read",+};++/** Route each signal's summary through its per-signal read facade. */+function summaryForSignal(signal: string, params: MetricParams) {+ if (signal === "logs") return getLogsSummary(params);+ if (signal === "metrics") return getMetricsSummary(params);+ return getTraceSummary(params, signal as "traces" | "exceptions");+}-export async function POST(+async function POSTHandler(request: Request,{ params }: { params: { signal: string } }) {- const [authErr, databaseConfigId] = await resolveDbConfigId(request);- if (authErr) {- return Response.json({ err: authErr }, { status: 401 });- }-if (!VALID_SIGNALS.has(params.signal)) {return Response.json({ err: "Invalid signal" }, { status: 400 });}+ const [permissionErr] = await asaw(+ requireRouteAccess(SIGNAL_ACCESS[params.signal])+ );+ if (permissionErr) {+ return Response.json({ err: String(permissionErr) }, { status: 403 });+ }const formData = await request.json();const metricParams: MetricParams = {timeLimit: formData.timeLimit as TimeLimit,selectedConfig: formData.selectedConfig || {},- databaseConfigId,+ ...(typeof formData.sourceId === "string" ? { sourceId: formData.sourceId } : {}),+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),};const validation = validateMetricsRequest(@@ -34,10 +60,17 @@ export async function POST();if (!validation.success) return Response.json(validation.err, { status: 400 });- return Response.json(- await getSignalSummary(- metricParams,- params.signal as "traces" | "exceptions" | "logs" | "metrics"- )- );+ try {+ return Response.json(await summaryForSignal(params.signal, metricParams));+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error);+ return Response.json(+ { err: message, code: "TELEMETRY_SOURCE_UNAVAILABLE" },+ { status: 503 }+ );+ }}++export const POST = withRouteAccess("observability.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/telemetry/trace/route.ts b/src/client/src/app/api/telemetry/trace/route.tsindex 417f2a17..5921d35f 100644--- a/src/client/src/app/api/telemetry/trace/route.ts+++ b/src/client/src/app/api/telemetry/trace/route.ts@@ -1,58 +1,70 @@import { MetricParams, TimeLimit } from "@/lib/platform/common";-import { getRequests, getRequestsConfig } from "@/lib/platform/request";+import {+ getTraceFilterConfig,+ listTraceRecords,+} from "@/lib/platform/traces/read";import {validateMetricsRequest,validateMetricsRequestType,} from "@/helpers/server/platform";-import { resolveDbConfigId } from "@/helpers/server/auth";+import { withRouteAccess } from "@/lib/access/route-access";+import { getRequestEnvironment } from "@/constants/openlit-context";-export async function POST(request: Request) {- try {- const [authErr, databaseConfigId] = await resolveDbConfigId(request);- if (authErr) {- return Response.json({ err: authErr }, { status: 401 });- }+async function POSTHandler(request: Request) {+ const formData = await request.json();+ const timeLimit = formData.timeLimit as TimeLimit;+ const limit = formData.limit || 10;+ const offset = formData.offset || 0;+ const selectedConfig = formData.selectedConfig || {};+ const sorting = formData.sorting || {};- const formData = await request.json();- const timeLimit = formData.timeLimit as TimeLimit;- const limit = formData.limit || 10;- const offset = formData.offset || 0;- const selectedConfig = formData.selectedConfig || {};- const sorting = formData.sorting || {};-- const params: MetricParams = {- timeLimit,- limit,- offset,- selectedConfig,- sorting,- databaseConfigId,- };-- const validationParam = validateMetricsRequest(- params,- validateMetricsRequestType.GET_ALL- );+ const params: MetricParams = {+ timeLimit,+ limit,+ offset,+ selectedConfig,+ sorting,+ ...(typeof formData.sourceId === "string" ? { sourceId: formData.sourceId } : {}),+ environment:+ typeof formData.environment === "string"+ ? formData.environment+ : getRequestEnvironment(request),+ };- if (!validationParam.success)- return Response.json(validationParam.err, {- status: 400,- });+ const validationParam = validateMetricsRequest(+ params,+ validateMetricsRequestType.GET_ALL+ );- const res: any = await getRequests(params);+ if (!validationParam.success)+ return Response.json(validationParam.err, {+ status: 400,+ });++ try {+ const res: any = await listTraceRecords(params);const { searchParams } = new URL(request.url);- const includeFilters = (searchParams.get("includeFilters") === "true") || (formData.includeFilters === true);+ const includeFilters =+ searchParams.get("includeFilters") === "true" ||+ formData.includeFilters === true;if (includeFilters && !res.err) {- const configRes = await getRequestsConfig(params);+ const configRes = await getTraceFilterConfig(params);res.pagination = { limit, offset, total: res.total || 0 };res.filters = (configRes.data as any)?.[0] || {};}return Response.json(res);} catch (error: any) {- console.error("Error in telemetry trace route:", error);- return Response.json({ err: error.message || "Internal Server Error" }, { status: 500 });+ const message = error instanceof Error ? error.message : String(error);+ return Response.json(+ { err: message, code: "TELEMETRY_SOURCE_UNAVAILABLE" },+ { status: 503 }+ );}}++export const POST = withRouteAccess("traces.read", POSTHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/telemetry/trace/span/[id]/heirarchy/route.ts b/src/client/src/app/api/telemetry/trace/span/[id]/heirarchy/route.tsindex e760488c..20c953f5 100644--- a/src/client/src/app/api/telemetry/trace/span/[id]/heirarchy/route.ts+++ b/src/client/src/app/api/telemetry/trace/span/[id]/heirarchy/route.ts@@ -1,7 +1,8 @@import { getHeirarchyViaSpanId } from "@/lib/platform/request";import { resolveDbConfigId } from "@/helpers/server/auth";+import { withRouteAccess } from "@/lib/access/route-access";-export async function GET(request: Request, context: any) {+async function GETHandler(request: Request, context: any) {const [authErr, databaseConfigId] = await resolveDbConfigId(request);if (authErr) {return Response.json({ err: authErr }, { status: 401 });@@ -18,3 +19,7 @@ export async function GET(request: Request, context: any) {const res: any = await getHeirarchyViaSpanId(id, databaseConfigId);return Response.json(res);}++export const GET = withRouteAccess("traces.read", GETHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/app/api/telemetry/trace/span/[id]/route.ts b/src/client/src/app/api/telemetry/trace/span/[id]/route.tsindex a63cace0..3e8cf00a 100644--- a/src/client/src/app/api/telemetry/trace/span/[id]/route.ts+++ b/src/client/src/app/api/telemetry/trace/span/[id]/route.ts@@ -1,13 +1,9 @@-import { getRequestViaSpanId } from "@/lib/platform/request";+import { getTraceSpanRecord } from "@/lib/platform/traces/read";import { getEvaluationSummaryForSpanId } from "@/lib/platform/evaluation";-import { resolveDbConfigId } from "@/helpers/server/auth";--export async function GET(request: Request, context: any) {- const [authErr, databaseConfigId] = await resolveDbConfigId(request);- if (authErr) {- return Response.json({ err: authErr }, { status: 401 });- }+import { consoleLog } from "@/utils/log";+import { withRouteAccess } from "@/lib/access/route-access";+async function GETHandler(request: Request, context: any) {const { id } = context.params || {};if (!id)@@ -15,14 +11,37 @@ export async function GET(request: Request, context: any) {status: 400,});- const [spanRes, evalSummary] = await Promise.all([- getRequestViaSpanId(id, databaseConfigId),- getEvaluationSummaryForSpanId(id, databaseConfigId),- ]);+ const traceId = new URL(request.url).searchParams.get("traceId") || undefined;+ const environment = request.headers.get("x-openlit-environment") || undefined;+ const startedAt = Date.now();+ consoleLog("[api] span detail request", {+ spanId: id,+ traceId: traceId || null,+ urlHasTraceId: !!traceId,+ });++ const spanRes = await getTraceSpanRecord(id, { traceId, environment });+ let evalSummary = null;+ if (spanRes.record) {+ evalSummary = await Promise.race([+ getEvaluationSummaryForSpanId(id).catch(() => null),+ new Promise<null>((resolve) => setTimeout(() => resolve(null), 750)),+ ]);+ }const res: any = { ...spanRes };if (evalSummary && evalSummary.runCount > 0) {res.evaluationSummary = evalSummary;}+ consoleLog("[api] span detail response", {+ spanId: id,+ traceId: traceId || null,+ found: !!spanRes.record,+ error: spanRes.err || null,+ evaluationSummary: !!res.evaluationSummary,+ elapsedMs: Date.now() - startedAt,+ });return Response.json(res);}++export const GET = withRouteAccess("traces.read", GETHandler, { requireDbConfig: true });diff --git a/src/client/src/app/api/telemetry/trace/trace/[id]/route.ts b/src/client/src/app/api/telemetry/trace/trace/[id]/route.tsindex e8433504..b5b3fa80 100644--- a/src/client/src/app/api/telemetry/trace/trace/[id]/route.ts+++ b/src/client/src/app/api/telemetry/trace/trace/[id]/route.ts@@ -1,7 +1,8 @@import { getRequestViaTraceId } from "@/lib/platform/request";import { resolveDbConfigId } from "@/helpers/server/auth";+import { withRouteAccess } from "@/lib/access/route-access";-export async function GET(request: Request, context: any) {+async function GETHandler(request: Request, context: any) {const [authErr, databaseConfigId] = await resolveDbConfigId(request);if (authErr) {return Response.json({ err: authErr }, { status: 401 });@@ -17,3 +18,7 @@ export async function GET(request: Request, context: any) {const res: any = await getRequestViaTraceId(id, databaseConfigId);return Response.json(res);}++export const GET = withRouteAccess("traces.read", GETHandler, {+ requireDbConfig: true,+});diff --git a/src/client/src/clickhouse/migrations/alter-telemetry-rollups-dimensions-migration.ts b/src/client/src/clickhouse/migrations/alter-telemetry-rollups-dimensions-migration.tsnew file mode 100644index 00000000..bbf37bbc--- /dev/null+++ b/src/client/src/clickhouse/migrations/alter-telemetry-rollups-dimensions-migration.ts@@ -0,0 +1,60 @@+import migrationHelper from "./migration-helper";+import {+ LLM_ROLLUPS_TABLE,+ SIGNAL_BUCKETS_TABLE,+ SPAN_HOT_CACHE_TABLE,+} from "@/lib/platform/telemetry/rollups";++const MIGRATION_ID = "alter-telemetry-rollups-dimensions-and-hot-cache";++/**+ * Adds service/environment (and model/provider on LLM rollups) so scoped+ * agent/dashboard reads can use L2, plus a bounded recent-span hot cache.+ */+export default async function AlterTelemetryRollupsDimensionsMigration(+ databaseConfigId?: string+) {+ const queries = [+ `+ ALTER TABLE ${SIGNAL_BUCKETS_TABLE}+ ADD COLUMN IF NOT EXISTS service LowCardinality(String) DEFAULT '',+ ADD COLUMN IF NOT EXISTS environment LowCardinality(String) DEFAULT ''+ `,+ `+ ALTER TABLE ${LLM_ROLLUPS_TABLE}+ ADD COLUMN IF NOT EXISTS service LowCardinality(String) DEFAULT '',+ ADD COLUMN IF NOT EXISTS environment LowCardinality(String) DEFAULT '',+ ADD COLUMN IF NOT EXISTS model LowCardinality(String) DEFAULT '',+ ADD COLUMN IF NOT EXISTS provider LowCardinality(String) DEFAULT ''+ `,+ `+ CREATE TABLE IF NOT EXISTS ${SPAN_HOT_CACHE_TABLE} (+ source_id String,+ trace_id String,+ span_id String,+ parent_span_id String DEFAULT '',+ name String DEFAULT '',+ service_name LowCardinality(String) DEFAULT '',+ environment LowCardinality(String) DEFAULT '',+ timestamp DateTime64(3),+ duration_ns UInt64 DEFAULT 0,+ status_code LowCardinality(String) DEFAULT '',+ status_message String DEFAULT '',+ span_kind LowCardinality(String) DEFAULT '',+ span_attributes String DEFAULT '{}',+ resource_attributes String DEFAULT '{}',+ cost Float64 DEFAULT 0,+ updated_at DateTime DEFAULT now()+ ) ENGINE = ReplacingMergeTree(updated_at)+ ORDER BY (source_id, service_name, timestamp, trace_id, span_id)+ TTL toDateTime(timestamp) + INTERVAL 2 HOUR DELETE+ SETTINGS index_granularity = 8192+ `,+ ];++ return migrationHelper({+ clickhouseMigrationId: MIGRATION_ID,+ databaseConfigId,+ queries,+ });+}diff --git a/src/client/src/clickhouse/migrations/create-providers-migration.ts b/src/client/src/clickhouse/migrations/create-providers-migration.tsindex 381ef9c8..0d5202d3 100644--- a/src/client/src/clickhouse/migrations/create-providers-migration.ts+++ b/src/client/src/clickhouse/migrations/create-providers-migration.ts@@ -108,6 +108,7 @@ export default async function CreateProvidersMigration(databaseConfigId?: string}// 3. Copy existing data from legacy openground_configs -> openlit_providers+ // dataCollector returns { err } instead of throwing for missing tables.try {const copyConfigsQuery = `INSERT INTO ${OPENLIT_PROVIDERS_TABLE_NAME}@@ -115,7 +116,14 @@ export default async function CreateProvidersMigration(databaseConfigId?: stringSELECT id, user_id, provider, vault_id, model_id, is_active, created_at, updated_atFROM ${OPENLIT_OPENGROUND_CONFIG_TABLE_NAME}`;- await dataCollector({ query: copyConfigsQuery }, "exec", dbConfig.id);+ const copyConfigs = await dataCollector(+ { query: copyConfigsQuery },+ "exec",+ dbConfig.id+ );+ if (copyConfigs?.err) {+ consoleLog("Legacy openground_configs not found or empty — skipping copy");+ }} catch (e) {consoleLog("Legacy openground_configs not found or empty — skipping copy");}@@ -136,7 +144,14 @@ export default async function CreateProvidersMigration(databaseConfigId?: stringcreated_by_user_id, created_at, updated_atFROM ${OPENLIT_OPENGROUND_CUSTOM_MODELS_TABLE_NAME}`;- await dataCollector({ query: copyModelsQuery }, "exec", dbConfig.id);+ const copyModels = await dataCollector(+ { query: copyModelsQuery },+ "exec",+ dbConfig.id+ );+ if (copyModels?.err) {+ consoleLog("Legacy openground_custom_models not found or empty — skipping copy");+ }} catch (e) {consoleLog("Legacy openground_custom_models not found or empty — skipping copy");}diff --git a/src/client/src/clickhouse/migrations/create-telemetry-rollups-migration.ts b/src/client/src/clickhouse/migrations/create-telemetry-rollups-migration.tsnew file mode 100644index 00000000..08e0edd2--- /dev/null+++ b/src/client/src/clickhouse/migrations/create-telemetry-rollups-migration.ts@@ -0,0 +1,81 @@+import migrationHelper from "./migration-helper";+import {+ LLM_ROLLUPS_TABLE,+ SIGNAL_BUCKETS_TABLE,+ SPAN_HOT_CACHE_TABLE,+} from "@/lib/platform/telemetry/rollups";++const MIGRATION_ID = "create-telemetry-rollups-tables";++export default async function CreateTelemetryRollupsMigration(+ databaseConfigId?: string+) {+ const queries = [+ `+ CREATE TABLE IF NOT EXISTS ${SIGNAL_BUCKETS_TABLE} (+ source_id String,+ service LowCardinality(String) DEFAULT '',+ environment LowCardinality(String) DEFAULT '',+ bucket_start DateTime,+ request_count UInt64 DEFAULT 0,+ avg_duration_seconds Float64 DEFAULT 0,+ total_cost Float64 DEFAULT 0,+ total_tokens Float64 DEFAULT 0,+ updated_at DateTime DEFAULT now()+ ) ENGINE = ReplacingMergeTree(updated_at)+ ORDER BY (source_id, service, environment, bucket_start)+ TTL bucket_start + INTERVAL 90 DAY DELETE+ SETTINGS index_granularity = 8192;+ `,+ `+ CREATE TABLE IF NOT EXISTS ${LLM_ROLLUPS_TABLE} (+ source_id String,+ dimension LowCardinality(String),+ group_value String,+ service LowCardinality(String) DEFAULT '',+ environment LowCardinality(String) DEFAULT '',+ model LowCardinality(String) DEFAULT '',+ provider LowCardinality(String) DEFAULT '',+ window_start DateTime,+ window_end DateTime,+ request_count UInt64 DEFAULT 0,+ total_cost Float64 DEFAULT 0,+ total_tokens Float64 DEFAULT 0,+ avg_duration_seconds Float64 DEFAULT 0,+ updated_at DateTime DEFAULT now()+ ) ENGINE = ReplacingMergeTree(updated_at)+ ORDER BY (source_id, dimension, service, environment, group_value, window_start, window_end)+ TTL window_end + INTERVAL 90 DAY DELETE+ SETTINGS index_granularity = 8192;+ `,+ `+ CREATE TABLE IF NOT EXISTS ${SPAN_HOT_CACHE_TABLE} (+ source_id String,+ trace_id String,+ span_id String,+ parent_span_id String DEFAULT '',+ name String DEFAULT '',+ service_name LowCardinality(String) DEFAULT '',+ environment LowCardinality(String) DEFAULT '',+ timestamp DateTime64(3),+ duration_ns UInt64 DEFAULT 0,+ status_code LowCardinality(String) DEFAULT '',+ status_message String DEFAULT '',+ span_kind LowCardinality(String) DEFAULT '',+ span_attributes String DEFAULT '{}',+ resource_attributes String DEFAULT '{}',+ cost Float64 DEFAULT 0,+ updated_at DateTime DEFAULT now()+ ) ENGINE = ReplacingMergeTree(updated_at)+ ORDER BY (source_id, service_name, timestamp, trace_id, span_id)+ TTL toDateTime(timestamp) + INTERVAL 2 HOUR DELETE+ SETTINGS index_granularity = 8192;+ `,+ ];++ return migrationHelper({+ clickhouseMigrationId: MIGRATION_ID,+ databaseConfigId,+ queries,+ });+}diff --git a/src/client/src/clickhouse/migrations/index.ts b/src/client/src/clickhouse/migrations/index.tsindex 3dc1e38f..d47f3497 100644--- a/src/client/src/clickhouse/migrations/index.ts+++ b/src/client/src/clickhouse/migrations/index.ts@@ -34,6 +34,8 @@ import AddCodingAgentLOCSummaryFieldsMigration from "./add-coding-agent-loc-summimport CreateCodingAgentsAuditMigration from "./create-coding-agents-audit-migration";import DropVcsMigration from "./drop-vcs-migration";import AddProviderModelsCachePricesMigration from "./add-provider-models-cache-prices-migration";+import CreateTelemetryRollupsMigration from "./create-telemetry-rollups-migration";+import AlterTelemetryRollupsDimensionsMigration from "./alter-telemetry-rollups-dimensions-migration";export default async function migrations(databaseConfigId?: string) {// Group 1: Independent table creations (safe to parallel)@@ -97,6 +99,9 @@ export default async function migrations(databaseConfigId?: string) {// the dependency explicit.await AddCodingAgentLOCSummaryFieldsMigration(databaseConfigId);+ await CreateTelemetryRollupsMigration(databaseConfigId);+ await AlterTelemetryRollupsDimensionsMigration(databaseConfigId);+// Group 7: Drop the never-populated v2 GitHub App VCS tables that// earlier deployments created via the now-removed// `create-vcs-migration`. Runs last so stale deployments still getdiff --git a/src/client/src/components/(playground)/agents/agent-dag.tsx b/src/client/src/components/(playground)/agents/agent-dag.tsxindex b65d1619..8de12573 100644--- a/src/client/src/components/(playground)/agents/agent-dag.tsx+++ b/src/client/src/components/(playground)/agents/agent-dag.tsx@@ -2,6 +2,7 @@import { useEffect, useState } from "react";import getMessage from "@/constants/messages";+import { getData } from "@/utils/api";import DagCanvas, {type DagEdgeInput,type DagNodeInput,@@ -39,10 +40,18 @@ export default function AgentDag({ agentKey, versionHash }: AgentDagProps) {const params = versionHash? `?versionHash=${encodeURIComponent(versionHash)}`: "";- const res = await fetch(`/api/agents/${agentKey}/graph${params}`);- if (!res.ok) throw new Error(`HTTP ${res.status}`);- const body = await res.json();+ const body = (await getData({+ url: `/api/agents/${agentKey}/graph${params}`,+ method: "GET",+ })) as {+ data?: { graph?: AggregateGraph };+ err?: string;+ error?: string;+ };if (cancelled) return;+ if (body?.err || body?.error) {+ throw new Error(String(body.err || body.error));+ }setGraph((body.data?.graph as AggregateGraph) || null);} catch (e) {if (!cancelled) setError(String(e));diff --git a/src/client/src/components/(playground)/agents/agent-monitoring-tab.tsx b/src/client/src/components/(playground)/agents/agent-monitoring-tab.tsxindex c9cc0898..8752d2dc 100644--- a/src/client/src/components/(playground)/agents/agent-monitoring-tab.tsx+++ b/src/client/src/components/(playground)/agents/agent-monitoring-tab.tsx@@ -1,8 +1,72 @@"use client";+import { useState } from "react";import ObservabilitySignalList from "@/components/(playground)/observability/signal-list";-import { getSignalConfig } from "@/components/(playground)/observability/registry";+import {+ OBSERVABILITY_SIGNALS,+ getSignalConfig,+} from "@/components/(playground)/observability/registry";+import { prepareObservabilitySignalChange } from "@/helpers/client/observability";+import { getFilterDetails, getUpdateConfig, getUpdateFilter } from "@/selectors/filter";+import { useRootStore } from "@/store";+/**+ * Agent Monitoring tab. Mirrors the Telemetry page's multi-signal browser+ * (Traces / Exceptions / Metrics / Logs) but stays scoped to the current+ * agent: the surrounding `AgentScopeProvider` locks `selectedConfig`+ * (serviceNames / environments / versionFilter), so every signal list here is+ * automatically filtered to this agent's service. Coding-only signals are+ * hidden — they live under the coding-agent hub.+ */export default function AgentMonitoringTab() {- return <ObservabilitySignalList config={getSignalConfig("traces")} />;+ const updateConfig = useRootStore(getUpdateConfig);+ const updateFilter = useRootStore(getUpdateFilter);+ const filter = useRootStore(getFilterDetails);+ const [activeKey, setActiveKey] = useState("traces");+ const activeConfig = getSignalConfig(activeKey);++ const signals = OBSERVABILITY_SIGNALS.filter(+ (signal) => signal.key !== "sessions" && signal.key !== "coding_users"+ );++ const onTabChange = (key: string) => {+ if (key === activeKey) return;+ // Reset sort / groupBy / pagination so a trace column can't leak into+ // the metrics or logs query. Preserve the agent scope lock — wiping+ // serviceNames unmounts the list via AgentScopeProvider and remounts+ // into an infinite update loop.+ prepareObservabilitySignalChange(updateConfig, updateFilter, undefined, {+ serviceNames: filter.selectedConfig?.serviceNames,+ services: filter.selectedConfig?.services,+ environments: filter.selectedConfig?.environments,+ versionFilter: filter.selectedConfig?.versionFilter,+ });+ setActiveKey(key);+ };++ return (+ <div className="flex flex-col gap-3">+ <div className="grid grid-cols-2 gap-2 sm:flex sm:flex-wrap">+ {signals.map((signal) => {+ const Icon = signal.icon;+ const isActive = signal.key === activeConfig.key;+ return (+ <button+ key={signal.key}+ onClick={() => onTabChange(signal.key)}+ className={`inline-flex items-center gap-2 rounded-md border px-2.5 py-1.5 text-xs transition ${+ isActive+ ? signal.tone+ : "border-stone-200 bg-stone-50 text-stone-600 hover:bg-stone-100 dark:border-stone-800 dark:bg-stone-900 dark:text-stone-300 dark:hover:bg-stone-800"+ }`}+ >+ <Icon className="h-4 w-4" />+ <span className="font-medium">{signal.label}</span>+ </button>+ );+ })}+ </div>+ <ObservabilitySignalList key={activeConfig.key} config={activeConfig} />+ </div>+ );}diff --git a/src/client/src/components/(playground)/agents/agent-scope-provider.tsx b/src/client/src/components/(playground)/agents/agent-scope-provider.tsxindex c5611ed5..458102d6 100644--- a/src/client/src/components/(playground)/agents/agent-scope-provider.tsx+++ b/src/client/src/components/(playground)/agents/agent-scope-provider.tsx@@ -1,9 +1,29 @@"use client";-import { useEffect, useRef } from "react";+import {+ createContext,+ useContext,+ useLayoutEffect,+ useMemo,+ useRef,+ useState,+} from "react";import { useRootStore } from "@/store";import { getUpdateFilter, getFilterDetails } from "@/selectors/filter";import type { VersionFilter } from "@/types/store/filter";+import { Loader2 } from "lucide-react";++/**+ * True for any subtree rendered inside an agent-detail scope lock. The shared+ * observability surfaces (Telemetry list/summary) read this to tell an+ * intentional per-agent `serviceNames` scope apart from a stale lock leaking+ * into the global page, and strip the latter before querying.+ */+const AgentScopeContext = createContext(false);++export function useIsAgentScoped(): boolean {+ return useContext(AgentScopeContext);+}interface AgentScopeProviderProps {serviceName: string;@@ -20,6 +40,7 @@ interface AgentScopeProviderProps {interface PreviousScope {serviceNames: string[];+ services: string[];applicationNames: string[];environments: string[];versionFilter?: VersionFilter;@@ -54,6 +75,10 @@ function versionFilterEqual(* (e.g. the user switches time range, which the store resets* `selectedConfig` for). Restores the previous scope on unmount so* navigating away doesn't leak the lock.+ *+ * Children are gated behind `scopeReady` so widget effects cannot fire an+ * unscoped request before the lock is applied (React runs child effects+ * before parent effects).*/export default function AgentScopeProvider({serviceName,@@ -64,6 +89,7 @@ export default function AgentScopeProvider({const updateFilter = useRootStore(getUpdateFilter);const filterDetails = useRootStore(getFilterDetails);const previousScopeRef = useRef<PreviousScope | null>(null);+ const [lockApplied, setLockApplied] = useState(false);const currentServiceNames =filterDetails.selectedConfig?.serviceNames;@@ -74,13 +100,28 @@ export default function AgentScopeProvider({const currentVersionFilter =filterDetails.selectedConfig?.versionFilter;- useEffect(() => {+ // Capture-on-mount / restore-on-unmount. This MUST be a layout effect: its+ // cleanup runs synchronously during the unmount commit, before the next+ // route's passive effects fire. A plain `useEffect` cleanup is a passive+ // effect that can run *after* the destination page (e.g. Telemetry) has+ // already issued its first request, leaking the `serviceNames` lock into a+ // global query and collapsing the list to this one agent's service.+ useLayoutEffect(() => {if (previousScopeRef.current === null) {+ // COPY the arrays — never hold the live store reference. The store's+ // `updateFilter` merges via lodash `merge`, which mutates arrays in+ // place; since an empty array is truthy, `serviceNames || []` would+ // otherwise capture the store's own array and the subsequent lock+ // apply would mutate this "previous" snapshot into the agent's+ // service. Restore would then write the agent scope back instead of+ // clearing it — a leak that compounds on every agent visit.previousScopeRef.current = {- serviceNames: filterDetails.selectedConfig?.serviceNames || [],- applicationNames:- filterDetails.selectedConfig?.applicationNames || [],- environments: filterDetails.selectedConfig?.environments || [],+ serviceNames: [...(filterDetails.selectedConfig?.serviceNames || [])],+ services: [...(filterDetails.selectedConfig?.services || [])],+ applicationNames: [+ ...(filterDetails.selectedConfig?.applicationNames || []),+ ],+ environments: [...(filterDetails.selectedConfig?.environments || [])],versionFilter: filterDetails.selectedConfig?.versionFilter,};}@@ -89,6 +130,7 @@ export default function AgentScopeProvider({const prev = previousScopeRef.current;if (!prev) return;updateFilter("selectedConfig.serviceNames", prev.serviceNames);+ updateFilter("selectedConfig.services", prev.services);updateFilter("selectedConfig.applicationNames", prev.applicationNames);if (environment) {updateFilter("selectedConfig.environments", prev.environments);@@ -103,19 +145,29 @@ export default function AgentScopeProvider({// eslint-disable-next-line react-hooks/exhaustive-deps}, []);- useEffect(() => {+ // Apply the lock synchronously before child paint/effects.+ useLayoutEffect(() => {const target = [serviceName];if (!arraysEqual(currentServiceNames, target)) {updateFilter("selectedConfig.serviceNames", target);}+ // Logs/metrics filter UI + builders historically key off `services`.+ // Mirror the agent lock there so Monitoring stays scoped for all signals.+ const currentServices = filterDetails.selectedConfig?.services;+ if (!arraysEqual(currentServices, target)) {+ updateFilter("selectedConfig.services", target);+ }if ((currentApplicationNames?.length || 0) > 0) {updateFilter("selectedConfig.applicationNames", []);}- if (environment) {+ if (environment && environment !== "default") {const envTarget = [environment];if (!arraysEqual(currentEnvironments, envTarget)) {updateFilter("selectedConfig.environments", envTarget);}+ } else if ((currentEnvironments?.length || 0) > 0) {+ // Don't lock the synthetic "default" env — it empties Loki/Tempo/Prom.+ updateFilter("selectedConfig.environments", []);}if (!versionFilterEqual(currentVersionFilter, versionFilter)) {updateFilter(@@ -123,6 +175,7 @@ export default function AgentScopeProvider({versionFilter || undefined);}+ setLockApplied(true);}, [serviceName,environment,@@ -131,8 +184,52 @@ export default function AgentScopeProvider({currentApplicationNames,currentEnvironments,currentVersionFilter,+ filterDetails.selectedConfig?.services,updateFilter,]);- return <>{children}</>;+ const scopeReady = useMemo(() => {+ if (!lockApplied) return false;+ if (!arraysEqual(currentServiceNames, [serviceName])) return false;+ if (+ environment &&+ environment !== "default" &&+ !arraysEqual(currentEnvironments, [environment])+ ) {+ return false;+ }+ if (!versionFilterEqual(currentVersionFilter, versionFilter)) return false;+ return true;+ }, [+ lockApplied,+ currentServiceNames,+ currentEnvironments,+ currentVersionFilter,+ serviceName,+ environment,+ versionFilter,+ ]);++ // Once children have rendered under a valid lock, keep them mounted even if+ // a transient wipe briefly clears serviceNames. Unmounting on mismatch+ // remounts ObservabilitySignalList, which clears filters again and loops.+ const [childrenUnlocked, setChildrenUnlocked] = useState(false);+ useLayoutEffect(() => {+ if (scopeReady) setChildrenUnlocked(true);+ }, [scopeReady]);++ if (!lockApplied || (!scopeReady && !childrenUnlocked)) {+ return (+ <div className="flex items-center justify-center py-12 text-sm text-stone-500 dark:text-stone-400 gap-2">+ <Loader2 className="w-4 h-4 animate-spin" />+ Loading agent scope…+ </div>+ );+ }++ return (+ <AgentScopeContext.Provider value={true}>+ {children}+ </AgentScopeContext.Provider>+ );}diff --git a/src/client/src/components/(playground)/chat/chat-floating-button.tsx b/src/client/src/components/(playground)/chat/chat-floating-button.tsxindex 2eeb48ea..2c32c722 100644--- a/src/client/src/components/(playground)/chat/chat-floating-button.tsx+++ b/src/client/src/components/(playground)/chat/chat-floating-button.tsx@@ -59,3 +59,29 @@ export default function ChatFloatingButton() {</div>);}++export function ChatHeaderButton() {+ const tooltip = useMemo(+ () => OTTER_TOOLTIPS[Math.floor(Math.random() * OTTER_TOOLTIPS.length)],+ []+ );++ return (+ <Tooltip>+ <TooltipTrigger asChild>+ <span className="relative isolate inline-flex overflow-hidden rounded-md p-px shadow-sm transition-shadow hover:shadow-primary/25">+ <span className="absolute inset-[-250%] animate-[spin_4s_linear_infinite] bg-[conic-gradient(from_90deg_at_50%_50%,#f97316_0deg,#ec4899_90deg,#8b5cf6_180deg,#06b6d4_270deg,#f97316_360deg)]" />+ <Link+ href="/chat"+ aria-label="Ask Otter"+ className="group relative flex h-8 items-center gap-1.5 rounded-[5px] bg-white px-2.5 text-xs font-semibold text-stone-800 transition-colors hover:bg-stone-50 dark:bg-stone-950 dark:text-stone-100 dark:hover:bg-stone-900"+ >+ <Otter className="h-5 w-5 transition-transform group-hover:rotate-6" />+ <span>Ask Otter</span>+ </Link>+ </span>+ </TooltipTrigger>+ <TooltipContent side="bottom" className="max-w-xs text-xs"><p>{tooltip}</p></TooltipContent>+ </Tooltip>+ );+}diff --git a/src/client/src/components/(playground)/chat/chat-panel.tsx b/src/client/src/components/(playground)/chat/chat-panel.tsxindex e2931885..7fcb8768 100644--- a/src/client/src/components/(playground)/chat/chat-panel.tsx+++ b/src/client/src/components/(playground)/chat/chat-panel.tsx@@ -3,6 +3,7 @@import { useCallback, useEffect, useRef } from "react";import { usePostHog } from "posthog-js/react";import getMessage from "@/constants/messages";+import { getRequestHeaders } from "@/utils/api";import { CLIENT_EVENTS } from "@/constants/events";import { useRootStore } from "@/store";import {@@ -187,7 +188,7 @@ export default function ChatPanel({const res = await fetch("/api/chat/message", {method: "POST",- headers: { "Content-Type": "application/json" },+ headers: getRequestHeaders({ "Content-Type": "application/json" }),body: JSON.stringify({ conversationId: currentConvId, content }),signal: controller.signal,});diff --git a/src/client/src/components/(playground)/chat/message-bubble.tsx b/src/client/src/components/(playground)/chat/message-bubble.tsxindex d94de613..0cb39581 100644--- a/src/client/src/components/(playground)/chat/message-bubble.tsx+++ b/src/client/src/components/(playground)/chat/message-bubble.tsx@@ -8,6 +8,7 @@ import DashboardImportCard from "./dashboard-import-card";import getMessage from "@/constants/messages";import { CheckCircle2, Circle, Coins, Clock, Database, Loader2, User, Bot } from "lucide-react";import { useRequest } from "@/components/(playground)/request/request-context";+import { getRequestHeaders } from "@/utils/api";interface EntityLink {type: string;@@ -213,7 +214,9 @@ function TraceReferencePills({ refs }: { refs: TraceReference[] }) {return;}try {- const res = await fetch(`/api/telemetry/trace/trace/${ref.id}`);+ const res = await fetch(`/api/telemetry/request/trace/${ref.id}`, {+ headers: getRequestHeaders(),+ });const result = await res.json();const record = result?.record || result?.data?.record || result?.data;const spanId = record?.SpanId || record?.spanId;diff --git a/src/client/src/components/(playground)/chat/result-display.tsx b/src/client/src/components/(playground)/chat/result-display.tsxindex 240f5fff..8d612f57 100644--- a/src/client/src/components/(playground)/chat/result-display.tsx+++ b/src/client/src/components/(playground)/chat/result-display.tsx@@ -122,9 +122,11 @@ export default function ResultDisplay({ data, stats, query }: ResultDisplayProps{VIZ_OPTIONS.map(({ type, icon: Icon, label }) => (<Buttonkey={type}- variant={vizType === type ? "default" : "ghost"}+ variant="ghost"size="sm"- className="h-7 px-2 text-xs"+ className={vizType === type+ ? "h-7 bg-stone-900 px-2 text-xs text-white hover:bg-stone-800 hover:text-white dark:bg-stone-100 dark:text-stone-900 dark:hover:bg-stone-200 dark:hover:text-stone-900"+ : "h-7 px-2 text-xs text-stone-700 hover:bg-stone-100 dark:text-stone-300 dark:hover:bg-stone-800"}onClick={() => setVizType(type)}><Icon className="h-3 w-3 mr-1" />@@ -142,7 +144,7 @@ export default function ResultDisplay({ data, stats, query }: ResultDisplayProps<Buttonvariant="outline"size="sm"- className="h-7 px-2 text-xs border-stone-200 dark:border-stone-700"+ className="h-7 border-stone-200 px-2 text-xs text-stone-700 dark:border-stone-700 dark:text-stone-200"onClick={() => setShowSaveDialog(true)}><Save className="h-3 w-3 mr-1" />diff --git a/src/client/src/components/(playground)/chat/sql-block.tsx b/src/client/src/components/(playground)/chat/sql-block.tsxindex 3f1bef6a..c9738100 100644--- a/src/client/src/components/(playground)/chat/sql-block.tsx+++ b/src/client/src/components/(playground)/chat/sql-block.tsx@@ -70,12 +70,12 @@ export default function SqlBlock({}, [code, messageId, onExecute]);return (- <div className="my-3 rounded-lg border border-stone-200 dark:border-stone-700 overflow-hidden bg-white dark:bg-stone-900">+ <div className="my-3 overflow-hidden rounded-lg border border-stone-200 bg-white dark:border-stone-700 dark:bg-stone-950">{/* SQL Query in Accordion — collapsed by default */}<Accordion type="single" collapsible><AccordionItem value="sql" className="border-b-0">- <div className="flex items-center justify-between px-3 py-1.5 bg-stone-50 dark:bg-stone-800/50">- <AccordionTrigger className="py-1.5 hover:no-underline gap-2 text-xs font-medium text-stone-500 dark:text-stone-400">+ <div className="flex items-center justify-between bg-stone-50 px-3 py-1.5 dark:bg-stone-900">+ <AccordionTrigger className="gap-2 py-1.5 text-xs font-medium text-stone-600 hover:no-underline dark:text-stone-300"><div className="flex items-center gap-1.5"><Code className="h-3.5 w-3.5" />{getMessage().CHAT_SQL_LABEL}@@ -85,7 +85,7 @@ export default function SqlBlock({<Buttonvariant="ghost"size="sm"- className="h-6 px-2 text-xs text-stone-500 dark:text-stone-400"+ className="h-7 px-2 text-xs text-stone-600 dark:text-stone-300"onClick={handleCopy}>{copied ? (@@ -116,13 +116,13 @@ export default function SqlBlock({<AccordionContent className="pb-0" parentClassName="border-t border-stone-200 dark:border-stone-700"><SyntaxHighlighterlanguage="sql"- style={resolvedTheme === "dark" ? oneDark : oneLight}- customStyle={{+ style={resolvedTheme === "light" ? oneLight : oneDark}+ customStyle={{margin: 0,borderRadius: 0,fontSize: "13px",padding: "12px 16px",- background: "transparent",+ background: "#1c1917",}}>{code}diff --git a/src/client/src/components/(playground)/clickhouse-connectivity-wrapper.tsx b/src/client/src/components/(playground)/clickhouse-connectivity-wrapper.tsxindex 812ae059..ba96c8bd 100644--- a/src/client/src/components/(playground)/clickhouse-connectivity-wrapper.tsx+++ b/src/client/src/components/(playground)/clickhouse-connectivity-wrapper.tsx@@ -2,11 +2,10 @@import { pingActiveDatabaseConfig } from "@/helpers/client/database-config";import { getPingDetails } from "@/selectors/database-config";import { useRootStore } from "@/store";-import Image from "next/image";import Link from "next/link";import { usePathname } from "next/navigation";import { useEffect } from "react";-import Loader from "../common/loader";+import { DatabaseZap, ExternalLink, RefreshCw } from "lucide-react";const ALLOWED_CONNECTIVITY_ALERT = /^\/home$|^\/dashboard$|^\/telemetry(?:\/.*)?$|^\/observability(?:\/.*)?$|^\/requests$|^\/exceptions$|^\/d\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$|^\/prompt-hub$|^\/vault$|^\/dashboards/;@@ -18,55 +17,33 @@ export default function ClickhouseConnectivityWrapper({ children }: { children:if (pingDetails.status === "pending") pingActiveDatabaseConfig();}, []);- if (!ALLOWED_CONNECTIVITY_ALERT.test(pathname)) {- return children;- }+ if (!ALLOWED_CONNECTIVITY_ALERT.test(pathname)) return children;- if (pingDetails.error) {- return (- <div className="p-4 mb-4 text-red-800 border border-red-300 rounded-md bg-red-50 dark:bg-red-950 dark:text-red-400 dark:border-red-800 w-full">- <div className="flex">- <div className="flex flex-col grow">- <h3 className="text-lg font-medium">- Looks like you've found the doorway to the great nothing- </h3>- <div className="mb-2 text-sm">- Sorry about that! Please visit settings page to configure your- active clickhouse database.- </div>- <div className="mb-2 text-sm text-red-500">- {pingDetails.error}- </div>- <Link- href="/organisation"- className="inline-flex my-2 border rounded md py-2 px-4 text-center bg-primary cursor-pointer text-white hover:bg-stone-950 outline-none self-start text-sm"- >- Take me there!+ return (+ <div className="flex min-h-0 w-full flex-1 flex-col">+ {pingDetails.error ? (+ <div className="mx-4 mt-3 flex items-start gap-3 rounded-lg border border-amber-300 bg-amber-50 px-4 py-3 text-amber-950 shadow-sm dark:border-amber-900/70 dark:bg-amber-950/30 dark:text-amber-100">+ <div className="flex h-9 w-9 shrink-0 items-center justify-center rounded-md bg-amber-100 text-amber-700 dark:bg-amber-900/50 dark:text-amber-300">+ <DatabaseZap className="h-5 w-5" />+ </div>+ <div className="min-w-0 flex-1">+ <p className="text-sm font-semibold">ClickHouse connector unavailable</p>+ <p className="mt-0.5 text-xs leading-5 text-amber-800 dark:text-amber-200/80">+ External telemetry connectors can still power observability. ClickHouse-backed evaluations and platform features will remain unavailable until a ClickHouse connector is reachable.+ </p>+ <p className="mt-1 truncate text-[11px] text-amber-700/80 dark:text-amber-300/70">{String(pingDetails.error)}</p>+ </div>+ <div className="flex shrink-0 items-center gap-2">+ <button type="button" onClick={() => void pingActiveDatabaseConfig()} className="inline-flex h-8 items-center gap-1.5 rounded-md border border-amber-300 bg-white px-2.5 text-xs font-medium text-amber-900 hover:bg-amber-100 dark:border-amber-800 dark:bg-amber-950/40 dark:text-amber-100 dark:hover:bg-amber-900/60">+ <RefreshCw className="h-3.5 w-3.5" /> Retry+ </button>+ <Link href="/connectors" className="inline-flex h-8 items-center gap-1.5 rounded-md bg-amber-700 px-2.5 text-xs font-medium text-white hover:bg-amber-800 dark:bg-amber-500 dark:text-stone-950 dark:hover:bg-amber-400">+ Manage connectors <ExternalLink className="h-3.5 w-3.5" /></Link></div>- <Image- alt="Connection failure"- src="/images/connect.svg"- className="flex-shrink-0 w-24 h-24 me-2 self-center"- height="128"- width="128"- /></div>- </div>- );- }-- if (pingDetails.status === "pending") {- return (- <div className="flex items-center justify-center h-full w-full">- <Loader />- </div>- );- }-- if (pingDetails.status === "success") {- return children;- }-- return null;+ ) : null}+ {children}+ </div>+ );}diff --git a/src/client/src/components/(playground)/coding-agents/coding-quick-filters.tsx b/src/client/src/components/(playground)/coding-agents/coding-quick-filters.tsxindex 3421c399..4e9c7e19 100644--- a/src/client/src/components/(playground)/coding-agents/coding-quick-filters.tsx+++ b/src/client/src/components/(playground)/coding-agents/coding-quick-filters.tsx@@ -6,17 +6,15 @@* deep-links and the back button round-trip cleanly with the table* state.*- * The picker's option set is fetched from- * `/api/coding-agents/users?limit=50` (vendor-scoped when the agent- * detail page pins a vendor). The list endpoint already enforces the- * privacy cohort floor, so `low_cohort` users that come back from the- * API are filtered client-side here — selecting a masked user- * wouldn't navigate anywhere useful.+ * Fetches via the same POST + OpenLIT context path as the Users tab+ * (`getData` → `/api/coding-agents/users`) so the picker shares the+ * page time window, vendor pin, and env-routed ClickHouse — a bare+ * `fetch` previously missed those headers and defaulted to a 24h GET+ * that often returned an empty set while Sessions still had rows.*- * F5 / F9 cleanup note: the previous "CodingQuickFilters" default- * export (a redundant inline filter bar) was removed; this file now- * holds only the picker that is actually rendered by- * `<CodingSessionsTab>`.+ * The list endpoint already enforces the privacy cohort floor, so+ * `low_cohort` / `unknown` rows are skipped client-side — selecting a+ * masked identity wouldn't navigate anywhere useful.*/import { useCallback, useEffect, useMemo, useState } from "react";@@ -31,6 +29,9 @@ import {DropdownMenuTrigger,} from "@/components/ui/dropdown-menu";import { ChevronDown, User, X } from "lucide-react";+import { getData } from "@/utils/api";+import { useRootStore } from "@/store";+import { getFilterDetails } from "@/selectors/filter";interface CodingUserRow {user: string;@@ -44,16 +45,22 @@ interface CodingUserPickerProps {const PICKER_LIMIT = 50;+function isSelectableUser(user: string | undefined): boolean {+ return Boolean(user) && user !== "low_cohort" && user !== "unknown";+}+export function CodingUserPicker({ vendorScope }: CodingUserPickerProps) {const router = useRouter();const pathname = usePathname();const searchParams = useSearchParams();const selectedUser = searchParams?.get("user") || "";+ const filter = useRootStore(getFilterDetails);const [open, setOpen] = useState(false);const [users, setUsers] = useState<CodingUserRow[]>([]);const [loading, setLoading] = useState(false);const [error, setError] = useState<string | null>(null);+ const [hadMaskedOnly, setHadMaskedOnly] = useState(false);const writeUserParam = useCallback((next: string | null) => {@@ -77,34 +84,39 @@ export function CodingUserPicker({ vendorScope }: CodingUserPickerProps) {(async () => {setLoading(true);setError(null);+ setHadMaskedOnly(false);try {- const url = new URL(- "/api/coding-agents/users",- typeof window !== "undefined"- ? window.location.origin- : "http://localhost"- );- url.searchParams.set("limit", String(PICKER_LIMIT));- if (vendorScope) {- url.searchParams.set("vendor", vendorScope);- }- const res = await fetch(url.toString());+ const body: Record<string, unknown> = {+ limit: PICKER_LIMIT,+ offset: 0,+ timeLimit: {+ start: filter.timeLimit?.start,+ end: filter.timeLimit?.end,+ },+ sorting: { type: "sessions", direction: "desc" },+ runFilters: {+ ...(vendorScope ? { vendor: vendorScope } : {}),+ },+ };+ const response = (await getData({+ url: "/api/coding-agents/users",+ method: "POST",+ body: JSON.stringify(body),+ })) as {+ data?: CodingUserRow[];+ records?: CodingUserRow[];+ err?: string;+ error?: string;+ };if (cancelled) return;- if (!res.ok) {- setError(`HTTP ${res.status}`);+ if (response?.err || response?.error) {+ setError(String(response.err || response.error));setUsers([]);return;}- const body = (await res.json()) as {- data?: CodingUserRow[];- records?: CodingUserRow[];- };- const rows = (body.data || body.records || []).filter(- (row) =>- row.user &&- row.user !== "low_cohort" &&- row.user !== "unknown"- );+ const raw = response.data || response.records || [];+ const rows = raw.filter((row) => isSelectableUser(row.user));+ setHadMaskedOnly(raw.length > 0 && rows.length === 0);setUsers(rows);} catch (e) {if (!cancelled) {@@ -118,13 +130,22 @@ export function CodingUserPicker({ vendorScope }: CodingUserPickerProps) {return () => {cancelled = true;};- }, [open, vendorScope]);+ }, [+ open,+ vendorScope,+ filter.timeLimit?.start,+ filter.timeLimit?.end,+ ]);const triggerLabel = useMemo(() => {if (selectedUser) return selectedUser;return "All users";}, [selectedUser]);+ const emptyLabel = hadMaskedOnly+ ? "No identifiable users in this window"+ : "No users in this window";+return (<div className="inline-flex items-center gap-1"><DropdownMenu open={open} onOpenChange={setOpen}>@@ -161,7 +182,7 @@ export function CodingUserPicker({ vendorScope }: CodingUserPickerProps) {)}{!loading && !error && users.length === 0 && (<div className="px-2 py-1.5 text-xs text-stone-500">- No users in this window+ {emptyLabel}</div>)}{users.map((row) => (diff --git a/src/client/src/components/(playground)/connectors/connectors-page.tsx b/src/client/src/components/(playground)/connectors/connectors-page.tsxnew file mode 100644index 00000000..51e3d9c5--- /dev/null+++ b/src/client/src/components/(playground)/connectors/connectors-page.tsx@@ -0,0 +1,407 @@+"use client";++import Image from "next/image";+import Link from "next/link";+import { useEffect, useMemo, useState } from "react";+import { ArrowRight, Cable, CheckCircle2, Lock, Plug, Search } from "lucide-react";+import FeatureAccess from "@/components/rbac/feature-access";+import DataSourcesPage from "@/components/(playground)/telemetry-source/data-sources-page";+import FeaturePageHeader from "@/components/(playground)/feature-page-header";+import { Badge } from "@/components/ui/badge";+import { Button } from "@/components/ui/button";+import { Skeleton } from "@/components/ui/skeleton";+import { Input } from "@/components/ui/input";+import {+ Accordion,+ AccordionContent,+ AccordionItem,+ AccordionTrigger,+} from "@/components/ui/accordion";+import getMessage from "@/constants/messages";+import { fetchProjectList, changeActiveProject } from "@/helpers/client/project";+import { getCurrentOrganisation } from "@/selectors/organisation";+import { getCurrentProject, getProjectList } from "@/selectors/project";+import { useRootStore } from "@/store";+import { isVisibleConnectorType } from "@/lib/platform/connectors/visible-types";++type ConnectorSummary = {+ id: string;+ name: string;+ type: string;+ environment?: string;+ category?: string;+ icon?: string;+};++type ConnectorType = {+ type: string;+ displayName: string;+ description?: string;+ category?: string;+ icon?: string;+ declaredSignals?: string[];+ plan?: "free" | "enterprise";+ locked?: boolean;+};++export default function ConnectorsPage() {+ const messages = getMessage();+ const currentOrg = useRootStore(getCurrentOrganisation);+ const projects = useRootStore(getProjectList) || [];+ const currentProject = useRootStore(getCurrentProject);+ const project = useMemo(+ () =>+ currentProject ||+ projects.find((item) => item.isCurrent) ||+ projects[0],+ [currentProject, projects]+ );+ const [connected, setConnected] = useState<ConnectorSummary[]>([]);+ const [types, setTypes] = useState<ConnectorType[]>([]);+ const [requestedType, setRequestedType] = useState<string | null>(null);+ const [loading, setLoading] = useState(true);+ const [loadError, setLoadError] = useState<string | null>(null);+ const [search, setSearch] = useState("");+ const connectorGroups = useMemo(() => {+ const groups = new Map<string, ConnectorType[]>();+ for (const connector of types) {+ const key = connector.category || "datasource";+ const group = groups.get(key) || [];+ group.push(connector);+ groups.set(key, group);+ }+ return Array.from(groups, ([key, connectors]) => ({+ key,+ label: key.replace(/[-_]/g, " ").replace(/\b\w/g, (letter) => letter.toUpperCase()),+ connectors,+ }));+ }, [types]);+ const filteredConnectorGroups = useMemo(() => {+ const query = search.trim().toLowerCase();+ if (!query) return connectorGroups;+ return connectorGroups+ .map((group) => ({+ ...group,+ connectors: group.connectors.filter((connector) =>+ [+ connector.displayName,+ connector.type,+ connector.category,+ connector.description,+ ...(connector.declaredSignals || []),+ ]+ .filter(Boolean)+ .some((value) => String(value).toLowerCase().includes(query))+ ),+ }))+ .filter((group) => group.connectors.length > 0);+ }, [connectorGroups, search]);++ const loadConnectors = () => {+ if (!project?.id) return;+ setLoading(true);+ setLoadError(null);+ setConnected([]);+ setTypes([]);+ Promise.all([fetch("/api/connectors"), fetch("/api/connectors/types")])+ .then(async ([connectorResponse, typeResponse]) => {+ if (!connectorResponse.ok) throw new Error("Failed to list connected connectors");+ if (!typeResponse.ok) throw new Error("Failed to list connector types");+ const [connectorData, typeData] = await Promise.all([+ connectorResponse.json(),+ typeResponse.json(),+ ]);+ setConnected((connectorData.connectors || []).filter((connector: ConnectorSummary) => isVisibleConnectorType(connector.type)));+ setTypes((typeData.types || []).filter((type: ConnectorType) => isVisibleConnectorType(type.type)));+ })+ .catch((error: unknown) => setLoadError(error instanceof Error ? error.message : messages.DATA_SOURCE_LOAD_FAILED))+ .finally(() => setLoading(false));+ };++ useEffect(() => {+ if (currentOrg?.id) void fetchProjectList(currentOrg.id);+ }, [currentOrg?.id]);++ useEffect(() => {+ if (project?.id && currentProject?.id !== project.id) {+ void changeActiveProject(project.id);+ }+ }, [currentProject?.id, project?.id]);++ useEffect(() => { loadConnectors(); }, [project?.id]);++ return (+ <div className="flex h-full w-full flex-col overflow-auto text-stone-700 dark:text-stone-300">+ <FeaturePageHeader+ eyebrow={messages.PROJECT}+ title={messages.GLOBAL_CONNECTORS}+ icon={<Plug className="h-4 w-4" />}+ tone="border-cyan-200 bg-cyan-50 text-cyan-700 dark:border-cyan-900/70 dark:bg-cyan-950/40 dark:text-cyan-300"+ actions={+ project?.id ? (+ <Button+ size="sm"+ className="gap-1.5"+ disabled={loading || types.length === 0}+ onClick={() => setRequestedType(types[0]?.type || null)}+ >+ <Cable className="h-3.5 w-3.5" />+ {messages.ADD_CONNECTOR}+ </Button>+ ) : null+ }+ />++ <FeatureAccess access="connectors.read" requireProject>+ <main className="flex flex-col gap-4 p-4">+ {loadError && <div className="rounded-lg border border-error/30 bg-error/5 p-4 dark:bg-error/10"><p className="text-sm font-semibold text-error">{messages.DATA_SOURCE_LOAD_FAILED}</p><p className="mt-1 text-xs text-muted-foreground">{loadError}</p><Button size="sm" variant="outline" className="mt-3" onClick={loadConnectors}>{messages.DATA_SOURCE_RETRY}</Button></div>}+ <ConnectedConnectorsSection+ connected={connected}+ loading={loading}+ projectName={project?.name || messages.NO_PROJECT}+ messages={messages}+ />++ <section className="border border-stone-200 bg-white p-4 dark:border-stone-800 dark:bg-stone-950">+ <div className="mb-4 flex flex-wrap items-center justify-between gap-3">+ <div>+ <h2 className="text-sm font-semibold text-stone-950 dark:text-stone-50">+ {messages.CONNECTOR_CATALOG}+ </h2>+ <p className="mt-1 text-xs text-muted-foreground">+ {messages.CONNECTOR_CATALOG_DESCRIPTION}+ </p>+ </div>+ <Button asChild variant="outline" size="sm">+ <Link href={`/organisation/project/${project?.id || ""}/connectors`}>+ {messages.MANAGE_CONNECTORS}+ <ArrowRight className="ml-1.5 h-3.5 w-3.5" />+ </Link>+ </Button>+ </div>+ <div className="relative mb-4 max-w-sm">+ <Search className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />+ <Input+ value={search}+ onChange={(event) => setSearch(event.target.value)}+ placeholder="Search connectors"+ aria-label="Search connectors"+ className="h-9 pl-9"+ />+ </div>++ {loading ? (+ <div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">+ {Array.from({ length: 8 }).map((_, index) => (+ <div+ key={index}+ className="space-y-3 rounded-md border border-stone-200 p-3 dark:border-stone-800"+ >+ <div className="flex items-center gap-2">+ <Skeleton className="h-8 w-8 rounded" />+ <div className="flex-1 space-y-2">+ <Skeleton className="h-3 w-3/4" />+ <Skeleton className="h-2.5 w-1/2" />+ </div>+ </div>+ <Skeleton className="h-8 w-full" />+ <Skeleton className="h-7 w-20" />+ </div>+ ))}+ </div>+ ) : (+ <>+ <Accordion+ type="multiple"+ defaultValue={filteredConnectorGroups.map((group) => group.key)}+ className="space-y-1"+ >+ {filteredConnectorGroups.map((group) => (+ <AccordionItem+ key={group.key}+ value={group.key}+ className="border-stone-200 dark:border-stone-800"+ >+ <AccordionTrigger className="py-3 text-sm font-semibold text-stone-950 hover:no-underline dark:text-stone-50">+ <span className="flex items-center gap-2">+ {group.label}+ <Badge variant="outline" className="text-[10px] font-normal">{group.connectors.length}</Badge>+ </span>+ </AccordionTrigger>+ <AccordionContent className="pb-3">+ <div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">+ {group.connectors.map((type) => (+ <div+ key={`${type.category}:${type.type}`}+ className="flex min-h-[190px] flex-col rounded-md border border-stone-200 p-3 dark:border-stone-800"+ >+ <div className="flex items-center gap-3">+ <Image+ src={type.icon || "/images/connect.svg"}+ alt=""+ width={32}+ height={32}+ className="h-8 w-8 shrink-0 object-contain"+ />+ <div className="min-w-0">+ <div className="flex items-center gap-1.5">+ <p className="truncate text-sm font-medium text-stone-950 dark:text-stone-50">+ {type.displayName}+ </p>+ {type.plan === "enterprise" ? (+ <Badge className="shrink-0 bg-amber-100 px-1.5 py-0 text-[9px] font-semibold text-amber-800 hover:bg-amber-100 dark:bg-amber-950/60 dark:text-amber-300 dark:hover:bg-amber-950/60">+ {messages.CONNECTOR_PREMIUM}+ </Badge>+ ) : null}+ {type.locked ? (+ <span+ className="inline-flex shrink-0 text-amber-700 dark:text-amber-300"+ title={messages.CONNECTOR_LOCKED}+ aria-label={messages.CONNECTOR_LOCKED}+ >+ <Lock className="h-3.5 w-3.5" />+ </span>+ ) : null}+ </div>+ <p className="text-[11px] text-muted-foreground">+ {type.type}+ </p>+ </div>+ </div>++ <p className="mt-3 line-clamp-2 text-xs leading-5 text-muted-foreground">+ {type.description || type.type}+ </p>++ <div className="mt-auto flex flex-wrap gap-1.5 pt-4">+ <Badge variant="secondary" className="text-[10px]">+ {type.category || "datasource"}+ </Badge>+ {type.declaredSignals?.map((signal) => (+ <Badge key={signal} variant="outline" className="text-[10px]">+ {signal}+ </Badge>+ ))}+ </div>++ <Button+ size="sm"+ variant="outline"+ className="mt-3 w-full"+ disabled={type.locked}+ title={type.locked ? messages.CONNECTOR_LOCKED : undefined}+ onClick={() => setRequestedType(type.type)}+ >+ {type.locked ? (+ <>+ <Lock className="mr-1.5 h-3.5 w-3.5" />+ {messages.CONNECTOR_LOCKED_ACTION}+ </>+ ) : (+ messages.ADD_CONNECTOR+ )}+ </Button>+ </div>+ ))}+ </div>+ </AccordionContent>+ </AccordionItem>+ ))}+ </Accordion>+ {!filteredConnectorGroups.length && (+ <p className="py-10 text-center text-sm text-muted-foreground">No connectors match your search.</p>+ )}+ </>+ )}+ </section>++ {project?.id ? (+ <DataSourcesPage+ projectId={project.id}+ showRouting={false}+ openType={requestedType}+ onOpenTypeHandled={() => setRequestedType(null)}+ />+ ) : null}+ </main>+ </FeatureAccess>+ </div>+ );+}++function ConnectedConnectorsSection({+ connected,+ loading,+ projectName,+ messages,+}: {+ connected: ConnectorSummary[];+ loading: boolean;+ projectName: string;+ messages: ReturnType<typeof getMessage>;+}) {+ return (+ <section className="border border-stone-200 bg-white p-4 dark:border-stone-800 dark:bg-stone-950">+ <div className="mb-4 flex items-center justify-between gap-3">+ <div>+ <h2 className="text-sm font-semibold text-stone-950 dark:text-stone-50">+ {messages.CONNECTED_CONNECTORS}+ </h2>+ <p className="mt-1 text-xs text-muted-foreground">{projectName}</p>+ </div>+ {loading ? (+ <Skeleton className="h-5 w-8 rounded-full" />+ ) : (+ <Badge variant="outline">{connected.length}</Badge>+ )}+ </div>++ {loading ? (+ <div className="grid gap-3 md:grid-cols-2 lg:grid-cols-3">+ {Array.from({ length: 3 }).map((_, index) => (+ <div+ key={index}+ className="flex items-center gap-3 rounded-md border border-stone-200 bg-stone-50 p-3 dark:border-stone-800 dark:bg-stone-900/60"+ >+ <Skeleton className="h-7 w-7 rounded" />+ <div className="flex-1 space-y-2">+ <Skeleton className="h-3 w-3/5" />+ <Skeleton className="h-2.5 w-2/5" />+ </div>+ </div>+ ))}+ </div>+ ) : connected.length ? (+ <div className="grid gap-3 md:grid-cols-2 lg:grid-cols-3">+ {connected.map((connector) => (+ <div+ key={connector.id}+ className="flex items-center justify-between gap-2 rounded-md border border-stone-200 bg-stone-50 p-3 dark:border-stone-800 dark:bg-stone-900/60"+ >+ <Image+ src={connector.icon || "/images/connect.svg"}+ alt=""+ width={28}+ height={28}+ className="h-7 w-7 shrink-0 object-contain"+ />+ <div className="min-w-0">+ <p className="truncate text-sm font-medium text-stone-950 dark:text-stone-50">+ {connector.name}+ </p>+ <p className="text-xs text-muted-foreground">+ {connector.type} · {connector.environment || "production"}+ </p>+ </div>+ <CheckCircle2 className="ml-auto h-4 w-4 shrink-0 text-emerald-600" />+ </div>+ ))}+ </div>+ ) : (+ <p className="text-sm text-muted-foreground">+ {messages.NO_CONNECTED_CONNECTORS}+ </p>+ )}+ </section>+ );+}diff --git a/src/client/src/components/(playground)/costs/manage-models-section.tsx b/src/client/src/components/(playground)/costs/manage-models-section.tsxindex 17b7755f..df666d53 100644--- a/src/client/src/components/(playground)/costs/manage-models-section.tsx+++ b/src/client/src/components/(playground)/costs/manage-models-section.tsx@@ -59,7 +59,10 @@ export default function ManageModelsSection() {const [editingProvider, setEditingProvider] = useState<ProviderFormData | null>(null);const [importJson, setImportJson] = useState("");- const databaseConfigList = useRootStore(getDatabaseConfigList) || [];+ const databaseConfigListRaw = useRootStore(getDatabaseConfigList);+ const databaseConfigList = Array.isArray(databaseConfigListRaw)+ ? databaseConfigListRaw+ : [];const activeDbConfig = useMemo(() => databaseConfigList.find((item) => !!item.isCurrent),[databaseConfigList]diff --git a/src/client/src/components/(playground)/database-config/database-config-page.tsx b/src/client/src/components/(playground)/database-config/database-config-page.tsxindex 1912a0d8..c3de6265 100644--- a/src/client/src/components/(playground)/database-config/database-config-page.tsx+++ b/src/client/src/components/(playground)/database-config/database-config-page.tsx@@ -1,26 +1,27 @@"use client";import FormBuilder from "@/components/common/form-builder";-import DatabaseConfigTabs from "@/components/(playground)/database-config/database-config-tabs";+import Image from "next/image";+import { Pencil, Trash2, Wifi } from "lucide-react";import { Button } from "@/components/ui/button";+import { Badge } from "@/components/ui/badge";+import { Dialog, DialogContent } from "@/components/ui/dialog";import { DatabaseConfig, DatabaseConfigWithActive } from "@/constants/dbConfig";import {- changeActiveDatabaseConfig,deleteDatabaseConfig,fetchDatabaseConfigList,} from "@/helpers/client/database-config";import {getDatabaseConfigList,- getDatabaseConfigListIsLoading,} from "@/selectors/database-config";+import { getCurrentProjectEnvironment } from "@/selectors/project";import { useRootStore } from "@/store";import useFetchWrapper from "@/utils/hooks/useFetchWrapper";-import { isNil, keyBy } from "lodash";-import { MouseEventHandler, useCallback, useState } from "react";+import { isNil } from "lodash";+import { useCallback, useEffect, useMemo, useState } from "react";import { toast } from "sonner";import { usePostHog } from "posthog-js/react";import { CLIENT_EVENTS } from "@/constants/events";-import { DatabaseConfigTabItemProps } from "@/types/database-config";import { FormBuilderEvent } from "@/types/form";import { PRIMARY_BACKGROUND } from "@/constants/common-classes";import getMessage from "@/constants/messages";@@ -29,14 +30,28 @@ function ModifyDatabaseConfig({dbConfig,canCreate = true,canUpdate = true,+ onSaved,}: {dbConfig?: DatabaseConfigWithActive;canCreate?: boolean;canUpdate?: boolean;+ onSaved?: () => void;}) {const posthog = usePostHog();+ const selectedEnvironment = useRootStore(getCurrentProjectEnvironment) || "production";const { fireRequest, isLoading } = useFetchWrapper();const messages = getMessage();+ const [environments, setEnvironments] = useState<string[]>([+ "production",+ dbConfig?.environment || selectedEnvironment,+ ].filter((value, index, values) => values.indexOf(value) === index));++ useEffect(() => {+ fetch("/api/project/environment")+ .then((response) => response.ok ? response.json() : { environments: [] })+ .then((body) => setEnvironments(Array.from(new Set(["production", ...(body.environments || []).map((item: { name: string }) => item.name)]))))+ .catch(() => undefined);+ }, []);const modifyDetails: FormBuilderEvent = useCallback((event) => {@@ -77,6 +92,7 @@ function ModifyDatabaseConfig({id: "db-config-details",});if (!dbConfig?.id) formElement.reset();+ onSaved?.();posthog?.capture(payload.id? CLIENT_EVENTS.DB_CONFIG_UPDATE_SUCCESS@@ -95,7 +111,7 @@ function ModifyDatabaseConfig({},});},- [dbConfig?.id]+ [dbConfig?.id, onSaved]);const formFieldsDisabled = dbConfig?.id@@ -121,12 +137,12 @@ function ModifyDatabaseConfig({{label: messages.DB_CONFIG_FIELD_ENVIRONMENT,inputKey: `${dbConfig?.id}-environment`,- fieldType: "INPUT",+ fieldType: "SELECT",fieldTypeProps: {- type: "text",name: "environment",placeholder: "production",- defaultValue: dbConfig?.environment,+ options: environments.map((environment) => ({ value: environment, label: environment })),+ defaultValue: dbConfig?.environment || selectedEnvironment,disabled: formFieldsDisabled,},},@@ -205,13 +221,13 @@ function ModifyDatabaseConfig({heading={dbConfig?.id? !dbConfig?.permissions?.canEdit- ? messages.DATABASE_CONFIG- : messages.UPDATE_DB_CONFIG- : messages.ADD_DB_CONFIG+ ? messages.CLICKHOUSE_CONNECTOR_EDIT_TITLE+ : messages.CLICKHOUSE_CONNECTOR_EDIT_TITLE+ : messages.CLICKHOUSE_CONNECTOR_ADD_TITLE}subHeading={!dbConfig?.id || dbConfig?.permissions?.canEdit- ? ""+ ? `${messages.CLICKHOUSE_CONNECTOR_DESCRIPTION} ${messages.CLICKHOUSE_CONNECTOR_INSTRUCTIONS}`: messages.DB_CONFIG_EDIT_PERMISSION_REQUIRED}subHeadingClass="text-error"@@ -228,163 +244,135 @@ function ModifyDatabaseConfig({}function DatabaseList({dbConfigs,- isLoadingList,canCreate,- canSelect,canUpdate,canDelete,canShare,+ hideHeader,+ openNew,+ onOpenNewHandled,+ hideEmpty,}: {dbConfigs: DatabaseConfigWithActive[];- isLoadingList: boolean;canCreate: boolean;- canSelect: boolean;canUpdate: boolean;canDelete: boolean;canShare: boolean;+ hideHeader?: boolean;+ openNew?: boolean;+ onOpenNewHandled?: () => void;+ hideEmpty?: boolean;}) {- const posthog = usePostHog();const messages = getMessage();- const [selectedDBConfigId, setSelectedDBConfigId] = useState<- string | undefined- >();-- const dbConfigByKey = keyBy(dbConfigs, "id");-- const onClickDB: MouseEventHandler<HTMLDivElement | HTMLButtonElement> = (- event- ) => {- const parent = (event.target as HTMLElement).closest(- ".item-element-card"- ) as HTMLElement;- if (!parent) return null;- const { itemId = "" } = parent.dataset;- setSelectedDBConfigId(itemId);- };-- const onClickDelete: MouseEventHandler<SVGSVGElement> = (event) => {- event.stopPropagation();- const parent = (event.target as HTMLElement).closest(- ".item-element-card"- ) as HTMLElement;- if (!parent) return null;- const { itemId = "" } = parent.dataset;+ const selectedEnvironment = useRootStore(getCurrentProjectEnvironment) || "production";+ const [editing, setEditing] = useState<DatabaseConfigWithActive | "new" | null>(null);+ const [testingId, setTestingId] = useState<string | null>(null);+ const visibleConfigs = useMemo(+ () => dbConfigs.filter((config) => (config.environment || "production").toLowerCase() === selectedEnvironment.toLowerCase()),+ [dbConfigs, selectedEnvironment]+ );+ useEffect(() => {+ if (openNew) {+ setEditing(visibleConfigs[0] || "new");+ onOpenNewHandled?.();+ }+ }, [onOpenNewHandled, openNew, visibleConfigs.length]);- if (itemId) deleteDatabaseConfig(itemId);+ const remove = (config: DatabaseConfigWithActive) => {+ if (!canDelete || !config.permissions?.canDelete) return;+ if (window.confirm(messages.DELETE_DATABASE_CONFIG_CONFIRMATION)) void deleteDatabaseConfig(config.id);};- const onClickSetCurrent: MouseEventHandler<HTMLDivElement> = (event) => {- event.stopPropagation();- const parent = (event.target as HTMLElement).closest(- ".item-element-card"- ) as HTMLElement;- if (!parent) return null;- const { itemId = "" } = parent.dataset;-- if (itemId) {- toast.loading(- messages.DB_CONFIG_SET_ACTIVE(dbConfigByKey[itemId].name),- {- id: "db-config-current",- }- );- changeActiveDatabaseConfig(itemId, () => {- posthog?.capture(CLIENT_EVENTS.DB_CONFIG_ACTION_CHANGE);- });+ const testConnection = async (config: DatabaseConfigWithActive) => {+ setTestingId(config.id);+ toast.loading(messages.DATA_SOURCE_TESTING, { id: "db-config-test" });+ try {+ const response = await fetch("/api/clickhouse", { method: "POST" });+ const body = await response.json().catch(() => ({}));+ if (!response.ok || body?.err) throw new Error(body?.err || messages.DATA_SOURCE_SAVE_FAILED);+ toast.success(messages.DATA_SOURCE_TEST_OK, { id: "db-config-test" });+ } catch (error: any) {+ toast.error(error?.message || messages.DATA_SOURCE_SAVE_FAILED, { id: "db-config-test" });+ } finally {+ setTestingId(null);}};- const items: DatabaseConfigTabItemProps[] = dbConfigs.map((dbConfig) => ({- id: dbConfig.id,- name: dbConfig.name,- badge: dbConfig.environment,- isCurrent: !!dbConfig.isCurrent,- canDelete: canDelete && !!dbConfig.permissions?.canDelete,- canEdit: canUpdate && !!dbConfig.permissions?.canEdit,- canShare: canShare && !!dbConfig.permissions?.canShare,- canSelect,- }));-return (- <div className="flex w-full flex-1 relative">- <DatabaseConfigTabs- addButton={canCreate}- items={items}- onClickTab={onClickDB}- selectedTabId={selectedDBConfigId || ""}- onClickItemChangeActive={onClickSetCurrent}- onClickItemDelete={onClickDelete}- />- <div className="flex flex-1 w-full h-full overflow-hidden">- {selectedDBConfigId ? (- <ModifyDatabaseConfig- dbConfig={dbConfigByKey[selectedDBConfigId]}- canCreate={canCreate}- canUpdate={canUpdate}- />- ) : (- <div className="flex flex-1 items-center justify-center">- <div className="flex flex-col items-center gap-1 text-center">- <h3 className="text-2xl font-bold tracking-tight text-stone-900 dark:text-stone-100">- {dbConfigs.length === 0- ? messages.DB_CONFIG_EMPTY_TITLE- : messages.DB_CONFIG_NOT_SELECTED_TITLE}- </h3>- <p className="text-sm text-stone-700 dark:text-stone-300">- {dbConfigs.length === 0- ? messages.DB_CONFIG_EMPTY_DESCRIPTION- : messages.DB_CONFIG_NOT_SELECTED_DESCRIPTION}- </p>- {dbConfigs.length !== 0 && canCreate && (- <Button- className="mt-4 item-element-card"- data-item-id={"ADD_NEW_ID"}- onClick={onClickDB}- >- {messages.ADD_DATABASE_CONFIG}- </Button>- )}+ <div className={hideHeader ? "relative contents" : "relative w-full p-4"}>+ {!hideHeader && <div className="mb-4 flex flex-wrap items-center justify-between gap-3">+ <div>+ <p className="text-xs font-semibold text-stone-950 dark:text-stone-50">ClickHouse targets for {selectedEnvironment}</p>+ <p className="mt-1 text-[11px] text-muted-foreground">Each target is a ClickHouse connector for this project environment.</p>+ </div>+ {canCreate && visibleConfigs.length === 0 && <Button size="sm" onClick={() => setEditing("new")}>+ {messages.ADD_DATABASE_CONFIG}</Button>}+ </div>}+ {visibleConfigs.length === 0 && hideEmpty ? null : visibleConfigs.length === 0 ? (+ <div className="rounded-lg border border-dashed border-stone-300 p-8 text-center dark:border-stone-700">+ <p className="text-sm font-medium text-stone-900 dark:text-stone-100">{messages.DB_CONFIG_EMPTY_TITLE}</p>+ <p className="mx-auto mt-1 max-w-md text-xs text-muted-foreground">{messages.DB_CONFIG_EMPTY_DESCRIPTION}</p>+ {canCreate && <Button size="sm" className="mt-4" onClick={() => setEditing("new")}>{messages.ADD_DATABASE_CONFIG}</Button>}+ </div>+ ) : (+ <div className={hideHeader ? "contents" : "grid gap-3 md:grid-cols-2"}>+ {visibleConfigs.map((config) => (+ <div key={config.id} className="flex min-h-[168px] flex-col justify-between rounded-lg border border-stone-200 bg-stone-50/70 p-3 transition-colors hover:border-primary/40 hover:bg-primary/[0.03] dark:border-stone-800 dark:bg-stone-900/50 dark:hover:border-primary/50">+ <div className="flex items-start gap-3">+ <div className="flex h-9 w-9 shrink-0 items-center justify-center rounded-md border border-stone-200 bg-white dark:border-stone-700 dark:bg-stone-950"><Image src="/images/connectors/clickhouse.svg" alt="" width={24} height={24} className="h-6 w-6 object-contain" /></div>+ <div className="min-w-0 flex-1"><p className="truncate text-sm font-medium text-stone-950 dark:text-stone-50">{config.name}</p><p className="mt-0.5 text-[11px] text-muted-foreground">ClickHouse connector</p></div>+ </div>+ <p className="mt-2 line-clamp-2 text-xs leading-5 text-muted-foreground">Telemetry, dashboards, and derived features for this project environment.</p>+ <div className="mt-2 flex flex-wrap gap-1"><Badge variant="secondary" className="text-[10px]">{config.environment || "production"}</Badge><Badge variant="outline" className="max-w-full truncate text-[10px]">{config.host}:{config.port}</Badge></div>+ <div className="mt-3 flex items-center justify-end gap-1 border-t border-stone-200 pt-2 dark:border-stone-800"><Button size="sm" variant="ghost" onClick={() => testConnection(config)} disabled={testingId === config.id}><Wifi className="mr-1 h-3.5 w-3.5" />{messages.DATA_SOURCE_TEST}</Button><Button size="icon" variant="ghost" onClick={() => setEditing(config)} disabled={!canUpdate || !config.permissions?.canEdit} aria-label="Edit connector"><Pencil className="h-3.5 w-3.5" /></Button><Button size="icon" variant="ghost" onClick={() => remove(config)} disabled={!canDelete || !config.permissions?.canDelete} aria-label="Delete connector"><Trash2 className="h-3.5 w-3.5 text-error" /></Button></div></div>- </div>- )}- </div>- {isLoadingList && (- <div className="flex absolute w-full left-0 top-0 h-full animate-pulse z-10" />+ ))}+ </div>)}+ {editing && <Dialog open onOpenChange={(open) => !open && setEditing(null)}><DialogContent className="max-h-[92vh] w-[calc(100vw-2rem)] max-w-3xl overflow-y-auto p-0"><ModifyDatabaseConfig dbConfig={editing === "new" ? undefined : editing} canCreate={canCreate} canUpdate={canUpdate} onSaved={() => setEditing(null)} /></DialogContent></Dialog>}</div>);}export default function Database({canCreate = true,- canSelect = true,canUpdate = true,canDelete = true,canShare = true,+ hideHeader = false,+ openNew = false,+ onOpenNewHandled,+ hideEmpty = false,}: {canCreate?: boolean;- canSelect?: boolean;canUpdate?: boolean;canDelete?: boolean;canShare?: boolean;+ hideHeader?: boolean;+ openNew?: boolean;+ onOpenNewHandled?: () => void;+ hideEmpty?: boolean;}) {- const messages = getMessage();const databaseList = useRootStore(getDatabaseConfigList);- const databaseListIsLoading = useRootStore(getDatabaseConfigListIsLoading);return isNil(databaseList) ? (- <div className="flex items-center justify-center w-full h-full animate-pulse dark:text-white">- {messages.OBSERVABILITY_LOADING}+ <div className={hideHeader ? "contents" : "p-4"}>+ <div className="flex min-h-[190px] flex-col gap-4 rounded-lg border border-stone-200 bg-stone-50/70 p-3 dark:border-stone-800 dark:bg-stone-900/50">+ <div className="flex items-center gap-3"><div className="h-10 w-10 animate-pulse rounded-md bg-stone-200 dark:bg-stone-800" /><div className="space-y-2"><div className="h-3 w-32 animate-pulse rounded bg-stone-200 dark:bg-stone-800" /><div className="h-2.5 w-24 animate-pulse rounded bg-stone-200 dark:bg-stone-800" /></div></div>+ <div className="space-y-2"><div className="h-2.5 w-full animate-pulse rounded bg-stone-200 dark:bg-stone-800" /><div className="h-2.5 w-3/4 animate-pulse rounded bg-stone-200 dark:bg-stone-800" /></div>+ <div className="mt-auto h-8 animate-pulse rounded bg-stone-200 dark:bg-stone-800" />+ </div></div>) : (<DatabaseListdbConfigs={(databaseList as DatabaseConfigWithActive[]) || []}- isLoadingList={databaseListIsLoading}canCreate={canCreate}- canSelect={canSelect}canUpdate={canUpdate}canDelete={canDelete}canShare={canShare}+ hideHeader={hideHeader}+ openNew={openNew}+ onOpenNewHandled={onOpenNewHandled}+ hideEmpty={hideEmpty}/>);}diff --git a/src/client/src/components/(playground)/filter/index.tsx b/src/client/src/components/(playground)/filter/index.tsxindex 8c422c5c..fa59e785 100644--- a/src/client/src/components/(playground)/filter/index.tsx+++ b/src/client/src/components/(playground)/filter/index.tsx@@ -3,7 +3,16 @@ import { getFilterDetails, getUpdateFilter } from "@/selectors/filter";import { useRootStore } from "@/store";import { DatePickerWithRange } from "@/components/ui/date-range-picker";import { TIME_RANGE_TYPE } from "@/store/filter";+import { usePostHog } from "posthog-js/react";+import { CLIENT_EVENTS } from "@/constants/events";import RefreshRate from "./refresh-rate";+import { useEffect, useMemo } from "react";+import {+ useSignalCapabilities,+ type Signal,+} from "@/utils/hooks/useSignalCapabilities";+import getMessage from "@/constants/messages";+import { getCurrentProjectEnvironment } from "@/selectors/project";const TIME_RANGE_TABS: { key: string; label: string }[] = Object.keys(TIME_RANGE_TYPE@@ -12,24 +21,137 @@ const TIME_RANGE_TABS: { key: string; label: string }[] = Object.keys(label: TIME_RANGE_TYPE[k as keyof typeof TIME_RANGE_TYPE],}));-const Filter = ({ className = "" }: { className?: string }) => {+const PRESET_RANGE_MS: Partial<Record<keyof typeof TIME_RANGE_TYPE, number>> = {+ "24H": 24 * 60 * 60 * 1000,+ "7D": 7 * 24 * 60 * 60 * 1000,+ "1M": 30 * 24 * 60 * 60 * 1000,+ "3M": 90 * 24 * 60 * 60 * 1000,+};++export function effectiveRangeLimit(resolved: {+ capabilities: {+ maxLookbackMs?: number;+ maxTimeRangeMs?: number;+ } | null;+} | null | undefined): number | undefined {+ const source = resolved?.capabilities;+ if (!source) return undefined;+ if (source.maxTimeRangeMs === undefined) return source.maxLookbackMs;+ return source.maxLookbackMs === undefined+ ? source.maxTimeRangeMs+ : Math.min(source.maxTimeRangeMs, source.maxLookbackMs);+}++export function isPresetRangeSupported(+ preset: keyof typeof TIME_RANGE_TYPE,+ maxTimeRangeMs?: number+): boolean {+ const presetMs = PRESET_RANGE_MS[preset];+ return maxTimeRangeMs === undefined || presetMs === undefined || presetMs <= maxTimeRangeMs;+}++export function clampDateRangeToLimit(+ start: Date,+ end: Date,+ maxTimeRangeMs?: number+): { start: Date; end: Date; clamped: boolean } {+ if (+ maxTimeRangeMs === undefined ||+ end.getTime() - start.getTime() <= maxTimeRangeMs+ ) {+ return { start, end, clamped: false };+ }+ return {+ start: new Date(end.getTime() - maxTimeRangeMs),+ end,+ clamped: true,+ };+}++const Filter = ({+ className = "",+ signal,+}: {+ className?: string;+ signal?: Signal;+}) => {+ const m = getMessage();+ const posthog = usePostHog();const filter = useRootStore(getFilterDetails);const updateFilter = useRootStore(getUpdateFilter);+ const environment = useRootStore(getCurrentProjectEnvironment);+ const { capabilities } = useSignalCapabilities(environment || undefined);+ const maxTimeRangeMs = useMemo(() => {+ if (signal) return effectiveRangeLimit(capabilities?.[signal]);+ const limits = Object.values(capabilities || {})+ .map(effectiveRangeLimit)+ .filter((value): value is number => Number.isFinite(value));+ return limits.length ? Math.min(...limits) : undefined;+ }, [capabilities, signal]);+ const maxTimeRangeDays = maxTimeRangeMs+ ? Math.max(1, Math.floor(maxTimeRangeMs / (24 * 60 * 60 * 1000)))+ : undefined;++ useEffect(() => {+ const current = filter.timeLimit.type as keyof typeof TIME_RANGE_TYPE;+ if (isPresetRangeSupported(current, maxTimeRangeMs)) return;+ const end = new Date();+ const start = new Date(end.getTime() - maxTimeRangeMs!);+ updateFilter("timeLimit.type", TIME_RANGE_TYPE.CUSTOM, { start, end });+ }, [filter.timeLimit.type, maxTimeRangeMs, updateFilter]);const handleChange = (key: string) => {+ const presetMs = PRESET_RANGE_MS[key as keyof typeof TIME_RANGE_TYPE];+ if (!isPresetRangeSupported(key as keyof typeof TIME_RANGE_TYPE, maxTimeRangeMs)) {+ return;+ }updateFilter("timeLimit.type", key);+ posthog?.capture(CLIENT_EVENTS.TIME_FILTER_CHANGE, {+ range: key,+ });};const onCustomDateChange = (start: Date, end: Date) => {- updateFilter("timeLimit.type", TIME_RANGE_TYPE.CUSTOM, { start, end });+ const bounded = clampDateRangeToLimit(start, end, maxTimeRangeMs);+ updateFilter("timeLimit.type", TIME_RANGE_TYPE.CUSTOM, {+ start: bounded.start,+ end: bounded.end,+ });+ posthog?.capture(CLIENT_EVENTS.TIME_FILTER_CHANGE, {+ range: TIME_RANGE_TYPE.CUSTOM,+ });};return (- <div className={`flex grow items-center gap-4 ${className}`}>- <Tabs value={filter.timeLimit.type} onValueChange={handleChange}>- <TabsList className="p-0 h-[30px] border border-stone-200 dark:border-stone-800">+ <div className={`flex min-w-0 shrink items-center gap-2 ${className}`}>+ <Tabs+ value={filter.timeLimit.type}+ onValueChange={handleChange}+ className="min-w-0 shrink"+ >+ <TabsList className="h-[30px] shrink-0 overflow-hidden p-0 border border-stone-200 dark:border-stone-800">{TIME_RANGE_TABS.map(({ label, key }) => (- <TabsTrigger key={key} value={key} className="py-1.5 text-xs">+ <TabsTrigger+ key={key}+ value={key}+ className="shrink-0 py-1.5 px-1.5 text-xs sm:px-2"+ disabled={+ maxTimeRangeMs !== undefined &&+ Boolean(+ PRESET_RANGE_MS[key as keyof typeof TIME_RANGE_TYPE] &&+ PRESET_RANGE_MS[key as keyof typeof TIME_RANGE_TYPE]! >+ maxTimeRangeMs+ )+ }+ title={+ maxTimeRangeDays &&+ PRESET_RANGE_MS[key as keyof typeof TIME_RANGE_TYPE] &&+ PRESET_RANGE_MS[key as keyof typeof TIME_RANGE_TYPE]! >+ maxTimeRangeMs!+ ? m.DATA_SOURCE_MAX_TIME_RANGE_HINT(maxTimeRangeDays)+ : undefined+ }+ >{label}</TabsTrigger>))}@@ -39,6 +161,7 @@ const Filter = ({ className = "" }: { className?: string }) => {<DatePickerWithRangeselectedDate={filter.timeLimit}onCustomDateChange={onCustomDateChange}+ maxRangeDays={maxTimeRangeDays}/>)}<RefreshRate />diff --git a/src/client/src/components/(playground)/filter/refresh-rate.tsx b/src/client/src/components/(playground)/filter/refresh-rate.tsxindex 5f223689..fe4a8c98 100644--- a/src/client/src/components/(playground)/filter/refresh-rate.tsx+++ b/src/client/src/components/(playground)/filter/refresh-rate.tsx@@ -82,7 +82,7 @@ const RefreshRate = () => {return (<DropdownMenu><DropdownMenuTrigger asChild>- <Button variant="outline" className="ml-auto flex h-[30px] w-auto shrink-0 justify-start gap-4 overflow-hidden px-2 py-1 font-normal text-stone-500 hover:text-stone-600 group-data-[state=close]:justify-center dark:bg-stone-800 dark:text-stone-400 dark:hover:bg-stone-900 dark:hover:text-stone-300">+ <Button variant="outline" className="flex h-[30px] w-auto max-w-[4.5rem] shrink-0 justify-start gap-1 overflow-hidden px-2 py-1 font-normal text-stone-500 hover:text-stone-600 group-data-[state=close]:justify-center dark:bg-stone-800 dark:text-stone-400 dark:hover:bg-stone-900 dark:hover:text-stone-300"><TimerReset className={`size-3 shrink-0`} /><span className="block text-ellipsis overflow-hidden whitespace-nowrap grow text-xs">{filter.refreshRate}</span><ChevronsUpDown className={`size-3 block shrink-0`} />diff --git a/src/client/src/components/(playground)/filter/traces-filter.tsx b/src/client/src/components/(playground)/filter/traces-filter.tsxindex 4e3bedac..cfd1bd48 100644--- a/src/client/src/components/(playground)/filter/traces-filter.tsx+++ b/src/client/src/components/(playground)/filter/traces-filter.tsx@@ -41,6 +41,7 @@ import {FILTER_PARAM_KEYS,getFilterStorageKey,} from "@/helpers/client/filter-persistence";+import type { Signal } from "@/utils/hooks/useSignalCapabilities";const m = getMessage();@@ -986,7 +987,8 @@ function loadFilterFromStorage(storageKey: string): PersistedFilter | null {function applyStoredFilter(saved: PersistedFilter,updateFilter: (key: string, value: any, extraParams?: any) => void,- validTimeRanges: Set<string>+ validTimeRanges: Set<string>,+ currentFilter?: FilterType) {// Time limit first (it resets selectedConfig, so must come before selectedConfig)if (saved.timeLimitType === "CUSTOM" && saved.timeLimitStart && saved.timeLimitEnd) {@@ -995,7 +997,17 @@ function applyStoredFilter(end: new Date(saved.timeLimitEnd),});} else if (validTimeRanges.has(saved.timeLimitType)) {- updateFilter("timeLimit.type", saved.timeLimitType as TIME_RANGES);+ // Skip re-stamping relative ranges when the store already holds the same+ // type with a recent end — remounts would otherwise bust the telemetry+ // request cache by changing end=now() every navigation.+ const sameType = currentFilter?.timeLimit?.type === saved.timeLimitType;+ const endMs = currentFilter?.timeLimit?.end+ ? new Date(currentFilter.timeLimit.end).getTime()+ : 0;+ const recentEnough = endMs > 0 && Date.now() - endMs < 30_000;+ if (!(sameType && recentEnough)) {+ updateFilter("timeLimit.type", saved.timeLimitType as TIME_RANGES);+ }}if (saved.limit) updateFilter("limit", saved.limit);if (saved.selectedConfig && hasActiveConfig(saved.selectedConfig)) {@@ -1048,7 +1060,14 @@ function useFilterUrlSync(});}} else {- updateFilter("timeLimit.type", tr);+ const sameType = filter.timeLimit?.type === tr;+ const endMs = filter.timeLimit?.end+ ? new Date(filter.timeLimit.end).getTime()+ : 0;+ const recentEnough = endMs > 0 && Date.now() - endMs < 30_000;+ if (!(sameType && recentEnough)) {+ updateFilter("timeLimit.type", tr);+ }}}const limitParam = params.get("limit");@@ -1070,7 +1089,7 @@ function useFilterUrlSync(} else {// ── Fall back to localStorage ─────────────────────────────────────const saved = loadFilterFromStorage(storageKey);- if (saved) applyStoredFilter(saved, updateFilter, VALID_TIME_RANGES);+ if (saved) applyStoredFilter(saved, updateFilter, VALID_TIME_RANGES, filter);}// Signal that the initial filter read (URL / localStorage) is complete.@@ -1136,11 +1155,12 @@ export default function TracesFilter({customSortOptions,pageName,columns,- configUrl = "/api/metrics/request/config",- attributeKeysUrl = "/api/metrics/request/attribute-keys",+ configUrl = "/api/telemetry/request/config",+ attributeKeysUrl = "/api/telemetry/request/attribute-keys",customAttributeTypes = ["SpanAttributes", "ResourceAttributes", "Field"],filterStorageScope,extraControls,+ signal,}: {total?: number;supportDynamicFilters?: boolean;@@ -1164,6 +1184,8 @@ export default function TracesFilter({// coding-agent pages to drop in a User picker that's visually// part of the same control cluster.extraControls?: React.ReactNode;+ /** Datasource signal whose capability limits drive the time selector. */+ signal?: Signal;}) {const [isVisibleFilters, setIsVisibileFilters] = useState<boolean>(false);const filter = useRootStore(getFilterDetails);@@ -1235,58 +1257,60 @@ export default function TracesFilter({}, []);return (- <div className="flex flex-col items-center w-full justify-between mb-4">- <div className="flex w-full gap-4">- <Filter />- {filterConfig && !!total && total > 0 && (- <TracesPagination- currentPage={filter.offset / filter.limit + 1}- currentSize={filter.limit}- totalPage={ceil(total / filter.limit)}- onClickPageAction={onClickPageAction}- onClickPageLimit={onClickPageLimit}- />- )}- {showVisibilityColumns && (- <VisibilityColumns columns={columns} pageName={pageName} />- )}- {!!total && total > 0 && (- <Sorting- sorting={filter.sorting}- includeOnlySorting={includeOnlySorting}- customOptions={customSortOptions}- />- )}- {supportDynamicFilters && showGroupBy && (- <GroupByDropdown- groupBy={filter.groupBy}- onChangeGroupBy={onChangeGroupBy}- customAttributeTypes={customAttributeTypes}- />- )}- {extraControls}- {supportDynamicFilters && (+ <div className="flex w-full min-w-0 flex-col items-stretch gap-2 mb-4">+ <div className="flex w-full min-w-0 flex-wrap items-center justify-between gap-3 overflow-x-auto pb-0.5 [scrollbar-width:thin]">+ <Filter className="min-w-0 shrink-0" signal={signal} />+ <div className="ml-auto flex shrink-0 items-center gap-2">+ {filterConfig && !!total && total > 0 && (+ <TracesPagination+ currentPage={filter.offset / filter.limit + 1}+ currentSize={filter.limit}+ totalPage={ceil(total / filter.limit)}+ onClickPageAction={onClickPageAction}+ onClickPageLimit={onClickPageLimit}+ />+ )}+ {showVisibilityColumns && (+ <VisibilityColumns columns={columns} pageName={pageName} />+ )}+ {!!total && total > 0 && (+ <Sorting+ sorting={filter.sorting}+ includeOnlySorting={includeOnlySorting}+ customOptions={customSortOptions}+ />+ )}+ {supportDynamicFilters && showGroupBy && (+ <GroupByDropdown+ groupBy={filter.groupBy}+ onChangeGroupBy={onChangeGroupBy}+ customAttributeTypes={customAttributeTypes}+ />+ )}+ {extraControls}+ {supportDynamicFilters && (+ <Button+ variant="outline"+ size="default"+ className="text-stone-500 hover:text-stone-600 dark:text-stone-400 dark:hover:text-stone-300 dark:bg-stone-800 dark:hover:bg-stone-900 aspect-square p-1 h-[30px] relative"+ onClick={toggleIsVisibleFilters}+ >+ <SlidersHorizontal className="w-3 h-3" />+ {areFiltersApplied && (+ <span className="w-2 h-2 bg-primary absolute top-1 right-1 rounded-full animate-ping" />+ )}+ </Button>+ )}<Buttonvariant="outline"size="default"- className="text-stone-500 hover:text-stone-600 dark:text-stone-400 dark:hover:text-stone-300 dark:bg-stone-800 dark:hover:bg-stone-900 aspect-square p-1 h-[30px] relative"- onClick={toggleIsVisibleFilters}+ title={m.OBSERVABILITY_COPY_SHARE_LINK}+ className="text-stone-500 hover:text-stone-600 dark:text-stone-400 dark:hover:text-stone-300 dark:bg-stone-800 dark:hover:bg-stone-900 aspect-square p-1 h-[30px]"+ onClick={onShareLink}>- <SlidersHorizontal className="w-3 h-3" />- {areFiltersApplied && (- <span className="w-2 h-2 bg-primary absolute top-1 right-1 rounded-full animate-ping" />- )}+ <Link2 className="w-3 h-3" /></Button>- )}- <Button- variant="outline"- size="default"- title={m.OBSERVABILITY_COPY_SHARE_LINK}- className="text-stone-500 hover:text-stone-600 dark:text-stone-400 dark:hover:text-stone-300 dark:bg-stone-800 dark:hover:bg-stone-900 aspect-square p-1 h-[30px]"- onClick={onShareLink}- >- <Link2 className="w-3 h-3" />- </Button>+ </div></div>{supportDynamicFilters && (<DynamicFiltersdiff --git a/src/client/src/components/(playground)/filter/traces-pagination.tsx b/src/client/src/components/(playground)/filter/traces-pagination.tsxindex aa7c48ce..4d31dad5 100644--- a/src/client/src/components/(playground)/filter/traces-pagination.tsx+++ b/src/client/src/components/(playground)/filter/traces-pagination.tsx@@ -43,48 +43,40 @@ export default function TracesPagination(props: PaginationProps) {props.totalPage === 0 || props.currentPage === props.totalPage;return (- <div className="flex gap-4 self-start">- <div className="flex align-end justify-center">- <p className="text-xs shrink-0 mr-1 self-center text-stone-950 dark:text-stone-100">- Size :{" "}+ <div className="flex shrink-0 items-center gap-2 self-center">+ <div className="flex items-center gap-1.5">+ <p className="text-xs shrink-0 text-stone-950 dark:text-stone-100">+ Size</p>- <div className="w-[80px]">- <Select- onValueChange={onSizeChange}- defaultValue={`${props.currentSize}`}+ <Select+ onValueChange={onSizeChange}+ defaultValue={`${props.currentSize}`}+ >+ <SelectTrigger+ id="page-size"+ aria-label="Page size"+ className="h-[30px] w-[4.5rem] shrink-0 gap-1 px-2 py-1 text-xs text-stone-500 hover:text-stone-600 dark:bg-stone-800 dark:text-stone-400 dark:hover:bg-stone-900 dark:hover:text-stone-300 [&>span]:line-clamp-none [&>svg]:ml-0 [&>svg]:h-3 [&>svg]:w-3">- <SelectTrigger- id="model"- className="items-center [&_[data-description]]:hidden h-auto py-1 text-stone-500 hover:text-stone-600 dark:text-stone-400 dark:hover:text-stone-300 dark:bg-stone-800 dark:hover:bg-stone-900 py-1 px-2 h-[30px] relative gap-1 text-xs"- >- <SelectValue- placeholder={`${props.currentSize}`}- defaultValue={`${props.currentSize}`}- />- </SelectTrigger>- <SelectContent>- {PageSizes.map((size: number) => (- <SelectItem- key={size}- value={`${size}`}- className="outline-none"- >- <div className="flex items-start text-muted-foreground ">- <div className="grid">- <p>{size}</p>- </div>- </div>- </SelectItem>- ))}- </SelectContent>- </Select>- </div>+ <SelectValue placeholder={`${props.currentSize}`} />+ </SelectTrigger>+ <SelectContent>+ {PageSizes.map((size: number) => (+ <SelectItem+ key={size}+ value={`${size}`}+ className="outline-none"+ >+ {size}+ </SelectItem>+ ))}+ </SelectContent>+ </Select></div>- <Pagination className="w-auto shrink-0 m-0">- <PaginationContent>+ <Pagination className="m-0 w-auto shrink-0">+ <PaginationContent className="gap-0.5"><PaginationItem><PaginationPrevious- className={`py-1 h-full ${+ className={`h-[30px] px-2 py-1 ${firstPage? "pointer-events-none cursor-not-allowed text-stone-400": "text-stone-950 dark:text-stone-100"@@ -95,13 +87,13 @@ export default function TracesPagination(props: PaginationProps) {/></PaginationItem><PaginationItem>- <div className="flex items-center text-sm text-stone-950 dark:text-stone-100">+ <div className="flex items-center whitespace-nowrap px-1 text-xs text-stone-950 dark:text-stone-100">{props.currentPage} of {props.totalPage || 1}</div></PaginationItem><PaginationItem><PaginationNext- className={`py-1 h-full ${+ className={`h-[30px] px-2 py-1 ${lastPage? "pointer-events-none cursor-not-allowed text-stone-400": "text-stone-950 dark:text-stone-100"diff --git a/src/client/src/components/(playground)/header.tsx b/src/client/src/components/(playground)/header.tsxindex e77f89d1..db26742f 100644--- a/src/client/src/components/(playground)/header.tsx+++ b/src/client/src/components/(playground)/header.tsx@@ -8,6 +8,7 @@ import HeaderAppTrail from "./header-app-trail";import HeaderPageTrail from "./header-page-trail";import { HeaderScopeSeparator } from "./header-scope-pill";import { playgroundTopBarClassName } from "./sidebar-layout-context";+import { ChatHeaderButton } from "./chat/chat-floating-button";export function HeaderContextRow() {const { setHeaderRef } = usePortal();@@ -32,7 +33,7 @@ export function HeaderContextRow() {<DatabaseConfigSwitch contentAlign="start" contentSide="bottom" /><HeaderAppTrail /><HeaderPageTrail />- <div ref={containerRef} className="ml-auto flex items-center" />+ <div className="ml-auto flex items-center gap-2"><ChatHeaderButton /><div ref={containerRef} className="flex items-center" /></div></div>);}diff --git a/src/client/src/components/(playground)/intermediate-state.tsx b/src/client/src/components/(playground)/intermediate-state.tsxindex 441a9199..2f730705 100644--- a/src/client/src/components/(playground)/intermediate-state.tsx+++ b/src/client/src/components/(playground)/intermediate-state.tsx@@ -1,4 +1,5 @@import type { ReactNode } from "react";+import { Loader2 } from "lucide-react";export type IntermediateState = {type: "loading" | "nodata";@@ -11,17 +12,18 @@ export default function IntermediateState(props: IntermediateState) {props.children) : (<div- className={`flex w-full items-center justify-center p-3 text-sm text-stone-500 ${+ className={`flex w-full items-center justify-center gap-2 p-3 text-sm text-stone-500 dark:text-stone-400 ${props.classNames || ""}`}>+ <Loader2 className="h-4 w-4 animate-spin shrink-0" />Loading...</div>);if (props.type === "nodata")return (<div- className={`flex w-full items-center justify-center p-3 text-sm text-stone-500 ${+ className={`flex w-full items-center justify-center p-3 text-sm text-stone-500 dark:text-stone-400 ${props.classNames || ""}`}>diff --git a/src/client/src/components/(playground)/manage-dashboard/board-creator/components/edit-widget-sheet.tsx b/src/client/src/components/(playground)/manage-dashboard/board-creator/components/edit-widget-sheet.tsxindex 145ff12c..8911aeaa 100644--- a/src/client/src/components/(playground)/manage-dashboard/board-creator/components/edit-widget-sheet.tsx+++ b/src/client/src/components/(playground)/manage-dashboard/board-creator/components/edit-widget-sheet.tsx@@ -38,10 +38,117 @@ import {import { Textarea } from "@/components/ui/textarea";import { useDashboard } from "../context/dashboard-context";import QueryDebugger from "./query-debugger";+import {+ StructuredQueryBuilder,+ type StructuredQueryValue,+} from "./structured-query-builder";import { ColorSelector } from "./color-selector";import MarkdownWidgetComponent from "../widgets/markdown-widget";import { Tooltip, TooltipTrigger, TooltipPortal, TooltipContent } from "@/components/ui/tooltip";import { DEFAULT_PRIMARY_COLOR, SUPPORTED_WIDGETS } from "../constants";+import {+ WIDGET_DATA_SOURCE_LABEL,+ WIDGET_DATA_SOURCE_BUILTIN,+ WIDGET_DATA_SOURCE_EMPTY,+ WIDGET_DATA_SOURCE_HINT,+ WIDGET_DATA_SOURCE_EXTERNAL_SQL_DISABLED,+ WIDGET_DATA_SOURCE_PROJECT_TRACES_HINT,+ WIDGET_STRUCTURED_VIEW_GENERATED_QUERY,+} from "@/constants/messages/en";+import {+ inferStructuredFromClickHouseSql,+ inferredToStructuredQuery,+ isLegacyOtelTracesSql,+ openLITQueryToClickHouseSql,+} from "@/lib/platform/manage-dashboard/widget-sql-bridge";+import { useRootStore } from "@/store";+import { getCurrentProjectEnvironment } from "@/selectors/project";++const BUILTIN_SOURCE_PREFIX = "builtin:";+const DATABASE_CONNECTOR_PREFIX = "database:";++interface TelemetrySourceOption {+ id: string;+ name: string;+ type: string;+ signals: string;+ environment?: string;+}++interface SignalBindingOption {+ sourceId: string;+ sourceName: string;+ sourceType: string;+}++interface TelemetrySourceTypeDescriptor {+ type: string;+ declaredSignals?: string[];+ capabilities?: { serverAggregation?: boolean };+}++type Signal = "traces" | "logs" | "metrics";++function parseSourceSignals(signals: string | undefined): Signal[] {+ if (!signals) return ["traces", "logs", "metrics"];+ return signals+ .split(",")+ .map((part) => part.trim())+ .filter(+ (part): part is Signal =>+ part === "traces" || part === "logs" || part === "metrics"+ );+}++function sourceServesSignal(+ source: Pick<TelemetrySourceOption, "id" | "signals" | "type">,+ signal: Signal+): boolean {+ if (+ source.type === "clickhouse" ||+ source.id.startsWith(BUILTIN_SOURCE_PREFIX)+ ) {+ return true;+ }+ const declared = parseSourceSignals(source.signals);+ return declared.length === 0 || declared.includes(signal);+}++function connectorToSourceOption(connector: {+ id?: string;+ name?: string;+ type?: string;+ signals?: string;+ environment?: string;+}): TelemetrySourceOption | null {+ const type = String(connector.type || "");+ const rawId = String(connector.id || "");+ if (!rawId || !type) return null;+ if (type === "clickhouse") {+ const databaseConfigId = rawId.startsWith(DATABASE_CONNECTOR_PREFIX)+ ? rawId.slice(DATABASE_CONNECTOR_PREFIX.length)+ : rawId;+ if (!databaseConfigId) return null;+ return {+ id: `${BUILTIN_SOURCE_PREFIX}${databaseConfigId}`,+ name: connector.name || WIDGET_DATA_SOURCE_BUILTIN,+ type: "clickhouse",+ signals: "traces,logs,metrics",+ environment: connector.environment || "production",+ };+ }+ const sourceId = rawId.startsWith("telemetry:")+ ? rawId.slice("telemetry:".length)+ : rawId;+ if (!sourceId) return null;+ return {+ id: sourceId,+ name: connector.name || sourceId,+ type,+ signals: connector.signals || "traces,logs,metrics",+ environment: connector.environment || "production",+ };+}interface NonMarkdownConfig {query: string;@@ -72,9 +179,292 @@ export const EditWidgetSheet: React.FC<EditWidgetSheetProps> = ({} = useEditWidget();const { handleWidgetCrud, loadWidgetData } = useDashboard();+ const currentEnvironment =+ useRootStore(getCurrentProjectEnvironment) || "production";const [queryResult, setQueryResult] = React.useState<any>(null);const [queryError, setQueryError] = React.useState<string | null>(null);const [isQueryLoading, setIsQueryLoading] = React.useState(false);+ const [sources, setSources] = React.useState<TelemetrySourceOption[]>([]);+ const [typeDescriptors, setTypeDescriptors] = React.useState<+ TelemetrySourceTypeDescriptor[]+ >([]);+ const [bindingBySignal, setBindingBySignal] = React.useState<+ Partial<Record<Signal, SignalBindingOption>>+ >({});+ const [showGeneratedQuery, setShowGeneratedQuery] = React.useState(false);+ const inferredForWidgetRef = React.useRef<string | null>(null);+ const autoSelectedWidgetRef = React.useRef<string | null>(null);++ useEffect(() => {+ let active = true;+ const params = new URLSearchParams({ environment: currentEnvironment });+ Promise.all([+ fetch(`/api/connectors`).then((r) => (r.ok ? r.json() : null)),+ fetch(`/api/telemetry-source?${params.toString()}`).then((r) =>+ r.ok ? r.json() : null+ ),+ fetch(`/api/telemetry-source/binding?${params.toString()}`).then((r) =>+ r.ok ? r.json() : null+ ),+ ])+ .then(([connectorsJson, sourcesJson, bindingsJson]) => {+ if (!active) return;+ const fromConnectors = Array.isArray(connectorsJson?.connectors)+ ? (connectorsJson.connectors as Array<Record<string, string>>)+ .map((connector) => connectorToSourceOption(connector))+ .filter((option): option is TelemetrySourceOption => !!option)+ : [];+ const fromSources = Array.isArray(sourcesJson?.sources)+ ? (sourcesJson.sources as TelemetrySourceOption[])+ : [];+ const merged = new Map<string, TelemetrySourceOption>();+ for (const option of [...fromConnectors, ...fromSources]) {+ merged.set(option.id, option);+ }+ setSources(Array.from(merged.values()));+ if (sourcesJson?.availableTypeDescriptors) {+ setTypeDescriptors(+ sourcesJson.availableTypeDescriptors as TelemetrySourceTypeDescriptor[]+ );+ } else if (connectorsJson?.availableTypeDescriptors) {+ setTypeDescriptors(+ connectorsJson.availableTypeDescriptors as TelemetrySourceTypeDescriptor[]+ );+ }+ const bindings = Array.isArray(bindingsJson?.bindings)+ ? (bindingsJson.bindings as Array<{+ signal?: string;+ sourceId?: string;+ sourceName?: string | null;+ sourceType?: string | null;+ }>)+ : [];+ const nextBindings: Partial<Record<Signal, SignalBindingOption>> = {};+ for (const binding of bindings) {+ if (+ (binding.signal === "traces" ||+ binding.signal === "logs" ||+ binding.signal === "metrics") &&+ binding.sourceId+ ) {+ nextBindings[binding.signal] = {+ sourceId: binding.sourceId,+ sourceName:+ binding.sourceName ||+ (binding.sourceType === "clickhouse"+ ? WIDGET_DATA_SOURCE_BUILTIN+ : binding.sourceId),+ sourceType: binding.sourceType || "clickhouse",+ };+ }+ }+ setBindingBySignal(nextBindings);+ })+ .catch(() => {});+ return () => {+ active = false;+ };+ }, [currentEnvironment]);++ const currentConfig = (currentWidget?.config || {}) as Record<string, any>;+ const selectedSignal = (+ currentConfig.structuredQuery?.query?.signal ||+ currentConfig.signal ||+ "traces"+ ) as Signal;++ const environmentSources = React.useMemo(() => {+ const merged = new Map<string, TelemetrySourceOption>();+ for (const source of sources) {+ if ((source.environment || "production") !== currentEnvironment) continue;+ merged.set(source.id, source);+ }+ // Always surface the project routers so the Select can show the bound value.+ for (const binding of Object.values(bindingBySignal)) {+ if (!binding?.sourceId || merged.has(binding.sourceId)) continue;+ merged.set(binding.sourceId, {+ id: binding.sourceId,+ name: binding.sourceName,+ type: binding.sourceType,+ signals: "traces,logs,metrics",+ environment: currentEnvironment,+ });+ }+ return Array.from(merged.values());+ }, [bindingBySignal, currentEnvironment, sources]);++ const signalSourceOptions = React.useMemo(+ () =>+ environmentSources.filter((source) =>+ sourceServesSignal(source, selectedSignal)+ ),+ [environmentSources, selectedSignal]+ );++ const bindingSourceId = bindingBySignal[selectedSignal]?.sourceId || null;+ const configuredSourceId =+ typeof currentConfig.sourceId === "string" && currentConfig.sourceId+ ? currentConfig.sourceId+ : null;+ const preferredSourceId =+ configuredSourceId ||+ bindingSourceId ||+ signalSourceOptions[0]?.id ||+ "";+ const selectedSourceId = signalSourceOptions.some(+ (option) => option.id === preferredSourceId+ )+ ? preferredSourceId+ : signalSourceOptions[0]?.id || "";+ const selectedSource =+ signalSourceOptions.find((s) => s.id === selectedSourceId) ||+ environmentSources.find((s) => s.id === selectedSourceId);+ const isExternalSource =+ !!selectedSource && selectedSource.type !== "clickhouse";+ const usesProjectExternalTraces =+ selectedSignal === "traces" &&+ isExternalSource &&+ typeof currentConfig.query === "string" &&+ /\botel_traces\b/i.test(currentConfig.query) &&+ !/\bopenlit_evaluation\b/i.test(currentConfig.query);+ const hasStructuredQuery = !!currentConfig.structuredQuery;+ const isOtelTracesSql =+ typeof currentConfig.query === "string" &&+ isLegacyOtelTracesSql(currentConfig.query);+ const showStructuredBuilder =+ isExternalSource ||+ usesProjectExternalTraces ||+ hasStructuredQuery ||+ isOtelTracesSql ||+ (!currentConfig.query && selectedSource?.type === "clickhouse");+ const selectedTypeDescriptor = selectedSource+ ? typeDescriptors.find((d) => d.type === selectedSource.type)+ : undefined;+ // Each signal can be routed to a different datasource. Keep all three in+ // the builder; changing signal immediately resolves that signal's binding.+ const sourceSignals: Signal[] = ["traces", "logs", "metrics"];+ const sourceSupportsAggregation =+ selectedTypeDescriptor?.capabilities?.serverAggregation !== false ||+ usesProjectExternalTraces ||+ selectedSource?.type === "clickhouse" ||+ !selectedSource;+ const structuredValue: StructuredQueryValue | undefined =+ currentConfig.structuredQuery+ ? {+ mode: currentConfig.structuredQuery.mode || "timeseries",+ query: currentConfig.structuredQuery.query || {},+ draftFilters: currentConfig.structuredQuery+ .draftFilters as StructuredQueryValue["draftFilters"],+ }+ : undefined;++ const resolveSourceForSignal = React.useCallback(+ (signal: Signal) =>+ bindingBySignal[signal]?.sourceId ||+ environmentSources.find((option) => sourceServesSignal(option, signal))+ ?.id ||+ null,+ [bindingBySignal, environmentSources]+ );++ const handleSourceChange = (nextSourceId: string) => {+ if (!currentWidget) return;+ updateWidget(currentWidget.id, {+ config: {+ ...currentConfig,+ sourceId: nextSourceId || null,+ signal: selectedSignal,+ },+ });+ };++ // Reset auto-select tracking when switching widgets, then apply the+ // project router for the current signal when the panel has no source yet.+ React.useEffect(() => {+ autoSelectedWidgetRef.current = null;+ }, [currentWidget?.id]);++ React.useEffect(() => {+ if (!currentWidget || currentWidget.type === WidgetType.MARKDOWN) return;+ if (autoSelectedWidgetRef.current === currentWidget.id) return;+ if (configuredSourceId) {+ autoSelectedWidgetRef.current = currentWidget.id;+ return;+ }+ const defaultSourceId = resolveSourceForSignal(selectedSignal);+ if (!defaultSourceId) return;+ autoSelectedWidgetRef.current = currentWidget.id;+ updateWidget(currentWidget.id, {+ config: {+ ...currentConfig,+ sourceId: defaultSourceId,+ signal: selectedSignal,+ },+ });+ }, [+ configuredSourceId,+ currentConfig,+ currentWidget,+ resolveSourceForSignal,+ selectedSignal,+ updateWidget,+ ]);++ // Seed builder from legacy ClickHouse SQL when opening a seeded LLM widget.+ React.useEffect(() => {+ if (!currentWidget || currentWidget.type === WidgetType.MARKDOWN) return;+ if (inferredForWidgetRef.current === currentWidget.id) return;+ inferredForWidgetRef.current = currentWidget.id;+ if (currentConfig.structuredQuery) return;+ if (+ typeof currentConfig.query !== "string" ||+ !isLegacyOtelTracesSql(currentConfig.query)+ ) {+ return;+ }+ const inferred = inferStructuredFromClickHouseSql(currentConfig.query);+ if (!inferred) return;+ const structured = inferredToStructuredQuery(inferred);+ updateWidget(currentWidget.id, {+ config: {+ ...currentConfig,+ signal: "traces",+ structuredQuery: structured,+ },+ });+ }, [currentWidget?.id]);++ const generatedQueryPreview = React.useMemo(() => {+ if (!structuredValue) return "";+ if (isExternalSource || usesProjectExternalTraces) {+ return JSON.stringify(structuredValue, null, 2);+ }+ return openLITQueryToClickHouseSql(+ structuredValue.query as any,+ structuredValue.mode+ );+ }, [structuredValue, isExternalSource, usesProjectExternalTraces]);++ const handleStructuredChange = (next: StructuredQueryValue) => {+ if (!currentWidget) return;+ const nextSignal = (next.query.signal as Signal) || "traces";+ // Signal owns the router: always follow that signal's project binding+ // so the two dropdowns don't fight each other.+ const nextSourceId = resolveSourceForSignal(nextSignal);++ updateWidget(currentWidget.id, {+ config: {+ ...currentConfig,+ sourceId: nextSourceId,+ signal: nextSignal,+ structuredQuery: {+ mode: next.mode,+ query: next.query,+ draftFilters: next.draftFilters,+ },+ },+ });+ };// if (!currentWidget) return null;@@ -109,12 +499,25 @@ export const EditWidgetSheet: React.FC<EditWidgetSheetProps> = ({};const handleRunQuery = async () => {- if (currentWidget?.type !== WidgetType.MARKDOWN && currentWidget?.config && 'query' in currentWidget.config) {+ const cfg = (currentWidget?.config || {}) as Record<string, any>;+ if (+ currentWidget?.type !== WidgetType.MARKDOWN &&+ currentWidget?.config &&+ ("query" in currentWidget.config || "structuredQuery" in currentWidget.config)+ ) {setIsQueryLoading(true);setQueryError(null);try {+ const useStructured =+ !!cfg.structuredQuery &&+ (isExternalSource ||+ usesProjectExternalTraces ||+ showStructuredBuilder);const result = await runQuery(currentWidget.id, {- userQuery: (currentWidget.config as NonMarkdownConfig).query,+ userQuery: useStructured ? undefined : cfg.query,+ structuredQuery: useStructured ? cfg.structuredQuery : undefined,+ signal: cfg.signal || selectedSignal,+ sourceId: cfg.sourceId || selectedSourceId || null,});setQueryResult(result.data);setQueryError(result.err);@@ -558,22 +961,122 @@ export const EditWidgetSheet: React.FC<EditWidgetSheetProps> = ({{currentWidget.type !== WidgetType.MARKDOWN ? (<><div className="space-y-2">- <div className="flex justify-between items-center">- <Label htmlFor="query" className="text-stone-900 dark:text-white">Query</Label>- </div>- <div className="border rounded-md h-[calc(100vh-400px)] bg-white dark:bg-stone-900 border-stone-200 dark:border-stone-700">- <CodeEditor- value={currentWidget.config && 'query' in currentWidget.config ? (currentWidget.config as NonMarkdownConfig).query : ""}- onChange={handleEditorChange}- language={editorLanguage}+ <Label+ htmlFor="widget-source"+ className="text-stone-900 dark:text-white"+ >+ {WIDGET_DATA_SOURCE_LABEL}+ </Label>+ {signalSourceOptions.length > 0 ? (+ <Select+ value={selectedSourceId || undefined}+ onValueChange={handleSourceChange}+ >+ <SelectTrigger+ id="widget-source"+ className="bg-white dark:bg-stone-900 border-stone-200 dark:border-stone-700 dark:text-white"+ >+ <SelectValue+ placeholder={WIDGET_DATA_SOURCE_LABEL}+ />+ </SelectTrigger>+ <SelectContent>+ {signalSourceOptions.map((source) => (+ <SelectItem key={source.id} value={source.id}>+ {source.name} ({source.type})+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ ) : (+ <div className="rounded-md border border-dashed border-stone-300 px-3 py-2 text-sm text-stone-500 dark:border-stone-700 dark:text-stone-400">+ {WIDGET_DATA_SOURCE_EMPTY}+ </div>+ )}+ <p className="text-xs text-stone-500 dark:text-stone-400">+ {WIDGET_DATA_SOURCE_HINT}+ </p>+ {isExternalSource && (+ <p className="text-xs text-stone-500 dark:text-stone-400 flex items-start gap-1">+ <Info className="h-3 w-3 mt-0.5 shrink-0" />+ {WIDGET_DATA_SOURCE_EXTERNAL_SQL_DISABLED}+ </p>+ )}+ {usesProjectExternalTraces && selectedSource && (+ <p className="text-xs text-stone-500 dark:text-stone-400 flex items-start gap-1">+ <Info className="h-3 w-3 mt-0.5 shrink-0" />+ {WIDGET_DATA_SOURCE_PROJECT_TRACES_HINT(+ selectedSource.name,+ selectedSource.type+ )}+ </p>+ )}+ </div>++ {showStructuredBuilder ? (+ <div className="space-y-3">+ <StructuredQueryBuilder+ signals={sourceSignals}+ supportsAggregation={sourceSupportsAggregation}+ capabilityAware={+ isExternalSource || usesProjectExternalTraces+ }+ sourceId={+ selectedSourceId &&+ !selectedSourceId.startsWith(BUILTIN_SOURCE_PREFIX)+ ? selectedSourceId+ : undefined+ }+ value={structuredValue}+ onChange={handleStructuredChange}/>+ <label className="flex items-center gap-2 text-xs text-stone-600 dark:text-stone-300">+ <input+ type="checkbox"+ checked={showGeneratedQuery}+ onChange={(e) =>+ setShowGeneratedQuery(e.target.checked)+ }+ className="rounded border-stone-300 dark:border-stone-600"+ />+ {WIDGET_STRUCTURED_VIEW_GENERATED_QUERY}+ </label>+ {showGeneratedQuery && (+ <div className="border rounded-md h-48 bg-white dark:bg-stone-900 border-stone-200 dark:border-stone-700">+ <CodeEditor+ value={generatedQueryPreview}+ onChange={() => {}}+ language={+ isExternalSource || usesProjectExternalTraces+ ? "json"+ : editorLanguage+ }+ readOnly={true}+ />+ </div>+ )}</div>- </div>+ ) : (+ <div className="space-y-2">+ <div className="flex justify-between items-center">+ <Label htmlFor="query" className="text-stone-900 dark:text-white">Query</Label>+ </div>+ <div className="border rounded-md h-[calc(100vh-400px)] bg-white dark:bg-stone-900 border-stone-200 dark:border-stone-700">+ <CodeEditor+ value={currentWidget.config && 'query' in currentWidget.config ? (currentWidget.config as NonMarkdownConfig).query : ""}+ onChange={handleEditorChange}+ language={editorLanguage}+ readOnly={false}+ />+ </div>+ </div>+ )}<div className="flex justify-end"><Buttonsize="sm"onClick={handleRunQuery}+ disabled={showStructuredBuilder && !hasStructuredQuery && !structuredValue}className="bg-primary hover:bg-primary/90 text-white">Run Querydiff --git a/src/client/src/components/(playground)/manage-dashboard/board-creator/components/structured-query-builder.tsx b/src/client/src/components/(playground)/manage-dashboard/board-creator/components/structured-query-builder.tsxnew file mode 100644index 00000000..07a98408--- /dev/null+++ b/src/client/src/components/(playground)/manage-dashboard/board-creator/components/structured-query-builder.tsx@@ -0,0 +1,1234 @@+"use client";++/**+ * Structured-query builder for external (non-SQL) telemetry sources.+ *+ * Built-in ClickHouse widgets use raw SQL; external sources speak their own+ * query language, so we collect a vendor-agnostic `OpenLITQuery` (minus its+ * time range, which the dashboard filter injects at run time) that+ * `executeStructuredWidgetQuery` dispatches by signal + mode. The output shape+ * matches `WidgetSourceConfig.structuredQuery`.+ */++import React, { useEffect, useId, useMemo, useState } from "react";+import { Check, ChevronsUpDown, Loader2, Plus, Trash2 } from "lucide-react";+import { Button } from "@/components/ui/button";+import { Checkbox } from "@/components/ui/checkbox";+import { Input } from "@/components/ui/input";+import { Label } from "@/components/ui/label";+import {+ Command,+ CommandEmpty,+ CommandGroup,+ CommandInput,+ CommandItem,+ CommandList,+} from "@/components/ui/command";+import {+ Popover,+ PopoverContent,+ PopoverTrigger,+} from "@/components/ui/popover";+import {+ Select,+ SelectContent,+ SelectItem,+ SelectTrigger,+ SelectValue,+} from "@/components/ui/select";+import {+ WIDGET_STRUCTURED_ADD_AGGREGATION,+ WIDGET_STRUCTURED_ADD_FILTER,+ WIDGET_STRUCTURED_AGGREGATIONS_LABEL,+ WIDGET_STRUCTURED_AGGREGATION_UNSUPPORTED,+ WIDGET_STRUCTURED_ALIAS_PLACEHOLDER,+ WIDGET_STRUCTURED_BUILDER_TITLE,+ WIDGET_STRUCTURED_DURATION_PLACEHOLDER,+ WIDGET_STRUCTURED_FIELD_PLACEHOLDER,+ WIDGET_STRUCTURED_FILTERS_LABEL,+ WIDGET_STRUCTURED_FILTER_TARGET_LABEL,+ WIDGET_STRUCTURED_GROUP_BY_LABEL,+ WIDGET_STRUCTURED_GROUP_BY_PLACEHOLDER,+ WIDGET_STRUCTURED_INTERVAL_LABEL,+ WIDGET_STRUCTURED_KEY_PLACEHOLDER,+ WIDGET_STRUCTURED_LIMIT_LABEL,+ WIDGET_STRUCTURED_MODE_AGGREGATE,+ WIDGET_STRUCTURED_MODE_LABEL,+ WIDGET_STRUCTURED_MODE_LIST,+ WIDGET_STRUCTURED_MODE_TIMESERIES,+ WIDGET_STRUCTURED_PREVIOUS_PERIOD_LABEL,+ WIDGET_STRUCTURED_SIGNAL_LABEL,+ WIDGET_STRUCTURED_SORT_ASC,+ WIDGET_STRUCTURED_SORT_DESC,+ WIDGET_STRUCTURED_SORT_FIELD_PLACEHOLDER,+ WIDGET_STRUCTURED_SORT_LABEL,+ WIDGET_STRUCTURED_SPAN_NAME_PLACEHOLDER,+ WIDGET_STRUCTURED_STATUS_PLACEHOLDER,+ WIDGET_STRUCTURED_VALUE_PLACEHOLDER,+} from "@/constants/messages/en";+import useFetchWrapper from "@/utils/hooks/useFetchWrapper";+import { getRequestHeaders } from "@/utils/api";++type StructuredMode = "list" | "aggregate" | "timeseries";+type Signal = "traces" | "logs" | "metrics";+type FilterScope = "span" | "resource" | "log" | "metric";+type FilterTarget = "attribute" | "spanName" | "status" | "duration";+type FilterOp =+ | "exists"+ | "notExists"+ | "eq"+ | "neq"+ | "in"+ | "notIn"+ | "gt"+ | "gte"+ | "lt"+ | "lte"+ | "contains";+type AggregationFn =+ | "count"+ | "sum"+ | "avg"+ | "min"+ | "max"+ | "p50"+ | "p90"+ | "p95"+ | "p99"+ | "cardinality";+type SortDirection = "asc" | "desc";++interface FilterRow {+ target: FilterTarget;+ scope: FilterScope;+ key: string;+ op: FilterOp;+ value: string;+}++interface AggregationRow {+ fn: AggregationFn;+ field?: string;+ as?: string;+}++interface SortRow {+ field: string;+ direction: SortDirection;+}++export interface StructuredQueryValue {+ mode: StructuredMode;+ query: Record<string, any>;+ /** UI-only rows; executable query.filters contains complete filters only. */+ draftFilters?: FilterRow[];+}++interface Props {+ /** Signals the selected source is configured to serve. */+ signals: Signal[];+ /** True when the source can aggregate server-side (from source capabilities). */+ supportsAggregation?: boolean;+ value?: StructuredQueryValue;+ onChange: (value: StructuredQueryValue) => void;+ /**+ * When true, the builder trusts the bound source's live metadata as the sole+ * source of field/group-by options (Grafana getTagKeys pattern) and hides the+ * built-in AI-attribute fallback list, so options always match what the+ * source can actually serve. Defaults to false (built-in ClickHouse), which+ * keeps the curated AI fallback keys for a good out-of-the-box experience.+ */+ capabilityAware?: boolean;+ /**+ * The widget's bound telemetry source id. Threaded into the metadata fetches+ * so field/group-by options come from the source the widget actually queries,+ * not the project's default traces routing.+ */+ sourceId?: string;+}++const inputClass =+ "h-8 bg-white dark:bg-stone-900 border-stone-200 dark:border-stone-700 dark:text-white text-sm";+const triggerClass =+ "h-8 bg-white dark:bg-stone-900 border-stone-200 dark:border-stone-700 dark:text-white text-sm";+const contentClass =+ "bg-white dark:bg-stone-900 border-stone-200 dark:border-stone-700";++const DEFAULT_SIGNAL: Signal = "traces";++/**+ * Curated AI-relevant keys used ONLY as a fallback when the bound source has+ * not (yet) reported live attribute keys, so the dropdowns are never empty for+ * the built-in source. Capability-aware mode (external sources) suppresses+ * these in favor of the source's own reported keys.+ */+const FALLBACK_GROUP_BY_KEYS = [+ "service.name",+ "gen_ai.request.model",+ "gen_ai.system",+ "gen_ai.operation.name",+ "deployment.environment",+] as const;++const STATUS_CODE_OPTIONS = [+ "STATUS_CODE_OK",+ "STATUS_CODE_ERROR",+ "STATUS_CODE_UNSET",+ "Ok",+ "Error",+ "Unset",+] as const;++function uniqueOptions(values: string[]) {+ return Array.from(new Set(values.map((value) => value.trim()).filter(Boolean)))+ .sort((a, b) => a.localeCompare(b));+}++function SuggestionCombobox({+ value,+ options,+ placeholder,+ onChange,+ loading = false,+ multiple = false,+}: {+ value: string;+ options: string[];+ placeholder: string;+ onChange: (value: string) => void;+ loading?: boolean;+ multiple?: boolean;+}) {+ const [open, setOpen] = useState(false);+ const [search, setSearch] = useState("");+ const selected = multiple+ ? value.split(",").map((item) => item.trim()).filter(Boolean)+ : value+ ? [value]+ : [];+ const normalizedSearch = search.trim();+ const visible = options.filter(+ (option) =>+ option.toLowerCase().includes(normalizedSearch.toLowerCase())+ );+ const canAdd =+ Boolean(normalizedSearch) &&+ !options.some((option) => option.toLowerCase() === normalizedSearch.toLowerCase()) &&+ !selected.includes(normalizedSearch);++ const selectValue = (next: string) => {+ if (multiple) {+ onChange(+ (selected.includes(next)+ ? selected.filter((item) => item !== next)+ : uniqueOptions([...selected, next])+ ).join(", ")+ );+ setSearch("");+ return;+ }+ onChange(next);+ setSearch("");+ setOpen(false);+ };++ return (+ <Popover open={open} onOpenChange={setOpen}>+ <PopoverTrigger asChild>+ <Button+ type="button"+ variant="outline"+ role="combobox"+ aria-expanded={open}+ aria-label={placeholder}+ className={`${triggerClass} min-w-[160px] flex-1 justify-between px-2 font-normal`}+ >+ <span className={value ? "truncate" : "truncate text-stone-400"}>+ {value || placeholder}+ </span>+ {loading ? (+ <Loader2 className="ml-2 h-3.5 w-3.5 shrink-0 animate-spin opacity-50" />+ ) : (+ <ChevronsUpDown className="ml-2 h-3.5 w-3.5 shrink-0 opacity-50" />+ )}+ </Button>+ </PopoverTrigger>+ <PopoverContent className="w-[280px] p-0" align="start">+ <Command shouldFilter={false}>+ <CommandInput+ value={search}+ onValueChange={setSearch}+ placeholder={placeholder}+ onKeyDown={(event) => {+ if (event.key === "Enter" && canAdd) {+ event.preventDefault();+ selectValue(normalizedSearch);+ }+ }}+ />+ <CommandList>+ {loading ? (+ <div className="flex items-center gap-2 px-3 py-3 text-xs text-stone-500">+ <Loader2 className="h-3.5 w-3.5 animate-spin" /> Loading values…+ </div>+ ) : null}+ {canAdd ? (+ <CommandItem value={normalizedSearch} onSelect={() => selectValue(normalizedSearch)}>+ <Plus className="mr-2 h-3.5 w-3.5" /> Add "{normalizedSearch}"+ </CommandItem>+ ) : null}+ {!loading && visible.length === 0 && !canAdd ? (+ <CommandEmpty>No values found.</CommandEmpty>+ ) : null}+ {visible.length ? (+ <CommandGroup>+ {visible.map((option) => (+ <CommandItem key={option} value={option} onSelect={() => selectValue(option)}>+ <Check className={`mr-2 h-3.5 w-3.5 ${selected.includes(option) ? "opacity-100" : "opacity-0"}`} />+ <span className="truncate">{option}</span>+ </CommandItem>+ ))}+ </CommandGroup>+ ) : null}+ </CommandList>+ </Command>+ </PopoverContent>+ </Popover>+ );+}++const FILTER_TARGETS: FilterTarget[] = [+ "attribute",+ "spanName",+ "status",+ "duration",+];++const OPS_BY_TARGET: Record<FilterTarget, FilterOp[]> = {+ attribute: [+ "eq",+ "neq",+ "in",+ "notIn",+ "contains",+ "exists",+ "notExists",+ "gt",+ "gte",+ "lt",+ "lte",+ ],+ spanName: ["eq", "in", "contains"],+ status: ["eq", "in"],+ duration: ["gt", "gte", "lt", "lte", "eq"],+};++const AGG_FNS: AggregationFn[] = [+ "count",+ "sum",+ "avg",+ "min",+ "max",+ "p50",+ "p90",+ "p95",+ "p99",+ "cardinality",+];+const FILTER_SCOPES: FilterScope[] = ["span", "resource", "log", "metric"];+const FILTER_OPS: FilterOp[] = [+ "exists",+ "notExists",+ "eq",+ "neq",+ "in",+ "notIn",+ "gt",+ "gte",+ "lt",+ "lte",+ "contains",+];++function defaultOpForTarget(target: FilterTarget): FilterOp {+ return OPS_BY_TARGET[target][0];+}++function isListOp(op: FilterOp): boolean {+ return op === "in" || op === "notIn";+}++function isUnaryOp(op: FilterOp): boolean {+ return op === "exists" || op === "notExists";+}++function needsKey(target: FilterTarget): boolean {+ return target === "attribute";+}++function needsScope(target: FilterTarget): boolean {+ return target === "attribute";+}++function parseFilterValue(op: FilterOp, raw: string): string | string[] | number {+ if (isListOp(op)) {+ return raw+ .split(",")+ .map((v) => v.trim())+ .filter(Boolean);+ }+ return raw;+}++function toFilterRows(filters: unknown): FilterRow[] {+ if (!Array.isArray(filters)) return [];+ return filters+ .filter((f) => {+ if (!f || typeof f !== "object") return false;+ const target = (f as any).target;+ return (+ target === "attribute" ||+ target === "spanName" ||+ target === "status" ||+ target === "duration"+ );+ })+ .map((f) => {+ const r = f as Record<string, any>;+ const target = (r.target as FilterTarget) || "attribute";+ const op = (r.op as FilterOp) || defaultOpForTarget(target);+ const value =+ typeof r.value === "number"+ ? String(r.value)+ : Array.isArray(r.value)+ ? r.value.join(", ")+ : String(r.value ?? "");+ return {+ target,+ scope: (r.scope as FilterScope) || "span",+ key: String(r.key || ""),+ op: FILTER_OPS.includes(op) ? op : defaultOpForTarget(target),+ value,+ };+ });+}++function toAggregationRows(aggregations: unknown): AggregationRow[] {+ if (!Array.isArray(aggregations)) return [];+ return aggregations.map((a) => {+ const r = a as Record<string, any>;+ return {+ fn: (r.fn as AggregationFn) || "count",+ field: r.field ? String(r.field) : undefined,+ as: r.as ? String(r.as) : undefined,+ };+ });+}++function toSortRow(sort: unknown): SortRow | null {+ if (!Array.isArray(sort) || !sort.length) return null;+ const first = sort[0];+ if (!first || typeof first !== "object") return null;+ const field = String((first as any).field || "").trim();+ if (!field) return null;+ const direction =+ (first as any).direction === "desc" ? "desc" : ("asc" as SortDirection);+ return { field, direction };+}++function filterIsComplete(f: FilterRow): boolean {+ if (needsKey(f.target) && !f.key.trim()) return false;+ if (isUnaryOp(f.op)) return true;+ if (f.target === "duration") {+ return Number.isFinite(Number(f.value.trim()));+ }+ return Boolean(f.value.trim());+}++function serializeFilter(f: FilterRow): Record<string, any> {+ const base: Record<string, any> = {+ target: f.target,+ op: f.op,+ };+ if (needsScope(f.target)) base.scope = f.scope;+ if (needsKey(f.target)) base.key = f.key.trim();++ if (isUnaryOp(f.op)) return base;++ if (f.target === "duration") {+ const n = Number(f.value.trim());+ base.value = Number.isFinite(n) ? n : f.value.trim();+ return base;+ }++ base.value = parseFilterValue(f.op, f.value);+ return base;+}++/** Serialize the builder rows back into an OpenLITQuery-shaped object. */+function buildQuery(+ signal: Signal,+ mode: StructuredMode,+ opts: {+ limit?: number;+ interval?: string;+ groupBy: string[];+ aggregations: AggregationRow[];+ filters: FilterRow[];+ sort: SortRow | null;+ includePrevious: boolean;+ }+): Record<string, any> {+ const filters = opts.filters.filter(filterIsComplete).map(serializeFilter);++ const query: Record<string, any> = { signal, aiSelector: signal === "traces" };+ if (filters.length) query.filters = filters;++ if (mode === "list") {+ query.limit = opts.limit && opts.limit > 0 ? opts.limit : 100;+ } else {+ const aggregations = (opts.aggregations.length+ ? opts.aggregations+ : [{ fn: "count" as AggregationFn }]+ ).map((a) => ({+ fn: a.fn,+ ...(a.field ? { field: a.field } : {}),+ ...(a.as ? { as: a.as } : {}),+ }));+ query.aggregations = aggregations;+ if (mode === "aggregate") {+ const groupBy = opts.groupBy.filter(Boolean);+ if (groupBy.length) query.groupBy = groupBy;+ }+ if (mode === "timeseries") {+ query.interval = opts.interval?.trim() || "1h";+ }+ if (opts.includePrevious) {+ query.includePrevious = true;+ }+ if (opts.sort?.field.trim()) {+ query.sort = [+ {+ field: opts.sort.field.trim(),+ direction: opts.sort.direction === "desc" ? "desc" : "asc",+ },+ ];+ }+ }++ return query;+}++function uniqueSorted(values: string[]): string[] {+ return Array.from(new Set(values.filter(Boolean))).sort((a, b) =>+ a.localeCompare(b)+ );+}++function relativeLast24h() {+ const end = new Date();+ const start = new Date(end.getTime() - 24 * 60 * 60 * 1000);+ return {+ start: start.toISOString(),+ end: end.toISOString(),+ type: "relative" as const,+ };+}++export const StructuredQueryBuilder: React.FC<Props> = ({+ signals,+ supportsAggregation = true,+ value,+ onChange,+ capabilityAware = false,+ sourceId,+}) => {+ const query = (value?.query || {}) as Record<string, any>;+ const availableSignals = signals.length ? signals : [DEFAULT_SIGNAL];+ const signal = (query.signal as Signal) || availableSignals[0];+ const mode: StructuredMode = value?.mode || "timeseries";++ const groupBy: string[] = Array.isArray(query.groupBy)+ ? query.groupBy.map(String)+ : [];+ const aggregations = toAggregationRows(query.aggregations);+ const filters = toFilterRows(value?.draftFilters ?? query.filters);+ const limit = typeof query.limit === "number" ? query.limit : undefined;+ const interval = typeof query.interval === "string" ? query.interval : "";+ const sort = toSortRow(query.sort);+ const includePrevious = Boolean(query.includePrevious);++ const listId = useId();++ const [spanAttributeKeys, setSpanAttributeKeys] = useState<string[]>([]);+ const [resourceAttributeKeys, setResourceAttributeKeys] = useState<string[]>(+ []+ );+ const [spanNames, setSpanNames] = useState<string[]>([]);+ const [models, setModels] = useState<string[]>([]);+ const [providers, setProviders] = useState<string[]>([]);+ const [applicationNames, setApplicationNames] = useState<string[]>([]);+ const [fieldValues, setFieldValues] = useState<Record<string, string[]>>({});+ const [fieldValuesLoading, setFieldValuesLoading] = useState<Record<string, boolean>>({});++ const { fireRequest: fireConfigRequest } = useFetchWrapper();+ const { fireRequest: fireAttrKeysRequest } = useFetchWrapper();++ useEffect(() => {+ const timeLimit = relativeLast24h();+ const body = JSON.stringify(+ sourceId ? { timeLimit, sourceId } : { timeLimit }+ );+ const configUrl =+ signal === "traces"+ ? "/api/telemetry/request/config"+ : `/api/telemetry/${signal}/config`;+ const attributeKeysUrl =+ signal === "traces"+ ? "/api/telemetry/request/attribute-keys"+ : `/api/telemetry/${signal}/attribute-keys`;++ fireConfigRequest({+ body,+ requestType: "POST",+ url: configUrl,+ successCb: (resp) => {+ const row = Array.isArray(resp?.data) ? resp.data[0] : resp?.data ?? resp;+ if (!row || typeof row !== "object") return;+ setModels(+ Array.isArray((row as any).models)+ ? (row as any).models.map(String)+ : []+ );+ setProviders(+ Array.isArray((row as any).providers)+ ? (row as any).providers.map(String)+ : []+ );+ setApplicationNames(+ Array.isArray((row as any).applicationNames)+ ? (row as any).applicationNames.map(String)+ : []+ );+ setSpanNames(+ Array.isArray((row as any).spanNames)+ ? (row as any).spanNames.map(String)+ : []+ );+ },+ failureCb: () => {+ /* metadata is optional for the builder */+ },+ });++ fireAttrKeysRequest({+ body,+ requestType: "POST",+ url: attributeKeysUrl,+ successCb: (resp) => {+ if (!resp || typeof resp !== "object") return;+ setSpanAttributeKeys(+ uniqueOptions([+ ...((resp as any).spanAttributeKeys || []),+ ...((resp as any).logAttributeKeys || []),+ ...((resp as any).metricAttributeKeys || []),+ ].map(String))+ );+ setResourceAttributeKeys(+ Array.isArray((resp as any).resourceAttributeKeys)+ ? (resp as any).resourceAttributeKeys.map(String)+ : []+ );+ },+ failureCb: () => {+ /* metadata is optional for the builder */+ },+ });+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [signal, sourceId]);++ const lookupFields = useMemo(+ () =>+ uniqueOptions(+ filters+ .map((filter) => {+ if (filter.target === "attribute") {+ return signal === "traces" && filter.key+ ? `${filter.scope}:${filter.key}`+ : filter.key;+ }+ if (filter.target === "spanName") {+ return signal === "metrics" ? "MetricName" : signal === "logs" ? "Body" : "SpanName";+ }+ if (filter.target === "status") {+ return signal === "logs" ? "SeverityText" : "StatusCode";+ }+ return "";+ })+ .filter(Boolean)+ ),+ [filters, signal]+ );++ useEffect(() => {+ const timeLimit = relativeLast24h();+ for (const field of lookupFields) {+ const cacheKey = `${signal}:${sourceId || "binding"}:${field}`;+ if (fieldValues[cacheKey] || fieldValuesLoading[cacheKey]) continue;+ setFieldValuesLoading((current) => ({ ...current, [cacheKey]: true }));+ fetch("/api/telemetry/field-values", {+ method: "POST",+ headers: getRequestHeaders({ "Content-Type": "application/json" }),+ body: JSON.stringify({+ signal,+ field,+ timeLimit,+ ...(sourceId ? { sourceId } : {}),+ }),+ })+ .then((response) => (response.ok ? response.json() : { values: [] }))+ .then((payload) => {+ setFieldValues((current) => ({+ ...current,+ [cacheKey]: Array.isArray(payload?.values)+ ? uniqueOptions(payload.values.map(String))+ : [],+ }));+ })+ .catch(() => {+ setFieldValues((current) => ({ ...current, [cacheKey]: [] }));+ })+ .finally(() => {+ setFieldValuesLoading((current) => ({ ...current, [cacheKey]: false }));+ });+ }+ }, [fieldValues, fieldValuesLoading, lookupFields, signal, sourceId]);++ // Dynamic-first (Grafana getTagKeys): options come from the source's live+ // attribute keys. The curated fallback is added only in non-capability-aware+ // mode (built-in) or when the source has reported nothing yet, so dropdowns+ // are never empty but external sources show exactly what they can serve.+ const dynamicKeys = useMemo(+ () => uniqueSorted([...spanAttributeKeys, ...resourceAttributeKeys]),+ [spanAttributeKeys, resourceAttributeKeys]+ );++ const mergedKeyOptions = useMemo(() => {+ const includeFallback = !capabilityAware || dynamicKeys.length === 0;+ return uniqueSorted([+ ...(includeFallback ? FALLBACK_GROUP_BY_KEYS : []),+ ...dynamicKeys,+ ]);+ }, [dynamicKeys, capabilityAware]);++ const attributeKeyOptions = mergedKeyOptions;+ const groupByOptions = mergedKeyOptions;+ const sortFieldOptions = useMemo(+ () =>+ uniqueOptions([+ ...groupByOptions,+ ...groupBy,+ ...aggregations.flatMap((aggregation) => [+ aggregation.as || "",+ aggregation.field || "",+ ]),+ ]),+ [groupByOptions, groupBy, aggregations]+ );++ const valueSuggestions = useMemo(() => {+ return uniqueSorted([+ ...models,+ ...providers,+ ...applicationNames,+ ...spanNames,+ ]);+ }, [models, providers, applicationNames, spanNames]);++ const emit = (+ nextMode: StructuredMode,+ nextSignal: Signal,+ overrides: Partial<{+ limit: number;+ interval: string;+ groupBy: string[];+ aggregations: AggregationRow[];+ filters: FilterRow[];+ sort: SortRow | null;+ includePrevious: boolean;+ }> = {}+ ) => {+ onChange({+ mode: nextMode,+ query: buildQuery(nextSignal, nextMode, {+ limit: overrides.limit ?? limit,+ interval: overrides.interval ?? interval,+ groupBy: overrides.groupBy ?? groupBy,+ aggregations: overrides.aggregations ?? aggregations,+ filters: overrides.filters ?? filters,+ sort: overrides.sort !== undefined ? overrides.sort : sort,+ includePrevious:+ overrides.includePrevious !== undefined+ ? overrides.includePrevious+ : includePrevious,+ }),+ draftFilters: overrides.filters ?? filters,+ });+ };++ const aggregateBlocked = !supportsAggregation && mode !== "list";+ const showAggregateExtras = mode === "aggregate" || mode === "timeseries";++ const updateFilter = (idx: number, patch: Partial<FilterRow>) => {+ const next = [...filters];+ const current = filters[idx];+ const merged = { ...current, ...patch };+ if (patch.target && patch.target !== current.target) {+ merged.op = defaultOpForTarget(patch.target);+ if (patch.target !== "attribute") {+ merged.key = "";+ merged.scope = "span";+ }+ if (patch.target === "duration" && !/^\d/.test(merged.value)) {+ merged.value = "";+ }+ }+ if (patch.op && isUnaryOp(patch.op)) {+ merged.value = "";+ }+ next[idx] = merged;+ emit(mode, signal, { filters: next });+ };++ const valuePlaceholder = (f: FilterRow): string => {+ if (f.target === "duration") return WIDGET_STRUCTURED_DURATION_PLACEHOLDER;+ if (f.target === "status") return WIDGET_STRUCTURED_STATUS_PLACEHOLDER;+ if (f.target === "spanName") return WIDGET_STRUCTURED_SPAN_NAME_PLACEHOLDER;+ return WIDGET_STRUCTURED_VALUE_PLACEHOLDER;+ };++ const lookupFieldFor = (filter: FilterRow) => {+ if (filter.target === "attribute") {+ return signal === "traces" && filter.key+ ? `${filter.scope}:${filter.key}`+ : filter.key;+ }+ if (filter.target === "spanName") {+ return signal === "metrics" ? "MetricName" : signal === "logs" ? "Body" : "SpanName";+ }+ if (filter.target === "status") return signal === "logs" ? "SeverityText" : "StatusCode";+ return "";+ };++ const fetchedValuesFor = (filter: FilterRow) => {+ const field = lookupFieldFor(filter);+ const cacheKey = `${signal}:${sourceId || "binding"}:${field}`;+ return {+ values: uniqueOptions([+ ...(fieldValues[cacheKey] || []),+ ...(filter.target === "status" ? STATUS_CODE_OPTIONS : []),+ ...(filter.target === "spanName" ? spanNames : []),+ ...(filter.target === "attribute" ? valueSuggestions : []),+ ]),+ loading: Boolean(fieldValuesLoading[cacheKey]),+ };+ };++ return (+ <div className="space-y-3 rounded-md border border-stone-200 dark:border-stone-700 p-3 bg-stone-50 dark:bg-stone-900/40">+ <Label className="text-stone-900 dark:text-white text-sm font-medium">+ {WIDGET_STRUCTURED_BUILDER_TITLE}+ </Label>+++ <div className="grid grid-cols-2 gap-3">+ <div className="space-y-1">+ <Label className="text-xs text-stone-500 dark:text-stone-400">+ {WIDGET_STRUCTURED_SIGNAL_LABEL}+ </Label>+ <Select+ value={signal}+ onValueChange={(v) => emit(mode, v as Signal)}+ >+ <SelectTrigger className={triggerClass}>+ <SelectValue />+ </SelectTrigger>+ <SelectContent className={contentClass}>+ {availableSignals.map((s) => (+ <SelectItem key={s} value={s} className="dark:text-white">+ {s}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ </div>++ <div className="space-y-1">+ <Label className="text-xs text-stone-500 dark:text-stone-400">+ {WIDGET_STRUCTURED_MODE_LABEL}+ </Label>+ <Select+ value={mode}+ onValueChange={(v) => emit(v as StructuredMode, signal)}+ >+ <SelectTrigger className={triggerClass}>+ <SelectValue />+ </SelectTrigger>+ <SelectContent className={contentClass}>+ <SelectItem value="list" className="dark:text-white">+ {WIDGET_STRUCTURED_MODE_LIST}+ </SelectItem>+ <SelectItem value="aggregate" className="dark:text-white">+ {WIDGET_STRUCTURED_MODE_AGGREGATE}+ </SelectItem>+ <SelectItem value="timeseries" className="dark:text-white">+ {WIDGET_STRUCTURED_MODE_TIMESERIES}+ </SelectItem>+ </SelectContent>+ </Select>+ </div>+ </div>++ {aggregateBlocked && (+ <p className="text-xs text-amber-600 dark:text-amber-400">+ {WIDGET_STRUCTURED_AGGREGATION_UNSUPPORTED}+ </p>+ )}++ {mode === "list" && (+ <div className="space-y-1">+ <Label className="text-xs text-stone-500 dark:text-stone-400">+ {WIDGET_STRUCTURED_LIMIT_LABEL}+ </Label>+ <Input+ type="number"+ min={1}+ value={limit ?? 100}+ onChange={(e) =>+ emit(mode, signal, { limit: Number(e.target.value) || 1 })+ }+ className={inputClass}+ />+ </div>+ )}++ {mode === "timeseries" && (+ <div className="space-y-1">+ <Label className="text-xs text-stone-500 dark:text-stone-400">+ {WIDGET_STRUCTURED_INTERVAL_LABEL}+ </Label>+ <Input+ value={interval}+ placeholder="1h"+ onChange={(e) => emit(mode, signal, { interval: e.target.value })}+ className={inputClass}+ />+ </div>+ )}++ {mode === "aggregate" && (+ <div className="space-y-1">+ <Label className="text-xs text-stone-500 dark:text-stone-400">+ {WIDGET_STRUCTURED_GROUP_BY_LABEL}+ </Label>+ <SuggestionCombobox+ value={groupBy.join(", ")}+ placeholder={WIDGET_STRUCTURED_GROUP_BY_PLACEHOLDER}+ options={groupByOptions}+ multiple+ onChange={(nextValue) =>+ emit(mode, signal, {+ groupBy: nextValue+ .split(",")+ .map((v) => v.trim())+ .filter(Boolean),+ })+ }+ />+ </div>+ )}++ {showAggregateExtras && (+ <div className="space-y-2">+ <div className="flex items-center justify-between">+ <Label className="text-xs text-stone-500 dark:text-stone-400">+ {WIDGET_STRUCTURED_AGGREGATIONS_LABEL}+ </Label>+ <Button+ type="button"+ size="sm"+ variant="outline"+ className="h-7 px-2 text-xs border-stone-200 dark:border-stone-700 dark:text-white"+ onClick={() =>+ emit(mode, signal, {+ aggregations: [...aggregations, { fn: "count" }],+ })+ }+ >+ <Plus className="h-3 w-3 mr-1" />+ {WIDGET_STRUCTURED_ADD_AGGREGATION}+ </Button>+ </div>+ {aggregations.map((agg, idx) => (+ <div key={idx} className="flex items-center gap-2">+ <Select+ value={agg.fn}+ onValueChange={(v) => {+ const next = [...aggregations];+ next[idx] = { ...agg, fn: v as AggregationFn };+ emit(mode, signal, { aggregations: next });+ }}+ >+ <SelectTrigger className={`${triggerClass} w-28`}>+ <SelectValue />+ </SelectTrigger>+ <SelectContent className={contentClass}>+ {AGG_FNS.map((fn) => (+ <SelectItem key={fn} value={fn} className="dark:text-white">+ {fn}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ <SuggestionCombobox+ value={agg.field || ""}+ placeholder={WIDGET_STRUCTURED_FIELD_PLACEHOLDER}+ options={attributeKeyOptions}+ onChange={(field) => {+ const next = [...aggregations];+ next[idx] = { ...agg, field };+ emit(mode, signal, { aggregations: next });+ }}+ />+ <Input+ value={agg.as || ""}+ placeholder={WIDGET_STRUCTURED_ALIAS_PLACEHOLDER}+ onChange={(e) => {+ const next = [...aggregations];+ next[idx] = { ...agg, as: e.target.value };+ emit(mode, signal, { aggregations: next });+ }}+ className={`${inputClass} w-24`}+ />+ <Button+ type="button"+ size="icon"+ variant="ghost"+ className="h-8 w-8 shrink-0 text-stone-500 hover:text-red-500"+ onClick={() =>+ emit(mode, signal, {+ aggregations: aggregations.filter((_, i) => i !== idx),+ })+ }+ >+ <Trash2 className="h-3.5 w-3.5" />+ </Button>+ </div>+ ))}++ <div className="space-y-1 pt-1">+ <Label className="text-xs text-stone-500 dark:text-stone-400">+ {WIDGET_STRUCTURED_SORT_LABEL}+ </Label>+ <div className="flex items-center gap-2">+ <SuggestionCombobox+ value={sort?.field || ""}+ placeholder={WIDGET_STRUCTURED_SORT_FIELD_PLACEHOLDER}+ options={sortFieldOptions}+ onChange={(field) => {+ emit(mode, signal, {+ sort: field.trim()+ ? {+ field,+ direction: sort?.direction || "asc",+ }+ : null,+ });+ }}+ />+ <Select+ value={sort?.direction || "asc"}+ onValueChange={(v) => {+ emit(mode, signal, {+ sort: {+ field: sort?.field || "",+ direction: v as SortDirection,+ },+ });+ }}+ disabled={!sort?.field?.trim()}+ >+ <SelectTrigger className={`${triggerClass} w-32`}>+ <SelectValue />+ </SelectTrigger>+ <SelectContent className={contentClass}>+ <SelectItem value="asc" className="dark:text-white">+ {WIDGET_STRUCTURED_SORT_ASC}+ </SelectItem>+ <SelectItem value="desc" className="dark:text-white">+ {WIDGET_STRUCTURED_SORT_DESC}+ </SelectItem>+ </SelectContent>+ </Select>+ </div>+ </div>++ <div className="flex items-center gap-2 pt-1">+ <Checkbox+ id={`${listId}-prev`}+ checked={includePrevious}+ onCheckedChange={(checked) =>+ emit(mode, signal, { includePrevious: checked === true })+ }+ className="border-stone-300 dark:border-stone-600"+ />+ <Label+ htmlFor={`${listId}-prev`}+ className="text-xs text-stone-600 dark:text-stone-300 cursor-pointer"+ >+ {WIDGET_STRUCTURED_PREVIOUS_PERIOD_LABEL}+ </Label>+ </div>+ </div>+ )}++ <div className="space-y-2">+ <div className="flex items-center justify-between">+ <Label className="text-xs text-stone-500 dark:text-stone-400">+ {WIDGET_STRUCTURED_FILTERS_LABEL}+ </Label>+ <Button+ type="button"+ size="sm"+ variant="outline"+ className="h-7 px-2 text-xs border-stone-200 dark:border-stone-700 dark:text-white"+ onClick={() =>+ emit(mode, signal, {+ filters: [+ ...filters,+ {+ target: "attribute",+ scope: "span",+ key: "",+ op: "eq",+ value: "",+ },+ ],+ })+ }+ >+ <Plus className="h-3 w-3 mr-1" />+ {WIDGET_STRUCTURED_ADD_FILTER}+ </Button>+ </div>+ {filters.map((f, idx) => {+ const ops = OPS_BY_TARGET[f.target] || FILTER_OPS;+ const showScope = needsScope(f.target);+ const showKey = needsKey(f.target);+ const showValue = !isUnaryOp(f.op);+ const fetchedValues = fetchedValuesFor(f);+ return (+ <div key={idx} className="flex flex-wrap items-center gap-2">+ <Select+ value={f.target}+ onValueChange={(v) =>+ updateFilter(idx, { target: v as FilterTarget })+ }+ >+ <SelectTrigger+ className={`${triggerClass} w-28`}+ aria-label={WIDGET_STRUCTURED_FILTER_TARGET_LABEL}+ >+ <SelectValue />+ </SelectTrigger>+ <SelectContent className={contentClass}>+ {FILTER_TARGETS.map((t) => (+ <SelectItem key={t} value={t} className="dark:text-white">+ {t}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ {showScope ? (+ <Select+ value={f.scope}+ onValueChange={(v) =>+ updateFilter(idx, { scope: v as FilterScope })+ }+ >+ <SelectTrigger className={`${triggerClass} w-24`}>+ <SelectValue />+ </SelectTrigger>+ <SelectContent className={contentClass}>+ {FILTER_SCOPES.map((s) => (+ <SelectItem key={s} value={s} className="dark:text-white">+ {s}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ ) : null}+ {showKey ? (+ <SuggestionCombobox+ value={f.key}+ placeholder={WIDGET_STRUCTURED_KEY_PLACEHOLDER}+ options={attributeKeyOptions}+ onChange={(key) => updateFilter(idx, { key, value: "" })}+ />+ ) : null}+ <Select+ value={ops.includes(f.op) ? f.op : ops[0]}+ onValueChange={(v) => updateFilter(idx, { op: v as FilterOp })}+ >+ <SelectTrigger className={`${triggerClass} w-28`}>+ <SelectValue />+ </SelectTrigger>+ <SelectContent className={contentClass}>+ {ops.map((op) => (+ <SelectItem key={op} value={op} className="dark:text-white">+ {op}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ {showValue && f.target !== "duration" ? (+ <SuggestionCombobox+ value={f.value}+ placeholder={valuePlaceholder(f)}+ options={fetchedValues.values}+ loading={fetchedValues.loading}+ multiple={isListOp(f.op)}+ onChange={(nextValue) => updateFilter(idx, { value: nextValue })}+ />+ ) : null}+ {showValue && f.target === "duration" ? (+ <Input+ value={f.value}+ placeholder={valuePlaceholder(f)}+ type="number"+ onChange={(e) => updateFilter(idx, { value: e.target.value })}+ className={inputClass}+ />+ ) : null}+ <Button+ type="button"+ size="icon"+ variant="ghost"+ className="h-8 w-8 shrink-0 text-stone-500 hover:text-red-500"+ onClick={() =>+ emit(mode, signal, {+ filters: filters.filter((_, i) => i !== idx),+ })+ }+ >+ <Trash2 className="h-3.5 w-3.5" />+ </Button>+ </div>+ );+ })}+ </div>+ </div>+ );+};++export default StructuredQueryBuilder;diff --git a/src/client/src/components/(playground)/manage-dashboard/board-creator/context/dashboard-context.tsx b/src/client/src/components/(playground)/manage-dashboard/board-creator/context/dashboard-context.tsxindex 9d093a0d..2c3c9edd 100644--- a/src/client/src/components/(playground)/manage-dashboard/board-creator/context/dashboard-context.tsx+++ b/src/client/src/components/(playground)/manage-dashboard/board-creator/context/dashboard-context.tsx@@ -43,6 +43,8 @@ interface DashboardContextType {) => Promise<{ data: any; err: string | null }>;handleWidgetCrud?: (updates: Partial<Widget>) => Promise<Widget>;widgetData: Record<string, any>;+ widgetLoading: Record<string, boolean>;+ widgetError: Record<string, string | null>;updateWidgetData: (widgetId: string, data: any) => void;clearWidgetData: (widgetId: string) => void;loadWidgetData: (widgetId: string) => Promise<void>;@@ -84,24 +86,46 @@ export const DashboardProvider: React.FC<DashboardProviderProps> = ({initialConfig?.widgets || {});const [widgetData, setWidgetData] = useState<Record<string, any>>({});+ const [widgetLoading, setWidgetLoading] = useState<Record<string, boolean>>(+ {}+ );+ const [widgetError, setWidgetError] = useState<Record<string, string | null>>(+ {}+ );const [editingWidget, setEditingWidget] = useState<string | null>(null);const [isEditing, setIsEditing] = useState(false);const posthog = usePostHog();- // Load widget data+ // Load widget data. Tracks per-widget loading/error so the renderer can show+ // a spinner while querying and surface failures instead of a misleading `0`.const loadWidgetData = async (widgetId: string) => {const widget = widgets[widgetId] as StatCardWidget | BarChartWidget | LineChartWidget | PieChartWidget | TableWidget;- if (widget.type === WidgetType.MARKDOWN || !widget?.config?.query) return;+ const hasQuery =+ !!(widget?.config as { query?: string } | undefined)?.query ||+ !!(widget?.config as { structuredQuery?: unknown } | undefined)+ ?.structuredQuery;+ if (widget.type === WidgetType.MARKDOWN || !hasQuery) return;+ if (!runQuery) return;+ setWidgetLoading((prev) => ({ ...prev, [widgetId]: true }));+ setWidgetError((prev) => ({ ...prev, [widgetId]: null }));try {- if (runQuery) {- const { data } = await runQuery(widgetId, {});- updateWidgetData(widgetId, data);+ const { data, err } = await runQuery(widgetId, {});+ if (err) {+ setWidgetError((prev) => ({ ...prev, [widgetId]: String(err) }));+ } else {+ updateWidgetData(widgetId, data);+ }+ } catch (error) {+ // Use separate parameters to prevent log injection+ console.error("Failed to load data for widget:", widgetId, error);+ setWidgetError((prev) => ({+ ...prev,+ [widgetId]: error instanceof Error ? error.message : String(error),+ }));+ } finally {+ setWidgetLoading((prev) => ({ ...prev, [widgetId]: false }));}- } catch (error) {- // Use separate parameters to prevent log injection- console.error('Failed to load data for widget:', widgetId, error);- }};// Update widget data@@ -119,6 +143,16 @@ export const DashboardProvider: React.FC<DashboardProviderProps> = ({delete newData[widgetId];return newData;});+ setWidgetLoading((prev) => {+ const next = { ...prev };+ delete next[widgetId];+ return next;+ });+ setWidgetError((prev) => {+ const next = { ...prev };+ delete next[widgetId];+ return next;+ });};// Update a widget@@ -289,6 +323,8 @@ export const DashboardProvider: React.FC<DashboardProviderProps> = ({runQuery: handleRunQuery,handleWidgetCrud,widgetData,+ widgetLoading,+ widgetError,updateWidgetData,clearWidgetData,loadWidgetData,diff --git a/src/client/src/components/(playground)/manage-dashboard/board-creator/utils/formatters.ts b/src/client/src/components/(playground)/manage-dashboard/board-creator/utils/formatters.tsindex 810335e3..3a3bf851 100644--- a/src/client/src/components/(playground)/manage-dashboard/board-creator/utils/formatters.ts+++ b/src/client/src/components/(playground)/manage-dashboard/board-creator/utils/formatters.ts@@ -28,6 +28,25 @@ export const formatPercentage = (value: number, locale = "en-US", digits = 1): s}).format(value / 100)}+/** Compact large dashboard values (1.2K, 3.4M) while retaining useful decimals. */+export const formatCompactNumber = (+ value: unknown,+ maximumFractionDigits = 2,+ locale = "en-US"+): string => {+ const numeric = typeof value === "number" ? value : Number(value);+ if (!Number.isFinite(numeric)) return value == null ? "0" : String(value);++ const absolute = Math.abs(numeric);+ return new Intl.NumberFormat(locale, {+ notation: absolute >= 1_000 ? "compact" : "standard",+ compactDisplay: "short",+ maximumFractionDigits: absolute > 0 && absolute < 1+ ? Math.max(maximumFractionDigits, 4)+ : maximumFractionDigits,+ }).format(numeric);+}+/*** Title cases a string (e.g. "hello_world" -> "Hello World")* @param str String to transform@@ -36,4 +55,3 @@ export const formatPercentage = (value: number, locale = "en-US", digits = 1): sexport const toTitleCase = (str: string): string => {return str.replace(/_/g, " ").replace(/\b\w/g, (l) => l.toUpperCase())}-diff --git a/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/area-chart-widget.tsx b/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/area-chart-widget.tsxindex 77646e87..080b4c51 100644--- a/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/area-chart-widget.tsx+++ b/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/area-chart-widget.tsx@@ -19,6 +19,18 @@ const AreaChartWidgetComponent: React.FC<AreaChartProps> = ({widget,data,}) => {+ const updatedData = (data || []).map((item) => {+ const next: Record<string, unknown> = {+ ...item,+ [widget.properties.xAxis]: item[widget.properties.xAxis] ?? "",+ };+ for (const yAxis of widget.properties.yAxes || []) {+ const n = Number(item[yAxis.key]);+ next[yAxis.key] = Number.isFinite(n) ? n : 0;+ }+ return next;+ });+return (<div className="flex flex-col h-full"><ResponsiveContainer width="100%" height="100%">@@ -26,7 +38,7 @@ const AreaChartWidgetComponent: React.FC<AreaChartProps> = ({<AreaChartwidth={500}height={400}- data={data || []}+ data={updatedData}margin={{top: 10,right: 30,@@ -39,6 +51,8 @@ const AreaChartWidgetComponent: React.FC<AreaChartProps> = ({dataKey={widget.properties.xAxis}className="text-xs stroke-stone-300"stroke="currentColor"+ interval="preserveStartEnd"+ minTickGap={24}/><YAxisclassName="text-xs stroke-stone-300"diff --git a/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/bar-chart-widget.tsx b/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/bar-chart-widget.tsxindex 5c5ba978..04de1166 100644--- a/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/bar-chart-widget.tsx+++ b/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/bar-chart-widget.tsx@@ -17,11 +17,11 @@ interface BarChartProps {const BarChartWidgetComponent: React.FC<BarChartProps> = ({ widget, data }) => {const updatedData = data?.map((item) => {- const yValue = parseFloat(item[widget.properties.yAxis]);+ const yValue = parseFloat(String(item[widget.properties.yAxis] ?? ""));return {...item,- [widget.properties.xAxis]: item[widget.properties.xAxis],- [widget.properties.yAxis]: isNaN(yValue) ? 0 : yValue,+ [widget.properties.xAxis]: item[widget.properties.xAxis] ?? "",+ [widget.properties.yAxis]: Number.isFinite(yValue) ? yValue : 0,};}) || [];@@ -43,6 +43,8 @@ const BarChartWidgetComponent: React.FC<BarChartProps> = ({ widget, data }) => {dataKey={widget.properties.xAxis}className="text-xs stroke-stone-300"stroke="currentColor"+ interval={0}+ minTickGap={8}/><YAxisdataKey={widget.properties.yAxis}diff --git a/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/line-chart-widget.tsx b/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/line-chart-widget.tsxindex c167c3d4..8a6effd7 100644--- a/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/line-chart-widget.tsx+++ b/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/line-chart-widget.tsx@@ -15,14 +15,19 @@ interface LineChartProps {data?: any[];}+function toFiniteNumber(value: unknown): number {+ const n = typeof value === "number" ? value : parseFloat(String(value ?? ""));+ return Number.isFinite(n) ? n : 0;+}+const LineChartWidgetComponent: React.FC<LineChartProps> = ({widget,data,}) => {const updatedData = data?.map((item) => ({...item,- [widget.properties.xAxis]: item[widget.properties.xAxis],- [widget.properties.yAxis]: parseFloat(item[widget.properties.yAxis]),+ [widget.properties.xAxis]: item[widget.properties.xAxis] ?? "",+ [widget.properties.yAxis]: toFiniteNumber(item[widget.properties.yAxis]),})) || [];return (@@ -42,6 +47,8 @@ const LineChartWidgetComponent: React.FC<LineChartProps> = ({dataKey={widget.properties.xAxis}className="text-xs stroke-stone-300"stroke="currentColor"+ interval="preserveStartEnd"+ minTickGap={24}/><YAxisdataKey={widget.properties.yAxis}diff --git a/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/stat-card-widget.tsx b/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/stat-card-widget.tsxindex 79e36762..0fc1a8e7 100644--- a/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/stat-card-widget.tsx+++ b/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/stat-card-widget.tsx@@ -3,6 +3,7 @@ import type { StatCardWidget } from "../types";import { TrendingDown } from "lucide-react";import { TrendingUp } from "lucide-react";import { isNil } from "lodash";+import { formatCompactNumber } from "../utils/formatters";interface StatCardProps {widget: StatCardWidget;@@ -10,14 +11,14 @@ interface StatCardProps {}const StatCardWidget: React.FC<StatCardProps> = ({ widget, data }) => {- let value = "";+ let value: unknown = "";let trend: number = 0;try {value = (widget.properties.value || "").split(".").reduce((acc: any, curr: string) => acc?.[curr], data);- value = (value || 0).toString();+ value = value || 0;trend = (widget.properties.trend || "").split(".").reduce((acc: any, curr: string) => acc?.[curr], data);@@ -25,12 +26,18 @@ const StatCardWidget: React.FC<StatCardProps> = ({ widget, data }) => {} catch (error) {console.error(error);}+ const displayValue = formatCompactNumber(value);+ const fullValue = `${widget.properties.prefix}${String(value)}${widget.properties.suffix}`;return (- <div className="flex flex-col justify-center items-center h-full">- <div className={`text-3xl font-bold`} style={{ color: widget.properties.color }}>+ <div className="flex min-w-0 flex-col justify-center items-center h-full overflow-hidden px-2">+ <div+ className="max-w-full truncate text-center text-3xl font-bold leading-tight"+ style={{ color: widget.properties.color }}+ title={fullValue}+ >{widget.properties.prefix}- {value}+ {displayValue}{widget.properties.suffix}</div>{!isNil(trend) && (@@ -41,7 +48,7 @@ const StatCardWidget: React.FC<StatCardProps> = ({ widget, data }) => {}`}>{widget.properties.trendPrefix}- {Math.abs(trend)}+ {formatCompactNumber(Math.abs(trend))}{widget.properties.trendSuffix}{trend > 0 ? <TrendingUp className="w-4 h-4" /> : <TrendingDown className="w-4 h-4" />}</div>diff --git a/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/widget-renderer.tsx b/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/widget-renderer.tsxindex 96622541..a7c67f72 100644--- a/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/widget-renderer.tsx+++ b/src/client/src/components/(playground)/manage-dashboard/board-creator/widgets/widget-renderer.tsx@@ -1,5 +1,5 @@import React, { useMemo, memo, useEffect } from "react";-import { Edit, Trash } from "lucide-react";+import { Edit, Trash, Loader2 } from "lucide-react";import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";import { WidgetType, type WidgetRendererProps } from "../types";import { SUPPORTED_WIDGETS } from "../constants";@@ -20,12 +20,15 @@ const WidgetRenderer: React.FC<WidgetRendererProps> = ({onRemove,runFilters,}) => {- const { widgetData, loadWidgetData } = useDashboard();+ const { widgetData, widgetLoading, widgetError, loadWidgetData } =+ useDashboard();- // Load data when not in edit mode+ // Load data when not in edit mode (SQL or structured-only widgets).useEffect(() => {- // @ts-ignore TODO: fix this- if (widget?.config?.query) {+ const cfg = widget?.config as+ | { query?: string; structuredQuery?: unknown }+ | undefined;+ if (cfg?.query || cfg?.structuredQuery) {loadWidgetData(widget.id);}}, [runFilters]);@@ -38,6 +41,11 @@ const WidgetRenderer: React.FC<WidgetRendererProps> = ({};const widgetEvaluatedData = widgetData[widget.id];+ const isWidgetLoading = !!widgetLoading[widget.id];+ const widgetLoadError = widgetError[widget.id];+ // Show the skeleton only on the first load (no data yet); on refetch keep+ // the last result visible and revalidate in the background.+ const showLoader = isWidgetLoading && widgetEvaluatedData === undefined;const WidgetComponent = useMemo(() => {switch (widget.type) {@@ -96,9 +104,28 @@ const WidgetRenderer: React.FC<WidgetRendererProps> = ({</div>)}</CardHeader>- <CardContent className="flex-grow overflow-auto">- {WidgetComponent && (- <WidgetComponent data={widgetEvaluatedData} widget={widget as any} />+ <CardContent className="flex-grow overflow-auto relative">+ {showLoader ? (+ <div className="flex h-full min-h-16 items-center justify-center gap-2 text-sm text-stone-500 dark:text-stone-400">+ <Loader2 className="h-4 w-4 animate-spin" />+ Loading…+ </div>+ ) : widgetLoadError && widgetEvaluatedData === undefined ? (+ <div className="flex h-full min-h-16 items-center justify-center px-2 text-center text-xs text-error">+ {widgetLoadError}+ </div>+ ) : (+ WidgetComponent && (+ <>+ <WidgetComponent+ data={widgetEvaluatedData}+ widget={widget as any}+ />+ {isWidgetLoading && (+ <Loader2 className="absolute right-2 top-2 h-3 w-3 animate-spin text-stone-400" />+ )}+ </>+ ))}</CardContent></Card>diff --git a/src/client/src/components/(playground)/observability/log-detail-page.tsx b/src/client/src/components/(playground)/observability/log-detail-page.tsxindex 15f99051..3f42daa7 100644--- a/src/client/src/components/(playground)/observability/log-detail-page.tsx+++ b/src/client/src/components/(playground)/observability/log-detail-page.tsx@@ -83,30 +83,36 @@ function ContextPill({ label, value }: { label: string; value?: unknown }) {export function LogDetailView({id,from,+ aroundTimestamp,variant = "page",extraActions,}: {id: string;from?: string | null;+ aroundTimestamp?: string | null;variant?: "page" | "sheet";extraActions?: ReactNode;}) {const m = getMessage();const router = useRouter();- const { data, fireRequest } = useFetchWrapper();+ const searchParams = useSearchParams();+ const { data, fireRequest, isFetched } = useFetchWrapper();+ const tsHint = aroundTimestamp || searchParams.get("ts") || undefined;const fetchData = useCallback(() => {+ const qs = tsHint ? `?ts=${encodeURIComponent(String(tsHint))}` : "";fireRequest({requestType: "GET",- url: `/api/telemetry/logs/${id}`,+ url: `/api/telemetry/logs/${id}${qs}`,});- }, [fireRequest, id]);+ }, [fireRequest, id, tsHint]);useEffect(() => {fetchData();}, [fetchData]);const record = (data as any)?.record;+ const fetchError = (data as any)?.err;const logAttributes = record?.LogAttributes || {};const eventName = compactValue(logAttributes["event.name"]);const logTitle =@@ -174,6 +180,13 @@ export function LogDetailView({<MetaTile icon={<BadgeDollarSign className="h-3.5 w-3.5" />} label="Cost" value={logAttributes.cost_usd ? `$${formatNumber(logAttributes.cost_usd)}` : undefined} /><MetaTile icon={<Zap className="h-3.5 w-3.5" />} label="Tokens" value={tokenSummary} /></div>+ ) : isFetched && fetchError ? (+ <div+ role="alert"+ className="rounded-md border border-red-200 bg-red-50 px-3 py-2 text-sm text-red-800 dark:border-red-900/50 dark:bg-red-950/30 dark:text-red-200"+ >+ {String(fetchError)}+ </div>) : undefined}>diff --git a/src/client/src/components/(playground)/observability/registry.tsx b/src/client/src/components/(playground)/observability/registry.tsxindex 333c0d3c..d43a67eb 100644--- a/src/client/src/components/(playground)/observability/registry.tsx+++ b/src/client/src/components/(playground)/observability/registry.tsx@@ -69,15 +69,15 @@ export const OBSERVABILITY_SIGNALS: ObservabilitySignalConfig[] = [tone: "text-sky-600 dark:text-sky-400 bg-sky-50 dark:bg-sky-950/40 border-sky-200 dark:border-sky-900",summary: m.OBSERVABILITY_TRACE_SUMMARY,icon: Activity,- listUrl: "/api/telemetry/trace",+ listUrl: "/api/telemetry/request",summaryUrl: "/api/telemetry/summary/traces",- configUrl: "/api/metrics/request/config",- attributeKeysUrl: "/api/metrics/request/attribute-keys",+ configUrl: "/api/telemetry/request/config",+ attributeKeysUrl: "/api/telemetry/request/attribute-keys",columns: traceColumns,pageName: "request",visibilityPage: "request",supportGrouping: true,- groupedUrl: "/api/metrics/request/grouped",+ groupedUrl: "/api/telemetry/request/grouped",customAttributeTypes: ["SpanAttributes", "ResourceAttributes", "Field"],normalize: normalizeTrace,getRowId: (row) => row.spanId,@@ -93,13 +93,13 @@ export const OBSERVABILITY_SIGNALS: ObservabilitySignalConfig[] = [icon: ShieldAlert,listUrl: "/api/telemetry/exception",summaryUrl: "/api/telemetry/summary/exceptions",- configUrl: "/api/metrics/request/config",- attributeKeysUrl: "/api/metrics/request/attribute-keys",+ configUrl: "/api/telemetry/request/config",+ attributeKeysUrl: "/api/telemetry/request/attribute-keys",columns: exceptionColumns,pageName: "exception",visibilityPage: "exception",supportGrouping: true,- groupedUrl: "/api/metrics/exception/grouped",+ groupedUrl: "/api/telemetry/exception/grouped",includeOnlySorting: ["Timestamp"],customAttributeTypes: ["SpanAttributes", "ResourceAttributes", "Field"],normalize: normalizeTrace,@@ -159,8 +159,10 @@ export const OBSERVABILITY_SIGNALS: ObservabilitySignalConfig[] = ["Field",],getRowId: (row) => String(row.rowId),- getDetailHref: (row, from) =>- `/telemetry/logs/${row.rowId}?from=${encodeURIComponent(from)}`,+ getDetailHref: (row, from) => {+ const ts = row.Timestamp ? `&ts=${encodeURIComponent(String(row.Timestamp))}` : "";+ return `/telemetry/logs/${row.rowId}?from=${encodeURIComponent(from)}${ts}`;+ },},{// Coding-agent sessions live alongside the other signals so a@@ -179,8 +181,8 @@ export const OBSERVABILITY_SIGNALS: ObservabilitySignalConfig[] = [// filter UI (span attribute / resource attribute keys); the// underlying server query for sessions still scopes by// `coding_agent.session.id` so unrelated traces never leak in.- configUrl: "/api/metrics/request/config",- attributeKeysUrl: "/api/metrics/request/attribute-keys",+ configUrl: "/api/telemetry/request/config",+ attributeKeysUrl: "/api/telemetry/request/attribute-keys",columns: sessionsColumns,pageName: "codingAgentSessions",visibilityPage: "codingAgentSessions",@@ -217,8 +219,8 @@ export const OBSERVABILITY_SIGNALS: ObservabilitySignalConfig[] = [// sessions page (vendor / user / classification live on// otel_traces span attributes) so we can reuse the existing// metrics/request config endpoints.- configUrl: "/api/metrics/request/config",- attributeKeysUrl: "/api/metrics/request/attribute-keys",+ configUrl: "/api/telemetry/request/config",+ attributeKeysUrl: "/api/telemetry/request/attribute-keys",columns: codingUsersColumns,pageName: "codingAgentSessions",visibilityPage: "codingAgentSessions",diff --git a/src/client/src/components/(playground)/observability/signal-list.tsx b/src/client/src/components/(playground)/observability/signal-list.tsxindex 5d73fd95..8341d3cf 100644--- a/src/client/src/components/(playground)/observability/signal-list.tsx+++ b/src/client/src/components/(playground)/observability/signal-list.tsx@@ -10,6 +10,7 @@ import GroupedTable, {import { getPingStatus } from "@/selectors/database-config";import {getFilterDetails,+ getUpdateAttributeKeys,getUpdateConfig,getUpdateFilter,} from "@/selectors/filter";@@ -29,6 +30,8 @@ import { Maximize2, X } from "lucide-react";import getMessage from "@/constants/messages";import { prepareObservabilitySignalChange } from "@/helpers/client/observability";import { ResizeablePanel } from "@/components/ui/resizeable-panel";+import { useIsAgentScoped } from "@/components/(playground)/agents/agent-scope-provider";+import { getCurrentProjectEnvironment } from "@/selectors/project";const DETAIL_SHEET_CONTENT_CLASS ="right-2 top-2 bottom-2 flex h-auto w-auto max-w-none flex-col gap-0 border-0 bg-transparent p-0 shadow-none focus-visible:outline-none sm:max-w-none";@@ -95,22 +98,45 @@ export default function ObservabilitySignalList({const filter = useRootStore(getFilterDetails);const updateFilter = useRootStore(getUpdateFilter);const updateConfig = useRootStore(getUpdateConfig);+ const updateAttributeKeys = useRootStore(getUpdateAttributeKeys);+ const currentProjectEnvironment = useRootStore(getCurrentProjectEnvironment);const pingStatus = useRootStore(getPingStatus);+ // `serviceNames` is the agent-detail scope lock, owned exclusively by+ // AgentScopeProvider. When this list renders OUTSIDE that provider (the+ // global Telemetry page), a leftover lock must never scope the query.+ const isAgentScoped = useIsAgentScoped();const visibilityColumns = useRootStore((state) =>getVisibilityColumnsOfPage(state, config.visibilityPage));const [previewSpanId, setPreviewSpanId] = useState<string | null>(null);const skipSelectedHydrationRef = useRef(false);const selectedParam = searchParams.get("selected");- const { data, fireRequest, isFetched, isLoading } = useFetchWrapper();+ const selectedEnvironment = currentProjectEnvironment || undefined;+ const { data, fireRequest, reset, isFetched, isLoading, error: listError } = useFetchWrapper();const {data: summaryData,fireRequest: fireSummaryRequest,+ reset: resetSummary,isLoading: isSummaryLoading,+ error: summaryError,} = useFetchWrapper();useEffect(() => {- prepareObservabilitySignalChange(updateConfig, updateFilter);+ prepareObservabilitySignalChange(+ updateConfig,+ updateFilter,+ updateAttributeKeys,+ isAgentScoped+ ? {+ serviceNames: filter.selectedConfig?.serviceNames,+ services: filter.selectedConfig?.services,+ environments: filter.selectedConfig?.environments,+ versionFilter: filter.selectedConfig?.versionFilter,+ }+ : null+ );+ // Only re-run when the signal changes. isAgentScoped / current scope+ // are read at that moment so we can preserve the agent lock.// eslint-disable-next-line react-hooks/exhaustive-deps}, [config.key]);@@ -147,8 +173,11 @@ export default function ObservabilitySignalList({!config.supportGrouping || !filter.groupBy || !!filter.groupValue;const fetchData = useCallback(() => {+ const requestFilter = selectedEnvironment+ ? { ...effectiveFilter, environment: selectedEnvironment }+ : effectiveFilter;fireRequest({- body: JSON.stringify(effectiveFilter),+ body: JSON.stringify(requestFilter),requestType: "POST",url: config.listUrl,failureCb: (err?: string) => {@@ -157,17 +186,39 @@ export default function ObservabilitySignalList({});},});- }, [config.key, config.listUrl, effectiveFilter, fireRequest, m.OBSERVABILITY_NO_SERVER_CONNECTION]);+ }, [config.key, config.listUrl, effectiveFilter, fireRequest, m.OBSERVABILITY_NO_SERVER_CONNECTION, selectedEnvironment]);const fetchSummary = useCallback(() => {+ const requestFilter = selectedEnvironment+ ? { ...effectiveFilter, environment: selectedEnvironment }+ : effectiveFilter;fireSummaryRequest({- body: JSON.stringify(effectiveFilter),+ body: JSON.stringify(requestFilter),requestType: "POST",url: config.summaryUrl,});- }, [config.summaryUrl, effectiveFilter, fireSummaryRequest]);+ }, [config.summaryUrl, effectiveFilter, fireSummaryRequest, selectedEnvironment]);useEffect(() => {+ reset();+ resetSummary();+ }, [reset, resetSummary, selectedEnvironment]);++ useEffect(() => {+ // Defensively strip a leaked agent scope before any request fires. On+ // the global Telemetry surface (`isAgentScoped === false`) a lingering+ // `serviceNames` lock — e.g. left in the shared store by the agent+ // Monitoring tab during a route transition — would otherwise collapse the+ // list to a single service. Clearing it re-runs this effect with an empty+ // scope, so the very first query already covers every service.+ if (+ !isAgentScoped &&+ Array.isArray(filter.selectedConfig?.serviceNames) &&+ filter.selectedConfig.serviceNames.length > 0+ ) {+ updateFilter("selectedConfig.serviceNames", []);+ return;+ }if (effectiveFilter.filterReady &&effectiveFilter.timeLimit.start &&@@ -178,13 +229,14 @@ export default function ObservabilitySignalList({if (showFlatList) fetchData();}// eslint-disable-next-line react-hooks/exhaustive-deps- }, [effectiveFilter, pingStatus, showFlatList]);+ }, [effectiveFilter, pingStatus, showFlatList, isAgentScoped, selectedEnvironment]);const rows = useMemo(() => {const records = (data as any)?.records || [];return config.normalize ? records.map(config.normalize) : records;}, [config, data]);const total = (data as any)?.total || 0;+ const telemetryError = summaryError || listError;const isTraceSignal = config.key === "traces" || config.key === "exceptions";const isMetricSignal = config.key === "metrics";const isLogSignal = config.key === "logs";@@ -195,12 +247,18 @@ export default function ObservabilitySignalList({const isSessionSignal = config.key === "sessions";const setSelectedInUrl = useCallback(- (id: string | null) => {+ (id: string | null, traceId?: string | null) => {const params = new URLSearchParams(searchParams.toString());if (id) {params.set("selected", id);+ if (traceId) {+ params.set("traceId", String(traceId));+ } else if (traceId === null) {+ params.delete("traceId");+ }} else {params.delete("selected");+ params.delete("traceId");}const query = params.toString();router.replace(`${pathname}${query ? `?${query}` : ""}`, {@@ -252,7 +310,7 @@ export default function ObservabilitySignalList({if (isTraceSignal) {skipSelectedHydrationRef.current = false;setPreviewSpanId(row.spanId);- setSelectedInUrl(row.spanId);+ setSelectedInUrl(row.spanId, row.traceId);return;}if (isSessionSignal) {@@ -268,7 +326,7 @@ export default function ObservabilitySignalList({if (!targetSpanId) return;skipSelectedHydrationRef.current = false;setPreviewSpanId(targetSpanId);- setSelectedInUrl(targetSpanId);+ setSelectedInUrl(targetSpanId, safe.trace_id || safe.TraceId || null);return;}if (isMetricSignal) {@@ -373,6 +431,13 @@ export default function ObservabilitySignalList({showGroupBy={!!config.supportGrouping}showVisibilityColumnsextraControls={toolbarExtraControls}+ signal={+ config.key === "metrics"+ ? "metrics"+ : config.key === "logs"+ ? "logs"+ : "traces"+ }/>{config.supportGrouping && filter.groupBy && (@@ -384,6 +449,12 @@ export default function ObservabilitySignalList({/>)}+ {telemetryError && (+ <div role="alert" className="mb-3 rounded-md border border-red-200 bg-red-50 px-3 py-2 text-sm text-red-800 dark:border-red-900/50 dark:bg-red-950/30 dark:text-red-200">+ {String(telemetryError)}+ </div>+ )}+{config.supportGrouping && filter.groupBy && !filter.groupValue ? (<GroupedTablegroupBy={filter.groupBy}@@ -531,6 +602,11 @@ export default function ObservabilitySignalList({{selectedLogRow && (<LogDetailViewid={config.getRowId(selectedLogRow)}+ aroundTimestamp={+ selectedLogRow.Timestamp+ ? String(selectedLogRow.Timestamp)+ : null+ }from={typeof window !== "undefined"? `${window.location.pathname}${window.location.search}`diff --git a/src/client/src/components/(playground)/observability/signal-records.tsx b/src/client/src/components/(playground)/observability/signal-records.tsxindex abaef526..be56c7b8 100644--- a/src/client/src/components/(playground)/observability/signal-records.tsx+++ b/src/client/src/components/(playground)/observability/signal-records.tsx@@ -270,26 +270,34 @@ function MetricRecord({onClick={() => onOpen(row)}className={`group w-full rounded-md border p-3 text-left transition ${isSelected- ? "border-emerald-500 bg-emerald-50 shadow-sm ring-1 ring-emerald-200 dark:border-emerald-500 dark:bg-emerald-950/30 dark:ring-emerald-900"- : "border-stone-200 bg-white hover:border-emerald-400 hover:bg-emerald-50/60 dark:border-stone-800 dark:bg-stone-950 dark:hover:border-emerald-700 dark:hover:bg-emerald-950/20"+ ? "border-primary bg-primary/10 shadow-sm ring-1 ring-primary/20 dark:border-primary dark:bg-primary/15"+ : "border-stone-200 bg-white hover:border-primary/50 hover:bg-primary/5 dark:border-stone-800 dark:bg-stone-950 dark:hover:border-primary/60 dark:hover:bg-primary/10"}`}><div className="flex flex-col gap-3 lg:flex-row lg:items-center lg:justify-between"><div className="min-w-0"><div className="flex min-w-0 items-center gap-2">- <span className="h-2 w-2 rounded-full bg-emerald-500" />+ <span className="h-2 w-2 shrink-0 rounded-full bg-sky-500" /><h3 className="truncate text-sm font-semibold text-stone-950 dark:text-stone-50">{show("metricName") ? row.metricName : m.OBSERVABILITY_METRIC}</h3></div><div className="mt-1 flex flex-wrap items-center gap-1.5">- {show("metricType") && <span className="rounded bg-emerald-100 px-1.5 py-0.5 text-[11px] font-medium text-emerald-700 dark:bg-emerald-950 dark:text-emerald-300">- {row.metricType}- </span>}- {show("serviceName") && <span className="rounded bg-stone-100 px-1.5 py-0.5 text-[11px] text-stone-600 dark:bg-stone-900 dark:text-stone-300">- {row.serviceName || "all services"}- </span>}- {show("lastSeen") && <span className="text-xs text-stone-500 dark:text-stone-400">{formatDate(row.lastSeen)}</span>}+ {show("metricType") && (+ <span className="rounded bg-stone-100 px-1.5 py-0.5 text-[11px] font-medium text-stone-600 dark:bg-stone-900 dark:text-stone-300">+ {row.metricType}+ </span>+ )}+ {show("serviceName") && (+ <span className="rounded bg-stone-100 px-1.5 py-0.5 text-[11px] text-stone-600 dark:bg-stone-900 dark:text-stone-300">+ {row.serviceName || "all services"}+ </span>+ )}+ {show("lastSeen") && (+ <span className="text-xs text-stone-500 dark:text-stone-400">+ {formatDate(row.lastSeen)}+ </span>+ )}</div></div><div className="grid grid-cols-2 gap-1.5 sm:flex sm:flex-wrap sm:justify-end">@@ -328,7 +336,7 @@ function MetricRecord({</div><div className="mt-3"><div className="h-1.5 rounded-full bg-stone-100 dark:bg-stone-900">- <div className="h-full rounded-full bg-emerald-500" style={{ width }} />+ <div className="h-full rounded-full bg-stone-400 dark:bg-stone-500" style={{ width }} /></div><div className="mt-1 flex items-center justify-between text-[11px] text-stone-500 dark:text-stone-400"><span>latest compared to max in this window</span>diff --git a/src/client/src/components/(playground)/observability/signal-summary.tsx b/src/client/src/components/(playground)/observability/signal-summary.tsxindex 827c3125..99ab239d 100644--- a/src/client/src/components/(playground)/observability/signal-summary.tsx+++ b/src/client/src/components/(playground)/observability/signal-summary.tsx@@ -19,6 +19,7 @@ type SummaryData = {total?: number;peak?: number;buckets?: Array<Record<string, any>>;+ freshness?: "live" | "sampled" | "accelerated";};function formatBucket(bucket?: string) {@@ -26,6 +27,13 @@ function formatBucket(bucket?: string) {return bucket.charAt(0).toUpperCase() + bucket.slice(1);}+function freshnessLabel(freshness?: SummaryData["freshness"]) {+ if (freshness === "live") return m.OBSERVABILITY_FRESHNESS_LIVE;+ if (freshness === "accelerated") return m.OBSERVABILITY_FRESHNESS_ACCELERATED;+ if (freshness === "sampled") return m.OBSERVABILITY_FRESHNESS_SAMPLED;+ return null;+}+function chartColor(signal: ObservabilitySignalConfig["key"]) {if (signal === "exceptions") return "#e11d48";if (signal === "logs") return "#d97706";@@ -48,7 +56,10 @@ export default function SignalSummary({? m.OBSERVABILITY_METRIC_POINTS: config.key === "logs"? m.OBSERVABILITY_LOG_EVENTS- : m.OBSERVABILITY_SPANS;+ : config.key === "exceptions"+ ? m.OBSERVABILITY_EXCEPTIONS+ : m.OBSERVABILITY_TRACES;+ const freshness = freshnessLabel(data?.freshness);return (<section className="rounded-md border border-stone-200 bg-stone-50/80 p-3 dark:border-stone-800 dark:bg-stone-900/40">@@ -62,9 +73,16 @@ export default function SignalSummary({{metricLabel}</h2></div>- <span className={`rounded-full border px-2 py-1 text-xs font-medium ${config.tone}`}>- {isLoading ? m.OBSERVABILITY_LOADING : `${buckets.length} buckets`}- </span>+ <div className="flex flex-wrap items-center gap-1.5">+ {freshness ? (+ <span className="rounded-full border border-stone-300 px-2 py-1 text-xs font-medium text-stone-600 dark:border-stone-700 dark:text-stone-300">+ {freshness}+ </span>+ ) : null}+ <span className={`rounded-full border px-2 py-1 text-xs font-medium ${config.tone}`}>+ {isLoading ? m.OBSERVABILITY_LOADING : `${buckets.length} buckets`}+ </span>+ </div></div><div className="h-28 min-w-0"><ResponsiveContainer width="100%" height="100%">diff --git a/src/client/src/components/(playground)/observability/span-hierarchy-explorer.tsx b/src/client/src/components/(playground)/observability/span-hierarchy-explorer.tsxindex 464b7b1d..3a9ab6c4 100644--- a/src/client/src/components/(playground)/observability/span-hierarchy-explorer.tsx+++ b/src/client/src/components/(playground)/observability/span-hierarchy-explorer.tsx@@ -4,6 +4,7 @@ import { useEffect, useMemo, useRef, useState } from "react";import type { ReactNode } from "react";import { BarChart3, DollarSign, GitBranch, MessageSquareText, Network, Sparkles } from "lucide-react";import useFetchWrapper from "@/utils/hooks/useFetchWrapper";+import { useSignalCapabilities } from "@/utils/hooks/useSignalCapabilities";import { TraceHeirarchySpan } from "@/types/trace";import {RequestProvider,@@ -84,11 +85,13 @@ function isCodingAgentTree(span?: TraceHeirarchySpan): boolean {function SpanHierarchyExplorerInner({hierarchySpanId,selectedSpanId,+ traceId,onSelectSpan,fill = false,}: {hierarchySpanId: string;selectedSpanId: string;+ traceId?: string;onSelectSpan?: (spanId: string) => void;fill?: boolean;}) {@@ -100,17 +103,29 @@ function SpanHierarchyExplorerInner({// snapping back to "chat" every time the trace data refreshes.const userPickedViewRef = useRef(false);const { data, fireRequest, isLoading } = useFetchWrapper();+ const { capabilities } = useSignalCapabilities();++ // The Chat view renders OTel span events (prompts/completions). Sources that+ // don't carry span events (e.g. Datadog, New Relic) can't populate it, so we+ // hide the tab and never auto-switch to it — honest capability gating.+ const supportsSpanEvents =+ capabilities?.traces?.capabilities?.spanEvents !== false;+ const availableViewModes = useMemo(+ () => VIEW_MODES.filter((v) => v.key !== "chat" || supportsSpanEvents),+ [supportsSpanEvents]+ );useEffect(() => {updateRequest({ spanId: selectedSpanId } as any);}, [selectedSpanId, updateRequest]);useEffect(() => {+ const qs = traceId ? `?traceId=${encodeURIComponent(traceId)}` : "";fireRequest({requestType: "GET",- url: `/api/telemetry/trace/span/${hierarchySpanId}/heirarchy`,+ url: `/api/telemetry/request/span/${hierarchySpanId}/heirarchy${qs}`,});- }, [fireRequest, hierarchySpanId]);+ }, [fireRequest, hierarchySpanId, traceId]);const typedData = (data as { record?: TraceHeirarchySpan; err?: string }) || {};const record = typedData.record;@@ -125,10 +140,17 @@ function SpanHierarchyExplorerInner({// surface that renders the conversational signal (prompts, thinking,// tools, edits, subagents). Plain LLM traces stay on the Tree view.useEffect(() => {- if (isCodingAgent && !userPickedViewRef.current) {+ if (isCodingAgent && supportsSpanEvents && !userPickedViewRef.current) {setViewMode("chat");}- }, [isCodingAgent]);+ }, [isCodingAgent, supportsSpanEvents]);++ // If the active view is no longer available for this source, fall back.+ useEffect(() => {+ if (!availableViewModes.some((v) => v.key === viewMode)) {+ setViewMode("tree");+ }+ }, [availableViewModes, viewMode]);return (<section@@ -144,7 +166,7 @@ function SpanHierarchyExplorerInner({<div className="flex flex-wrap items-center gap-2 border-b border-stone-200 bg-stone-50 px-2 py-1.5 dark:border-stone-800 dark:bg-stone-900"><div className="flex rounded-md border border-stone-200 dark:border-stone-800 bg-white dark:bg-stone-950 p-0.5">- {VIEW_MODES.map((mode) => (+ {availableViewModes.map((mode) => (<buttonkey={mode.key}onClick={() => {@@ -234,11 +256,13 @@ function SpanHierarchyExplorerInner({export default function SpanHierarchyExplorer({hierarchySpanId,selectedSpanId,+ traceId,onSelectSpan,fill,}: {hierarchySpanId: string;selectedSpanId: string;+ traceId?: string;onSelectSpan?: (spanId: string) => void;fill?: boolean;}) {@@ -247,6 +271,7 @@ export default function SpanHierarchyExplorer({<SpanHierarchyExplorerInnerhierarchySpanId={hierarchySpanId}selectedSpanId={selectedSpanId}+ traceId={traceId}onSelectSpan={onSelectSpan}fill={fill}/>diff --git a/src/client/src/components/(playground)/observability/trace-detail-page.tsx b/src/client/src/components/(playground)/observability/trace-detail-page.tsxindex 3d0fa4ac..4873eacb 100644--- a/src/client/src/components/(playground)/observability/trace-detail-page.tsx+++ b/src/client/src/components/(playground)/observability/trace-detail-page.tsx@@ -32,6 +32,8 @@ import {hasCodingAgentVendorIcon,} from "@/components/svg/coding-agents";import { toast } from "sonner";+import { useRootStore } from "@/store";+import { getCurrentProjectEnvironment } from "@/selectors/project";// Mirror of `durationLabel` in observability/columns.tsx — kept local// so the top-card override for coding-agent sessions can format the@@ -103,15 +105,18 @@ function Stat({ icon, label, value }: { icon: ReactNode; label: string; value?:function CostStat({costValue,spanId,+ traceId,hasModel,onRecalculated,}: {costValue?: string;spanId?: string;+ traceId?: string;hasModel: boolean;onRecalculated: () => void;}) {const m = getMessage();+ const currentEnvironment = useRootStore(getCurrentProjectEnvironment);const hasCost = !!costValue && costValue !== "-";const canRecalculate = hasModel && !!spanId;const { fireRequest, isLoading } = useFetchWrapper<{@@ -125,7 +130,10 @@ function CostStat({if (!spanId || isLoading) return;fireRequest({requestType: "POST",- url: `/api/pricing/${spanId}`,+ url: `/api/pricing/${spanId}?${new URLSearchParams({+ ...(currentEnvironment ? { environment: currentEnvironment } : {}),+ ...(traceId ? { traceId } : {}),+ })}`,successCb: (response) => {if (response?.success) {toast.success(@@ -311,6 +319,7 @@ export function TraceDetailView({const router = useRouter();const searchParams = useSearchParams();const from = searchParams.get("from");+ const urlTraceId = searchParams.get("traceId") || undefined;const [selectedSpanId, setSelectedSpanId] = useState(spanId);const [activeListSpanId, setActiveListSpanId] = useState(spanId);// Per-session code-impact rollups (lines added / removed,@@ -359,7 +368,7 @@ export function TraceDetailView({total?: number;} | null>(null);const fromRef = useRef(from);- const listUrlRef = useRef(type === "exceptions" ? "/api/telemetry/exception" : "/api/telemetry/trace");+ const listUrlRef = useRef(type === "exceptions" ? "/api/telemetry/exception" : "/api/telemetry/request");const detailBasePathRef = useRef(type === "exceptions" ? "/telemetry/exceptions" : "/telemetry/traces");@@ -369,12 +378,27 @@ export function TraceDetailView({fireRequest: fireListRequest,isLoading: isListLoading,} = useFetchWrapper<any>();+ const listHintTraceId = useMemo(() => {+ const row = (navigationRows || []).find(+ (r: any) => r.spanId === selectedSpanId || r.spanId === activeListSpanId+ );+ return (+ (row?.traceId as string | undefined) ||+ (row?.id as string | undefined) ||+ (row?.TraceId as string | undefined) ||+ undefined+ );+ }, [navigationRows, selectedSpanId, activeListSpanId]);+ const knownTraceId = urlTraceId || listHintTraceId;const fetchData = useCallback(() => {+ const qs = knownTraceId+ ? `?traceId=${encodeURIComponent(knownTraceId)}`+ : "";fireRequest({requestType: "GET",- url: `/api/telemetry/trace/span/${selectedSpanId}`,+ url: `/api/telemetry/request/span/${selectedSpanId}${qs}`,});- }, [fireRequest, selectedSpanId]);+ }, [fireRequest, selectedSpanId, knownTraceId]);useEffect(() => {hierarchySpanIdRef.current = spanId;@@ -392,12 +416,15 @@ export function TraceDetailView({}, [fetchData]);const navigateToListSpan = useCallback(- (nextSpanId: string) => {+ (nextSpanId: string, nextTraceId?: string) => {hierarchySpanIdRef.current = nextSpanId;setActiveListSpanId(nextSpanId);setSelectedSpanId(nextSpanId);const source = fromRef.current;- const qs = source ? `?from=${encodeURIComponent(source)}` : "";+ const params = new URLSearchParams();+ if (source) params.set("from", source);+ if (nextTraceId) params.set("traceId", nextTraceId);+ const qs = params.toString() ? `?${params.toString()}` : "";if (variant === "page") {router.replace(`${detailBasePathRef.current}/${nextSpanId}${qs}`, { scroll: false });}@@ -437,7 +464,7 @@ export function TraceDetailView({const records = ((response as any)?.records || []).map(normalizeTrace);const target =direction === 1 ? records[0] : records[records.length - 1];- if (target?.spanId) {+ if (target?.spanId) {const nextSource = sourceWithOffset(fromRef.current, offset);fromRef.current = nextSource;setListOffset(offset);@@ -447,7 +474,7 @@ export function TraceDetailView({total: (response as any)?.total,});onNavigationPageChange?.(offset);- navigateToListSpan(target.spanId);+ navigateToListSpan(target.spanId, target.id || target.traceId || target.TraceId);}},});@@ -647,6 +674,7 @@ export function TraceDetailView({<RequestProvider syncUrl={false}><TraceAiAnalysisPanelspanId={selectedSpanId}+ traceId={knownTraceId || trace?.id}scope="span"description={m.TRACE_AI_IMPROVEMENT_SPAN_DESCRIPTION}/>@@ -661,7 +689,12 @@ export function TraceDetailView({{id: "evaluations",label: "Evaluations",- content: <Evaluations trace={trace} surface="observability" />,+ content: (+ <Evaluations+ trace={trace}+ surface="observability"+ />+ ),},]: []),@@ -722,7 +755,8 @@ export function TraceDetailView({const selectPrev = () => {if (currentIndex > 0) {- navigateToListSpan(effectiveListRows[currentIndex - 1].spanId);+ const previous = effectiveListRows[currentIndex - 1];+ navigateToListSpan(previous.spanId, previous.id || previous.traceId || previous.TraceId);} else if (effectiveListOffset > 0) {fetchList(Math.max(0, effectiveListOffset - navigationLimit), -1);}@@ -730,7 +764,8 @@ export function TraceDetailView({const selectNext = () => {if (currentIndex >= 0 && currentIndex < effectiveListRows.length - 1) {- navigateToListSpan(effectiveListRows[currentIndex + 1].spanId);+ const next = effectiveListRows[currentIndex + 1];+ navigateToListSpan(next.spanId, next.id || next.traceId || next.TraceId);} else if (effectiveListOffset + effectiveListRows.length < total) {fetchList(effectiveListOffset + navigationLimit, 1);}@@ -812,6 +847,7 @@ export function TraceDetailView({<CostStatcostValue={costValue}spanId={trace.spanId}+ traceId={knownTraceId}hasModel={!!modelValue}onRecalculated={fetchData}/>@@ -918,6 +954,7 @@ export function TraceDetailView({<SpanHierarchyExplorerhierarchySpanId={hierarchySpanIdRef.current}selectedSpanId={selectedSpanId}+ traceId={knownTraceId || trace?.id}onSelectSpan={selectSpanInCurrentTrace}fill/>@@ -939,6 +976,7 @@ export function TraceDetailView({<SpanHierarchyExplorerhierarchySpanId={hierarchySpanIdRef.current}selectedSpanId={selectedSpanId}+ traceId={knownTraceId || trace?.id}onSelectSpan={selectSpanInCurrentTrace}/><DetailObjectTabsdiff --git a/src/client/src/components/(playground)/organisation/project-connectors-page.tsx b/src/client/src/components/(playground)/organisation/project-connectors-page.tsxnew file mode 100644index 00000000..891b7225--- /dev/null+++ b/src/client/src/components/(playground)/organisation/project-connectors-page.tsx@@ -0,0 +1,54 @@+"use client";++import { useEffect, useMemo } from "react";+import { Layers } from "lucide-react";+import DataSourcesPage from "@/components/(playground)/telemetry-source/data-sources-page";+import ProjectPageHeader from "./project-page-header";+import getMessage from "@/constants/messages";+import FeatureAccess from "@/components/rbac/feature-access";+import { getCurrentOrganisation } from "@/selectors/organisation";+import { getCurrentProject, getCurrentProjectEnvironment, getProjectList } from "@/selectors/project";+import { changeActiveProject, fetchProjectList } from "@/helpers/client/project";+import { useRootStore } from "@/store";++/**+ * Unified project connection management. ClickHouse connections and external+ * telemetry connectors live together because both are project integrations+ * and may be combined independently by signal and application feature.+ */+export default function ProjectConnectorsPage({ projectId }: { projectId?: string }) {+ const messages = getMessage();+ const environment = useRootStore(getCurrentProjectEnvironment) || "production";+ const currentOrg = useRootStore(getCurrentOrganisation);+ const projects = useRootStore(getProjectList) || [];+ const currentProject = useRootStore(getCurrentProject);+ const project = useMemo(() => projects.find((item) => item.id === projectId) || (currentProject?.id === projectId ? currentProject : { id: projectId, name: projectId }), [currentProject, projectId, projects]);++ useEffect(() => {+ if (currentOrg?.id) fetchProjectList(currentOrg.id);+ }, [currentOrg?.id]);++ useEffect(() => {+ if (project?.id && currentProject?.id !== project.id) changeActiveProject(project.id);+ }, [currentProject?.id, project?.id]);++ return (+ <div className="flex h-full w-full flex-col overflow-auto text-stone-700 dark:text-stone-300">+ <ProjectPageHeader project={project} />+ <FeatureAccess access="connectors.read" requireProject>+ <div className="flex min-h-0 w-full flex-col gap-4 p-4">+ <section className="border border-primary/20 bg-primary/[0.04] p-4 dark:border-primary/30 dark:bg-primary/[0.08]">+ <div className="flex items-start gap-3">+ <Layers className="mt-0.5 h-4 w-4 shrink-0 text-primary" />+ <div>+ <h2 className="text-sm font-semibold text-stone-950 dark:text-stone-50">{messages.PROJECT_ENVIRONMENTS}: {environment}</h2>+ <p className="mt-1 text-xs leading-5 text-muted-foreground">{messages.PROJECT_ENVIRONMENTS_DESCRIPTION}</p>+ </div>+ </div>+ </section>+ <DataSourcesPage projectId={projectId} />+ </div>+ </FeatureAccess>+ </div>+ );+}diff --git a/src/client/src/components/(playground)/organisation/project-environment-switcher.tsx b/src/client/src/components/(playground)/organisation/project-environment-switcher.tsxnew file mode 100644index 00000000..6630e35c--- /dev/null+++ b/src/client/src/components/(playground)/organisation/project-environment-switcher.tsx@@ -0,0 +1,73 @@+"use client";++import { useEffect, useState } from "react";+import { Plus } from "lucide-react";+import { Button } from "@/components/ui/button";+import { Input } from "@/components/ui/input";+import { Label } from "@/components/ui/label";+import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";+import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";+import getMessage from "@/constants/messages";++export default function ProjectEnvironmentSwitcher({ value, onChange }: { value: string; onChange: (value: string) => void }) {+ const messages = getMessage();+ const [environments, setEnvironments] = useState<string[]>(["production"]);+ const [open, setOpen] = useState(false);+ const [name, setName] = useState("");+ const [saving, setSaving] = useState(false);++ const load = async () => {+ const response = await fetch("/api/project/environment");+ if (!response.ok) return;+ const body = await response.json();+ const names = (body.environments || []).map((item: { name: string }) => item.name);+ setEnvironments(Array.from(new Set(["production", ...names])).sort());+ };++ useEffect(() => {+ if (typeof fetch !== "undefined") void load();+ }, []);++ const create = async () => {+ if (!name.trim()) return;+ setSaving(true);+ try {+ const response = await fetch("/api/project/environment", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ name }),+ });+ if (!response.ok) return;+ const body = await response.json();+ const created = body.environment.name;+ await load();+ onChange(created);+ setName("");+ setOpen(false);+ } finally {+ setSaving(false);+ }+ };++ return (+ <div className="flex items-center gap-1.5">+ <DropdownMenu>+ <DropdownMenuTrigger asChild><Button type="button" variant="outline" size="sm" className="h-8 min-w-36 justify-between border-stone-300 bg-white text-xs dark:border-stone-700 dark:bg-stone-950">{value}<span className="ml-2 text-muted-foreground">⌄</span></Button></DropdownMenuTrigger>+ <DropdownMenuContent align="end" className="w-56">+ <DropdownMenuLabel>{messages.CONNECTOR_ENVIRONMENT}</DropdownMenuLabel>+ <DropdownMenuSeparator />+ {environments.map((environment) => <DropdownMenuItem key={environment} onSelect={() => onChange(environment)}>{environment}</DropdownMenuItem>)}+ <DropdownMenuSeparator />+ <DropdownMenuItem onSelect={() => setOpen(true)}><Plus className="mr-2 h-3.5 w-3.5" />Create environment</DropdownMenuItem>+ </DropdownMenuContent>+ </DropdownMenu>+ <Dialog open={open} onOpenChange={setOpen}>+ <DialogContent>+ <DialogHeader><DialogTitle>Create environment</DialogTitle><DialogDescription>Environments group ClickHouse databases and observability connectors inside this project.</DialogDescription></DialogHeader>+ <div className="space-y-2"><Label htmlFor="new-project-environment">Environment name</Label><Input id="new-project-environment" value={name} onChange={(event) => setName(event.target.value.toLowerCase())} placeholder="staging" /></div>+ <DialogFooter><Button type="button" onClick={create} disabled={saving || !name.trim()}>{saving ? "Creating…" : "Create environment"}</Button></DialogFooter>+ </DialogContent>+ </Dialog>+ </div>+ );+}diff --git a/src/client/src/components/(playground)/organisation/project-page-header.tsx b/src/client/src/components/(playground)/organisation/project-page-header.tsxindex 0b55bf46..efd29879 100644--- a/src/client/src/components/(playground)/organisation/project-page-header.tsx+++ b/src/client/src/components/(playground)/organisation/project-page-header.tsx@@ -1,24 +1,33 @@"use client";+import type { ReactNode } from "react";import Link from "next/link";-import { ArrowLeft, FolderKanban } from "lucide-react";-import { Badge } from "@/components/ui/badge";+import { usePathname, useSearchParams } from "next/navigation";+import { ArrowLeft, FolderKanban, Settings2 } from "lucide-react";import { Button } from "@/components/ui/button";import FeaturePageHeader from "@/components/(playground)/feature-page-header";import getMessage from "@/constants/messages";+import ProjectEnvironmentSwitcher from "./project-environment-switcher";+import { getCurrentProjectEnvironment } from "@/selectors/project";+import { useRootStore } from "@/store";export default function ProjectPageHeader({project,+ additionalActions,}: {- project?: { name?: string; isCurrent?: boolean; isDefault?: boolean };+ project?: { id?: string; name?: string; isCurrent?: boolean; isDefault?: boolean };+ additionalActions?: ReactNode;}) {const messages = getMessage();+ const pathname = usePathname();+ const searchParams = useSearchParams();+ const environment = useRootStore(getCurrentProjectEnvironment) || "production";const backLabel = messages.BACK_TO_ORGANISATION;return (<FeaturePageHeadereyebrow={messages.ORGANISATION}- title={project?.name || messages.LOADING_PROJECT}+ title={project?.name || project?.id || messages.LOADING_PROJECT}icon={<FolderKanban className="h-4 w-4" />}tone="border-primary/20 bg-primary/10 text-primary dark:border-primary/30"leading={@@ -34,15 +43,18 @@ export default function ProjectPageHeader({</Button>}actions={- <div className="flex flex-wrap items-center gap-2">- {project?.isCurrent ? (- <Badge className="h-6">{messages.CURRENT}</Badge>- ) : null}- {project?.isDefault ? (- <Badge variant="outline" className="h-6">- {messages.DEFAULT_PROJECT}- </Badge>- ) : null}+ <div className="flex flex-wrap items-center justify-end gap-2">+ <ProjectEnvironmentSwitcher key={project?.id} value={environment} onChange={(nextEnvironment) => {+ if (project?.id) window.localStorage.setItem(`openlit:environment:${project.id}`, nextEnvironment);+ useRootStore.getState().project.setCurrentEnvironment(nextEnvironment);+ }} />+ {additionalActions}+ <Button asChild size="sm" variant={pathname?.endsWith(`/project/${project?.id}`) && searchParams?.get("tab") !== "access" ? "default" : "outline"} className="h-8 gap-1.5 text-xs">+ <Link href={`/organisation/project/${project?.id || ""}`}><FolderKanban className="h-3.5 w-3.5" />{messages.PROJECT_OVERVIEW}</Link>+ </Button>+ <Button asChild size="sm" variant={pathname?.endsWith("/connectors") ? "default" : "outline"} className="h-8 gap-1.5 text-xs">+ <Link href={`/organisation/project/${project?.id || ""}/environments`}><Settings2 className="h-3.5 w-3.5" />{messages.PROJECT_ENVIRONMENTS}</Link>+ </Button></div>}/>diff --git a/src/client/src/components/(playground)/organisation/project-page.tsx b/src/client/src/components/(playground)/organisation/project-page.tsxindex 33980c2b..5167e0f1 100644--- a/src/client/src/components/(playground)/organisation/project-page.tsx+++ b/src/client/src/components/(playground)/organisation/project-page.tsx@@ -1,43 +1,32 @@"use client";-import { useEffect, useMemo } from "react";-import { usePathname, useRouter, useSearchParams } from "next/navigation";-import { Database, FolderKanban } from "lucide-react";-import DatabaseConfigPage from "@/components/(playground)/database-config/database-config-page";+import { useEffect, useMemo, useState } from "react";+import { Badge } from "@/components/ui/badge";import ProjectPageHeader from "./project-page-header";-import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";import { getCurrentOrganisation } from "@/selectors/organisation";import { getCurrentProject, getProjectList } from "@/selectors/project";import { changeActiveProject, fetchProjectList } from "@/helpers/client/project";import { useRootStore } from "@/store";import getMessage from "@/constants/messages";-export default function OrganisationProjectPage({- projectId,-}: {- projectId: string;-}) {+export default function OrganisationProjectPage({ projectId }: { projectId: string }) {const messages = getMessage();const currentOrg = useRootStore(getCurrentOrganisation);- const router = useRouter();- const pathname = usePathname();- const searchParams = useSearchParams();const projects = useRootStore(getProjectList) || [];const currentProject = useRootStore(getCurrentProject);+ const [projectEnvironments, setProjectEnvironments] = useState<string[]>([]);const project = useMemo(- () => projects.find((item) => item.id === projectId),- [projectId, projects]+ () => projects.find((item) => item.id === projectId) || (currentProject?.id === projectId ? currentProject : { id: projectId, organisationId: "", name: projectId, slug: "-", isDefault: false, isCurrent: false, createdAt: "" }),+ [currentProject, projectId, projects]);- const selectedTab = searchParams.get("tab") === "database" ? "database" : "overview";- const setSelectedTab = (tab: string) => {- const params = new URLSearchParams(searchParams.toString());- if (tab === "overview") params.delete("tab");- else params.set("tab", tab);- router.replace(`${pathname}${params.size ? `?${params}` : ""}`);- };useEffect(() => {- if (currentOrg?.id) fetchProjectList(currentOrg.id);+ if (currentOrg?.id) {+ fetchProjectList(currentOrg.id);+ fetch("/api/project/environment").then((response) => response.ok ? response.json() : { environments: [] }).then((body) => {+ setProjectEnvironments(Array.from(new Set(["production", ...(body.environments || []).map((item: { name: string }) => item.name)])));+ }).catch(() => undefined);+ }}, [currentOrg?.id]);useEffect(() => {@@ -46,63 +35,69 @@ export default function OrganisationProjectPage({}}, [currentProject?.id, project?.id]);- return (- <div className="flex h-full w-full flex-col gap-4 overflow-auto p-3 text-stone-700 dark:text-stone-300">- <ProjectPageHeader project={project} />+ const createdAt = project?.createdAt+ ? new Intl.DateTimeFormat(undefined, { dateStyle: "medium" }).format(+ new Date(project.createdAt)+ )+ : "-";+ const environments = Array.from(+ new Set([+ ...projectEnvironments,+ ])+ );- <Tabs value={selectedTab} onValueChange={setSelectedTab} className="flex min-h-0 w-full flex-1 flex-col">- <TabsList className="h-auto w-full justify-start rounded-md border border-stone-200 bg-white p-1 dark:border-stone-800 dark:bg-stone-950 md:w-auto">- <TabsTrigger value="overview" className="gap-1.5 text-xs"><FolderKanban className="h-3.5 w-3.5" />{messages.PROJECT_OVERVIEW}</TabsTrigger>- <TabsTrigger value="database" className="gap-1.5 text-xs"><Database className="h-3.5 w-3.5" />{messages.PROJECT_DATABASE_CONFIGS}</TabsTrigger>- </TabsList>- <TabsContent value="overview" className="mt-0 data-[state=active]:mt-3">- <section className="border border-stone-200 bg-white p-4 dark:border-stone-800 dark:bg-stone-950">- <div className="mb-4 flex items-center gap-2">- <FolderKanban className="h-4 w-4 text-primary" />- <h2 className="text-sm font-semibold text-stone-950 dark:text-stone-50">- {messages.PROJECT_DETAILS}- </h2>- </div>- <div className="space-y-3 text-sm">+ return (+ <div className="flex h-full w-full flex-col overflow-auto text-stone-700 dark:text-stone-300">+ <ProjectPageHeader project={project} />+ <main className="flex w-full flex-col gap-4 p-4">+ <section className="border border-stone-200 bg-white p-5 dark:border-stone-800 dark:bg-stone-950">+ <div className="flex flex-col gap-3 border-b border-stone-200 pb-4 dark:border-stone-800 md:flex-row md:items-start md:justify-between"><div>- <p className="text-xs uppercase text-muted-foreground">- {messages.PROJECT_NAME}- </p>- <p className="font-medium text-stone-950 dark:text-stone-50">- {project?.name || "-"}+ <p className="text-xs uppercase tracking-wide text-muted-foreground">{messages.PROJECT_DETAILS}</p>+ <h2 className="mt-1 text-lg font-semibold text-stone-950 dark:text-stone-50">+ {project?.name || messages.LOADING_PROJECT}+ </h2>+ <p className="mt-1 max-w-2xl text-sm text-muted-foreground">+ {messages.PROJECT_DETAILS_DESCRIPTION}</p></div>- <div>- <p className="text-xs uppercase text-muted-foreground">- {messages.SLUG}- </p>- <p className="font-mono text-xs text-stone-700 dark:text-stone-300">- {project?.slug || "-"}- </p>+ <div className="flex flex-wrap gap-2">+ {project?.isCurrent ? <Badge>{messages.CURRENT}</Badge> : null}+ {project?.isDefault ? <Badge variant="outline">{messages.DEFAULT_PROJECT}</Badge> : null}</div></div>- </section>- </TabsContent>- <TabsContent value="database" className="mt-0 data-[state=active]:mt-3">- <section className="min-h-[520px] overflow-hidden border border-stone-200 bg-white dark:border-stone-800 dark:bg-stone-950">- <div className="border-b border-stone-200 p-4 dark:border-stone-800">- <div className="flex items-center gap-2">- <Database className="h-4 w-4 text-primary" />- <h2 className="text-sm font-semibold text-stone-950 dark:text-stone-50">- {messages.PROJECT_DATABASE_CONFIGS}- </h2>- </div>- <p className="mt-1 text-xs text-muted-foreground">- {messages.PROJECT_DATABASE_CONFIGS_DESCRIPTION}- </p>- </div>- <div className="flex h-[clamp(360px,calc(100dvh-320px),620px)] min-h-0 overflow-hidden">- <DatabaseConfigPage />+ <div className="mt-5 grid gap-3 sm:grid-cols-2 lg:grid-cols-4">+ <Detail label={messages.PROJECT_NAME} value={project?.name || "-"} />+ <Detail label={messages.SLUG} value={project?.slug || "-"} mono />+ <Detail label={messages.PROJECT_ENVIRONMENT} value={environments.length ? environments.join(", ") : "production"} />+ <Detail label={messages.CREATED_AT} value={createdAt} /></div></section>- </TabsContent>- </Tabs>++ <section className="grid gap-4 md:grid-cols-2">+ <InfoCard label={messages.PROJECT_ID} value={project?.id || "-"} mono />+ <InfoCard label={messages.PROJECT_CONNECTION_COUNT} value={`${environments.length}`} />+ </section>+ </main>+ </div>+ );+}++function Detail({ label, value, mono = false }: { label: string; value: string; mono?: boolean }) {+ return (+ <div className="rounded-md border border-stone-200 bg-stone-50 p-3 dark:border-stone-800 dark:bg-stone-900/60">+ <p className="text-[11px] uppercase tracking-wide text-muted-foreground">{label}</p>+ <p className={`mt-1 truncate text-sm font-medium text-stone-950 dark:text-stone-50 ${mono ? "font-mono text-xs" : ""}`}>{value}</p>+ </div>+ );+}++function InfoCard({ label, value, mono = false }: { label: string; value: string; mono?: boolean }) {+ return (+ <div className="border border-stone-200 bg-white p-4 dark:border-stone-800 dark:bg-stone-950">+ <p className="text-xs text-muted-foreground">{label}</p>+ <p className={`mt-2 truncate text-sm text-stone-950 dark:text-stone-50 ${mono ? "font-mono text-xs" : "font-medium"}`}>{value}</p></div>);}diff --git a/src/client/src/components/(playground)/pie-chart-card.tsx b/src/client/src/components/(playground)/pie-chart-card.tsxindex 57bb54a7..85e816b4 100644--- a/src/client/src/components/(playground)/pie-chart-card.tsx+++ b/src/client/src/components/(playground)/pie-chart-card.tsx@@ -7,7 +7,7 @@ import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";import { PieChart, Pie, Sector, ResponsiveContainer } from "recharts";import { COLORS } from "../../../styles/colors";import IntermediateState from "./intermediate-state";-import { Skeleton } from "../ui/skeleton";+import { Loader2 } from "lucide-react";import { getFilterParamsForDashboard } from "@/helpers/client/filter";const renderActiveShape = (props: any) => {@@ -154,8 +154,11 @@ const PieChartCard = memo(<CardContent className="h-60">{isLoading || !isFetched || pingStatus === "pending" ? (<IntermediateState type="loading">- <div className="flex w-full h-full items-center justify-center">- <Skeleton className={`h-9 w-1/3 rounded`} />+ <div className="flex w-full h-full items-center justify-center gap-2">+ <Loader2 className="h-5 w-5 animate-spin text-stone-500 dark:text-stone-400" />+ <span className="text-sm text-stone-500 dark:text-stone-400">+ Loading...+ </span></div></IntermediateState>) : updatedData?.length > 0 ? (diff --git a/src/client/src/components/(playground)/request/components/evaluations.tsx b/src/client/src/components/(playground)/request/components/evaluations.tsxindex 31a5b9b0..7f3e8f9f 100644--- a/src/client/src/components/(playground)/request/components/evaluations.tsx+++ b/src/client/src/components/(playground)/request/components/evaluations.tsx@@ -115,9 +115,11 @@ function EvaluationCard({function ManualFeedbackForm({spanId,+ traceId,onSuccess,}: {spanId: string;+ traceId?: string;onSuccess: () => void;}) {const [rating, setRating] = useState<@@ -128,8 +130,10 @@ function ManualFeedbackForm({const handleSubmit = () => {if (!rating) return;+ const params = new URLSearchParams();+ if (traceId) params.set("traceId", traceId);fireRequest({- url: `/api/evaluation/${spanId}/feedback`,+ url: `/api/evaluation/${spanId}/feedback${params.size ? `?${params}` : ""}`,requestType: "POST",body: JSON.stringify({ rating, comment: comment.trim() || undefined }),successCb: () => {@@ -442,8 +446,10 @@ export default function Evaluations({} = useFetchWrapper();const runEvaluation = () => {+ const params = new URLSearchParams();+ if (trace.id) params.set("traceId", String(trace.id));runEvaluationRequest({- url: `/api/evaluation/${trace.spanId}`,+ url: `/api/evaluation/${trace.spanId}${params.size ? `?${params}` : ""}`,requestType: "POST",responseDataKey: "data",successCb: (data: { success: boolean; error?: string }) => {@@ -460,8 +466,10 @@ export default function Evaluations({};const getEvaluations = () => {+ const params = new URLSearchParams();+ if (trace.id) params.set("traceId", String(trace.id));fireRequest({- url: `/api/evaluation/${trace.spanId}`,+ url: `/api/evaluation/${trace.spanId}${params.size ? `?${params}` : ""}`,requestType: "GET",});};@@ -622,7 +630,11 @@ export default function Evaluations({{/* Manual feedback */}<div className="space-y-3 pt-3"><FeedbackList feedbacks={feedbacks} />- <ManualFeedbackForm spanId={trace.spanId} onSuccess={getEvaluations} />+ <ManualFeedbackForm+ spanId={trace.spanId}+ traceId={trace.id}+ onSuccess={getEvaluations}+ /></div></div>,{diff --git a/src/client/src/components/(playground)/request/components/trace-ai-analysis-panel.tsx b/src/client/src/components/(playground)/request/components/trace-ai-analysis-panel.tsxindex 0807f96f..fe233b32 100644--- a/src/client/src/components/(playground)/request/components/trace-ai-analysis-panel.tsx+++ b/src/client/src/components/(playground)/request/components/trace-ai-analysis-panel.tsx@@ -7,6 +7,8 @@ export type TraceAiAnalysisScope = "trace" | "span";interface TraceAiAnalysisPanelProps {spanId: string;+ traceId?: string;+ environment?: string;scope?: TraceAiAnalysisScope;title?: string;description?: string;@@ -14,6 +16,8 @@ interface TraceAiAnalysisPanelProps {export default function TraceAiAnalysisPanel({spanId,+ traceId,+ environment,scope = "trace",title,description,@@ -23,6 +27,8 @@ export default function TraceAiAnalysisPanel({return (<TraceImprovementViewspanId={spanId}+ traceId={traceId}+ environment={environment}scope={scope}title={title || m.TRACE_AI_IMPROVEMENT_TITLE}description={diff --git a/src/client/src/components/(playground)/request/components/trace-improvement-view.tsx b/src/client/src/components/(playground)/request/components/trace-improvement-view.tsxindex 638debd6..82130b73 100644--- a/src/client/src/components/(playground)/request/components/trace-improvement-view.tsx+++ b/src/client/src/components/(playground)/request/components/trace-improvement-view.tsx@@ -20,6 +20,7 @@ import {import { Button } from "@/components/ui/button";import ReactMarkdown from "react-markdown";import { toast } from "sonner";+import { getRequestHeaders } from "@/utils/api";import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";import { Badge } from "@/components/ui/badge";import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion";@@ -485,11 +486,15 @@ function FindingCard({export default function TraceImprovementView({spanId,+ traceId,+ environment,scope = "trace",title,description,}: {spanId: string;+ traceId?: string;+ environment?: string;scope?: "trace" | "span";title?: string;description?: string;@@ -526,8 +531,13 @@ export default function TraceImprovementView({) => {try {setIsLoading(true);- const scopeParam = targetScope === "span" ? "?scope=span" : "";- const res = await fetch(`/api/chat/improvement/${targetSpanId}${scopeParam}`);+ const params = new URLSearchParams();+ if (targetScope === "span") params.set("scope", "span");+ if (traceId) params.set("traceId", traceId);+ const query = params.toString() ? `?${params.toString()}` : "";+ const res = await fetch(`/api/chat/improvement/${targetSpanId}${query}`, {+ headers: getRequestHeaders(),+ });if (!res.ok) {const err = await res.json();throw new Error(typeof err === "string" ? err : m.TRACE_AI_LOAD_FAILED);@@ -612,10 +622,14 @@ export default function TraceImprovementView({const timeoutId = setTimeout(() => abortController.abort(), 120_000);try {- const scopeParam = scope === "span" ? "?scope=span" : "";- const res = await fetch(`/api/chat/improvement/${spanId}${scopeParam}`, {+ const params = new URLSearchParams();+ if (scope === "span") params.set("scope", "span");+ if (traceId) params.set("traceId", traceId);+ const query = params.toString() ? `?${params.toString()}` : "";+ const res = await fetch(`/api/chat/improvement/${spanId}${query}`, {method: "POST",signal: abortController.signal,+ headers: getRequestHeaders(),});if (!res.ok || !res.body) {const err = await res.json();@@ -681,8 +695,7 @@ export default function TraceImprovementView({setIsFetched(false);setSteps([]);if (spanId) fetchAnalysis(spanId, scope, requestKey);- // eslint-disable-next-line react-hooks/exhaustive-deps- }, [spanId, scope]);+ }, [spanId, scope, traceId, environment]);const persistedRuns = useMemo<AnalysisRun[]>(() => {const runs = analysis?.data?.runs || [];diff --git a/src/client/src/components/(playground)/request/grouped-table.tsx b/src/client/src/components/(playground)/request/grouped-table.tsxindex 4fcf6151..743e3d54 100644--- a/src/client/src/components/(playground)/request/grouped-table.tsx+++ b/src/client/src/components/(playground)/request/grouped-table.tsx@@ -128,7 +128,7 @@ function GroupRow({export default function GroupedTable({groupBy,- apiUrl = "/api/metrics/request/grouped",+ apiUrl = "/api/telemetry/request/grouped",}: {groupBy: string;apiUrl?: string;diff --git a/src/client/src/components/(playground)/rule-engine/condition-builder.tsx b/src/client/src/components/(playground)/rule-engine/condition-builder.tsxindex 0a302720..5d9bb73f 100644--- a/src/client/src/components/(playground)/rule-engine/condition-builder.tsx+++ b/src/client/src/components/(playground)/rule-engine/condition-builder.tsx@@ -31,6 +31,7 @@ import {} from "@/components/ui/tooltip";import { useRootStore } from "@/store";import getMessage from "@/constants/messages";+import { getRequestHeaders } from "@/utils/api";export const CONDITION_FIELDS = () => {const m = getMessage();@@ -142,6 +143,9 @@ function ConditionValueInput({onChange: (v: string) => void;}) {const messages = getMessage();+ const currentEnvironment = useRootStore(+ (s) => s.project?.currentEnvironment || "production"+ );const [open, setOpen] = useState(false);const [inputValue, setInputValue] = useState("");const inputRef = useRef<HTMLInputElement>(null);@@ -158,7 +162,11 @@ function ConditionValueInput({const staticOptions = fieldDefinition?.valueOptions;const staticOptionValues = staticOptions || [];const staticOptionsKey = staticOptionValues.join("|");- const cacheKey = valueSourceUrl || (staticOptionValues.length ? `static:${field}:${staticOptionsKey}` : "");+ const cacheKey = valueSourceUrl+ ? `${valueSourceUrl}::${currentEnvironment}`+ : staticOptionValues.length+ ? `static:${field}:${staticOptionsKey}`+ : "";const cached = cacheKey ? fieldValuesCache[cacheKey] ?? null : null;const labels = cacheKey ? fieldLabelsCache[cacheKey] ?? null : null;const isLoading = cacheKey ? fieldValuesLoading[cacheKey] ?? false : false;@@ -181,8 +189,11 @@ function ConditionValueInput({return;}setFieldValuesLoading(cacheKey, true);- fetch(valueSourceUrl)- .then((r) => r.json())+ fetch(valueSourceUrl, { headers: getRequestHeaders() })+ .then((r) => {+ if (!r.ok) throw new Error(`Field value request failed (${r.status})`);+ return r.json();+ }).then((d) => {setFieldValues(cacheKey, d.values ?? []);if (d.labels && typeof d.labels === "object" && !Array.isArray(d.labels)) {diff --git a/src/client/src/components/(playground)/rule-engine/rule-preview-section.tsx b/src/client/src/components/(playground)/rule-engine/rule-preview-section.tsxindex f9d4ffef..38efa2e0 100644--- a/src/client/src/components/(playground)/rule-engine/rule-preview-section.tsx+++ b/src/client/src/components/(playground)/rule-engine/rule-preview-section.tsx@@ -11,6 +11,7 @@ import {TooltipTrigger,} from "@/components/ui/tooltip";import getMessage from "@/constants/messages";+import { getRequestHeaders } from "@/utils/api";type PreviewResult = {TraceId: string;@@ -32,6 +33,7 @@ export default function RulePreviewSection({ ruleId }: { ruleId: string }) {try {const res = await fetch(`/api/rule-engine/rules/${ruleId}/preview`, {method: "POST",+ headers: getRequestHeaders(),});const json = await res.json();if (!res.ok || json.error) {diff --git a/src/client/src/components/(playground)/sidebar/database-config-switch.tsx b/src/client/src/components/(playground)/sidebar/database-config-switch.tsxindex 3b70b754..493edf01 100644--- a/src/client/src/components/(playground)/sidebar/database-config-switch.tsx+++ b/src/client/src/components/(playground)/sidebar/database-config-switch.tsx@@ -1,112 +1,163 @@-import { getDatabaseConfigList } from "@/selectors/database-config";-import { getCurrentProject } from "@/selectors/project";-import { useRootStore } from "@/store";-import { useEffect } from "react";-import {- changeActiveDatabaseConfig,- fetchDatabaseConfigList,-} from "@/helpers/client/database-config";-import { usePostHog } from "posthog-js/react";-import { CLIENT_EVENTS } from "@/constants/events";-import { DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";+"use client";++import { useEffect, useState } from "react";import Link from "next/link";+import { Cable, ChevronDown, Plus } from "lucide-react";import { useRouter } from "next/navigation";-import { ChevronDown } from "lucide-react";-import getMessage from "@/constants/messages";+import { getCurrentProject, getCurrentProjectEnvironment } from "@/selectors/project";+import { useRootStore } from "@/store";+import { Button } from "@/components/ui/button";+import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";+import { Input } from "@/components/ui/input";+import { Label } from "@/components/ui/label";+import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";import { cn } from "@/lib/utils";import { headerScopeTriggerClassName } from "../header-scope-pill";+import getMessage from "@/constants/messages";-type DatabaseConfigSwitchProps = {- className?: string;- contentAlign?: "center" | "end" | "start";- contentSide?: "bottom" | "left" | "right" | "top";-};--const triggerClasses = headerScopeTriggerClassName;+type Environment = { name: string };export default function DatabaseConfigSwitch({- className,- contentAlign = "start",- contentSide = "right",-}: DatabaseConfigSwitchProps) {- const posthog = usePostHog();- const router = useRouter();- const messages = getMessage();- const list = useRootStore(getDatabaseConfigList) || [];- const currentProject = useRootStore(getCurrentProject);- const activeDatabase = list.find((item) => !!item.isCurrent);- const manageDbConfigHref = currentProject?.id- ? `/organisation/project/${currentProject.id}`- : "/organisation";- const onClickItem = (id: string) => {- changeActiveDatabaseConfig(id, () => {- posthog?.capture(CLIENT_EVENTS.DB_CONFIG_ACTION_CHANGE);- });- };+ className,+ contentAlign = "start",+ contentSide = "right",+}: {+ className?: string;+ contentAlign?: "center" | "end" | "start";+ contentSide?: "bottom" | "left" | "right" | "top";+}) {+ const messages = getMessage();+ const router = useRouter();+ const project = useRootStore(getCurrentProject);+ const currentEnvironment = useRootStore(getCurrentProjectEnvironment);+ const [environments, setEnvironments] = useState<Environment[]>([]);+ const [persistedEnvironment, setPersistedEnvironment] = useState<string>();+ const [createOpen, setCreateOpen] = useState(false);+ const [name, setName] = useState("");+ const [saving, setSaving] = useState(false);++ const environmentStorageKey = project?.id ? `openlit:environment:${project.id}` : "";+ const selectedEnvironment = currentEnvironment || persistedEnvironment || "production";++ useEffect(() => {+ if (!environmentStorageKey) return;+ const stored = window.localStorage.getItem(environmentStorageKey);+ const environment = stored || "production";+ setPersistedEnvironment(environment);+ useRootStore.getState().project.setCurrentEnvironment(environment);+ }, [environmentStorageKey]);- useEffect(() => {- fetchDatabaseConfigList((data: any[]) => {- posthog?.capture(CLIENT_EVENTS.DB_CONFIG_LIST, {- count: data.length,- });- });- }, []);+ const loadEnvironments = async () => {+ const response = await fetch("/api/project/environment");+ if (!response.ok) return;+ const body = await response.json();+ setEnvironments(+ Array.from(+ new Set<string>([+ "production",+ ...(body.environments || []).map((item: Environment) => item.name),+ ])+ )+ .sort()+ .map((environment) => ({ name: environment }))+ );+ };++ useEffect(() => {+ void loadEnvironments();+ }, [project?.id]);++ const selectEnvironment = async (environment: string) => {+ if (environmentStorageKey) {+ window.localStorage.setItem(environmentStorageKey, environment);+ setPersistedEnvironment(environment);+ }+ useRootStore.getState().project.setCurrentEnvironment(environment);+ window.dispatchEvent(new CustomEvent("openlit:environment-changed", { detail: { environment } }));+ router.refresh();+ };- // Show manage button when no database configs exist- if (list.length === 0) {- return (- <button- type="button"- className={cn(triggerClasses, className)}- onClick={() => router.push(manageDbConfigHref)}- >- <span className="min-w-0 truncate">{messages.MANAGE_DB_CONFIG}</span>- <ChevronDown className="size-3 shrink-0 opacity-50" />- </button>- );- }+ const createEnvironment = async () => {+ if (!name.trim()) return;+ setSaving(true);+ try {+ const response = await fetch("/api/project/environment", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ name }),+ });+ if (!response.ok) return;+ const body = await response.json();+ await loadEnvironments();+ await selectEnvironment(body.environment.name);+ setName("");+ setCreateOpen(false);+ } finally {+ setSaving(false);+ }+ };- const activeDatabaseId = activeDatabase?.id;- const displayDatabaseName = activeDatabase?.name || messages.PLEASE_SELECT;+ return (+ <>+ <div className={cn("flex items-center gap-1", className)}>+ <DropdownMenu>+ <DropdownMenuTrigger asChild>+ <button type="button" className={headerScopeTriggerClassName}>+ <span className="min-w-0 truncate">{selectedEnvironment}</span>+ <ChevronDown className="size-3 shrink-0 opacity-50" />+ </button>+ </DropdownMenuTrigger>+ <DropdownMenuContent className="w-64" side={contentSide} align={contentAlign}>+ <DropdownMenuLabel>Environments</DropdownMenuLabel>+ <DropdownMenuSeparator />+ {environments.map((environment) => {+ return (+ <DropdownMenuItem+ key={environment.name}+ onSelect={() => void selectEnvironment(environment.name)}+ >+ <span className="font-medium">{environment.name}</span>+ </DropdownMenuItem>+ );+ })}+ <DropdownMenuItem asChild>+ <Link href={project?.id ? `/organisation/project/${project.id}/environments` : "/organisation"}>+ <Cable className="mr-2 size-3.5" />{messages.PROJECT_ENVIRONMENTS}+ </Link>+ </DropdownMenuItem>+ <DropdownMenuSeparator />+ <DropdownMenuItem onSelect={() => setCreateOpen(true)}>+ <Plus className="mr-2 size-3.5" />+ Create environment+ </DropdownMenuItem>+ </DropdownMenuContent>+ </DropdownMenu>+ </div>- return (- <DropdownMenu>- <DropdownMenuTrigger asChild>- <button type="button" className={cn(triggerClasses, className)}>- <span className="min-w-0 truncate">{displayDatabaseName}</span>- <ChevronDown className="size-3 shrink-0 opacity-50" />- </button>- </DropdownMenuTrigger>- <DropdownMenuContent className="w-56" side={contentSide} align={contentAlign}>- <DropdownMenuLabel>{messages.DATABASES}</DropdownMenuLabel>- <DropdownMenuSeparator />- {list.map((item) => (- <DropdownMenuCheckboxItem- key={item.id}- checked={item.id === activeDatabaseId}- onCheckedChange={() => onClickItem(item.id)}- >- <div className="flex items-start text-muted-foreground ">- <div className="grid">- <p>- <span className="font-medium text-foreground">- {item.name}- </span>- </p>- <p className="text-xs" data-description>- {item.environment}- </p>- </div>- </div>- </DropdownMenuCheckboxItem>- ))}- <DropdownMenuSeparator />- <DropdownMenuItem className="py-1.5 pl-8 pr-2">- <Link href={manageDbConfigHref} className=" flex items-center">- {messages.ADD_NEW_CONFIG}- </Link>- </DropdownMenuItem>- </DropdownMenuContent>- </DropdownMenu>- );+ <Dialog open={createOpen} onOpenChange={setCreateOpen}>+ <DialogContent>+ <DialogHeader>+ <DialogTitle>Create environment</DialogTitle>+ <DialogDescription>+ Environments group the ClickHouse database and telemetry connectors used by this project.+ </DialogDescription>+ </DialogHeader>+ <div className="space-y-2">+ <Label htmlFor="global-new-environment">Environment name</Label>+ <Input+ id="global-new-environment"+ value={name}+ onChange={(event) => setName(event.target.value.toLowerCase())}+ placeholder="staging"+ />+ </div>+ <DialogFooter>+ <Button type="button" onClick={() => void createEnvironment()} disabled={saving || !name.trim()}>+ {saving ? "Creating…" : "Create environment"}+ </Button>+ </DialogFooter>+ </DialogContent>+ </Dialog>+ </>+ );}diff --git a/src/client/src/components/(playground)/sidebar/index.tsx b/src/client/src/components/(playground)/sidebar/index.tsxindex 270a8d6c..8f767bad 100644--- a/src/client/src/components/(playground)/sidebar/index.tsx+++ b/src/client/src/components/(playground)/sidebar/index.tsx@@ -5,6 +5,7 @@ import { usePathname, useRouter, useSearchParams } from "next/navigation";import { useEffect, useMemo, useState } from "react";import {ChevronRight,+ Lock,Search,X,} from "lucide-react";@@ -21,6 +22,8 @@ import {} from "@/components/ui/command";import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";import { SIDEBAR_ITEMS } from "@/constants/sidebar";+import getMessage from "@/constants/messages";+import { useIsNavLinkLocked } from "@/features/nav-locks";import { cn } from "@/lib/utils";import { getCurrentUserId } from "@/selectors/user";import { useRootStore } from "@/store";@@ -73,9 +76,17 @@ function NavigationLink({onNavigate?: () => void;compact?: boolean;}) {+ const messages = getMessage();+ const isLocked = useIsNavLinkLocked(item.link);const content = <>{item.icon}<span className={cn("min-w-0 truncate", compact && "sr-only")}>{item.text}</span>+ {isLocked && !compact ? (+ <Lock+ className="ml-auto size-3.5 shrink-0 text-amber-700 dark:text-amber-300"+ aria-label={messages.NAV_FEATURE_LOCKED}+ />+ ) : null}</>;const className = cn("flex w-full items-center gap-2 rounded-lg px-2.5 py-2 text-left text-[13px] font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-stone-950 focus-visible:ring-offset-2 focus-visible:ring-offset-white dark:focus-visible:ring-stone-300 dark:focus-visible:ring-offset-stone-950",diff --git a/src/client/src/components/(playground)/stat-card.tsx b/src/client/src/components/(playground)/stat-card.tsxindex 8d2ded6b..9b344c06 100644--- a/src/client/src/components/(playground)/stat-card.tsx+++ b/src/client/src/components/(playground)/stat-card.tsx@@ -5,10 +5,10 @@ import { getFilterDetails } from "@/selectors/filter";import { useRootStore } from "@/store";import { getPingStatus } from "@/selectors/database-config";import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";-import { Skeleton } from "@/components/ui/skeleton";-import { LucideIcon, TrendingDown, TrendingUp } from "lucide-react";+import { LucideIcon, Loader2, TrendingDown, TrendingUp } from "lucide-react";import IntermediateState from "./intermediate-state";import { getFilterParamsForDashboard } from "@/helpers/client/filter";+import { formatCompactNumber } from "@/components/(playground)/manage-dashboard/board-creator/utils/formatters";type StatCardProps = {heading?: string;@@ -79,6 +79,8 @@ const StatCard = memo(: 0;const value = (data as Record<any, any>)?.[dataKey] || 0;+ const parsedValue = typeof parser === "function" ? parser(value) : value;+ const roundedValue = round(parsedValue, roundTo);return (<Card className="relative overflow-hidden">@@ -95,20 +97,23 @@ const StatCard = memo(<CardContent>{isLoadingData ? (<IntermediateState type="loading">- <Skeleton className={`h-4 w-full rounded-xl ${loadingClass}`} />+ <div className="flex items-center gap-2">+ <Loader2 className="h-4 w-4 animate-spin text-stone-500 dark:text-stone-400" />+ <span className="text-sm text-stone-500 dark:text-stone-400">+ Loading...+ </span>+ </div></IntermediateState>) : (<div- className={`font-semibold text-primary ${+ className={`max-w-full truncate font-semibold text-primary ${textClass.match(/text-(xs|sm|base|lg|xl|[2-9]xl)/)? "": "text-3xl"} ${textClass}`}+ title={`${textPrefix}${roundedValue}${textSuffix}`}>- {`${textPrefix}${round(- typeof parser === "function" ? parser(value) : value,- roundTo- )}${textSuffix}`}+ {`${textPrefix}${formatCompactNumber(roundedValue)}${textSuffix}`}</div>)}<spandiff --git a/src/client/src/components/(playground)/telemetry-source/data-sources-page.tsx b/src/client/src/components/(playground)/telemetry-source/data-sources-page.tsxnew file mode 100644index 00000000..e3c281d8--- /dev/null+++ b/src/client/src/components/(playground)/telemetry-source/data-sources-page.tsx@@ -0,0 +1,1151 @@+"use client";++import { useCallback, useEffect, useMemo, useState } from "react";+import { toast } from "sonner";+import {+ Database,+ Plus,+ Trash2,+ Wifi,+ Pencil,+ ShieldCheck,+ Settings2,+ BookOpen,+ ExternalLink,+} from "lucide-react";+import Image from "next/image";+import Link from "next/link";+import { Button } from "@/components/ui/button";+import DatabaseConfigPage from "@/components/(playground)/database-config/database-config-page";+import { Input } from "@/components/ui/input";+import { Label } from "@/components/ui/label";+import { Badge } from "@/components/ui/badge";+import { Switch } from "@/components/ui/switch";+import { Separator } from "@/components/ui/separator";+import {+ Select,+ SelectContent,+ SelectItem,+ SelectTrigger,+ SelectValue,+} from "@/components/ui/select";+import {+ Dialog,+ DialogContent,+ DialogDescription,+ DialogFooter,+ DialogHeader,+ DialogTitle,+} from "@/components/ui/dialog";+import getMessage from "@/constants/messages";+import { CLIENT_EVENTS } from "@/constants/events";+import FeatureAccess from "@/components/rbac/feature-access";+import {+ connectorCreateEventProps,+ signalRoutingChangedEventProps,+} from "@/helpers/client/connector-analytics";+import { usePostHog } from "posthog-js/react";++import type { FieldDef } from "@/lib/platform/connectors/datasource/types";+import { getDatabaseConfigList } from "@/selectors/database-config";+import { useRootStore } from "@/store";+import { getCurrentProjectEnvironment } from "@/selectors/project";+import { getRequestHeaders } from "@/utils/api";+import { isVisibleConnectorType } from "@/lib/platform/connectors/visible-types";++type Signal = "traces" | "logs" | "metrics" | "intelligence";+const SIGNALS: Signal[] = ["traces", "logs", "metrics", "intelligence"];+const BUILTIN = "builtin";++interface TypeDescriptor {+ type: string;+ displayName: string;+ description?: string;+ icon?: string;+ declaredSignals: Signal[];+ correlation?: { crossSignal: boolean; keys: string[] };+ configFields?: FieldDef[];+ authStyle?: "none" | "http" | "api-key" | "custom";+ authHelp?: string;+ docsUrl?: string;+}++/** Config fields for a type from the fetched descriptors (descriptor-driven). */+function fieldsForType(+ descriptors: TypeDescriptor[],+ type: string+): FieldDef[] {+ return descriptors.find((d) => d.type === type)?.configFields ?? [];+}++function isFieldVisible(+ field: FieldDef,+ values: Record<string, string | boolean>+): boolean {+ return !field.visibleWhen || values[field.visibleWhen.key] === field.visibleWhen.value;+}++interface SourceRow {+ id: string;+ name: string;+ type: string;+ environment: string;+ signals: string;+ settings: string;+ isDefault: boolean;+ hasSecret?: boolean;+}++interface BindingRow {+ signal: string;+ environment?: string;+ sourceId: string;+ sourceName: string | null;+ sourceType: string | null;+}++function parseSignals(csv: string): Signal[] {+ return csv+ .split(",")+ .map((s) => s.trim())+ .filter((s): s is Signal => SIGNALS.includes(s as Signal));+}++/** Select value for connector / binding ids (connector registry already prefixes telemetry:). */+function signalRoutingSelectValue(sourceId?: string | null): string | undefined {+ const raw = String(sourceId || "").trim();+ if (!raw) return undefined;+ if (raw.startsWith("builtin:") || raw.startsWith("telemetry:")) return raw;+ return `telemetry:${raw}`;+}++function ConnectorOption({+ value,+ name,+ type,+ icon,+ detail,+}: {+ value: string;+ name: string;+ type: string;+ icon?: string;+ detail?: string;+}) {+ return (+ <SelectItem value={value} className="items-center py-2">+ <div className="flex min-w-0 items-center gap-2.5">+ <div className="flex h-7 w-7 shrink-0 items-center justify-center rounded border border-stone-200 bg-white dark:border-stone-700 dark:bg-stone-950">+ <Image+ src={icon || "/images/connect.svg"}+ alt=""+ width={16}+ height={16}+ className="h-4 w-4 object-contain"+ />+ </div>+ <div className="min-w-0 flex-1 text-left">+ <p className="truncate text-xs font-medium text-stone-950 dark:text-stone-50">+ {name}+ </p>+ <p className="truncate text-[10px] text-muted-foreground">+ {type}+ {detail ? ` · ${detail}` : ""}+ </p>+ </div>+ </div>+ </SelectItem>+ );+}++async function jsonFetch(url: string, init?: RequestInit) {+ const res = await fetch(url, {+ ...init,+ headers: getRequestHeaders((init?.headers as Record<string, string> | undefined) || undefined),+ });+ const text = await res.text();+ let body: any = undefined;+ try {+ body = text ? JSON.parse(text) : undefined;+ } catch {+ body = text;+ }+ if (!res.ok) {+ const err =+ (body && (body.err || body.error || body.message)) ||+ (typeof body === "string" ? body : "Request failed");+ throw new Error(err);+ }+ return body;+}++export default function DataSourcesPage({+ projectId,+ showRouting = true,+ openType,+ onOpenTypeHandled,+}: {+ projectId?: string;+ showRouting?: boolean;+ openType?: string | null;+ onOpenTypeHandled?: () => void;+}) {+ const messages = getMessage();+ const posthog = usePostHog();+ const currentProjectEnvironment = useRootStore(getCurrentProjectEnvironment);+ const databaseConfigs = useRootStore(getDatabaseConfigList) || [];+ const [loading, setLoading] = useState(true);+ const [loadError, setLoadError] = useState<string | null>(null);+ const [sources, setSources] = useState<SourceRow[]>([]);+ const [descriptors, setDescriptors] = useState<TypeDescriptor[]>([]);+ const [bindings, setBindings] = useState<BindingRow[]>([]);+ const [editing, setEditing] = useState<SourceRow | "new" | null>(null);+ const [newType, setNewType] = useState<string | undefined>();+ const [testingId, setTestingId] = useState<string | null>(null);+ const environment = currentProjectEnvironment || "production";+ const visibleSources = useMemo(+ () => sources.filter((source) => (source.environment || "production") === environment),+ [environment, sources]+ );++ const load = useCallback(async () => {+ setLoading(true);+ setLoadError(null);+ setSources([]);+ setBindings([]);+ try {+ const [list, binds] = await Promise.all([+ jsonFetch("/api/connectors"),+ jsonFetch("/api/telemetry-source/binding"),+ ]);+ const connectors = list?.connectors || [];+ const clickHouseConfigs = connectors+ .filter((connector: any) => connector.type === "clickhouse")+ .map((connector: any) => {+ let settings: Record<string, any> = {};+ try { settings = JSON.parse(connector.settings || "{}"); } catch { /* use empty defaults */ }+ return {+ id: String(connector.id).replace(/^database:/, ""),+ name: connector.name,+ environment: connector.environment || "production",+ username: settings.username || "default",+ host: settings.host || "127.0.0.1",+ port: settings.port || "8123",+ database: settings.database || "openlit",+ query: settings.query || "",+ password: "",+ isCurrent: false,+ permissions: { canEdit: true, canDelete: true, canShare: true },+ };+ });+ useRootStore.getState().databaseConfig.setList(clickHouseConfigs);+ setSources(connectors.filter((connector: SourceRow) => isVisibleConnectorType(connector.type) && connector.type !== "clickhouse"));+ setDescriptors((list?.availableTypeDescriptors || []).filter((descriptor: TypeDescriptor) => isVisibleConnectorType(descriptor.type)));+ setBindings(binds?.bindings || []);+ } catch (e: any) {+ const message = e?.message || messages.DATA_SOURCE_LOAD_FAILED;+ setLoadError(message);+ toast.error(message, { id: "data-source-load" });+ } finally {+ setLoading(false);+ }+ }, [environment, messages.DATA_SOURCE_LOAD_FAILED]);++ useEffect(() => {+ if (openType) {+ setNewType(openType);+ setEditing("new");+ onOpenTypeHandled?.();+ }+ }, [onOpenTypeHandled, openType]);++ useEffect(() => {+ load();+ }, [load, projectId]);++ const bindingForSignal = useCallback(+ (signal: Signal) => bindings.find(+ (b) => b.signal === signal && (b.environment || "production") === environment+ ),+ [bindings, environment]+ );+ const environmentDatabases = useMemo(+ () => databaseConfigs.filter((db) => (db.environment || "production").toLowerCase() === environment),+ [databaseConfigs, environment]+ );++ const setBinding = async (signal: Signal, sourceId: string) => {+ const previous = bindingForSignal(signal);+ const previousSourceId = previous?.sourceId || null;+ const previousConnectorType = previous?.sourceType || null;+ const nextConnectorType =+ sourceId === BUILTIN+ ? null+ : sourceId.startsWith("builtin:")+ ? "clickhouse"+ : sources.find((source) => signalRoutingSelectValue(source.id) === sourceId || source.id === sourceId)?.type || null;+ toast.loading(messages.DATA_SOURCE_BINDING_SAVED, { id: "ds-bind" });+ try {+ if (sourceId === BUILTIN) {+ await jsonFetch(+ `/api/telemetry-source/binding?signal=${encodeURIComponent(signal)}&environment=${encodeURIComponent(environment)}`,+ { method: "DELETE" }+ );+ } else {+ let resolvedSourceId = sourceId;+ while (resolvedSourceId.startsWith("telemetry:")) {+ resolvedSourceId = resolvedSourceId.slice("telemetry:".length);+ }+ await jsonFetch("/api/telemetry-source/binding", {+ method: "PUT",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({+ signal,+ sourceId: resolvedSourceId,+ environment,+ }),+ });+ }+ posthog?.capture(+ CLIENT_EVENTS.SIGNAL_ROUTING_CHANGED,+ signalRoutingChangedEventProps({+ signal,+ environment,+ previousSourceId,+ nextSourceId: sourceId,+ previousConnectorType,+ nextConnectorType,+ })+ );+ toast.success(messages.DATA_SOURCE_BINDING_SAVED, { id: "ds-bind" });+ await load();+ } catch (e: any) {+ toast.error(e?.message || messages.DATA_SOURCE_BINDING_FAILED, {+ id: "ds-bind",+ });+ }+ };++ const removeSource = async (row: SourceRow) => {+ if (!window.confirm(messages.DATA_SOURCE_DELETE_CONFIRM(row.name))) return;+ toast.loading(messages.DATA_SOURCE_DELETED, { id: "ds-del" });+ try {+ await jsonFetch(`/api/connectors/${row.id}`, { method: "DELETE" });+ toast.success(messages.DATA_SOURCE_DELETED, { id: "ds-del" });+ await load();+ } catch (e: any) {+ toast.error(e?.message || messages.DATA_SOURCE_DELETE_FAILED, {+ id: "ds-del",+ });+ }+ };++ const testSource = async (row: SourceRow) => {+ setTestingId(row.id);+ toast.loading(messages.DATA_SOURCE_TESTING, { id: "ds-test" });+ try {+ const res = await jsonFetch(`/api/connectors/${row.id}/health`, {+ method: "POST",+ });+ const health = res?.health;+ const validation = res?.validation;+ if (!health?.ok) {+ const raw = String(health?.message || "");+ const usesHttpAuth =+ descriptors.find((d) => d.type === row.type)?.authStyle === "http";+ const hint =+ /401|no credentials|unauthorized/i.test(raw) && usesHttpAuth+ ? ` ${messages.DATA_SOURCE_AUTH_401_HINT}`+ : "";+ toast.error(+ (health?.message || messages.DATA_SOURCE_SAVE_FAILED) + hint,+ { id: "ds-test" }+ );+ return;+ }+ // Loki/Mimir (and other non-trace sources) skip AI-span validation.+ if (validation?.supported === false) {+ toast.success(messages.DATA_SOURCE_TEST_OK, { id: "ds-test" });+ return;+ }+ if (validation?.message) {+ const raw = String(validation.message);+ const hint = /401|no credentials|unauthorized/i.test(raw)+ ? ` ${messages.DATA_SOURCE_AUTH_401_HINT}`+ : "";+ toast.error(+ messages.DATA_SOURCE_TEST_VALIDATION_FAILED(raw) + hint,+ { id: "ds-test" }+ );+ return;+ }+ if (validation?.ok && validation.sampleCount > 0) {+ toast.success(messages.DATA_SOURCE_TEST_AI_OK(validation.sampleCount), {+ id: "ds-test",+ });+ } else {+ // Replace the loading toast with a finite, dismissible result. Using+ // toast.message here can leave the original loading notification+ // visible in Sonner when the validation returns zero samples.+ toast.success(messages.DATA_SOURCE_TEST_AI_NONE, { id: "ds-test" });+ }+ } catch (e: any) {+ toast.error(e?.message || messages.DATA_SOURCE_SAVE_FAILED, {+ id: "ds-test",+ });+ } finally {+ setTestingId(null);+ }+ };++ return (+ <div className="flex h-full w-full flex-col gap-4 overflow-auto text-stone-700 dark:text-stone-300">+ {showRouting && (+ <SignalRoutingSection+ projectId={projectId}+ environment={environment}+ databaseConfigs={databaseConfigs}+ sources={visibleSources}+ descriptors={descriptors}+ bindingForSignal={bindingForSignal}+ onSetBinding={setBinding}+ onAddConnector={() => {+ setNewType(undefined);+ setEditing("new");+ }}+ />+ )}+ <section className="grid gap-3 border border-stone-200 bg-white p-4 md:grid-cols-2 dark:border-stone-800 dark:bg-stone-950">+ <div className="-mx-4 -mt-4 flex flex-wrap items-start justify-between gap-3 border-b border-stone-200 p-4 md:col-span-2 dark:border-stone-800">+ <div>+ <div className="flex items-center gap-2">+ <Database className="h-4 w-4 text-primary" />+ <h2 className="text-sm font-semibold text-stone-950 dark:text-stone-50">Connectors · {environment}</h2>+ </div>+ <p className="mt-1 text-xs text-muted-foreground">{messages.PROJECT_CONNECTORS_DESCRIPTION} Add multiple ClickHouse targets and external integrations to each environment, then choose the connector used by each signal.</p>+ </div>+ <div className="flex items-center gap-2">+ <FeatureAccess access="connectors.create" hideWhenDenied>+ <Button size="sm" onClick={() => setEditing("new")}>+ <Plus className="mr-1.5 h-3.5 w-3.5" />+ {messages.DATA_SOURCE_ADD}+ </Button>+ </FeatureAccess>+ </div>+ </div>+ <div className="contents">+ <DatabaseConfigPage hideHeader hideEmpty />+ </div>+ <div className="contents">+ {/* External sources list */}+ {loading ? (+ <div className="contents">+ {[0].map((item) => (+ <div key={item} className="flex min-h-[168px] flex-col gap-4 rounded-lg border border-stone-200 bg-stone-50/70 p-4 dark:border-stone-800 dark:bg-stone-900/50">+ <div className="flex items-center gap-3"><div className="h-9 w-9 animate-pulse rounded-md bg-stone-200 dark:bg-stone-800" /><div className="space-y-2"><div className="h-3 w-32 animate-pulse rounded bg-stone-200 dark:bg-stone-800" /><div className="h-2.5 w-20 animate-pulse rounded bg-stone-200 dark:bg-stone-800" /></div></div>+ <div className="space-y-2"><div className="h-2.5 w-full animate-pulse rounded bg-stone-200 dark:bg-stone-800" /><div className="h-2.5 w-3/4 animate-pulse rounded bg-stone-200 dark:bg-stone-800" /></div>+ <div className="mt-auto h-8 animate-pulse rounded bg-stone-200 dark:bg-stone-800" />+ </div>+ ))}+ </div>+ ) : loadError ? (+ <div className="rounded-lg border border-error/30 bg-error/5 p-6 text-center dark:bg-error/10">+ <p className="text-sm font-semibold text-error">{messages.DATA_SOURCE_LOAD_FAILED}</p>+ <p className="mx-auto mt-1 max-w-xl text-xs text-muted-foreground">{loadError}</p>+ <Button size="sm" variant="outline" className="mt-4" onClick={() => void load()}>{messages.DATA_SOURCE_RETRY}</Button>+ </div>+ ) : visibleSources.length === 0 ? (+ <div className="flex flex-col items-center gap-1 py-10 text-center">+ <h3 className="text-base font-semibold text-stone-900 dark:text-stone-100">+ {messages.DATA_SOURCE_EMPTY_TITLE}+ </h3>+ <p className="max-w-md text-sm text-muted-foreground">+ {messages.DATA_SOURCE_EMPTY_DESCRIPTION}+ </p>+ </div>+ ) : (+ <div className="contents">+ {visibleSources.map((s) => (+ <div+ key={s.id}+ className="flex min-h-[168px] flex-col justify-between rounded-lg border border-stone-200 bg-stone-50/70 p-3 transition-colors hover:border-primary/40 hover:bg-primary/[0.03] dark:border-stone-800 dark:bg-stone-900/50 dark:hover:border-primary/50"+ >+ <div>+ <div className="flex items-center gap-2">+ <div className="flex h-9 w-9 items-center justify-center rounded-md border border-stone-200 bg-white dark:border-stone-700 dark:bg-stone-950">+ <Image src={descriptors.find((d) => d.type === s.type)?.icon || "/images/connect.svg"} alt="" width={24} height={24} className="h-6 w-6 object-contain" />+ </div>+ <span className="text-sm font-medium text-stone-950 dark:text-stone-50">+ {s.name}+ </span>+ <Badge variant="outline" className="text-[10px]">+ {s.type}+ </Badge>+ <Badge variant="secondary" className="text-[10px]">+ {s.environment || "production"}+ </Badge>+ {s.isDefault && (+ <Badge className="text-[10px]">default</Badge>+ )}+ {s.hasSecret && (+ <span+ className="flex items-center gap-0.5 text-[10px] text-emerald-600 dark:text-emerald-400"+ title={messages.DATA_SOURCE_CREDENTIALS_SET}+ >+ <ShieldCheck className="h-3 w-3" />+ </span>+ )}+ </div>+ <p className="mt-2 line-clamp-2 text-xs leading-5 text-muted-foreground">+ {descriptors.find((d) => d.type === s.type)?.description || `${s.type} telemetry connector.`}+ </p>+ <div className="flex flex-wrap gap-1">+ {parseSignals(s.signals).map((sig) => (+ <Badge+ key={sig}+ variant="secondary"+ className="text-[10px]"+ >+ {sig}+ </Badge>+ ))}+ </div>+ </div>+ <div className="mt-3 flex items-center justify-end gap-1 border-t border-stone-200 pt-2 dark:border-stone-800">+ <div className="flex items-center gap-1">+ <Button+ size="sm"+ variant="ghost"+ disabled={testingId === s.id}+ onClick={() => testSource(s)}+ >+ <Wifi className="mr-1 h-3.5 w-3.5" />+ {messages.DATA_SOURCE_TEST}+ </Button>+ <Button+ size="icon"+ variant="ghost"+ title="Edit connector and signal routing"+ aria-label={`Edit ${s.name} connector`}+ onClick={() => setEditing(s)}+ >+ <Pencil className="h-3.5 w-3.5" />+ </Button>+ <Button+ size="sm"+ variant="ghost"+ onClick={() => removeSource(s)}+ >+ <Trash2 className="h-3.5 w-3.5 text-error" />+ </Button>+ </div>+ </div>+ </div>+ ))}+ </div>+ )}+ </div>+ </section>++ {editing && (+ <SourceFormDialog+ source={editing === "new" ? null : editing}+ descriptors={descriptors}+ initialType={editing === "new" ? newType : undefined}+ initialEnvironment={environment}+ showRouting={showRouting || editing !== "new"}+ bindingForSignal={bindingForSignal}+ onSetBinding={setBinding}+ bindings={bindings}+ sources={visibleSources}+ databaseConfigs={databaseConfigs}+ environment={environment}+ onClose={() => {+ setEditing(null);+ setNewType(undefined);+ }}+ onSaved={async () => {+ setEditing(null);+ setNewType(undefined);+ await load();+ }}+ />+ )}++ </div>+ );+}++function FieldInput({+ field,+ value,+ onChange,+}: {+ field: FieldDef;+ value: string | boolean;+ onChange: (v: string | boolean) => void;+}) {+ if (field.kind === "switch") {+ return (+ <div className="flex items-center justify-between rounded-md border border-stone-200 px-3 py-2 dark:border-stone-800">+ <Label className="text-xs">{field.label}</Label>+ <Switch+ checked={!!value}+ onCheckedChange={(c) => onChange(c)}+ />+ </div>+ );+ }+ if (field.kind === "select" && field.options) {+ return (+ <div className="space-y-1.5">+ <Label className="text-xs">{field.label}</Label>+ <Select value={String(value)} onValueChange={(v) => onChange(v)}>+ <SelectTrigger className="bg-white dark:bg-stone-900">+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ {field.options.map((o) => (+ <SelectItem key={o.value} value={o.value}>+ {o.label}+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ </div>+ );+ }+ return (+ <div className="space-y-1.5">+ <Label className="text-xs">{field.label}</Label>+ <Input+ type={field.kind === "password" ? "password" : "text"}+ value={String(value ?? "")}+ placeholder={field.placeholder}+ onChange={(e) => onChange(e.target.value)}+ className="bg-white dark:bg-stone-900"+ />+ {field.description ? (+ <p className="text-[11px] leading-4 text-muted-foreground">{field.description}</p>+ ) : null}+ </div>+ );+}++function SignalRoutingSection({+ projectId,+ environment,+ databaseConfigs,+ sources,+ descriptors,+ bindingForSignal,+ onSetBinding,+ onAddConnector,+}: {+ projectId?: string;+ environment: string;+ databaseConfigs: any[];+ sources: SourceRow[];+ descriptors: TypeDescriptor[];+ bindingForSignal: (signal: Signal) => BindingRow | undefined;+ onSetBinding: (signal: Signal, sourceId: string) => Promise<void>;+ onAddConnector?: () => void;+}) {+ const messages = getMessage();+ const environmentDatabases = databaseConfigs.filter(+ (db) => (db.environment || "production").toLowerCase() === environment+ );+ return (+ <section className="border border-stone-200 bg-white p-4 dark:border-stone-800 dark:bg-stone-950">+ <div className="mb-1 flex items-center gap-2">+ <Database className="h-4 w-4 text-primary" />+ <h2 className="text-sm font-semibold text-stone-950 dark:text-stone-50">{messages.DATA_SOURCE_SIGNAL_ROUTING_TITLE}</h2>+ </div>+ <p className="mb-3 text-xs text-muted-foreground">{messages.DATA_SOURCE_SIGNAL_ROUTING_DESCRIPTION}</p>+ <div className="grid gap-3 sm:grid-cols-3">+ {SIGNALS.map((signal) => {+ const binding = bindingForSignal(signal);+ const value = signalRoutingSelectValue(binding?.sourceId);+ const options = sources.filter((source) => parseSignals(source.signals).includes(signal));+ const hasOptions = environmentDatabases.length > 0 || options.length > 0;+ const label = signal === "traces" ? messages.DATA_SOURCE_SIGNAL_TRACES : signal === "logs" ? messages.DATA_SOURCE_SIGNAL_LOGS : signal === "metrics" ? messages.DATA_SOURCE_SIGNAL_METRICS : "Intelligence";+ return (+ <div key={signal} className="space-y-1.5">+ <Label className="text-xs uppercase text-muted-foreground">{label}</Label>+ {hasOptions ? (+ <FeatureAccess access="connectors.bind" hideWhenDenied>+ <Select value={value} onValueChange={(next) => onSetBinding(signal, next)}>+ <SelectTrigger className="h-auto min-h-12 items-center gap-2 overflow-hidden border-stone-300 bg-white py-2 text-left text-stone-950 [&>span]:min-w-0 [&>span]:flex-1 [&>span]:line-clamp-none dark:border-stone-700 dark:bg-stone-900 dark:text-stone-50"><SelectValue placeholder="Select a connector" /></SelectTrigger>+ <SelectContent className="w-[var(--radix-select-trigger-width)] min-w-[var(--radix-select-trigger-width)] max-w-[var(--radix-select-trigger-width)]">+ {environmentDatabases.map((db) => <ConnectorOption key={db.id} value={`builtin:${db.id}`} name={db.name} type="ClickHouse" detail={environment} icon="/images/connectors/clickhouse.svg" />)}+ {options.map((source) => <ConnectorOption key={source.id} value={signalRoutingSelectValue(source.id)!} name={source.name} type={source.type} detail={environment} icon={descriptors.find((descriptor) => descriptor.type === source.type)?.icon} />)}+ </SelectContent>+ </Select>+ </FeatureAccess>+ ) : (+ <div className="flex min-h-12 items-center justify-between rounded-md border border-dashed border-stone-300 bg-stone-50 px-3 text-xs text-muted-foreground dark:border-stone-700 dark:bg-stone-900/60">+ <span>No connector configured</span>+ {onAddConnector ? (+ <FeatureAccess access="connectors.create" hideWhenDenied>+ <button+ type="button"+ className="font-medium text-primary hover:underline"+ onClick={onAddConnector}+ >+ {messages.ADD_CONNECTOR}+ </button>+ </FeatureAccess>+ ) : (+ <Link+ className="font-medium text-primary hover:underline"+ href={projectId ? `/organisation/project/${projectId}/connectors` : "/connectors"}+ >+ {messages.ADD_CONNECTOR}+ </Link>+ )}+ </div>+ )}+ </div>+ );+ })}+ </div>+ </section>+ );+}++function SignalRoutingEditor({+ sources,+ databaseConfigs,+ environment: routingEnvironment,+ bindingForSignal,+ onSetBinding,+ source,+}: {+ sources: SourceRow[];+ databaseConfigs: any[];+ environment: string;+ bindingForSignal: (signal: Signal) => BindingRow | undefined;+ onSetBinding: (signal: Signal, sourceId: string) => Promise<void>;+ source: SourceRow;+}) {+ const messages = getMessage();+ return (+ <div className="space-y-2 rounded-lg border border-primary/20 bg-primary/5 p-3 dark:border-primary/30 dark:bg-primary/10">+ <div>+ <p className="text-xs font-semibold text-stone-950 dark:text-stone-50">{messages.DATA_SOURCE_SIGNAL_ROUTING_TITLE}</p>+ <p className="text-[11px] leading-4 text-muted-foreground">{messages.DATA_SOURCE_SIGNAL_ROUTING_DIALOG_DESCRIPTION(routingEnvironment)}</p>+ </div>+ <div className="grid gap-2 sm:grid-cols-3">+ {SIGNALS.map((signal) => {+ const binding = bindingForSignal(signal);+ const value = signalRoutingSelectValue(binding?.sourceId);+ const eligibleSources = sources.filter((item) => parseSignals(item.signals).includes(signal));+ const routingDatabases = databaseConfigs.filter((db) => (db.environment || "production").toLowerCase() === routingEnvironment);+ const hasOptions = routingDatabases.length > 0 || eligibleSources.length > 0;+ return <div key={signal} className="space-y-1"><Label className="text-[11px] uppercase text-muted-foreground">{signal}</Label>{hasOptions ? <FeatureAccess access="connectors.bind" hideWhenDenied><Select value={value} onValueChange={(next) => onSetBinding(signal, next)}><SelectTrigger className="h-auto min-h-11 bg-white py-1.5 dark:bg-stone-900"><SelectValue placeholder="Select a connector" /></SelectTrigger><SelectContent className="w-[var(--radix-select-trigger-width)] min-w-[var(--radix-select-trigger-width)] max-w-[var(--radix-select-trigger-width)]">{routingDatabases.map((db) => <ConnectorOption key={db.id} value={`builtin:${db.id}`} name={db.name} type="ClickHouse" detail={routingEnvironment} icon="/images/connectors/clickhouse.svg" />)}{eligibleSources.map((item) => <ConnectorOption key={item.id} value={signalRoutingSelectValue(item.id)!} name={item.name} type={item.type} detail={routingEnvironment} />)}</SelectContent></Select></FeatureAccess> : <p className="rounded border border-dashed border-stone-300 p-2 text-[11px] text-muted-foreground dark:border-stone-700">No connector configured for {routingEnvironment}.</p>}</div>;+ })}+ </div>+ <p className="text-[11px] text-muted-foreground">{messages.DATA_SOURCE_SIGNAL_ROUTING_DIALOG_FOOTER(source.name, routingEnvironment)}</p>+ </div>+ );+}++function SourceFormDialog({+ source,+ descriptors,+ initialType,+ initialEnvironment,+ showRouting = false,+ bindingForSignal,+ onSetBinding,+ bindings,+ sources,+ databaseConfigs,+ environment: routingEnvironment,+ onClose,+ onSaved,+}: {+ source: SourceRow | null;+ descriptors: TypeDescriptor[];+ initialType?: string;+ initialEnvironment?: string;+ showRouting?: boolean;+ bindingForSignal?: (signal: Signal) => BindingRow | undefined;+ onSetBinding?: (signal: Signal, sourceId: string) => Promise<void>;+ bindings?: BindingRow[];+ sources?: SourceRow[];+ databaseConfigs?: any[];+ environment?: string;+ onClose: () => void;+ onSaved: () => void;+}) {+ const messages = getMessage();+ const posthog = usePostHog();+ const isEdit = !!source;+ const [name, setName] = useState(source?.name || "");+ const [environment, setEnvironment] = useState(source?.environment || initialEnvironment || "production");+ const [environments, setEnvironments] = useState<string[]>(Array.from(new Set(["production", source?.environment, initialEnvironment].filter(Boolean) as string[])));+ const externalDescriptors = useMemo(() => descriptors.filter((descriptor) => descriptor.type !== "clickhouse"), [descriptors]);+ const [type, setType] = useState(source?.type || initialType || externalDescriptors[0]?.type || "");+ const clickHouseFields = useMemo<FieldDef[]>(() => [+ { key: "username", label: messages.DB_CONFIG_FIELD_USERNAME, kind: "text", group: "settings", placeholder: "default" },+ { key: "host", label: messages.DB_CONFIG_FIELD_HOST, kind: "text", group: "settings", placeholder: "clickhouse.example.com" },+ { key: "port", label: messages.DB_CONFIG_FIELD_PORT, kind: "text", group: "settings", placeholder: "8123" },+ { key: "database", label: messages.DB_CONFIG_FIELD_DATABASE, kind: "text", group: "settings", placeholder: "openlit" },+ { key: "tracesTable", label: "Traces table", kind: "text", group: "settings", placeholder: "otel_traces", defaultValue: "otel_traces" },+ { key: "logsTable", label: "Logs table", kind: "text", group: "settings", placeholder: "otel_logs", defaultValue: "otel_logs" },+ { key: "metricsTable", label: "Metrics table", kind: "text", group: "settings", placeholder: "otel_metrics", defaultValue: "otel_metrics" },+ { key: "query", label: messages.DB_CONFIG_FIELD_QUERY_PARAMS, kind: "text", group: "settings", placeholder: "a=b&c=d" },+ { key: "password", label: messages.DB_CONFIG_FIELD_PASSWORD, kind: "password", group: "credentials", placeholder: "*******" },+ ], [messages]);+ const [isDefault, setIsDefault] = useState(!!source?.isDefault);+ const [values, setValues] = useState<Record<string, string | boolean>>({});+ const [saving, setSaving] = useState(false);++ useEffect(() => {+ fetch("/api/project/environment")+ .then((response) => response.ok ? response.json() : { environments: [] })+ .then((body) => setEnvironments(Array.from(new Set(["production", ...(body.environments || []).map((item: { name: string }) => item.name), environment]))))+ .catch(() => undefined);+ }, [environment]);++ const fields = useMemo(+ () => type === "clickhouse" ? clickHouseFields : fieldsForType(descriptors, type),+ [clickHouseFields, descriptors, type]+ );++ // Seed defaults + stored settings whenever the type (or source) changes.+ useEffect(() => {+ const next: Record<string, string | boolean> = {};+ let stored: Record<string, unknown> = {};+ if (source?.settings) {+ try {+ stored = JSON.parse(source.settings) || {};+ } catch {+ stored = {};+ }+ }+ for (const f of fields) {+ if (f.group === "settings") {+ next[f.key] =+ stored[f.key] !== undefined+ ? (stored[f.key] as string | boolean)+ : f.defaultValue ?? (f.kind === "switch" ? false : "");+ } else {+ next[f.key] = "";+ }+ }+ setValues(next);+ }, [fields, source]);++ const settingsFields = fields.filter(+ (f) => f.group === "settings" && f.key !== "authType" && isFieldVisible(f, values)+ );+ // authType is stored in settings JSON but shown with secrets under Authentication.+ const authenticationFields = fields.filter(+ (f) =>+ (f.key === "authType" || f.group === "credentials") &&+ isFieldVisible(f, { ...values })+ );++ const submit = async () => {+ if (!name.trim()) {+ toast.error(messages.TELEMETRY_SOURCE_NAME_REQUIRED);+ return;+ }+ const settings: Record<string, unknown> = {};+ for (const f of fields) {+ if (f.group === "settings" && isFieldVisible(f, values)) {+ settings[f.key] = values[f.key];+ }+ }+ const credentials: Record<string, string> = {};+ for (const f of fields) {+ if (f.group !== "credentials" || !isFieldVisible(f, values)) continue;+ const v = values[f.key];+ if (typeof v === "string" && v.trim() !== "") credentials[f.key] = v;+ }++ setSaving(true);+ toast.loading(messages.DATA_SOURCE_SAVED, { id: "ds-save" });+ try {+ if (type === "clickhouse") {+ const payload: Record<string, unknown> = {+ type: "clickhouse",+ category: "datasource",+ name: name.trim(),+ environment: environment.trim().toLowerCase() || "production",+ settings,+ };+ if (Object.keys(credentials).length) payload.credentials = credentials;+ await jsonFetch("/api/connectors", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify(payload),+ });+ posthog?.capture(+ CLIENT_EVENTS.CONNECTOR_CREATE_SUCCESS,+ connectorCreateEventProps({+ type: "clickhouse",+ environment,+ })+ );+ toast.success(messages.DATA_SOURCE_SAVED, { id: "ds-save" });+ onSaved();+ return;+ }+ const payload: Record<string, unknown> = {+ name: name.trim(),+ environment: environment.trim().toLowerCase() || "production",+ settings,+ isDefault,+ };+ if (Object.keys(credentials).length) payload.credentials = credentials;+ if (isEdit) {+ await jsonFetch(`/api/connectors/${source!.id}`, {+ method: "PATCH",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify(payload),+ });+ } else {+ if (!type) {+ toast.error(messages.TELEMETRY_SOURCE_TYPE_UNKNOWN(""));+ return;+ }+ payload.type = type;+ await jsonFetch("/api/connectors", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify(payload),+ });+ posthog?.capture(+ CLIENT_EVENTS.CONNECTOR_CREATE_SUCCESS,+ connectorCreateEventProps({+ type,+ environment,+ })+ );+ }+ toast.success(messages.DATA_SOURCE_SAVED, { id: "ds-save" });+ onSaved();+ } catch (e: any) {+ if (!isEdit) {+ posthog?.capture(+ CLIENT_EVENTS.CONNECTOR_CREATE_FAILURE,+ connectorCreateEventProps({+ type: type || "unknown",+ environment,+ })+ );+ }+ toast.error(e?.message || messages.DATA_SOURCE_SAVE_FAILED, {+ id: "ds-save",+ });+ } finally {+ setSaving(false);+ }+ };++ const activeDescriptor = descriptors.find((d) => d.type === type);+ const setupGuide = messages.DATA_SOURCE_SETUP_GUIDES[type];++ return (+ <Dialog open onOpenChange={(o) => !o && onClose()}>+ <DialogContent className="w-[calc(100vw-2rem)] max-w-4xl max-h-[92vh] overflow-y-auto border-stone-200 bg-white text-stone-950 shadow-2xl dark:border-stone-800 dark:bg-stone-950 dark:text-stone-50 sm:max-w-4xl">+ <DialogHeader>+ <DialogTitle>+ {isEdit ? messages.DATA_SOURCE_DETAILS : messages.DATA_SOURCE_ADD}+ </DialogTitle>+ <DialogDescription>+ {isEdit ? activeDescriptor?.description : messages.PROJECT_DATA_SOURCES_DESCRIPTION}+ </DialogDescription>+ </DialogHeader>++ <div className="space-y-4">+ <section className="rounded-lg border border-primary/25 bg-primary/[0.04] p-4 dark:border-primary/35 dark:bg-primary/[0.08]">+ <div className="mb-3 flex items-center gap-2">+ <Settings2 className="h-4 w-4 text-primary" />+ <div>+ <p className="text-xs font-semibold text-stone-950 dark:text-stone-50">{messages.DATA_SOURCE_CONNECTOR_SECTION}</p>+ <p className="text-[11px] text-muted-foreground">{messages.DATA_SOURCE_CONNECTOR_SECTION_DESCRIPTION}</p>+ </div>+ </div>+ <div className="space-y-1.5">+ <Label className="text-xs">{messages.DATA_SOURCE_FIELD_TYPE}</Label>+ <Select+ value={type}+ onValueChange={(nextType) => {+ setType(nextType);+ }}+ disabled={isEdit}+ >+ <SelectTrigger className="h-auto min-h-14 items-center gap-2 overflow-hidden border-stone-300 bg-white py-2.5 text-left text-stone-950 [&>span]:min-w-0 [&>span]:flex-1 [&>span]:line-clamp-none dark:border-stone-700 dark:bg-stone-900 dark:text-stone-50">+ <SelectValue placeholder="Select a connector">+ {activeDescriptor ? (+ <div className="flex min-w-0 items-center gap-2.5">+ <div className="flex h-8 w-8 shrink-0 items-center justify-center rounded border border-stone-200 bg-white dark:border-stone-700 dark:bg-stone-950">+ <Image src={activeDescriptor.icon || "/images/connect.svg"} alt="" width={20} height={20} className="h-5 w-5 object-contain" />+ </div>+ <div className="min-w-0">+ <p className="truncate text-sm font-medium">{activeDescriptor.displayName}</p>+ <p className="truncate text-[11px] text-muted-foreground">{activeDescriptor.description || `${activeDescriptor.displayName} telemetry connector.`}</p>+ </div>+ </div>+ ) : undefined}+ </SelectValue>+ </SelectTrigger>+ <SelectContent className="grid max-h-96 min-w-[var(--radix-select-trigger-width)] grid-cols-2 items-stretch gap-2 p-2 sm:min-w-[680px]">+ {descriptors.map((d) => (+ <SelectItem key={d.type} value={d.type} className="min-h-[72px] items-start py-2 pl-2 pr-8">+ <div className="flex items-start gap-3">+ <div className="mt-0.5 flex h-7 w-7 shrink-0 items-center justify-center rounded border border-stone-200 bg-white dark:border-stone-700 dark:bg-stone-950">+ <Image src={d.icon || "/images/connect.svg"} alt="" width={18} height={18} className="h-[18px] w-[18px] object-contain" />+ </div>+ <div className="min-w-0">+ <p className="font-medium">{d.displayName}</p>+ <p className="mt-0.5 line-clamp-2 max-w-[220px] whitespace-normal text-[10px] leading-4 text-muted-foreground">{d.description || `${d.displayName} telemetry connector.`}</p>+ </div>+ </div>+ </SelectItem>+ ))}+ </SelectContent>+ </Select>+ {isEdit && <p className="text-[11px] text-muted-foreground">{messages.DATA_SOURCE_TYPE_LOCKED}</p>}+ </div>+ {activeDescriptor && (+ <div className="mt-3 flex flex-wrap items-center gap-1.5">+ <span className="mr-1 text-[11px] text-muted-foreground">{messages.DATA_SOURCE_SIGNALS_SECTION}:</span>+ {activeDescriptor.declaredSignals.map((sig) => (+ <Badge key={sig} variant="secondary" className="text-[10px]">{sig}</Badge>+ ))}+ </div>+ )}+ </section>++ {setupGuide && (+ <details open className="rounded-lg border border-stone-200 bg-stone-50/70 p-4 dark:border-stone-800 dark:bg-stone-900/50">+ <summary className="flex cursor-pointer list-none items-center gap-2 text-xs font-semibold text-stone-950 marker:hidden dark:text-stone-50">+ <BookOpen className="h-4 w-4 text-primary" />+ {messages.DATA_SOURCE_SETUP_TITLE}+ </summary>+ <div className="mt-3 space-y-3 pl-6">+ <p className="text-[11px] leading-4 text-muted-foreground">{setupGuide.summary}</p>+ <ol className="list-decimal space-y-1.5 pl-4 text-[11px] leading-4 text-stone-700 dark:text-stone-300">+ {setupGuide.steps.map((step) => <li key={step}>{step}</li>)}+ </ol>+ <a href={setupGuide.docsUrl} target="_blank" rel="noreferrer noopener" className="inline-flex items-center gap-1 text-[11px] font-medium text-primary underline underline-offset-2">+ {messages.DATA_SOURCE_DOCS_LINK}<ExternalLink className="h-3 w-3" />+ </a>+ </div>+ </details>+ )}++ <section className="space-y-3 rounded-lg border border-stone-200 p-4 dark:border-stone-800">+ <div>+ <p className="text-xs font-semibold text-stone-950 dark:text-stone-50">{messages.DATA_SOURCE_CONNECTION_SECTION}</p>+ <p className="mt-0.5 text-[11px] text-muted-foreground">{messages.DATA_SOURCE_CONNECTION_SECTION_DESCRIPTION}</p>+ </div>+ <div className="grid gap-3 sm:grid-cols-2">+ <div className="space-y-1.5">+ <Label className="text-xs">{messages.DATA_SOURCE_FIELD_NAME}</Label>+ <Input+ value={name}+ onChange={(e) => setName(e.target.value)}+ placeholder="production-traces"+ className="bg-white dark:bg-stone-900"+ />+ </div>+ <div className="space-y-1.5">+ <Label className="text-xs">{messages.CONNECTOR_ENVIRONMENT}</Label>+ <Select value={environment} onValueChange={setEnvironment}>+ <SelectTrigger className="border-stone-300 bg-white text-stone-950 dark:border-stone-700 dark:bg-stone-900 dark:text-stone-50"><SelectValue placeholder={messages.CONNECTOR_ENVIRONMENT_PLACEHOLDER} /></SelectTrigger>+ <SelectContent>{environments.map((item) => <SelectItem key={item} value={item}>{item}</SelectItem>)}</SelectContent>+ </Select>+ </div>+ </div>+ </section>++ {settingsFields.length > 0 && <section className="space-y-3 rounded-lg border border-stone-200 p-4 dark:border-stone-800">+ <div><p className="text-xs font-semibold text-stone-950 dark:text-stone-50">{messages.DATA_SOURCE_SETTINGS_SECTION}</p><p className="mt-0.5 text-[11px] text-muted-foreground">{messages.DATA_SOURCE_SETTINGS_SECTION_DESCRIPTION}</p></div>+ <div className="grid gap-3 sm:grid-cols-2">{settingsFields.map((f) => (+ <FieldInput+ key={f.key}+ field={f}+ value={values[f.key] ?? ""}+ onChange={(v) => setValues((p) => ({ ...p, [f.key]: v }))}+ />))}</div>+ </section>}++ {authenticationFields.length > 0 && (+ <section className="space-y-3 rounded-lg border border-stone-200 p-4 dark:border-stone-800">+ <div>+ <p className="text-xs font-semibold text-stone-950 dark:text-stone-50">+ {fields.some((f) => f.key === "authType")+ ? messages.DATA_SOURCE_AUTHENTICATION_SECTION+ : messages.DATA_SOURCE_CREDENTIALS_TITLE}+ </p>+ <p className="text-xs text-muted-foreground">+ {isEdit && source?.hasSecret+ ? messages.DATA_SOURCE_CREDENTIALS_SET+ : messages.DATA_SOURCE_CREDENTIALS_HELP}+ </p>+ {activeDescriptor?.authHelp && (+ <p className="mt-1 text-xs text-muted-foreground">+ {activeDescriptor.authHelp}+ </p>+ )}+ {activeDescriptor?.docsUrl && (+ <a+ href={activeDescriptor.docsUrl}+ target="_blank"+ rel="noreferrer noopener"+ className="mt-1 inline-block text-xs text-primary underline"+ >+ {messages.DATA_SOURCE_DOCS_LINK}+ </a>+ )}+ </div>+ <div className="grid gap-3 sm:grid-cols-2">{authenticationFields.map((f) => (+ <FieldInput+ key={f.key}+ field={f}+ value={values[f.key] ?? ""}+ onChange={(v) =>+ setValues((p) => ({ ...p, [f.key]: v }))+ }+ />+ ))}</div>+ </section>+ )}++ {showRouting && source && bindingForSignal && onSetBinding && (+ <SignalRoutingEditor+ sources={sources || []}+ databaseConfigs={databaseConfigs || []}+ environment={routingEnvironment || "production"}+ bindingForSignal={bindingForSignal}+ onSetBinding={onSetBinding}+ source={source}+ />+ )}++ <div className="flex items-center justify-between rounded-md border border-stone-200 px-3 py-2 dark:border-stone-800">+ <Label className="text-xs">+ {messages.DATA_SOURCE_FIELD_DEFAULT}+ </Label>+ <Switch checked={isDefault} onCheckedChange={setIsDefault} />+ </div>+ </div>++ <DialogFooter>+ <Button variant="outline" onClick={onClose} disabled={saving}>+ {messages.CANCEL}+ </Button>+ <Button onClick={submit} disabled={saving}>+ {messages.SAVE}+ </Button>+ </DialogFooter>+ </DialogContent>+ </Dialog>+ );+}diff --git a/src/client/src/components/common/form-builder.tsx b/src/client/src/components/common/form-builder.tsxindex a3335419..723a15f0 100644--- a/src/client/src/components/common/form-builder.tsx+++ b/src/client/src/components/common/form-builder.tsx@@ -37,7 +37,8 @@ const FormBuilder = ({if (field.fieldType === "INPUT" ||field.fieldType === "TEXTAREA" ||- field.fieldType === "RADIOGROUP"+ field.fieldType === "RADIOGROUP" ||+ field.fieldType === "SELECT") {if (field.fieldTypeProps.name &&diff --git a/src/client/src/components/ui/custom-select.tsx b/src/client/src/components/ui/custom-select.tsxindex a8affc26..ce3f98e3 100644--- a/src/client/src/components/ui/custom-select.tsx+++ b/src/client/src/components/ui/custom-select.tsx@@ -29,6 +29,7 @@ export function CustomSelect({hasOtherOption,name,id,+ disabled = false,}: CustomSelectProps) {const [open, setOpen] = React.useState(false);const [value, setValue] = React.useState(defaultValue);@@ -94,6 +95,7 @@ export function CustomSelect({return (<div className="space-y-2">+ <input type="hidden" name={name} value={inputValue} readOnly /><Popover open={open} onOpenChange={setOpen}><PopoverTrigger asChild><Button@@ -101,6 +103,7 @@ export function CustomSelect({role="combobox"aria-expanded={open}className="w-full justify-between"+ disabled={disabled}ref={triggerRef}>{value === "other" && inputValuediff --git a/src/client/src/components/ui/date-range-picker.tsx b/src/client/src/components/ui/date-range-picker.tsxindex 6efd72e0..b505b746 100644--- a/src/client/src/components/ui/date-range-picker.tsx+++ b/src/client/src/components/ui/date-range-picker.tsx@@ -8,6 +8,7 @@ import { DateRange } from "react-day-picker";import { cn } from "@/lib/utils";import { Button } from "@/components/ui/button";import { Calendar } from "@/components/ui/calendar";+import getMessage from "@/constants/messages";import {Popover,PopoverContent,@@ -18,13 +19,16 @@ export function DatePickerWithRange({className,onCustomDateChange,selectedDate,+ maxRangeDays,}: React.HTMLAttributes<HTMLDivElement> & {onCustomDateChange: (start: Date, end: Date) => void;selectedDate?: {start?: Date;end?: Date;};+ maxRangeDays?: number;}) {+ const m = getMessage();const [date, setDate] = React.useState<DateRange | undefined>({from: selectedDate?.start || addDays(new Date(), -15),to: selectedDate?.end || new Date(),@@ -68,9 +72,17 @@ export function DatePickerWithRange({defaultMonth={date?.from}selected={date}onSelect={setDate}+ max={maxRangeDays}numberOfMonths={2}/>- <div className="flex justify-end px-3 pb-3">+ <div className="flex items-center justify-between gap-4 px-3 pb-3">+ {maxRangeDays ? (+ <p className="text-xs text-stone-500 dark:text-stone-400">+ {m.DATA_SOURCE_MAX_TIME_RANGE_HINT(maxRangeDays)}+ </p>+ ) : (+ <span />+ )}<ButtonclassName="w-20"variant="default"diff --git a/src/client/src/constants/documentation-links.ts b/src/client/src/constants/documentation-links.tsindex f152d1e8..1c8ade7c 100644--- a/src/client/src/constants/documentation-links.ts+++ b/src/client/src/constants/documentation-links.ts@@ -3,6 +3,11 @@ const DOCUMENTATION_LINKS = {openground: "https://docs.openlit.io/latest/openlit/prompts-experiments/openground",vault: "https://docs.openlit.io/latest/openlit/developer-resources/vault",multipleDb: "https://docs.openlit.io/latest/openlit/developer-resources/multiple-db",+ databaseConfig: "https://docs.openlit.io/latest/openlit/organisation/database-config",+ environments: "https://docs.openlit.io/latest/openlit/organisation/environments",+ connectors: "https://docs.openlit.io/latest/openlit/organisation/connectors",+ signalRouting: "https://docs.openlit.io/latest/openlit/organisation/signal-routing",+ telemetrySources: "https://docs.openlit.io/latest/openlit/organisation/telemetry-sources",anonymousTelemetry: "https://docs.openlit.io/latest/openlit/developer-resources/anonymous-telemetry",apiReference: "https://docs.openlit.io/latest/openlit/developer-resources/api-reference",promptHubApiReference: "https://docs.openlit.io/latest/openlit/developer-resources/api-reference/endpoint/prompt-hub",diff --git a/src/client/src/constants/events.ts b/src/client/src/constants/events.tsindex c8f056fc..c8a0d405 100644--- a/src/client/src/constants/events.ts+++ b/src/client/src/constants/events.ts@@ -82,6 +82,7 @@ export const CLIENT_EVENTS = {// Observability / telemetry explorer page.OBSERVABILITY_PAGE_VISITED: "OBSERVABILITY_PAGE_VISITED",+ TIME_FILTER_CHANGE: "TIME_FILTER_CHANGE",// OtterOTTER_CHAT_MESSAGE_SENT: "OTTER_CHAT_MESSAGE_SENT",@@ -117,6 +118,11 @@ export const CLIENT_EVENTS = {PROJECT_SWITCHED: "PROJECT_SWITCHED",PROJECT_LIST: "PROJECT_LIST",+ // Connectors + signal routing (product analytics — no secrets)+ CONNECTOR_CREATE_SUCCESS: "CONNECTOR_CREATE_SUCCESS",+ CONNECTOR_CREATE_FAILURE: "CONNECTOR_CREATE_FAILURE",+ SIGNAL_ROUTING_CHANGED: "SIGNAL_ROUTING_CHANGED",+// Page visitsLOGIN_PAGE_VISITED: "LOGIN_PAGE_VISITED",REGISTER_PAGE_VISITED: "REGISTER_PAGE_VISITED",@@ -277,5 +283,21 @@ export const SERVER_EVENTS = {// Daily anonymous instance snapshot (inventory + world-total ingestion).// Fired once per day per install by the telemetry-snapshot cron.- INSTANCE_TELEMETRY_SNAPSHOT: "INSTANCE_TELEMETRY_SNAPSHOT",+ INSTANCE_TELEMETRY_SNAPSHOT: "INSTANCE_TELEMETRY_SNAPSHOT", RULE_LIST_SUCCESS: "RULE_LIST_SUCCESS",+ RULE_LIST_FAILURE: "RULE_LIST_FAILURE",+ RULE_GET_SUCCESS: "RULE_GET_SUCCESS",+ RULE_GET_FAILURE: "RULE_GET_FAILURE",+ RULE_PREVIEW_SUCCESS: "RULE_PREVIEW_SUCCESS",+ RULE_PREVIEW_FAILURE: "RULE_PREVIEW_FAILURE",+ RULE_ENTITIES_LIST_SUCCESS: "RULE_ENTITIES_LIST_SUCCESS",+ RULE_ENTITIES_LIST_FAILURE: "RULE_ENTITIES_LIST_FAILURE",+ RULE_FIELD_VALUES_SUCCESS: "RULE_FIELD_VALUES_SUCCESS",+ RULE_FIELD_VALUES_FAILURE: "RULE_FIELD_VALUES_FAILURE",+ AI_ANALYSIS_GET_SUCCESS: "AI_ANALYSIS_GET_SUCCESS",+ AI_ANALYSIS_GET_FAILURE: "AI_ANALYSIS_GET_FAILURE",+ EVALUATION_GET_SUCCESS: "EVALUATION_GET_SUCCESS",+ EVALUATION_GET_FAILURE: "EVALUATION_GET_FAILURE",+ DASHBOARD_QUERY_RUN_SUCCESS: "DASHBOARD_QUERY_RUN_SUCCESS",+ DASHBOARD_QUERY_RUN_FAILURE: "DASHBOARD_QUERY_RUN_FAILURE",+};diff --git a/src/client/src/constants/messages/en.ts b/src/client/src/constants/messages/en.tsindex 66871616..f1690fa5 100644--- a/src/client/src/constants/messages/en.ts+++ b/src/client/src/constants/messages/en.ts@@ -302,6 +302,9 @@ export const OBSERVABILITY_AUTO = "Auto";export const OBSERVABILITY_METRIC_POINTS = "Metric points";export const OBSERVABILITY_LOG_EVENTS = "Log events";export const OBSERVABILITY_SPANS = "Spans";+export const OBSERVABILITY_FRESHNESS_LIVE = "Live";+export const OBSERVABILITY_FRESHNESS_SAMPLED = "Sampled";+export const OBSERVABILITY_FRESHNESS_ACCELERATED = "Accelerated";export const OBSERVABILITY_TOTAL = "Total";export const OBSERVABILITY_PEAK = "Peak";export const OBSERVABILITY_SPAN_HIERARCHY = "Span Hierarchy";@@ -358,6 +361,59 @@ export const BOARD_LAYOUT_UPDATED_SUCCESSFULLY ="Board layout updated successfully!";export const WIDGET_FETCH_FAILED = "Widget fetch failed!";export const WIDGET_RUN_FAILED = "Widget run failed!";+export const WIDGET_RAW_SQL_SOURCE_ONLY = (source: string) =>+ `Raw SQL widgets run only on the built-in ClickHouse source. This widget targets "${source}", so configure a structured query instead.`;+export const WIDGET_NO_STRUCTURED_QUERY =+ "This widget has no structured query configured for its data source.";+export const WIDGET_STRUCTURED_QUERY_FAILED = "Structured widget query failed.";+export const WIDGET_SOURCE_SIGNAL_UNSUPPORTED = (signal: string, source: string) =>+ `The data source "${source}" does not serve ${signal}.`;+export const WIDGET_DATA_SOURCE_LABEL = "Data Source";+export const WIDGET_DATA_SOURCE_BUILTIN = "ClickHouse";+export const WIDGET_DATA_SOURCE_EMPTY =+ "No data sources configured for this environment and signal.";+export const WIDGET_DATA_SOURCE_HINT =+ "Defaults to the project router for the selected signal. Change the signal to switch routers, or override for this panel only.";+export const WIDGET_DATA_SOURCE_EXTERNAL_SQL_DISABLED =+ "Raw SQL is available only on the built-in ClickHouse source. This source runs a structured query configured by its signal.";+export const WIDGET_DATA_SOURCE_PROJECT_TRACES_HINT = (+ name: string,+ type: string+) =>+ `This widget stores ClickHouse SQL for compatibility. At query time it runs against your project traces source: ${name} (${type}).`;+export const WIDGET_STRUCTURED_BUILDER_TITLE = "Structured Query";+export const WIDGET_STRUCTURED_SIGNAL_LABEL = "Signal";+export const WIDGET_STRUCTURED_MODE_LABEL = "Mode";+export const WIDGET_STRUCTURED_MODE_LIST = "List rows";+export const WIDGET_STRUCTURED_MODE_AGGREGATE = "Aggregate (group by)";+export const WIDGET_STRUCTURED_MODE_TIMESERIES = "Time series";+export const WIDGET_STRUCTURED_LIMIT_LABEL = "Row limit";+export const WIDGET_STRUCTURED_INTERVAL_LABEL = "Interval (e.g. 1m, 1h)";+export const WIDGET_STRUCTURED_GROUP_BY_LABEL = "Group by (attribute keys)";+export const WIDGET_STRUCTURED_GROUP_BY_PLACEHOLDER =+ "gen_ai.request.model, service.name";+export const WIDGET_STRUCTURED_AGGREGATIONS_LABEL = "Aggregations";+export const WIDGET_STRUCTURED_ADD_AGGREGATION = "Add aggregation";+export const WIDGET_STRUCTURED_ADD_FILTER = "Add filter";+export const WIDGET_STRUCTURED_FILTERS_LABEL = "Filters";+export const WIDGET_STRUCTURED_FIELD_PLACEHOLDER = "field (optional for count)";+export const WIDGET_STRUCTURED_ALIAS_PLACEHOLDER = "alias";+export const WIDGET_STRUCTURED_KEY_PLACEHOLDER = "attribute key";+export const WIDGET_STRUCTURED_VALUE_PLACEHOLDER = "value";+export const WIDGET_STRUCTURED_AGGREGATION_UNSUPPORTED =+ "This source cannot aggregate server-side. Use list mode, or pick a source that supports aggregation.";+export const WIDGET_STRUCTURED_FILTER_TARGET_LABEL = "Target";+export const WIDGET_STRUCTURED_SORT_LABEL = "Sort";+export const WIDGET_STRUCTURED_SORT_FIELD_PLACEHOLDER = "sort field";+export const WIDGET_STRUCTURED_SORT_ASC = "Ascending";+export const WIDGET_STRUCTURED_SORT_DESC = "Descending";+export const WIDGET_STRUCTURED_PREVIOUS_PERIOD_LABEL =+ "Compare with previous period";+export const WIDGET_STRUCTURED_DURATION_PLACEHOLDER = "nanoseconds";+export const WIDGET_STRUCTURED_STATUS_PLACEHOLDER =+ "STATUS_CODE_OK, STATUS_CODE_ERROR";+export const WIDGET_STRUCTURED_SPAN_NAME_PLACEHOLDER = "span name";+export const WIDGET_STRUCTURED_VIEW_GENERATED_QUERY = "View generated query";export const BOARD_DELETE_FAILED = "Board delete failed!";export const BOARD_DELETED_SUCCESSFULLY = "Board deleted successfully!";export const FOLDER_DELETE_FAILED =@@ -367,6 +423,7 @@ export const MAIN_DASHBOARD_NOT_FOUND = "Main dashboard not found!";export const BOARD_CREATE_FAILED = "Board create failed!";export const BOARD_IMPORT_FAILED = "Board import failed!";export const BOARD_IMPORT_SUCCESSFULLY = "Board import successfully!";+export const BOARD_IMPORT_INVALID_JSON = "Dashboard import file must be valid JSON.";export const NO_WIDGETS_YET = "No widgets yet!";export const NO_WIDGETS_YET_DESCRIPTION = "Create your first widget to start building your custom dashboard. Add charts, stats, and more to visualize your data.";export const NO_WIDGETS_YET_ACTION_BUTTON = "Add Your First Widget";@@ -677,6 +734,14 @@ export const AGENTS_NO_CODING_AGENTS_DESCRIPTION = "Pick a tool to see the insta// Trace-detail content-capture banner. Surfaces a one-liner the user// can run when prompts / responses are missing from a coding-agent+// Coding Agents — env-routed ClickHouse resolution errors+export const CODING_AGENTS_DB_NOT_IN_PROJECT =+ "The selected ClickHouse datasource is not available in the current project.";+export const CODING_AGENTS_REQUIRES_CLICKHOUSE =+ "Coding Agents require a ClickHouse datasource for the selected environment's traces or intelligence routing.";+export const OBSERVABILITY_LOG_NOT_FOUND =+ "Log entry not found in the selected time range or data source.";+// Shown when coding-agent sessions lack captured prompts / completions from the// trace (i.e. the CLI is in metadata_only or minimal mode). Tracks the// OPENLIT_CODING_CONTENT_CAPTURE flag used by the CLI.export const CODING_AGENT_CONTENT_CAPTURE_NOTE_TITLE = "Content capture is off";@@ -1189,6 +1254,12 @@ export const DB_CONFIG_FIELD_DATABASE = "Database";export const DB_CONFIG_FIELD_QUERY_PARAMS = "Query params";export const UPDATE_DB_CONFIG = "Update Database config";export const ADD_DB_CONFIG = "Add Database config";+export const CLICKHOUSE_CONNECTOR_ADD_TITLE = "Add ClickHouse connector";+export const CLICKHOUSE_CONNECTOR_EDIT_TITLE = "Edit ClickHouse connector";+export const CLICKHOUSE_CONNECTOR_DESCRIPTION =+ "Connect this project environment to the ClickHouse database used by telemetry and derived features.";+export const CLICKHOUSE_CONNECTOR_INSTRUCTIONS =+ "Use the host, port, database, and credentials for the ClickHouse instance that belongs to this environment.";export const DB_CONFIG_EDIT_PERMISSION_REQUIRED ="You don't have enough permissions to edit this database config";export const UPDATE = "Update";@@ -1200,6 +1271,8 @@ export const DB_CONFIG_EMPTY_DESCRIPTION =export const DB_CONFIG_NOT_SELECTED_DESCRIPTION ="Select a database config to update its details.";export const ADD_DATABASE_CONFIG = "Add database config";+export const DELETE_DATABASE_CONFIG_CONFIRMATION =+ "Delete this ClickHouse connector? This cannot be undone.";export const MARK_DB_CONFIG_ACTIVE = (name: string) =>`Mark ${name} as the active database config`;export const SHARE_DATABASE_CONFIG = "Share Database config";@@ -1223,6 +1296,7 @@ export const PROJECTS = "Projects";export const PROJECT_NAME = "Project Name";export const PROJECT_NAME_PLACEHOLDER = "Production";export const MANAGE_PROJECTS = "Manage Projects";+export const MANAGE_DATA = "Manage Data";export const DEFAULT_PROJECT = "Default Project";export const PROJECT_NAME_REQUIRED = "Project name is required";export const PROJECT_NAME_LENGTH_ERROR = "Project name must be 120 characters or less";@@ -1245,6 +1319,9 @@ export const BACK_TO_ORGANISATION = "Back to Organisation";export const PROJECT_DETAILS = "Project details";export const PROJECT_DETAILS_DESCRIPTION ="Review project identity, current status, and database configuration.";+export const PROJECT_ENVIRONMENT = "Environments";+export const PROJECT_ID = "Project ID";+export const PROJECT_CONNECTION_COUNT = "ClickHouse connections";export const PROJECT_DATABASE_CONFIGS = "Database configurations";export const PROJECT_DATABASE_CONFIGS_DESCRIPTION ="Create, select, and update database configs scoped to this project.";@@ -1935,3 +2012,216 @@ export const TRACE_AI_TOOL_MISUSE_EMPTY_DETAIL =export const TRACE_AI_TOKENS_SAVED = (tokens: number | string) =>`${tokens} tokens saved`;export const TRACE_AI_USD_SAVED = (usd: number | string) => `$${usd} saved`;++// Telemetry Sources (pluggable observability backends)+export const TELEMETRY_SOURCE_BUILTIN_NAME = "ClickHouse";+export const TELEMETRY_SOURCE_NOT_CONFIGURED =+ "No telemetry source is configured for this project.";+export const TELEMETRY_SOURCE_NOT_SUPPORTED_IN_CE =+ "External telemetry sources are an enterprise feature. This deployment reads telemetry from the built-in ClickHouse source.";+export const TELEMETRY_SOURCE_CAPABILITY_UNSUPPORTED = (+ capability: string,+ source: string+) => `"${capability}" is not supported by the "${source}" data source.`;+export const TELEMETRY_SOURCE_SIGNAL_UNSUPPORTED = (+ signal: string,+ source: string+) => `This data source ("${source}") does not serve ${signal}.`;+export const TELEMETRY_SOURCE_HEALTHCHECK_OK = "Data source is reachable.";+export const TELEMETRY_SOURCE_HEALTHCHECK_FAILED = (reason: string) =>+ `Data source is unreachable: ${reason}`;+export const TELEMETRY_SOURCE_NO_AI_SIGNAL =+ "No AI telemetry was found in the probe window for this data source. Confirm your apps emit gen_ai.* / coding_agent.* attributes or the OpenLIT SDK.";+export const TELEMETRY_SOURCE_AI_VALIDATION_UNSUPPORTED = (type: string) =>+ `AI signal validation is not applicable for "${type}" (logs/metrics sources). Connection health is enough for this source.`;+export const TELEMETRY_SOURCE_CHAT_NATIVE_ONLY = (source: string) =>+ `Natural-language SQL chat runs only on the built-in ClickHouse source. This project reads telemetry from "${source}", so use the Telemetry, trace, and dashboard views instead.`;+export const DATA_SOURCE_AUTH_401_HINT =+ "Authentication failed. For Grafana Cloud, use Basic auth: Username = instance ID, Password = access policy token (leave Bearer blank).";+export const DATA_SOURCE_AUTH_TYPE = "Authentication type";+export const DATA_SOURCE_AUTH_TYPE_BASIC = "Basic (username + password)";+export const DATA_SOURCE_AUTH_TYPE_BEARER = "Bearer token";+export const DATA_SOURCE_AUTH_TYPE_NONE = "No authentication";+export const TELEMETRY_SOURCE_NAME_REQUIRED =+ "A telemetry source name is required.";+export const TELEMETRY_SOURCE_NAME_TAKEN = (name: string, environment: string) =>+ `A connector named "${name}" already exists in the ${environment} environment. Choose a different name.`;+export const TELEMETRY_SOURCE_TYPE_UNKNOWN = (type: string) =>+ `Unknown telemetry source type "${type}".`;+export const TELEMETRY_SOURCE_INVALID_JSON =+ "Request body is not valid JSON.";+export const TELEMETRY_SOURCE_NO_PROJECT =+ "No current project is selected.";+export const TELEMETRY_SOURCE_NOT_FOUND =+ "Telemetry source not found in the current project.";+export const TELEMETRY_SOURCE_INVALID_SETTINGS =+ "Telemetry source settings must be a JSON object.";+export const TELEMETRY_SOURCE_SIGNAL_NOT_IN_TYPE = (+ signals: string,+ type: string+) => `Source type "${type}" cannot serve these signals: ${signals}.`;+export const TELEMETRY_SOURCE_NO_SIGNALS =+ "A telemetry source must serve at least one signal.";+export const TELEMETRY_SOURCE_INVALID_SIGNAL = (signal: string) =>+ `Unknown telemetry signal "${signal}". Expected one of traces, logs, metrics.`;+export const TELEMETRY_SOURCE_BINDING_SIGNAL_UNSERVED = (+ signal: string,+ source: string+) => `The bound source "${source}" does not serve ${signal}.`;+export const TELEMETRY_SOURCE_BINDING_ENVIRONMENT_MISMATCH = (+ source: string,+ sourceEnvironment: string,+ bindingEnvironment: string+) =>+ `Cannot bind "${source}" (${sourceEnvironment}) into the ${bindingEnvironment} routing slot. Pick a connector from the same environment.`;+export const TELEMETRY_SOURCE_NO_SOURCE_FOR_SIGNAL = (signal: string) =>+ `No connector is configured or routed for ${signal}. Add a connector for this environment and select it in Signal routing.`;+export const TELEMETRY_SOURCE_ADAPTER_UNAVAILABLE = (type: string) =>+ `No adapter is registered for telemetry source type "${type}".`;++// ---- Data sources UI (project "Data sources" tab) ------------------------+export const PROJECT_DATA_SOURCES = "Data sources";+export const PROJECT_DATA_SOURCES_DESCRIPTION =+ "Point each telemetry signal at the observability backend where it lives. OpenLIT reads raw traces, logs, and metrics from these sources and runs its intelligence on top.";+export const PROJECT_CONNECTORS = "Connectors";+export const PROJECT_ENVIRONMENTS = "Manage Environments";+export const PROJECT_ENVIRONMENTS_DESCRIPTION =+ "Select an environment, manage its ClickHouse targets and telemetry connectors, and route traces, logs, and metrics independently.";+export const PROJECT_CONNECTORS_DESCRIPTION =+ "Manage ClickHouse databases and external integrations for this project.";+export const GLOBAL_CONNECTORS = "Connectors";+export const CONNECTED_CONNECTORS = "Configured connectors";+export const NO_CONNECTED_CONNECTORS = "No connectors are configured for the active project yet.";+export const CONNECTOR_CATALOG = "Connector catalog";+export const CONNECTOR_CATALOG_DESCRIPTION = "Browse connector types and add more integrations to the active project.";+export const ADD_CONNECTOR = "Add connector";+export const CONNECTOR_PREMIUM = "Premium";+export const CONNECTOR_LOCKED = "Requires a paid plan or license";+export const CONNECTOR_LOCKED_ACTION = "License required";+export const NAV_FEATURE_LOCKED = "Locked";+export const MANAGE_CONNECTORS = "Manage connectors";+export const CONNECTOR_ENVIRONMENT = "Environment";+export const CONNECTOR_ENVIRONMENT_PLACEHOLDER = "production";+export const CONNECTOR_ENVIRONMENT_DESCRIPTION =+ "Keep connector assignments separate for production, staging, and other environments.";+export const DATA_SOURCE_BUILTIN_TITLE = "ClickHouse connector";+export const DATA_SOURCE_BUILTIN_DERIVED =+ "ClickHouse is optional for telemetry viewing. It becomes the execution target for evaluations, AI analysis, costing, and other features that require OpenLIT-managed storage.";+export const DATA_SOURCE_BUILTIN_FIELD_LABEL = "Derived intelligence & evaluations";+export const DATA_SOURCE_SIGNAL_ROUTING_TITLE = "Signal routing";+export const DATA_SOURCE_SIGNAL_ROUTING_DESCRIPTION =+ "Choose which connector serves each signal for this project. The ClickHouse connector correlates all signals; splitting signals across backends limits cross-signal features to best-effort.";+export const DATA_SOURCE_SIGNAL_ROUTING_DIALOG_DESCRIPTION = (environment: string) =>+ `Choose which connector serves each signal in ${environment}. Changes apply immediately.`;+export const DATA_SOURCE_SIGNAL_ROUTING_DIALOG_FOOTER = (name: string, environment: string) =>+ `${name} · routing is scoped to the ${environment} environment.`;+export const DATA_SOURCE_SIGNAL_TRACES = "Traces";+export const DATA_SOURCE_SIGNAL_LOGS = "Logs";+export const DATA_SOURCE_SIGNAL_METRICS = "Metrics";+export const DATA_SOURCE_SIGNAL_BUILTIN_OPTION = "ClickHouse connector";+export const DATA_SOURCE_SOURCES_TITLE = "External sources";+export const DATA_SOURCE_SOURCES_DESCRIPTION =+ "Connect Grafana Tempo or Jaeger for external traces, or ClickHouse for OpenLIT-managed telemetry and intelligence.";+export const DATA_SOURCE_ADD = "Add source";+export const DATA_SOURCE_EDIT = "Edit source";+export const DATA_SOURCE_DETAILS = "Connector details";+export const DATA_SOURCE_CONNECTOR_SECTION = "Connector";+export const DATA_SOURCE_CONNECTION_SECTION = "Connection details";+export const DATA_SOURCE_SETTINGS_SECTION = "Connection settings";+export const DATA_SOURCE_SIGNALS_SECTION = "Signals served";+export const DATA_SOURCE_TYPE_LOCKED = "Connector type cannot be changed after creation.";+export const DATA_SOURCE_VIEW_DETAILS = "View details";+export const DATA_SOURCE_CONNECTOR_SECTION_DESCRIPTION = "Choose the integration and identify this connection.";+export const DATA_SOURCE_CONNECTION_SECTION_DESCRIPTION = "Use a recognizable name and keep configuration isolated by environment.";+export const DATA_SOURCE_SETTINGS_SECTION_DESCRIPTION = "Connection-specific options for this connector.";+export const DATA_SOURCE_SETUP_TITLE = "How to connect";+export const DATA_SOURCE_SETUP_DESCRIPTION = "Use these steps to prepare the endpoint and credentials before saving.";+export const DATA_SOURCE_SETUP_GUIDES: Record<string, { summary: string; steps: string[]; docsUrl: string }> = {+ clickhouse: {+ summary: "Use the project database configuration for the ClickHouse store used by OpenLIT intelligence and evaluations.",+ steps: ["Open the ClickHouse service's Connect menu and copy the HTTPS host, database, username, and password.", "Keep TLS enabled for hosted ClickHouse services.", "Save the connection in the project's database configuration, then test the connection."],+ docsUrl: "https://clickhouse.com/integrations/clickhouse_client",+ },+ tempo: {+ summary: "Grafana Cloud Tempo uses the stack endpoint with Basic authentication.",+ steps: ["Copy the Tempo query URL from your Grafana Cloud stack, usually ending in /tempo.", "Use the Tempo instance ID as Username.", "Create a Grafana Cloud Access Policy token with traces:read and enter it as Password."],+ docsUrl: "https://grafana.com/docs/grafana/latest/datasources/tempo/configure-tempo-data-source/",+ },+ jaeger: {+ summary: "OpenLIT reads traces from the Jaeger Query Service.",+ steps: ["Enter the Jaeger Query Service URL, commonly port 16686.", "Use the configured Basic/Bearer credentials if the query service is protected.", "Ensure the OpenLIT server can reach the query service over the network."],+ docsUrl: "https://www.jaegertracing.io/docs/1.24/apis/",+ },+};+export const DATA_SOURCE_EMPTY_TITLE = "No external sources yet";+export const DATA_SOURCE_EMPTY_DESCRIPTION =+ "Add a source to read telemetry from your existing observability backend. Until then, everything reads from the built-in ClickHouse.";+export const DATA_SOURCE_FIELD_NAME = "Name";+export const DATA_SOURCE_FIELD_TYPE = "Type";+export const DATA_SOURCE_FIELD_SIGNALS = "Signals served";+export const DATA_SOURCE_FIELD_DEFAULT = "Set as project default";+export const DATA_SOURCE_FIELD_ENDPOINT = "Endpoint URL";+export const DATA_SOURCE_FIELD_ENDPOINT_HELP =+ "When OpenLIT runs in Docker, use http://host.docker.internal:<port> for services on the host, or the Compose service name (e.g. http://loki:3100) on the same network. localhost inside the container is not the host.";+export const DATA_SOURCE_FIELD_ALLOW_HTTP = "Allow plain HTTP (self-hosted)";+export const DATA_SOURCE_FIELD_ALLOW_PRIVATE_NETWORK = "Allow private or localhost endpoints";+export const DATA_SOURCE_PRIVATE_NETWORK_BLOCKED = (host: string) =>+ `Host "${host}" is not allowed. Enable "Allow private or localhost endpoints" on this data source to reach private or loopback addresses.`;+export const DATA_SOURCE_RESPONSE_TOO_LARGE = (maxBytes: number) =>+ `Data source response exceeded the ${Math.round(maxBytes / (1024 * 1024))} MiB safety limit.`;+export const DATA_SOURCE_FIELD_SITE = "Datadog site";+export const DATA_SOURCE_FIELD_REGION = "Region";+export const DATA_SOURCE_FIELD_ACCOUNT_ID = "Account ID";+export const DATA_SOURCE_FIELD_API_KEY = "API key";+export const DATA_SOURCE_FIELD_APP_KEY = "Application key";+export const DATA_SOURCE_FIELD_TOKEN = "Bearer token (optional)";+export const DATA_SOURCE_FIELD_USERNAME = "Username / Instance ID";+export const DATA_SOURCE_FIELD_USERNAME_PLACEHOLDER = "e.g. Grafana Cloud instance ID";+export const DATA_SOURCE_FIELD_PASSWORD = "Password / Access policy token";+export const DATA_SOURCE_FIELD_PASSWORD_PLACEHOLDER = "Basic-auth password or Cloud access policy token";+export const DATA_SOURCE_FIELD_TENANT = "Tenant / Org ID";+export const DATA_SOURCE_FIELD_TENANT_PLACEHOLDER = "X-Scope-OrgID or Victoria AccountID";+export const DATA_SOURCE_FIELD_MAX_TIME_RANGE_DAYS = "Maximum query window (days)";+export const DATA_SOURCE_FIELD_MAX_TIME_RANGE_DAYS_PLACEHOLDER = "30";+export const DATA_SOURCE_MAX_TIME_RANGE_HINT = (days: number) =>+ `Maximum ${days} day${days === 1 ? "" : "s"} for the selected data source.`;+export const DATA_SOURCE_SECRET_UNAVAILABLE =+ "The connector credentials could not be loaded from the OpenLIT vault. Check the selected ClickHouse connection and try again.";+export const DATA_SOURCE_SECRET_NOT_FOUND =+ "The connector credential is missing from the OpenLIT vault. Re-save the connector credentials.";+export const DATA_SOURCE_SECRET_DECRYPT_FAILED =+ "The connector credential could not be decrypted. Re-save it with the current vault encryption key.";+export const DATA_SOURCE_AUTH_REQUIRED = (sourceName: string, authType: string) =>+ `${sourceName} is configured for ${authType} authentication, but its credentials could not be resolved.`;+export const DATA_SOURCE_CREDENTIALS_TITLE = "Credentials";+export const DATA_SOURCE_AUTHENTICATION_SECTION = "Authentication";+export const DATA_SOURCE_CREDENTIALS_HELP =+ "Stored encrypted in the OpenLIT vault and only decrypted server-side. Leave blank to keep existing credentials.";+export const DATA_SOURCE_AUTH_HELP_HTTP =+ "Grafana Cloud query APIs use Basic auth (Instance ID + access policy token). Self-hosted LGTM often needs no auth, or Basic/Bearer via your reverse proxy. Fill Basic fields for Cloud; use Bearer only when your backend expects it.";+export const DATA_SOURCE_AUTH_HELP_DATADOG =+ "Datadog requires an API key and Application key for Spans, Logs, and Metrics APIs.";+export const DATA_SOURCE_AUTH_HELP_NEWRELIC =+ "New Relic uses a user or ingest API key with NerdGraph / NRQL access for the account.";+export const DATA_SOURCE_CREDENTIALS_SET = "Credentials are set.";+export const DATA_SOURCE_DOCS_LINK = "View connection docs";+export const DATA_SOURCE_TEST = "Test connection";+export const DATA_SOURCE_TESTING = "Testing connection…";+export const DATA_SOURCE_TEST_OK = "Data source is reachable.";+export const DATA_SOURCE_TEST_AI_OK = (count: number) =>+ `Reachable, and ${count} AI telemetry record${count === 1 ? "" : "s"} found in the last hour.`;+export const DATA_SOURCE_TEST_AI_NONE =+ "Reachable, but no AI telemetry was found in the last hour.";+export const DATA_SOURCE_TEST_VALIDATION_FAILED = (reason: string) =>+ `Connection is reachable, but telemetry validation failed: ${reason}`;+export const DATA_SOURCE_SAVED = "Data source saved.";+export const DATA_SOURCE_SAVE_FAILED = "Failed to save data source.";+export const DATA_SOURCE_DELETED = "Data source deleted.";+export const DATA_SOURCE_DELETE_FAILED = "Failed to delete data source.";+export const DATA_SOURCE_DELETE_CONFIRM = (name: string) =>+ `Delete data source "${name}"? Signals bound to it will fall back to the built-in ClickHouse.`;+export const DATA_SOURCE_BINDING_SAVED = "Signal routing updated.";+export const DATA_SOURCE_BINDING_FAILED = "Failed to update signal routing.";+export const DATA_SOURCE_LOAD_FAILED = "Failed to load data sources.";+export const DATA_SOURCE_RETRY = "Try again";+diff --git a/src/client/src/constants/openlit-context.ts b/src/client/src/constants/openlit-context.tsindex ed169cc0..9082afd9 100644--- a/src/client/src/constants/openlit-context.ts+++ b/src/client/src/constants/openlit-context.ts@@ -2,4 +2,9 @@ export const OPENLIT_CONTEXT_HEADERS = {organisationId: "x-openlit-organisation-id",projectId: "x-openlit-project-id",databaseConfigId: "x-openlit-database-config-id",+ environment: "x-openlit-environment",} as const;++export function getRequestEnvironment(request: Request): string | undefined {+ return request.headers.get(OPENLIT_CONTEXT_HEADERS.environment) || undefined;+}diff --git a/src/client/src/constants/sidebar.tsx b/src/client/src/constants/sidebar.tsxindex ecb2f88f..3ba2d0b7 100644--- a/src/client/src/constants/sidebar.tsx+++ b/src/client/src/constants/sidebar.tsx@@ -1,8 +1,10 @@+import OpenTelemetrySvg from "@/components/svg/opentelemetry";import { getEnterpriseSidebarItems } from "@/features/sidebar";import getMessage from "@/constants/messages";import { SidebarItemProps } from "@/types/sidebar";import {Activity,+ Cable,BookKey,BookOpen,BookText,@@ -72,7 +74,12 @@ export const SIDEBAR_ITEMS: SidebarItemProps[] = [link: "/costs",type: "action",},- ...getEnterpriseSidebarItems("configuration", ICON_CLASSES),+ {+ icon: <OpenTelemetrySvg className={ICON_CLASSES} />,+ text: "Fleet Hub",+ link: "/fleet-hub",+ type: "action",+ },],},{@@ -110,6 +117,18 @@ export const SIDEBAR_ITEMS: SidebarItemProps[] = [},],},+ {+ title: "Configuration",+ children: [+ {+ icon: <Cable className={ICON_CLASSES} />,+ text: "Connectors",+ link: "/connectors",+ type: "action",+ },+ ...getEnterpriseSidebarItems("configuration", ICON_CLASSES),+ ],+ },],},{diff --git a/src/client/src/features/nav-locks.ts b/src/client/src/features/nav-locks.tsnew file mode 100644index 00000000..0c24f8e2--- /dev/null+++ b/src/client/src/features/nav-locks.ts@@ -0,0 +1,7 @@+/**+ * CE fallback: no paid nav routes, so sidebar never shows lock icons.+ * Enterprise overrides this to read entitlement state for route-prefixed features.+ */+export function useIsNavLinkLocked(_link?: string): boolean {+ return false;+}diff --git a/src/client/src/helpers/client/connector-analytics.ts b/src/client/src/helpers/client/connector-analytics.tsnew file mode 100644index 00000000..5ace866c--- /dev/null+++ b/src/client/src/helpers/client/connector-analytics.ts@@ -0,0 +1,53 @@+/**+ * Minimal PostHog props for connector create + signal routing changes.+ * No secrets, endpoints, or credential fields — only product-intent metadata.+ */++export type SignalRoutingChangeKind = "bound" | "switched" | "cleared";++export const BUILTIN_ROUTING_VALUE = "builtin";++export function classifySignalRoutingChange(+ previousSourceId: string | null | undefined,+ nextSourceId: string+): SignalRoutingChangeKind {+ const next = String(nextSourceId || "").trim();+ const previous = previousSourceId ? String(previousSourceId).trim() : "";+ // Exact "builtin" is the UI sentinel used to DELETE a binding.+ if (!next || next === BUILTIN_ROUTING_VALUE) return "cleared";+ if (!previous) return "bound";+ return previous === next ? "bound" : "switched";+}++export function connectorCreateEventProps(input: {+ type: string;+ environment?: string;+}) {+ return {+ connector_type: input.type,+ environment: (input.environment || "production").toLowerCase(),+ };+}++export function signalRoutingChangedEventProps(input: {+ signal: string;+ environment?: string;+ previousSourceId?: string | null;+ nextSourceId: string;+ previousConnectorType?: string | null;+ nextConnectorType?: string | null;+}) {+ const kind = classifySignalRoutingChange(+ input.previousSourceId,+ input.nextSourceId+ );+ return {+ signal: input.signal,+ environment: (input.environment || "production").toLowerCase(),+ change: kind,+ previous_source_id: input.previousSourceId || null,+ next_source_id: input.nextSourceId || null,+ previous_connector_type: input.previousConnectorType || null,+ next_connector_type: input.nextConnectorType || null,+ };+}diff --git a/src/client/src/helpers/client/database-config.ts b/src/client/src/helpers/client/database-config.tsindex e7c1217b..0bc90be5 100644--- a/src/client/src/helpers/client/database-config.ts+++ b/src/client/src/helpers/client/database-config.ts@@ -15,8 +15,9 @@ export const fetchDatabaseConfigList = async (}));- successCb(data || []);- useRootStore.getState().databaseConfig.setList(data || []);+ const list = Array.isArray(data) ? data : [];+ successCb(list);+ useRootStore.getState().databaseConfig.setList(list);};export const pingActiveDatabaseConfig = async () => {@@ -40,7 +41,8 @@ export const pingActiveDatabaseConfig = async () => {export const changeActiveDatabaseConfig = async (databaseConfigId: string,- successCb: () => void+ successCb: () => void,+ options: { silent?: boolean } = {}) => {const [err, data] = await asaw(getData({@@ -64,9 +66,11 @@ export const changeActiveDatabaseConfig = async (return acc;}, []);useRootStore.getState().databaseConfig.setList(updatedList);- toast.success(`Db config: set active successfully!`, {- id: "db-config-current",- });+ if (!options.silent) {+ toast.success(`Db config: set active successfully!`, {+ id: "db-config-current",+ });+ }pingActiveDatabaseConfig();};diff --git a/src/client/src/helpers/client/observability.ts b/src/client/src/helpers/client/observability.tsindex de04e945..7807d4fe 100644--- a/src/client/src/helpers/client/observability.ts+++ b/src/client/src/helpers/client/observability.ts@@ -1,5 +1,5 @@import { DEFAULT_SORTING } from "@/store/filter";-import { FilterConfig } from "@/types/store/filter";+import { AttributeKeys, FilterConfig } from "@/types/store/filter";export type UpdateFilterFn = (key: string,@@ -9,32 +9,60 @@ export type UpdateFilterFn = (export type UpdateConfigFn = (config?: FilterConfig) => void;-// E3: when the active observability signal/tab changes (traces ↔-// metrics ↔ logs ↔ coding-agent sessions), the previous tab's sort-// key (e.g. "Tokens") is almost never a valid column on the new tab.-// Leaving it set leaks ORDER BY clauses across signals and causes-// the new tab to fall back to an inappropriate default ordering at-// best — or a 500 at worst when the SQL column doesn't exist.-//-// selectedConfig must be cleared too. Each signal owns its own-// localStorage scope (`openlit_filter_v1:<signal>`), so preserving-// the previous tab's filters in the Zustand store defeats that and-// silently empties the new tab. The concrete failure mode: Traces-// `providers=openai` surviving into Exceptions, where every-// StatusCode=Error span from a failed LLM call has empty-// gen_ai.provider.name / gen_ai.system (the SDK never stamped them-// before the error) — the provider WHERE matches zero rows and the-// Exceptions tab renders empty despite hundreds of errors in CH.-// After the wipe, the new tab's useFilterUrlSync re-applies that-// signal's own persisted filters from localStorage / URL.+export type UpdateAttributeKeysFn = (keys: AttributeKeys) => void;++const EMPTY_ATTRIBUTE_KEYS: AttributeKeys = {+ spanAttributeKeys: [],+ resourceAttributeKeys: [],+ logAttributeKeys: [],+ scopeAttributeKeys: [],+ metricAttributeKeys: [],+};++/** Agent-detail scope fields that must survive a signal/tab filter wipe. */+export type ObservabilityScopePreserve = Partial<+ Pick<+ FilterConfig,+ "serviceNames" | "services" | "environments" | "versionFilter"+ >+>;++/**+ * When the active observability signal/tab changes (traces ↔ metrics ↔+ * logs ↔ coding-agent sessions), reset sort, pagination, selected filters,+ * and attribute-key caches so one signal's filters cannot leak into another.+ *+ * On agent-detail pages, pass `preserveScope` so `serviceNames` /+ * `services` / `environments` / `versionFilter` stay locked. Wiping those+ * fields makes `AgentScopeProvider` briefly report not-ready, unmount the+ * list, remount it, wipe again, and hit "Maximum update depth exceeded".+ */export function prepareObservabilitySignalChange(updateConfig: UpdateConfigFn,- updateFilter: UpdateFilterFn+ updateFilter: UpdateFilterFn,+ updateAttributeKeys?: UpdateAttributeKeysFn,+ preserveScope?: ObservabilityScopePreserve | null) {updateConfig(undefined);- updateFilter("selectedConfig", {}, { clearFilter: true });updateFilter("groupBy", null);updateFilter("groupValue", null);updateFilter("sorting", DEFAULT_SORTING);updateFilter("offset", 0);++ const preserved: Partial<FilterConfig> = {};+ if (preserveScope?.serviceNames?.length) {+ preserved.serviceNames = [...preserveScope.serviceNames];+ }+ if (preserveScope?.services?.length) {+ preserved.services = [...preserveScope.services];+ }+ if (preserveScope?.environments?.length) {+ preserved.environments = [...preserveScope.environments];+ }+ if (preserveScope?.versionFilter) {+ preserved.versionFilter = preserveScope.versionFilter;+ }++ updateFilter("selectedConfig", preserved, { clearFilter: true });+ updateAttributeKeys?.(EMPTY_ATTRIBUTE_KEYS);}diff --git a/src/client/src/helpers/client/organisation.ts b/src/client/src/helpers/client/organisation.tsindex efdf42bc..e3918b68 100644--- a/src/client/src/helpers/client/organisation.ts+++ b/src/client/src/helpers/client/organisation.ts@@ -24,7 +24,7 @@ export const fetchOrganisationList = async () => {return;}- useRootStore.getState().organisation.setList(data || []);+ useRootStore.getState().organisation.setList(Array.isArray(data) ? data : []);posthog?.capture(CLIENT_EVENTS.ORGANISATION_LIST, {count: Array.isArray(data) ? data.length : 0,});diff --git a/src/client/src/helpers/server/trace.ts b/src/client/src/helpers/server/trace.tsindex 4493db79..4e4d45e0 100644--- a/src/client/src/helpers/server/trace.ts+++ b/src/client/src/helpers/server/trace.ts@@ -4,40 +4,70 @@ export {getTraceMappingValue,} from "../client/trace";+const ZERO_PARENTS = new Set(["0".repeat(16), "0".repeat(32)]);++function isEmptyParent(parentId: unknown): boolean {+ if (parentId == null || parentId === "") return true;+ return ZERO_PARENTS.has(String(parentId));+}++function compareTimestamp(a: any, b: any): number {+ const tsA = a?.Timestamp ? new Date(a.Timestamp).getTime() : 0;+ const tsB = b?.Timestamp ? new Date(b.Timestamp).getTime() : 0;+ return tsA - tsB;+}++function sortChildren(node: any) {+ if (node?.children?.length) {+ node.children.sort(compareTimestamp);+ node.children.forEach(sortChildren);+ }+}++/**+ * Build a ParentSpanId tree for the trace detail UI.+ *+ * Incomplete Jaeger/OTEL exports often omit the true root while every+ * remaining span still points at it. When no empty-parent root exists, promote+ * those orphan tops into a forest and return the earliest as the primary root+ * (siblings hang under it) so hierarchy/detail APIs do not fail closed.+ */export const buildHierarchy = (data: any[]) => {- // Create a map for quick lookup of nodes by SpanId- const nodeMap = new Map();+ if (!Array.isArray(data) || data.length === 0) return null;++ const nodeMap = new Map<string, any>();data.forEach((item) => {+ if (!item?.SpanId) return;nodeMap.set(item.SpanId, { ...item, children: [] });});+ if (nodeMap.size === 0) return null;- let root = null;+ const explicitRoots: any[] = [];+ const orphans: any[] = [];- // Build the hierarchydata.forEach((item) => {const node = nodeMap.get(item.SpanId);- if (item.ParentSpanId === "") {- // Root node found- root = node;- } else if (nodeMap.has(item.ParentSpanId)) {- // Link the node to its parent- const parent = nodeMap.get(item.ParentSpanId);- parent.children.push(node);+ if (!node) return;+ if (isEmptyParent(item.ParentSpanId)) {+ explicitRoots.push(node);+ return;+ }+ if (nodeMap.has(item.ParentSpanId)) {+ nodeMap.get(item.ParentSpanId).children.push(node);+ return;}+ orphans.push(node);});- // Sort children at every level by Timestamp (chronological order)- function sortChildren(node: any) {- if (node?.children?.length) {- node.children.sort((a: any, b: any) => {- const tsA = a.Timestamp ? new Date(a.Timestamp).getTime() : 0;- const tsB = b.Timestamp ? new Date(b.Timestamp).getTime() : 0;- return tsA - tsB;- });- node.children.forEach(sortChildren);- }- }- sortChildren(root);+ // Prefer real roots; only promote orphans when the backend omitted them.+ const roots = explicitRoots.length > 0 ? explicitRoots : orphans;+ if (roots.length === 0) return null;- return root; // Returns the hierarchical tree+ roots.sort(compareTimestamp);+ const primary = roots[0];+ for (const sibling of roots.slice(1)) {+ primary.children.push(sibling);+ }+ sortChildren(primary);+ return primary;};diff --git a/src/client/src/lib/access/connector-entitlement.ts b/src/client/src/lib/access/connector-entitlement.tsnew file mode 100644index 00000000..72dbf7ee--- /dev/null+++ b/src/client/src/lib/access/connector-entitlement.ts@@ -0,0 +1,21 @@+/**+ * CE fallback: premium connector types are gated only by adapter registration+ * and the visible-types allow-list. Enterprise overrides this module to enforce+ * entitlements for paid datasource connectors (Datadog, New Relic, …).+ */+export async function assertPremiumConnectorAllowed(+ _type: string+): Promise<void> {+ // no-op in CE+}++/**+ * CE fallback: catalog UI never locks types by plan. Enterprise returns which+ * premium datasource types should show as locked for the current organisation.+ */+export async function getConnectorCatalogLocks(): Promise<{+ entitled: boolean;+ premiumTypes: ReadonlySet<string>;+}> {+ return { entitled: true, premiumTypes: new Set() };+}diff --git a/src/client/src/lib/access/connector-route.ts b/src/client/src/lib/access/connector-route.tsnew file mode 100644index 00000000..be5d8ae9--- /dev/null+++ b/src/client/src/lib/access/connector-route.ts@@ -0,0 +1,17 @@+/**+ * CE fallback for connector route protection and audit hooks.+ * Enterprise replaces this neutral module through its path aliases.+ */+export type ConnectorAction = "read" | "create" | "update" | "delete" | "test" | "bind";++export function withConnectorAccess<THandler>(+ _action: ConnectorAction,+ handler: THandler+): THandler {+ return handler;+}++export function withConnectorAudit<THandler>(handler: THandler): THandler {+ return handler;+}+diff --git a/src/client/src/lib/db-config.ts b/src/client/src/lib/db-config.tsindex 63f0cc80..f668e6fc 100644--- a/src/client/src/lib/db-config.ts+++ b/src/client/src/lib/db-config.ts@@ -1,13 +1,27 @@import asaw from "@/utils/asaw";import prisma from "./prisma";+import { removeLegacyConnector, syncDatabaseConfigConnector } from "@/lib/platform/connectors/instances";import { getCurrentUser } from "./session";import { DatabaseConfig, DatabaseConfigInvitedUser } from "@prisma/client";import migrations from "@/clickhouse/migrations";import getMessage from "@/constants/messages";import { throwIfError } from "@/utils/error";import { consoleLog } from "@/utils/log";-import { getCurrentOrganisation, getCurrentProjectForOrganisation } from "./organisation";+import { getCurrentOrganisation, getCurrentProjectForOrganisation } from "@/lib/organisation";import { validateDatabaseHost } from "@/utils/validation";+import { createProjectEnvironment, normalizeProjectEnvironment } from "./project-environment";++async function ensureEnvironmentDatabaseBindings(projectId: string, databaseConfigId: string, environment: string) {+ if (!prisma.telemetrySourceBinding?.findUnique) return;+ for (const signal of ["traces", "logs", "metrics", "intelligence"]) {+ const existing = await prisma.telemetrySourceBinding.findUnique({+ where: { projectId_signal_environment: { projectId, signal, environment } },+ });+ if (!existing) {+ await prisma.telemetrySourceBinding.create({ data: { projectId, signal, environment, databaseConfigId } });+ }+ }+}export const getDBConfigByUser = async (currentOnly?: boolean) => {const user = await getCurrentUser();@@ -19,7 +33,6 @@ export const getDBConfigByUser = async (currentOnly?: boolean) => {const currentProject = currentOrg?.id? await getCurrentProjectForOrganisation(currentOrg.id): null;-// Auto-migrate orphaned configs: If user has a current organisation, move any orphaned configs// they have access to into its current project. This handles edge cases where migration didn't run// or new orphaned configs were created.@@ -123,6 +136,19 @@ export const getDBConfigById = async ({ id }: { id: string }) => {return getDBConfigByIdForUser({ id, userId: user.id });};+/**+ * DatabaseConfig lookup for callers that already have an id and may run+ * without a user session (cron materializer, auto-eval, auto-pricing).+ * Interactive requests with a session stay user- and project-scoped.+ */+export const getDBConfigByIdForBackground = async ({ id }: { id: string }) => {+ const user = await getCurrentUser();+ if (!user) {+ return getDBConfigByIdInternal({ id });+ }+ return getDBConfigByIdForUser({ id, userId: user.id });+};+export const getDBConfigByIdForUser = async ({id,userId,@@ -170,7 +196,12 @@ export const upsertDBConfig = async (if (!dbConfig.port) throw new Error("No port provided");if (!dbConfig.database) throw new Error("No database provided");- const hostValidation = validateDatabaseHost(dbConfig.host);+ // ClickHouse is an explicitly configured project connector. Local and+ // private-network hosts are valid here (including the default 127.0.0.1+ // database); HTTP telemetry connectors retain the stricter SSRF checks.+ const hostValidation = validateDatabaseHost(dbConfig.host, {+ allowPrivateNetwork: true,+ });if (!hostValidation.valid) {throw new Error(hostValidation.error || "Invalid host");}@@ -188,11 +219,15 @@ export const upsertDBConfig = async (const currentProject = currentOrg?.id? await getCurrentProjectForOrganisation(currentOrg.id): null;+ const environment = normalizeProjectEnvironment(dbConfig.environment || "production");+ dbConfig.environment = environment;+ if (currentProject?.id) await createProjectEnvironment(environment);const existingDBName = await prisma.databaseConfig.findFirst({where: {name: dbConfig.name,projectId: currentProject?.id || null,+ environment,NOT: {id,},@@ -229,9 +264,10 @@ export const upsertDBConfig = async (// When creating with a project, use compound unique constraintelse if (currentProject?.id) {const whereObject = {- name_projectId: {+ name_projectId_environment: {name: dbConfig.name,projectId: currentProject.id,+ environment,},};const [err, result] = await asaw(@@ -287,7 +323,37 @@ export const upsertDBConfig = async (canDelete: true,canShare: true,});- migrations(createddbConfig.id);+ // A saved connector must be ready for every ClickHouse-backed feature+ // before the API returns. In particular, Rule Engine must not race the+ // asynchronous creation of its openlit_rules tables.+ await migrations(createddbConfig.id);+ }+ if (createddbConfig.projectId) {+ try {+ await ensureEnvironmentDatabaseBindings(createddbConfig.projectId, createddbConfig.id, createddbConfig.environment || environment);+ } catch (error) {+ // Bindings are a routing projection. Do not roll back a valid database+ // configuration when an older installation has not applied the binding+ // migration yet; the next connector/environment read can repair it.+ console.error("[connectors] ClickHouse signal binding projection failed", {+ databaseConfigId: createddbConfig.id,+ projectId: createddbConfig.projectId,+ environment: createddbConfig.environment,+ error,+ });+ }+ }+ // The legacy database config remains the write path for platform features.+ // Connector indexing is a compatibility projection and must not make a+ // valid ClickHouse configuration impossible to save if an older deployment+ // has not applied the connector framework migration yet.+ try {+ await syncDatabaseConfigConnector(createddbConfig);+ } catch (error) {+ console.error("[connectors] ClickHouse connector projection failed", {+ databaseConfigId: createddbConfig.id,+ error,+ });}return `${id ? "Updated" : "Added"} db details successfully`;@@ -314,6 +380,7 @@ export async function deleteDBConfig(id: string) {id,},});+ await removeLegacyConnector("database-config", id);return "Deleted successfully!";}diff --git a/src/client/src/lib/platform/agents/agent-key.ts b/src/client/src/lib/platform/agents/agent-key.tsnew file mode 100644index 00000000..c5ee3672--- /dev/null+++ b/src/client/src/lib/platform/agents/agent-key.ts@@ -0,0 +1,69 @@+/**+ * Leaf helpers for agent identity + cache invalidation.+ *+ * Extracted from `./index` so that `snapshot` and `materialize` can compute+ * agent keys / drop cached rows WITHOUT importing `./index`. `index` imports+ * `materialize`, and both `materialize` and `snapshot` previously imported+ * `computeAgentKey`/`invalidateAgent` back from `index`, forming an+ * index <-> materialize <-> snapshot import cycle. That latent cycle produced a+ * "Cannot access 'X' before initialization" TDZ once webpack chunked these+ * modules together in a production build. This module depends only on `crypto`+ * and the leaf `./cache`, so it breaks the cycle.+ */++import { createHash } from "crypto";+import { invalidate } from "./cache";++/** Drop the cached detail row for an agent so the next read is fresh. */+export function invalidateAgent(agentKey: string, dbConfigId?: string) {+ invalidate(`agents:detail:${dbConfigId || "default"}:${agentKey}`);+}++/**+ * Collapse placeholder / local-dev environment labels to `default` so SDK+ * sample apps don't create a noisy `local` dimension on agent identity.+ */+export function normalizeDeploymentEnvironment(+ environment?: string | null+): string {+ const env = (environment || "").trim();+ if (+ !env ||+ env.toLowerCase() === "local" ||+ env === "default_environment"+ ) {+ return "default";+ }+ return env;+}++/**+ * ClickHouse predicate that treats empty / local-dev labels as `default`.+ */+export function deploymentEnvironmentSqlPredicate(+ environment: string | null | undefined,+ escape: (value: string) => string+): string {+ const env = normalizeDeploymentEnvironment(environment);+ if (env === "default") {+ return `(ResourceAttributes['deployment.environment'] IN ('default', 'local', 'default_environment', ''))`;+ }+ return `ResourceAttributes['deployment.environment'] = '${escape(env)}'`;+}++/**+ * Compute the deterministic agent_key used as the URL slug + primary key.+ * Matches the formula used by the materializer.+ */+export function computeAgentKey(+ clusterId: string,+ environment: string,+ serviceName: string+): string {+ const cluster = clusterId || "default";+ const env = normalizeDeploymentEnvironment(environment);+ return createHash("sha1")+ .update(`${cluster}|${env}|${serviceName}`)+ .digest("hex")+ .slice(0, 16);+}diff --git a/src/client/src/lib/platform/agents/aggregate-graph.ts b/src/client/src/lib/platform/agents/aggregate-graph.tsindex 6ed7a46c..ed3e2153 100644--- a/src/client/src/lib/platform/agents/aggregate-graph.ts+++ b/src/client/src/lib/platform/agents/aggregate-graph.ts@@ -17,14 +17,18 @@*/import {- dataCollector,+ intelligenceDataCollector,OTEL_TRACES_TABLE_NAME,} from "@/lib/platform/common";import { buildVersionWhereClause } from "./version-filter";+import { deploymentEnvironmentSqlPredicate } from "./agent-key";import { agentsLogger } from "./logger";import type { VersionFilter } from "@/types/platform";import { escapeClickHouseString } from "@/lib/clickhouse-escape";-import { deploymentEnvironmentSqlPredicate } from "./index";+import { resolveSignalReadContext } from "@/lib/platform/connectors/datasource/facade";+import { buildAggregateDag } from "@/lib/platform/connectors/datasource/graph/aggregate-dag";+import type { AggregateDag } from "@/lib/platform/connectors/datasource/graph/aggregate-dag";+import type { OpenLITQuery } from "@/lib/platform/connectors/datasource/types";const escape = escapeClickHouseString;@@ -71,9 +75,16 @@ export interface AggregateGraphParams {dbConfigId?: string;/** Cap aggregated traces. Higher = more accurate p50 but heavier query. */maxTraces?: number;+ /** Absolute window for external sampling (defaults to last 24h). */+ timeRange?: { start: Date; end: Date };}const DEFAULT_MAX_TRACES = 500;+/** External vendors download full OTLP per trace — keep this small. */+const EXTERNAL_DEFAULT_MAX_TRACES = 30;+const EXTERNAL_HARD_CAP = 40;+/** Cap external DAG windows so version first→last doesn't scan months. */+const EXTERNAL_MAX_WINDOW_MS = 24 * 60 * 60 * 1000;const HEALTHY_STATUS_VALUES = ["STATUS_CODE_OK",@@ -87,10 +98,151 @@ const HEALTHY_STATUS_VALUES = [* supplied the function falls back to a recent 24h window so the call-site* still has something to render.*/+/**+ * Convert the vendor-agnostic `AggregateDag` (built in-process from sampled+ * external traces) into the `AggregateGraph` shape the detail page renders.+ * External trace backends lack ClickHouse's self-join, so we sample bounded+ * full traces and reconstruct nodes/edges here (see `buildAggregateDag`).+ */+function dagToAggregateGraph(dag: AggregateDag): AggregateGraph {+ const nodes: AggregateNode[] = dag.nodes.map((n) => ({+ id: n.name,+ spanName: n.name,+ spanCount: n.count,+ p50Ms: n.p50DurationMs,+ errorRate: n.count ? n.errorCount / n.count : 0,+ depth: 0,+ kind: "span",+ }));+ const edges: AggregateEdge[] = dag.edges+ .filter((e) => e.from !== e.to)+ .map((e) => ({ from: e.from, to: e.to, count: e.count, p50Ms: 0, errorRate: 0 }));+ assignDepths(nodes, edges);+ const spanCount = nodes.reduce((s, n) => s + n.spanCount, 0);+ return { nodes, edges, traceCount: dag.sampledTraces, spanCount };+}++/**+ * External trace sources (Tempo, Jaeger, …) cannot express the ClickHouse+ * self-join. When traces resolve to a non-built-in source, sample a bounded+ * set of full traces via the adapter and reconstruct the DAG in-process.+ * Returns `null` when the source is built-in (caller keeps the SQL path),+ * when sampling fails, or when the sample is empty so the caller can fall+ * back to ClickHouse (common when Tempo is bound but agent spans still live+ * in the intelligence / vault ClickHouse).+ */+async function getExternalAggregateGraph(+ params: AggregateGraphParams,+ maxTraces: number+): Promise<AggregateGraph | null> {+ let adapter;+ try {+ const ctx = await resolveSignalReadContext("traces", {+ environment: params.environment,+ });+ if (ctx.isBuiltIn) return null;+ adapter = ctx.adapter;+ } catch {+ // Source resolution failed — keep the built-in ClickHouse SQL path.+ return null;+ }++ let end =+ params.timeRange?.end ||+ (params.versionFilter?.lastSeen+ ? new Date(params.versionFilter.lastSeen)+ : new Date());+ let start =+ params.timeRange?.start ||+ (params.versionFilter?.firstSeen+ ? new Date(params.versionFilter.firstSeen)+ : new Date(end.getTime() - EXTERNAL_MAX_WINDOW_MS));+ if (end.getTime() - start.getTime() > EXTERNAL_MAX_WINDOW_MS) {+ start = new Date(end.getTime() - EXTERNAL_MAX_WINDOW_MS);+ }+ const filters: OpenLITQuery["filters"] = [+ {+ target: "attribute",+ scope: "resource",+ key: "service.name",+ op: "eq",+ value: params.serviceName,+ },+ ];+ if (params.environment && params.environment !== "default") {+ filters.push({+ target: "attribute",+ scope: "resource",+ key: "deployment.environment",+ op: "eq",+ value: params.environment,+ });+ }+ // Mirror ClickHouse hybrid version scoping: only require the stamped+ // hash when the version is known to carry it. Otherwise the time window+ // alone scopes the sample (historical / unstamped SDKs).+ if (+ params.versionFilter?.versionHash &&+ params.versionFilter.hasAttributeSpans+ ) {+ filters.push({+ target: "attribute",+ key: "openlit.agent.version_hash",+ op: "eq",+ value: params.versionFilter.versionHash,+ });+ }+ const query: OpenLITQuery = {+ signal: "traces",+ timeRange: { start, end },+ filters,+ // Service is already pinned; the AI selector is optional enrichment+ // and must not exclude non-heuristic agent spans for this surface.+ aiSelector: false,+ };+ try {+ const spans = await adapter.sampleTracesForGraph(query, maxTraces);+ const graph = dagToAggregateGraph(buildAggregateDag(spans));+ if (graph.traceCount === 0) return null;+ return graph;+ } catch (err) {+ agentsLogger.error("aggregate_graph_external_failed", { err });+ return null;+ }+}++async function resolveAggregateGraphClickHouseId(+ params: AggregateGraphParams+): Promise<string | undefined> {+ if (params.dbConfigId) return params.dbConfigId;+ try {+ const { resolveCodingAgentsClickHouseDbConfigId } = await import(+ "@/lib/platform/coding-agents/source"+ );+ return (+ (await resolveCodingAgentsClickHouseDbConfigId({+ environment: params.environment,+ })) ?? undefined+ );+ } catch {+ return undefined;+ }+}+export async function getAggregateGraph(params: AggregateGraphParams): Promise<AggregateGraph> {+ // External path first with a tighter sample budget (full OTLP downloads).+ const externalMax = Math.min(+ params.maxTraces || EXTERNAL_DEFAULT_MAX_TRACES,+ EXTERNAL_HARD_CAP+ );+ const external = await getExternalAggregateGraph(params, externalMax);+ if (external) return external;+const maxTraces = Math.max(50, params.maxTraces || DEFAULT_MAX_TRACES);+ const dbConfigId = await resolveAggregateGraphClickHouseId(params);+const envPredicate = deploymentEnvironmentSqlPredicate(params.environment,escape@@ -204,10 +356,10 @@ export async function getAggregateGraph(FROM sampled_traces`;- const combinedRes = await dataCollector(+ const combinedRes = await intelligenceDataCollector({ query: combinedQuery },"query",- params.dbConfigId+ dbConfigId);if (combinedRes.err) {agentsLogger.error("aggregate_graph_query_failed", {diff --git a/src/client/src/lib/platform/agents/cache.ts b/src/client/src/lib/platform/agents/cache.tsindex 48a108a1..571d4c57 100644--- a/src/client/src/lib/platform/agents/cache.ts+++ b/src/client/src/lib/platform/agents/cache.ts@@ -57,16 +57,32 @@ export interface SWRPolicy {staleMs: number;}+export interface SWROptions<T> {+ /** Override freshness from the loaded value (e.g. empty graphs). */+ policyFor?: (value: T) => SWRPolicy;+}++function remember<T>(key: string, value: T, policy: SWRPolicy) {+ store.set(key, {+ value: value as unknown,+ freshUntil: Date.now() + policy.freshMs,+ staleUntil: Date.now() + policy.freshMs + policy.staleMs,+ });+ evictIfNeeded();+}+/*** Cache the result of `loader` under `key` with stale-while-revalidate.*/export async function swr<T>(key: string,policy: SWRPolicy,- loader: () => Promise<T>+ loader: () => Promise<T>,+ options?: SWROptions<T>): Promise<T> {const now = Date.now();const cached = store.get(key) as Entry<T> | undefined;+ const policyForValue = (value: T) => options?.policyFor?.(value) ?? policy;if (cached && now < cached.freshUntil) {touch(key, cached);@@ -78,12 +94,7 @@ export async function swr<T>(touch(key, cached);void singleFlight(key, loader).then((value) => {- store.set(key, {- value: value as unknown,- freshUntil: Date.now() + policy.freshMs,- staleUntil: Date.now() + policy.freshMs + policy.staleMs,- });- evictIfNeeded();+ remember(key, value, policyForValue(value));}).catch(() => {// Swallow background-refresh failures; next request will retry.@@ -92,12 +103,7 @@ export async function swr<T>(}const value = await singleFlight(key, loader);- store.set(key, {- value: value as unknown,- freshUntil: Date.now() + policy.freshMs,- staleUntil: Date.now() + policy.freshMs + policy.staleMs,- });- evictIfNeeded();+ remember(key, value, policyForValue(value));return value as T;}@@ -131,3 +137,5 @@ export const POLICY_LIST: SWRPolicy = { freshMs: 30_000, staleMs: 5 * 60_000 };export const POLICY_DETAIL: SWRPolicy = { freshMs: 30_000, staleMs: 5 * 60_000 };export const POLICY_VERSIONS: SWRPolicy = { freshMs: 5 * 60_000, staleMs: 60 * 60_000 };export const POLICY_TOOLS: SWRPolicy = { freshMs: 5 * 60_000, staleMs: 60 * 60_000 };+/** Empty Tempo/Jaeger samples should not occupy the 5-minute tools window. */+export const POLICY_EMPTY_GRAPH: SWRPolicy = { freshMs: 15_000, staleMs: 15_000 };diff --git a/src/client/src/lib/platform/agents/external-discovery.ts b/src/client/src/lib/platform/agents/external-discovery.tsnew file mode 100644index 00000000..c76150b6--- /dev/null+++ b/src/client/src/lib/platform/agents/external-discovery.ts@@ -0,0 +1,373 @@+/**+ * Adapter-backed agent discovery for when raw traces live in an external+ * telemetry source (Datadog, Tempo, Jaeger, …). Controllers / derived tables+ * still live in OpenLIT ClickHouse; only the otel_traces reads move here.+ */++import type {+ DataSourceAdapter,+ DiscoveredService,+ NormalizedSpan,+ QueryTimeRange,+ ServiceRollup,+} from "@/lib/platform/connectors/datasource/types";+import type {+ AgentRuntimeConfig,+ AgentSnapshot,+ AgentTool,+ CodingAgentVendor,+} from "@/types/agents";+import { computeAgentKey } from "./agent-key";+import { fingerprint } from "./snapshot";+import { mergeProviders } from "./provider-normalize";++export interface SdkDiscoveryRow {+ service_name: string;+ environment: string;+ cluster_id: string;+ workload_key: string;+ sdk_version: string;+ sdk_language: string;+ first_seen: string;+ last_seen: string;+}++export interface CodingDiscoveryRow {+ vendor: CodingAgentVendor;+ client_version: string;+ first_seen: string;+ last_seen: string;+ session_count_24h: number;+ cost_usd_24h: number;+ active_users_24h: number;+ lines_added_24h: number;+ lines_removed_24h: number;+ lines_accepted_24h: number;+ lines_rejected_24h: number;+ edit_accept_24h: number;+ edit_reject_24h: number;+ commit_count_24h: number;+ pr_count_24h: number;+}++function windowHours(hours: number): QueryTimeRange {+ const end = new Date();+ const start = new Date(end.getTime() - hours * 60 * 60 * 1000);+ return { start, end };+}++function isCodingService(svc: DiscoveredService): boolean {+ const sdk = (svc.sdkName || "").toLowerCase();+ const name = (svc.serviceName || "").toLowerCase();+ if (sdk === "openlit-cli") return true;+ if (["cursor", "claude-code", "codex", "windsurf"].includes(name)) return true;+ return false;+}++/** Map adapter discoverServices() into the SDK discovery row shape. */+export async function discoverSdkRowsFromAdapter(+ adapter: DataSourceAdapter,+ lookbackMinutes = 30+): Promise<SdkDiscoveryRow[]> {+ const range: QueryTimeRange = {+ start: new Date(Date.now() - Math.max(lookbackMinutes, 30) * 60 * 1000),+ end: new Date(),+ };+ let services: DiscoveredService[] = [];+ try {+ services = await adapter.discoverServices(range);+ } catch {+ return [];+ }+ return services+ .filter((s) => s.serviceName && !isCodingService(s))+ .map((s) => ({+ service_name: s.serviceName,+ environment: s.environment || "default",+ cluster_id: s.clusterId || "default",+ workload_key: s.workloadKey || "",+ sdk_version: s.sdkVersion || "",+ sdk_language: s.sdkLanguage || "",+ first_seen: s.firstSeen || new Date().toISOString(),+ last_seen: s.lastSeen || new Date().toISOString(),+ }));+}++/**+ * Best-effort coding-agent discovery from sampled spans. Full ClickHouse+ * coding rollups (lines/edits/commits) degrade to session count + cost when+ * the vendor cannot express the rich GROUP BY.+ */+export async function discoverCodingRowsFromAdapter(+ adapter: DataSourceAdapter+): Promise<CodingDiscoveryRow[]> {+ const range = windowHours(24);+ let spans: NormalizedSpan[] = [];+ try {+ spans = await adapter.sampleTracesForGraph(+ {+ signal: "traces",+ timeRange: range,+ aiSelector: true,+ limit: 200,+ },+ 50+ );+ } catch {+ try {+ const frame = await adapter.listSpans({+ signal: "traces",+ timeRange: range,+ aiSelector: true,+ limit: 500,+ });+ spans = frame.rows;+ } catch {+ return [];+ }+ }++ type Acc = {+ vendor: string;+ client_version: string;+ first_seen: string;+ last_seen: string;+ sessions: Set<string>;+ users: Set<string>;+ cost: number;+ };+ const byVendor = new Map<string, Acc>();++ for (const span of spans) {+ const vendor =+ span.spanAttributes["coding_agent.client"] ||+ span.resourceAttributes["coding_agent.client"] ||+ (span.resourceAttributes["service.name"] === "claude-code"+ ? "claude-code"+ : "") ||+ (span.serviceName === "cursor" ? "cursor" : "");+ if (!vendor) continue;+ const session =+ span.spanAttributes["coding_agent.session.id"] ||+ span.resourceAttributes["coding_agent.session.id"] ||+ span.spanAttributes["session.id"] ||+ span.resourceAttributes["session.id"] ||+ "";+ let acc = byVendor.get(vendor);+ if (!acc) {+ acc = {+ vendor,+ client_version:+ span.spanAttributes["coding_agent.client.version"] ||+ span.resourceAttributes["coding_agent.hook.cli.version"] ||+ "",+ first_seen: span.timestamp,+ last_seen: span.timestamp,+ sessions: new Set(),+ users: new Set(),+ cost: 0,+ };+ byVendor.set(vendor, acc);+ }+ if (session) acc.sessions.add(session);+ const user =+ span.spanAttributes["gen_ai.user.name"] ||+ span.resourceAttributes["gen_ai.user.name"] ||+ "";+ if (user) acc.users.add(user);+ const cost = Number(+ span.spanAttributes["gen_ai.usage.cost"] ||+ span.spanAttributes["coding_agent.session.cost_usd"] ||+ 0+ );+ if (Number.isFinite(cost)) acc.cost += cost;+ if (span.timestamp < acc.first_seen) acc.first_seen = span.timestamp;+ if (span.timestamp > acc.last_seen) acc.last_seen = span.timestamp;+ }++ const allowed = new Set(["cursor", "claude-code", "codex", "windsurf"]);+ return Array.from(byVendor.values())+ .filter((a) => allowed.has(a.vendor))+ .map((a) => ({+ vendor: a.vendor as CodingAgentVendor,+ client_version: a.client_version,+ first_seen: a.first_seen,+ last_seen: a.last_seen,+ session_count_24h: a.sessions.size || 1,+ cost_usd_24h: a.cost,+ active_users_24h: a.users.size,+ lines_added_24h: 0,+ lines_removed_24h: 0,+ lines_accepted_24h: 0,+ lines_rejected_24h: 0,+ edit_accept_24h: 0,+ edit_reject_24h: 0,+ commit_count_24h: 0,+ pr_count_24h: 0,+ }));+}++export async function fetchRequestCountsFromAdapter(+ adapter: DataSourceAdapter,+ agents: Array<{+ agent_key: string;+ service_name: string;+ environment: string;+ cluster_id: string;+ source: string;+ coding_session_count_24h?: number;+ }>+): Promise<Map<string, number>> {+ const map = new Map<string, number>();+ const traditional = agents.filter((a) => a.source !== "coding");+ for (const coding of agents.filter((a) => a.source === "coding")) {+ map.set(coding.agent_key, coding.coding_session_count_24h || 0);+ }+ if (!traditional.length) return map;++ let rollups: ServiceRollup[] = [];+ try {+ rollups = await adapter.aggregateByService(windowHours(24));+ } catch {+ return map;+ }+ for (const r of rollups) {+ const key = computeAgentKey(+ r.clusterId || "default",+ r.environment || "default",+ r.serviceName+ );+ map.set(key, Number(r.requestCount || 0));+ }+ // Fallback: match by service name alone when env/cluster were empty on the vendor.+ for (const agent of traditional) {+ if (map.has(agent.agent_key)) continue;+ const match = rollups.find((r) => r.serviceName === agent.service_name);+ if (match) map.set(agent.agent_key, Number(match.requestCount || 0));+ }+ return map;+}++/**+ * Lightweight snapshot derivation from sampled external spans. Skips the+ * ClickHouse-native tool-definition log join; still fingerprints models /+ * providers / tools found on span attributes.+ */+export async function deriveSnapshotFromAdapter(+ adapter: DataSourceAdapter,+ params: {+ serviceName: string;+ environment?: string;+ clusterId?: string;+ lookbackMinutes?: number;+ }+): Promise<AgentSnapshot | null> {+ const lookback = Math.max(1, params.lookbackMinutes || 60);+ const range: QueryTimeRange = {+ start: new Date(Date.now() - lookback * 60 * 1000),+ end: new Date(),+ };+ let spans: NormalizedSpan[] = [];+ try {+ const frame = await adapter.listSpans({+ signal: "traces",+ timeRange: range,+ aiSelector: true,+ limit: 100,+ filters: [+ {+ target: "attribute",+ scope: "resource",+ key: "service.name",+ op: "eq",+ value: params.serviceName,+ },+ ],+ });+ spans = frame.rows;+ } catch {+ return null;+ }+ if (!spans.length) return null;++ const models = new Set<string>();+ const providers = new Set<string>();+ const tools: AgentTool[] = [];+ const toolNames = new Set<string>();+ let primaryModel = "";+ let systemPrompt = "";+ const runtimeConfig: AgentRuntimeConfig = {};++ for (const span of spans) {+ const model = span.spanAttributes["gen_ai.request.model"];+ const provider = span.spanAttributes["gen_ai.system"];+ if (model) {+ models.add(model);+ if (!primaryModel) primaryModel = model;+ }+ if (provider) providers.add(provider);+ const defs = span.spanAttributes["gen_ai.tool.definitions"];+ if (defs && !tools.length) {+ try {+ const parsed = JSON.parse(defs);+ if (Array.isArray(parsed)) {+ for (const t of parsed) {+ if (t?.name) {+ tools.push({+ name: String(t.name),+ description: String(t.description || ""),+ schema: t.parameters || t.schema || null,+ });+ }+ }+ }+ } catch {+ // ignore malformed tool defs+ }+ }+ const toolName = span.spanAttributes["gen_ai.tool.name"];+ if (toolName) toolNames.add(toolName);+ const prompt =+ span.spanAttributes["gen_ai.system_instructions"] ||+ span.spanAttributes["gen_ai.prompt"];+ if (prompt && !systemPrompt) systemPrompt = prompt;+ const temp = span.spanAttributes["gen_ai.request.temperature"];+ if (temp !== undefined && runtimeConfig.temperature === undefined) {+ runtimeConfig.temperature = Number(temp);+ }+ }+ for (const name of Array.from(toolNames)) {+ if (!tools.some((t) => t.name === name)) {+ tools.push({ name, description: "", schema: null });+ }+ }++ const providerList = mergeProviders(Array.from(providers));+ if (providerList.length) runtimeConfig.provider = providerList.slice().sort()[0];+ const env = params.environment || "default";+ const cluster = params.clusterId || "default";+ const hash = fingerprint({+ systemPrompt,+ tools,+ primaryModel,+ runtimeConfig,+ providers: providerList,+ });++ return {+ agent_key: computeAgentKey(cluster, env, params.serviceName),+ service_name: params.serviceName,+ environment: env,+ cluster_id: cluster,+ system_prompt: systemPrompt,+ tools,+ primary_model: primaryModel,+ models: Array.from(models),+ providers: providerList,+ runtime_config: runtimeConfig,+ version_hash: hash,+ request_count: spans.length,+ first_seen: spans[spans.length - 1]?.timestamp || new Date().toISOString(),+ last_seen: spans[0]?.timestamp || new Date().toISOString(),+ };+}diff --git a/src/client/src/lib/platform/agents/index.ts b/src/client/src/lib/platform/agents/index.tsindex 8bfb4eef..9b52646f 100644--- a/src/client/src/lib/platform/agents/index.ts+++ b/src/client/src/lib/platform/agents/index.ts@@ -8,8 +8,7 @@* current within seconds of a click without racing the materializer's tick.*/-import { createHash } from "crypto";-import { dataCollector } from "@/lib/platform/common";+import { intelligenceDataCollector } from "@/lib/platform/common";import {CONTROLLER_ACTIONS_TABLE,CONTROLLER_DESIRED_STATES_V2_TABLE,@@ -21,7 +20,8 @@ import type {AgentSource,UnifiedAgent,} from "@/types/agents";-import { invalidate, POLICY_DETAIL, POLICY_LIST, swr } from "./cache";+import { POLICY_DETAIL, POLICY_LIST, swr } from "./cache";+import { normalizeDeploymentEnvironment } from "./agent-key";import { agentsLogger } from "./logger";import { AGENTS_SUMMARY_TABLE } from "./table-details";import { escapeClickHouseString } from "@/lib/clickhouse-escape";@@ -415,7 +415,7 @@ async function loadAgents(params: ListAgentsParams): Promise<ListAgentsResult> {SETTINGS join_use_nulls = 1`;- const res = await dataCollector({ query }, "query", params.dbConfigId);+ const res = await intelligenceDataCollector({ query }, "query", params.dbConfigId);if (res.err) {agentsLogger.error("list_agents_failed", {err: res.err,@@ -521,7 +521,7 @@ async function loadAgent(LIMIT 1SETTINGS join_use_nulls = 1`;- const res = await dataCollector({ query }, "query", dbConfigId);+ const res = await intelligenceDataCollector({ query }, "query", dbConfigId);if (res.err) {agentsLogger.error("get_agent_failed", {err: res.err,@@ -534,56 +534,13 @@ async function loadAgent(return rowToAgent(rows[0]);}-/** Drop the cached detail row for an agent so the next read is fresh. */-export function invalidateAgent(agentKey: string, dbConfigId?: string) {- invalidate(`agents:detail:${dbConfigId || "default"}:${agentKey}`);-}--/**- * Collapse placeholder / local-dev environment labels to `default` so SDK- * sample apps don't create a noisy `local` dimension on agent identity.- */-export function normalizeDeploymentEnvironment(- environment?: string | null-): string {- const env = (environment || "").trim();- if (- !env ||- env.toLowerCase() === "local" ||- env === "default_environment"- ) {- return "default";- }- return env;-}--/**- * ClickHouse predicate that treats empty / local-dev labels as `default`.- */-export function deploymentEnvironmentSqlPredicate(- environment: string | null | undefined,- escape: (value: string) => string-): string {- const env = normalizeDeploymentEnvironment(environment);- if (env === "default") {- return `(ResourceAttributes['deployment.environment'] IN ('default', 'local', 'default_environment', ''))`;- }- return `ResourceAttributes['deployment.environment'] = '${escape(env)}'`;-}--/**- * Compute the deterministic agent_key used as the URL slug + primary key.- * Matches the formula used by the materializer.- */-export function computeAgentKey(- clusterId: string,- environment: string,- serviceName: string-): string {- const cluster = clusterId || "default";- const env = normalizeDeploymentEnvironment(environment);- return createHash("sha1")- .update(`${cluster}|${env}|${serviceName}`)- .digest("hex")- .slice(0, 16);-}+// `computeAgentKey` + `invalidateAgent` (+ env normalize helpers) now live in+// the leaf `./agent-key` module so `snapshot`/`materialize` can use them+// without importing `./index` (which would re-form an import cycle).+// Re-exported here for existing callers.+export {+ computeAgentKey,+ invalidateAgent,+ normalizeDeploymentEnvironment,+ deploymentEnvironmentSqlPredicate,+} from "./agent-key";diff --git a/src/client/src/lib/platform/agents/materialize.ts b/src/client/src/lib/platform/agents/materialize.tsindex 690c0a06..ddb20502 100644--- a/src/client/src/lib/platform/agents/materialize.ts+++ b/src/client/src/lib/platform/agents/materialize.ts@@ -8,7 +8,7 @@* they read from the materialized tables.*/-import { dataCollector, OTEL_TRACES_TABLE_NAME } from "@/lib/platform/common";+import { intelligenceDataCollector, OTEL_TRACES_TABLE_NAME } from "@/lib/platform/common";import {CONTROLLER_DESIRED_STATES_V2_TABLE,CONTROLLER_SERVICES_TABLE,@@ -18,7 +18,7 @@ import {computeAgentKey,invalidateAgent,normalizeDeploymentEnvironment,-} from "./index";+} from "./agent-key";import { invalidatePrefix } from "./cache";import { agentsLogger } from "./logger";import {@@ -106,6 +106,28 @@ interface DiscoveredAgent {const SDK_DISCOVERY_LOOKBACK_MINUTES = 30;+/** Resolve an external traces adapter for this dbConfig, or null when built-in. */+async function resolveExternalTracesAdapter(dbConfigId?: string) {+ if (!dbConfigId) return null;+ try {+ const { getTelemetryAdapterForDbConfig } = await import(+ "@/lib/telemetry-source"+ );+ const resolved = await getTelemetryAdapterForDbConfig(dbConfigId, "traces");+ if (resolved.isBuiltIn) return null;+ return resolved.adapter;+ } catch (err) {+ agentsLogger.error("materializer_external_adapter_resolve_failed", {+ err,+ dbConfigId,+ });+ // Fail closed. Returning null here used to make every downstream branch+ // interpret an unavailable Tempo binding as "built-in ClickHouse" and+ // silently discover agents from the wrong otel_traces table.+ throw err;+ }+}+async function discoverAgents(dbConfigId?: string,clusterFilter?: string@@ -214,13 +236,7 @@ async function discoverAgents(FROM sdk_seen`;- const sdkRes = await dataCollector({ query: sdkQuery }, "query", dbConfigId);- if (sdkRes.err) {- agentsLogger.error("materializer_sdk_discovery_failed", {- err: sdkRes.err,- });- }- const sdkRows = (sdkRes.data as Array<{+ let sdkRows: Array<{service_name: string;environment: string;cluster_id: string;@@ -229,7 +245,34 @@ async function discoverAgents(sdk_language: string;first_seen: string;last_seen: string;- }>) || [];+ }> = [];++ const externalAdapter = await resolveExternalTracesAdapter(dbConfigId);+ if (externalAdapter) {+ const { discoverSdkRowsFromAdapter } = await import("./external-discovery");+ sdkRows = await discoverSdkRowsFromAdapter(+ externalAdapter,+ SDK_DISCOVERY_LOOKBACK_MINUTES+ );+ } else {+ const sdkRes = await intelligenceDataCollector({ query: sdkQuery }, "query", dbConfigId);+ if (sdkRes.err) {+ agentsLogger.error("materializer_sdk_discovery_failed", {+ err: sdkRes.err,+ });+ }+ sdkRows =+ (sdkRes.data as Array<{+ service_name: string;+ environment: string;+ cluster_id: string;+ workload_key: string;+ sdk_version: string;+ sdk_language: string;+ first_seen: string;+ last_seen: string;+ }>) || [];+ }// The materializer needs to keep stopped workloads in agents_summary// even after their last heartbeat aged out of the 24h window. We do@@ -313,7 +356,7 @@ async function discoverAgents(latest.last_seen AS last_seenFROM latest`;- const ctrlRes = await dataCollector({ query: ctrlQuery }, "query", dbConfigId);+ const ctrlRes = await intelligenceDataCollector({ query: ctrlQuery }, "query", dbConfigId);if (ctrlRes.err) {agentsLogger.error("materializer_controller_discovery_failed", {err: ctrlRes.err,@@ -517,10 +560,42 @@ export async function recomputeCodingAgentsForWindow(return discoverCodingAgents(dbConfigId, window);}+/**+ * Coding-agent rollups are ClickHouse SQL (`coding_agent.*`, `greatest()`).+ * Tempo/Jaeger sampling drops session/cost/edit stats and diverges from+ * `/coding-agents`. SDK/controller discovery may still use the traces adapter.+ */+async function resolveCodingClickHouseDbConfigId(+ dbConfigId?: string+): Promise<string | undefined> {+ if (!dbConfigId) return undefined;+ try {+ const { getDBConfigByIdInternal } = await import("@/lib/db-config");+ const dbConfig = await getDBConfigByIdInternal({ id: dbConfigId });+ const { resolveCodingAgentsClickHouseDbConfigId } = await import(+ "@/lib/platform/coding-agents/source"+ );+ const routed = await resolveCodingAgentsClickHouseDbConfigId({+ environment: dbConfig?.environment,+ projectId: dbConfig?.projectId ?? null,+ dbConfigId,+ });+ return routed || dbConfigId;+ } catch (err) {+ agentsLogger.error("materializer_coding_db_resolve_failed", {+ err,+ dbConfigId,+ });+ return dbConfigId;+ }+}+async function discoverCodingAgents(dbConfigId?: string,window?: CodingAgentDiscoveryWindow): Promise<DiscoveredAgent[]> {+ const codingDbConfigId = await resolveCodingClickHouseDbConfigId(dbConfigId);+// Cost rollup notes:// * `coding_agent.session.cost_usd` is only stamped on the// session-root span at sessionEnd. Active / still-running@@ -706,7 +781,11 @@ async function discoverCodingAgents(HAVING vendor != ''`;- const res = await dataCollector({ query }, "query", dbConfigId);+ const res = await intelligenceDataCollector(+ { query },+ "query",+ codingDbConfigId+ );if (res.err) {agentsLogger.error("materializer_coding_discovery_failed", {err: res.err,@@ -813,6 +892,14 @@ async function fetchRequestCounts(): Promise<Map<string, number>> {if (!agents.length) return new Map();+ const externalAdapter = await resolveExternalTracesAdapter(dbConfigId);+ if (externalAdapter) {+ const { fetchRequestCountsFromAdapter } = await import(+ "./external-discovery"+ );+ return fetchRequestCountsFromAdapter(externalAdapter, agents);+ }+// Split coding agents from the rest. Their request_count is the// total number of spans observed under the vendor in the last 24h// (rolled up at discovery time as `session_count_24h`); the@@ -839,7 +926,7 @@ async function fetchRequestCounts(AND ServiceName IN (${namesList})GROUP BY service_name, environment, cluster_id`;- const res = await dataCollector({ query }, "query", dbConfigId);+ const res = await intelligenceDataCollector({ query }, "query", dbConfigId);if (res.err) {agentsLogger.error("materializer_request_count_failed", {err: res.err,@@ -1026,6 +1113,26 @@ export async function materializeAgents(dbConfigId);+ // Correlation boundary: resolve once whether the project's logs signal lives+ // in this built-in ClickHouse store. When it doesn't, deriveSnapshot skips+ // the trace->logs tool-definition enrichment instead of querying the wrong+ // store. Never fails materialization; defaults to correlatable on error.+ let logsCorrelatable = false;+ try {+ // Dynamic import keeps the auth/session (jose) chain out of the agents+ // module graph; it is only pulled at materialization time.+ const { isSignalServedByBuiltInClickHouse } = await import(+ "@/lib/telemetry-source"+ );+ logsCorrelatable = await isSignalServedByBuiltInClickHouse("logs", {+ dbConfigId,+ });+ } catch {+ // Unknown routing is not permission to query ClickHouse logs. Snapshot+ // derivation can proceed without the optional trace-to-log enrichment.+ logsCorrelatable = false;+ }+let processed = 0;let newVersions = 0;let errors = 0;@@ -1040,13 +1147,30 @@ export async function materializeAgents(// agent_versions with rows that have no system_prompt.const snapshot = agent.source === "coding"? null- : await deriveSnapshot({- serviceName: agent.service_name,- environment: agent.environment,- clusterId: agent.cluster_id,- lookbackMinutes,- dbConfigId,- });+ : await (async () => {+ const externalAdapter = await resolveExternalTracesAdapter(+ dbConfigId+ );+ if (externalAdapter) {+ const { deriveSnapshotFromAdapter } = await import(+ "./external-discovery"+ );+ return deriveSnapshotFromAdapter(externalAdapter, {+ serviceName: agent.service_name,+ environment: agent.environment,+ clusterId: agent.cluster_id,+ lookbackMinutes,+ });+ }+ return deriveSnapshot({+ serviceName: agent.service_name,+ environment: agent.environment,+ clusterId: agent.cluster_id,+ lookbackMinutes,+ dbConfigId,+ logsCorrelatable,+ });+ })();let versionHash = "";let versionNumber = 0;@@ -1159,7 +1283,7 @@ export async function materializeAgents(const escaped = nonCodingKeys.map((k) => `'${k.replace(/'/g, "''")}'`).join(", ");- const conflictRes = await dataCollector(+ const conflictRes = await intelligenceDataCollector({query: `SELECT agent_key@@ -1197,7 +1321,7 @@ export async function materializeAgents(return { processed, newVersions, errors };}- const res = await dataCollector(+ const res = await intelligenceDataCollector({ table: AGENTS_SUMMARY_TABLE, values: summaryRows },"insert",dbConfigId@@ -1231,7 +1355,7 @@ export async function materializeAgents(async function purgePlaceholderEnvironmentRows(dbConfigId?: string): Promise<void> {- const keysRes = await dataCollector(+ const keysRes = await intelligenceDataCollector({query: `SELECT agent_key@@ -1255,7 +1379,7 @@ async function purgePlaceholderEnvironmentRows(.filter(Boolean): [];- const summaryRes = await dataCollector(+ const summaryRes = await intelligenceDataCollector({query: `ALTER TABLE ${AGENTS_SUMMARY_TABLE}@@ -1274,7 +1398,7 @@ async function purgePlaceholderEnvironmentRows(if (!keys.length) return;const escaped = keys.map((k) => `'${escape(k)}'`).join(", ");- const versionsRes = await dataCollector(+ const versionsRes = await intelligenceDataCollector({query: `ALTER TABLE ${AGENT_VERSIONS_TABLE}diff --git a/src/client/src/lib/platform/agents/snapshot.ts b/src/client/src/lib/platform/agents/snapshot.tsindex 87194bb5..1f39bcf9 100644--- a/src/client/src/lib/platform/agents/snapshot.ts+++ b/src/client/src/lib/platform/agents/snapshot.ts@@ -31,7 +31,7 @@ function toAgentTimestamp(value: string | Date | number | undefined): string {return valid.toISOString().slice(0, 19).replace("T", " ");}import {- dataCollector,+ intelligenceDataCollector,OTEL_LOGS_TABLE_NAME,OTEL_TRACES_TABLE_NAME,} from "@/lib/platform/common";@@ -41,7 +41,11 @@ import type {AgentTool,AgentVersion,} from "@/types/agents";-import { computeAgentKey, deploymentEnvironmentSqlPredicate, normalizeDeploymentEnvironment } from "./index";+import {+ computeAgentKey,+ deploymentEnvironmentSqlPredicate,+ normalizeDeploymentEnvironment,+} from "./agent-key";import { AGENT_VERSIONS_TABLE } from "./table-details";import { escapeClickHouseString } from "@/lib/clickhouse-escape";import { mergeProviders } from "./provider-normalize";@@ -162,6 +166,14 @@ export interface DeriveSnapshotParams {clusterId?: string;lookbackMinutes?: number;dbConfigId?: string;+ /**+ * Correlation boundary (Grafana-style): whether the project's `logs` signal+ * is served by the same built-in ClickHouse store these traces live in. When+ * false, the trace->logs tool-definition enrichment is skipped (best-effort),+ * because the logs live in a different backend and cannot be correlated by+ * this ClickHouse-native query. Defaults to true (built-in store).+ */+ logsCorrelatable?: boolean;}interface SnapshotRow {@@ -213,7 +225,7 @@ async function fetchToolDefinitionsFromLogs(AND Timestamp >= now() - INTERVAL ${lookbackMinutes} MINUTEAND ScopeName LIKE 'openlit.instrumentation.%'`;- const res = await dataCollector({ query }, "query", dbConfigId);+ const res = await intelligenceDataCollector({ query }, "query", dbConfigId);if (res.err) {agentsLogger.error("fetch_tool_definitions_failed", {err: res.err,@@ -286,7 +298,7 @@ export async function deriveSnapshot(FORMAT JSON`;- const res = await dataCollector(+ const res = await intelligenceDataCollector({ query: query.replace(/\s+FORMAT\s+JSON\s*$/i, "") },"query",params.dbConfigId@@ -319,7 +331,11 @@ export async function deriveSnapshot(// 3. trace per-tool aggregation (tools_fallback) — names + descriptions// only, no schema. Last-ditch.let tools = parseToolDefinitions(row.tool_definitions_json || "");- if (!tools.length) {+ // Correlation boundary: only consult otel_logs when the project's logs+ // signal lives in this same built-in ClickHouse store. When logs are routed+ // to a different backend the trace->logs join is not possible here, so we+ // degrade gracefully (empty tool defs) rather than query the wrong store.+ if (!tools.length && params.logsCorrelatable !== false) {const logTools = await fetchToolDefinitionsFromLogs(params.serviceName,lookback,@@ -452,7 +468,7 @@ export async function upsertVersion()`;- const insertRes = await dataCollector(+ const insertRes = await intelligenceDataCollector({ query: insertQuery },"exec",dbConfigId@@ -476,7 +492,7 @@ export async function upsertVersion(WHERE agent_key = '${ak}' AND version_hash = '${vh}'LIMIT 1`;- const readRes = await dataCollector(+ const readRes = await intelligenceDataCollector({ query: readQuery },"query",dbConfigId@@ -553,7 +569,7 @@ export async function getVersions(ORDER BY version_number DESCLIMIT ${Math.max(1, Math.min(limit, 200))}`;- const res = await dataCollector({ query }, "query", dbConfigId);+ const res = await intelligenceDataCollector({ query }, "query", dbConfigId);if (res.err) {agentsLogger.error("get_versions_failed", {err: res.err,@@ -576,7 +592,7 @@ export async function getVersion(AND version_hash = '${escape(versionHash)}'LIMIT 1`;- const res = await dataCollector({ query }, "query", dbConfigId);+ const res = await intelligenceDataCollector({ query }, "query", dbConfigId);if (res.err) {agentsLogger.error("get_version_failed", {err: res.err,@@ -638,7 +654,7 @@ export async function getVersionTimeline(WHERE agent_key = '${escape(agentKey)}'LIMIT 1`;- const r = await dataCollector({ query: q }, "query", options.dbConfigId);+ const r = await intelligenceDataCollector({ query: q }, "query", options.dbConfigId);return (r.data as Array<{service_name: string;environment: string;@@ -684,7 +700,7 @@ export async function getVersionTimeline(ORDER BY bucket ASC`;- const res = await dataCollector({ query }, "query", options.dbConfigId);+ const res = await intelligenceDataCollector({ query }, "query", options.dbConfigId);if (res.err) {agentsLogger.error("version_timeline_failed", {err: res.err,@@ -768,7 +784,7 @@ export async function getLatestVersion(ORDER BY version_number DESCLIMIT 1`;- const res = await dataCollector({ query }, "query", dbConfigId);+ const res = await intelligenceDataCollector({ query }, "query", dbConfigId);if (res.err) {agentsLogger.error("get_latest_version_failed", {err: res.err,@@ -809,7 +825,7 @@ export async function getLatestVersionsBatch(FROM ${AGENT_VERSIONS_TABLE} FINALINNER JOIN latest USING (agent_key, version_number)`;- const res = await dataCollector({ query }, "query", dbConfigId);+ const res = await intelligenceDataCollector({ query }, "query", dbConfigId);if (res.err) {agentsLogger.error("get_latest_versions_batch_failed", {err: res.err,diff --git a/src/client/src/lib/platform/agents/version-filter.ts b/src/client/src/lib/platform/agents/version-filter.tsindex 8bc32800..b6efe8f7 100644--- a/src/client/src/lib/platform/agents/version-filter.ts+++ b/src/client/src/lib/platform/agents/version-filter.ts@@ -17,7 +17,7 @@* `getFilterWhereCondition` and the requests query builder.*/import {- dataCollector,+ intelligenceDataCollector,OTEL_TRACES_TABLE_NAME,} from "@/lib/platform/common";import type { VersionFilter } from "@/types/platform";@@ -70,6 +70,52 @@ interface AttributeProbeParams {async function probeAttributeStamping(params: AttributeProbeParams): Promise<boolean> {+ if (params.dbConfigId) {+ try {+ const { getTelemetryAdapterForDbConfig } = await import(+ "@/lib/telemetry-source"+ );+ const resolved = await getTelemetryAdapterForDbConfig(+ params.dbConfigId,+ "traces"+ );+ if (!resolved.isBuiltIn) {+ const first = new Date(params.firstSeen);+ const last = new Date(params.lastSeen);+ const frame = await resolved.adapter.listSpans({+ signal: "traces",+ timeRange: {+ start: Number.isNaN(first.getTime())+ ? new Date(Date.now() - 24 * 60 * 60 * 1000)+ : first,+ end: Number.isNaN(last.getTime()) ? new Date() : last,+ },+ aiSelector: true,+ limit: 1,+ filters: [+ {+ target: "attribute",+ scope: "resource",+ key: "service.name",+ op: "eq",+ value: params.serviceName,+ },+ {+ target: "attribute",+ scope: "span",+ key: "openlit.agent.version_hash",+ op: "eq",+ value: params.versionHash,+ },+ ],+ });+ return frame.rows.length > 0;+ }+ } catch {+ // Fall through to ClickHouse probe.+ }+ }+const first = toClickHouseDateTime(params.firstSeen);const last = toClickHouseDateTime(params.lastSeen);const envPredicate = deploymentEnvironmentSqlPredicate(@@ -85,7 +131,7 @@ async function probeAttributeStamping(AND Timestamp BETWEEN parseDateTimeBestEffort('${first}') AND parseDateTimeBestEffort('${last}')LIMIT 1`;- const res = await dataCollector({ query }, "query", params.dbConfigId);+ const res = await intelligenceDataCollector({ query }, "query", params.dbConfigId);if (res.err) {agentsLogger.error("probe_attribute_stamping_failed", {err: res.err,@@ -135,4 +181,3 @@ export async function getVersionWindow(};});}-diff --git a/src/client/src/lib/platform/api-keys/index.ts b/src/client/src/lib/platform/api-keys/index.tsindex f757d910..0895ce74 100644--- a/src/client/src/lib/platform/api-keys/index.ts+++ b/src/client/src/lib/platform/api-keys/index.ts@@ -10,6 +10,12 @@ import { getCurrentOrganisation } from "@/lib/organisation";const APIKEY_PREFIX = "openlit-";+export interface APIKeyInfo {+ id: string;+ databaseConfigId: string | null;+ createdByUser?: { email: string } | null;+}+function createAPIKey() {// Generate 32 random bytesconst key = crypto.randomBytes(32);@@ -76,6 +82,7 @@ export async function getAPIKeyInfo({ apiKey }: { apiKey: string }) {},],},+ include: { createdByUser: { select: { email: true } } },}));}diff --git a/src/client/src/lib/platform/chat/config.ts b/src/client/src/lib/platform/chat/config.tsindex 160ea704..76faf293 100644--- a/src/client/src/lib/platform/chat/config.ts+++ b/src/client/src/lib/platform/chat/config.ts@@ -1,4 +1,4 @@-import { dataCollector } from "../common";+import { intelligenceDataCollector } from "../common";import { OPENLIT_CHAT_CONFIG_TABLE } from "./table-details";import Sanitizer from "@/utils/sanitizer";import { getSecretById } from "../vault";@@ -27,7 +27,7 @@ export async function getChatConfig(LIMIT 1`;- const { data, err } = await dataCollector({ query }, "query", databaseConfigId);+ const { data, err } = await intelligenceDataCollector({ query }, "query", databaseConfigId);if (err) {return { err };@@ -91,7 +91,7 @@ export async function upsertChatConfig(const sanitizedVaultId = Sanitizer.sanitizeValue(config.vaultId);const sanitizedMeta = Sanitizer.sanitizeValue(config.meta || "{}");- const { err } = await dataCollector(+ const { err } = await intelligenceDataCollector({table: OPENLIT_CHAT_CONFIG_TABLE,values: [diff --git a/src/client/src/lib/platform/chat/conversation.ts b/src/client/src/lib/platform/chat/conversation.tsindex d47ddb60..e288296b 100644--- a/src/client/src/lib/platform/chat/conversation.ts+++ b/src/client/src/lib/platform/chat/conversation.ts@@ -1,4 +1,4 @@-import { dataCollector } from "../common";+import { intelligenceDataCollector } from "../common";import {OPENLIT_CHAT_CONVERSATION_TABLE,OPENLIT_CHAT_MESSAGE_TABLE,@@ -114,7 +114,7 @@ export async function getConversations(LIMIT 50`;- const { data, err } = await dataCollector({ query }, "query", databaseConfigId);+ const { data, err } = await intelligenceDataCollector({ query }, "query", databaseConfigId);if (err) {return { err };@@ -146,7 +146,7 @@ export async function getConversationWithMessages(LIMIT 1`;- const { data: convData, err: convErr } = await dataCollector(+ const { data: convData, err: convErr } = await intelligenceDataCollector({ query: convQuery },"query",databaseConfigId@@ -183,7 +183,7 @@ export async function getConversationWithMessages(ORDER BY created_at ASC`;- const { data: msgData, err: msgErr } = await dataCollector(+ const { data: msgData, err: msgErr } = await intelligenceDataCollector({ query: msgQuery },"query",databaseConfigId@@ -216,7 +216,7 @@ export async function createConversation(const meta = JSON.stringify(options?.meta || {});const conversationId = randomUUID();- const { err } = await dataCollector(+ const { err } = await intelligenceDataCollector({table: OPENLIT_CHAT_CONVERSATION_TABLE,values: [@@ -248,7 +248,7 @@ export async function deleteConversation(const safeId = Sanitizer.sanitizeValue(conversationId);// Delete messages first- const { err: msgErr } = await dataCollector(+ const { err: msgErr } = await intelligenceDataCollector({query: `DELETE FROM ${OPENLIT_CHAT_MESSAGE_TABLE} WHERE conversation_id = '${safeId}'`,},@@ -261,7 +261,7 @@ export async function deleteConversation(}// Delete conversation- const { err } = await dataCollector(+ const { err } = await intelligenceDataCollector({query: `DELETE FROM ${OPENLIT_CHAT_CONVERSATION_TABLE} WHERE id = '${safeId}'`,},@@ -301,7 +301,7 @@ export async function addMessage(databaseConfigId?: string): Promise<{ data?: string; err?: unknown }> {const messageId = randomUUID();- const { err } = await dataCollector(+ const { err } = await intelligenceDataCollector({table: OPENLIT_CHAT_MESSAGE_TABLE,values: [@@ -366,7 +366,7 @@ export async function updateMessage(WHERE id = '${safeId}'`;- const { err } = await dataCollector({ query }, "exec", databaseConfigId);+ const { err } = await intelligenceDataCollector({ query }, "exec", databaseConfigId);return { err };}@@ -409,7 +409,7 @@ export async function updateConversation(WHERE id = '${safeId}'`;- const { err } = await dataCollector({ query }, "exec", databaseConfigId);+ const { err } = await intelligenceDataCollector({ query }, "exec", databaseConfigId);return { err };}@@ -439,7 +439,7 @@ export async function getConversationMessages(LIMIT ${limit}`;- const { data, err } = await dataCollector({ query }, "query", databaseConfigId);+ const { data, err } = await intelligenceDataCollector({ query }, "query", databaseConfigId);if (err) {return { err };diff --git a/src/client/src/lib/platform/chat/improvement.ts b/src/client/src/lib/platform/chat/improvement.tsindex 458f3509..2b4dc4a3 100644--- a/src/client/src/lib/platform/chat/improvement.ts+++ b/src/client/src/lib/platform/chat/improvement.ts@@ -2,9 +2,10 @@ import { streamText } from "ai";import { createHash, randomUUID } from "crypto";import { getChatConfigWithApiKey } from "./config";import { OPENLIT_TRACE_ANALYSIS_TABLE } from "./table-details";-import { dataCollector } from "../common";+import { intelligenceDataCollector } from "../common";import { getModelInstance } from "./stream";-import { getHeirarchyViaSpanId } from "../request";+import { getTraceHierarchy } from "../traces/read";+import { isNativeSqlChatAvailable } from "@/lib/telemetry-source";import { TraceHeirarchySpan, TraceRow } from "@/types/trace";import {TRACE_ANALYSIS_DIMENSIONS,@@ -215,7 +216,7 @@ export async function getTraceAnalysisRuns(AND analysis_type = '${safeAnalysisType}'ORDER BY run_number ASC`;- const { data, err } = await dataCollector({ query }, "query", databaseConfigId);+ const { data, err } = await intelligenceDataCollector({ query }, "query", databaseConfigId);if (err) return { err };return { data: (data as TraceAnalysisRun[]) || [] };}@@ -251,7 +252,7 @@ export async function saveTraceAnalysisRun(const analysisJson = JSON.stringify(analysis);const worstSeverity = computeWorstSeverity(analysis);- const { err } = await dataCollector(+ const { err } = await intelligenceDataCollector({table: OPENLIT_TRACE_ANALYSIS_TABLE,values: [@@ -1326,10 +1327,12 @@ function buildAggregatedSummary(dimensionSummaries: Partial<Record<TraceAnalysisexport async function getTraceImprovement(spanId: string,databaseConfigId?: string,- scope: TraceAnalysisScope = "trace"+ scope: TraceAnalysisScope = "trace",+ environment?: string,+ traceId?: string): Promise<{ data?: { rootSpanId: string; runs: TraceAnalysisRun[] }; err?: unknown }> {- logTraceAnalysis("get_start", { spanId, scope, databaseConfigId: databaseConfigId || "" });- const { record, err } = await getHeirarchyViaSpanId(spanId);+ logTraceAnalysis("get_start", { spanId, traceId, scope, databaseConfigId: databaseConfigId || "" });+ const { record, err } = await getTraceHierarchy(spanId, { traceId, environment });const hierarchyRecord = record as TraceHeirarchySpan | undefined;if (err || !hierarchyRecord?.SpanId) {logTraceAnalysisError("get_hierarchy_failed", err || "Trace hierarchy not found", { spanId });@@ -1338,8 +1341,21 @@ export async function getTraceImprovement(const { analysisRoot, rootSpanId } = getAnalysisTarget(hierarchyRecord, spanId, scope);const analysisType = analysisTypeForScope(scope);+ const intelligenceSource = await isNativeSqlChatAvailable({+ signal: "intelligence",+ environment,+ });+ if (!intelligenceSource.available || !intelligenceSource.databaseConfigId) {+ return {+ err: `The intelligence connector is not configured for environment "${environment || "the active environment"}".`,+ };+ }- const { data: runs, err: runsErr } = await getTraceAnalysisRuns(rootSpanId, databaseConfigId, analysisType);+ const { data: runs, err: runsErr } = await getTraceAnalysisRuns(+ rootSpanId,+ intelligenceSource.databaseConfigId,+ analysisType+ );logTraceAnalysis("get_runs_loaded", {spanId,scope,@@ -1384,9 +1400,11 @@ function createDebugEvent(export async function streamTraceImprovementAnalysis(spanId: string,databaseConfigId?: string,- scope: TraceAnalysisScope = "trace"+ scope: TraceAnalysisScope = "trace",+ environment?: string,+ traceId?: string) {- logTraceAnalysis("start", { spanId, scope, databaseConfigId: databaseConfigId || "" });+ logTraceAnalysis("start", { spanId, traceId, scope, databaseConfigId: databaseConfigId || "" });const { data: config, err: configErr } =await getChatConfigWithApiKey(databaseConfigId);if (configErr || !config) {@@ -1404,7 +1422,7 @@ export async function streamTraceImprovementAnalysis(hasApiKey: Boolean(config.apiKey),});- const { record, err: hierarchyErr } = await getHeirarchyViaSpanId(spanId);+ const { record, err: hierarchyErr } = await getTraceHierarchy(spanId, { traceId, environment });const hierarchyRecord = record as TraceHeirarchySpan | undefined;if (hierarchyErr || !hierarchyRecord?.SpanId) {logTraceAnalysisError("hierarchy_failed", hierarchyErr || "Trace hierarchy not found", { spanId });@@ -1413,7 +1431,21 @@ export async function streamTraceImprovementAnalysis(const { analysisRoot, rootSpanId } = getAnalysisTarget(hierarchyRecord, spanId, scope);const analysisType = analysisTypeForScope(scope);- const { data: existingRuns } = await getTraceAnalysisRuns(rootSpanId, databaseConfigId, analysisType);+ const intelligenceSource = await isNativeSqlChatAvailable({+ signal: "intelligence",+ environment,+ });+ if (!intelligenceSource.available || !intelligenceSource.databaseConfigId) {+ return {+ err: `The intelligence connector is not configured for environment "${environment || "the active environment"}".`,+ };+ }+ const intelligenceDatabaseConfigId = intelligenceSource.databaseConfigId;+ const { data: existingRuns } = await getTraceAnalysisRuns(+ rootSpanId,+ intelligenceDatabaseConfigId,+ analysisType+ );const runNumber = (existingRuns?.length || 0) + 1;const summary = summarizeSpan(analysisRoot);@@ -1421,7 +1453,7 @@ export async function streamTraceImprovementAnalysis(const ruleContext = await getRuleContextForTraceHierarchy(analysisRoot,spanId,- databaseConfigId+ intelligenceDatabaseConfigId);logTraceAnalysis("hierarchy_loaded", {spanId,@@ -1740,7 +1772,7 @@ export async function streamTraceImprovementAnalysis(completionTokens: finishStats.completionTokens,cost: finishStats.cost,},- databaseConfigId+ intelligenceDatabaseConfigId);if (saveErr || !savedRun) {diff --git a/src/client/src/lib/platform/chat/otter-runs.ts b/src/client/src/lib/platform/chat/otter-runs.tsindex b8f39499..2c89e8ff 100644--- a/src/client/src/lib/platform/chat/otter-runs.ts+++ b/src/client/src/lib/platform/chat/otter-runs.ts@@ -1,5 +1,5 @@import { randomUUID } from "crypto";-import { dataCollector } from "../common";+import { intelligenceDataCollector } from "../common";import { OPENLIT_OTTER_RUNS_TABLE } from "./table-details";import Sanitizer from "@/utils/sanitizer";@@ -36,7 +36,7 @@ export async function saveOtterRun(databaseConfigId?: string): Promise<{ data?: string; err?: unknown }> {const id = randomUUID();- const { err } = await dataCollector(+ const { err } = await intelligenceDataCollector({table: OPENLIT_OTTER_RUNS_TABLE,values: [diff --git a/src/client/src/lib/platform/chat/save-widget.ts b/src/client/src/lib/platform/chat/save-widget.tsindex 772b1232..0ad09a4e 100644--- a/src/client/src/lib/platform/chat/save-widget.ts+++ b/src/client/src/lib/platform/chat/save-widget.ts@@ -1,5 +1,5 @@import { createWidget } from "@/lib/platform/manage-dashboard/widget";-import { dataCollector } from "@/lib/platform/common";+import { intelligenceDataCollector } from "@/lib/platform/common";import { OPENLIT_BOARD_WIDGET_TABLE_NAME } from "@/lib/platform/manage-dashboard/table-details";import Sanitizer from "@/utils/sanitizer";import crypto from "crypto";@@ -83,7 +83,7 @@ export async function saveQueryAsWidget(input: SaveWidgetInput) {const createdWidgetId = (widgetData as any)?.id || widgetId;const position = JSON.stringify({ x: 0, y: Infinity, w: 4, h: 6 });- const { err: linkErr } = await dataCollector(+ const { err: linkErr } = await intelligenceDataCollector({table: OPENLIT_BOARD_WIDGET_TABLE_NAME,values: [diff --git a/src/client/src/lib/platform/chat/schema-context.ts b/src/client/src/lib/platform/chat/schema-context.tsindex 29070ddc..ad5a6f07 100644--- a/src/client/src/lib/platform/chat/schema-context.ts+++ b/src/client/src/lib/platform/chat/schema-context.ts@@ -3,8 +3,9 @@ export function getChatSystemPrompt(): string {1. **Data Queries**: Convert natural language questions into ClickHouse SQL queries to analyze observability data (traces, metrics, costs, tokens, etc.)2. **Platform Management**: Create and manage platform resources using the available tools — rules, alerts, contexts, prompts, vault secrets, custom models, and trace analysis.+3. **Connector-routed telemetry**: Use the query_telemetry tool for current traces, logs, or metrics. It reads from the connector selected by the project's signal routing, so do not use ClickHouse SQL for telemetry when an external source is configured.-When the user asks a question about their data, generate a SQL query. When the user asks to create or manage a resource, use the appropriate tool. If unclear, ask for clarification.+When the user asks which connector, data source, database, or backend is being used, you MUST call get_telemetry_routing first and answer from its returned routing values. Never infer the connector from the SQL schema, the ClickHouse table names, or the fact that SQL is available. When the user asks a question about current traces, logs, or metrics, use query_telemetry. Use SQL only for ClickHouse-backed platform data and derived analytics that require SQL. When the user asks to create or manage a resource, use the appropriate tool. If unclear, ask for clarification.## Available Tools (called automatically when needed)@@ -15,6 +16,7 @@ When the user asks a question about their data, generate a SQL query. When the u**Vault** — create_vault_secret, update_vault_secret, delete_vault_secret, list_vault_secrets**Models** — create_custom_model, update_custom_model, delete_custom_model, list_custom_models**Trace analysis** — analyze_trace, get_trace_analysis, analyze_trace_batch, analyze_traces_by_attribute+**Telemetry** — get_telemetry_routing (reports the active connector per signal), query_telemetry (reads traces, logs, and metrics through signal routing)Guidelines:- When the user asks to create something (vault secret, rule, context, prompt, model), do it IMMEDIATELY by calling the tool. Do NOT ask for confirmation first — just create it and report what was created.diff --git a/src/client/src/lib/platform/chat/stream.ts b/src/client/src/lib/platform/chat/stream.tsindex 6c33c56e..23cd37f5 100644--- a/src/client/src/lib/platform/chat/stream.ts+++ b/src/client/src/lib/platform/chat/stream.ts@@ -6,13 +6,15 @@ import { createMistral } from "@ai-sdk/mistral";import { createCohere } from "@ai-sdk/cohere";import { getChatSystemPrompt } from "./schema-context";import { validateSQL, extractSQLFromResponse } from "./sql-validator";-import { dataCollector } from "../common";+import { intelligenceDataCollector } from "../common";+import { isNativeSqlChatAvailable } from "@/lib/telemetry-source";import {addMessage,getConversationMessages,updateConversation,} from "./conversation";import { getChatTools } from "./tools";+import { authorizeTelemetrySQLRouting } from "./telemetry-sql-routing";// ==================== Provider Factories ====================@@ -105,6 +107,7 @@ export interface StreamChatParams {model: string;userId: string;dbConfigId: string;+ environment?: string;onDelta?: (text: string) => void;onStep?: (label: string, status?: "active" | "complete" | "error", detail?: string) => void;}@@ -123,7 +126,7 @@ export interface StreamChatResult {* - Handling tool call fallback text*/export async function streamChatMessage(params: StreamChatParams): Promise<StreamChatResult> {- const { conversationId, content, provider, apiKey, model, userId, dbConfigId, onDelta, onStep } = params;+ const { conversationId, content, provider, apiKey, model, userId, dbConfigId, environment, onDelta, onStep } = params;// Save user messageonStep?.("Saving user message", "active");@@ -140,7 +143,7 @@ export async function streamChatMessage(params: StreamChatParams): Promise<Streaconst modelInstance = getModelInstance(provider, apiKey, model);const isFirstMessage = messages.filter((m) => m.role === "user").length === 1;- const tools = getChatTools(userId, dbConfigId);+ const tools = getChatTools(userId, dbConfigId, environment);onStep?.("Preparing model and tools", "complete", `${Object.keys(tools).length} tools available`);let streamError: any = null;@@ -271,7 +274,7 @@ export async function streamChatMessage(params: StreamChatParams): Promise<Streaif (responseText && !responseText.startsWith("**Error:**")) {onStep?.("Executing generated SQL", "active");const streamedText = responseText;- responseText = await executeSQLBlocksInResponse(responseText, dbConfigId);+ responseText = await executeSQLBlocksInResponse(responseText, environment);onStep?.("Executing generated SQL", "complete");if (responseText.startsWith(streamedText) && responseText.length > streamedText.length) {onDelta?.(responseText.slice(streamedText.length));@@ -301,19 +304,33 @@ export async function streamChatMessage(params: StreamChatParams): Promise<Strea/*** Find SQL code blocks in a response, execute them, and append query-result blocks.*/-async function executeSQLBlocksInResponse(text: string, dbConfigId?: string): Promise<string> {+async function executeSQLBlocksInResponse(text: string, environment?: string): Promise<string> {const sqlBlocks = extractSQLFromResponse(text);if (sqlBlocks.length === 0) return text;+ const chatSource = await isNativeSqlChatAvailable({+ signal: "intelligence",+ environment,+ });+ if (!chatSource.available || !chatSource.databaseConfigId) return text;+let enrichedText = text;for (const sql of sqlBlocks) {const validation = validateSQL(sql);if (validation.valid && validation.query) {try {- const { data: queryData, err: queryErr } = await dataCollector({+ const routing = await authorizeTelemetrySQLRouting(validation.query, {+ environment,+ databaseConfigId: chatSource.databaseConfigId,+ });+ if (!routing.allowed) {+ enrichedText += `\n\n> ${routing.error}`;+ continue;+ }+ const { data: queryData, err: queryErr } = await intelligenceDataCollector({query: validation.query,enable_readonly: true,- }, "query", dbConfigId);+ }, "query", chatSource.databaseConfigId);if (!queryErr && queryData) {const resultJson = JSON.stringify(queryData);const escapedSql = sql.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");diff --git a/src/client/src/lib/platform/chat/telemetry-sql-routing.ts b/src/client/src/lib/platform/chat/telemetry-sql-routing.tsnew file mode 100644index 00000000..1363e9fc--- /dev/null+++ b/src/client/src/lib/platform/chat/telemetry-sql-routing.ts@@ -0,0 +1,86 @@+import type { Signal } from "@/lib/platform/connectors/datasource/types";+import { resolveSignalSource } from "@/lib/telemetry-source";++const TELEMETRY_TABLE_SIGNAL: Record<string, Signal> = {+ otel_traces: "traces",+ otel_logs: "logs",+ otel_metrics_gauge: "metrics",+ otel_metrics_sum: "metrics",+ otel_metrics_histogram: "metrics",+ otel_metrics_summary: "metrics",+ otel_metrics_exponential_histogram: "metrics",+};++export interface TelemetrySQLRoutingResult {+ allowed: boolean;+ blockedSignals: Array<{+ signal: Signal;+ sourceName: string;+ sourceType: string;+ }>;+ error?: string;+}++/** Signals whose raw OTel tables are referenced by a SQL statement. */+export function telemetrySignalsReferencedBySQL(query: string): Signal[] {+ const signals = new Set<Signal>();+ for (const [table, signal] of Object.entries(TELEMETRY_TABLE_SIGNAL)) {+ if (new RegExp(`\\b${table}\\b`, "i").test(query)) signals.add(signal);+ }+ return Array.from(signals);+}++/**+ * Prevent Otter SQL from bypassing per-signal connector routing.+ *+ * SQL executes against the ClickHouse database selected for intelligence. A+ * raw telemetry table is safe to read only when that signal is routed to the+ * exact same DatabaseConfig. Tempo (or a second ClickHouse database) must not+ * accidentally expose records from the intelligence database.+ */+export async function authorizeTelemetrySQLRouting(+ query: string,+ options: { environment?: string; databaseConfigId: string }+): Promise<TelemetrySQLRoutingResult> {+ const signals = telemetrySignalsReferencedBySQL(query);+ const resolutions = await Promise.all(+ signals.map(async (signal) => ({+ signal,+ resolution: await resolveSignalSource(signal, {+ environment: options.environment,+ }),+ }))+ );+ const blockedSignals = resolutions+ .filter(({ resolution }) => {+ const descriptor = resolution.descriptor;+ return (+ !resolution.hasSource ||+ descriptor.type !== "clickhouse" ||+ !descriptor.dbConfigId ||+ descriptor.dbConfigId !== options.databaseConfigId+ );+ })+ .map(({ signal, resolution }) => ({+ signal,+ sourceName: resolution.hasSource+ ? resolution.descriptor.name+ : "no configured source",+ sourceType: resolution.hasSource+ ? resolution.descriptor.type+ : "none",+ }));++ if (!blockedSignals.length) return { allowed: true, blockedSignals: [] };+ const routes = blockedSignals+ .map(+ ({ signal, sourceName, sourceType }) =>+ `${signal} -> ${sourceName} (${sourceType})`+ )+ .join(", ");+ return {+ allowed: false,+ blockedSignals,+ error: `ClickHouse telemetry query blocked by signal routing: ${routes}. Use connector-routed telemetry instead.`,+ };+}diff --git a/src/client/src/lib/platform/chat/tools.ts b/src/client/src/lib/platform/chat/tools.tsindex d37d05f5..049442f9 100644--- a/src/client/src/lib/platform/chat/tools.ts+++ b/src/client/src/lib/platform/chat/tools.ts@@ -37,7 +37,10 @@ import {TraceAnalysisRun,} from "./improvement";import { TRACE_ANALYSIS_DIMENSIONS } from "@/types/trace-analysis";-import { dataCollector } from "../common";+import { listTraceRecords } from "../traces/read";+import { getLogs } from "../logs/read";+import { listMetricRecords } from "../metrics/read";+import { resolveSignalSource } from "@/lib/telemetry-source";import Sanitizer from "@/utils/sanitizer";import {createAlertDestinationTool,@@ -168,10 +171,11 @@ async function runTraceAnalysisTool(spanId: string,scope: OtterTraceAnalysisScope,rerun: boolean,- databaseConfigId: string+ databaseConfigId: string,+ environment?: string) {if (!rerun) {- const existing = await getTraceImprovement(spanId, databaseConfigId, scope);+ const existing = await getTraceImprovement(spanId, databaseConfigId, scope, environment);if (existing.err) return { success: false, error: String(existing.err) };const latest = existing.data?.runs?.at(-1);if (latest) {@@ -185,7 +189,7 @@ async function runTraceAnalysisTool(}}- const { response, err } = await streamTraceImprovementAnalysis(spanId, databaseConfigId, scope);+ const { response, err } = await streamTraceImprovementAnalysis(spanId, databaseConfigId, scope, environment);if (err || !response) {return { success: false, error: String(err || "Failed to start trace analysis") };}@@ -219,7 +223,7 @@ function normalizeVaultKey(key: string): string {.toUpperCase();}-export function getChatTools(userId: string, databaseConfigId: string) {+export function getChatTools(userId: string, databaseConfigId: string, environment?: string) {return {// ==================== RULE ENGINE ====================@@ -265,10 +269,10 @@ export function getChatTools(userId: string, databaseConfigId: string) {description: params.description || "",group_operator: params.group_operator || "AND",status: params.status || "ACTIVE",- });+ }, { databaseConfigId });const ruleId = (result as any)?.id;if (ruleId && params.condition_groups?.length > 0) {- await addConditionGroupsToRule(ruleId, params.condition_groups);+ await addConditionGroupsToRule(ruleId, params.condition_groups, { databaseConfigId });}return { success: true, message: "Rule created", details: `Name: "${params.name}" | ID: ${ruleId} | Status: ${params.status || "ACTIVE"} | Conditions: ${params.condition_groups?.length || 0} groups` };} catch (e: any) {@@ -297,7 +301,7 @@ export function getChatTools(userId: string, databaseConfigId: string) {description: params.description,group_operator: params.group_operator,status: params.status,- });+ }, { databaseConfigId });return { success: true, message: "Rule updated", details: `ID: ${params.id}` };} catch (e: any) {return { success: false, error: e.message || "Failed to update rule" };@@ -314,7 +318,7 @@ export function getChatTools(userId: string, databaseConfigId: string) {}) as any,execute: async (params: any) => {try {- const [err] = await deleteRule(params.id);+ const [err] = await deleteRule(params.id, { databaseConfigId });if (err) return { success: false, error: err };return { success: true, message: "Rule deleted", details: `ID: ${params.id}` };} catch (e: any) {@@ -328,7 +332,7 @@ export function getChatTools(userId: string, databaseConfigId: string) {inputSchema: jsonSchema({ type: "object" as const, properties: {} }) as any,execute: async () => {try {- const { data, err } = await getRules();+ const { data, err } = await getRules(databaseConfigId);if (err) return { success: false, error: String(err) };const rules = (data as any[]) || [];return { success: true, count: rules.length, rules: rules.map((r: any) => ({ id: r.id, name: r.name, status: r.status, description: r.description })) };@@ -347,7 +351,7 @@ export function getChatTools(userId: string, databaseConfigId: string) {}) as any,execute: async (params: any) => {try {- const result = await getRuleById(params.id);+ const result = await getRuleById(params.id, databaseConfigId);if ((result as any).err) return { success: false, error: (result as any).err };return { success: true, rule: (result as any).data };} catch (e: any) {@@ -373,7 +377,7 @@ export function getChatTools(userId: string, databaseConfigId: string) {rule_id: params.rule_id,entity_type: params.entity_type,entity_id: params.entity_id,- });+ }, { databaseConfigId });return { success: true, message: `${params.entity_type} linked to rule`, details: `Rule: ${params.rule_id} → ${params.entity_type}: ${params.entity_id}` };} catch (e: any) {return { success: false, error: e.message || "Failed to link entity" };@@ -390,7 +394,7 @@ export function getChatTools(userId: string, databaseConfigId: string) {}) as any,execute: async (params: any) => {try {- const [err] = await deleteRuleEntity(params.id);+ const [err] = await deleteRuleEntity(params.id, { databaseConfigId });if (err) return { success: false, error: err };return { success: true, message: "Entity unlinked from rule" };} catch (e: any) {@@ -410,7 +414,7 @@ export function getChatTools(userId: string, databaseConfigId: string) {}) as any,execute: async (params: any) => {try {- const { data, err } = await getRuleEntities(params);+ const { data, err } = await getRuleEntities(params, databaseConfigId);if (err) return { success: false, error: String(err) };return { success: true, entities: data };} catch (e: any) {@@ -1027,6 +1031,99 @@ export function getChatTools(userId: string, databaseConfigId: string) {},}),+ get_telemetry_routing: tool<any, any>({+ description: "Report the currently routed connector for traces, logs, metrics, and intelligence in the active environment. Always use this tool before answering which connector or database OpenLIT is using; never infer the answer from the ClickHouse schema or generated SQL.",+ inputSchema: jsonSchema({+ type: "object" as const,+ properties: {},+ }) as any,+ execute: async () => {+ try {+ const signals = ["traces", "logs", "metrics", "intelligence"] as const;+ const routed = await Promise.all(+ signals.map(async (signal) => {+ const resolution = await resolveSignalSource(signal, { environment });+ return {+ signal,+ environment: environment || resolution.descriptor.environment || "active",+ connector: resolution.hasSource ? resolution.descriptor.name : null,+ connectorType: resolution.hasSource ? resolution.descriptor.type : null,+ isBuiltIn: resolution.hasSource ? resolution.descriptor.isBuiltIn : false,+ configured: resolution.hasSource,+ resolution: resolution.via,+ };+ })+ );+ return {+ success: true,+ environment: environment || "active",+ routing: routed,+ };+ } catch (e: any) {+ return { success: false, error: e.message || "Failed to resolve telemetry connector routing" };+ }+ },+ }),++ query_telemetry: tool<any, any>({+ description: "Read traces, logs, or metrics from the connector selected by the current project's signal routing. Use this for telemetry questions instead of generating ClickHouse SQL when the user asks about current observability data.",+ inputSchema: jsonSchema({+ type: "object" as const,+ properties: {+ signal: { type: "string", enum: ["traces", "logs", "metrics"] },+ limit: { type: "number", description: "Maximum records to return, up to 100." },+ start: { type: "string", description: "ISO start time. Defaults to the last 24 hours." },+ end: { type: "string", description: "ISO end time. Defaults to now." },+ service_name: { type: "string", description: "Optional service.name filter." },+ attribute_key: { type: "string", description: "Optional span/log/metric attribute key." },+ attribute_value: { type: "string", description: "Optional exact attribute value." },+ },+ required: ["signal"],+ }) as any,+ execute: async (params: any) => {+ try {+ const signal = params.signal as "traces" | "logs" | "metrics";+ const end = params.end ? new Date(params.end) : new Date();+ const start = params.start+ ? new Date(params.start)+ : new Date(end.getTime() - 24 * 60 * 60 * 1000);+ if (Number.isNaN(start.getTime()) || Number.isNaN(end.getTime())) {+ return { success: false, error: "start and end must be valid ISO timestamps" };+ }+ const customFilters = [] as Array<Record<string, string>>;+ if (params.service_name) {+ customFilters.push({ key: "service.name", value: String(params.service_name), scope: signal === "logs" ? "resource" : "resource" });+ }+ if (params.attribute_key && params.attribute_value !== undefined) {+ customFilters.push({ key: String(params.attribute_key), value: String(params.attribute_value), scope: signal === "logs" ? "log" : signal === "metrics" ? "metric" : "span" });+ }+ const queryParams = {+ timeLimit: { type: "CUSTOM", start, end },+ limit: Math.max(1, Math.min(Number(params.limit) || 25, 100)),+ offset: 0,+ sorting: { type: "Timestamp", direction: "desc" },+ selectedConfig: customFilters.length ? { customFilters } : undefined,+ environment,+ };+ const result = (signal === "traces"+ ? await listTraceRecords(queryParams)+ : signal === "logs"+ ? await getLogs(queryParams)+ : await listMetricRecords(queryParams)) as { err?: unknown; records?: unknown[]; total?: number };+ if (result.err) return { success: false, error: String(result.err) };+ return {+ success: true,+ signal,+ count: result.records?.length || 0,+ total: result.total || 0,+ records: result.records || [],+ };+ } catch (e: any) {+ return { success: false, error: e.message || "Failed to read telemetry through the selected connector" };+ }+ },+ }),+// ==================== TRACE ANALYSIS ====================analyze_trace: tool<any, any>({@@ -1047,7 +1144,8 @@ export function getChatTools(userId: string, databaseConfigId: string) {params.span_id,scope,Boolean(params.rerun),- databaseConfigId+ databaseConfigId,+ environment);} catch (e: any) {return { success: false, error: e.message || "Failed to run trace analysis" };@@ -1068,7 +1166,7 @@ export function getChatTools(userId: string, databaseConfigId: string) {execute: async (params: any) => {try {const scope: OtterTraceAnalysisScope = params.scope === "span" ? "span" : "trace";- const { data, err } = await getTraceImprovement(params.span_id, databaseConfigId, scope);+ const { data, err } = await getTraceImprovement(params.span_id, databaseConfigId, scope, environment);if (err) return { success: false, error: String(err) };const runs = data?.runs || [];return {@@ -1116,7 +1214,8 @@ export function getChatTools(userId: string, databaseConfigId: string) {spanId,scope,Boolean(params.rerun),- databaseConfigId+ databaseConfigId,+ environment));}@@ -1154,25 +1253,35 @@ export function getChatTools(userId: string, databaseConfigId: string) {return { success: false, error: "Attribute key and value are required" };}- const query = `- SELECT- TraceId AS traceId,- any(SpanId) AS spanId,- count() AS spanCount,- max(Timestamp) AS lastSeen- FROM otel_traces- WHERE SpanAttributes['${attributeKey}'] = '${attributeValue}'- GROUP BY TraceId- ORDER BY lastSeen DESC- LIMIT ${limit}- `;- const { data, err } = await dataCollector(- { query, enable_readonly: true },- "query",- databaseConfigId- );- if (err) return { success: false, error: String(err) };- const matches = ((data as any[]) || []).filter((row) => row.spanId);+ const end = new Date();+ const start = new Date(end.getTime() - 7 * 24 * 60 * 60 * 1000);+ const telemetryResult = await listTraceRecords({+ timeLimit: { type: "CUSTOM", start, end },+ limit: 1000,+ offset: 0,+ sorting: { type: "Timestamp", direction: "desc" },+ selectedConfig: {+ customFilters: [{+ key: attributeKey,+ value: attributeValue,+ scope: "span",+ }],+ },+ environment,+ });+ if (telemetryResult.err) return { success: false, error: String(telemetryResult.err) };+ const grouped = new Map<string, { traceId: string; spanId: string; spanCount: number }>();+ for (const row of (telemetryResult.records || []) as any[]) {+ if (!row.TraceId || !row.SpanId) continue;+ const existing = grouped.get(String(row.TraceId));+ if (existing) existing.spanCount += 1;+ else grouped.set(String(row.TraceId), {+ traceId: String(row.TraceId),+ spanId: String(row.SpanId),+ spanCount: 1,+ });+ }+ const matches = Array.from(grouped.values()).slice(0, limit);if (!matches.length) {return {success: false,@@ -1190,7 +1299,8 @@ export function getChatTools(userId: string, databaseConfigId: string) {match.spanId,"trace",Boolean(params.rerun),- databaseConfigId+ databaseConfigId,+ environment),});}diff --git a/src/client/src/lib/platform/chat/usage.ts b/src/client/src/lib/platform/chat/usage.tsindex a095bdcb..1b189920 100644--- a/src/client/src/lib/platform/chat/usage.ts+++ b/src/client/src/lib/platform/chat/usage.ts@@ -1,5 +1,5 @@import getMessage from "@/constants/messages";-import { dataCollector } from "../common";+import { intelligenceDataCollector } from "../common";import {OPENLIT_CHAT_CONVERSATION_TABLE,OPENLIT_CHAT_MESSAGE_TABLE,@@ -275,10 +275,10 @@ export async function getOtterUsage(`;const [chatMetricsResult, chatResult, analysisResult, otterRunsResult] = await Promise.allSettled([- dataCollector({ query: chatConversationMetricsQuery }, "query", databaseConfigId),- dataCollector({ query: chatQuery }, "query", databaseConfigId),- dataCollector({ query: analysisQuery }, "query", databaseConfigId),- dataCollector({ query: otterRunsQuery }, "query", databaseConfigId),+ intelligenceDataCollector({ query: chatConversationMetricsQuery }, "query", databaseConfigId),+ intelligenceDataCollector({ query: chatQuery }, "query", databaseConfigId),+ intelligenceDataCollector({ query: analysisQuery }, "query", databaseConfigId),+ intelligenceDataCollector({ query: otterRunsQuery }, "query", databaseConfigId),]);const chatMetricsRow =diff --git a/src/client/src/lib/platform/coding-agents/auth.ts b/src/client/src/lib/platform/coding-agents/auth.tsindex b3448825..c93bf660 100644--- a/src/client/src/lib/platform/coding-agents/auth.ts+++ b/src/client/src/lib/platform/coding-agents/auth.ts@@ -33,6 +33,12 @@ export interface CodingAgentAuth {role: CodingAgentRole;/** Underlying organisation_users.role; useful for finer-grained UI gating. */rawRole: string;+ /**+ * Env-routed ClickHouse id for coding-agents SQL. Set by API routes via+ * `resolveCodingAgentsDatabaseConfigId` so reads match traces/intelligence+ * signal routing instead of the session default DB.+ */+ dbConfigId?: string;}export class CodingAgentUnauthorizedError extends Error {diff --git a/src/client/src/lib/platform/coding-agents/queries.ts b/src/client/src/lib/platform/coding-agents/queries.tsindex 2fa9b5d8..8b470de9 100644--- a/src/client/src/lib/platform/coding-agents/queries.ts+++ b/src/client/src/lib/platform/coding-agents/queries.ts@@ -21,7 +21,7 @@* value with the literal string 'low_cohort'.*/-import { dataCollector } from "@/lib/platform/common";+import { intelligenceDataCollector } from "@/lib/platform/common";import {CODING_AGENT_AUDIT_LOG_TABLE,CODING_AGENT_DISPUTES_TABLE,@@ -676,7 +676,7 @@ const SESSION_BASE_COLUMNS = `* - `ResourceAttributes['organization.id']` is the convention we* plan to set in the materializer migration; for v1 we don't yet* populate it, so the filter degrades into a per-database-config- * query (the dataCollector already scopes to the user's+ * query (the intelligence collector already scopes to the user's* databaseConfigId via session). We still emit the WHERE so we* don't have to migrate every consumer when v2 lights it up.* - Span name in CODING_AGENT_SPAN_NAMES so unrelated traces don't@@ -684,7 +684,7 @@ const SESSION_BASE_COLUMNS = `*/// whereScope is the canonical WHERE block for every coding-agents// read query. Org isolation today happens at the ClickHouse layer-// (one DB per org, picked by `dataCollector`), so we don't emit an+// (one DB per org, picked by `intelligenceDataCollector`), so we don't emit an// `organization.id = ...` filter here unless the CLI has started// stamping the resource attribute AND the deployment has opted// into the extra filter via `OPENLIT_REQUIRE_ORG_FILTER=1`.@@ -795,14 +795,14 @@ export async function listSessions(LIMIT ${limit + 1}`;- const dataPromise = dataCollector({ query: dataQuery });+ const dataPromise = intelligenceDataCollector({ query: dataQuery }, "query", auth.dbConfigId);const totalPromise = opts.withTotal- ? dataCollector({+ ? intelligenceDataCollector({query: `${sessionsRawCte}SELECT toInt64(count()) AS total FROM sessions_raw`,- })+ }, "query", auth.dbConfigId): Promise.resolve({ data: [] as Array<{ total: number }>, err: null });const [{ data, err }, { data: totalData }] = await Promise.all([@@ -1099,7 +1099,7 @@ export async function getCodingSessionDigest(LIMIT 1`;- const { data, err } = await dataCollector({ query });+ const { data, err } = await intelligenceDataCollector({ query }, "query", auth.dbConfigId);if (err) throw err;const row = (data as Array<Record<string, unknown>> | undefined)?.[0];if (!row) return null;@@ -1181,7 +1181,7 @@ async function countUserSessionsForCohort(HAVING ${USER_EXPR} = '${safeUser}')`;- const { data, err } = await dataCollector({ query });+ const { data, err } = await intelligenceDataCollector({ query }, "query", auth.dbConfigId);if (err) {// On error, fail closed: treat as below-floor for safety.// A spurious 404 is preferable to leaking the digest.@@ -1472,8 +1472,8 @@ export async function getCodingUserDigest(const [{ data: digestData, err: digestErr }, { data: vendorsData }] =await Promise.all([- dataCollector({ query: digestQuery }),- dataCollector({ query: vendorsQuery }),+ intelligenceDataCollector({ query: digestQuery }, "query", auth.dbConfigId),+ intelligenceDataCollector({ query: vendorsQuery }, "query", auth.dbConfigId),]);if (digestErr) throw digestErr;const digestRow = (digestData as CodingUserDigest[] | undefined)?.[0];@@ -1764,13 +1764,13 @@ export async function listCodingUsers(`;const totalPromise = opts.withTotal- ? dataCollector({+ ? intelligenceDataCollector({query: `SELECT toInt64(count()) AS total ${baseSubquery}`,- })+ }, "query", auth.dbConfigId): Promise.resolve({ data: [] as Array<{ total: number }>, err: null });const [{ data, err }, { data: totalData }] = await Promise.all([- dataCollector({ query: dataQuery }),+ intelligenceDataCollector({ query: dataQuery }, "query", auth.dbConfigId),totalPromise,]);if (err) throw err;@@ -1856,7 +1856,7 @@ async function applyCohortFloor<T extends { user: string }>(WHERE user IN (${inList})GROUP BY user`;- const { data, err } = await dataCollector({ query });+ const { data, err } = await intelligenceDataCollector({ query }, "query", auth.dbConfigId);if (err) throw err;const counts = new Map<string, number>();for (const row of (data || []) as { user: string; sessions: number }[]) {@@ -1909,12 +1909,12 @@ export class DisputeError extends Error {// sessionExists checks whether the chat-id rolled-up// session is actually visible. Org scoping is enforced by-// `dataCollector` picking the per-org ClickHouse database (see+// `intelligenceDataCollector` picking the per-org ClickHouse database (see// `whereScope` for the explanation), so we don't need to wrap the// org id in the SQL. We do still constrain to coding-agent span// names so a request can't probe arbitrary trace ids.async function disputeSessionExists(- _auth: CodingAgentAuth,+ auth: CodingAgentAuth,sessionId: string): Promise<boolean> {const sid = escape(sessionId);@@ -1929,7 +1929,7 @@ async function disputeSessionExists()LIMIT 1`;- const { data, err } = await dataCollector({ query });+ const { data, err } = await intelligenceDataCollector({ query }, "query", auth.dbConfigId);if (err) {// Treat lookup failure as "not present" — better to reject a// dispute than to accept one that points at a non-existent@@ -1961,7 +1961,7 @@ async function disputeAlreadyExists(AND status = 'open'LIMIT 1`;- const { data, err } = await dataCollector({ query });+ const { data, err } = await intelligenceDataCollector({ query }, "query", auth.dbConfigId);if (err) {// Fail-closed: a lookup failure here means we can't tell if an// open dispute already exists. The cost of a false-positive@@ -1993,7 +1993,7 @@ async function disputeRateLimitExceeded(AND action = 'coding_agent.classification.dispute'AND created_at >= now() - INTERVAL ${DISPUTE_RATE_LIMIT_WINDOW_MIN} MINUTE`;- const { data, err } = await dataCollector({ query });+ const { data, err } = await intelligenceDataCollector({ query }, "query", auth.dbConfigId);if (err) {// Fail-closed: a lookup failure must NOT degrade into "no rate// limit applied" — that turns this guard into a bypass-on-error@@ -2046,7 +2046,7 @@ export async function submitClassificationDispute(const id = crypto.randomUUID();- const { err: disputeErr } = await dataCollector(+ const { err: disputeErr } = await intelligenceDataCollector({table: CODING_AGENT_DISPUTES_TABLE,values: [@@ -2062,7 +2062,7 @@ export async function submitClassificationDispute(},],},- "insert"+ "insert", auth.dbConfigId);if (disputeErr) throw disputeErr;@@ -2083,7 +2083,7 @@ export async function writeAuditLog(auth: CodingAgentAuth,entry: { action: string; subject?: string; payload?: string }): Promise<void> {- const { err } = await dataCollector(+ const { err } = await intelligenceDataCollector({table: CODING_AGENT_AUDIT_LOG_TABLE,values: [@@ -2096,7 +2096,7 @@ export async function writeAuditLog(},],},- "insert"+ "insert", auth.dbConfigId);if (err) {// We don't throw on audit failure — the user-visible action@@ -2106,4 +2106,3 @@ export async function writeAuditLog(console.error("coding_agent.audit_log.insert_failed", err);}}-diff --git a/src/client/src/lib/platform/coding-agents/source.ts b/src/client/src/lib/platform/coding-agents/source.tsnew file mode 100644index 00000000..697232c7--- /dev/null+++ b/src/client/src/lib/platform/coding-agents/source.ts@@ -0,0 +1,107 @@+import {+ getRequestEnvironment,+ OPENLIT_CONTEXT_HEADERS,+} from "@/constants/openlit-context";+import { getDBConfigByUser } from "@/lib/db-config";+import { resolveSignalSource } from "@/lib/telemetry-source";+import getMessage from "@/constants/messages";+import {+ requireCodingAgentAuth,+ type CodingAgentAuth,+} from "./auth";++/**+ * Seed / coding-agent dashboard SQL relies on ClickHouse-only constructs+ * (session rollups, greatest(), coding_agent.* attributes). Never bridge+ * these through Tempo/Jaeger structured inference.+ */+export function isCodingAgentClickHouseSql(sql: string): boolean {+ return /coding_agent\.|claude_code\.|coding.?agent/i.test(sql);+}++/**+ * Resolve the ClickHouse that coding-agents SQL should query for an+ * environment (no HTTP request). Prefer traces when it is ClickHouse;+ * otherwise intelligence. Returns null when neither binding is CH.+ */+export async function resolveCodingAgentsClickHouseDbConfigId(options?: {+ environment?: string | null;+ /** Sessionless callers (cron / materializer) must pass project scope. */+ projectId?: string | null;+ dbConfigId?: string;+}): Promise<string | null> {+ const environment = options?.environment ?? undefined;+ const sourceOptions = {+ environment,+ ...(options?.projectId !== undefined ? { projectId: options.projectId } : {}),+ ...(options?.dbConfigId ? { dbConfigId: options.dbConfigId } : {}),+ };++ const traces = await resolveSignalSource("traces", sourceOptions);+ if (+ traces.hasSource &&+ traces.descriptor.type === "clickhouse" &&+ traces.descriptor.dbConfigId+ ) {+ return traces.descriptor.dbConfigId;+ }++ const intelligence = await resolveSignalSource("intelligence", sourceOptions);+ if (+ intelligence.hasSource &&+ intelligence.descriptor.type === "clickhouse" &&+ intelligence.descriptor.dbConfigId+ ) {+ return intelligence.descriptor.dbConfigId;+ }++ return null;+}++/**+ * Resolve the ClickHouse that coding-agents SQL should query for this request.+ *+ * Prefer env signal routing (traces CH → intelligence CH). The ambient+ * `x-openlit-database-config-id` header from `getData` is the project's+ * current credential vault — often a Tempo-linked config — and must not+ * override signal routing or the Sessions list and user picker diverge.+ *+ * The header is only used as a last-resort project-scoped ClickHouse when+ * neither traces nor intelligence resolves to ClickHouse.+ */+export async function resolveCodingAgentsDatabaseConfigId(+ request: Request+): Promise<string> {+ const environment = getRequestEnvironment(request);+ const routedId = await resolveCodingAgentsClickHouseDbConfigId({+ environment,+ });+ if (routedId) {+ return routedId;+ }++ const requestedId = request.headers.get(+ OPENLIT_CONTEXT_HEADERS.databaseConfigId+ );+ if (requestedId) {+ const available = await getDBConfigByUser();+ const selected = Array.isArray(available)+ ? available.find((database) => database.id === requestedId)+ : undefined;+ if (!selected?.id) {+ throw new Error(getMessage().CODING_AGENTS_DB_NOT_IN_PROJECT);+ }+ return selected.id;+ }++ throw new Error(getMessage().CODING_AGENTS_REQUIRES_CLICKHOUSE);+}++/** Auth + env-routed ClickHouse for coding-agents query helpers. */+export async function requireCodingAgentQueryContext(+ request: Request+): Promise<CodingAgentAuth & { dbConfigId: string }> {+ const auth = await requireCodingAgentAuth();+ const dbConfigId = await resolveCodingAgentsDatabaseConfigId(request);+ return { ...auth, dbConfigId };+}diff --git a/src/client/src/lib/platform/common.ts b/src/client/src/lib/platform/common.tsindex 0840dc20..ea131d90 100644--- a/src/client/src/lib/platform/common.ts+++ b/src/client/src/lib/platform/common.ts@@ -1,19 +1,14 @@import { Pool } from "generic-pool";-import {- getDBConfigByIdInternal,- getDBConfigByIdForUser,- getDBConfigByUser,-} from "../db-config";+import { getDBConfigByIdForBackground, getDBConfigByUser } from "../db-config";import createClickhousePool from "./clickhouse/clickhouse-client";import asaw from "@/utils/asaw";-import { getCurrentUser } from "@/lib/session";import {ClickHouseClient,QueryParams,InsertParams,ExecParams,CommandParams,-} from "@clickhouse/client-common";+} from "@clickhouse/client";import { OPERATION_TYPE } from "@/types/platform";export const OTEL_TRACES_TABLE_NAME = "otel_traces";@@ -40,7 +35,16 @@ export interface MetricParams {sorting?: any;operationType?: OPERATION_TYPE;statusCode?: string[];+ /**+ * Explicit telemetry source id (e.g. a dashboard widget's bound `sourceId`).+ * When set, read facades resolve metadata against this source instead of the+ * project's default routing, so the query builder sees the fields that the+ * widget's actual source can serve.+ */+ sourceId?: string;+ /** Explicit project ClickHouse configuration selected by the request. */databaseConfigId?: string;+ environment?: string;}export type GPU_TYPE_KEY =@@ -59,38 +63,48 @@ export type GPU_TYPE_KEY =export interface GPUMetricParams extends MetricParams { }export type DataCollectorType = { err?: unknown; data?: unknown };-export async function dataCollector(+type CollectorParams = Partial<+ QueryParams &+ InsertParams &+ ExecParams &+ CommandParams & { enable_readonly?: boolean }+>;+type CollectorQueryType = "query" | "command" | "insert" | "exec" | "ping";++async function collectClickHouseData({query,format = "JSONEachRow",table,values,- enable_readonly = false,clickhouse_settings,- }: Partial<QueryParams & InsertParams & ExecParams & CommandParams & { enable_readonly?: boolean }>,- clientQueryType: "query" | "command" | "insert" | "exec" | "ping" = "query",- dbConfigId?: string+ enable_readonly,+ }: CollectorParams,+ clientQueryType: CollectorQueryType,+ dbConfigId: string | undefined,+ readPath: "openplait" | "direct"): Promise<DataCollectorType> {let err, dbConfig;if (dbConfigId) {- const [userErr, user] = await asaw(getCurrentUser());- if (!userErr && user?.id) {- [err, dbConfig] = await asaw(- getDBConfigByIdForUser({ id: dbConfigId, userId: user.id })- );- } else {- [err, dbConfig] = await asaw(getDBConfigByIdInternal({ id: dbConfigId }));- }+ [err, dbConfig] = await asaw(getDBConfigByIdForBackground({ id: dbConfigId }));} else {[err, dbConfig] = await asaw(getDBConfigByUser(true));}if (err) return { err, data: [] };- if (!dbConfig) return { err: "Database config is not accessible", data: [] };let clickhousePool: Pool<ClickHouseClient> | undefined;let client: ClickHouseClient | undefined;try {+ if (clientQueryType === "query" && readPath === "openplait") {+ if (!query) return { err: "No query specified!" };+ const { executeOpenPlaitRead } = await import("./openplait");+ const [queryErr, rows] = await asaw(+ executeOpenPlaitRead({ query, dbConfig })+ );+ return { err: queryErr, data: rows || [] };+ }+clickhousePool = createClickhousePool(dbConfig);const [err, clientClick] = await asaw(clickhousePool.acquire());@@ -105,23 +119,22 @@ export async function dataCollector(if (clientQueryType === "query") {if (!query) return { err: "No query specified!" };- const object: QueryParams = {- query,- format,- }- if (enable_readonly) {- object.clickhouse_settings = {- readonly: "2",- }- }-+ const querySettings = {+ ...(enable_readonly ? { readonly: "1" } : {}),+ ...(clickhouse_settings || {}),+ };[respErr, result] = await asaw(- client.query(object)+ client.query({+ query,+ format,+ ...(Object.keys(querySettings).length+ ? { clickhouse_settings: querySettings }+ : {}),+ } as QueryParams));-- if (result) {- const [err, data] = await asaw(result?.json());- return { err, data };+ if (!respErr && result) {+ const [jsonErr, rows] = await asaw((result as any).json());+ return { err: jsonErr, data: rows || [] };}} else if (clientQueryType === "insert") {if (!table || !values) return { err: "No table specified!" };@@ -177,3 +190,34 @@ export async function dataCollector(if (clickhousePool && client) clickhousePool?.release(client);}}++/**+ * Shared read boundary for product telemetry. Every traces/logs/metrics read,+ * including the built-in ClickHouse source, must enter ClickHouse through+ * OpenPlait here. Mutations retain the native ClickHouse client.+ */+export async function dataCollector(+ params: CollectorParams,+ clientQueryType: CollectorQueryType = "query",+ dbConfigId?: string+): Promise<DataCollectorType> {+ return collectClickHouseData(+ params,+ clientQueryType,+ dbConfigId,+ "openplait"+ );+}++/**+ * Deliberate exception for OpenLIT's internal intelligence/materialization+ * layer. These queries read and write OpenLIT-owned ClickHouse state directly;+ * they are not datasource signal reads and must not pass through OpenPlait.+ */+export async function intelligenceDataCollector(+ params: CollectorParams,+ clientQueryType: CollectorQueryType = "query",+ dbConfigId?: string+): Promise<DataCollectorType> {+ return collectClickHouseData(params, clientQueryType, dbConfigId, "direct");+}diff --git a/src/client/src/lib/platform/connectors/README.md b/src/client/src/lib/platform/connectors/README.mdnew file mode 100644index 00000000..683083ab--- /dev/null+++ b/src/client/src/lib/platform/connectors/README.md@@ -0,0 +1,16 @@+# Connector framework++Connectors are the common integration boundary for OpenLIT. A connector type+belongs to a category (`datasource`, `notification`, `memory`, and so on), and+an instance is a project- or organisation-scoped configuration with secrets+stored in the vault.++Datasource adapters are registered automatically from the existing datasource+registry. New datasource contributors should implement the existing+`DataSourceAdapterFactory`, declare its descriptor and capabilities, and add+adapter tests. The generic connector registry exposes that type to shared+configuration and future consumers without adding vendor-specific UI logic.++Enterprise-only providers can register through the same neutral registry from+the enterprise repository. Availability and plan metadata are represented by+the shared contract; permission and entitlement policy remains outside CE.diff --git a/src/client/src/lib/platform/connectors/datasource/README.md b/src/client/src/lib/platform/connectors/datasource/README.mdnew file mode 100644index 00000000..bd79339e--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/README.md@@ -0,0 +1,117 @@+# Datasource module — add-a-datasource checklist++This layer is **datasource-agnostic**: OpenLIT talks to any external telemetry+vendor (Grafana LGTM today; dash0, Honeycomb, or any OTLP vendor later) through+one adapter + a self-describing descriptor. The shared forms, query planner,+Prisma schema, and every UI surface are driven by that descriptor, so **adding a+new datasource must not require editing shared forms, the planner, the schema,+or any UI** — and needs no Prisma migration (`type` stays a free string).++## What a new datasource needs (and nothing more)++1. **`datasource/<vendor>/adapter.ts`** — a class extending `BaseExternalAdapter`+ that implements the `DataSourceAdapter` methods its `capabilities()`+ advertises. Reuse `otlp-json.ts` for OTLP trace/log parsing, `http/safe-fetch`+ for every outbound call (SSRF-safe, default-deny), and `http/cache` for+ response caching. Methods for unsupported capabilities should throw+ `UnsupportedCapabilityError` (the UI gates them into honest "not supported"+ states automatically).+2. **A `describe(): SourceTypeDescriptor`** on the factory. This is the single+ source of truth for the add/edit form. Set:+ - `type`, `displayName`, `declaredSignals`, `capabilities`, `correlation`+ - `configFields` — reuse `config-fields.ts` helpers (`httpVendorFields`,+ `endpointField`, `httpAuthFields`, `tenantField`) so labels come from+ `constants/messages/en.ts` (no hard-coded strings)+ - `authStyle` (`"none" | "http" | "api-key" | "custom"`), optional `authHelp`+ and `docsUrl` — the form renders credential hints from these, not from+ `type === "<vendor>"` branches+ - optional `maxDataPoints` / `maxLookbackMs` capability hints (adapters that+ omit these still work — the contract stays simple)+3. **`datasource/<vendor>/selector.ts`** (for query languages) — translate the+ shared AI selector (`ai-selector.ts`) into the vendor query language+ (TraceQL, …). Push aggregation down to the vendor; never+ pull raw rows to aggregate in-process.+4. **Register the factory** in `bootstrap.ts` (`VENDOR_FACTORIES`), or ship it+ from the private repo via the `getExternalDataSourceAdapters()` hook for+ EE-only vendors.++## What you must NOT touch++- No new form fields in `data-sources-page.tsx` — it renders `configFields`.+- No Prisma migration or enum — `type` is a free string.+- No per-vendor `switch` anywhere — capability gating + descriptors handle it.+- No `@/ee/**` import from CE; keep vendor strings in `constants/messages/en.ts`.++## Verify extensibility (the dash0 readiness test)++A fictional descriptor-only type with only `configFields` renders end to end,+and the same-shaped adapter test used for `tempo` / `jaeger` passes for any new+OTLP vendor. If your new adapter needs an edit outside `datasource/<vendor>/**`+(plus one line in `bootstrap.ts`), the extensibility contract has regressed —+fix the shared layer, not the vendor.++## Performance contract (Grafana parity)++- **Push-down aggregation**: counts/rates/histograms run in the vendor, not here.+- **Pixel-bounded downsampling**: honor `OpenLITQuery.maxDataPoints`; compute+ `interval/step` via `downsample.ts` (Grafana math) and clamp point counts.+- **Instant vs range**: single-value/table views use instant queries, series use+ range queries.+- **Budgets**: every read passes through `clampQueryBudget` (`maxRows`,+ `maxRangeMs`, `maxLookbackMs`) so no query can trigger an unbounded scan.+- **Resilience**: reads use `concurrencyKey` + `retry` + per-query `timeoutMs`,+ and tag heavy queries with `X-Query-Tags` for ops guardrails.++## OpenPlait-backed ClickHouse reads++The built-in ClickHouse connector uses the generic OpenPlait runtime and+`@openplait/adapter-clickhouse` at `lib/platform/openplait`. The shared+`dataCollector` routes product read-only ClickHouse queries through that+boundary. The traces/logs/metrics facades never branch around the resolved+adapter for built-in ClickHouse. Inserts, migrations, commands, and other+mutations intentionally keep the existing ClickHouse client path.++OpenLIT's internal intelligence layer is a deliberate exception. Otter,+AI-analysis/coding-agent materialization, and telemetry rollup tables use+`intelligenceDataCollector`, which talks to OpenLIT's own ClickHouse state+directly and is not a routed datasource signal read.++## Signal-read debugging map++Use these exact files as breakpoints, in order:++| Signal | HTTP entry | Product facade |+| --- | --- | --- |+| Traces | `src/app/api/metrics/request/route.ts` (served as `/api/telemetry/request` by `src/app/api/telemetry/[...segments]/route.ts`) | `src/lib/platform/traces/read.ts` |+| Logs | `src/app/api/telemetry/logs/route.ts` | `src/lib/platform/logs/read.ts` |+| Metrics | `src/app/api/telemetry/metrics/route.ts` | `src/lib/platform/metrics/read.ts` |+| Summaries | `src/app/api/telemetry/summary/[signal]/route.ts` | the same signal facade above |++All three then enter:++1. `src/lib/platform/connectors/datasource/facade.ts` — resolves the current+ project/environment signal binding.+2. `src/lib/telemetry-source.ts` — constructs the selected adapter.+3. `src/lib/platform/connectors/datasource/clickhouse/adapter.ts` for built-in+ ClickHouse, or the selected external adapter (for example+ `grafana/tempo.ts`).+4. Built-in ClickHouse reads enter `src/lib/platform/common.ts::dataCollector`+ and finally `src/lib/platform/openplait/index.ts::executeOpenPlaitRead`.++Interactive signal reads can use the short-lived, source-scoped in-memory+cache in `datasource/http/cache.ts`; Tempo also keeps a bounded in-memory span+index for detail navigation. These caches do not persist datasource rows.+Persisted rollup/hot-cache tables belong to the internal intelligence layer and+are not read by the interactive trace facade.++The OpenPlait runtime registry is generic and keyed by datasource+configuration. ClickHouse reads use the shared runtime, while the Tempo bridge+uses `@openplait/adapter-tempo` for bounded TraceQL search, trace detail+normalization, tag discovery, build/version inspection, and TraceQL metrics.+Unknown Tempo versions use conservative syntax; `most_recent` is enabled only+after a 2.8+ health probe or an explicit setting. Metrics windows are chunked+separately from trace-search windows because Tempo applies different limits.+OpenLIT retains its SSRF-safe transport, credential resolution, caching, and+product-specific aggregation layer. External trace resolution fails closed:+an unavailable Tempo source is never interpreted as permission to read the+built-in ClickHouse `otel_traces` table.diff --git a/src/client/src/lib/platform/connectors/datasource/ai-selector.ts b/src/client/src/lib/platform/connectors/datasource/ai-selector.tsnew file mode 100644index 00000000..cef00842--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/ai-selector.ts@@ -0,0 +1,250 @@+/**+ * Unified AI telemetry selector (CE).+ *+ * A customer's external observability backend (Datadog, Tempo, ...) holds all+ * of their telemetry — HTTP, DB, infra — not just AI. This module defines the+ * single, layered predicate that identifies "AI-relevant" telemetry, seeded+ * from the selectors already used across the app (materialize.ts, snapshot.ts,+ * getFilterWhereCondition, CODING_AGENT_SPAN_NAMES, SUPPORTED_EVALUATION_OPERATIONS).+ *+ * The selector is expressed as a vendor-agnostic predicate tree so every+ * adapter can translate it to its native filter language and always push it+ * down to the vendor. The ClickHouse translation lives here as the reference.+ *+ * OpenLIT's own SDKs/CLI/controller self-stamp AI telemetry, which is what+ * makes this filter reliable:+ * - OpenLIT SDK (Python/TS/Go) + controller OBI: resource `telemetry.sdk.name = "openlit"`.+ * - Coding CLI: resource `telemetry.distro.name = "openlit-cli"` + `coding_agent.*`.+ * - Generic GenAI: `gen_ai.*` semconv (operation, model, system/provider, tool).+ * - Native Claude Code: `service.name = "claude-code"` + `session.id`.+ */++import { CODING_AGENT_SPAN_NAMES } from "@/lib/platform/coding-agents/table-details";+import { SUPPORTED_EVALUATION_OPERATIONS } from "@/constants/traces";+import type { AttributeScope } from "./types";++/** Marker attribute keys used by the AI selector. */+export const AI_SELECTOR_MARKERS = {+ telemetrySdkName: "telemetry.sdk.name",+ telemetryDistroName: "telemetry.distro.name",+ openlitSdkValue: "openlit",+ openlitCliValue: "openlit-cli",+ genAiOperation: "gen_ai.operation.name",+ genAiModel: "gen_ai.request.model",+ genAiSystem: "gen_ai.system",+ genAiProvider: "gen_ai.provider.name",+ genAiTool: "gen_ai.tool.name",+ genAiSystemInstructions: "gen_ai.system_instructions",+ codingSessionId: "coding_agent.session.id",+ serviceName: "service.name",+ claudeCodeValue: "claude-code",+ sessionId: "session.id",+ vectorDbOperationValue: "vectordb",+} as const;++/** A single condition in the selector predicate. */+export interface SelectorCondition {+ target: "attribute" | "spanName";+ scope?: AttributeScope;+ key?: string;+ op: "exists" | "eq" | "in";+ value?: string | string[];+}++/** A predicate: all conditions ANDed together. */+export interface SelectorPredicate {+ allOf: SelectorCondition[];+}++/** The full selector: any predicate matching (OR) qualifies a row as AI data. */+export interface AITelemetrySelector {+ anyOf: SelectorPredicate[];+}++const single = (cond: SelectorCondition): SelectorPredicate => ({+ allOf: [cond],+});++/**+ * Build the unified AI telemetry selector. Order is not significant (OR), but+ * cheapest/most-selective identity markers are listed first for readability.+ */+export function buildAITelemetrySelector(): AITelemetrySelector {+ return {+ anyOf: [+ // Tier A — OpenLIT SDK + controller OBI identity.+ single({+ target: "attribute",+ scope: "resource",+ key: AI_SELECTOR_MARKERS.telemetrySdkName,+ op: "eq",+ value: AI_SELECTOR_MARKERS.openlitSdkValue,+ }),+ // Tier C — coding-agent CLI distro identity.+ single({+ target: "attribute",+ scope: "resource",+ key: AI_SELECTOR_MARKERS.telemetryDistroName,+ op: "eq",+ value: AI_SELECTOR_MARKERS.openlitCliValue,+ }),+ // Tier B — generic GenAI semconv (LLM / vectordb / agent frameworks).+ single({+ target: "attribute",+ scope: "span",+ key: AI_SELECTOR_MARKERS.genAiOperation,+ op: "exists",+ }),+ single({+ target: "attribute",+ scope: "span",+ key: AI_SELECTOR_MARKERS.genAiModel,+ op: "exists",+ }),+ single({+ target: "attribute",+ scope: "span",+ key: AI_SELECTOR_MARKERS.genAiSystem,+ op: "exists",+ }),+ single({+ target: "attribute",+ scope: "span",+ key: AI_SELECTOR_MARKERS.genAiProvider,+ op: "exists",+ }),+ single({+ target: "attribute",+ scope: "span",+ key: AI_SELECTOR_MARKERS.genAiTool,+ op: "exists",+ }),+ // Tier C — coding agents (span + resource variants).+ single({+ target: "attribute",+ scope: "span",+ key: AI_SELECTOR_MARKERS.codingSessionId,+ op: "exists",+ }),+ single({+ target: "attribute",+ scope: "resource",+ key: AI_SELECTOR_MARKERS.codingSessionId,+ op: "exists",+ }),+ // Native Claude Code — service.name=claude-code AND session.id present.+ {+ allOf: [+ {+ target: "attribute",+ scope: "resource",+ key: AI_SELECTOR_MARKERS.serviceName,+ op: "eq",+ value: AI_SELECTOR_MARKERS.claudeCodeValue,+ },+ {+ target: "attribute",+ scope: "span",+ key: AI_SELECTOR_MARKERS.sessionId,+ op: "exists",+ },+ ],+ },+ // Coding-agent span names.+ single({+ target: "spanName",+ op: "in",+ value: [...CODING_AGENT_SPAN_NAMES],+ }),+ ],+ };+}++/** ClickHouse string escaping for selector values. */+function escapeCH(value: string): string {+ return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'");+}++function chScopeColumn(scope: AttributeScope | undefined): string {+ switch (scope) {+ case "resource":+ return "ResourceAttributes";+ case "log":+ return "LogAttributes";+ case "metric":+ return "Attributes";+ case "span":+ default:+ return "SpanAttributes";+ }+}++/** Translate a single condition to a ClickHouse boolean expression. */+function conditionToClickHouse(cond: SelectorCondition): string {+ if (cond.target === "spanName") {+ if (cond.op === "in" && Array.isArray(cond.value)) {+ const list = cond.value.map((v) => `'${escapeCH(v)}'`).join(", ");+ return `SpanName IN (${list})`;+ }+ if (cond.op === "eq" && typeof cond.value === "string") {+ return `SpanName = '${escapeCH(cond.value)}'`;+ }+ throw new Error(`Unsupported spanName op: ${cond.op}`);+ }++ const col = `${chScopeColumn(cond.scope)}['${escapeCH(cond.key || "")}']`;+ switch (cond.op) {+ case "exists":+ return `notEmpty(${col})`;+ case "eq":+ return `${col} = '${escapeCH(String(cond.value ?? ""))}'`;+ case "in": {+ const values = Array.isArray(cond.value) ? cond.value : [cond.value];+ const list = values+ .map((v) => `'${escapeCH(String(v ?? ""))}'`)+ .join(", ");+ return `${col} IN (${list})`;+ }+ default:+ throw new Error(`Unsupported attribute op: ${cond.op}`);+ }+}++/**+ * Translate the AI selector into a ClickHouse WHERE fragment (no leading+ * WHERE). Returns a parenthesized OR of AND-groups.+ */+export function aiSelectorToClickHouse(+ selector: AITelemetrySelector = buildAITelemetrySelector()+): string {+ const groups = selector.anyOf.map((predicate) => {+ const parts = predicate.allOf.map(conditionToClickHouse);+ return parts.length === 1 ? parts[0] : `(${parts.join(" AND ")})`;+ });+ return `(${groups.join(" OR ")})`;+}++/**+ * Sub-selector: operation-type split used by dashboards/telemetry.+ * `vectordb` isolates vector DB spans; `llm` excludes them.+ */+export function operationTypeClauseToClickHouse(+ operationType: "llm" | "vectordb"+): string {+ const col = `SpanAttributes['${AI_SELECTOR_MARKERS.genAiOperation}']`;+ return operationType === "vectordb"+ ? `${col} = '${AI_SELECTOR_MARKERS.vectorDbOperationValue}'`+ : `${col} != '${AI_SELECTOR_MARKERS.vectorDbOperationValue}'`;+}++/**+ * Sub-selector: the eval-candidate subset. Matches spans whose operation is in+ * the supported evaluation operations (currently `chat`).+ */+export function evalOperationClauseToClickHouse(): string {+ const col = `SpanAttributes['${AI_SELECTOR_MARKERS.genAiOperation}']`;+ const list = SUPPORTED_EVALUATION_OPERATIONS.map(+ (op) => `'${escapeCH(op)}'`+ ).join(", ");+ return `${col} IN (${list})`;+}diff --git a/src/client/src/lib/platform/connectors/datasource/base-adapter.ts b/src/client/src/lib/platform/connectors/datasource/base-adapter.tsnew file mode 100644index 00000000..f3310e19--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/base-adapter.ts@@ -0,0 +1,121 @@+/**+ * Base class for external DataSourceAdapters.+ *+ * Every method defaults to throwing `UnsupportedCapabilityError`, so a vendor+ * adapter only implements what its backend actually supports and any surface+ * that reaches an unsupported capability gets a first-class, explained error+ * (never a silent wrong result). `capabilities()` is the single source of+ * truth the UI reads to gate features.+ */++import {+ UnsupportedCapabilityError,+ type AISignalValidation,+ type DataFrame,+ type DataSourceAdapter,+ type DiscoveredService,+ type HealthCheckResult,+ type NormalizedLog,+ type NormalizedMetricPoint,+ type NormalizedSpan,+ type OpenLITQuery,+ type QueryTimeRange,+ type ServiceRollup,+ type Signal,+ type SourceCapabilities,+ type TelemetrySourceDescriptor,+} from "./types";++export abstract class BaseExternalAdapter implements DataSourceAdapter {+ abstract readonly type: string;+ protected readonly descriptor: TelemetrySourceDescriptor;++ constructor(descriptor: TelemetrySourceDescriptor) {+ this.descriptor = descriptor;+ }++ /** Used by L1 sample cache so summary/grouped/config share one download. */+ get sampleCacheKey(): string {+ return this.descriptor.id;+ }++ abstract capabilities(): SourceCapabilities;+ abstract healthCheck(): Promise<HealthCheckResult>;++ protected unsupported(capability: string): never {+ throw new UnsupportedCapabilityError(this.type, capability);+ }++ async validateAISignal(_window: QueryTimeRange): Promise<AISignalValidation> {+ this.unsupported("validateAISignal");+ }++ // Traces+ async listSpans(_q: OpenLITQuery): Promise<DataFrame<NormalizedSpan>> {+ this.unsupported("listSpans");+ }+ async getSpan(_spanId: string): Promise<NormalizedSpan | null> {+ this.unsupported("getSpan");+ }+ async getTraceSpans(_traceId: string): Promise<NormalizedSpan[]> {+ this.unsupported("getTraceSpans");+ }+ async getSpansBySession(_sessionId: string): Promise<NormalizedSpan[]> {+ this.unsupported("getSpansBySession");+ }+ async aggregateSpans(_q: OpenLITQuery): Promise<DataFrame> {+ this.unsupported("aggregateSpans");+ }+ async spanTimeSeries(_q: OpenLITQuery): Promise<DataFrame> {+ this.unsupported("spanTimeSeries");+ }+ async distinctValues(_key: string, _q: OpenLITQuery): Promise<string[]> {+ this.unsupported("distinctValues");+ }+ async attributeKeys(_signal: Signal, _w: QueryTimeRange): Promise<string[]> {+ this.unsupported("attributeKeys");+ }++ // Logs+ async listLogs(_q: OpenLITQuery): Promise<DataFrame<NormalizedLog>> {+ this.unsupported("listLogs");+ }+ async getLog(+ _logId: string,+ _opts?: { aroundTimestamp?: string | Date; timeRange?: QueryTimeRange }+ ): Promise<NormalizedLog | null> {+ this.unsupported("getLog");+ }+ async logTimeSeries(_q: OpenLITQuery): Promise<DataFrame> {+ this.unsupported("logTimeSeries");+ }++ // Metrics+ async listMetricSeries(+ _q: OpenLITQuery+ ): Promise<DataFrame<NormalizedMetricPoint>> {+ this.unsupported("listMetricSeries");+ }+ async metricTimeSeries(+ _q: OpenLITQuery+ ): Promise<DataFrame<NormalizedMetricPoint>> {+ this.unsupported("metricTimeSeries");+ }+ async metricNames(_w: QueryTimeRange): Promise<string[]> {+ this.unsupported("metricNames");+ }++ // Discovery+ async discoverServices(_w: QueryTimeRange): Promise<DiscoveredService[]> {+ this.unsupported("discoverServices");+ }+ async aggregateByService(_w: QueryTimeRange): Promise<ServiceRollup[]> {+ this.unsupported("aggregateByService");+ }+ async sampleTracesForGraph(+ _q: OpenLITQuery,+ _maxTraces: number+ ): Promise<NormalizedSpan[]> {+ this.unsupported("sampleTracesForGraph");+ }+}diff --git a/src/client/src/lib/platform/connectors/datasource/bootstrap.ts b/src/client/src/lib/platform/connectors/datasource/bootstrap.tsnew file mode 100644index 00000000..f4414e9c--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/bootstrap.ts@@ -0,0 +1,51 @@+/**+ * Adapter bootstrap for the CE datasource connectors: ClickHouse, Tempo, Loki,+ * Prometheus, and Jaeger. Enterprise builds may contribute additional factories+ * through the neutral extension hook.+ */++import { hasAdapterFactory, registerAdapterFactory } from "./registry";+import { clickHouseAdapterFactory } from "./clickhouse/adapter";+import { tempoAdapterFactory } from "./grafana/tempo";+import { lokiAdapterFactory } from "./grafana/loki";+import { prometheusAdapterFactory } from "./prometheus/adapter";+import { jaegerAdapterFactory } from "./jaeger/adapter";+import { getExternalDataSourceAdapters } from "@/lib/platform/connectors/datasource/enterprise";+import { registerDatasourceConnectorTypes } from "@/lib/platform/connectors/datasource";++const VENDOR_FACTORIES = [+ tempoAdapterFactory,+ lokiAdapterFactory,+ prometheusAdapterFactory,+ jaegerAdapterFactory,+];++let registered = false;++/**+ * Register CE (+ external) adapter factories.+ *+ * Re-registers when the registry Map is empty even if this module previously+ * marked itself registered. Next.js HMR can reload `registry.ts` into a fresh+ * empty Map while leaving this module's `registered` flag set, which would+ * otherwise make create/list reject known types such as `loki` with 400.+ */+export function ensureAdaptersRegistered(): void {+ if (registered && hasAdapterFactory("clickhouse") && hasAdapterFactory("loki")) {+ return;+ }+ registered = true;+ registerAdapterFactory(clickHouseAdapterFactory);+ for (const factory of VENDOR_FACTORIES) {+ registerAdapterFactory(factory);+ }+ for (const factory of getExternalDataSourceAdapters()) {+ registerAdapterFactory(factory);+ }+ registerDatasourceConnectorTypes();+}++/** Test-only: allow re-registration. */+export function __resetBootstrapForTests(): void {+ registered = false;+}diff --git a/src/client/src/lib/platform/connectors/datasource/clickhouse/adapter.ts b/src/client/src/lib/platform/connectors/datasource/clickhouse/adapter.tsnew file mode 100644index 00000000..f37a89c2--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/clickhouse/adapter.ts@@ -0,0 +1,633 @@+/**+ * ClickHouse reference DataSourceAdapter (CE).+ *+ * This is the default/reference implementation of the pluggable telemetry+ * contract. It reads OpenLIT's own `otel_traces|logs|metrics` tables via the+ * existing `dataCollector` choke point and the already-tested query builders,+ * normalizing results into the vendor-agnostic contract shapes. External+ * vendor adapters (EE) implement the same contract against their APIs.+ *+ * It advertises the full ClickHouse capability set (trace tree, span events,+ * server-side aggregation, span mutation, distinct values, cross-trace coding+ * sessions, raw queries), so surfaces never degrade on the native path.+ */++import { dataCollector } from "@/lib/platform/common";+import {+ OTEL_TRACES_TABLE_NAME,+} from "@/lib/platform/common";+import {+ getRequests,+ getRequestViaSpanId,+ getAttributeKeys,+} from "@/lib/platform/request";+import {+ getLogs,+ getLogByRowId,+ getMetrics,+ getMetricsConfig,+ getMetricDetail,+ getLogsConfig,+ getSignalSummary,+ getLogAttributeKeys,+ getMetricAttributeKeys,+} from "@/lib/platform/observability";+import { getFilterWhereCondition } from "@/helpers/server/platform";+import {+ aiSelectorToClickHouse,+ AI_SELECTOR_MARKERS,+} from "../ai-selector";+import type {+ AISignalValidation,+ DataFrame,+ DataSourceAdapter,+ DiscoveredService,+ HealthCheckResult,+ NormalizedLog,+ NormalizedMetricPoint,+ NormalizedSpan,+ OpenLITQuery,+ QueryTimeRange,+ ServiceRollup,+ Signal,+ SourceCapabilities,+ SourceTypeDescriptor,+ TelemetrySourceDescriptor,+} from "../types";+import { normalizeLogRow, normalizeSpanRow } from "./normalize";+import { toMetricParams } from "./query-map";++function escapeCH(value: string): string {+ return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'");+}++const SAFE_CH_IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/;+const ALLOWED_CH_FIELDS = new Set([+ "SpanId",+ "TraceId",+ "ParentSpanId",+ "SpanName",+ "ServiceName",+ "Duration",+ "Timestamp",+ "StatusCode",+ "StatusMessage",+ "Kind",+ "TraceType",+]);++function safeAlias(value: unknown, fallback: string): string {+ const alias = String(value || fallback);+ if (!SAFE_CH_IDENTIFIER.test(alias)) {+ throw new Error("Invalid dashboard aggregation alias");+ }+ return alias;+}++function assertLegacyReadSucceeded(result: { err?: unknown }): void {+ if (!result.err) return;+ throw result.err instanceof Error ? result.err : new Error(String(result.err));+}++/** Format a Date to a ClickHouse UTC datetime literal. */+function chDateTime(date: Date): string {+ return date.toISOString().replace("T", " ").replace("Z", "").split(".")[0];+}++function timeRangeClause(range: QueryTimeRange, column = "Timestamp"): string {+ return `${column} >= '${chDateTime(range.start)}' AND ${column} <= '${chDateTime(+ range.end+ )}'`;+}++const AGG_FN_MAP: Record<string, (field: string) => string> = {+ count: () => "count()",+ sum: (f) => `sum(toFloat64OrZero(${f}))`,+ avg: (f) => `avg(toFloat64OrZero(${f}))`,+ min: (f) => `min(toFloat64OrZero(${f}))`,+ max: (f) => `max(toFloat64OrZero(${f}))`,+ p50: (f) => `quantile(0.5)(toFloat64OrZero(${f}))`,+ p90: (f) => `quantile(0.9)(toFloat64OrZero(${f}))`,+ p95: (f) => `quantile(0.95)(toFloat64OrZero(${f}))`,+ p99: (f) => `quantile(0.99)(toFloat64OrZero(${f}))`,+ cardinality: (f) => `uniqExact(${f})`,+};++/** Resolve a group-by / field key to a ClickHouse expression. */+function fieldToExpr(field: string): string {+ const scoped = field.match(/^(span|resource):(.+)$/);+ if (scoped) {+ const [, scope, key] = scoped;+ return `${scope === "resource" ? "ResourceAttributes" : "SpanAttributes"}['${escapeCH(key)}']`;+ }+ if (field === "duration") return "Duration / 1000000000";+ if (field === "service.name") {+ return "ResourceAttributes['service.name']";+ }+ if (field === "deployment.environment") {+ return "ResourceAttributes['deployment.environment']";+ }+ if (field === "SpanName" || field === "ServiceName" || field === "Duration") {+ return field;+ }+ // Treat dotted keys as span attributes by default.+ if (field.includes(".")) {+ return `SpanAttributes['${escapeCH(field)}']`;+ }+ if (!ALLOWED_CH_FIELDS.has(field)) {+ throw new Error(`Unsupported ClickHouse field: ${field}`);+ }+ return field;+}++export class ClickHouseAdapter implements DataSourceAdapter {+ readonly type = "clickhouse";+ readonly sampleCacheKey: string;+ private readonly descriptor: TelemetrySourceDescriptor;++ constructor(descriptor: TelemetrySourceDescriptor) {+ this.descriptor = descriptor;+ this.sampleCacheKey = descriptor.id;+ }++ private get dbConfigId(): string | undefined {+ return this.descriptor.dbConfigId;+ }++ capabilities(): SourceCapabilities {+ return {+ signals: ["traces", "logs", "metrics"],+ traceTree: true,+ spanEvents: true,+ serverAggregation: true,+ spanMutation: true,+ distinctValues: true,+ crossTraceSession: true,+ rawQuery: true,+ };+ }++ async healthCheck(): Promise<HealthCheckResult> {+ const start = Date.now();+ const { err, data } = await dataCollector({}, "ping", this.dbConfigId);+ return {+ ok: !err && !!data,+ message: err ? String(err) : undefined,+ latencyMs: Date.now() - start,+ };+ }++ async validateAISignal(window: QueryTimeRange): Promise<AISignalValidation> {+ const query = `SELECT CAST(COUNT(*) AS INTEGER) AS c+ FROM ${OTEL_TRACES_TABLE_NAME}+ WHERE ${timeRangeClause(window)} AND ${aiSelectorToClickHouse()}`;+ const { data, err } = await dataCollector({ query }, "query", this.dbConfigId);+ const sampleCount = (data as { c?: number }[])?.[0]?.c ?? 0;+ return {+ ok: !err && sampleCount > 0,+ sampleCount,+ missingAttributes: [],+ message: err ? String(err) : undefined,+ };+ }++ // ---- Traces -----------------------------------------------------------++ async listSpans(query: OpenLITQuery): Promise<DataFrame<NormalizedSpan>> {+ const start = Date.now();+ const result = await getRequests(toMetricParams(query, this.dbConfigId));+ assertLegacyReadSucceeded(result);+ const rows = ((result.records as Record<string, unknown>[]) || []).map(+ normalizeSpanRow+ );+ return {+ fields: spanFields(),+ rows,+ meta: {+ latencyMs: Date.now() - start,+ rowsScanned: Number(result.total) || rows.length,+ },+ };+ }++ async getSpan(spanId: string): Promise<NormalizedSpan | null> {+ const result = await getRequestViaSpanId(spanId, this.dbConfigId);+ assertLegacyReadSucceeded(result);+ const { record } = result;+ return record ? normalizeSpanRow(record as Record<string, unknown>) : null;+ }++ async getTraceSpans(traceId: string): Promise<NormalizedSpan[]> {+ const query = `SELECT * FROM ${OTEL_TRACES_TABLE_NAME}+ WHERE TraceId = '${escapeCH(traceId)}'+ ORDER BY Timestamp ASC+ LIMIT 5000`;+ const result = await dataCollector({ query }, "query", this.dbConfigId);+ assertLegacyReadSucceeded(result);+ const { data } = result;+ return ((data as Record<string, unknown>[]) || []).map(normalizeSpanRow);+ }++ async getSpansBySession(sessionId: string): Promise<NormalizedSpan[]> {+ const s = escapeCH(sessionId);+ const query = `SELECT * FROM ${OTEL_TRACES_TABLE_NAME}+ WHERE SpanAttributes['${AI_SELECTOR_MARKERS.codingSessionId}'] = '${s}'+ OR ResourceAttributes['coding_agent.agent.parent_id'] = '${s}'+ OR SpanAttributes['coding_agent.agent.parent_id'] = '${s}'+ ORDER BY Timestamp ASC+ LIMIT 5000`;+ const result = await dataCollector({ query }, "query", this.dbConfigId);+ assertLegacyReadSucceeded(result);+ const { data } = result;+ return ((data as Record<string, unknown>[]) || []).map(normalizeSpanRow);+ }++ async aggregateSpans(query: OpenLITQuery): Promise<DataFrame> {+ const start = Date.now();+ const params = toMetricParams(query, this.dbConfigId);+ const groupExprs = (query.groupBy || []).map(+ (g, i) => `${fieldToExpr(g)} AS g${i}`+ );+ const aggExprs = (query.aggregations || [{ fn: "count" as const }]).map(+ (a, i) => {+ const fn = AGG_FN_MAP[a.fn] || AGG_FN_MAP.count;+ const field = a.field ? fieldToExpr(a.field) : "";+ return `${fn(field)} AS ${safeAlias(a.as, `agg${i}`)}`;+ }+ );+ const selects = [...groupExprs, ...aggExprs].join(", ");+ const groupBy = query.groupBy?.length+ ? `GROUP BY ${query.groupBy.map((_, i) => `g${i}`).join(", ")}`+ : "";+ const where = [+ getFilterWhereCondition(params, true),+ query.aiSelector !== false ? aiSelectorToClickHouse() : "",+ ]+ .filter(Boolean)+ .join(" AND ");+ const sql = `SELECT ${selects} FROM ${OTEL_TRACES_TABLE_NAME}+ WHERE ${where} ${groupBy}+ ${query.limit ? `LIMIT ${Number(query.limit)}` : ""}`;+ const result = await dataCollector({ query: sql }, "query", this.dbConfigId);+ assertLegacyReadSucceeded(result);+ const { data } = result;+ return {+ fields: [],+ rows: (data as Record<string, unknown>[]) || [],+ meta: { latencyMs: Date.now() - start },+ };+ }++ async spanTimeSeries(query: OpenLITQuery): Promise<DataFrame> {+ const start = Date.now();+ const summary = await getSignalSummary(+ toMetricParams(query, this.dbConfigId),+ "traces"+ );+ assertLegacyReadSucceeded(summary);+ return {+ fields: [],+ rows: (summary.buckets as Record<string, unknown>[]) || [],+ meta: { latencyMs: Date.now() - start },+ };+ }++ async distinctValues(key: string, query: OpenLITQuery): Promise<string[]> {+ if (query.signal === "logs") {+ const result = await getLogsConfig(toMetricParams(query, this.dbConfigId));+ assertLegacyReadSucceeded(result);+ const { data } = result;+ const row = (data as Record<string, unknown>[] | undefined)?.[0] || {};+ if (key === "service.name") return (row.services as string[]) || [];+ if (key === "severity") return (row.severities as string[]) || [];+ return [];+ }+ if (query.signal === "metrics") {+ const result = await getMetricsConfig(toMetricParams(query, this.dbConfigId));+ assertLegacyReadSucceeded(result);+ const { data } = result;+ const row = (data as Record<string, unknown>[] | undefined)?.[0] || {};+ if (key === "service.name") return (row.services as string[]) || [];+ if (key === "metric.name" || key === "MetricName") {+ return (row.metricNames as string[]) || [];+ }+ return [];+ }+ const expr = fieldToExpr(key);+ const where = [+ timeRangeClause(query.timeRange),+ query.aiSelector !== false ? aiSelectorToClickHouse() : "",+ ]+ .filter(Boolean)+ .join(" AND ");+ const sql = `SELECT DISTINCT ${expr} AS v FROM ${OTEL_TRACES_TABLE_NAME}+ WHERE ${where} AND notEmpty(toString(${expr}))+ ORDER BY v+ LIMIT 1000`;+ const result = await dataCollector({ query: sql }, "query", this.dbConfigId);+ assertLegacyReadSucceeded(result);+ const { data } = result;+ return ((data as { v?: unknown }[]) || [])+ .map((r) => String(r.v ?? ""))+ .filter(Boolean);+ }++ async attributeKeys(signal: Signal, _window: QueryTimeRange): Promise<string[]> {+ if (signal === "logs") {+ const r = await getLogAttributeKeys(+ paramsForWindow(_window, this.dbConfigId)+ );+ return dedupeKeys(r);+ }+ if (signal === "metrics") {+ const r = await getMetricAttributeKeys(+ paramsForWindow(_window, this.dbConfigId)+ );+ return dedupeKeys(r);+ }+ const r = await getAttributeKeys(paramsForWindow(_window, this.dbConfigId));+ return [+ ...(r.spanAttributeKeys || []),+ ...(r.resourceAttributeKeys || []),+ ];+ }++ // ---- Logs -------------------------------------------------------------++ async listLogs(query: OpenLITQuery): Promise<DataFrame<NormalizedLog>> {+ const start = Date.now();+ const result = await getLogs(toMetricParams(query, this.dbConfigId));+ assertLegacyReadSucceeded(result);+ const rows = ((result.records as Record<string, unknown>[]) || []).map(+ normalizeLogRow+ );+ return {+ fields: [],+ rows,+ meta: { latencyMs: Date.now() - start, rowsScanned: Number(result.total) || rows.length },+ };+ }++ async getLog(logId: string): Promise<NormalizedLog | null> {+ const result = await getLogByRowId(logId, this.dbConfigId);+ assertLegacyReadSucceeded(result);+ const { record } = result;+ return record ? normalizeLogRow(record as Record<string, unknown>) : null;+ }++ async logTimeSeries(query: OpenLITQuery): Promise<DataFrame> {+ const start = Date.now();+ const summary = await getSignalSummary(+ toMetricParams(query, this.dbConfigId),+ "logs"+ );+ assertLegacyReadSucceeded(summary);+ return {+ fields: [],+ rows: (summary.buckets as Record<string, unknown>[]) || [],+ meta: { latencyMs: Date.now() - start },+ };+ }++ // ---- Metrics ----------------------------------------------------------++ async listMetricSeries(+ query: OpenLITQuery+ ): Promise<DataFrame<NormalizedMetricPoint>> {+ const start = Date.now();+ const result = await getMetrics(toMetricParams(query, this.dbConfigId));+ assertLegacyReadSucceeded(result);+ const records =+ (result as { records?: Record<string, unknown>[] }).records ||+ ((result as { data?: Record<string, unknown>[] }).data as+ | Record<string, unknown>[]+ | undefined) ||+ [];+ const rows = records.map((row) => ({+ metricName: String(row.metricName || ""),+ description: row.metricDescription ? String(row.metricDescription) : undefined,+ unit: row.metricUnit ? String(row.metricUnit) : undefined,+ serviceName: row.serviceName ? String(row.serviceName) : undefined,+ timestamp: String(row.lastSeen || ""),+ value: Number(row.latestValue || 0),+ attributes: {},+ resourceAttributes: {},+ }));+ return {+ fields: [],+ rows,+ meta: {+ latencyMs: Date.now() - start,+ rowsScanned: Number((result as { total?: unknown }).total) || rows.length,+ },+ };+ }++ async metricTimeSeries(+ query: OpenLITQuery+ ): Promise<DataFrame<NormalizedMetricPoint>> {+ const metricFilter = (query.filters || []).find(+ (filter) => filter.target === "spanName"+ );+ const metricName = Array.isArray(metricFilter?.value)+ ? metricFilter?.value[0]+ : metricFilter?.value;+ if (metricName) {+ const serviceFilter = (query.filters || []).find(+ (filter) => filter.target === "attribute" && filter.key === "service.name"+ );+ const serviceName = Array.isArray(serviceFilter?.value)+ ? serviceFilter?.value[0]+ : serviceFilter?.value;+ const detail = await getMetricDetail(+ String(metricName),+ undefined,+ serviceName ? String(serviceName) : undefined,+ toMetricParams(query, this.dbConfigId)+ );+ assertLegacyReadSucceeded(detail);+ const rows = ((detail.series || []) as Array<Record<string, unknown>>).map(+ (row) => ({+ metricName: String(metricName),+ serviceName: serviceName ? String(serviceName) : undefined,+ timestamp: String(row.request_time || ""),+ value: Number(row.value || 0),+ attributes: {},+ resourceAttributes: {},+ })+ );+ return { fields: [], rows };+ }+ const summary = await getSignalSummary(+ toMetricParams(query, this.dbConfigId),+ "metrics"+ );+ assertLegacyReadSucceeded(summary);+ return {+ fields: [],+ rows: summary.buckets as unknown as NormalizedMetricPoint[],+ };+ }++ async metricNames(window: QueryTimeRange): Promise<string[]> {+ const result = await getMetricsConfig(+ paramsForWindow(window, this.dbConfigId)+ );+ assertLegacyReadSucceeded(result);+ const { data } = result;+ const cfg = (data as { metricNames?: string[] }[])?.[0];+ return cfg?.metricNames || [];+ }++ // ---- Discovery --------------------------------------------------------++ async discoverServices(window: QueryTimeRange): Promise<DiscoveredService[]> {+ const sql = `SELECT+ ServiceName AS serviceName,+ any(ResourceAttributes['deployment.environment']) AS environment,+ any(ResourceAttributes['k8s.cluster.name']) AS clusterId,+ any(ResourceAttributes['telemetry.sdk.name']) AS sdkName,+ any(ResourceAttributes['telemetry.sdk.language']) AS sdkLanguage,+ any(ResourceAttributes['telemetry.sdk.version']) AS sdkVersion,+ min(Timestamp) AS firstSeen,+ max(Timestamp) AS lastSeen+ FROM ${OTEL_TRACES_TABLE_NAME}+ WHERE ${timeRangeClause(window)} AND ${aiSelectorToClickHouse()}+ GROUP BY ServiceName`;+ const result = await dataCollector({ query: sql }, "query", this.dbConfigId);+ assertLegacyReadSucceeded(result);+ const { data } = result;+ return ((data as Record<string, unknown>[]) || []).map((r) => ({+ serviceName: String(r.serviceName ?? ""),+ environment: String(r.environment ?? ""),+ clusterId: String(r.clusterId ?? ""),+ sdkName: r.sdkName ? String(r.sdkName) : undefined,+ sdkLanguage: r.sdkLanguage ? String(r.sdkLanguage) : undefined,+ sdkVersion: r.sdkVersion ? String(r.sdkVersion) : undefined,+ firstSeen: r.firstSeen ? String(r.firstSeen) : undefined,+ lastSeen: r.lastSeen ? String(r.lastSeen) : undefined,+ }));+ }++ async aggregateByService(window: QueryTimeRange): Promise<ServiceRollup[]> {+ const sql = `SELECT+ ServiceName AS serviceName,+ any(ResourceAttributes['deployment.environment']) AS environment,+ any(ResourceAttributes['k8s.cluster.name']) AS clusterId,+ CAST(COUNT(*) AS INTEGER) AS requestCount,+ groupUniqArray(SpanAttributes['gen_ai.request.model']) AS models,+ groupUniqArray(SpanAttributes['gen_ai.system']) AS providers+ FROM ${OTEL_TRACES_TABLE_NAME}+ WHERE ${timeRangeClause(window)} AND ${aiSelectorToClickHouse()}+ GROUP BY ServiceName`;+ const result = await dataCollector({ query: sql }, "query", this.dbConfigId);+ assertLegacyReadSucceeded(result);+ const { data } = result;+ return ((data as Record<string, unknown>[]) || []).map((r) => ({+ serviceName: String(r.serviceName ?? ""),+ environment: String(r.environment ?? ""),+ clusterId: String(r.clusterId ?? ""),+ requestCount: Number(r.requestCount) || 0,+ models: ((r.models as string[]) || []).filter(Boolean),+ providers: ((r.providers as string[]) || []).filter(Boolean),+ }));+ }++ async sampleTracesForGraph(+ query: OpenLITQuery,+ maxTraces: number+ ): Promise<NormalizedSpan[]> {+ const params = toMetricParams(query, this.dbConfigId);+ const where = [+ getFilterWhereCondition(params, true),+ query.aiSelector !== false ? aiSelectorToClickHouse() : "",+ ]+ .filter(Boolean)+ .join(" AND ");+ const traceIdSql = `SELECT DISTINCT TraceId FROM ${OTEL_TRACES_TABLE_NAME}+ WHERE ${where}+ ORDER BY TraceId+ LIMIT ${Number(maxTraces) || 100}`;+ const idResult = await dataCollector(+ { query: traceIdSql },+ "query",+ this.dbConfigId+ );+ assertLegacyReadSucceeded(idResult);+ const idRows = idResult.data;+ const traceIds = ((idRows as { TraceId?: string }[]) || [])+ .map((r) => r.TraceId)+ .filter((id): id is string => !!id)+ .map((id) => `'${escapeCH(id)}'`);+ if (traceIds.length === 0) return [];+ const spanSql = `SELECT * FROM ${OTEL_TRACES_TABLE_NAME}+ WHERE TraceId IN (${traceIds.join(", ")})+ ORDER BY Timestamp ASC+ LIMIT 50000`;+ const result = await dataCollector({ query: spanSql }, "query", this.dbConfigId);+ assertLegacyReadSucceeded(result);+ const { data } = result;+ return ((data as Record<string, unknown>[]) || []).map(normalizeSpanRow);+ }+}++function spanFields() {+ return [+ { name: "traceId", type: "string" as const },+ { name: "spanId", type: "string" as const },+ { name: "name", type: "string" as const },+ { name: "timestamp", type: "time" as const },+ { name: "durationNs", type: "number" as const },+ ];+}++function paramsForWindow(window: QueryTimeRange, databaseConfigId?: string) {+ return {+ timeLimit: {+ start: window.start.toISOString(),+ end: window.end.toISOString(),+ type: "CUSTOM",+ },+ databaseConfigId,+ } as unknown as Parameters<typeof getAttributeKeys>[0];+}++function dedupeKeys(result: unknown): string[] {+ const r = result as Record<string, unknown>;+ const out = new Set<string>();+ for (const value of Object.values(r)) {+ if (Array.isArray(value)) {+ for (const v of value) if (typeof v === "string") out.add(v);+ }+ }+ return Array.from(out);+}++/** Factory used by the registry. */+export const clickHouseAdapterFactory = {+ type: "clickhouse",+ create: (descriptor: TelemetrySourceDescriptor): DataSourceAdapter =>+ new ClickHouseAdapter(descriptor),+ describe: (): SourceTypeDescriptor => ({+ type: "clickhouse",+ displayName: "ClickHouse",+ declaredSignals: ["traces", "logs", "metrics"],+ capabilities: {+ traceTree: true,+ spanEvents: true,+ serverAggregation: true,+ spanMutation: true,+ distinctValues: true,+ crossTraceSession: true,+ rawQuery: true,+ },+ correlation: {+ crossSignal: true,+ keys: ["traceId", "spanId", "service", "session"],+ },+ // Built-in source: connection comes from the project's DB config, so the+ // external add/edit form needs no config fields.+ configFields: [],+ authStyle: "none",+ }),+};diff --git a/src/client/src/lib/platform/connectors/datasource/clickhouse/normalize.ts b/src/client/src/lib/platform/connectors/datasource/clickhouse/normalize.tsnew file mode 100644index 00000000..ecfe81a9--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/clickhouse/normalize.ts@@ -0,0 +1,303 @@+/**+ * Normalizers: raw ClickHouse `otel_*` rows -> contract shapes.+ *+ * These convert the ClickHouse JSONEachRow representation (column names,+ * nested Events arrays, Map columns) into the vendor-agnostic+ * `NormalizedSpan` / `NormalizedLog` / `NormalizedMetricPoint` shapes so every+ * external adapter produces the same surface-facing structure.+ */++import type {+ NormalizedLog,+ NormalizedMetricPoint,+ NormalizedSpan,+ NormalizedSpanEvent,+} from "../types";++/**+ * Deterministic 53-bit hash for a set of fields. Built-in ClickHouse rows carry+ * a `cityHash64` rowId; external rows have no such id, so we synthesize a stable+ * one from the same identifying fields for React keys and detail lookups.+ */+function stableRowId(parts: string[]): string {+ let h1 = 0xdeadbeef;+ let h2 = 0x41c6ce57;+ const str = parts.join("\u0000");+ for (let i = 0; i < str.length; i++) {+ const ch = str.charCodeAt(i);+ h1 = Math.imul(h1 ^ ch, 2654435761);+ h2 = Math.imul(h2 ^ ch, 1597334677);+ }+ h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);+ h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);+ const hash = 4294967296 * (2097151 & h2) + (h1 >>> 0);+ return String(hash);+}++/** Stable external log row id — must match `denormalizeLogToClickHouseRow`. */+export function logStableRowId(log: Pick<+ NormalizedLog,+ "timestamp" | "traceId" | "spanId" | "severityText" | "body"+>): string {+ return stableRowId([+ log.timestamp,+ log.traceId || "",+ log.spanId || "",+ log.severityText || "",+ log.body,+ ]);+}++function asString(value: unknown): string {+ if (value === null || value === undefined) return "";+ return String(value);+}++function asNumber(value: unknown): number {+ const n = Number(value);+ return Number.isFinite(n) ? n : 0;+}++function asMap(value: unknown): Record<string, string> {+ if (!value || typeof value !== "object") return {};+ const out: Record<string, string> = {};+ for (const [k, v] of Object.entries(value as Record<string, unknown>)) {+ out[k] = asString(v);+ }+ return out;+}++/**+ * Normalize span events. ClickHouse can return the Nested `Events` column+ * either as parallel arrays (`Events.Timestamp`, `Events.Name`,+ * `Events.Attributes`) or as an array of objects, depending on the query. We+ * handle both.+ */+function normalizeEvents(row: Record<string, unknown>): NormalizedSpanEvent[] {+ // Array-of-objects form.+ if (Array.isArray(row.Events)) {+ return (row.Events as unknown[]).map((ev) => {+ const e = (ev || {}) as Record<string, unknown>;+ return {+ name: asString(e.Name),+ timestamp: e.Timestamp ? asString(e.Timestamp) : undefined,+ attributes: asMap(e.Attributes),+ };+ });+ }+ // Parallel-arrays form.+ const names = row["Events.Name"];+ if (Array.isArray(names)) {+ const timestamps = (row["Events.Timestamp"] as unknown[]) || [];+ const attributes = (row["Events.Attributes"] as unknown[]) || [];+ return (names as unknown[]).map((name, i) => ({+ name: asString(name),+ timestamp: timestamps[i] ? asString(timestamps[i]) : undefined,+ attributes: asMap(attributes[i]),+ }));+ }+ return [];+}++/**+ * Convert a NormalizedSpan back into the ClickHouse-shaped row the Telemetry+ * UI already understands (`TraceId`, `SpanAttributes`, …). External adapters+ * return NormalizedSpan; the traces facade denormalizes so React pages do not+ * need a parallel code path.+ */+export function denormalizeSpanToTraceRow(+ span: NormalizedSpan+): Record<string, unknown> {+ const events = (span.events || []).map((ev) => ({+ Name: ev.name,+ Timestamp: ev.timestamp || "",+ Attributes: ev.attributes || {},+ }));+ const spanAttributes = { ...span.spanAttributes };+ if (span.cost !== undefined && spanAttributes["gen_ai.usage.cost"] === undefined) {+ spanAttributes["gen_ai.usage.cost"] = String(span.cost);+ }+ return {+ TraceId: span.traceId,+ SpanId: span.spanId,+ ParentSpanId: span.parentSpanId || "",+ SpanName: span.name,+ ServiceName: span.serviceName,+ Timestamp: span.timestamp,+ Duration: span.durationNs,+ StatusCode: span.statusCode,+ StatusMessage: span.statusMessage || "",+ SpanKind: span.spanKind || "",+ TraceState: "",+ ScopeName: "",+ ScopeVersion: "",+ SpanAttributes: spanAttributes,+ ResourceAttributes: span.resourceAttributes || {},+ Events: events,+ Links: [],+ Cost: span.cost,+ };+}++/** Normalize a raw ClickHouse `otel_traces` row into a NormalizedSpan. */+export function normalizeSpanRow(+ row: Record<string, unknown>+): NormalizedSpan {+ const spanAttributes = asMap(row.SpanAttributes);+ const resourceAttributes = asMap(row.ResourceAttributes);+ const cost =+ row.Cost !== undefined+ ? asNumber(row.Cost)+ : spanAttributes["gen_ai.usage.cost"] !== undefined+ ? asNumber(spanAttributes["gen_ai.usage.cost"])+ : undefined;++ return {+ traceId: asString(row.TraceId),+ spanId: asString(row.SpanId),+ parentSpanId: asString(row.ParentSpanId),+ name: asString(row.SpanName),+ serviceName: asString(row.ServiceName),+ timestamp: asString(row.Timestamp),+ durationNs: asNumber(row.Duration),+ statusCode: asString(row.StatusCode),+ statusMessage: row.StatusMessage ? asString(row.StatusMessage) : undefined,+ spanKind: row.SpanKind ? asString(row.SpanKind) : undefined,+ spanAttributes,+ resourceAttributes,+ events: normalizeEvents(row),+ cost,+ };+}++/**+ * Convert a NormalizedLog back into the ClickHouse-shaped `otel_logs` row the+ * Logs UI understands (`Timestamp`, `Body`, `SeverityText`, `LogAttributes`, …).+ * External adapters return NormalizedLog; the logs facade denormalizes so the+ * observability pages need no parallel code path. `rowId` is synthesized (see+ * `stableRowId`) since external sources carry no ClickHouse `cityHash64` id.+ */+export function denormalizeLogToClickHouseRow(+ log: NormalizedLog+): Record<string, unknown> {+ const rowId = logStableRowId(log);+ return {+ rowId,+ Timestamp: log.timestamp,+ TraceId: log.traceId || "",+ SpanId: log.spanId || "",+ SeverityText: log.severityText || "",+ SeverityNumber: log.severityNumber ?? 0,+ Body: log.body,+ ServiceName: log.serviceName || "",+ ScopeName: "",+ ScopeVersion: "",+ LogAttributes: log.logAttributes || {},+ ResourceAttributes: log.resourceAttributes || {},+ ScopeAttributes: log.scopeAttributes || {},+ };+}++/**+ * Aggregate point-level `NormalizedMetricPoint`s into the grouped list rows the+ * Metrics table renders (one row per metricName + serviceName). External metric+ * adapters return points; the metrics facade folds them into the same shape+ * `getMetrics` produces from ClickHouse (`latestValue`/`avgValue`/…).+ */+export function denormalizeMetricPointsToListRows(+ points: NormalizedMetricPoint[]+): Record<string, unknown>[] {+ const groups = new Map<+ string,+ {+ metricName: string;+ serviceName: string;+ description?: string;+ unit?: string;+ values: number[];+ latest: { value: number; ts: number };+ lastSeen: string;+ }+ >();++ for (const p of points) {+ const serviceName = p.serviceName || "";+ const key = `${p.metricName}\u0000${serviceName}`;+ const ts = new Date(p.timestamp).getTime();+ const existing = groups.get(key);+ if (!existing) {+ groups.set(key, {+ metricName: p.metricName,+ serviceName,+ description: p.description,+ unit: p.unit,+ values: [p.value],+ latest: { value: p.value, ts: Number.isFinite(ts) ? ts : 0 },+ lastSeen: p.timestamp,+ });+ continue;+ }+ existing.values.push(p.value);+ if (Number.isFinite(ts) && ts >= existing.latest.ts) {+ existing.latest = { value: p.value, ts };+ existing.lastSeen = p.timestamp;+ }+ }++ return Array.from(groups.values()).map((g) => {+ const count = g.values.length;+ const sum = g.values.reduce((a, b) => a + b, 0);+ return {+ metricName: g.metricName,+ metricType: "gauge",+ serviceName: g.serviceName,+ metricDescription: g.description || "",+ metricUnit: g.unit || "",+ latestValue: g.latest.value,+ avgValue: count ? sum / count : 0,+ minValue: count ? Math.min(...g.values) : 0,+ maxValue: count ? Math.max(...g.values) : 0,+ pointCount: count,+ observationCount: count,+ lastSeen: g.lastSeen,+ };+ });+}++/** Normalize a raw ClickHouse `otel_logs` row into a NormalizedLog. */+export function normalizeLogRow(row: Record<string, unknown>): NormalizedLog {+ return {+ timestamp: asString(row.Timestamp),+ traceId: row.TraceId ? asString(row.TraceId) : undefined,+ spanId: row.SpanId ? asString(row.SpanId) : undefined,+ severityText: row.SeverityText ? asString(row.SeverityText) : undefined,+ severityNumber:+ row.SeverityNumber !== undefined ? asNumber(row.SeverityNumber) : undefined,+ body: asString(row.Body),+ serviceName: row.ServiceName ? asString(row.ServiceName) : undefined,+ logAttributes: asMap(row.LogAttributes),+ resourceAttributes: asMap(row.ResourceAttributes),+ scopeAttributes: row.ScopeAttributes ? asMap(row.ScopeAttributes) : undefined,+ };+}++/**+ * Normalize a raw ClickHouse metric row (from the UNION over the 5 metric+ * tables) into a NormalizedMetricPoint.+ */+export function normalizeMetricRow(+ row: Record<string, unknown>+): NormalizedMetricPoint {+ return {+ metricName: asString(row.MetricName),+ description: row.MetricDescription+ ? asString(row.MetricDescription)+ : undefined,+ unit: row.MetricUnit ? asString(row.MetricUnit) : undefined,+ serviceName: row.ServiceName ? asString(row.ServiceName) : undefined,+ timestamp: asString(row.TimeUnix ?? row.Timestamp),+ value: asNumber(row.Value),+ attributes: asMap(row.Attributes),+ resourceAttributes: asMap(row.ResourceAttributes),+ };+}diff --git a/src/client/src/lib/platform/connectors/datasource/clickhouse/query-map.ts b/src/client/src/lib/platform/connectors/datasource/clickhouse/query-map.tsnew file mode 100644index 00000000..06cf4333--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/clickhouse/query-map.ts@@ -0,0 +1,472 @@+/**+ * Map between the vendor-agnostic `OpenLITQuery` and the existing ClickHouse+ * `MetricParams` shape used by Telemetry UI filters / query builders.+ */++import type { MetricParams, TimeLimit } from "@/lib/platform/common";+import type {+ NormalizedFilter,+ OpenLITQuery,+ QuerySort,+ Signal,+} from "../types";++/** Build a `TimeLimit` from an OpenLITQuery time range. */+export function toTimeLimit(query: OpenLITQuery): TimeLimit {+ return {+ // Legacy SQL builders interpolate these values into+ // parseDateTimeBestEffort(). Date#toString includes a parenthesized local+ // timezone name that ClickHouse cannot parse, so preserve ISO explicitly.+ start: query.timeRange.start.toISOString(),+ end: query.timeRange.end.toISOString(),+ type: "CUSTOM",+ };+}++/**+ * Extract a status-code filter (if present) into the `statusCode` array the+ * builders understand.+ */+function extractStatusCodes(query: OpenLITQuery): string[] | undefined {+ const codes = (query.filters || [])+ .filter((f) => f.target === "status")+ .flatMap((f) => (Array.isArray(f.value) ? f.value : [f.value]))+ .filter((v): v is string => typeof v === "string");+ return codes.length ? codes : undefined;+}++/** Build a `MetricParams` from an OpenLITQuery. */+export function toMetricParams(+ query: OpenLITQuery,+ databaseConfigId?: string+): MetricParams {+ const sort = query.sort?.[0];+ const selectedConfig: Record<string, unknown> = {};+ const customFilters: Array<Record<string, unknown>> = [];+ const add = (key: string, value: unknown) => {+ const values = Array.isArray(value) ? value.map(String) : [String(value)];+ selectedConfig[key] = [+ ...((selectedConfig[key] as string[] | undefined) || []),+ ...values,+ ];+ };+ for (const filter of query.filters || []) {+ if (filter.target === "spanName") {+ add(query.signal === "metrics" ? "metricNames" : "spanNames", filter.value);+ continue;+ }+ if (filter.target !== "attribute" || !filter.key) continue;+ if (filter.key === "service.name") {+ add(query.signal === "traces" ? "applicationNames" : "services", filter.value);+ continue;+ }+ if (query.signal === "traces" && filter.key === "gen_ai.request.model") {+ add("models", filter.value);+ continue;+ }+ if (query.signal === "traces" && filter.key === "gen_ai.system") {+ add("providers", filter.value);+ continue;+ }+ if (query.signal === "traces" && filter.key === "gen_ai.operation.name") {+ add("traceTypes", filter.value);+ continue;+ }+ if (query.signal === "traces" && filter.key === "deployment.environment") {+ add("environments", filter.value);+ continue;+ }+ const value = Array.isArray(filter.value) ? filter.value[0] : filter.value;+ customFilters.push({+ attributeType:+ filter.scope === "resource"+ ? "ResourceAttributes"+ : query.signal === "logs"+ ? "LogAttributes"+ : query.signal === "metrics"+ ? "Attributes"+ : "SpanAttributes",+ key: filter.key,+ value: value === undefined ? "" : String(value),+ });+ }+ if (customFilters.length) selectedConfig.customFilters = customFilters;+ return {+ timeLimit: toTimeLimit(query),+ limit: query.limit,+ offset: query.offset,+ statusCode: extractStatusCodes(query),+ databaseConfigId,+ selectedConfig,+ sorting: sort+ ? { type: sort.field, direction: sort.direction }+ : undefined,+ } as MetricParams;+}++function asDate(value: Date | string | undefined, fallback: Date): Date {+ if (value instanceof Date) return value;+ if (typeof value === "string" && value) {+ const d = new Date(value);+ if (!Number.isNaN(d.getTime())) return d;+ }+ return fallback;+}++function stringList(value: unknown): string[] {+ if (!Array.isArray(value)) return [];+ return value.map((v) => String(v)).filter((v) => v.length > 0);+}++function uniqueStrings(values: string[]): string[] {+ return Array.from(new Set(values));+}++/**+ * OpenLIT uses `default` as a synthetic stand-in when+ * `deployment.environment` was missing at materialize time. Emitting it as a+ * hard filter empties Tempo/Loki/Prometheus queries (and misses CH rows with+ * an empty attribute). Treat a lone `default` as "no environment filter".+ */+function realEnvironments(cfg: Record<string, unknown>): string[] {+ const environments = stringList(cfg.environments);+ if (environments.length === 1 && environments[0] === "default") {+ return [];+ }+ return environments;+}++function pushEnvironmentFilter(+ filters: NormalizedFilter[],+ cfg: Record<string, unknown>+) {+ const environments = realEnvironments(cfg);+ if (!environments.length) return;+ filters.push({+ target: "attribute",+ scope: "resource",+ key: "deployment.environment",+ op: "in",+ value: environments,+ });+}++type CustomFilterOp = "eq" | "neq" | "contains" | "in";++function normalizeCustomFilterOp(cf: Record<string, unknown>): CustomFilterOp {+ const opRaw = String(cf.operator || cf.op || "eq");+ if (opRaw === "neq" || opRaw === "!=") return "neq";+ if (opRaw === "contains") return "contains";+ if (opRaw === "in") return "in";+ return "eq";+}++function customFilterValue(value: unknown): string | string[] | undefined {+ if (Array.isArray(value)) return value.map(String);+ return value !== undefined ? String(value) : undefined;+}++/** Build the trace-signal filters (models/providers/spanNames/services/custom). */+function tracesFilters(cfg: Record<string, unknown>): NormalizedFilter[] {+ const filters: NormalizedFilter[] = [];++ const models = stringList(cfg.models);+ if (models.length) {+ filters.push({+ target: "attribute",+ scope: "span",+ key: "gen_ai.request.model",+ op: "in",+ value: models,+ });+ }++ const providers = stringList(cfg.providers);+ if (providers.length) {+ // Primary GenAI provider attr; adapters that only support equality may+ // degrade. Multi-namespace OR (db.system / coding_agent.client) stays+ // ClickHouse-native for now.+ filters.push({+ target: "attribute",+ scope: "span",+ key: "gen_ai.system",+ op: "in",+ value: providers,+ });+ }++ const spanNames = stringList(cfg.spanNames);+ if (spanNames.length) {+ filters.push({ target: "spanName", op: "in", value: spanNames });+ }++ const traceTypes = stringList(cfg.traceTypes);+ if (traceTypes.length) {+ filters.push({+ target: "attribute",+ scope: "span",+ key: "gen_ai.operation.name",+ op: "in",+ value: traceTypes,+ });+ }++ const serviceNames = stringList(cfg.serviceNames);+ if (serviceNames.length) {+ filters.push({+ target: "attribute",+ scope: "resource",+ key: "service.name",+ op: "in",+ value: serviceNames,+ });+ }++ pushEnvironmentFilter(filters, cfg);++ const applicationNames = stringList(cfg.applicationNames);+ if (applicationNames.length) {+ filters.push({+ target: "attribute",+ scope: "resource",+ key: "service.name",+ op: "in",+ value: applicationNames,+ });+ }++ const versionFilter = cfg.versionFilter as+ | { versionHash?: string; firstSeen?: string; lastSeen?: string }+ | undefined;+ if (versionFilter?.versionHash) {+ filters.push({+ target: "attribute",+ key: "openlit.agent.version_hash",+ op: "eq",+ value: String(versionFilter.versionHash),+ });+ }++ const maxCost = typeof cfg.maxCost === "number" ? cfg.maxCost : Number(cfg.maxCost);+ if (Number.isFinite(maxCost)) {+ filters.push({+ target: "attribute",+ scope: "span",+ key: "gen_ai.usage.cost",+ op: "lte",+ value: maxCost,+ });+ }++ const customFilters = Array.isArray(cfg.customFilters)+ ? (cfg.customFilters as Array<Record<string, unknown>>)+ : [];+ for (const cf of customFilters) {+ const key = typeof cf.key === "string" ? cf.key : "";+ if (!key) continue;+ const attributeType = String(cf.attributeType || cf.type || "");+ if (attributeType === "Field") {+ if (key === "SpanName") {+ filters.push({ target: "spanName", op: "eq", value: String(cf.value ?? "") });+ }+ continue;+ }+ const scope =+ cf.scope === "resource" ||+ attributeType === "ResourceAttributes"+ ? "resource"+ : "span";+ filters.push({+ target: "attribute",+ scope,+ key,+ op: normalizeCustomFilterOp(cf),+ value: customFilterValue(cf.value),+ });+ }++ return filters;+}++/**+ * Build log-signal filters. Prefer agent-scoped `serviceNames` (and+ * `applicationNames`) in addition to the logs UI's `services` key so the+ * agent Monitoring tab stays locked to the selected agent.+ */+function logsFilters(cfg: Record<string, unknown>): NormalizedFilter[] {+ const filters: NormalizedFilter[] = [];++ const services = uniqueStrings([+ ...stringList(cfg.serviceNames),+ ...stringList(cfg.services),+ ...stringList(cfg.applicationNames),+ ]);+ if (services.length) {+ filters.push({+ target: "attribute",+ scope: "resource",+ key: "service.name",+ op: "in",+ value: services,+ });+ }++ pushEnvironmentFilter(filters, cfg);++ const severities = stringList(cfg.severities);+ if (severities.length) {+ filters.push({+ target: "attribute",+ scope: "log",+ key: "severity",+ op: "in",+ value: severities,+ });+ }++ const customFilters = Array.isArray(cfg.customFilters)+ ? (cfg.customFilters as Array<Record<string, unknown>>)+ : [];+ for (const cf of customFilters) {+ const key = typeof cf.key === "string" ? cf.key : "";+ if (!key) continue;+ const attrType = String(cf.attributeType || cf.type || "");+ const scope =+ attrType === "ResourceAttributes" || cf.scope === "resource"+ ? "resource"+ : "log";+ filters.push({+ target: "attribute",+ scope,+ key,+ op: normalizeCustomFilterOp(cf),+ value: customFilterValue(cf.value),+ });+ }++ return filters;+}++/**+ * Build metric-signal filters. Honor agent-scoped `serviceNames` the same way+ * traces do, not only the metrics UI's `services` key.+ */+function metricsFilters(cfg: Record<string, unknown>): NormalizedFilter[] {+ const filters: NormalizedFilter[] = [];++ const metricNames = stringList(cfg.metricNames);+ if (metricNames.length) {+ filters.push({ target: "spanName", op: "in", value: metricNames });+ }++ const services = uniqueStrings([+ ...stringList(cfg.serviceNames),+ ...stringList(cfg.services),+ ...stringList(cfg.applicationNames),+ ]);+ if (services.length) {+ filters.push({+ target: "attribute",+ scope: "resource",+ key: "service.name",+ op: "in",+ value: services,+ });+ }++ pushEnvironmentFilter(filters, cfg);++ const customFilters = Array.isArray(cfg.customFilters)+ ? (cfg.customFilters as Array<Record<string, unknown>>)+ : [];+ for (const cf of customFilters) {+ const key = typeof cf.key === "string" ? cf.key : "";+ if (!key) continue;+ const attrType = String(cf.attributeType || cf.type || "");+ const scope =+ attrType === "ResourceAttributes" || cf.scope === "resource"+ ? "resource"+ : "metric";+ filters.push({+ target: "attribute",+ scope,+ key,+ op: normalizeCustomFilterOp(cf),+ value: customFilterValue(cf.value),+ });+ }++ return filters;+}++/**+ * Map Telemetry UI `MetricParams` onto an `OpenLITQuery` for external adapters.+ * Covers time range, pagination, sort, status, and the signal-specific+ * `selectedConfig` filters. Filters a vendor cannot express are best-effort —+ * adapters push what they can and may return a broader set. `aiSelector` is+ * pushed for traces (the AI intelligence surface); logs/metrics are returned+ * as-is so the observability pages match their ClickHouse behavior.+ */+export function metricParamsToOpenLITQuery(+ params: MetricParams,+ signal: Signal = "traces",+ opts: { maxDataPoints?: number; interval?: string; aiSelector?: boolean } = {}+): OpenLITQuery {+ const now = new Date();+ const start = asDate(+ params.timeLimit?.start as Date | string,+ new Date(now.getTime() - 60 * 60 * 1000)+ );+ const end = asDate(params.timeLimit?.end as Date | string, now);+ const cfg = (params.selectedConfig || {}) as Record<string, unknown>;+ const filters: NormalizedFilter[] = [];++ if (signal === "traces" && params.statusCode?.length) {+ filters.push({ target: "status", op: "in", value: params.statusCode });+ }++ // Mirror ClickHouse `operationType` (llm vs vectordb) onto gen_ai.operation.name.+ if (signal === "traces" && params.operationType === "vectordb") {+ filters.push({+ target: "attribute",+ scope: "span",+ key: "gen_ai.operation.name",+ op: "eq",+ value: "vectordb",+ });+ } else if (signal === "traces" && params.operationType === "llm") {+ filters.push({+ target: "attribute",+ scope: "span",+ key: "gen_ai.operation.name",+ op: "neq",+ value: "vectordb",+ });+ }++ if (signal === "traces") filters.push(...tracesFilters(cfg));+ else if (signal === "logs") filters.push(...logsFilters(cfg));+ else if (signal === "metrics") filters.push(...metricsFilters(cfg));++ let sort: QuerySort[] | undefined;+ if (params.sorting?.type) {+ sort = [+ {+ field: String(params.sorting.type),+ direction: params.sorting.direction === "asc" ? "asc" : "desc",+ },+ ];+ }++ return {+ signal,+ timeRange: { start, end },+ filters: filters.length ? filters : undefined,+ sort,+ limit: params.limit,+ offset: params.offset,+ aiSelector: opts.aiSelector ?? signal === "traces",+ interval: opts.interval,+ maxDataPoints: opts.maxDataPoints,+ };+}diff --git a/src/client/src/lib/platform/connectors/datasource/config-fields.ts b/src/client/src/lib/platform/connectors/datasource/config-fields.tsnew file mode 100644index 00000000..987778ae--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/config-fields.ts@@ -0,0 +1,129 @@+/**+ * Shared config-field builders for source-type descriptors.+ *+ * Adapters compose these in their `describe().configFields` so the add/edit+ * form stays fully generic (Grafana-style plugin config schema). A new HTTP+ * vendor reuses `httpVendorFields()`; a bespoke vendor declares its own fields.+ * Labels are resolved from the CE message catalog so all user-facing strings+ * stay centralized.+ */++import getMessage from "@/constants/messages";+import type { FieldDef } from "./types";++/** The datasource HTTP endpoint URL field. */+export function endpointField(placeholder?: string): FieldDef {+ const messages = getMessage();+ return {+ key: "url",+ label: messages.DATA_SOURCE_FIELD_ENDPOINT,+ kind: "url",+ group: "settings",+ placeholder,+ description: messages.DATA_SOURCE_FIELD_ENDPOINT_HELP,+ };+}++/** Toggle allowing plain-HTTP endpoints (defaults on for local stacks). */+export function allowHttpField(): FieldDef {+ const messages = getMessage();+ return {+ key: "allowHttp",+ label: messages.DATA_SOURCE_FIELD_ALLOW_HTTP,+ kind: "switch",+ group: "settings",+ defaultValue: true,+ };+}++/** Explicit SSRF-policy opt-in for localhost, RFC1918, and in-cluster endpoints. */+export function allowPrivateNetworkField(): FieldDef {+ const messages = getMessage();+ return {+ key: "allowPrivateNetwork",+ label: messages.DATA_SOURCE_FIELD_ALLOW_PRIVATE_NETWORK,+ kind: "switch",+ group: "settings",+ defaultValue: false,+ };+}++/**+ * HTTP auth selector + credential fields.+ * `authType` stays in `settings` (adapters read it from settings JSON) but the+ * add/edit form renders it in the Authentication section with the secrets.+ */+export function httpAuthFields(): FieldDef[] {+ const messages = getMessage();+ return [+ {+ key: "authType",+ label: messages.DATA_SOURCE_AUTH_TYPE,+ kind: "select",+ group: "settings",+ options: [+ { value: "none", label: messages.DATA_SOURCE_AUTH_TYPE_NONE },+ { value: "basic", label: messages.DATA_SOURCE_AUTH_TYPE_BASIC },+ { value: "bearer", label: messages.DATA_SOURCE_AUTH_TYPE_BEARER },+ ],+ defaultValue: "none",+ },+ {+ key: "username",+ label: messages.DATA_SOURCE_FIELD_USERNAME,+ kind: "text",+ group: "credentials",+ placeholder: messages.DATA_SOURCE_FIELD_USERNAME_PLACEHOLDER,+ visibleWhen: { key: "authType", value: "basic" },+ },+ {+ key: "password",+ label: messages.DATA_SOURCE_FIELD_PASSWORD,+ kind: "password",+ group: "credentials",+ placeholder: messages.DATA_SOURCE_FIELD_PASSWORD_PLACEHOLDER,+ visibleWhen: { key: "authType", value: "basic" },+ },+ {+ key: "token",+ label: messages.DATA_SOURCE_FIELD_TOKEN,+ kind: "password",+ group: "credentials",+ visibleWhen: { key: "authType", value: "bearer" },+ },+ ];+}++/** Multi-tenant org/account id field (X-Scope-OrgID / AccountID). */+export function tenantField(): FieldDef {+ const messages = getMessage();+ return {+ key: "tenant",+ label: messages.DATA_SOURCE_FIELD_TENANT,+ kind: "text",+ group: "credentials",+ placeholder: messages.DATA_SOURCE_FIELD_TENANT_PLACEHOLDER,+ };+}++/**+ * Standard field set for an HTTP OTLP/observability vendor: endpoint URL,+ * plain-HTTP toggle, HTTP basic/bearer auth, and (optionally) a tenant id.+ * Reused by Tempo, Loki, Prometheus/Mimir, Jaeger, and Victoria adapters, and+ * by any future OTLP vendor.+ */+export function httpVendorFields(+ opts: { placeholder?: string; tenant?: boolean } = {}+): FieldDef[] {+ const [authType, ...credentialFields] = httpAuthFields();+ const fields = [+ endpointField(opts.placeholder),+ allowHttpField(),+ allowPrivateNetworkField(),+ authType,+ ];+ // Tenant/org id sits above username/password/token in Authentication.+ if (opts.tenant) fields.push(tenantField());+ fields.push(...credentialFields);+ return fields;+}diff --git a/src/client/src/lib/platform/connectors/datasource/downsample.ts b/src/client/src/lib/platform/connectors/datasource/downsample.tsnew file mode 100644index 00000000..a9c9d160--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/downsample.ts@@ -0,0 +1,150 @@+/**+ * Pixel-bounded downsampling math (Grafana parity).+ *+ * Every time-series read computes a bucket/step from the requested range and a+ * target point count (`maxDataPoints`, usually the panel pixel width) so a wide+ * window never returns more points than the surface can render. This mirrors+ * Grafana's `PanelQueryRunner`/`rangeUtil.calculateInterval` behavior:+ *+ * intervalMs = max(ceil(rangeMs / maxDataPoints), minIntervalMs), rounded to+ * a "nice" human step.+ *+ * Adapters translate the resulting step into their native units (Prometheus+ * seconds, Tempo Go-duration, ClickHouse bucket labels).+ */++import type { OpenLITQuery, QueryTimeRange } from "./types";++/** Default target point count when a caller does not supply pixel width. */+export const DEFAULT_MAX_DATA_POINTS = 300;+/** Floor so we never ask a vendor for sub-second buckets. */+export const MIN_INTERVAL_MS = 10_000;+/** Hard ceiling on returned points regardless of range (protects the UI). */+export const MAX_SERIES_POINTS = 1_000;++/** "Nice" step ladder in ms — Grafana snaps the raw interval up to one of these. */+const NICE_STEPS_MS = [+ 10_000, // 10s+ 30_000, // 30s+ 60_000, // 1m+ 5 * 60_000, // 5m+ 10 * 60_000, // 10m+ 15 * 60_000, // 15m+ 30 * 60_000, // 30m+ 60 * 60_000, // 1h+ 3 * 60 * 60_000, // 3h+ 6 * 60 * 60_000, // 6h+ 12 * 60 * 60_000, // 12h+ 24 * 60 * 60_000, // 1d+ 7 * 24 * 60 * 60_000, // 1w+];++/** Snap a raw interval up to the nearest "nice" step. */+export function roundIntervalMs(rawMs: number): number {+ for (const step of NICE_STEPS_MS) {+ if (rawMs <= step) return step;+ }+ // Beyond a week, round up to whole days.+ const day = 24 * 60 * 60_000;+ return Math.ceil(rawMs / day) * day;+}++function rangeMsOf(range: QueryTimeRange): number {+ return Math.max(0, range.end.getTime() - range.start.getTime());+}++/**+ * Compute the bucket interval in ms for a query. Honors an explicit+ * `query.interval` label when present; otherwise derives it from the range and+ * `maxDataPoints` (Grafana math) and rounds to a nice step.+ */+export function computeIntervalMs(+ query: Pick<OpenLITQuery, "timeRange" | "maxDataPoints" | "interval">,+ opts: { minIntervalMs?: number } = {}+): number {+ const minInterval = opts.minIntervalMs ?? MIN_INTERVAL_MS;+ const explicit = query.interval ? parseDurationMs(query.interval) : 0;+ if (explicit > 0) return Math.max(explicit, minInterval);++ const rangeMs = rangeMsOf(query.timeRange);+ const mdp =+ query.maxDataPoints && query.maxDataPoints > 0+ ? query.maxDataPoints+ : DEFAULT_MAX_DATA_POINTS;+ const raw = Math.ceil(rangeMs / mdp);+ return roundIntervalMs(Math.max(raw, minInterval));+}++/**+ * Grafana `$__rate_interval` equivalent for rate/increase queries:+ * `max(step + scrapeInterval, 4 * scrapeInterval)` so short ranges still cover+ * at least a few scrapes. Defaults scrapeInterval to 15s.+ */+export function rateIntervalMs(stepMs: number, scrapeIntervalMs = 15_000): number {+ return Math.max(stepMs + scrapeIntervalMs, 4 * scrapeIntervalMs);+}++/**+ * Clamp the expected number of points for a range/step to `MAX_SERIES_POINTS`,+ * returning a (possibly widened) step that keeps the count under the cap.+ */+export function clampStepMs(rangeMs: number, stepMs: number): number {+ if (stepMs <= 0) return MIN_INTERVAL_MS;+ const points = Math.ceil(rangeMs / stepMs);+ if (points <= MAX_SERIES_POINTS) return stepMs;+ return Math.ceil(rangeMs / MAX_SERIES_POINTS);+}++/** Parse a duration label ("30s","5m","1h","1d","1w") to ms. Returns 0 if invalid. */+export function parseDurationMs(label: string): number {+ const raw = label.trim().toLowerCase();+ const named: Record<string, number> = {+ minute: 60_000,+ hour: 3_600_000,+ day: 86_400_000,+ week: 604_800_000,+ month: 2_592_000_000,+ };+ if (named[raw]) return named[raw];+ const m = raw.match(/^(\d+)\s*(ms|s|m|h|d|w)$/);+ if (!m) return 0;+ const n = Number(m[1]);+ const unit: Record<string, number> = {+ ms: 1,+ s: 1_000,+ m: 60_000,+ h: 3_600_000,+ d: 86_400_000,+ w: 604_800_000,+ };+ return n * (unit[m[2]] || 0);+}++/** Format a step in ms as a compact duration label ("30s","5m","1h","1d"). */+export function intervalMsToLabel(stepMs: number): string {+ const s = Math.max(1, Math.round(stepMs / 1000));+ if (s % 86400 === 0) return `${s / 86400}d`;+ if (s % 3600 === 0) return `${s / 3600}h`;+ if (s % 60 === 0) return `${s / 60}m`;+ return `${s}s`;+}++/** Prometheus wants an integer seconds step. */+export function intervalMsToSeconds(stepMs: number): number {+ return Math.max(1, Math.round(stepMs / 1000));+}++/**+ * Align a time range down/up to a step boundary so identical windows within a+ * poll cycle hash to the same cache key (Grafana rounds range to interval to+ * dedupe). Returns new Date instances; never mutates the input.+ */+export function alignRangeToStep(+ range: QueryTimeRange,+ stepMs: number+): QueryTimeRange {+ if (stepMs <= 0) return range;+ const start = Math.floor(range.start.getTime() / stepMs) * stepMs;+ const end = Math.ceil(range.end.getTime() / stepMs) * stepMs;+ return { start: new Date(start), end: new Date(end) };+}diff --git a/src/client/src/lib/platform/connectors/datasource/enterprise.ts b/src/client/src/lib/platform/connectors/datasource/enterprise.tsnew file mode 100644index 00000000..3b9a4396--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/enterprise.ts@@ -0,0 +1,14 @@+/**+ * Neutral extension hook for additional telemetry data source adapters.+ *+ * Atomic vendor adapters (Datadog, Tempo, Loki, etc.) are registered in CE via+ * `bootstrap.ts`. This hook stays empty in CE so a private fork can contribute+ * extra factories without importing `@/ee/**` from shared code.+ */++import type { DataSourceAdapterFactory } from "./types";++/** CE no-op: no extra private adapters beyond the CE bootstrap set. */+export function getExternalDataSourceAdapters(): DataSourceAdapterFactory[] {+ return [];+}diff --git a/src/client/src/lib/platform/connectors/datasource/facade.ts b/src/client/src/lib/platform/connectors/datasource/facade.tsnew file mode 100644index 00000000..7874e7d0--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/facade.ts@@ -0,0 +1,99 @@+/**+ * Shared helpers for the per-signal read facades (traces / logs / metrics).+ *+ * Every facade resolves the currently bound source here and invokes the same+ * DataSourceAdapter contract. The built-in ClickHouse adapter is not a bypass:+ * its reads ultimately enter ClickHouse through `dataCollector` -> OpenPlait.+ * Results are denormalized back to the rows the existing UI consumes.+ *+ * FOLLOW-UP (columnar end-to-end): adapters already produce the normalized+ * columnar contract (`DataFrame` / `NormalizedSpan|Log|MetricPoint`), but these+ * facades denormalize back to ClickHouse-shaped rows so the existing UI tables+ * keep working unchanged. That denormalization is the one place a new adapter+ * inherits CH-shaped assumptions. When the UI tables are migrated to consume+ * `DataFrame` directly, drop the `denormalize*` calls here and pass frames+ * through — no adapter changes required. Tracked as a known follow-up; not a+ * blocker for adding new datasources (which only touch adapter + descriptor).+ */++import { AdapterError } from "@openplait/adapter-sdk";+import type { DataSourceAdapter, Signal } from "./types";+import { UnsupportedCapabilityError } from "./types";+import getMessage from "@/constants/messages";+import { SourceResponseError } from "./http/safe-fetch";++export interface SignalReadContext {+ adapter: DataSourceAdapter;+ descriptor: {+ id: string;+ type: string;+ isBuiltIn: boolean;+ name: string;+ dbConfigId?: string;+ };+ /** Metadata only; signal facades must not branch around the adapter on it. */+ isBuiltIn: boolean;+}++/** Resolve the adapter + descriptor for a signal read. */+export async function resolveSignalReadContext(+ signal: Signal,+ options: { sourceId?: string; environment?: string; dbConfigId?: string; projectId?: string | null } = {}+): Promise<SignalReadContext> {+ const { getTelemetryAdapter, resolveTelemetrySourceDescriptor } =+ await import("@/lib/telemetry-source");+ const descriptor = await resolveTelemetrySourceDescriptor({ signal, ...options });+ const adapter = await getTelemetryAdapter({ signal, ...options, descriptor });+ return {+ adapter,+ descriptor,+ isBuiltIn: descriptor.isBuiltIn || descriptor.type === "clickhouse",+ };+}++/** Prefer a concise upstream explanation when OpenPlait only reports HTTP status. */+function adapterUpstreamMessage(error: AdapterError): string | undefined {+ const body = error.details?.body;+ if (typeof body !== "string") return undefined;+ const cleaned = body+ .replace(/^Data source responded \d+:\s*/i, "")+ .replace(/\s+/g, " ")+ .trim();+ if (!cleaned) return undefined;+ // Keep the first sentence/line — enough for Loki/Tempo limit messages.+ return cleaned.split(/(?<=\.)\s|\n/)[0]?.slice(0, 300) || undefined;+}++/** Normalize any thrown value into a user-facing error string. */+export function facadeErrorMessage(err: unknown): string {+ if (err instanceof UnsupportedCapabilityError) return err.message;+ if (err instanceof AdapterError) {+ const upstream = adapterUpstreamMessage(err);+ if (upstream && /returned HTTP \d+/i.test(err.message)) {+ return `${err.message.replace(/\.$/, "")}: ${upstream}`;+ }+ return err.message;+ }+ if (err instanceof Error) return err.message;+ return typeof err === "string" ? err : getMessage().WIDGET_RUN_FAILED;+}++/**+ * List/summary facades should not swallow adapter/connection failures as soft+ * `{ err }` payloads — rethrow so routes can map them to 503.+ * Capability gaps stay soft (empty UI) and are not rethrown.+ */+export function rethrowIfSourceFailure(err: unknown): void {+ if (err instanceof UnsupportedCapabilityError) return;+ if (err instanceof AdapterError) throw err;+ if (err instanceof SourceResponseError) throw err;+ if (!(err instanceof Error)) return;+ const message = err.message.toLowerCase();+ if (+ /econnrefused|enotfound|etimedout|econnreset|fetch failed|socket hang up|network error|connect(?:ion)? (?:refused|timed out|reset)/i.test(+ message+ )+ ) {+ throw err;+ }+}diff --git a/src/client/src/lib/platform/connectors/datasource/field-values.ts b/src/client/src/lib/platform/connectors/datasource/field-values.tsnew file mode 100644index 00000000..b0075eb7--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/field-values.ts@@ -0,0 +1,29 @@+import type { MetricParams } from "@/lib/platform/common";+import { metricParamsToOpenLITQuery } from "@/lib/platform/connectors/datasource/clickhouse/query-map";+import { facadeErrorMessage, resolveSignalReadContext } from "@/lib/platform/connectors/datasource/facade";+import { planAndDistinctValues } from "@/lib/platform/connectors/datasource/query-planner";+import type { Signal } from "@/lib/platform/connectors/datasource/types";++/**+ * Common datasource-routed field-value lookup used by query builders.+ * The selected signal binding is resolved exactly like list/summary reads.+ */+export async function getSignalFieldValues(+ signal: Exclude<Signal, "intelligence">,+ field: string,+ params: MetricParams+) {+ try {+ const { adapter } = await resolveSignalReadContext(signal, params);+ const query = metricParamsToOpenLITQuery(params, signal);+ const values = await planAndDistinctValues(adapter, field, query);+ return {+ err: null,+ values: Array.from(new Set(values.map(String).filter(Boolean)))+ .sort((a, b) => a.localeCompare(b))+ .slice(0, 100),+ };+ } catch (error) {+ return { err: facadeErrorMessage(error), values: [] as string[] };+ }+}diff --git a/src/client/src/lib/platform/connectors/datasource/grafana/loki.ts b/src/client/src/lib/platform/connectors/datasource/grafana/loki.tsnew file mode 100644index 00000000..f5435667--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/grafana/loki.ts@@ -0,0 +1,565 @@+import { AdapterError } from "@openplait/adapter-sdk";+import { LokiAdapter as OpenPlaitLokiAdapter } from "@openplait/adapter-loki";+import { OpenPlaitHttpAdapter } from "./openplait-http";+import type {+ DataFrame,+ HealthCheckResult,+ NormalizedLog,+ OpenLITQuery,+ QueryTimeRange,+ Signal,+ SourceCapabilities,+ SourceTypeDescriptor,+ TelemetrySourceDescriptor,+} from "../types";+import { openPlaitFramesToRows } from "@/lib/platform/openplait/frames";+import { logStableRowId } from "@/lib/platform/connectors/datasource/clickhouse/normalize";+import { computeIntervalMs, intervalMsToLabel } from "../downsample";+import { httpVendorFields } from "../config-fields";+import { SourceResponseError } from "../http/safe-fetch";+import getMessage from "@/constants/messages";++/** Loki's default `max_query_length` is typically 30d1h (~721h). */+const DEFAULT_MAX_QUERY_RANGE_MS = 30 * 24 * 60 * 60 * 1_000;+/** Cap how many log row ids we remember for list → detail lookups. */+const LOG_INDEX_MAX = 5_000;+/** Recent-window fallback when a detail link misses the warm index. */+const GET_LOG_FALLBACK_RANGE_MS = 24 * 60 * 60 * 1_000;+/** When a list row supplies Timestamp, search a tight window around it. */+const GET_LOG_AROUND_PAD_MS = 60 * 60 * 1_000;+const GET_LOG_FALLBACK_LIMIT = 500;+const GET_LOG_AROUND_LIMIT = 2_000;+const learnedQueryRangeBySource = new Map<string, number>();++/**+ * Process-wide log index so Telemetry list → detail works across separate HTTP+ * handlers in the same Node process (adapters are constructed per request).+ */+const logIndexBySource = new Map<string, Map<string, NormalizedLog>>();++function rememberLogs(sourceId: string, logs: NormalizedLog[]) {+ let map = logIndexBySource.get(sourceId);+ if (!map) {+ map = new Map();+ logIndexBySource.set(sourceId, map);+ }+ for (const log of logs) {+ const rowId = logStableRowId(log);+ if (map.size >= LOG_INDEX_MAX && !map.has(rowId)) {+ const oldest = map.keys().next().value;+ if (oldest) map.delete(oldest);+ }+ map.set(rowId, log);+ }+}++function lookupIndexedLog(+ sourceId: string,+ logId: string+): NormalizedLog | undefined {+ return logIndexBySource.get(sourceId)?.get(logId);+}++/** Test-only: clear the process-wide Loki log index / learned limits. */+export function __resetLokiLearningForTests() {+ learnedQueryRangeBySource.clear();+ logIndexBySource.clear();+}++const LABELS: Record<string, string> = {+ "service.name": "service_name",+ serviceName: "service_name",+ "trace.id": "trace_id",+ traceId: "trace_id",+ "span.id": "span_id",+ spanId: "span_id",+ severity: "level",+ severityText: "level",+ job: "job",+ instance: "instance",+};++function escape(value: string): string {+ return `"${value+ .replace(/\\/g, "\\\\")+ .replace(/"/g, '\\"')+ .replace(/[\r\n\0]/g, " ")}"`;+}++function escapeRegex(value: string): string {+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");+}++function labelName(key: string): string {+ const mapped = LABELS[key] || key;+ return /^[A-Za-z_][A-Za-z0-9_]*$/.test(mapped)+ ? mapped+ : mapped.replace(/[^A-Za-z0-9_]/g, "_");+}++function filterValues(value: unknown): string[] {+ const raw = Array.isArray(value) ? value : [value];+ return raw+ .filter((item) => item !== undefined && item !== null && String(item) !== "")+ .map(String);+}++function selector(query: OpenLITQuery, fallback: string): string {+ const labels: string[] = [];+ const lines: string[] = [];+ for (const filter of query.filters || []) {+ const values = filterValues(filter.value);+ if (+ filter.target === "spanName" ||+ (filter.target === "attribute" &&+ (filter.key === "body" || filter.key === "message"))+ ) {+ for (const value of values) {+ lines.push(+ `${+ filter.op === "neq" || filter.op === "notIn"+ ? "!="+ : "|="+ } ${escape(value)}`+ );+ }+ continue;+ }+ if (filter.target !== "attribute" || !filter.key || values.length === 0) {+ continue;+ }+ const negative = filter.op === "neq" || filter.op === "notIn";+ if (filter.op === "contains") {+ labels.push(+ `${labelName(filter.key)}=~${escape(`.*${escapeRegex(values[0])}.*`)}`+ );+ continue;+ }+ if (values.length === 1 && filter.op !== "in" && filter.op !== "notIn") {+ labels.push(+ `${labelName(filter.key)}${negative ? "!=" : "="}${escape(values[0])}`+ );+ continue;+ }+ labels.push(+ `${labelName(filter.key)}${negative ? "!~" : "=~"}${escape(+ values.map(escapeRegex).join("|")+ )}`+ );+ }+ const base = labels.length ? `{${labels.join(",")}}` : fallback;+ return `${base}${lines.length ? ` ${lines.join(" ")}` : ""}`;+}++/** Parse Loki/Go-style durations such as `30d1h`, `721h`, or `24h0m0s`. */+export function parseLokiDurationMs(value: string): number | undefined {+ let total = 0;+ let matched = 0;+ const unitMs: Record<string, number> = {+ w: 7 * 24 * 60 * 60 * 1_000,+ d: 24 * 60 * 60 * 1_000,+ h: 60 * 60 * 1_000,+ m: 60 * 1_000,+ s: 1_000,+ ms: 1,+ };+ const expression = /(\d+(?:\.\d+)?)(ms|w|d|h|m|s)/g;+ let match: RegExpExecArray | null;+ while ((match = expression.exec(value)) !== null) {+ total += Number(match[1]) * unitMs[match[2]];+ matched += match[0].length;+ }+ return matched === value.replace(/\s+/g, "").length && total > 0+ ? total+ : undefined;+}++/** Extract Loki's reported max query length from an HTTP 400 body. */+export function reportedLokiMaxQueryRangeMs(body: string): number | undefined {+ const match = body.match(+ /query time range exceeds the limit[^\n]*limit:\s*([0-9a-z.]+)/i+ );+ if (!match?.[1]) return undefined;+ return parseLokiDurationMs(match[1]);+}++function adapterErrorBody(error: unknown): string {+ if (error instanceof AdapterError) {+ const body = error.details?.body;+ if (typeof body === "string") return body;+ }+ return error instanceof Error ? error.message : "";+}++function clampTimeRange(+ range: QueryTimeRange,+ maxMs: number+): QueryTimeRange {+ const end = range.end.getTime();+ const start = range.start.getTime();+ if (!Number.isFinite(end) || !Number.isFinite(start) || end < start) {+ return range;+ }+ if (end - start <= maxMs) return range;+ return { start: new Date(end - maxMs), end: range.end };+}++export class LokiAdapter extends OpenPlaitHttpAdapter {+ readonly type = "loki";++ constructor(descriptor: TelemetrySourceDescriptor) {+ super(descriptor);+ }++ private get defaultSelector(): string {+ const value = String(this.descriptor.settings.defaultSelector || "").trim();+ return /^\{.+\}$/.test(value) ? value : '{service_name=~".+"}';+ }++ private get configuredMaxTimeRangeMs(): number | undefined {+ const value = Number(this.descriptor.settings.maxTimeRangeMs);+ return Number.isFinite(value) && value > 0 ? Math.floor(value) : undefined;+ }++ private get maxQueryRangeMs(): number {+ const configured = this.configuredMaxTimeRangeMs;+ const learned = learnedQueryRangeBySource.get(this.descriptor.id);+ const candidates = [+ configured,+ learned,+ configured === undefined && learned === undefined+ ? DEFAULT_MAX_QUERY_RANGE_MS+ : undefined,+ ].filter((value): value is number => value !== undefined && value > 0);+ return Math.min(...candidates);+ }++ private async adapter(): Promise<OpenPlaitLokiAdapter> {+ const connection = await this.openPlaitConnection();+ return new OpenPlaitLokiAdapter(+ {+ url: this.baseUrl,+ httpHeaders: connection.headers,+ allowNativeQueries: true,+ maxResultRows: this.positiveSetting("maxResultRows") || 5_000,+ // OpenPlait rejects oversized ranges; OpenLIT clamps first so we+ // do not also set maxTimeRangeMs here.+ maxLookbackMs: this.positiveSetting("maxLookbackMs"),+ },+ { fetch: connection.fetch }+ );+ }++ capabilities(): SourceCapabilities {+ return {+ signals: ["logs"],+ traceTree: false,+ spanEvents: false,+ serverAggregation: true,+ spanMutation: false,+ distinctValues: true,+ crossTraceSession: false,+ maxLookbackMs: this.positiveSetting("maxLookbackMs"),+ maxTimeRangeMs: this.maxQueryRangeMs,+ rawQuery: false,+ };+ }++ async healthCheck(): Promise<HealthCheckResult> {+ const started = Date.now();+ try {+ await (await this.adapter()).labelNames({ timeoutMs: 10_000 });+ return { ok: true, latencyMs: Date.now() - started };+ } catch (error) {+ return {+ ok: false,+ latencyMs: Date.now() - started,+ message: String((error as Error)?.message || error),+ };+ }+ }++ private range(query: OpenLITQuery): { from: string; to: string } {+ const clamped = clampTimeRange(query.timeRange, this.maxQueryRangeMs);+ return {+ from: clamped.start.toISOString(),+ to: clamped.end.toISOString(),+ };+ }++ private clampedQuery(query: OpenLITQuery): OpenLITQuery {+ const timeRange = clampTimeRange(query.timeRange, this.maxQueryRangeMs);+ return timeRange === query.timeRange ? query : { ...query, timeRange };+ }++ private async withRangeRetry<T>(+ query: OpenLITQuery,+ run: (query: OpenLITQuery) => Promise<T>+ ): Promise<T> {+ const effective = this.clampedQuery(query);+ try {+ return await run(effective);+ } catch (error) {+ const body = adapterErrorBody(error);+ const reported = reportedLokiMaxQueryRangeMs(body);+ const rangeMs =+ effective.timeRange.end.getTime() - effective.timeRange.start.getTime();+ const status =+ error instanceof AdapterError+ ? error.details?.status+ : error instanceof SourceResponseError+ ? error.status+ : typeof (error as { status?: unknown })?.status === "number"+ ? (error as { status: number }).status+ : undefined;+ if (status === 400 && reported !== undefined && rangeMs > reported) {+ learnedQueryRangeBySource.set(this.descriptor.id, reported);+ const retried = this.clampedQuery(query);+ return run(retried);+ }+ throw error;+ }+ }++ async listLogs(query: OpenLITQuery): Promise<DataFrame<NormalizedLog>> {+ return this.withRangeRetry(query, async (effective) => {+ const result = await this.executeNative(await this.adapter(), {+ operation: "list-logs",+ kind: "LokiDatasource",+ language: "logql",+ statement: selector(effective, this.defaultSelector),+ extension: "io.openplait.loki",+ extensionValue: {+ timeRange: this.range(effective),+ limit: effective.limit || 500,+ direction:+ effective.sort?.[0]?.direction === "asc" ? "forward" : "backward",+ },+ });+ const rows = openPlaitFramesToRows(result.frames).map((row) => {+ const labels = (row.labels || {}) as Record<string, string>;+ return {+ timestamp: String(row.timestamp),+ body: String(row.body || ""),+ traceId: labels.trace_id,+ spanId: labels.span_id,+ severityText: labels.level,+ serviceName: labels.service_name || labels.service,+ logAttributes: labels,+ resourceAttributes: labels,+ } satisfies NormalizedLog;+ });+ rememberLogs(this.descriptor.id, rows);+ return {+ fields: [+ { name: "timestamp", type: "time" },+ { name: "body", type: "string" },+ { name: "logAttributes", type: "map" },+ ],+ rows,+ meta: {+ latencyMs: result.metadata?.executionTimeMs,+ freshness: "live",+ },+ };+ });+ }++ async getLog(+ logId: string,+ opts?: { aroundTimestamp?: string | Date; timeRange?: QueryTimeRange }+ ): Promise<NormalizedLog | null> {+ const id = String(logId || "").trim();+ if (!id) return null;+ const cached = lookupIndexedLog(this.descriptor.id, id);+ if (cached) return cached;++ let start: Date;+ let end: Date;+ let limit = GET_LOG_FALLBACK_LIMIT;+ if (opts?.timeRange?.start && opts?.timeRange?.end) {+ start = new Date(opts.timeRange.start);+ end = new Date(opts.timeRange.end);+ limit = GET_LOG_AROUND_LIMIT;+ } else if (opts?.aroundTimestamp) {+ const center = new Date(opts.aroundTimestamp);+ if (!Number.isNaN(center.getTime())) {+ start = new Date(center.getTime() - GET_LOG_AROUND_PAD_MS);+ end = new Date(center.getTime() + GET_LOG_AROUND_PAD_MS);+ limit = GET_LOG_AROUND_LIMIT;+ } else {+ end = new Date();+ start = new Date(end.getTime() - GET_LOG_FALLBACK_RANGE_MS);+ }+ } else {+ end = new Date();+ start = new Date(end.getTime() - Math.min(this.maxQueryRangeMs, 7 * 24 * 60 * 60 * 1_000));+ limit = GET_LOG_AROUND_LIMIT;+ }++ const frame = await this.listLogs({+ signal: "logs",+ timeRange: { start, end },+ limit,+ sort: [{ field: "timestamp", direction: "desc" }],+ });+ return (+ frame.rows.find((row) => logStableRowId(row) === id) ||+ lookupIndexedLog(this.descriptor.id, id) ||+ null+ );+ }++ async logTimeSeries(query: OpenLITQuery): Promise<DataFrame> {+ return this.withRangeRetry(query, async (effective) => {+ const started = Date.now();+ const stepMs = computeIntervalMs(effective);+ const step = intervalMsToLabel(stepMs);+ const stepSeconds = Math.max(1, Math.round(stepMs / 1000));+ // OpenPlait's Loki adapter does not yet forward `step`, which Loki+ // requires for metric query_range — call the API directly.+ const statement = `sum(count_over_time(${selector(effective, this.defaultSelector)}[${step}]))`;+ const range = this.range(effective);+ const connection = await this.openPlaitConnection();+ const url = new URL("loki/api/v1/query_range", `${this.baseUrl}/`);+ url.searchParams.set("query", statement);+ url.searchParams.set(+ "start",+ `${new Date(range.from).getTime()}000000`+ );+ url.searchParams.set("end", `${new Date(range.to).getTime()}000000`);+ url.searchParams.set("step", String(stepSeconds));+ const response = await connection.fetch(url.toString(), {+ headers: connection.headers,+ });+ if (!response.ok) {+ throw new SourceResponseError(+ response.status,+ await response.text()+ );+ }+ const body = (await response.json()) as {+ data?: {+ result?: Array<{ values?: Array<[number | string, string]> }>;+ };+ };+ const rows: Record<string, unknown>[] = [];+ for (const series of body.data?.result || []) {+ for (const sample of series.values || []) {+ const raw = Number(sample[0]);+ if (!Number.isFinite(raw)) continue;+ const timestampMs =+ raw > 10_000_000_000_000 ? raw / 1_000_000 : raw * 1_000;+ const timestamp = new Date(timestampMs).toISOString();+ const count = Number(sample[1]) || 0;+ rows.push({+ timestamp,+ value: count,+ label: timestamp,+ count,+ });+ }+ }+ return {+ fields: [+ { name: "timestamp", type: "time" },+ { name: "value", type: "number" },+ { name: "label", type: "string" },+ { name: "count", type: "number" },+ ],+ rows,+ meta: {+ latencyMs: Date.now() - started,+ freshness: "live",+ },+ };+ });+ }++ async attributeKeys(signal: Signal, window: QueryTimeRange): Promise<string[]> {+ if (signal !== "logs") return [];+ return this.discoverLabelValues("loki/api/v1/labels", window);+ }++ async distinctValues(key: string, query: OpenLITQuery): Promise<string[]> {+ const label = labelName(key);+ return this.discoverLabelValues(+ `loki/api/v1/label/${encodeURIComponent(label)}/values`,+ query.timeRange+ );+ }++ /**+ * Loki's labels APIs return an empty `data` payload unless `start`/`end`+ * are supplied (nanoseconds). OpenPlait's discovery helpers do not yet+ * pass a window, so OpenLIT queries the label endpoints directly with the+ * clamped UI time range.+ */+ private async discoverLabelValues(+ path: string,+ window: QueryTimeRange+ ): Promise<string[]> {+ const connection = await this.openPlaitConnection();+ const clamped = clampTimeRange(window, this.maxQueryRangeMs);+ const url = new URL(path, `${this.baseUrl}/`);+ url.searchParams.set("start", `${clamped.start.getTime()}000000`);+ url.searchParams.set("end", `${clamped.end.getTime()}000000`);+ const response = await connection.fetch(url.toString(), {+ headers: connection.headers,+ });+ if (!response.ok) {+ throw new SourceResponseError(+ response.status,+ await response.text()+ );+ }+ const body = (await response.json()) as { data?: unknown };+ return Array.isArray(body.data)+ ? body.data.filter((item): item is string => typeof item === "string")+ : [];+ }+}++export const lokiAdapterFactory = {+ type: "loki",+ create: (descriptor: TelemetrySourceDescriptor) => new LokiAdapter(descriptor),+ describe: (): SourceTypeDescriptor => ({+ type: "loki",+ displayName: "Grafana Loki",+ description: "Logs from a Loki-compatible query API.",+ declaredSignals: ["logs"],+ capabilities: {+ traceTree: false,+ spanEvents: false,+ serverAggregation: true,+ spanMutation: false,+ distinctValues: true,+ crossTraceSession: false,+ rawQuery: false,+ maxTimeRangeMs: DEFAULT_MAX_QUERY_RANGE_MS,+ },+ correlation: { crossSignal: true, keys: ["traceId", "spanId", "service"] },+ configFields: [+ ...httpVendorFields({+ placeholder: "http://localhost:3100",+ tenant: true,+ }),+ {+ key: "defaultSelector",+ label: "Default LogQL stream selector",+ kind: "text",+ group: "settings",+ placeholder: '{service_name=~".+"}',+ },+ {+ key: "maxTimeRangeMs",+ label: "Maximum query range (ms)",+ kind: "text",+ group: "settings",+ placeholder: String(DEFAULT_MAX_QUERY_RANGE_MS),+ },+ ],+ authStyle: "http",+ authHelp: getMessage().DATA_SOURCE_AUTH_HELP_HTTP,+ }),+};diff --git a/src/client/src/lib/platform/connectors/datasource/grafana/openplait-http.ts b/src/client/src/lib/platform/connectors/datasource/grafana/openplait-http.tsnew file mode 100644index 00000000..caf3f492--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/grafana/openplait-http.ts@@ -0,0 +1,103 @@+import type { NativeQuery, QueryResult } from "@openplait/core";+import { OPENPLAIT_API_VERSION } from "@openplait/core";+import type { DatasourceAdapter as OpenPlaitDatasourceAdapter } from "@openplait/adapter-sdk";+import { BaseExternalAdapter } from "../base-adapter";+import type { TelemetrySourceDescriptor } from "../types";+import { applyHttpAuthCredentials } from "../http/auth-headers";+import { normalizeDatasourceEndpointUrl, rewriteLoopbackEndpointForDocker } from "../http/endpoint-url";+import { resolveSourceSecret, redactableSecretValues } from "../http/secret";+import { safeFetch, selfHostedNetworkOptions, SourceResponseError } from "../http/safe-fetch";++const AUTH_TTL_MS = 30_000;++/** Shared OpenLIT host boundary for OpenPlait's HTTP datasource adapters. */+export abstract class OpenPlaitHttpAdapter extends BaseExternalAdapter {+ private authCache?: { expiresAt: number; headers: Record<string, string>; redact: string[] };++ constructor(descriptor: TelemetrySourceDescriptor) {+ super(descriptor);+ }++ protected get baseUrl(): string {+ const url = normalizeDatasourceEndpointUrl(String(this.descriptor.settings.url || ""));+ const network = selfHostedNetworkOptions(this.descriptor.settings);+ return network.allowPrivateNetwork+ ? rewriteLoopbackEndpointForDocker(url)+ : url;+ }++ protected positiveSetting(key: string): number | undefined {+ const value = Number(this.descriptor.settings[key]);+ return Number.isFinite(value) && value > 0 ? Math.floor(value) : undefined;+ }++ private async auth() {+ if (this.authCache && this.authCache.expiresAt > Date.now()) return this.authCache;+ const secret = await resolveSourceSecret(this.descriptor.secretRef, this.descriptor.dbConfigId, this.descriptor.projectId);+ const headers = applyHttpAuthCredentials(secret.credentials, {+ authType: this.descriptor.settings.authType as string | undefined,+ tenantHeader: "X-Scope-OrgID",+ });+ this.authCache = { expiresAt: Date.now() + AUTH_TTL_MS, headers, redact: redactableSecretValues(secret) };+ return this.authCache;+ }++ protected async openPlaitConnection(): Promise<{ headers: Record<string, string>; fetch: typeof fetch }> {+ const { headers, redact } = await this.auth();+ const network = selfHostedNetworkOptions(this.descriptor.settings);+ const sourceId = this.descriptor.id;+ const guarded = (async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {+ const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;+ try {+ const payload = await safeFetch<unknown>(url, {+ method: init?.method || "GET",+ headers: Object.fromEntries(new Headers(init?.headers).entries()),+ ...network,+ redactValues: redact,+ timeoutMs: 15_000,+ concurrencyKey: sourceId,+ maxConcurrent: 6,+ retry: true,+ });+ return {+ ok: true,+ status: 200,+ statusText: "OK",+ headers: new Headers({ "Content-Type": "application/json" }),+ json: async () => payload,+ text: async () => JSON.stringify(payload),+ } as Response;+ } catch (error) {+ if (error instanceof SourceResponseError) return {+ ok: false,+ status: error.status,+ statusText: "Upstream datasource error",+ headers: new Headers(),+ json: async () => ({ error: error.message }),+ text: async () => error.message,+ } as Response;+ throw error;+ }+ }) as typeof fetch;+ return { headers, fetch: guarded };+ }++ protected async executeNative(+ adapter: OpenPlaitDatasourceAdapter<unknown, unknown>,+ options: { operation: string; kind: string; language: string; statement: string; extension: string; extensionValue: Record<string, unknown> }+ ): Promise<QueryResult> {+ const query: NativeQuery = {+ apiVersion: OPENPLAIT_API_VERSION,+ kind: "Query",+ metadata: { name: `openlit-${this.type}-${options.operation}` },+ spec: {+ mode: "native",+ datasource: { kind: options.kind, name: this.descriptor.id },+ native: { language: options.language, statement: options.statement },+ extensions: { [options.extension]: options.extensionValue },+ },+ };+ const requestId = `openlit:${this.type}:${this.descriptor.id.replace(/[^A-Za-z0-9_.:-]/g, "_").slice(0, 64)}:${options.operation}:${Date.now()}`;+ return adapter.execute(query, { audit: { requestId }, queryId: requestId });+ }+}diff --git a/src/client/src/lib/platform/connectors/datasource/grafana/tempo.ts b/src/client/src/lib/platform/connectors/datasource/grafana/tempo.tsnew file mode 100644index 00000000..c3261de9--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/grafana/tempo.ts@@ -0,0 +1,1783 @@+/**+ * Grafana Tempo adapter (traces).+ *+ * TraceQL search (`GET /api/search`) with the AI selector pushed down, plus+ * `GET /api/traces/{id}` for full spans (including events, so chat view + evals+ * work). Trace-volume summaries bucket lightweight Tempo search rows, while+ * product aggregations and the aggregate agent DAG can use TraceQL metrics or+ * a bounded sample of full traces; cost/token rollups are gated accordingly.+ */++import { BaseExternalAdapter } from "../base-adapter";+import {+ OPENPLAIT_API_VERSION,+ type NativeQuery,+} from "@openplait/core";+import {+ TempoAdapter as OpenPlaitTempoAdapter,+ type TempoAdapterConfig,+ type TempoServerCapabilities,+} from "@openplait/adapter-tempo";+import { AdapterError } from "@openplait/adapter-sdk";+import type {+ AISignalValidation,+ DataFrame,+ DiscoveredService,+ HealthCheckResult,+ NormalizedSpan,+ OpenLITQuery,+ QueryTimeRange,+ ServiceRollup,+ SourceCapabilities,+ SourceTypeDescriptor,+ TelemetrySourceDescriptor,+} from "../types";+import { applyHttpAuthCredentials } from "../http/auth-headers";+import { httpVendorFields } from "../config-fields";+import { computeIntervalMs, clampStepMs } from "../downsample";+import getMessage from "@/constants/messages";+import {+ safeFetch,+ selfHostedNetworkOptions,+ SourceResponseError,+} from "../http/safe-fetch";+import { cacheKey, cachedQuery } from "../http/cache";+import { resolveSourceSecret, redactableSecretValues } from "../http/secret";+import { normalizeOtlpId } from "../otlp-json";+import { openPlaitFramesToRows } from "@/lib/platform/openplait/frames";+import {+ buildAITelemetrySelector,+ type AITelemetrySelector,+ type SelectorCondition,+} from "../ai-selector";+import type { NormalizedFilter } from "../types";+import { mapPool } from "../graph/map-pool";+import {+ computeAggregateSpansL1,+ computeDistinctValuesL1,+ computeSpanTimeSeriesL1,+} from "../l1-compute";+import { bucketSpansByInterval } from "../graph/sample-aggregate";+import { clampQueryToSource } from "../http/limits";++const TTL_MS = 30_000;+const MAX_TRACE_FETCH = 200;+/** Lightweight Tempo search summaries used for the trace-volume chart. */+const TRACE_SUMMARY_CAP = 5_000;+/** Request one extra row so truncation can be reported honestly. */+const MAX_TRACE_SEARCH = TRACE_SUMMARY_CAP + 1;+/** Parallel full-trace downloads after TraceQL search (Grafana Explore loads lazily). */+const TRACE_FETCH_CONCURRENCY = 4;+/** Cap how many span ids we remember for detail/hierarchy lookups. */+const SPAN_INDEX_MAX = 5_000;+const DEFAULT_TEMPO_METRICS_WINDOW_MS = 24 * 60 * 60 * 1_000;+/**+ * Conservative search ceiling when the source has no configured max and Tempo+ * has not yet reported one. Self-hosted Tempo commonly sets+ * `query_frontend.max_duration` to 168h; probing 7D/1M without a cap guarantees+ * a 400 on those instances.+ */+const DEFAULT_TEMPO_SEARCH_RANGE_MS = 7 * 24 * 60 * 60 * 1_000;+const TEMPO_PROFILE_TTL_MS = 10 * 60 * 1000;++type CachedTempoProfile = TempoServerCapabilities & { expiresAt: number };+const tempoProfileBySource = new Map<string, CachedTempoProfile>();+const learnedSearchRangeBySource = new Map<string, number>();+const learnedSearchLimitBySource = new Map<string, number>();++function adapterErrorDiagnostics(error: unknown): Record<string, unknown> {+ if (!(error instanceof AdapterError)) {+ return { message: String((error as Error)?.message || error) };+ }+ const details = error.details || {};+ return {+ message: error.message,+ code: error.code,+ ...(typeof details.status === "number" ? { status: details.status } : {}),+ ...(typeof details.body === "string"+ ? { upstreamBody: details.body.slice(0, 1000) }+ : {}),+ ...(typeof details.endpoint === "string"+ ? { endpoint: details.endpoint }+ : {}),+ ...(typeof details.queryLength === "number"+ ? { queryLength: details.queryLength }+ : {}),+ ...(typeof details.upstreamRequestId === "string"+ ? { upstreamRequestId: details.upstreamRequestId }+ : {}),+ };+}++function adapterErrorStatus(error: unknown): number | undefined {+ const value = error instanceof AdapterError ? error.details?.status : undefined;+ return typeof value === "number" ? value : undefined;+}++function adapterErrorBody(error: unknown): string {+ const value = error instanceof AdapterError ? error.details?.body : undefined;+ return typeof value === "string" ? value : "";+}++function withoutMostRecentHint(traceql: string): string {+ // Linear scan — avoid a trailing `\s+…\s*` regex that can backtrack on+ // long space-padded TraceQL strings (CodeQL js/polynomial-redos).+ const trimmed = traceql.trimEnd();+ const marker = "with (most_recent=true)";+ const lower = trimmed.toLowerCase();+ const idx = lower.lastIndexOf(marker);+ if (idx < 0) return traceql;+ const before = trimmed.slice(0, idx).trimEnd();+ const after = trimmed.slice(idx + marker.length).trim();+ if (after.length > 0) return traceql;+ return before;+}++function goDurationMs(value: string): number | undefined {+ let total = 0;+ let matched = 0;+ const unitMs: Record<string, number> = {+ h: 3_600_000,+ m: 60_000,+ s: 1_000,+ ms: 1,+ };+ const expression = /(\d+(?:\.\d+)?)(ms|h|m|s)/g;+ let match: RegExpExecArray | null;+ while ((match = expression.exec(value)) !== null) {+ total += Number(match[1]) * unitMs[match[2]];+ matched += match[0].length;+ }+ return matched === value.length && total > 0 ? total : undefined;+}++/** True when a Tempo 400 is a search-window ceiling, not a generic query error. */+function isSearchRangeLimitMessage(body: string): boolean {+ if (+ /(?:max(?:imum)?|limit).{0,40}duration|duration.{0,40}(?:max(?:imum)?|limit)/i.test(+ body+ )+ ) {+ return true;+ }+ // query-frontend: "range specified by start and end exceeds 168h0m0s"+ return /(?:range|exceed(?:s|ed)?).{0,80}\d+(?:\.\d+)?(?:ms|h|m|s)|\d+(?:\.\d+)?(?:ms|h|m|s).{0,80}(?:range|exceed(?:s|ed)?)/i.test(+ body+ );+}++/** Extract a Tempo-reported search ceiling without guessing from generic 400s. */+function reportedMaxDurationMs(body: string): number | undefined {+ if (!isSearchRangeLimitMessage(body)) {+ return undefined;+ }+ const candidates: string[] = [];+ const expression = /\b(\d+(?:\.\d+)?(?:ms|h|m|s)(?:\d+(?:\.\d+)?(?:ms|h|m|s))*)\b/g;+ let match: RegExpExecArray | null;+ while ((match = expression.exec(body)) !== null) candidates.push(match[1]);+ for (let index = candidates.length - 1; index >= 0; index--) {+ const parsed = goDurationMs(candidates[index]);+ if (parsed) return parsed;+ }+ return undefined;+}++/** Extract a Tempo-reported search result ceiling (for example, max limit 1000). */+function reportedMaxSearchLimit(body: string): number | undefined {+ const patterns = [+ /limit\s+\d+\s+exceeds\s+max(?:imum)?\s+limit\s+(\d+)/i,+ /max(?:imum)?(?:\s+search)?\s+limit(?:\s+is|:)?\s*(\d+)/i,+ /search[^\n]{0,80}max_result_limit[^\d]*(\d+)/i,+ ];+ for (const pattern of patterns) {+ const value = Number(body.match(pattern)?.[1]);+ if (Number.isSafeInteger(value) && value > 0) return value;+ }+ return undefined;+}++function safeRequestId(sourceId: string, operation: string): string {+ const source = sourceId.replace(/[^A-Za-z0-9_.:-]/g, "_").slice(0, 72);+ return `openlit:tempo:${source}:${operation}:${Date.now()}`;+}++function stringMap(value: unknown): Record<string, string> {+ if (!value || typeof value !== "object" || Array.isArray(value)) return {};+ return Object.fromEntries(+ Object.entries(value as Record<string, unknown>).map(([key, item]) => [+ key,+ typeof item === "string" ? item : JSON.stringify(item),+ ])+ );+}++function eventTimestamp(value: unknown): string | undefined {+ if (typeof value !== "string" && typeof value !== "number") return undefined;+ try {+ const nanos = BigInt(value);+ return new Date(Number(nanos / BigInt(1_000_000))).toISOString();+ } catch {+ return undefined;+ }+}++function eventAttributes(value: unknown): Record<string, string> {+ if (!Array.isArray(value)) return stringMap(value);+ const out: Record<string, string> = {};+ for (const item of value) {+ if (!item || typeof item !== "object") continue;+ const entry = item as { key?: unknown; value?: Record<string, unknown> };+ if (typeof entry.key !== "string") continue;+ const wrapped = entry.value || {};+ const raw =+ wrapped.stringValue ??+ wrapped.intValue ??+ wrapped.doubleValue ??+ wrapped.boolValue ??+ "";+ out[entry.key] = typeof raw === "string" ? raw : String(raw);+ }+ return out;+}++function normalizedEvents(value: unknown): NormalizedSpan["events"] {+ if (!Array.isArray(value)) return [];+ return value.map((item) => {+ const event =+ item && typeof item === "object"+ ? (item as Record<string, unknown>)+ : {};+ return {+ name: String(event.name || ""),+ timestamp: eventTimestamp(event.timeUnixNano ?? event.timestamp),+ attributes: eventAttributes(event.attributes),+ };+ });+}++function normalizedStatus(value: unknown): string {+ const status = String(value || "").toLowerCase();+ if (status === "error" || status === "status_code_error") {+ return "STATUS_CODE_ERROR";+ }+ if (status === "ok" || status === "status_code_ok") {+ return "STATUS_CODE_OK";+ }+ return status === "unset" ? "STATUS_CODE_UNSET" : String(value || "");+}++function normalizedSpanKind(value: unknown): string | undefined {+ const kind = String(value || "").toLowerCase();+ const number = {+ unspecified: "0",+ internal: "1",+ server: "2",+ client: "3",+ producer: "4",+ consumer: "5",+ }[kind as "unspecified" | "internal" | "server" | "client" | "producer" | "consumer"];+ return number || (value === undefined || value === null ? undefined : String(value));+}++function openPlaitRowsToSpans(rows: Record<string, unknown>[]): NormalizedSpan[] {+ return rows.map((row) => {+ const resourceAttributes = stringMap(row["resource.attributes"]);+ return {+ traceId: normalizeOtlpId(String(row["trace.id"] || "")),+ spanId: normalizeOtlpId(String(row["span.id"] || "")),+ parentSpanId: normalizeOtlpId(String(row["span.parent_id"] || "")),+ name: String(row["span.name"] || ""),+ serviceName: String(+ row["service.name"] || resourceAttributes["service.name"] || ""+ ),+ timestamp: String(row.timestamp || ""),+ durationNs: Math.max(0, Number(row.duration || 0) * 1_000_000),+ statusCode: normalizedStatus(row["status.code"]),+ statusMessage: String(row["status.message"] || ""),+ spanKind: normalizedSpanKind(row["span.kind"]),+ spanAttributes: stringMap(row["span.attributes"]),+ resourceAttributes,+ events: normalizedEvents(row.events),+ };+ });+}++/**+ * Process-wide span index so Telemetry list → detail works across separate+ * HTTP handlers in the same Node process (adapters are constructed per request).+ */+const spanIndexBySource = new Map<string, Map<string, NormalizedSpan>>();++function rememberSpans(sourceId: string, spans: NormalizedSpan[]) {+ let map = spanIndexBySource.get(sourceId);+ if (!map) {+ map = new Map();+ spanIndexBySource.set(sourceId, map);+ }+ for (const span of spans) {+ if (!span.spanId) continue;+ if (map.size >= SPAN_INDEX_MAX) {+ const oldest = map.keys().next().value;+ if (oldest) map.delete(oldest);+ }+ map.set(span.spanId, span);+ }+}++function lookupIndexedSpan(+ sourceId: string,+ spanId: string+): NormalizedSpan | undefined {+ const map = spanIndexBySource.get(sourceId);+ if (!map) return undefined;+ return map.get(spanId) || map.get(normalizeOtlpId(spanId));+}++/** Test-only: clear the process-wide Tempo span index. */+export function __clearTempoSpanIndex() {+ spanIndexBySource.clear();+ tempoProfileBySource.clear();+ learnedSearchRangeBySource.clear();+ learnedSearchLimitBySource.clear();+}++function pickRootSpan(spans: NormalizedSpan[]): NormalizedSpan | undefined {+ if (spans.length === 0) return undefined;+ return (+ spans.find((s) => !s.parentSpanId || s.parentSpanId === "0".repeat(16)) ||+ spans[0]+ );+}++function traceqlValue(v: string): string {+ return `"${v.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;+}++/**+ * Allow only safe TraceQL attribute path segments. Hostile keys could otherwise+ * inject operators/scopes into `{ span.${key} = ... }` clauses.+ */+function sanitizeTraceQLAttrKey(key: string): string | null {+ const k = String(key || "").trim();+ if (!k || k.length > 128) return null;+ if (!/^[A-Za-z_][A-Za-z0-9_./-]*$/.test(k)) return null;+ return k;+}++function conditionToTraceQL(cond: SelectorCondition): string {+ if (cond.target === "spanName") {+ const values = Array.isArray(cond.value) ? cond.value : [cond.value || ""];+ return `(${values.map((v) => `name = ${traceqlValue(String(v))}`).join(" || ")})`;+ }+ const safeKey = sanitizeTraceQLAttrKey(cond.key ?? "");+ if (!safeKey) return "";+ const scope = cond.scope === "resource" ? "resource" : "span";+ const key = `${scope}.${safeKey}`;+ if (cond.op === "exists") return `${key} != ""`;+ if (cond.op === "eq") return `${key} = ${traceqlValue(String(cond.value ?? ""))}`;+ if (cond.op === "in") {+ const values = Array.isArray(cond.value) ? cond.value : [cond.value || ""];+ return `(${values.map((v) => `${key} = ${traceqlValue(String(v))}`).join(" || ")})`;+ }+ return "";+}++function filterToTraceQL(filter: NormalizedFilter): string {+ if (filter.target === "spanName") {+ const values = Array.isArray(filter.value)+ ? filter.value+ : [filter.value || ""];+ return `(${values.map((v) => `name = ${traceqlValue(String(v))}`).join(" || ")})`;+ }+ if (filter.target === "status") {+ const values = Array.isArray(filter.value)+ ? filter.value.map(String)+ : [String(filter.value || "")];+ const wantsError = values.some((v) =>+ /error/i.test(v) || v === "STATUS_CODE_ERROR"+ );+ return wantsError ? `status = error` : `status != error`;+ }+ if (filter.target === "attribute" && filter.key) {+ const safeKey = sanitizeTraceQLAttrKey(filter.key);+ if (!safeKey) return "";+ const scope =+ filter.scope === "resource"+ ? "resource"+ : safeKey.startsWith("service.") ||+ safeKey === "deployment.environment" ||+ safeKey.startsWith("k8s.") ||+ safeKey.startsWith("telemetry.sdk")+ ? "resource"+ : "span";+ const key = `${scope}.${safeKey}`;+ if (filter.op === "exists") return `${key} != ""`;+ if (filter.op === "eq")+ return `${key} = ${traceqlValue(String(filter.value ?? ""))}`;+ if (filter.op === "in") {+ const values = Array.isArray(filter.value)+ ? filter.value+ : [filter.value || ""];+ return `(${values.map((v) => `${key} = ${traceqlValue(String(v))}`).join(" || ")})`;+ }+ }+ return "";+}++/** Build TraceQL `{ … }` from AI selector + OpenLITQuery filters. */+export function buildTempoSearchQuery(+ query: OpenLITQuery,+ opts?: { mostRecent?: boolean }+): string {+ const parts: string[] = [];+ if (query.aiSelector !== false) {+ const ai = tempoAISelectorQuery().replace(/^\{\s*|\s*\}$/g, "").trim();+ // The AI selector is a multi-group OR (`g1 || g2 || …`). TraceQL binds+ // `&&` tighter than `||`, so joining it with filters unparenthesized+ // (`g1 || g2 && service = "x"`) would scope only the LAST group and leak+ // every other service. Wrap it so filters constrain the whole selector.+ if (ai) parts.push(`(${ai})`);+ }+ for (const filter of query.filters || []) {+ const clause = filterToTraceQL(filter);+ if (clause) parts.push(clause);+ }+ const body = parts.length === 0 ? "{}" : `{ ${parts.join(" && ")} }`;+ // Tempo 2.8+: prefer time-ordered search so the newest traces surface first+ // within each service sample (still needs per-service fan-out for fairness).+ // Opt-in only — TraceQL metrics pipelines reject the `with` hint.+ if (opts?.mostRecent === true) {+ return `${body} with (most_recent=true)`;+ }+ return body;+}++export function tempoAISelectorQuery(+ selector: AITelemetrySelector = buildAITelemetrySelector()+): string {+ const groups = selector.anyOf.map((p) => {+ const parts = p.allOf.map(conditionToTraceQL).filter(Boolean);+ return parts.length === 1 ? parts[0] : `(${parts.join(" && ")})`;+ });+ return `{ ${groups.join(" || ")} }`;+}++// ─── TraceQL metrics helpers (server-side aggregation, Grafana-style) ──────────+//+// Tempo has no generic aggregation endpoint, but it exposes a Prometheus-style+// TraceQL metrics API (`/api/metrics/query_range`) that computes counts/sums/avgs+// over EVERY matching span in the window — not a bounded sample. Dashboards must+// use this so totals are accurate (a 200-trace sample reports "200" for every+// busy service). We fall back to the L1 sample only when metrics are unavailable.++/** Map an OpenLIT numeric field to a scoped TraceQL attribute reference. */+function metricAttrRef(field?: string): string {+ const raw = (field || "").trim();+ if (!raw) return "";+ if (raw === "duration" || raw === "Duration" || raw === "durationNs") return "duration";+ if (raw.startsWith("span.") || raw.startsWith("resource.")) {+ const prefix = raw.startsWith("span.") ? "span." : "resource.";+ const safe = sanitizeTraceQLAttrKey(raw.slice(prefix.length));+ return safe ? `${prefix}${safe}` : "";+ }+ const f = sanitizeTraceQLAttrKey(raw);+ if (!f) return "";+ if (+ f === "service.name" ||+ f.startsWith("service.") ||+ f === "deployment.environment" ||+ f.startsWith("k8s.") ||+ f.startsWith("telemetry.sdk")+ ) {+ return `resource.${f}`;+ }+ return `span.${f}`;+}++/** Map a groupBy field to the attribute used in a metrics `by (...)` clause. */+function metricGroupByAttr(field: string): string | null {+ const f = (field || "").trim();+ if (!f) return null;+ if (f === "SpanName" || f === "spanName" || f === "name" || f === "Name") {+ return "name";+ }+ if (+ f === "service.name" ||+ f === "ServiceName" ||+ f === "serviceName" ||+ f === "resource.service.name" ||+ f === "applicationName"+ ) {+ return "resource.service.name";+ }+ return metricAttrRef(f);+}++/** The TraceQL metric pipeline function for an aggregation, or null if unsupported. */+function aggregationToMetricExpr(+ fn: string,+ field?: string+): string | null {+ if (fn === "count") return "count_over_time()";+ const attr = metricAttrRef(field);+ if (!attr) return null;+ if (fn === "sum") return `sum_over_time(${attr})`;+ if (fn === "avg") return `avg_over_time(${attr})`;+ if (fn === "min") return `min_over_time(${attr})`;+ if (fn === "max") return `max_over_time(${attr})`;+ return null;+}++/**+ * Valid Tempo `step` (Go duration — no `d`/`w` units, so days become hours).+ * The count query and every companion metric query share this so their bucket+ * timestamps line up for a clean per-bucket merge.+ */+function msToTempoDuration(ms: number): string {+ // Tempo `step` is a Go duration and does not support `d`/`w`; days collapse+ // to hours (e.g. 24h) so bucket timestamps still line up.+ const s = Math.max(1, Math.round(ms / 1000));+ if (s % 3600 === 0) return `${s / 3600}h`;+ if (s % 60 === 0) return `${s / 60}m`;+ return `${s}s`;+}++/**+ * Pixel-bounded Tempo metrics `step`. Derives the bucket from the range and+ * `maxDataPoints` (Grafana math) — or an explicit `interval` — and clamps the+ * point count so a wide window can't return an unbounded series. The count+ * query and every companion metric query share this so bucket timestamps line+ * up for a clean per-bucket merge.+ */+function metricsStepForQuery(query: OpenLITQuery): string {+ const rangeMs =+ query.timeRange.end.getTime() - query.timeRange.start.getTime();+ const stepMs = clampStepMs(rangeMs, computeIntervalMs(query));+ return msToTempoDuration(stepMs);+}++interface TempoMetricsSample {+ timestampMs?: string | number;+ timestamp_ms?: string | number;+ value?: string | number;+}+interface TempoMetricsSeriesLabel {+ key?: string;+ value?: { stringValue?: string; string_value?: string } | string;+}+interface TempoMetricsSeries {+ labels?: TempoMetricsSeriesLabel[];+ samples?: TempoMetricsSample[];+ values?: [number, number][];+}+function splitTempoMetricWindows(+ timeRange: QueryTimeRange,+ maxWindowMs: number+): QueryTimeRange[] {+ const windows: QueryTimeRange[] = [];+ let cursor = timeRange.start.getTime();+ const end = timeRange.end.getTime();+ while (cursor < end) {+ const next = Math.min(end, cursor + maxWindowMs);+ windows.push({ start: new Date(cursor), end: new Date(next) });+ cursor = next;+ }+ return windows.length ? windows : [timeRange];+}++function normalizedMetricRowsToSeries(+ rows: Record<string, unknown>[]+): TempoMetricsSeries[] {+ const grouped = new Map<+ string,+ { labels: Record<string, string>; samples: Map<number, number> }+ >();+ for (const row of rows) {+ const labels = stringMap(row.labels);+ const key = JSON.stringify(Object.entries(labels).sort(([a], [b]) => a.localeCompare(b)));+ let series = grouped.get(key);+ if (!series) {+ series = { labels, samples: new Map() };+ grouped.set(key, series);+ }+ const timestamp = Date.parse(String(row.timestamp || ""));+ const value = Number(row.value);+ if (Number.isFinite(timestamp) && Number.isFinite(value)) {+ // Adjacent inclusive Tempo windows can repeat their boundary bucket.+ // Overwrite that identical timestamp instead of double-counting it.+ series.samples.set(timestamp, value);+ }+ }+ return Array.from(grouped.values()).map(({ labels, samples }) => ({+ labels: Object.entries(labels).map(([key, value]) => ({ key, value })),+ samples: Array.from(samples.entries())+ .sort(([left], [right]) => left - right)+ .map(([timestampMs, value]) => ({ timestampMs, value })),+ }));+}++/** Extract a series' grouping label value (defensive across JSON shapes). */+function seriesLabelValue(series: TempoMetricsSeries, attr: string | null): string {+ if (!attr) return "";+ // Tempo emits the group label under the bare attribute name (e.g.+ // `service.name`) as well as the scoped form; accept either.+ const bare = attr.replace(/^(resource|span)\./, "");+ for (const label of series.labels || []) {+ if (label.key !== attr && label.key !== bare) continue;+ const v = label.value;+ if (typeof v === "string") return v;+ if (v && typeof v === "object") {+ return String(v.stringValue ?? v.string_value ?? "");+ }+ }+ return "";+}++/** Parse a series' data points into `{ tsMs, value }`, tolerating both shapes. */+function seriesBuckets(+ series: TempoMetricsSeries+): Array<{ ts: number; value: number }> {+ if (Array.isArray(series.samples)) {+ return series.samples.map((s) => ({+ ts: Number(s.timestampMs ?? s.timestamp_ms ?? 0),+ value: Number(s.value ?? 0),+ }));+ }+ if (Array.isArray(series.values)) {+ // `[secondsEpoch, value]` tuples.+ return series.values.map(([ts, v]) => ({ ts: Number(ts) * 1000, value: Number(v) }));+ }+ return [];+}++export class TempoAdapter extends BaseExternalAdapter {+ readonly type = "tempo";+ private authCache?: {+ expiresAt: number;+ headers: Record<string, string>;+ redact: string[];+ };++ private get baseUrl(): string {+ return String(this.descriptor.settings.url || "").replace(/\/$/, "");+ }++ private get networkOpts() {+ return selfHostedNetworkOptions(this.descriptor.settings);+ }++ private get configuredMaxTimeRangeMs(): number | undefined {+ const explicitMs = Number(this.descriptor.settings.maxTimeRangeMs);+ const explicitDays = Number(this.descriptor.settings.maxTimeRangeDays);+ return (+ Number.isFinite(explicitMs) && explicitMs > 0+ ? Math.max(60_000, Math.floor(explicitMs))+ : Number.isFinite(explicitDays) && explicitDays > 0+ ? Math.max(60_000, Math.floor(explicitDays * 24 * 60 * 60 * 1_000))+ : undefined+ );+ }++ private get maxTimeRangeMs(): number | undefined {+ const configured = this.configuredMaxTimeRangeMs;+ const learned = learnedSearchRangeBySource.get(this.descriptor.id);+ if (configured === undefined && learned === undefined) {+ return DEFAULT_TEMPO_SEARCH_RANGE_MS;+ }+ if (configured === undefined) return learned;+ return learned === undefined ? configured : Math.min(configured, learned);+ }++ private get configuredMaxSearchResults(): number | undefined {+ const value = Number(+ this.descriptor.settings.maxSearchResults ??+ this.descriptor.settings.maxResultRows+ );+ return Number.isSafeInteger(value) && value > 0 ? value : undefined;+ }++ private searchResultLimit(requested: number): number {+ const limits = [+ Math.max(1, Math.floor(requested)),+ this.configuredMaxSearchResults,+ learnedSearchLimitBySource.get(this.descriptor.id),+ ].filter((value): value is number => value !== undefined);+ return Math.max(1, Math.min(...limits));+ }++ private cachedTempoProfile(): CachedTempoProfile | undefined {+ const cached = tempoProfileBySource.get(this.descriptor.id);+ return cached && cached.expiresAt > Date.now() ? cached : undefined;+ }++ private rememberTempoProfile(profile: TempoServerCapabilities): void {+ tempoProfileBySource.set(this.descriptor.id, {+ ...profile,+ expiresAt: Date.now() + TEMPO_PROFILE_TTL_MS,+ });+ }++ private async authHeaders() {+ if (this.authCache && this.authCache.expiresAt > Date.now()) {+ return this.authCache;+ }+ const secret = await resolveSourceSecret(+ this.descriptor.secretRef,+ this.descriptor.dbConfigId,+ this.descriptor.projectId+ );+ const headers = applyHttpAuthCredentials(secret.credentials, {+ authType: this.descriptor.settings.authType as string | undefined,+ tenantHeader: "X-Scope-OrgID",+ });+ const authType = String(+ this.descriptor.settings.authType || "auto"+ ).toLowerCase();+ if ((authType === "basic" || authType === "bearer") && !headers.Authorization) {+ throw new Error(+ getMessage().DATA_SOURCE_AUTH_REQUIRED(this.descriptor.name, authType)+ );+ }+ this.authCache = {+ expiresAt: Date.now() + TTL_MS,+ headers,+ redact: redactableSecretValues(secret),+ };+ return this.authCache;+ }++ /**+ * Bind OpenPlait's Tempo adapter to OpenLIT's guarded HTTP transport. The+ * package owns TraceQL execution and response normalization; OpenLIT keeps+ * project-scoped secrets, SSRF policy, redaction, caching, and concurrency.+ */+ private async openPlaitAdapter(): Promise<OpenPlaitTempoAdapter> {+ const { headers, redact } = await this.authHeaders();+ const guardedFetch = (async (+ input: RequestInfo | URL,+ init?: RequestInit+ ): Promise<Response> => {+ const url =+ typeof input === "string"+ ? input+ : input instanceof URL+ ? input.toString()+ : input.url;+ const requestHeaders = Object.fromEntries(+ new Headers(init?.headers).entries()+ );+ let payload: unknown;+ try {+ payload = await safeFetch<unknown>(url, {+ method: init?.method || "GET",+ headers: requestHeaders,+ ...this.networkOpts,+ redactValues: redact,+ timeoutMs: 15_000,+ concurrencyKey: this.descriptor.id,+ maxConcurrent: TRACE_FETCH_CONCURRENCY,+ retry: true,+ });+ } catch (error) {+ if (error instanceof SourceResponseError) {+ return {+ ok: false,+ status: error.status,+ statusText: "Upstream Tempo error",+ headers: new Headers({ "Content-Type": "text/plain" }),+ json: async () => ({ error: error.message }),+ text: async () => error.message,+ } as Response;+ }+ throw error;+ }+ // safeFetch has already performed the HTTP request and parsed JSON. A+ // lightweight Fetch Response keeps the OpenPlait transport boundary+ // portable across Node and Jest/jsdom without serializing it twice.+ return {+ ok: true,+ status: 200,+ statusText: "OK",+ headers: new Headers({ "Content-Type": "application/json" }),+ json: async () => payload,+ text: async () => JSON.stringify(payload),+ } as Response;+ }) as typeof fetch;+ const profile = this.cachedTempoProfile();+ const maxTimeRangeMs = this.maxTimeRangeMs;+ const configuredVersion = String(+ this.descriptor.settings.tempoVersion || profile?.version || ""+ ).trim();+ const config: TempoAdapterConfig = {+ url: this.baseUrl,+ httpHeaders: headers,+ allowNativeQueries: true,+ requestTimeoutMs: 15_000,+ queryTimeoutMs: 15_000,+ maxResultRows: this.searchResultLimit(MAX_TRACE_SEARCH),+ maxSpansPerSpanSet: 100,+ ...(maxTimeRangeMs === undefined ? {} : { maxTimeRangeMs }),+ ...(configuredVersion ? { tempoVersion: configuredVersion } : {}),+ ...(typeof this.descriptor.settings.enableMostRecent === "boolean"+ ? { enableMostRecent: this.descriptor.settings.enableMostRecent }+ : profile?.features.mostRecent === null || profile?.features.mostRecent === undefined+ ? {}+ : { enableMostRecent: profile.features.mostRecent }),+ ...(profile?.features.traceqlMetrics === null || profile?.features.traceqlMetrics === undefined+ ? {}+ : { enableTraceqlMetrics: profile.features.traceqlMetrics }),+ ...(profile?.features.tagSearchV2 === null || profile?.features.tagSearchV2 === undefined+ ? {}+ : { enableTagSearchV2: profile.features.tagSearchV2 }),+ };+ return new OpenPlaitTempoAdapter(config, { fetch: guardedFetch });+ }++ private async inspectTempoServer(): Promise<TempoServerCapabilities> {+ const cached = this.cachedTempoProfile();+ if (cached) return cached;+ const adapter = await this.openPlaitAdapter();+ const profile = await adapter.inspectServer({ timeoutMs: 8_000 });+ this.rememberTempoProfile(profile);+ return profile;+ }++ private async openPlaitTraceSearchRows(+ traceql: string,+ timeRange: QueryTimeRange,+ limit: number,+ compatibility: { hint?: boolean; range?: boolean; resultLimit?: boolean } = {}+ ): Promise<Record<string, unknown>[]> {+ const requestLimit = this.searchResultLimit(limit);+ const adapter = await this.openPlaitAdapter();+ const maxMs = this.maxTimeRangeMs;+ const rangeMs = timeRange.end.getTime() - timeRange.start.getTime();+ const searchRange =+ maxMs !== undefined && rangeMs > maxMs+ ? {+ start: new Date(timeRange.end.getTime() - maxMs),+ end: timeRange.end,+ }+ : timeRange;+ const resource: NativeQuery = {+ apiVersion: OPENPLAIT_API_VERSION,+ kind: "Query",+ metadata: { name: "openlit-tempo-search" },+ spec: {+ mode: "native",+ datasource: {+ kind: "TempoDatasource",+ name: this.descriptor.id,+ },+ native: { language: "traceql", statement: traceql },+ extensions: {+ "io.openplait.tempo": {+ timeRange: {+ from: searchRange.start.toISOString(),+ to: searchRange.end.toISOString(),+ },+ limit: requestLimit,+ spansPerSpanSet: 100,+ },+ },+ },+ };+ try {+ const result = await adapter.execute(resource, {+ audit: {+ requestId: safeRequestId(this.descriptor.id, "search"),+ },+ });+ return openPlaitFramesToRows(result.frames);+ } catch (error) {+ const status = adapterErrorStatus(error);+ const body = adapterErrorBody(error);+ const reportedMaxResults =+ status === 400 ? reportedMaxSearchLimit(body) : undefined;+ if (+ compatibility.resultLimit !== false &&+ reportedMaxResults !== undefined &&+ reportedMaxResults < requestLimit+ ) {+ learnedSearchLimitBySource.set(+ this.descriptor.id,+ reportedMaxResults+ );+ return this.openPlaitTraceSearchRows(+ traceql,+ timeRange,+ reportedMaxResults,+ { ...compatibility, resultLimit: false }+ );+ }+ if (+ compatibility.hint !== false &&+ status === 400 &&+ withoutMostRecentHint(traceql) !== traceql+ ) {+ const cached = this.cachedTempoProfile();+ if (cached) {+ this.rememberTempoProfile({+ ...cached,+ features: { ...cached.features, mostRecent: false },+ });+ }+ return this.openPlaitTraceSearchRows(+ withoutMostRecentHint(traceql),+ timeRange,+ limit,+ { ...compatibility, hint: false }+ );+ }+ const reportedMaxMs = status === 400 ? reportedMaxDurationMs(body) : undefined;+ if (+ compatibility.range !== false &&+ reportedMaxMs !== undefined &&+ searchRange.end.getTime() - searchRange.start.getTime() > reportedMaxMs+ ) {+ learnedSearchRangeBySource.set(this.descriptor.id, reportedMaxMs);+ const compatibleRange = {+ start: new Date(searchRange.end.getTime() - reportedMaxMs),+ end: searchRange.end,+ };+ return this.openPlaitTraceSearchRows(+ traceql,+ compatibleRange,+ limit,+ { ...compatibility, range: false }+ );+ }+ throw error;+ }+ }++ private async openPlaitTraceSearch(+ traceql: string,+ timeRange: QueryTimeRange,+ limit: number+ ): Promise<string[]> {+ return (await this.openPlaitTraceSearchRows(traceql, timeRange, limit))+ .map((row) => normalizeOtlpId(String(row["trace.id"] || "")))+ .filter(Boolean);+ }++ capabilities(): SourceCapabilities {+ const maxTimeRangeMs = this.maxTimeRangeMs;+ return {+ signals: ["traces"],+ traceTree: true,+ spanEvents: true,+ serverAggregation: false,+ spanMutation: false,+ distinctValues: true,+ crossTraceSession: false,+ ...(maxTimeRangeMs === undefined ? {} : { maxTimeRangeMs }),+ rawQuery: false,+ };+ }++ async healthCheck(): Promise<HealthCheckResult> {+ const start = Date.now();+ try {+ await this.inspectTempoServer();+ return { ok: true, latencyMs: Date.now() - start };+ } catch (err) {+ const status = adapterErrorStatus(err);+ // Some managed Tempo gateways deliberately hide `/api/status/buildinfo`.+ // A small, hint-free search still proves endpoint/auth compatibility,+ // while leaving version-dependent capabilities conservatively disabled.+ if (status === 403 || status === 404) {+ try {+ const end = new Date();+ await this.openPlaitTraceSearchRows(+ "{}",+ { start: new Date(end.getTime() - 5 * 60 * 1_000), end },+ 1,+ { hint: false, range: false }+ );+ return { ok: true, latencyMs: Date.now() - start };+ } catch (searchErr) {+ err = searchErr;+ }+ }+ return { ok: false, message: String((err as Error)?.message || err) };+ }+ }++ private async searchTraceIds(+ query: OpenLITQuery,+ limit: number+ ): Promise<string[]> {+ const boundedQuery = clampQueryToSource(this, query).query;+ const traceql = buildTempoSearchQuery(boundedQuery, {+ mostRecent:+ this.descriptor.settings.enableMostRecent === true ||+ this.cachedTempoProfile()?.features.mostRecent === true,+ });+ const key = cacheKey(this.descriptor.id, [+ "openplait-search",+ traceql,+ boundedQuery.timeRange.start.toISOString(),+ boundedQuery.timeRange.end.toISOString(),+ limit,+ ]);+ return cachedQuery(key, TTL_MS, () =>+ this.openPlaitTraceSearch(traceql, boundedQuery.timeRange, limit)+ );+ }++ async getTraceSpans(traceId: string): Promise<NormalizedSpan[]> {+ const id = normalizeOtlpId(traceId) || traceId;+ const key = cacheKey(this.descriptor.id, ["openplait-trace", id]);+ const result = await cachedQuery(key, TTL_MS, async () => {+ const adapter = await this.openPlaitAdapter();+ return adapter.getTrace(id, {+ audit: {+ requestId: safeRequestId(this.descriptor.id, "trace"),+ },+ });+ });+ const spans = openPlaitRowsToSpans(openPlaitFramesToRows(result.frames));+ rememberSpans(this.descriptor.id, spans);+ return spans;+ }++ async getSpan(spanId: string): Promise<NormalizedSpan | null> {+ const id = normalizeOtlpId(spanId) || spanId;+ const cached = lookupIndexedSpan(this.descriptor.id, id);+ if (cached) {+ return cached;+ }++ // Tempo has no direct span API. Prefer OpenPlait TraceQL by hex span id+ // (Grafana Explore pattern: resolve trace, then fetch once).+ // Tempo's search endpoint otherwise uses its short default lookback,+ // which makes a valid span appear missing when the detail request is+ // made after the list query. Keep the span-only fallback aligned with+ // the normal observability lookback.+ const end = new Date();+ const lookbackMs = this.maxTimeRangeMs ?? 30 * 24 * 60 * 60 * 1_000;+ const start = new Date(end.getTime() - lookbackMs);+ const traceql = `{ span:id = ${traceqlValue(id)} }`;+ try {+ const traceIds = await this.openPlaitTraceSearch(+ traceql,+ { start, end },+ 1+ );+ const traceId = traceIds[0];+ if (!traceId) {+ return null;+ }+ const spans = await this.getTraceSpans(traceId);+ const result =+ spans.find((s) => s.spanId === id || s.spanId === spanId) || null;+ return result;+ } catch {+ return null;+ }+ }++ /**+ * Download full OTLP for each TraceQL hit (needed for events / gen_ai+ * bodies). Fetches in parallel with a concurrency cap — Grafana Explore+ * only loads one trace on click; we still need samples for list/agents.+ */+ private async fetchSampledSpans(+ query: OpenLITQuery,+ maxTraces: number+ ): Promise<NormalizedSpan[]> {+ // Keep a reserve of candidates so an oversized/deleted trace does not+ // collapse the list. Grafana Cloud can return a valid search hit whose+ // full OTLP payload exceeds its per-trace response limit (HTTP 422).+ const candidateLimit = Math.min(+ MAX_TRACE_FETCH,+ Math.max(maxTraces, maxTraces * 2)+ );+ const ids = await this.searchTraceIds(query, candidateLimit);+ const perTrace = await mapPool(+ ids,+ TRACE_FETCH_CONCURRENCY,+ async (id) => {+ try {+ return await this.getTraceSpans(id);+ } catch (error) {+ const status = adapterErrorStatus(error);+ if (status !== 404 && status !== 413 && status !== 422) throw error;+ return [];+ }+ }+ );+ return perTrace.filter((spans) => spans.length > 0).slice(0, maxTraces).flat();+ }++ async listSpans(query: OpenLITQuery): Promise<DataFrame<NormalizedSpan>> {+ const start = Date.now();+ const pageSize = Math.min(query.limit || 20, MAX_TRACE_FETCH);+ const offset = Math.max(0, query.offset || 0);+ // Prefer TraceQL search summaries for the Telemetry list (one row per+ // trace). Full OTLP downloads are reserved for detail / graph sample+ // paths — Explore also only loads a full tree on click.+ const { rows: summaries, truncated, effectiveLimit } =+ await this.traceSummaryRows(query);+ const page = summaries.slice(offset, offset + pageSize);+ const rows: NormalizedSpan[] = page.map((row) => {+ const traceId = normalizeOtlpId(String(row["trace.id"] || ""));+ return {+ traceId,+ spanId: traceId,+ parentSpanId: "",+ name: String(row["root.span.name"] || ""),+ serviceName: String(row["root.service.name"] || ""),+ timestamp: String(row.timestamp || ""),+ durationNs: Math.max(0, Number(row.duration || 0) * 1_000_000),+ statusCode: "",+ spanAttributes: {},+ resourceAttributes: {},+ };+ });+ return {+ fields: [],+ rows,+ meta: {+ latencyMs: Date.now() - start,+ rowsScanned: summaries.length,+ truncated: truncated || summaries.length >= effectiveLimit,+ degraded: ["serverAggregation"],+ },+ };+ }++ async sampleTracesForGraph(+ query: OpenLITQuery,+ maxTraces: number+ ): Promise<NormalizedSpan[]> {+ // Stratification (multi-service fan-out) lives in shared+ // `fetchSpansForAggregation` so all L1 backends benefit. Per-service+ // calls land here with a single service.name filter.+ return this.fetchSampledSpans(query, Math.min(maxTraces, MAX_TRACE_FETCH));+ }++ /**+ * Server-side aggregation via Tempo TraceQL metrics (accurate over the whole+ * window), falling back to the bounded L1 sample only when metrics are+ * unavailable. This is what stops dashboards from reporting the 200-trace+ * sample cap as the "total".+ */+ async aggregateSpans(query: OpenLITQuery): Promise<DataFrame> {+ try {+ const native = await this.nativeAggregate(query);+ if (native) return native;+ } catch {+ // Any metrics failure -> sample fallback below.+ }+ return computeAggregateSpansL1(this, query);+ }++ private async traceSummaryRows(+ query: OpenLITQuery+ ): Promise<{+ rows: Record<string, unknown>[];+ truncated: boolean;+ effectiveLimit: number;+ }> {+ const boundedQuery = clampQueryToSource(this, query).query;+ const traceql = buildTempoSearchQuery(boundedQuery, {+ mostRecent:+ this.descriptor.settings.enableMostRecent === true ||+ this.cachedTempoProfile()?.features.mostRecent === true,+ });+ const key = cacheKey(this.descriptor.id, [+ "openplait-trace-summaries",+ traceql,+ boundedQuery.timeRange.start.toISOString(),+ boundedQuery.timeRange.end.toISOString(),+ ]);+ const rows = await cachedQuery(key, TTL_MS, () =>+ this.openPlaitTraceSearchRows(+ traceql,+ boundedQuery.timeRange,+ MAX_TRACE_SEARCH+ )+ );+ const effectiveLimit = this.searchResultLimit(MAX_TRACE_SEARCH);+ return {+ rows: rows.slice(0, TRACE_SUMMARY_CAP),+ // Tempo search has no offset/cursor. Hitting either OpenLIT's cap or+ // the negotiated server cap means the result must be presented as a+ // bounded sample, even when the backend returned exactly that limit.+ truncated:+ rows.length > TRACE_SUMMARY_CAP ||+ (effectiveLimit <= TRACE_SUMMARY_CAP && rows.length >= effectiveLimit),+ effectiveLimit,+ };+ }++ /** Trace-row count for stable Tempo list pagination (not child-span count). */+ async countTraces(+ query: OpenLITQuery+ ): Promise<{ total: number; truncated: boolean }> {+ const result = await this.traceSummaryRows(query);+ return { total: result.rows.length, truncated: result.truncated };+ }++ /** Exact matching span count for list pagination, independent of the sample cap. */+ async countSpans(query: OpenLITQuery): Promise<number | null> {+ const boundedQuery = clampQueryToSource(this, query).query;+ const series = await this.fetchMetricsSeries(+ `${buildTempoSearchQuery(boundedQuery)} | count_over_time()`,+ boundedQuery.timeRange,+ metricsStepForQuery(boundedQuery)+ );+ if (!series) return null;+ return series.reduce(+ (total, item) =>+ total + seriesBuckets(item).reduce((sum, bucket) => sum + bucket.value, 0),+ 0+ );+ }++ /**+ * Build the Telemetry trace-volume chart from Tempo search summaries.+ * Search already returns one row per trace with its start time and duration,+ * so this stays trace-correct and avoids downloading every child span when+ * TraceQL metrics are unavailable.+ */+ async traceTimeSeries(query: OpenLITQuery): Promise<DataFrame> {+ const startedAt = Date.now();+ const { rows, truncated, effectiveLimit } = await this.traceSummaryRows(query);+ const summaries: NormalizedSpan[] = rows+ .map((row) => {+ const traceId = normalizeOtlpId(String(row["trace.id"] || ""));+ return {+ traceId,+ spanId: traceId,+ parentSpanId: "",+ name: String(row["root.span.name"] || ""),+ serviceName: String(row["root.service.name"] || ""),+ timestamp: String(row.timestamp || ""),+ durationNs: Math.max(0, Number(row.duration || 0) * 1_000_000),+ statusCode: "",+ spanAttributes: {},+ resourceAttributes: {},+ };+ })+ .filter((span) => span.traceId && !Number.isNaN(Date.parse(span.timestamp)));+ const frame = bucketSpansByInterval(+ summaries,+ query.interval || "1h",+ query.aggregations || [{ fn: "count" }],+ query.timeRange+ );+ return {+ ...frame,+ meta: {+ ...frame.meta,+ truncated,+ rowsScanned: summaries.length,+ latencyMs: Date.now() - startedAt,+ freshness: truncated ? "sampled" : "live",+ ...(truncated ? { degraded: ["traceSummaryLimit"] } : {}),+ },+ };+ }++ async spanTimeSeries(query: OpenLITQuery): Promise<DataFrame> {+ try {+ const native = await this.nativeSpanTimeSeries(query);+ if (native) return native;+ } catch {+ // fall through to sample compute+ }+ return computeSpanTimeSeriesL1(this, query);+ }++ /** GET `/api/metrics/query_range` and return raw series (null on failure). */+ private async fetchMetricsSeries(+ metricQuery: string,+ timeRange: QueryTimeRange,+ step: string+ ): Promise<TempoMetricsSeries[] | null> {+ if (this.cachedTempoProfile()?.features.traceqlMetrics === false) return null;+ const explicitMs = Number(this.descriptor.settings.metricsMaxTimeRangeMs);+ const explicitHours = Number(+ this.descriptor.settings.metricsMaxTimeRangeHours+ );+ const maxWindowMs =+ Number.isFinite(explicitMs) && explicitMs > 0+ ? explicitMs+ : Number.isFinite(explicitHours) && explicitHours > 0+ ? explicitHours * 60 * 60 * 1000+ : DEFAULT_TEMPO_METRICS_WINDOW_MS;+ const windows = splitTempoMetricWindows(timeRange, maxWindowMs);+ const key = cacheKey(this.descriptor.id, [+ "openplait-metrics-range",+ metricQuery,+ timeRange.start.toISOString(),+ timeRange.end.toISOString(),+ step,+ maxWindowMs,+ ]);+ try {+ return await cachedQuery(key, TTL_MS, async () => {+ const adapter = await this.openPlaitAdapter();+ const windowResults = await mapPool(+ windows,+ TRACE_FETCH_CONCURRENCY,+ async (window) => {+ const result = await adapter.queryMetrics(+ {+ query: metricQuery,+ from: window.start.toISOString(),+ to: window.end.toISOString(),+ step,+ },+ {+ audit: {+ requestId: safeRequestId(this.descriptor.id, "metrics"),+ },+ }+ );+ return openPlaitFramesToRows(result.frames);+ }+ );+ return normalizedMetricRowsToSeries(windowResults.flat());+ });+ } catch (error) {+ if (adapterErrorStatus(error) === 404) {+ const profile = this.cachedTempoProfile();+ if (profile) {+ this.rememberTempoProfile({+ ...profile,+ features: { ...profile.features, traceqlMetrics: false },+ });+ }+ }+ return null;+ }+ }++ /**+ * Compute count / sum / avg (optionally grouped) via TraceQL metrics. Returns+ * null when the anchor count query can't run so the caller can fall back to+ * the L1 sample. An empty result (no matching spans) is a valid "0", not a+ * fallback trigger.+ */+ private async nativeAggregate(+ query: OpenLITQuery+ ): Promise<DataFrame | null> {+ const aggregations = query.aggregations?.length+ ? query.aggregations+ : [{ fn: "count" as const, as: "count" }];+ const groupField = query.groupBy?.[0];+ const groupAttr = groupField ? metricGroupByAttr(groupField) : null;+ const selector = buildTempoSearchQuery(query);+ const step = metricsStepForQuery(query);+ const byClause = groupAttr ? ` by (${groupAttr})` : "";++ const sumSeries = async (expr: string) => {+ const series = await this.fetchMetricsSeries(+ `${selector} | ${expr}${byClause}`,+ query.timeRange,+ step+ );+ if (!series) return null;+ const totals = new Map<string, number>();+ for (const s of series) {+ const gv = seriesLabelValue(s, groupAttr);+ const total = seriesBuckets(s).reduce((a, b) => a + b.value, 0);+ totals.set(gv, (totals.get(gv) ?? 0) + total);+ }+ return totals;+ };++ // Anchor on count: it both establishes the groups and weights averages.+ const countTotals = await sumSeries("count_over_time()");+ if (!countTotals) return null;++ const groups = groupField+ ? Array.from(countTotals.keys())+ : [""];+ const rows: Record<string, unknown>[] = groups.map((gv) => {+ const row: Record<string, unknown> = { group_value: gv };+ if (groupField) row[groupField] = gv;+ return row;+ });+ const rowFor = (gv: string) =>+ rows.find((r) => r.group_value === gv) ?? rows[0];++ for (const agg of aggregations) {+ const as = agg.as || agg.fn;+ if (agg.fn === "count") {+ for (const gv of groups) rowFor(gv)[as] = countTotals.get(gv) ?? 0;+ continue;+ }+ if (agg.fn === "avg") {+ // Weighted average over the window: sum(field) / count.+ const attr = metricAttrRef(agg.field);+ const sums = attr ? await sumSeries(`sum_over_time(${attr})`) : null;+ for (const gv of groups) {+ const count = countTotals.get(gv) ?? 0;+ const sum = sums?.get(gv) ?? 0;+ let v = count > 0 ? sum / count : 0;+ if (agg.field === "duration") v = v / 1e9; // ns -> seconds+ rowFor(gv)[as] = v;+ }+ continue;+ }+ // sum / min / max+ const expr = aggregationToMetricExpr(agg.fn, agg.field);+ const totals = expr ? await sumSeries(expr) : null;+ for (const gv of groups) {+ let v = totals?.get(gv) ?? 0;+ if (agg.field === "duration") v = v / 1e9;+ rowFor(gv)[as] = v;+ }+ }++ return {+ fields: [],+ rows,+ meta: { freshness: "live", truncated: false },+ };+ }++ /**+ * Per-bucket time series via TraceQL metrics. Count is the anchor; cost /+ * tokens / duration are merged onto the same buckets (identical step keeps+ * timestamps aligned). `avg_over_time` is already a per-bucket average, so no+ * weighting is needed here.+ */+ private async nativeSpanTimeSeries(+ query: OpenLITQuery+ ): Promise<DataFrame | null> {+ const selector = buildTempoSearchQuery(query);+ const step = metricsStepForQuery(query);+ const aggregations = query.aggregations?.length+ ? query.aggregations+ : [{ fn: "count" as const, as: "count" }];++ const countSeries = await this.fetchMetricsSeries(+ `${selector} | count_over_time()`,+ query.timeRange,+ step+ );+ if (!countSeries) return null;++ const buckets = new Map<number, Record<string, number>>();+ const bucketAt = (ts: number) => {+ let b = buckets.get(ts);+ if (!b) {+ b = {};+ buckets.set(ts, b);+ }+ return b;+ };+ for (const s of countSeries) {+ for (const p of seriesBuckets(s)) {+ bucketAt(p.ts).count = (bucketAt(p.ts).count ?? 0) + p.value;+ }+ }++ for (const agg of aggregations) {+ if (agg.fn === "count") continue;+ const expr = aggregationToMetricExpr(agg.fn, agg.field);+ if (!expr) continue;+ const series = await this.fetchMetricsSeries(+ `${selector} | ${expr}`,+ query.timeRange,+ step+ );+ if (!series) continue;+ const as = agg.as || agg.fn;+ const perTs = new Map<number, number[]>();+ for (const s of series) {+ for (const p of seriesBuckets(s)) {+ const arr = perTs.get(p.ts) ?? [];+ arr.push(p.value);+ perTs.set(p.ts, arr);+ }+ }+ for (const [ts, arr] of Array.from(perTs.entries())) {+ let v =+ agg.fn === "avg"+ ? arr.reduce((a, b) => a + b, 0) / arr.length+ : arr.reduce((a, b) => a + b, 0);+ if (agg.field === "duration") v = v / 1e9;+ bucketAt(ts)[as] = v;+ }+ }++ const sorted = Array.from(buckets.keys()).sort((a, b) => a - b);+ const rows = sorted.map((ts) => {+ const iso = new Date(ts).toISOString();+ return {+ bucket: iso,+ label: iso,+ request_time: iso,+ count: 0,+ ...buckets.get(ts),+ };+ });+ return {+ fields: [],+ rows,+ meta: { freshness: "live", truncated: false },+ };+ }++ async distinctValues(key: string, query: OpenLITQuery): Promise<string[]> {+ if (key === "service.name" || key === "ServiceName") {+ try {+ const discovered = await this.discoverServices(query.timeRange);+ if (discovered.length) {+ return discovered.map((d) => d.serviceName).filter(Boolean);+ }+ } catch {+ // Fall through to L1 sample.+ }+ }+ return computeDistinctValuesL1(this, key, query);+ }++ /**+ * Enumerate tag values via Tempo's search-tag APIs (v2 then v1).+ * This is how we discover *all* services in the window without depending+ * on a recency-biased TraceQL search sample.+ */+ private async searchTagValues(+ tag: string,+ window: QueryTimeRange,+ traceQlFilter?: string+ ): Promise<string[]> {+ const boundedWindow = clampQueryToSource(this, {+ signal: "traces",+ timeRange: window,+ limit: 50,+ }).query.timeRange;+ try {+ const key = cacheKey(this.descriptor.id, [+ "openplait-tag-values",+ tag,+ boundedWindow.start.toISOString(),+ boundedWindow.end.toISOString(),+ traceQlFilter || "",+ ]);+ const values = await cachedQuery(key, TTL_MS, async () => {+ const adapter = await this.openPlaitAdapter();+ return adapter.discoverTagValues(+ tag,+ { timeoutMs: 15_000 },+ {+ ...(traceQlFilter ? { query: traceQlFilter } : {}),+ range: {+ from: boundedWindow.start.toISOString(),+ to: boundedWindow.end.toISOString(),+ },+ }+ );+ });+ if (values.length) return Array.from(new Set(values)).slice(0, 50);+ } catch {+ // Older Tempo releases may not expose the v2 endpoint; use v1 below.+ }++ const { headers, redact } = await this.authHeaders();+ try {+ const url = new URL(+ `${this.baseUrl}/api/search/tag/${encodeURIComponent(tag)}/values`+ );+ url.searchParams.set(+ "start",+ String(Math.floor(boundedWindow.start.getTime() / 1000))+ );+ url.searchParams.set(+ "end",+ String(Math.floor(boundedWindow.end.getTime() / 1000))+ );+ url.searchParams.set("limit", "50");+ const response = await safeFetch<{ tagValues?: string[] }>(url.toString(), {+ headers,+ ...this.networkOpts,+ redactValues: redact,+ timeoutMs: 15_000,+ });+ return Array.from(new Set(response?.tagValues || [])).filter(Boolean);+ } catch {+ return [];+ }+ }++ async discoverServices(window: QueryTimeRange): Promise<DiscoveredService[]> {+ const aiFilter = tempoAISelectorQuery();+ let names = await this.searchTagValues(+ "resource.service.name",+ window,+ aiFilter+ );+ if (!names.length) {+ names = await this.searchTagValues("service.name", window, aiFilter);+ }+ if (!names.length) {+ names = await this.searchTagValues("resource.service.name", window);+ }+ if (!names.length) {+ names = await this.searchTagValues("service.name", window);+ }++ if (names.length > 0) {+ return names.slice(0, 50).map((serviceName) => ({+ serviceName,+ environment: "default",+ clusterId: "default",+ }));+ }++ // Fallback: biased sample (last resort when tag APIs are disabled).+ const spans = await this.fetchSampledSpans(+ { signal: "traces", timeRange: window, aiSelector: true, limit: 100 },+ 100+ );+ const byService = new Map<string, DiscoveredService>();+ for (const span of spans) {+ const name = span.serviceName || span.resourceAttributes["service.name"];+ if (!name || byService.has(name)) continue;+ byService.set(name, {+ serviceName: name,+ environment:+ span.resourceAttributes["deployment.environment"] || "default",+ clusterId: span.resourceAttributes["k8s.cluster.name"] || "default",+ sdkName: span.resourceAttributes["telemetry.sdk.name"],+ sdkLanguage: span.resourceAttributes["telemetry.sdk.language"],+ sdkVersion: span.resourceAttributes["telemetry.sdk.version"],+ firstSeen: span.timestamp,+ lastSeen: span.timestamp,+ });+ }+ return Array.from(byService.values());+ }++ async aggregateByService(window: QueryTimeRange): Promise<ServiceRollup[]> {+ const spans = await this.fetchSampledSpans(+ { signal: "traces", timeRange: window, aiSelector: true, limit: 100 },+ 100+ );+ const byKey = new Map<+ string,+ ServiceRollup & { modelSet: Set<string>; providerSet: Set<string> }+ >();+ for (const span of spans) {+ const serviceName =+ span.serviceName || span.resourceAttributes["service.name"] || "";+ if (!serviceName) continue;+ const environment =+ span.resourceAttributes["deployment.environment"] || "default";+ const clusterId =+ span.resourceAttributes["k8s.cluster.name"] || "default";+ const key = `${clusterId}|${environment}|${serviceName}`;+ let row = byKey.get(key);+ if (!row) {+ row = {+ serviceName,+ environment,+ clusterId,+ requestCount: 0,+ models: [],+ providers: [],+ modelSet: new Set(),+ providerSet: new Set(),+ };+ byKey.set(key, row);+ }+ row.requestCount += 1;+ const model = span.spanAttributes["gen_ai.request.model"];+ const provider = span.spanAttributes["gen_ai.system"];+ if (model) row.modelSet.add(model);+ if (provider) row.providerSet.add(provider);+ }+ return Array.from(byKey.values()).map(+ ({ modelSet, providerSet, ...rest }) => ({+ ...rest,+ models: Array.from(modelSet),+ providers: Array.from(providerSet),+ })+ );+ }++ async validateAISignal(window: QueryTimeRange): Promise<AISignalValidation> {+ try {+ const ids = await this.searchTraceIds(+ { signal: "traces", timeRange: window, aiSelector: true },+ 1+ );+ return { ok: ids.length > 0, sampleCount: ids.length, missingAttributes: [] };+ } catch (err) {+ return {+ ok: false,+ sampleCount: 0,+ missingAttributes: [],+ message: String((err as Error)?.message || err),+ };+ }+ }+}++export const tempoAdapterFactory = {+ type: "tempo",+ create: (descriptor: TelemetrySourceDescriptor) => new TempoAdapter(descriptor),+ describe: (): SourceTypeDescriptor => ({+ type: "tempo",+ displayName: "Grafana Tempo",+ declaredSignals: ["traces"],+ capabilities: {+ traceTree: true,+ spanEvents: true,+ serverAggregation: false,+ spanMutation: false,+ distinctValues: true,+ crossTraceSession: false,+ rawQuery: false,+ },+ correlation: {+ crossSignal: true,+ keys: ["traceId", "spanId", "service"],+ },+ configFields: [+ ...httpVendorFields({+ placeholder: "https://tempo-prod-xxx.grafana.net/tempo",+ tenant: true,+ }),+ {+ key: "maxTimeRangeDays",+ label: getMessage().DATA_SOURCE_FIELD_MAX_TIME_RANGE_DAYS,+ kind: "text",+ group: "settings",+ placeholder:+ getMessage().DATA_SOURCE_FIELD_MAX_TIME_RANGE_DAYS_PLACEHOLDER,+ },+ {+ key: "tempoVersion",+ label: "Tempo version (optional)",+ kind: "text",+ group: "settings",+ placeholder: "2.8.0 (auto-detected by health check)",+ },+ {+ key: "enableMostRecent",+ label: "Enable experimental most-recent search hint",+ kind: "switch",+ group: "settings",+ defaultValue: false,+ },+ {+ key: "metricsMaxTimeRangeHours",+ label: "TraceQL metrics window hours",+ kind: "text",+ group: "settings",+ placeholder: "24",+ },+ ],+ authStyle: "http",+ authHelp: getMessage().DATA_SOURCE_AUTH_HELP_HTTP,+ }),+};diff --git a/src/client/src/lib/platform/connectors/datasource/graph/aggregate-dag.ts b/src/client/src/lib/platform/connectors/datasource/graph/aggregate-dag.tsnew file mode 100644index 00000000..0a719160--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/graph/aggregate-dag.ts@@ -0,0 +1,110 @@+/**+ * Build an aggregate agent DAG in-process from sampled full traces.+ *+ * The agent aggregate-graph (`getAggregateGraph`) uses ClickHouse's self-join+ * for the built-in source and this in-process reconstruction for external+ * trace sources, fed by `adapter.sampleTracesForGraph`.+ *+ * ClickHouse builds this via a self-join on `ParentSpanId` within `TraceId`+ * plus `GROUP BY SpanName` + `quantile`/`countIf`. Vendors without server-side+ * aggregation (Tempo, Jaeger) cannot express that, so their adapters fetch a+ * bounded sample of full traces and this function reproduces the DAG: nodes+ * keyed by span name with call counts + duration quantiles, and edges keyed by+ * (parentName -> childName) with call counts.+ */++import type { NormalizedSpan } from "../types";++export interface DagNode {+ name: string;+ count: number;+ errorCount: number;+ p50DurationMs: number;+ p95DurationMs: number;+ totalCost: number;+}++export interface DagEdge {+ from: string;+ to: string;+ count: number;+}++export interface AggregateDag {+ nodes: DagNode[];+ edges: DagEdge[];+ sampledTraces: number;+ sampledSpans: number;+}++function quantile(sortedNs: number[], q: number): number {+ if (sortedNs.length === 0) return 0;+ const idx = Math.min(+ sortedNs.length - 1,+ Math.max(0, Math.floor(q * (sortedNs.length - 1)))+ );+ return sortedNs[idx];+}++const isError = (span: NormalizedSpan): boolean =>+ /error/i.test(span.statusCode || "");++/** Build the aggregate DAG from a flat list of sampled spans. */+export function buildAggregateDag(spans: NormalizedSpan[]): AggregateDag {+ const byId = new Map<string, NormalizedSpan>();+ const traceIds = new Set<string>();+ for (const s of spans) {+ if (s.spanId) byId.set(s.spanId, s);+ if (s.traceId) traceIds.add(s.traceId);+ }++ const nodeDurations = new Map<string, number[]>();+ const nodeCount = new Map<string, number>();+ const nodeErrors = new Map<string, number>();+ const nodeCost = new Map<string, number>();+ const edgeCount = new Map<string, number>();++ for (const span of spans) {+ const name = span.name || "(unnamed)";+ nodeCount.set(name, (nodeCount.get(name) || 0) + 1);+ if (isError(span)) nodeErrors.set(name, (nodeErrors.get(name) || 0) + 1);+ if (typeof span.cost === "number") {+ nodeCost.set(name, (nodeCost.get(name) || 0) + span.cost);+ }+ const durations = nodeDurations.get(name) || [];+ durations.push(span.durationNs || 0);+ nodeDurations.set(name, durations);++ // Edge: parent span (same trace) -> this span.+ const parent = span.parentSpanId ? byId.get(span.parentSpanId) : undefined;+ if (parent && parent.traceId === span.traceId) {+ const from = parent.name || "(unnamed)";+ const key = `${from}\u0000${name}`;+ edgeCount.set(key, (edgeCount.get(key) || 0) + 1);+ }+ }++ const nodes: DagNode[] = Array.from(nodeCount.keys()).map((name) => {+ const sorted = (nodeDurations.get(name) || []).slice().sort((a, b) => a - b);+ return {+ name,+ count: nodeCount.get(name) || 0,+ errorCount: nodeErrors.get(name) || 0,+ p50DurationMs: quantile(sorted, 0.5) / 1e6,+ p95DurationMs: quantile(sorted, 0.95) / 1e6,+ totalCost: nodeCost.get(name) || 0,+ };+ });++ const edges: DagEdge[] = Array.from(edgeCount.entries()).map(([key, count]) => {+ const [from, to] = key.split("\u0000");+ return { from, to, count };+ });++ return {+ nodes,+ edges,+ sampledTraces: traceIds.size,+ sampledSpans: spans.length,+ };+}diff --git a/src/client/src/lib/platform/connectors/datasource/graph/map-pool.ts b/src/client/src/lib/platform/connectors/datasource/graph/map-pool.tsnew file mode 100644index 00000000..a555fc58--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/graph/map-pool.ts@@ -0,0 +1,24 @@+/**+ * Run async work over `items` with at most `concurrency` in flight.+ * Preserves result order.+ */+export async function mapPool<T, R>(+ items: T[],+ concurrency: number,+ fn: (item: T, index: number) => Promise<R>+): Promise<R[]> {+ if (items.length === 0) return [];+ const results = new Array<R>(items.length);+ let next = 0;+ const workers = Array.from(+ { length: Math.min(concurrency, items.length) },+ async () => {+ while (next < items.length) {+ const i = next++;+ results[i] = await fn(items[i], i);+ }+ }+ );+ await Promise.all(workers);+ return results;+}diff --git a/src/client/src/lib/platform/connectors/datasource/graph/sample-aggregate.ts b/src/client/src/lib/platform/connectors/datasource/graph/sample-aggregate.tsnew file mode 100644index 00000000..b172d26f--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/graph/sample-aggregate.ts@@ -0,0 +1,413 @@+import type {+ Aggregation,+ AggregationFn,+ DataFrame,+ NormalizedSpan,+} from "../types";++const TOKEN_ATTR_KEYS = [+ "gen_ai.usage.total_tokens",+ "gen_ai.client.token.usage",+ "total_tokens",+] as const;++const COST_ATTR_KEYS = ["gen_ai.usage.cost", "coding_agent.session.cost_usd"] as const;++const ROOT_PARENT_IDS = new Set(["", "0".repeat(16)]);++function asNumber(value: unknown): number | undefined {+ if (typeof value === "number" && Number.isFinite(value)) return value;+ if (typeof value === "string" && value.trim() !== "") {+ const n = Number(value);+ if (Number.isFinite(n)) return n;+ }+ return undefined;+}++function lookupAttr(+ attrs: Record<string, string>,+ keys: readonly string[]+): string | undefined {+ for (const key of keys) {+ const v = attrs[key];+ if (v !== undefined && v !== "") return v;+ }+ return undefined;+}++/** Resolve a numeric or string field from a normalized span for aggregation/groupBy. */+export function spanFieldValue(+ span: NormalizedSpan,+ field: string+): number | string | undefined {+ const f = field.trim();+ if (!f) return undefined;+ const scoped = f.match(/^(span|resource):(.+)$/);+ if (scoped) {+ const [, scope, key] = scoped;+ return scope === "resource"+ ? span.resourceAttributes[key]+ : span.spanAttributes[key];+ }+ const lower = f.toLowerCase();++ if (+ lower === "duration" ||+ f === "Duration" ||+ f === "requestDuration"+ ) {+ return (span.durationNs || 0) / 1e9;+ }+ if (f === "durationNs") return span.durationNs;++ if (lower === "cost" || COST_ATTR_KEYS.includes(f as (typeof COST_ATTR_KEYS)[number])) {+ if (typeof span.cost === "number" && Number.isFinite(span.cost)) {+ return span.cost;+ }+ const raw =+ span.spanAttributes[f] ??+ lookupAttr(span.spanAttributes, COST_ATTR_KEYS) ??+ span.resourceAttributes[f];+ return asNumber(raw);+ }++ if (+ lower === "tokens" ||+ lower === "totaltokens" ||+ lower === "total_tokens" ||+ TOKEN_ATTR_KEYS.includes(f as (typeof TOKEN_ATTR_KEYS)[number])+ ) {+ const raw =+ span.spanAttributes[f] ??+ lookupAttr(span.spanAttributes, TOKEN_ATTR_KEYS) ??+ span.resourceAttributes[f];+ return asNumber(raw);+ }++ if (f === "SpanName" || f === "spanName" || f === "name") return span.name;+ if (+ f === "ServiceName" ||+ f === "serviceName" ||+ f === "service.name"+ ) {+ return span.serviceName || span.resourceAttributes["service.name"];+ }+ if (f === "StatusCode" || f === "statusCode") return span.statusCode;+ if (f === "TraceId" || f === "traceId") return span.traceId;+ if (f === "SpanId" || f === "spanId") return span.spanId;+ if (f === "ParentSpanId" || f === "parentSpanId") return span.parentSpanId;++ if (Object.prototype.hasOwnProperty.call(span.spanAttributes, f)) {+ return span.spanAttributes[f];+ }+ if (Object.prototype.hasOwnProperty.call(span.resourceAttributes, f)) {+ return span.resourceAttributes[f];+ }++ return undefined;+}++type IntervalUnit = "minute" | "hour" | "day" | "week" | "month";++function parseIntervalUnit(interval: string): IntervalUnit {+ const trimmed = (interval || "1h").trim();+ // Month uses capital M ("1M"); lowercase "1m" means minute.+ if (trimmed === "month" || /^[0-9]+M$/.test(trimmed)) return "month";+ const raw = trimmed.toLowerCase();+ if (raw === "week" || /^[0-9]+w$/.test(raw)) return "week";+ if (raw === "day" || /^[0-9]+d$/.test(raw)) return "day";+ if (raw === "hour" || /^[0-9]+h$/.test(raw)) return "hour";+ if (raw === "minute" || raw === "m" || /^[0-9]+m$/.test(raw)) return "minute";+ return "hour";+}++function truncateToBucket(date: Date, unit: IntervalUnit): Date {+ const d = new Date(date.getTime());+ d.setUTCMilliseconds(0);+ d.setUTCSeconds(0);+ if (unit === "minute") return d;+ d.setUTCMinutes(0);+ if (unit === "hour") return d;+ d.setUTCHours(0);+ if (unit === "day") return d;+ if (unit === "week") {+ // Use ISO-style Monday boundaries so a 90-day view produces stable+ // weekly buckets instead of falling back to an hour and truncating at 168.+ d.setUTCDate(d.getUTCDate() - ((d.getUTCDay() + 6) % 7));+ return d;+ }+ d.setUTCDate(1);+ return d;+}++function pad2(n: number): string {+ return n < 10 ? `0${n}` : String(n);+}++function formatBucketLabel(bucket: Date, unit: IntervalUnit): string {+ const y = bucket.getUTCFullYear();+ const mo = pad2(bucket.getUTCMonth() + 1);+ const day = pad2(bucket.getUTCDate());+ const h = pad2(bucket.getUTCHours());+ if (unit === "month") return `${y}/${mo}`;+ if (unit === "day" || unit === "week") return `${y}/${mo}/${day}`;+ if (unit === "hour") return `${mo}/${day} ${h}:00`;+ return `${mo}/${day} ${h}:${pad2(bucket.getUTCMinutes())}`;+}++function aggAlias(agg: Aggregation, index: number): string {+ const alias = String(agg.as || `agg${index}`);+ // Reject prototype-polluting / non-identifier keys before using as object props.+ if (+ alias === "__proto__" ||+ alias === "constructor" ||+ alias === "prototype" ||+ !/^[A-Za-z_][A-Za-z0-9_.:-]*$/.test(alias)+ ) {+ return `agg${index}`;+ }+ return alias;+}++function safeGroupKey(key: string, index: number): string {+ if (+ key === "__proto__" ||+ key === "constructor" ||+ key === "prototype" ||+ !/^[A-Za-z_][A-Za-z0-9_.:-]*$/.test(key)+ ) {+ return `group${index}`;+ }+ return key;+}++function percentile(sorted: number[], q: number): number {+ if (sorted.length === 0) return 0;+ const idx = Math.min(+ sorted.length - 1,+ Math.max(0, Math.floor(q * (sorted.length - 1)))+ );+ return sorted[idx];+}++function collectFieldNumbers(+ spans: NormalizedSpan[],+ field: string | undefined+): number[] {+ if (!field) return [];+ const out: number[] = [];+ for (const span of spans) {+ const v = asNumber(spanFieldValue(span, field));+ if (v !== undefined) out.push(v);+ }+ return out;+}++function applyAggregation(+ spans: NormalizedSpan[],+ agg: Aggregation+): number {+ const fn: AggregationFn = agg.fn;+ if (fn === "count") return spans.length;++ if (fn === "cardinality") {+ const seen = new Set<string>();+ for (const span of spans) {+ const v = spanFieldValue(span, agg.field || "");+ if (v === undefined || v === "") continue;+ seen.add(String(v));+ }+ return seen.size;+ }++ const values = collectFieldNumbers(spans, agg.field);+ if (values.length === 0) return 0;++ switch (fn) {+ case "sum":+ return values.reduce((a, b) => a + b, 0);+ case "avg":+ return values.reduce((a, b) => a + b, 0) / values.length;+ case "min":+ return Math.min(...values);+ case "max":+ return Math.max(...values);+ case "p50":+ return percentile(values.slice().sort((a, b) => a - b), 0.5);+ case "p90":+ return percentile(values.slice().sort((a, b) => a - b), 0.9);+ case "p95":+ return percentile(values.slice().sort((a, b) => a - b), 0.95);+ case "p99":+ return percentile(values.slice().sort((a, b) => a - b), 0.99);+ default:+ return 0;+ }+}++function applyAggregations(+ spans: NormalizedSpan[],+ aggregations: Aggregation[]+): Record<string, number> {+ const aggs =+ aggregations.length > 0 ? aggregations : [{ fn: "count" as const }];+ const row: Record<string, number> = Object.create(null);+ aggs.forEach((agg, i) => {+ row[aggAlias(agg, i)] = applyAggregation(spans, agg);+ });+ return row;+}++function parseSpanTime(span: NormalizedSpan): Date | null {+ if (!span.timestamp) return null;+ const d = new Date(span.timestamp);+ return Number.isNaN(d.getTime()) ? null : d;+}++/** Bucket spans by interval and compute aggregations per bucket. */+export function bucketSpansByInterval(+ spans: NormalizedSpan[],+ interval: string,+ aggregations: Aggregation[],+ timeRange?: { start: Date; end: Date }+): DataFrame {+ const unit = parseIntervalUnit(interval);+ const byBucket = new Map<number, NormalizedSpan[]>();++ for (const span of spans) {+ const t = parseSpanTime(span);+ if (!t) continue;+ const bucket = truncateToBucket(t, unit);+ const key = bucket.getTime();+ const list = byBucket.get(key) || [];+ list.push(span);+ byBucket.set(key, list);+ }++ let keys = Array.from(byBucket.keys()).sort((a, b) => a - b);++ // Zero-fill across the requested window so charts aren't a single spike+ // when the L1 sample lands in one bucket.+ if (timeRange?.start && timeRange?.end) {+ const startKey = truncateToBucket(timeRange.start, unit).getTime();+ const endKey = truncateToBucket(timeRange.end, unit).getTime();+ const filled: number[] = [];+ const stepMs =+ unit === "minute"+ ? 60_000+ : unit === "hour"+ ? 3_600_000+ : unit === "day"+ ? 86_400_000+ : unit === "week"+ ? 7 * 86_400_000+ : 0;+ const MAX_BUCKETS = 168; // 7d hourly+ if (stepMs > 0) {+ for (+ let t = startKey;+ t <= endKey && filled.length < MAX_BUCKETS;+ t += stepMs+ ) {+ filled.push(t);+ }+ keys = filled;+ } else {+ // Month: walk calendar months+ const cursor = new Date(startKey);+ while (cursor.getTime() <= endKey && filled.length < MAX_BUCKETS) {+ filled.push(cursor.getTime());+ cursor.setUTCMonth(cursor.getUTCMonth() + 1);+ }+ keys = filled;+ }+ }++ const emptyAggs = applyAggregations([], aggregations);+ const rows = keys.map((key) => {+ const bucketDate = new Date(key);+ const iso = bucketDate.toISOString();+ const groupSpans = byBucket.get(key) || [];+ return {+ bucket: iso,+ label: formatBucketLabel(bucketDate, unit),+ request_time: iso,+ ...(groupSpans.length+ ? applyAggregations(groupSpans, aggregations)+ : emptyAggs),+ };+ });++ return { fields: [], rows };+}++function groupKeyValue(span: NormalizedSpan, key: string): string {+ const v = spanFieldValue(span, key);+ if (v === undefined || v === null) return "";+ return String(v);+}++/** Group spans and compute aggregations in-process. */+export function aggregateSpansInProcess(+ spans: NormalizedSpan[],+ groupBy: string[],+ aggregations: Aggregation[]+): DataFrame {+ const keys = (groupBy || []).filter(Boolean);++ if (keys.length === 0) {+ return {+ fields: [],+ rows: [applyAggregations(spans, aggregations)],+ };+ }++ const groups = new Map<string, NormalizedSpan[]>();+ for (const span of spans) {+ const parts = keys.map((k) => groupKeyValue(span, k));+ const key = parts.join("\u0000");+ const list = groups.get(key) || [];+ list.push(span);+ groups.set(key, list);+ }++ const rows = Array.from(groups.entries()).map(([composite, groupSpans]) => {+ const parts = composite.split("\u0000");+ const row: Record<string, unknown> = Object.assign(Object.create(null), {+ ...applyAggregations(groupSpans, aggregations),+ group_value: parts[0] ?? "",+ });+ keys.forEach((k, i) => {+ row[safeGroupKey(k, i)] = parts[i] ?? "";+ });+ return row;+ });++ return { fields: [], rows };+}++/** Distinct non-empty values for an attribute / service.name / SpanName key. */+export function distinctFromSpans(+ spans: NormalizedSpan[],+ key: string+): string[] {+ const seen = new Set<string>();+ for (const span of spans) {+ const v = spanFieldValue(span, key);+ if (v === undefined || v === null || v === "") continue;+ seen.add(String(v));+ }+ return Array.from(seen).sort();+}++/** True when rows look like one root span per trace (listSpans Explore shape). */+export function looksLikeRootsOnly(spans: NormalizedSpan[]): boolean {+ if (spans.length === 0) return false;+ const traceIds = new Set<string>();+ for (const span of spans) {+ if (span.traceId) traceIds.add(span.traceId);+ const parent = span.parentSpanId || "";+ if (parent && !ROOT_PARENT_IDS.has(parent)) return false;+ }+ return traceIds.size === spans.length;+}diff --git a/src/client/src/lib/platform/connectors/datasource/graph/sample-fetch.ts b/src/client/src/lib/platform/connectors/datasource/graph/sample-fetch.tsnew file mode 100644index 00000000..5d02e315--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/graph/sample-fetch.ts@@ -0,0 +1,311 @@+/**+ * Bounded span sampling for L1 (in-process) aggregation.+ *+ * External trace backends without serverAggregation download a capped set of+ * full traces, then group/bucket in-process. This module owns the sample+ * budget and a short TTL cache so Telemetry summary + grouped + filter-config+ * requests share one sample instead of re-downloading the same traces.+ */++import type {+ DataFrame,+ DiscoveredService,+ NormalizedFilter,+ NormalizedSpan,+ OpenLITQuery,+ QueryTimeRange,+ SourceCapabilities,+} from "../types";+import { clampQueryToSource } from "../http/limits";+import { cacheKey, cachedQuery } from "../http/cache";+import { mapPool } from "./map-pool";+import { looksLikeRootsOnly } from "./sample-aggregate";++export interface SampleFetchSource {+ capabilities?(): SourceCapabilities;+ sampleTracesForGraph?(+ query: OpenLITQuery,+ maxTraces: number+ ): Promise<NormalizedSpan[]>;+ listSpans?(query: OpenLITQuery): Promise<DataFrame<NormalizedSpan>>;+ getTraceSpans?(traceId: string): Promise<NormalizedSpan[]>;+ discoverServices?(window: QueryTimeRange): Promise<DiscoveredService[]>;+ /**+ * When true, `sampleTracesForGraph` already fans out per service (e.g.+ * Jaeger). Shared stratification must not wrap it again.+ */+ samplesAreServiceStratified?: boolean;+ /** Stable id for cross-route L1 sample caching (usually source descriptor id). */+ sampleCacheKey?: string;+}++/** Cap for flat list / detail samples. */+export const DEFAULT_SAMPLE_TRACE_CAP = 100;+/** Cap for aggregate / timeseries / distinct L1 samples (shared across panels). */+export const AGGREGATE_SAMPLE_TRACE_CAP = 200;+export const DEFAULT_SAMPLE_CONCURRENCY = 6;+const MAX_SERVICES_IN_SAMPLE = 24;+const MIN_TRACES_PER_SERVICE = 8;+const STRATIFY_CONCURRENCY = 4;+/** Share L1 samples across summary/grouped/config for the poll window. */+const L1_SAMPLE_TTL_MS = 45_000;++function uniqueTraceIds(spans: NormalizedSpan[], maxTraces: number): string[] {+ const ids: string[] = [];+ const seen = new Set<string>();+ for (const span of spans) {+ const id = span.traceId;+ if (!id || seen.has(id)) continue;+ seen.add(id);+ ids.push(id);+ if (ids.length >= maxTraces) break;+ }+ return ids;+}++function extractServiceNames(filters: NormalizedFilter[] | undefined): string[] {+ const names: string[] = [];+ for (const filter of filters || []) {+ if (filter.target !== "attribute" || filter.key !== "service.name") continue;+ if (Array.isArray(filter.value)) {+ names.push(...filter.value.map(String).filter(Boolean));+ } else if (filter.value !== undefined && filter.value !== "") {+ names.push(String(filter.value));+ }+ }+ return Array.from(new Set(names));+}++function withServiceEq(query: OpenLITQuery, serviceName: string): OpenLITQuery {+ const filters = (query.filters || []).filter(+ (f) => !(f.target === "attribute" && f.key === "service.name")+ );+ filters.push({+ target: "attribute",+ scope: "resource",+ key: "service.name",+ op: "eq",+ value: serviceName,+ });+ return { ...query, filters };+}++function resolveCacheId(source: SampleFetchSource): string {+ return source.sampleCacheKey || "anon";+}++async function sampleOnce(+ source: SampleFetchSource,+ query: OpenLITQuery,+ maxTraces: number+): Promise<NormalizedSpan[]> {+ if (typeof source.sampleTracesForGraph === "function") {+ return source.sampleTracesForGraph(query, maxTraces);+ }+ return [];+}++/**+ * Fan out across discovered services so one high-volume app cannot dominate+ * the sample (Tempo search is recency-ordered and otherwise returns one service).+ */+async function fetchStratifiedSample(+ source: SampleFetchSource,+ query: OpenLITQuery,+ maxTraces: number+): Promise<{ spans: NormalizedSpan[]; truncated: boolean } | null> {+ // Service discovery is intentionally AI-focused: adapters use it to find+ // workloads that emit GenAI telemetry for aggregates and analysis. A flat+ // trace list has no AI selector and must search the backend directly;+ // otherwise an unfiltered list can omit non-AI services while a service+ // filter returns rows from them.+ if (query.aiSelector === false) return null;+ if (source.samplesAreServiceStratified) return null;+ if (typeof source.sampleTracesForGraph !== "function") return null;+ if (typeof source.discoverServices !== "function") return null;++ const filtered = extractServiceNames(query.filters);+ if (filtered.length === 1) return null;++ let names = filtered;+ if (!names.length) {+ try {+ const discovered = await source.discoverServices(query.timeRange);+ names = discovered.map((d) => d.serviceName).filter(Boolean);+ } catch {+ return null;+ }+ }+ if (names.length <= 1) return null;++ names = names.slice(0, Math.min(MAX_SERVICES_IN_SAMPLE, maxTraces));+ const baseBudget = Math.floor(maxTraces / names.length);+ const remainder = maxTraces % names.length;+ const serviceBudgets = names.map((name, index) => ({+ name,+ limit: baseBudget + (index < remainder ? 1 : 0),+ }));++ const batches = await mapPool(serviceBudgets, STRATIFY_CONCURRENCY, ({ name, limit }) =>+ sampleOnce(source, withServiceEq(query, name), limit)+ );+ const spans = batches.flat();+ const traceCount = new Set(spans.map((s) => s.traceId).filter(Boolean)).size;+ const allowedTraceIds = new Set(uniqueTraceIds(spans, maxTraces));+ return {+ spans: spans.filter((span) => allowedTraceIds.has(span.traceId)),+ truncated: traceCount >= maxTraces,+ };+}++async function fetchUnstratified(+ source: SampleFetchSource,+ query: OpenLITQuery,+ maxTraces: number+): Promise<{ spans: NormalizedSpan[]; truncated: boolean }> {+ const sampleQuery: OpenLITQuery = { ...query, limit: maxTraces };++ if (typeof source.sampleTracesForGraph === "function") {+ const spans = await source.sampleTracesForGraph(sampleQuery, maxTraces);+ const traceCount = new Set(spans.map((s) => s.traceId).filter(Boolean)).size;+ return { spans, truncated: traceCount >= maxTraces };+ }++ if (typeof source.listSpans !== "function") {+ return { spans: [], truncated: false };+ }++ const frame = await source.listSpans(sampleQuery);+ const rows = frame.rows || [];+ const listTruncated = !!frame.meta?.truncated || rows.length >= maxTraces;++ if (looksLikeRootsOnly(rows) && typeof source.getTraceSpans === "function") {+ const ids = uniqueTraceIds(rows, maxTraces);+ const perTrace = await mapPool(ids, DEFAULT_SAMPLE_CONCURRENCY, (id) =>+ source.getTraceSpans!(id)+ );+ return {+ spans: perTrace.flat(),+ truncated: listTruncated || ids.length >= maxTraces,+ };+ }++ return { spans: rows, truncated: listTruncated };+}++/**+ * Pull a bounded set of spans suitable for in-process aggregation.+ * Prefers stratified sampling when the adapter can discover services.+ * Results are cached briefly so Telemetry panels share one download.+ */+export async function fetchSpansForAggregation(+ source: SampleFetchSource,+ query: OpenLITQuery,+ opts?: { maxTraces?: number; skipCache?: boolean }+): Promise<{ spans: NormalizedSpan[]; truncated: boolean }> {+ const { query: clamped } = clampQueryToSource(source, query);+ const requested =+ opts?.maxTraces ?? clamped.limit ?? AGGREGATE_SAMPLE_TRACE_CAP;+ const maxTraces = Math.max(+ 1,+ Math.min(requested, AGGREGATE_SAMPLE_TRACE_CAP)+ );++ const load = async () => {+ const stratified = await fetchStratifiedSample(source, clamped, maxTraces);+ if (stratified) return stratified;+ return fetchUnstratified(source, clamped, maxTraces);+ };++ if (opts?.skipCache) return load();++ const key = cacheKey(resolveCacheId(source), [+ "l1-sample",+ clamped.timeRange.start.toISOString(),+ clamped.timeRange.end.toISOString(),+ clamped.aiSelector !== false,+ clamped.filters || [],+ maxTraces,+ ]);+ return cachedQuery(key, L1_SAMPLE_TTL_MS, load);+}++const ROOT_PARENT_IDS = new Set(["", "0".repeat(16)]);++/** Pick the root (or earliest) span for a single trace. */+export function pickRootSpan(spans: NormalizedSpan[]): NormalizedSpan | undefined {+ if (spans.length === 0) return undefined;+ return (+ spans.find(+ (s) => !s.parentSpanId || ROOT_PARENT_IDS.has(s.parentSpanId)+ ) || spans[0]+ );+}++/** Collapse full-trace spans to one root row per trace, newest first. */+export function collapseToRootSpans(spans: NormalizedSpan[]): NormalizedSpan[] {+ const byTrace = new Map<string, NormalizedSpan[]>();+ for (const span of spans) {+ if (!span.traceId) continue;+ const list = byTrace.get(span.traceId) || [];+ list.push(span);+ byTrace.set(span.traceId, list);+ }+ const roots: NormalizedSpan[] = [];+ for (const group of Array.from(byTrace.values())) {+ const root = pickRootSpan(group);+ if (root) roots.push(root);+ }+ roots.sort((a, b) => {+ const ta = a.timestamp ? new Date(a.timestamp).getTime() : 0;+ const tb = b.timestamp ? new Date(b.timestamp).getTime() : 0;+ return tb - ta;+ });+ return roots;+}++/**+ * Stratified flat-list sample for Telemetry tables.+ * Fans out across services (unless already scoped to one), then collapses to+ * one root span per trace so a single high-volume app cannot monopolize the page.+ */+export async function fetchSpansForList(+ source: SampleFetchSource,+ query: OpenLITQuery,+ opts?: { maxRows?: number; skipCache?: boolean }+): Promise<{ spans: NormalizedSpan[]; truncated: boolean }> {+ const { query: clamped } = clampQueryToSource(source, query);+ const maxRows = Math.min(+ opts?.maxRows ?? clamped.limit ?? DEFAULT_SAMPLE_TRACE_CAP,+ DEFAULT_SAMPLE_TRACE_CAP+ );++ const load = async () => {+ const sampleTraceCap =+ clamped.aiSelector === false+ ? maxRows+ : Math.max(maxRows, MIN_TRACES_PER_SERVICE * 4);+ const sample = await fetchSpansForAggregation(source, clamped, {+ maxTraces: sampleTraceCap,+ skipCache: true,+ });+ const roots = collapseToRootSpans(sample.spans).slice(0, maxRows);+ return {+ spans: roots,+ truncated: sample.truncated || roots.length >= maxRows,+ };+ };++ if (opts?.skipCache) return load();++ const key = cacheKey(resolveCacheId(source), [+ "l1-list",+ clamped.timeRange.start.toISOString(),+ clamped.timeRange.end.toISOString(),+ clamped.aiSelector !== false,+ clamped.filters || [],+ maxRows,+ clamped.offset || 0,+ ]);+ return cachedQuery(key, L1_SAMPLE_TTL_MS, load);+}diff --git a/src/client/src/lib/platform/connectors/datasource/http/auth-headers.ts b/src/client/src/lib/platform/connectors/datasource/http/auth-headers.tsnew file mode 100644index 00000000..54d99df7--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/http/auth-headers.ts@@ -0,0 +1,37 @@+/**+ * Shared Authorization / tenant header construction for HTTP telemetry adapters.+ *+ * Grafana Cloud Tempo/Loki/Mimir query APIs use Basic auth+ * (instance ID + access policy token). Self-hosted LGTM often uses no auth,+ * Basic via a reverse proxy, or a Bearer token. Prefer Basic when a username is+ * present so Cloud configs win over a leftover bearer field.+ */+export function applyHttpAuthCredentials(+ credentials: Record<string, string>,+ opts?: {+ tenantHeader?: "X-Scope-OrgID" | "AccountID";+ authType?: string;+ }+): Record<string, string> {+ const headers: Record<string, string> = {};+ const username = credentials.username?.trim();+ const token = credentials.token?.trim();++ const authType = opts?.authType || (username ? "basic" : token ? "bearer" : "none");++ if (authType === "basic" && username) {+ const basic = Buffer.from(+ `${username}:${credentials.password || ""}`+ ).toString("base64");+ headers.Authorization = `Basic ${basic}`;+ } else if (authType === "bearer" && token) {+ headers.Authorization = `Bearer ${token}`;+ }++ const tenant = credentials.tenant?.trim();+ if (tenant && opts?.tenantHeader) {+ headers[opts.tenantHeader] = tenant;+ }++ return headers;+}diff --git a/src/client/src/lib/platform/connectors/datasource/http/cache.ts b/src/client/src/lib/platform/connectors/datasource/http/cache.tsnew file mode 100644index 00000000..4e3a5fe7--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/http/cache.ts@@ -0,0 +1,126 @@+/**+ * Per-source in-memory query cache with TTL, in-flight de-duplication, and a+ * byte-aware soft budget so full OTLP payloads cannot grow RSS without bound.+ *+ * External vendors (notably Datadog: 300 spans req/hr) rate-limit aggressively,+ * so identical queries within a short window must be served from cache and+ * concurrent identical queries must share a single in-flight request.+ */++interface CacheEntry<T> {+ value: T;+ expiresAt: number;+ /** Approximate serialized size in bytes (UTF-8 JSON estimate). */+ bytes: number;+}++const store = new Map<string, CacheEntry<unknown>>();+const inFlight = new Map<string, Promise<unknown>>();++/** Soft cap so a long-lived process cannot grow the Map without bound. */+export const MAX_CACHE_ENTRIES = 500;++/** Soft RSS budget for cached payloads (entries + values). */+export const MAX_CACHE_BYTES = 64 * 1024 * 1024;++let totalBytes = 0;++/** Build a stable cache key from a source id and a query descriptor. */+export function cacheKey(sourceId: string, parts: unknown): string {+ return `${sourceId}::${stableStringify(parts)}`;+}++/** Approximate UTF-8 byte size of a cached value. */+export function estimateCacheBytes(value: unknown): number {+ try {+ const encoded = JSON.stringify(value) ?? "null";+ return typeof Buffer !== "undefined"+ ? Buffer.byteLength(encoded, "utf8")+ : encoded.length;+ } catch {+ // Non-JSON-serializable values (rare): charge a conservative fixed cost.+ return 4 * 1024;+ }+}++function deleteEntry(key: string): void {+ const existing = store.get(key);+ if (!existing) return;+ totalBytes = Math.max(0, totalBytes - existing.bytes);+ store.delete(key);+}++function pruneExpired(now: number): void {+ for (const [key, entry] of Array.from(store.entries())) {+ if (entry.expiresAt <= now) deleteEntry(key);+ }+ while (store.size > MAX_CACHE_ENTRIES || totalBytes > MAX_CACHE_BYTES) {+ const oldest = store.keys().next().value;+ if (oldest === undefined) break;+ deleteEntry(oldest);+ }+}++function stableStringify(value: unknown): string {+ if (value === null || typeof value !== "object") return JSON.stringify(value);+ if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`;+ const keys = Object.keys(value as Record<string, unknown>).sort();+ return `{${keys+ .map((k) => `${JSON.stringify(k)}:${stableStringify((value as Record<string, unknown>)[k])}`)+ .join(",")}}`;+}++/**+ * Get-or-load with TTL + in-flight coalescing. Concurrent callers with the+ * same key await the same request; results are cached for `ttlMs`.+ */+export async function cachedQuery<T>(+ key: string,+ ttlMs: number,+ loader: () => Promise<T>+): Promise<T> {+ const now = Date.now();+ const hit = store.get(key);+ if (hit && hit.expiresAt > now) {+ return hit.value as T;+ }+ const pending = inFlight.get(key);+ if (pending) return pending as Promise<T>;++ const promise = (async () => {+ try {+ const value = await loader();+ const expiresAt = Date.now() + ttlMs;+ const bytes = estimateCacheBytes(value);+ // Skip caching oversized single payloads that would dominate the budget.+ if (bytes <= MAX_CACHE_BYTES) {+ deleteEntry(key);+ store.set(key, { value, expiresAt, bytes });+ totalBytes += bytes;+ }+ pruneExpired(Date.now());+ return value;+ } finally {+ inFlight.delete(key);+ }+ })();+ inFlight.set(key, promise);+ return promise;+}++/** Clear the in-process query response cache (bind/credential rotation). */+export function clearQueryCache(): void {+ store.clear();+ inFlight.clear();+ totalBytes = 0;+}++/** @deprecated Prefer clearQueryCache(); kept for existing tests. */+export function __clearCache(): void {+ clearQueryCache();+}++/** Test-only: inspect cache accounting. */+export function __cacheStats(): { entries: number; bytes: number } {+ return { entries: store.size, bytes: totalBytes };+}diff --git a/src/client/src/lib/platform/connectors/datasource/http/endpoint-url.ts b/src/client/src/lib/platform/connectors/datasource/http/endpoint-url.tsnew file mode 100644index 00000000..a29a7e12--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/http/endpoint-url.ts@@ -0,0 +1,70 @@+/**+ * Normalize user-supplied datasource endpoint URLs before persist/fetch.+ *+ * Browsers and paste paths sometimes collapse `http://host` into `http:/host`+ * (single slash). `URL` can still parse that, but we store the canonical form.+ */++import { existsSync } from "fs";++const LOOPBACK_HOSTNAMES = new Set(["localhost", "127.0.0.1", "::1"]);++/** Fix `http:/host` / `https:/host` and trim trailing slashes. */+export function normalizeDatasourceEndpointUrl(raw: string): string {+ const trimmed = String(raw || "").trim();+ if (!trimmed) return trimmed;+ const withAuthority = trimmed.replace(/^(https?:)\/(?!\/)/i, "$1//");+ try {+ const url = new URL(withAuthority);+ if (url.protocol !== "http:" && url.protocol !== "https:") {+ return withAuthority.replace(/\/+$/, "");+ }+ return url.toString().replace(/\/+$/, "");+ } catch {+ return withAuthority.replace(/\/+$/, "");+ }+}++/** True when the OpenLIT process is running inside a Docker container. */+export function isRunningInDocker(+ exists: (path: string) => boolean = existsSync+): boolean {+ try {+ return exists("/.dockerenv");+ } catch {+ return false;+ }+}++/**+ * When OpenLIT runs in Docker, `localhost` / `127.0.0.1` refer to the+ * container — not the host where Loki/Tempo/Prometheus often listen.+ * Rewrite loopback hosts to `host.docker.internal` for outbound fetches.+ * Stored config stays unchanged; only the request URL is rewritten.+ */+export function rewriteLoopbackEndpointForDocker(+ rawUrl: string,+ options: {+ enabled?: boolean;+ dockerHost?: string;+ } = {}+): string {+ const enabled = options.enabled ?? isRunningInDocker();+ if (!enabled) return rawUrl;+ const normalized = normalizeDatasourceEndpointUrl(rawUrl);+ if (!normalized) return rawUrl;+ try {+ const url = new URL(normalized);+ const hostname = url.hostname.toLowerCase().replace(/^\[|\]$/g, "");+ if (!LOOPBACK_HOSTNAMES.has(hostname)) return normalized;+ url.hostname = options.dockerHost || "host.docker.internal";+ return url.toString().replace(/\/+$/, "");+ } catch {+ return rawUrl;+ }+}++/** True for checkbox / switch values that mean enabled. */+export function isEnabledSetting(value: unknown): boolean {+ return value === true || value === "true" || value === 1 || value === "1";+}diff --git a/src/client/src/lib/platform/connectors/datasource/http/limits.ts b/src/client/src/lib/platform/connectors/datasource/http/limits.tsnew file mode 100644index 00000000..9fe58586--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/http/limits.ts@@ -0,0 +1,221 @@+/**+ * Backend pull hardening for external telemetry sources (Grafana-inspired).+ *+ * External observability backends rate-limit hard and have per-query scan+ * ceilings, so every outbound pull should:+ * - stay under a per-source concurrency cap (a semaphore keyed by source id),+ * - retry transient failures (429 / 5xx / network) with exponential backoff,+ * - respect query budgets (max rows, max time range) so a widget can never+ * ask a vendor for an unbounded scan.+ *+ * These are pure, injectable utilities so they are unit-testable without real+ * network access.+ */++import type { OpenLITQuery, SourceCapabilities } from "../types";++// ---- Query budgets --------------------------------------------------------++export interface QueryBudget {+ /** Hard cap on returned rows. */+ maxRows: number;+ /** Hard cap on the query time range (ms). */+ maxRangeMs: number;+ /**+ * Hard cap on how far back `start` may reach from `end` (ms). Sourced from a+ * source's `capabilities().maxLookbackMs` (e.g. a vendor's retention window)+ * so we never ask for data older than the vendor keeps.+ */+ maxLookbackMs?: number;+}++const DAY_MS = 24 * 60 * 60 * 1000;++/** Conservative default budget applied to external structured queries. */+export const DEFAULT_QUERY_BUDGET: QueryBudget = {+ maxRows: 5000,+ maxRangeMs: 30 * DAY_MS,+};++type CapabilityBudgetSource = {+ capabilities?: () => Pick<+ SourceCapabilities,+ "maxLookbackMs" | "maxTimeRangeMs"+ >;+};++/** Build the effective budget advertised by one concrete datasource. */+export function queryBudgetForSource(+ source: CapabilityBudgetSource,+ base: QueryBudget = DEFAULT_QUERY_BUDGET+): QueryBudget {+ try {+ const capabilities = source.capabilities?.();+ return {+ ...base,+ ...(capabilities?.maxTimeRangeMs !== undefined+ ? { maxRangeMs: capabilities.maxTimeRangeMs }+ : {}),+ ...(capabilities?.maxLookbackMs !== undefined+ ? { maxLookbackMs: capabilities.maxLookbackMs }+ : {}),+ };+ } catch {+ return base;+ }+}++/** Clamp using the selected datasource's own range/retention capabilities. */+export function clampQueryToSource(+ source: CapabilityBudgetSource,+ query: OpenLITQuery,+ base: QueryBudget = DEFAULT_QUERY_BUDGET+): { query: OpenLITQuery; clamped: string[] } {+ return clampQueryBudget(query, queryBudgetForSource(source, base));+}++/**+ * Clamp an OpenLITQuery to a budget. Returns the (possibly) clamped query and+ * the list of fields that were clamped so callers can surface a truncation+ * note. The time range is clamped by moving `start` forward toward `end`.+ */+export function clampQueryBudget(+ query: OpenLITQuery,+ budget: QueryBudget = DEFAULT_QUERY_BUDGET+): { query: OpenLITQuery; clamped: string[] } {+ const clamped: string[] = [];+ const next: OpenLITQuery = { ...query };++ if (next.limit === undefined || next.limit > budget.maxRows) {+ if (next.limit !== undefined && next.limit > budget.maxRows) {+ clamped.push("limit");+ }+ next.limit = Math.min(next.limit ?? budget.maxRows, budget.maxRows);+ }++ const { start, end } = next.timeRange;+ // Effective range ceiling is the tighter of the absolute range cap and the+ // vendor lookback/retention window.+ const effectiveMaxRangeMs =+ budget.maxLookbackMs !== undefined+ ? Math.min(budget.maxRangeMs, budget.maxLookbackMs)+ : budget.maxRangeMs;+ const rangeMs = end.getTime() - start.getTime();+ if (rangeMs > effectiveMaxRangeMs) {+ next.timeRange = {+ start: new Date(end.getTime() - effectiveMaxRangeMs),+ end,+ };+ clamped.push(+ rangeMs > budget.maxRangeMs ? "timeRange" : "maxLookback"+ );+ }++ return { query: next, clamped };+}++// ---- Per-source concurrency cap ------------------------------------------++/** A minimal FIFO counting semaphore. */+export class Semaphore {+ private readonly max: number;+ private active = 0;+ private readonly queue: (() => void)[] = [];++ constructor(max: number) {+ this.max = Math.max(1, max);+ }++ async acquire(): Promise<() => void> {+ if (this.active < this.max) {+ this.active++;+ return () => this.release();+ }+ await new Promise<void>((resolve) => this.queue.push(resolve));+ this.active++;+ return () => this.release();+ }++ private release(): void {+ this.active--;+ const next = this.queue.shift();+ if (next) next();+ }+}++const semaphores = new Map<string, Semaphore>();++/** Run `fn` under a per-key concurrency cap (default cap is created lazily). */+export async function withSourceConcurrency<T>(+ key: string,+ maxConcurrent: number,+ fn: () => Promise<T>+): Promise<T> {+ let sem = semaphores.get(key);+ if (!sem) {+ sem = new Semaphore(maxConcurrent);+ semaphores.set(key, sem);+ }+ const release = await sem.acquire();+ try {+ return await fn();+ } finally {+ release();+ }+}++/** Test-only: reset the semaphore registry. */+export function __resetConcurrencyForTests(): void {+ semaphores.clear();+}++// ---- Retry with exponential backoff --------------------------------------++export interface RetryOptions {+ /** Number of retry attempts after the first try. Default 2. */+ retries?: number;+ /** Base delay in ms; grows exponentially. Default 200. */+ baseDelayMs?: number;+ /** Whether an error is retryable. Default: 429 / 5xx / network. */+ isRetryable?: (err: unknown) => boolean;+ /** Injectable sleep for tests. */+ sleep?: (ms: number) => Promise<void>;+}++const defaultSleep = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));++/** Default retryability: transient HTTP (429/5xx) or network/timeout errors. */+export function defaultIsRetryable(err: unknown): boolean {+ const status = (err as { status?: number })?.status;+ if (typeof status === "number") return status === 429 || status >= 500;+ const message = String((err as Error)?.message || "").toLowerCase();+ return (+ message.includes("timed out") ||+ message.includes("network") ||+ message.includes("econn") ||+ message.includes("fetch failed")+ );+}++/** Run `fn`, retrying transient failures with exponential backoff. */+export async function withRetry<T>(+ fn: () => Promise<T>,+ options: RetryOptions = {}+): Promise<T> {+ const retries = options.retries ?? 2;+ const baseDelayMs = options.baseDelayMs ?? 200;+ const isRetryable = options.isRetryable ?? defaultIsRetryable;+ const sleep = options.sleep ?? defaultSleep;++ let lastErr: unknown;+ for (let attempt = 0; attempt <= retries; attempt++) {+ try {+ return await fn();+ } catch (err) {+ lastErr = err;+ if (attempt === retries || !isRetryable(err)) break;+ await sleep(baseDelayMs * Math.pow(2, attempt));+ }+ }+ throw lastErr;+}diff --git a/src/client/src/lib/platform/connectors/datasource/http/safe-fetch.ts b/src/client/src/lib/platform/connectors/datasource/http/safe-fetch.tsnew file mode 100644index 00000000..b1b06794--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/http/safe-fetch.ts@@ -0,0 +1,443 @@+/**+ * SSRF-safe server-side fetch for user-supplied observability endpoints+ * (Prometheus, Loki, Tempo, Grafana, Jaeger, Victoria, Datadog, New Relic).+ *+ * Enforces rule F4: never treat URLs as strings, only allow http/https, reject+ * `javascript:`/`data:` and credentials-in-URL. By default, private / loopback+ * / link-local ranges are blocked. Self-hosted OSS backends (Tempo/Loki/…)+ * may explicitly opt in via `allowPrivateNetwork` so in-cluster RFC1918 endpoints work;+ * cloud metadata endpoints stay blocked even then. DNS lookup and fetch are+ * injectable so this is unit-testable without real network access.+ */++import net from "net";+import { promises as dns } from "dns";+import getMessage from "@/constants/messages";+import {+ isEnabledSetting,+ normalizeDatasourceEndpointUrl,+ rewriteLoopbackEndpointForDocker,+} from "./endpoint-url";+import { withRetry, withSourceConcurrency, type RetryOptions } from "./limits";++export type LookupFn = (hostname: string) => Promise<{ address: string }[]>;++/** HTTP error carrying the vendor response status so retry can classify it. */+export class SourceResponseError extends Error {+ readonly status: number;+ constructor(status: number, message: string) {+ super(message);+ this.name = "SourceResponseError";+ this.status = status;+ }+}++/** Always blocked — cloud instance-metadata SSRF targets. */+const METADATA_HOSTNAMES = new Set([+ "metadata.google.internal",+ "metadata.goog",+ "169.254.169.254",+ "metadata.azure.com",+ "instance-data",+]);++/** Loopback hostnames: blocked unless `allowPrivateNetwork` is set. */+const LOOPBACK_HOSTNAMES = new Set(["localhost", "127.0.0.1", "::1"]);++/** HTTP redirect status codes we follow manually (with per-hop re-validation). */+const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);++/** Cap on manually-followed redirects to avoid loops. */+const MAX_REDIRECTS = 5;++/** Classify an IP (v4 or v6) as private / loopback / link-local / unspecified. */+export function isPrivateAddress(ip: string): boolean {+ const version = net.isIP(ip);+ if (version === 4) {+ const p = ip.split(".").map(Number);+ if (p.length !== 4 || p.some((n) => Number.isNaN(n))) return true;+ if (p[0] === 127) return true; // loopback+ if (p[0] === 10) return true; // private+ if (p[0] === 172 && p[1] >= 16 && p[1] <= 31) return true; // private+ if (p[0] === 192 && p[1] === 168) return true; // private+ if (p[0] === 169 && p[1] === 254) return true; // link-local+ if (p[0] === 100 && p[1] >= 64 && p[1] <= 127) return true; // CGNAT+ if (p[0] === 0) return true; // unspecified / this-network+ return false;+ }+ if (version === 6) {+ const lower = ip.toLowerCase().replace(/^\[|\]$/g, "");+ if (lower === "::1" || lower === "::") return true; // loopback / unspecified+ if (lower.startsWith("fe80:")) return true; // link-local+ if (lower.startsWith("fc") || lower.startsWith("fd")) return true; // ULA+ // IPv4-mapped IPv6 (::ffff:a.b.c.d)+ const mapped = lower.match(/::ffff:(\d+\.\d+\.\d+\.\d+)$/);+ if (mapped) return isPrivateAddress(mapped[1]);+ return false;+ }+ // Not an IP literal.+ return false;+}++export class SsrfError extends Error {+ constructor(message: string) {+ super(message);+ this.name = "SsrfError";+ }+}++export interface AssertUrlOptions {+ /** Permit plain http (self-hosted OSS backends). Default false (https only). */+ allowHttp?: boolean;+ /**+ * Permit RFC1918 / loopback / ULA targets for self-hosted observability+ * stacks living next to OpenLIT. Default false. Cloud metadata endpoints+ * (169.254.169.254, metadata.google.internal, …) stay blocked either way.+ */+ allowPrivateNetwork?: boolean;+ /** Injectable DNS resolver for tests. */+ lookup?: LookupFn;+}++/**+ * Network options for self-hosted OSS backends. Plain HTTP and private-network+ * access are explicit connector settings; permissive defaults are unsafe for+ * multi-tenant deployments.+ */+export function selfHostedNetworkOptions(+ settings: Record<string, unknown> = {}+): Pick<AssertUrlOptions, "allowHttp" | "allowPrivateNetwork"> {+ return {+ // Default on for self-hosted OSS backends unless explicitly disabled.+ allowHttp: settings.allowHttp !== false && settings.allowHttp !== "false",+ allowPrivateNetwork: isEnabledSetting(settings.allowPrivateNetwork),+ };+}++const defaultLookup: LookupFn = async (hostname) => {+ const results = await dns.lookup(hostname, { all: true });+ return results.map((r) => ({ address: r.address }));+};++/**+ * Validate a user-supplied URL for outbound fetch. Throws `SsrfError` on any+ * violation. Returns the parsed URL when safe.+ *+ * Residual note: Node's global `fetch` re-resolves DNS at connect time, so a+ * hostname that passes here could in theory rebind to a private address before+ * the socket opens (TOCTOU). `safeFetch` re-runs this check on every redirect+ * hop, but airtight rebinding protection requires socket-level egress control+ * (a pinned dispatcher / proxy) that is out of scope for the self-hosted model.+ */+export async function assertPublicUrl(+ rawUrl: string,+ options: AssertUrlOptions = {}+): Promise<URL> {+ let url: URL;+ try {+ url = new URL(normalizeDatasourceEndpointUrl(rawUrl));+ } catch {+ throw new SsrfError("Invalid URL");+ }++ const messages = getMessage();+ const allowedProtocols = options.allowHttp+ ? new Set(["http:", "https:"])+ : new Set(["https:"]);+ if (!allowedProtocols.has(url.protocol)) {+ throw new SsrfError(+ `Protocol "${url.protocol}" is not allowed for this data source`+ );+ }++ if (url.username || url.password) {+ throw new SsrfError("Credentials must not be embedded in the URL");+ }++ const hostname = url.hostname.toLowerCase().replace(/^\[|\]$/g, "");+ if (METADATA_HOSTNAMES.has(hostname)) {+ throw new SsrfError(`Host "${hostname}" is not allowed`);+ }+ if (!options.allowPrivateNetwork && LOOPBACK_HOSTNAMES.has(hostname)) {+ throw new SsrfError(messages.DATA_SOURCE_PRIVATE_NETWORK_BLOCKED(hostname));+ }++ const allowPrivate = !!options.allowPrivateNetwork;++ // Literal IP host: validate directly.+ if (net.isIP(hostname)) {+ // Link-local / cloud metadata range stays blocked even with private allow.+ if (isCloudMetadataAddress(hostname)) {+ throw new SsrfError(`Host "${hostname}" is not allowed`);+ }+ if (!allowPrivate && isPrivateAddress(hostname)) {+ throw new SsrfError(messages.DATA_SOURCE_PRIVATE_NETWORK_BLOCKED(hostname));+ }+ return url;+ }++ // Hostname: resolve and validate every returned address.+ const lookup = options.lookup || defaultLookup;+ let addresses: { address: string }[];+ try {+ addresses = await lookup(hostname);+ } catch {+ throw new SsrfError(`Could not resolve host "${hostname}"`);+ }+ if (addresses.length === 0) {+ throw new SsrfError(`Host "${hostname}" did not resolve to any address`);+ }+ for (const { address } of addresses) {+ if (isCloudMetadataAddress(address)) {+ throw new SsrfError(+ `Host "${hostname}" resolves to a cloud metadata address`+ );+ }+ if (!allowPrivate && isPrivateAddress(address)) {+ throw new SsrfError(messages.DATA_SOURCE_PRIVATE_NETWORK_BLOCKED(hostname));+ }+ }++ return url;+}++/** Cloud metadata / link-local IPs that must never be reachable via SSRF. */+export function isCloudMetadataAddress(ip: string): boolean {+ const version = net.isIP(ip);+ if (version === 4) {+ const p = ip.split(".").map(Number);+ if (p.length !== 4 || p.some((n) => Number.isNaN(n))) return true;+ // AWS/GCP/Azure link-local metadata+ if (p[0] === 169 && p[1] === 254) return true;+ return false;+ }+ if (version === 6) {+ const lower = ip.toLowerCase().replace(/^\[|\]$/g, "");+ const mapped = lower.match(/::ffff:(\d+\.\d+\.\d+\.\d+)$/);+ if (mapped) return isCloudMetadataAddress(mapped[1]);+ return false;+ }+ return false;+}++export interface SafeFetchOptions extends AssertUrlOptions {+ method?: string;+ headers?: Record<string, string>;+ body?: string;+ /** Request timeout in ms. Default 15000. */+ timeoutMs?: number;+ /**+ * Max response body size in bytes. Default 25 MiB. Oversized bodies are+ * rejected before JSON parse so a fat Tempo/Jaeger OTLP dump cannot OOM+ * the Node process.+ */+ maxResponseBytes?: number;+ /** Injectable fetch for tests. Defaults to global fetch. */+ fetchImpl?: typeof fetch;+ /** Secret values to redact from any thrown error message. */+ redactValues?: string[];+ /**+ * When set, the fetch runs under a per-source concurrency cap keyed by this+ * value so a rate-limited backend is never hammered by parallel widgets.+ */+ concurrencyKey?: string;+ /** Max concurrent outbound requests per `concurrencyKey`. Default 4. */+ maxConcurrent?: number;+ /** Retry transient 429/5xx/network failures with exponential backoff. */+ retry?: RetryOptions | boolean;+}++/** Default max vendor response body size (25 MiB). */+export const DEFAULT_MAX_RESPONSE_BYTES = 25 * 1024 * 1024;++/**+ * Read a fetch Response body with a hard byte cap. Prefers streaming when+ * `body.getReader` is available; otherwise falls back to `text()` and checks+ * the resulting UTF-8 byte length (used by unit-test mocks).+ */+export async function readLimitedResponseText(+ response: Response,+ maxBytes: number = DEFAULT_MAX_RESPONSE_BYTES+): Promise<string> {+ const messages = getMessage();+ const contentLengthHeader = response.headers?.get?.("content-length");+ if (contentLengthHeader) {+ const declared = Number(contentLengthHeader);+ if (Number.isFinite(declared) && declared > maxBytes) {+ throw new Error(messages.DATA_SOURCE_RESPONSE_TOO_LARGE(maxBytes));+ }+ }++ const body = response.body as ReadableStream<Uint8Array> | null | undefined;+ if (body && typeof body.getReader === "function") {+ const reader = body.getReader();+ const chunks: Uint8Array[] = [];+ let total = 0;+ try {+ while (true) {+ const { done, value } = await reader.read();+ if (done) break;+ if (!value) continue;+ total += value.byteLength;+ if (total > maxBytes) {+ await reader.cancel().catch(() => undefined);+ throw new Error(messages.DATA_SOURCE_RESPONSE_TOO_LARGE(maxBytes));+ }+ chunks.push(value);+ }+ } finally {+ reader.releaseLock?.();+ }+ const merged = new Uint8Array(total);+ let offset = 0;+ for (const chunk of chunks) {+ merged.set(chunk, offset);+ offset += chunk.byteLength;+ }+ return new TextDecoder("utf-8").decode(merged);+ }++ const text = await response.text();+ const bytes =+ typeof Buffer !== "undefined"+ ? Buffer.byteLength(text, "utf8")+ : text.length;+ if (bytes > maxBytes) {+ throw new Error(messages.DATA_SOURCE_RESPONSE_TOO_LARGE(maxBytes));+ }+ return text;+}++/** Redact sensitive substrings from a message. */+export function redact(message: string, values: string[] = []): string {+ let out = message;+ for (const v of values) {+ if (v && v.length >= 4) {+ out = out.split(v).join("[REDACTED]");+ }+ }+ return out;+}++/**+ * Perform an SSRF-validated fetch with a timeout. Throws on non-2xx with a+ * redacted message. Returns the parsed JSON body (or text when not JSON).+ */+export async function safeFetch<T = unknown>(+ rawUrl: string,+ options: SafeFetchOptions = {}+): Promise<T> {+ const redactValues = options.redactValues || [];+ const maxResponseBytes = options.maxResponseBytes ?? DEFAULT_MAX_RESPONSE_BYTES;+ // Inside Docker, loopback points at the container — remap to the host+ // gateway when the connector explicitly allowed private/localhost targets.+ const fetchUrl =+ options.allowPrivateNetwork+ ? rewriteLoopbackEndpointForDocker(rawUrl)+ : rawUrl;+ const url = await assertPublicUrl(fetchUrl, {+ allowHttp: options.allowHttp,+ allowPrivateNetwork: options.allowPrivateNetwork,+ lookup: options.lookup,+ });+ const fetchImpl = options.fetchImpl || fetch;++ // Follow redirects manually so every hop is re-validated against the SSRF+ // rules. Default `fetch` follows redirects transparently, which would let a+ // vendor 3xx to an internal/loopback address bypass `assertPublicUrl`.+ const performOnce = async (target: URL) => {+ const controller = new AbortController();+ const timeout = setTimeout(+ () => controller.abort(),+ options.timeoutMs ?? 15000+ );+ try {+ return await fetchImpl(target.toString(), {+ method: options.method || "GET",+ headers: options.headers,+ body: options.body,+ signal: controller.signal,+ redirect: "manual",+ });+ } finally {+ clearTimeout(timeout);+ }+ };++ const doFetch = async (): Promise<T> => {+ let currentUrl = url;+ for (let hop = 0; hop <= MAX_REDIRECTS; hop++) {+ let response: Awaited<ReturnType<typeof fetchImpl>>;+ try {+ response = await performOnce(currentUrl);+ } catch (err) {+ if ((err as Error)?.name === "AbortError") {+ throw new Error("Data source request timed out");+ }+ throw new Error(+ redact(String((err as Error)?.message || err), redactValues)+ );+ }++ if (REDIRECT_STATUSES.has(response.status)) {+ const location = response.headers?.get?.("location");+ if (!location) {+ throw new SourceResponseError(+ response.status,+ `Data source responded ${response.status} without a Location header`+ );+ }+ let next: URL;+ try {+ next = new URL(location, currentUrl);+ } catch {+ throw new SsrfError("Data source redirected to an invalid URL");+ }+ // Re-validate the redirect target; throws SsrfError if internal.+ currentUrl = await assertPublicUrl(next.toString(), {+ allowHttp: options.allowHttp,+ allowPrivateNetwork: options.allowPrivateNetwork,+ lookup: options.lookup,+ });+ continue;+ }++ const text = await readLimitedResponseText(response, maxResponseBytes);+ if (!response.ok) {+ throw new SourceResponseError(+ response.status,+ redact(+ `Data source responded ${response.status}: ${text.slice(0, 500)}`,+ redactValues+ )+ );+ }+ if (!text) return undefined as unknown as T;+ try {+ return JSON.parse(text) as T;+ } catch {+ return text as unknown as T;+ }+ }+ throw new SsrfError(+ "Data source exceeded the maximum number of redirects"+ );+ };++ const withRetryMaybe = options.retry+ ? () =>+ withRetry(+ doFetch,+ typeof options.retry === "object" ? options.retry : {}+ )+ : doFetch;++ if (options.concurrencyKey) {+ return withSourceConcurrency(+ options.concurrencyKey,+ options.maxConcurrent ?? 4,+ withRetryMaybe+ );+ }+ return withRetryMaybe();+}diff --git a/src/client/src/lib/platform/connectors/datasource/http/secret.ts b/src/client/src/lib/platform/connectors/datasource/http/secret.tsnew file mode 100644index 00000000..e3ad9d47--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/http/secret.ts@@ -0,0 +1,136 @@+/**+ * Resolve an external telemetry source's credentials from the vault.+ *+ * `TelemetrySource.secretRef` is an `openlit_vault` secret id. The secret value+ * is a JSON blob of vendor credentials (e.g. `{ "apiKey": "...", "appKey": "..." }`+ * for Datadog, `{ "token": "..." }` for New Relic). Decryption happens+ * server-side only; secret values are never logged.+ */++import { getSecretById } from "@/lib/platform/vault";+import {+ DATA_SOURCE_SECRET_DECRYPT_FAILED,+ DATA_SOURCE_SECRET_NOT_FOUND,+ DATA_SOURCE_SECRET_UNAVAILABLE,+} from "@/constants/messages/en";++export interface ResolvedSecret {+ /** Raw decrypted secret string. */+ raw: string;+ /** Parsed JSON credentials when the secret is a JSON object; else {}. */+ credentials: Record<string, string>;+}++interface CachedSecret {+ value: ResolvedSecret;+ expiresAt: number;+ staleUntil: number;+}++// Adapters are request-scoped, while a short ClickHouse reset can outlive one+// request. Keep successfully decrypted credentials briefly at the server+// boundary so new adapter instances do not immediately lose vendor auth. The+// stale copy is used only when the vault read itself fails, never when the+// secret was deleted or is unreadable.+const SOURCE_SECRET_CACHE_TTL_MS = 2 * 60_000;+/** Brief vault-outage grace after fresh TTL — shortened from 30m. */+const SOURCE_SECRET_STALE_TTL_MS = 5 * 60_000;+const sourceSecretCache = new Map<string, CachedSecret>();++function sourceSecretCacheKey(+ secretRef: string,+ dbConfigId?: string,+ projectId?: string | null+): string {+ return `${projectId || "session"}:${dbConfigId || "current"}:${secretRef}`;+}++/**+ * Drop cached vault credentials after rotate/rebind. When `secretRef` is+ * omitted, clears the entire process cache.+ */+export function invalidateSourceSecretCache(secretRef?: string): void {+ if (!secretRef) {+ sourceSecretCache.clear();+ return;+ }+ const needle = `:${secretRef}`;+ for (const key of Array.from(sourceSecretCache.keys())) {+ if (key.endsWith(needle)) sourceSecretCache.delete(key);+ }+}++/** Test-only cache reset. */+export function __resetSourceSecretCacheForTests(): void {+ invalidateSourceSecretCache();+}++/**+ * Fetch and decrypt a vault secret for a source. Returns empty credentials+ * when no secretRef is set (public/no-auth sources).+ */+export async function resolveSourceSecret(+ secretRef: string | null | undefined,+ dbConfigId?: string,+ projectId?: string | null+): Promise<ResolvedSecret> {+ if (!secretRef) return { raw: "", credentials: {} };+ const cacheKey = sourceSecretCacheKey(secretRef, dbConfigId, projectId);+ const cached = sourceSecretCache.get(cacheKey);+ if (cached && cached.expiresAt > Date.now()) return cached.value;++ let result: Awaited<ReturnType<typeof getSecretById>>;+ try {+ result = await getSecretById(secretRef, dbConfigId, false, {+ logDecryptErrors: false,+ projectId: projectId || undefined,+ });+ } catch {+ if (cached && cached.staleUntil > Date.now()) return cached.value;+ throw new Error(DATA_SOURCE_SECRET_UNAVAILABLE);+ }+ if ((result as { err?: unknown } | null | undefined)?.err) {+ if (cached && cached.staleUntil > Date.now()) return cached.value;+ throw new Error(DATA_SOURCE_SECRET_UNAVAILABLE);+ }+ const row = (result?.data as { value?: string }[] | undefined)?.[0];+ const raw = typeof row?.value === "string" ? row.value : "";+ if (!row || !raw) throw new Error(DATA_SOURCE_SECRET_NOT_FOUND);++ let credentials: Record<string, string> = {};+ if (raw) {+ // decryptValue returns the ciphertext unchanged when decryption fails.+ if (raw.startsWith("enc:v1:")) {+ throw new Error(DATA_SOURCE_SECRET_DECRYPT_FAILED);+ }+ try {+ const parsed = JSON.parse(raw);+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {+ credentials = Object.fromEntries(+ Object.entries(parsed).map(([k, v]) => [k, String(v)])+ );+ }+ } catch {+ // A manually selected vault value may be a single opaque bearer token.+ credentials = { token: raw };+ }+ }++ const value = { raw, credentials };+ sourceSecretCache.set(cacheKey, {+ value,+ expiresAt: Date.now() + SOURCE_SECRET_CACHE_TTL_MS,+ staleUntil: Date.now() + SOURCE_SECRET_STALE_TTL_MS,+ });+ return value;+}++/** All secret values that must be redacted from any outbound error message. */+export function redactableSecretValues(secret: ResolvedSecret): string[] {+ const values = new Set<string>();+ if (secret.raw) values.add(secret.raw);+ for (const v of Object.values(secret.credentials)) {+ if (v) values.add(v);+ }+ return Array.from(values);+}diff --git a/src/client/src/lib/platform/connectors/datasource/index.ts b/src/client/src/lib/platform/connectors/datasource/index.tsnew file mode 100644index 00000000..aeb08ea5--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/index.ts@@ -0,0 +1,67 @@+import {+ createAdapter,+ listSourceTypeDescriptors,+} from "@/lib/platform/connectors/datasource/registry";+import type {+ DataSourceAdapter,+ SourceTypeDescriptor,+ TelemetrySourceDescriptor,+} from "@/lib/platform/connectors/datasource/types";+import { registerConnectorType } from "../registry";+import type { ConnectorField, ConnectorInstanceDescriptor } from "../types";+import { connectorIconPath } from "../icons";+import { connectorDescription } from "../descriptions";++function fields(descriptor: SourceTypeDescriptor): ConnectorField[] {+ return descriptor.configFields.map((field) => ({+ key: field.key,+ label: field.label,+ kind:+ field.kind === "switch"+ ? "boolean"+ : field.kind === "password"+ ? "password"+ : field.kind === "url"+ ? "url"+ : field.kind === "select"+ ? "select"+ : "text",+ defaultValue: field.defaultValue,+ options: field.options,+ }));+}++/** Register existing datasource types in the generic connector registry. */+export function registerDatasourceConnectorTypes(): void {+ for (const descriptor of listSourceTypeDescriptors({ includeInternal: true })) {+ registerConnectorType({+ descriptor: {+ type: descriptor.type,+ category: "datasource",+ displayName: descriptor.displayName,+ description: connectorDescription(descriptor.type, descriptor.displayName),+ icon: connectorIconPath(descriptor.type),+ scope: "project",+ configFields: fields(descriptor),+ capabilities: descriptor.capabilities,+ internal: descriptor.internal,+ },+ create: (instance: ConnectorInstanceDescriptor) => {+ const source: TelemetrySourceDescriptor = {+ type: instance.type,+ id: instance.id,+ isBuiltIn: instance.type === "clickhouse",+ settings: instance.settings,+ secretRef: instance.secretRef,+ signals: descriptor.declaredSignals,+ projectId: instance.projectId ?? null,+ name: instance.name,+ environment: instance.environment,+ };+ const adapter = createAdapter(source) as DataSourceAdapter | undefined;+ if (!adapter) throw new Error(`Connector adapter unavailable: ${instance.type}`);+ return adapter;+ },+ });+ }+}diff --git a/src/client/src/lib/platform/connectors/datasource/jaeger/adapter.ts b/src/client/src/lib/platform/connectors/datasource/jaeger/adapter.tsnew file mode 100644index 00000000..054d581b--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/jaeger/adapter.ts@@ -0,0 +1,770 @@+/**+ * Jaeger DataSourceAdapter (traces only).+ *+ * HTTP + span normalization come from `@openplait/adapter-jaeger`. OpenLIT keeps+ * project-scoped secrets, SSRF-safe fetch, caching, AI-selector filtering, and+ * in-process L1 aggregation (Jaeger cannot aggregate server-side).+ */++import {+ JaegerAdapter as OpenPlaitJaegerAdapter,+ flattenJaegerTraces,+ type JaegerAdapterConfig,+ type JaegerNormalizedSpan,+ type JaegerTrace,+} from "@openplait/adapter-jaeger";+import { BaseExternalAdapter } from "../base-adapter";+import type {+ AISignalValidation,+ DataFrame,+ DiscoveredService,+ HealthCheckResult,+ NormalizedFilter,+ NormalizedSpan,+ OpenLITQuery,+ QueryTimeRange,+ ServiceRollup,+ Signal,+ SourceCapabilities,+ SourceTypeDescriptor,+ TelemetrySourceDescriptor,+} from "../types";+import { applyHttpAuthCredentials } from "../http/auth-headers";+import { httpVendorFields } from "../config-fields";+import getMessage from "@/constants/messages";+import {+ safeFetch,+ selfHostedNetworkOptions,+ SourceResponseError,+} from "../http/safe-fetch";+import { cacheKey, cachedQuery } from "../http/cache";+import { resolveSourceSecret, redactableSecretValues } from "../http/secret";+import { spanMatchesAISelector, traceMatchesAISelector } from "../selector-match";+import {+ computeAggregateSpansL1,+ computeDistinctValuesL1,+} from "../l1-compute";+import { spanFieldValue } from "../graph/sample-aggregate";+import { mapPool } from "../graph/map-pool";++const TTL_MS = 30_000;+const MAX_SERVICES = 50;+const MAX_QUERY_SERVICES = 24;+const SERVICE_TRACE_TIMEOUT_MS = 15_000;+const SERVICE_TRACE_CONCURRENCY = 8;+const SPAN_INDEX_MAX = 5_000;+const ZERO_PARENT = new Set(["", "0".repeat(16), "0".repeat(32)]);++const spanIndexBySource = new Map<string, Map<string, NormalizedSpan>>();+const timedOutServices = new Map<string, number>();++function rememberSpans(sourceId: string, spans: NormalizedSpan[]) {+ let index = spanIndexBySource.get(sourceId);+ if (!index) {+ index = new Map();+ spanIndexBySource.set(sourceId, index);+ }+ for (const span of spans) {+ if (!span.spanId) continue;+ if (index.size >= SPAN_INDEX_MAX) {+ const oldest = index.keys().next().value;+ if (oldest) index.delete(oldest);+ }+ index.set(span.spanId, span);+ }+}++function toNormalizedSpan(span: JaegerNormalizedSpan): NormalizedSpan {+ return {+ traceId: span.traceId,+ spanId: span.spanId,+ parentSpanId: span.parentSpanId,+ name: span.name,+ serviceName: span.serviceName,+ timestamp: span.timestamp,+ durationNs: span.durationNs,+ statusCode: span.statusCode,+ ...(span.spanKind ? { spanKind: span.spanKind } : {}),+ spanAttributes: span.spanAttributes,+ resourceAttributes: span.resourceAttributes,+ events: span.events,+ ...(span.cost !== undefined ? { cost: span.cost } : {}),+ };+}++function pickRootSpan(spans: NormalizedSpan[]): NormalizedSpan | undefined {+ if (spans.length === 0) return undefined;+ const ids = new Set(spans.map((s) => s.spanId));+ const explicit =+ spans.find((s) => ZERO_PARENT.has(s.parentSpanId || "")) ||+ spans.find((s) => s.parentSpanId && !ids.has(s.parentSpanId));+ if (explicit) return explicit;+ return [...spans].sort((a, b) => {+ const ta = a.timestamp ? new Date(a.timestamp).getTime() : 0;+ const tb = b.timestamp ? new Date(b.timestamp).getTime() : 0;+ return ta - tb;+ })[0];+}++function spanMatchesFilters(+ span: NormalizedSpan,+ filters?: NormalizedFilter[]+): boolean {+ if (!filters?.length) return true;+ return filters.every((filter) => {+ if (filter.target === "spanName") {+ const values = Array.isArray(filter.value)+ ? filter.value.map(String)+ : [String(filter.value || "")];+ return values.includes(span.name);+ }+ if (filter.target === "status") {+ const values = Array.isArray(filter.value)+ ? filter.value.map(String)+ : [String(filter.value || "")];+ const wantsError = values.some(+ (v) => /error/i.test(v) || v === "STATUS_CODE_ERROR"+ );+ const isError = /error/i.test(span.statusCode || "");+ return wantsError ? isError : !isError;+ }+ if (filter.target === "attribute" && filter.key) {+ const raw = spanFieldValue(span, filter.key);+ const value = raw === undefined ? undefined : String(raw);+ if (filter.op === "exists") return !!value;+ if (filter.op === "eq") return value === String(filter.value ?? "");+ if (filter.op === "neq") return value !== String(filter.value ?? "");+ if (filter.op === "in") {+ const values = Array.isArray(filter.value)+ ? filter.value.map(String)+ : [String(filter.value || "")];+ return value !== undefined && values.includes(value);+ }+ }+ return true;+ });+}++/** Prefer explicit service.name filters so we do not fan out across every service. */+function servicesFromFilters(filters?: NormalizedFilter[]): string[] | undefined {+ if (!filters?.length) return undefined;+ const matched = new Set<string>();+ for (const filter of filters) {+ if (filter.target !== "attribute" || filter.key !== "service.name") continue;+ if (filter.op === "eq" && filter.value != null) {+ matched.add(String(filter.value));+ } else if (filter.op === "in") {+ const values = Array.isArray(filter.value)+ ? filter.value+ : [filter.value];+ for (const value of values) {+ if (value != null && String(value)) matched.add(String(value));+ }+ }+ }+ return matched.size ? Array.from(matched) : undefined;+}++/** Map UI span-name filters onto Jaeger's single `operation` search param. */+function operationFromFilters(filters?: NormalizedFilter[]): string | undefined {+ if (!filters?.length) return undefined;+ for (const filter of filters) {+ if (filter.target !== "spanName") continue;+ if (filter.op === "eq" && filter.value != null && String(filter.value)) {+ return String(filter.value);+ }+ if (filter.op === "in") {+ const values = (Array.isArray(filter.value) ? filter.value : [filter.value])+ .map((value) => String(value || ""))+ .filter(Boolean);+ // Jaeger accepts one operation per search; use the first selected name.+ if (values[0]) return values[0];+ }+ }+ return undefined;+}++export class JaegerAdapter extends BaseExternalAdapter {+ readonly type = "jaeger";+ /** Jaeger already fans out `/api/traces?service=` per service. */+ readonly samplesAreServiceStratified = true;+ private apiBaseUrl = this.baseUrl;++ private get baseUrl(): string {+ return String(this.descriptor.settings.url || "").replace(/\/$/, "");+ }+ private get networkOpts() {+ return selfHostedNetworkOptions(this.descriptor.settings);+ }+ private get configuredServices(): string[] | undefined {+ const s = this.descriptor.settings.services;+ const services = Array.isArray(s) ? s.map(String).filter(Boolean) : [];+ return services.length ? services : undefined;+ }+ private get perServiceLimit(): number {+ return Number(this.descriptor.settings.perServiceLimit) || 100;+ }++ private async authHeaders() {+ const secret = await resolveSourceSecret(+ this.descriptor.secretRef,+ this.descriptor.dbConfigId,+ this.descriptor.projectId+ );+ return {+ headers: applyHttpAuthCredentials(secret.credentials, {+ authType: this.descriptor.settings.authType as string | undefined,+ }),+ redact: redactableSecretValues(secret),+ };+ }++ /**+ * Bind OpenPlait's Jaeger adapter to OpenLIT's guarded HTTP transport.+ */+ private async openPlaitAdapter(): Promise<OpenPlaitJaegerAdapter> {+ const { headers, redact } = await this.authHeaders();+ const guardedFetch = (async (+ input: RequestInfo | URL,+ init?: RequestInit+ ): Promise<Response> => {+ const url =+ typeof input === "string"+ ? input+ : input instanceof URL+ ? input.toString()+ : input.url;+ const requestHeaders = Object.fromEntries(+ new Headers(init?.headers).entries()+ );+ let payload: unknown;+ try {+ payload = await safeFetch<unknown>(url, {+ method: init?.method || "GET",+ headers: requestHeaders,+ ...this.networkOpts,+ redactValues: redact,+ timeoutMs: SERVICE_TRACE_TIMEOUT_MS,+ concurrencyKey: this.descriptor.id,+ retry: true,+ });+ } catch (error) {+ if (error instanceof SourceResponseError) {+ return {+ ok: false,+ status: error.status,+ statusText: "Upstream Jaeger error",+ headers: new Headers({ "Content-Type": "text/plain" }),+ json: async () => ({ error: error.message }),+ text: async () => error.message,+ } as Response;+ }+ throw error;+ }+ return {+ ok: true,+ status: 200,+ statusText: "OK",+ headers: new Headers({ "Content-Type": "application/json" }),+ json: async () => payload,+ text: async () => JSON.stringify(payload),+ } as Response;+ }) as typeof fetch;++ const config: JaegerAdapterConfig = {+ url: this.apiBaseUrl || this.baseUrl,+ httpHeaders: headers,+ requestTimeoutMs: SERVICE_TRACE_TIMEOUT_MS,+ maxResultRows: 1_000,+ perServiceLimit: this.perServiceLimit,+ ...(this.configuredServices ? { services: this.configuredServices } : {}),+ };+ return new OpenPlaitJaegerAdapter(config, { fetch: guardedFetch });+ }++ capabilities(): SourceCapabilities {+ return {+ signals: ["traces"],+ traceTree: true,+ spanEvents: true,+ serverAggregation: false,+ spanMutation: false,+ distinctValues: true,+ crossTraceSession: false,+ rawQuery: false,+ };+ }++ async healthCheck(): Promise<HealthCheckResult> {+ const start = Date.now();+ try {+ // Build a probe adapter without the configured-services short-circuit so+ // Test Connection always hits Jaeger Query Service.+ const { headers, redact } = await this.authHeaders();+ const probeFetch = (async (+ input: RequestInfo | URL,+ init?: RequestInit+ ): Promise<Response> => {+ const url =+ typeof input === "string"+ ? input+ : input instanceof URL+ ? input.toString()+ : input.url;+ const requestHeaders = Object.fromEntries(+ new Headers(init?.headers).entries()+ );+ try {+ const payload = await safeFetch<unknown>(url, {+ method: init?.method || "GET",+ headers: requestHeaders,+ ...this.networkOpts,+ redactValues: redact,+ timeoutMs: SERVICE_TRACE_TIMEOUT_MS,+ concurrencyKey: `${this.descriptor.id}:health`,+ retry: true,+ });+ return {+ ok: true,+ status: 200,+ statusText: "OK",+ headers: new Headers({ "Content-Type": "application/json" }),+ json: async () => payload,+ text: async () => JSON.stringify(payload),+ } as Response;+ } catch (error) {+ if (error instanceof SourceResponseError) {+ return {+ ok: false,+ status: error.status,+ statusText: "Upstream Jaeger error",+ headers: new Headers({ "Content-Type": "text/plain" }),+ json: async () => ({ error: error.message }),+ text: async () => error.message,+ } as Response;+ }+ throw error;+ }+ }) as typeof fetch;++ const probe = new OpenPlaitJaegerAdapter(+ {+ url: this.baseUrl,+ httpHeaders: headers,+ requestTimeoutMs: SERVICE_TRACE_TIMEOUT_MS,+ },+ { fetch: probeFetch }+ );+ const services = await probe.listServices();+ this.apiBaseUrl = probe.resolvedUrl;+ if (!services.length) {+ return {+ ok: false,+ latencyMs: Date.now() - start,+ message: "Jaeger returned no services",+ };+ }+ return { ok: true, latencyMs: Date.now() - start };+ } catch (err) {+ return { ok: false, message: String((err as Error)?.message || err) };+ }+ }++ private async listServices(): Promise<string[]> {+ if (this.configuredServices) return this.configuredServices;+ const key = cacheKey(this.descriptor.id, ["services"]);+ const services = await cachedQuery(key, TTL_MS, async () => {+ const adapter = await this.openPlaitAdapter();+ const discovered = await adapter.listServices();+ this.apiBaseUrl = adapter.resolvedUrl;+ return discovered.map(String).filter(Boolean).slice(0, MAX_SERVICES);+ });+ return services;+ }++ private async fetchServiceTraces(+ service: string,+ window: QueryTimeRange,+ limit: number,+ operation?: string+ ): Promise<JaegerTrace[]> {+ const key = cacheKey(this.descriptor.id, [+ "traces",+ service,+ operation || "",+ String(window.start.getTime()),+ String(window.end.getTime()),+ String(limit),+ ]);+ return cachedQuery(key, TTL_MS, async () => {+ const adapter = await this.openPlaitAdapter();+ return adapter.searchTraces({+ service,+ startMs: window.start.getTime(),+ endMs: window.end.getTime(),+ limit,+ ...(operation ? { operation } : {}),+ });+ });+ }++ /** GET /api/services/{service}/operations — Jaeger Search operation enum. */+ private async listOperations(service: string): Promise<string[]> {+ const key = cacheKey(this.descriptor.id, ["operations", service]);+ return cachedQuery(key, TTL_MS, async () => {+ const adapter = await this.openPlaitAdapter();+ const base = adapter.resolvedUrl.replace(/\/$/, "");+ const { headers, redact } = await this.authHeaders();+ const payload = await safeFetch<{ data?: string[] }>(+ `${base}/api/services/${encodeURIComponent(service)}/operations`,+ {+ method: "GET",+ headers: { Accept: "application/json", ...headers },+ ...this.networkOpts,+ redactValues: redact,+ timeoutMs: SERVICE_TRACE_TIMEOUT_MS,+ concurrencyKey: this.descriptor.id,+ retry: true,+ }+ );+ return (payload?.data || []).map(String).filter(Boolean);+ });+ }++ private async resolveQueryServices(query: OpenLITQuery): Promise<string[]> {+ const filteredServices = servicesFromFilters(query.filters);+ const discoveredServices = filteredServices?.length+ ? filteredServices+ : await this.listServices();+ return discoveredServices.slice(0, MAX_QUERY_SERVICES);+ }++ /**+ * Search Jaeger by **trace** budget (not span count). Fat traces (1000++ * spans) previously filled a span budget after one Cursor session and made+ * the Telemetry list show "1 of 1".+ */+ private async collectTraces(+ query: OpenLITQuery,+ maxTraces: number+ ): Promise<JaegerTrace[]> {+ const services = await this.resolveQueryServices(query);+ const operation = operationFromFilters(query.filters);+ const perServiceLimit = Math.min(+ this.perServiceLimit,+ Math.max(maxTraces, 25)+ );++ const traceBatches = await mapPool(+ services,+ SERVICE_TRACE_CONCURRENCY,+ async (service) => {+ const failureKey = `${this.descriptor.id}:${service}`;+ const retryAfter = timedOutServices.get(failureKey) || 0;+ if (retryAfter > Date.now()) {+ return [] as JaegerTrace[];+ }+ try {+ const traces = await this.fetchServiceTraces(+ service,+ query.timeRange,+ perServiceLimit,+ operation+ );+ timedOutServices.delete(failureKey);+ return traces;+ } catch {+ timedOutServices.set(failureKey, Date.now() + 30_000);+ return [] as JaegerTrace[];+ }+ }+ );++ const byId = new Map<string, { trace: JaegerTrace; startMs: number }>();+ for (const trace of traceBatches.flat()) {+ const traceId = String(trace.traceID || "");+ if (!traceId || byId.has(traceId)) continue;+ const spans = flattenJaegerTraces([trace]).map(toNormalizedSpan);+ if (query.aiSelector !== false && !traceMatchesAISelector(spans)) {+ continue;+ }+ if (query.filters?.length) {+ const anyMatch = spans.some((span) =>+ spanMatchesFilters(span, query.filters)+ );+ if (!anyMatch) continue;+ }+ const root = pickRootSpan(spans);+ const startMs = root?.timestamp+ ? new Date(root.timestamp).getTime()+ : Math.min(+ ...spans.map((s) =>+ s.timestamp ? new Date(s.timestamp).getTime() : Date.now()+ )+ );+ byId.set(traceId, { trace, startMs: Number.isFinite(startMs) ? startMs : 0 });+ }++ const sorted = Array.from(byId.values())+ .sort((a, b) => b.startMs - a.startMs)+ .slice(0, Math.max(1, maxTraces))+ .map((entry) => entry.trace);++ return sorted;+ }++ private spansFromTraces(traces: JaegerTrace[]): NormalizedSpan[] {+ const spans = flattenJaegerTraces(traces).map(toNormalizedSpan);+ rememberSpans(this.descriptor.id, spans);+ return spans;+ }++ /** Legacy helper: flatten accepted traces, optionally capping total spans. */+ private async collectSpans(+ query: OpenLITQuery,+ maxSpans: number+ ): Promise<NormalizedSpan[]> {+ const maxTraces = Math.min(Math.max(25, Math.ceil(maxSpans / 20)), 200);+ const traces = await this.collectTraces(query, maxTraces);+ return this.spansFromTraces(traces).slice(0, maxSpans);+ }++ /**+ * Explore-style list: one row per Jaeger search hit (root / orphan top).+ * Full trees load on detail via getTraceSpans.+ */+ async listSpans(query: OpenLITQuery): Promise<DataFrame<NormalizedSpan>> {+ const start = Date.now();+ const pageSize = Math.min(query.limit || 25, 200);+ const offset = Math.max(0, query.offset || 0);+ const traces = await this.collectTraces(query, offset + pageSize);+ const roots = traces+ .map((trace) => pickRootSpan(flattenJaegerTraces([trace]).map(toNormalizedSpan)))+ .filter((span): span is NormalizedSpan => !!span);+ rememberSpans(this.descriptor.id, this.spansFromTraces(traces));+ const rows = roots.slice(offset, offset + pageSize);+ return {+ fields: [],+ rows,+ meta: {+ latencyMs: Date.now() - start,+ truncated: roots.length >= offset + pageSize,+ degraded: ["serverAggregation"],+ },+ };+ }++ async countTraces(+ query: OpenLITQuery+ ): Promise<{ total: number; truncated: boolean }> {+ const traces = await this.collectTraces(query, 200);+ return { total: traces.length, truncated: traces.length >= 200 };+ }++ /**+ * Trace-level volume series from Jaeger search hits (not child-span flood).+ * Powers Telemetry summary bars with unique-trace counts per bucket.+ */+ async traceTimeSeries(query: OpenLITQuery): Promise<DataFrame> {+ const start = Date.now();+ const traces = await this.collectTraces(query, 200);+ const roots = traces+ .map((trace) => pickRootSpan(flattenJaegerTraces([trace]).map(toNormalizedSpan)))+ .filter((span): span is NormalizedSpan => !!span);+ rememberSpans(this.descriptor.id, this.spansFromTraces(traces));+ const { bucketSpansByInterval } = await import("../graph/sample-aggregate");+ const frame = bucketSpansByInterval(+ roots,+ query.interval || "1h",+ query.aggregations || [{ fn: "count" }],+ query.timeRange+ );+ return {+ ...frame,+ meta: {+ ...frame.meta,+ latencyMs: Date.now() - start,+ freshness: "sampled",+ truncated: traces.length >= 200,+ degraded: ["serverAggregation"],+ rowsScanned: traces.length,+ },+ };+ }++ async getTraceSpans(traceId: string): Promise<NormalizedSpan[]> {+ const key = cacheKey(this.descriptor.id, ["trace", traceId]);+ const spans = await cachedQuery(key, TTL_MS, async () => {+ const adapter = await this.openPlaitAdapter();+ const trace = await adapter.getTrace(traceId);+ if (!trace) return [] as NormalizedSpan[];+ return flattenJaegerTraces([trace]).map(toNormalizedSpan);+ });+ rememberSpans(this.descriptor.id, spans);+ return spans;+ }++ async getSpan(spanId: string): Promise<NormalizedSpan | null> {+ const indexed = spanIndexBySource.get(this.descriptor.id)?.get(spanId);+ if (indexed) return indexed;++ const end = new Date();+ const start = new Date(end.getTime() - 7 * 24 * 60 * 60 * 1000);+ const traces = await this.collectTraces(+ { signal: "traces", timeRange: { start, end }, aiSelector: false },+ 50+ );+ const rows = this.spansFromTraces(traces);+ return rows.find((span) => span.spanId === spanId) || null;+ }++ async sampleTracesForGraph(+ query: OpenLITQuery,+ maxTraces: number+ ): Promise<NormalizedSpan[]> {+ const traces = await this.collectTraces(+ query,+ Math.min(maxTraces || 100, 100)+ );+ // Cap flattened spans so one Cursor session cannot explode L1 memory.+ return this.spansFromTraces(traces).slice(0, 5_000);+ }++ async aggregateSpans(query: OpenLITQuery): Promise<DataFrame> {+ return computeAggregateSpansL1(this, query);+ }++ async spanTimeSeries(query: OpenLITQuery): Promise<DataFrame> {+ return this.traceTimeSeries(query);+ }++ async distinctValues(key: string, query: OpenLITQuery): Promise<string[]> {+ const normalized = key.replace(/^SpanAttributes\./, "").replace(/^ResourceAttributes\./, "");+ if (+ normalized === "SpanName" ||+ normalized === "span.name" ||+ normalized === "name"+ ) {+ const services = await this.resolveQueryServices(query);+ const ops = new Set<string>();+ await mapPool(services, SERVICE_TRACE_CONCURRENCY, async (service) => {+ try {+ for (const op of await this.listOperations(service)) ops.add(op);+ } catch {+ // Fall through to L1 sample below when operations endpoint fails.+ }+ });+ if (ops.size) return Array.from(ops).sort();+ }+ if (normalized === "service.name" || normalized === "ServiceName") {+ return this.resolveQueryServices(query);+ }+ return computeDistinctValuesL1(this, key, query);+ }++ async attributeKeys(+ _signal: Signal,+ window: QueryTimeRange+ ): Promise<string[]> {+ const traces = await this.collectTraces(+ { signal: "traces", timeRange: window, aiSelector: false },+ 40+ );+ const spans = this.spansFromTraces(traces).slice(0, 2_000);+ const keys = new Set<string>();+ for (const span of spans) {+ for (const key of Object.keys(span.spanAttributes || {})) keys.add(key);+ for (const key of Object.keys(span.resourceAttributes || {})) keys.add(key);+ }+ return Array.from(keys).sort();+ }++ async discoverServices(_window: QueryTimeRange): Promise<DiscoveredService[]> {+ // Application Names filter should mirror Jaeger Search's service dropdown+ // (`GET /api/services`), not a sampled AI-only subset.+ const services = await this.listServices();+ return services.map((serviceName) => ({+ serviceName,+ environment: "",+ clusterId: "",+ }));+ }++ async aggregateByService(window: QueryTimeRange): Promise<ServiceRollup[]> {+ const discovered = await this.discoverServices(window);+ const rollups = await mapPool(+ discovered,+ SERVICE_TRACE_CONCURRENCY,+ async (svc) => {+ const traces = await this.fetchServiceTraces(svc.serviceName, window, 20);+ const spans = flattenJaegerTraces(traces).map(toNormalizedSpan);+ const models = new Set<string>();+ const providers = new Set<string>();+ for (const span of spans) {+ const model = span.spanAttributes["gen_ai.request.model"];+ const provider = span.spanAttributes["gen_ai.system"];+ if (model) models.add(model);+ if (provider) providers.add(provider);+ }+ return {+ serviceName: svc.serviceName,+ environment: svc.environment || "default",+ clusterId: svc.clusterId || "default",+ requestCount: new Set(spans.map((s) => s.traceId).filter(Boolean)).size,+ models: Array.from(models),+ providers: Array.from(providers),+ } satisfies ServiceRollup;+ }+ );+ return rollups;+ }++ async validateAISignal(window: QueryTimeRange): Promise<AISignalValidation> {+ try {+ const traces = await this.collectTraces(+ { signal: "traces", timeRange: window, aiSelector: true },+ 1+ );+ return {+ ok: traces.length > 0,+ sampleCount: traces.length,+ missingAttributes: [],+ };+ } catch (err) {+ return {+ ok: false,+ sampleCount: 0,+ missingAttributes: [],+ message: String((err as Error)?.message || err),+ };+ }+ }+}++export const jaegerAdapterFactory = {+ type: "jaeger",+ create: (descriptor: TelemetrySourceDescriptor) => new JaegerAdapter(descriptor),+ describe: (): SourceTypeDescriptor => ({+ type: "jaeger",+ displayName: "Jaeger",+ declaredSignals: ["traces"],+ capabilities: {+ traceTree: true,+ spanEvents: true,+ serverAggregation: false,+ spanMutation: false,+ distinctValues: true,+ crossTraceSession: false,+ rawQuery: false,+ },+ correlation: {+ crossSignal: true,+ keys: ["traceId", "spanId", "service"],+ },+ configFields: httpVendorFields({+ placeholder: "https://jaeger.example.com",+ }),+ authStyle: "http",+ authHelp: getMessage().DATA_SOURCE_AUTH_HELP_HTTP,+ }),+};diff --git a/src/client/src/lib/platform/connectors/datasource/l1-compute.ts b/src/client/src/lib/platform/connectors/datasource/l1-compute.tsnew file mode 100644index 00000000..19fd3d45--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/l1-compute.ts@@ -0,0 +1,86 @@+import type { DataFrame, OpenLITQuery } from "./types";+import {+ fetchSpansForAggregation,+ collapseToRootSpans,+ type SampleFetchSource,+} from "./graph/sample-fetch";+import {+ aggregateSpansInProcess,+ bucketSpansByInterval,+ distinctFromSpans,+} from "./graph/sample-aggregate";++function withL1Meta(+ frame: DataFrame,+ opts: {+ start: number;+ spansScanned: number;+ truncated: boolean;+ }+): DataFrame {+ return {+ ...frame,+ meta: {+ ...frame.meta,+ degraded: ["serverAggregation"],+ freshness: "sampled",+ truncated: opts.truncated,+ latencyMs: Date.now() - opts.start,+ rowsScanned: opts.spansScanned,+ },+ };+}++/** L1: sample spans then groupBy + aggregate in-process. */+export async function computeAggregateSpansL1(+ source: SampleFetchSource,+ query: OpenLITQuery+): Promise<DataFrame> {+ const start = Date.now();+ const { spans, truncated } = await fetchSpansForAggregation(source, query);+ // Trace summaries and request totals are trace-level metrics. External+ // adapters return full traces, so aggregating every child span inflates the+ // count (for example, one trace with 200 spans appeared as 200 requests).+ const traceRoots = collapseToRootSpans(spans);+ const frame = aggregateSpansInProcess(+ traceRoots,+ query.groupBy || [],+ query.aggregations || [{ fn: "count" }]+ );+ return withL1Meta(frame, {+ start,+ spansScanned: spans.length,+ truncated,+ });+}++/** L1: sample spans then bucket by interval in-process. */+export async function computeSpanTimeSeriesL1(+ source: SampleFetchSource,+ query: OpenLITQuery+): Promise<DataFrame> {+ const start = Date.now();+ const { spans, truncated } = await fetchSpansForAggregation(source, query);+ const traceRoots = collapseToRootSpans(spans);+ const frame = bucketSpansByInterval(+ traceRoots,+ query.interval || "1h",+ query.aggregations || [{ fn: "count" }],+ query.timeRange+ );+ return withL1Meta(frame, {+ start,+ spansScanned: spans.length,+ truncated,+ });+}++/** L1: sample spans then collect distinct field values. */+export async function computeDistinctValuesL1(+ source: SampleFetchSource,+ key: string,+ query: OpenLITQuery+): Promise<string[]> {+ const { spans } = await fetchSpansForAggregation(source, query);+ return distinctFromSpans(spans, key);+}diff --git a/src/client/src/lib/platform/connectors/datasource/otlp-json.ts b/src/client/src/lib/platform/connectors/datasource/otlp-json.tsnew file mode 100644index 00000000..a63de9c4--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/otlp-json.ts@@ -0,0 +1,168 @@+/**+ * Parse OTLP/JSON trace payloads into NormalizedSpan[].+ *+ * Tempo (`GET /api/traces/{id}`) and Jaeger v3 (`GET /api/v3/traces/{id}`)+ * both return OTLP/JSON: a top-level `batches` (or `resourceSpans`) array,+ * each with a `resource.attributes` list and `scopeSpans`/`instrumentationLibrarySpans`+ * holding spans. Attribute values are the OTLP AnyValue shape+ * (`{ stringValue | intValue | doubleValue | boolValue }`).+ */++import type { NormalizedSpan, NormalizedSpanEvent } from "./types";++interface OtlpAnyValue {+ stringValue?: string;+ intValue?: string | number;+ doubleValue?: number;+ boolValue?: boolean;+ arrayValue?: { values?: OtlpAnyValue[] };+}++interface OtlpKeyValue {+ key: string;+ value?: OtlpAnyValue;+}++interface OtlpSpan {+ traceId?: string;+ spanId?: string;+ parentSpanId?: string;+ name?: string;+ kind?: string | number;+ startTimeUnixNano?: string | number;+ endTimeUnixNano?: string | number;+ attributes?: OtlpKeyValue[];+ status?: { code?: string | number; message?: string };+ events?: {+ name?: string;+ timeUnixNano?: string | number;+ attributes?: OtlpKeyValue[];+ }[];+}++interface OtlpScopeSpans {+ spans?: OtlpSpan[];+}++interface OtlpResourceSpans {+ resource?: { attributes?: OtlpKeyValue[] };+ scopeSpans?: OtlpScopeSpans[];+ instrumentationLibrarySpans?: OtlpScopeSpans[];+}++interface OtlpTrace {+ trace?: OtlpTrace;+ batches?: OtlpResourceSpans[];+ resourceSpans?: OtlpResourceSpans[];+}++function anyValueToString(v?: OtlpAnyValue): string {+ if (!v) return "";+ if (v.stringValue !== undefined) return v.stringValue;+ if (v.intValue !== undefined) return String(v.intValue);+ if (v.doubleValue !== undefined) return String(v.doubleValue);+ if (v.boolValue !== undefined) return String(v.boolValue);+ if (v.arrayValue?.values) {+ return JSON.stringify(v.arrayValue.values.map(anyValueToString));+ }+ return "";+}++function attrsToMap(attrs?: OtlpKeyValue[]): Record<string, string> {+ const out: Record<string, string> = {};+ for (const kv of attrs || []) {+ if (kv.key) out[kv.key] = anyValueToString(kv.value);+ }+ return out;+}++function nanoToIso(nano?: string | number): string {+ if (nano === undefined) return "";+ const ns = typeof nano === "string" ? Number(nano) : nano;+ if (!Number.isFinite(ns)) return "";+ return new Date(ns / 1e6).toISOString();+}++function durationNs(+ start?: string | number,+ end?: string | number+): number {+ const s = Number(start);+ const e = Number(end);+ if (!Number.isFinite(s) || !Number.isFinite(e)) return 0;+ return Math.max(0, e - s);+}++function statusCode(code?: string | number): string {+ if (code === undefined) return "";+ // OTLP status: 0 UNSET, 1 OK, 2 ERROR+ if (code === 2 || code === "STATUS_CODE_ERROR" || code === "ERROR")+ return "STATUS_CODE_ERROR";+ if (code === 1 || code === "STATUS_CODE_OK" || code === "OK")+ return "STATUS_CODE_OK";+ return String(code);+}++function normalizeEvents(span: OtlpSpan): NormalizedSpanEvent[] {+ return (span.events || []).map((e) => ({+ name: e.name || "",+ timestamp: e.timeUnixNano ? nanoToIso(e.timeUnixNano) : undefined,+ attributes: attrsToMap(e.attributes),+ }));+}++/**+ * Tempo `/api/traces/{id}` often encodes binary IDs as base64 in OTLP/JSON,+ * while TraceQL search and `/api/traces/{hex}` expect lowercase hex. Normalize+ * so list rows, detail lookups, and TraceQL `{ span:id = … }` stay aligned.+ */+export function normalizeOtlpId(raw?: string): string {+ if (!raw) return "";+ const value = String(raw).trim();+ if (!value) return "";+ if (/^[0-9a-fA-F]+$/.test(value) && value.length % 2 === 0) {+ return value.toLowerCase();+ }+ try {+ const buf = Buffer.from(value, "base64");+ if (buf.length === 8 || buf.length === 16) {+ return buf.toString("hex");+ }+ } catch {+ // keep original+ }+ return value;+}++/** Parse an OTLP/JSON trace payload into normalized spans. */+export function parseOtlpTrace(payload: unknown): NormalizedSpan[] {+ const root = (payload || {}) as OtlpTrace;+ const trace = root.trace || root;+ const resourceSpans = trace.batches || trace.resourceSpans || [];+ const out: NormalizedSpan[] = [];+ for (const rs of resourceSpans) {+ const resourceAttributes = attrsToMap(rs.resource?.attributes);+ const serviceName = resourceAttributes["service.name"] || "";+ const scopes = rs.scopeSpans || rs.instrumentationLibrarySpans || [];+ for (const scope of scopes) {+ for (const span of scope.spans || []) {+ out.push({+ traceId: normalizeOtlpId(span.traceId),+ spanId: normalizeOtlpId(span.spanId),+ parentSpanId: normalizeOtlpId(span.parentSpanId),+ name: String(span.name || ""),+ serviceName,+ timestamp: nanoToIso(span.startTimeUnixNano),+ durationNs: durationNs(span.startTimeUnixNano, span.endTimeUnixNano),+ statusCode: statusCode(span.status?.code),+ statusMessage: span.status?.message,+ spanKind: span.kind !== undefined ? String(span.kind) : undefined,+ spanAttributes: attrsToMap(span.attributes),+ resourceAttributes,+ events: normalizeEvents(span),+ });+ }+ }+ }+ return out;+}diff --git a/src/client/src/lib/platform/connectors/datasource/prometheus/adapter.ts b/src/client/src/lib/platform/connectors/datasource/prometheus/adapter.tsnew file mode 100644index 00000000..b9ee0d7e--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/prometheus/adapter.ts@@ -0,0 +1,150 @@+import { PrometheusAdapter as OpenPlaitPrometheusAdapter } from "@openplait/adapter-prometheus";+import { OpenPlaitHttpAdapter } from "../grafana/openplait-http";+import type { DataFrame, HealthCheckResult, NormalizedMetricPoint, OpenLITQuery, QueryTimeRange, Signal, SourceCapabilities, SourceTypeDescriptor, TelemetrySourceDescriptor } from "../types";+import { openPlaitFramesToRows } from "@/lib/platform/openplait/frames";+import { computeIntervalMs, clampStepMs, intervalMsToLabel, rateIntervalMs } from "../downsample";+import { httpVendorFields } from "../config-fields";+import getMessage from "@/constants/messages";+import { SourceResponseError } from "../http/safe-fetch";++const LABELS: Record<string, string> = {+ "service.name": "service_name",+ serviceName: "service_name",+ "metric.name": "__name__",+ metricName: "__name__",+ job: "job",+ instance: "instance",+};++function escape(value: string): string {+ return `"${value+ .replace(/\\/g, "\\\\")+ .replace(/"/g, '\\"')+ .replace(/[\r\n\0]/g, " ")}"`;+}++/** Escape PromQL regex metacharacters inside `=~` / `!~` matchers. */+function escapeRegex(value: string): string {+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");+}++function labelName(key: string): string {+ const mapped = LABELS[key] || key;+ return /^[A-Za-z_][A-Za-z0-9_]*$/.test(mapped)+ ? mapped+ : mapped.replace(/[^A-Za-z0-9_]/g, "_");+}++function filterValues(value: unknown): string[] {+ const raw = Array.isArray(value) ? value : [value];+ return raw+ .filter((item) => item !== undefined && item !== null && String(item) !== "")+ .map(String);+}++/** Build a PromQL label matcher, using `=~` when multiple values are selected. */+function labelMatcher(+ name: string,+ values: string[],+ op: "eq" | "neq" | "in" | "notIn" | "contains" | string | undefined+): string {+ if (values.length === 0) return "";+ if (op === "contains") {+ return `${name}=~${escape(`.*${escapeRegex(values[0])}.*`)}`;+ }+ const negative = op === "neq" || op === "notIn";+ if (values.length === 1 && op !== "in" && op !== "notIn") {+ return `${name}${negative ? "!=" : "="}${escape(values[0])}`;+ }+ return `${name}${negative ? "!~" : "=~"}${escape(+ values.map(escapeRegex).join("|")+ )}`;+}++export function prometheusSelector(query: OpenLITQuery): string {+ const matchers: string[] = [];+ for (const filter of query.filters || []) {+ if (filter.target === "spanName") {+ const matcher = labelMatcher("__name__", filterValues(filter.value), filter.op);+ if (matcher) matchers.push(matcher);+ continue;+ }+ if (filter.target !== "attribute" || !filter.key) continue;+ const matcher = labelMatcher(+ labelName(filter.key),+ filterValues(filter.value),+ filter.op+ );+ if (matcher) matchers.push(matcher);+ }+ if (!matchers.some((item) => item.startsWith("__name__"))) {+ matchers.unshift('__name__=~".+"');+ }+ return `{${matchers.join(",")}}`;+}++function selector(query: OpenLITQuery): string {+ return prometheusSelector(query);+}++export class PrometheusAdapter extends OpenPlaitHttpAdapter {+ readonly type = "prometheus";+ constructor(descriptor: TelemetrySourceDescriptor) { super(descriptor); }+ private async adapter(): Promise<OpenPlaitPrometheusAdapter> { const connection = await this.openPlaitConnection(); return new OpenPlaitPrometheusAdapter({ url: this.baseUrl, httpHeaders: connection.headers, allowNativeQueries: true, maxResultRows: this.positiveSetting("maxResultRows") || 100_000, maxSeries: this.positiveSetting("maxSeries") || 10_000, maxTimeRangeMs: this.positiveSetting("maxTimeRangeMs"), maxLookbackMs: this.positiveSetting("maxLookbackMs"), defaultStep: String(this.descriptor.settings.defaultStep || "30s") }, { fetch: connection.fetch }); }+ capabilities(): SourceCapabilities { return { signals: ["metrics"], traceTree: false, spanEvents: false, serverAggregation: true, spanMutation: false, distinctValues: true, crossTraceSession: false, maxLookbackMs: this.positiveSetting("maxLookbackMs"), maxTimeRangeMs: this.positiveSetting("maxTimeRangeMs"), rawQuery: false }; }+ async healthCheck(): Promise<HealthCheckResult> { const started = Date.now(); try { await (await this.adapter()).labelNames({ timeoutMs: 10_000 }); return { ok: true, latencyMs: Date.now() - started }; } catch (error) { return { ok: false, latencyMs: Date.now() - started, message: String((error as Error)?.message || error) }; } }+ private range(query: OpenLITQuery) { return { from: query.timeRange.start.toISOString(), to: query.timeRange.end.toISOString() }; }+ private stepLabel(query: OpenLITQuery): string {+ const rangeMs = Math.max(0, query.timeRange.end.getTime() - query.timeRange.start.getTime());+ return intervalMsToLabel(clampStepMs(rangeMs, computeIntervalMs(query)));+ }+ private async query(query: OpenLITQuery, operation: string, statement: string): Promise<DataFrame<NormalizedMetricPoint>> { const step = this.stepLabel(query); const result = await this.executeNative(await this.adapter(), { operation, kind: "PrometheusDatasource", language: "promql", statement, extension: "io.openplait.prometheus", extensionValue: { timeRange: this.range(query), step } }); const rows = openPlaitFramesToRows(result.frames).map((row) => { const labels = (row.labels || {}) as Record<string, string>; return { metricName: labels.__name__ || operation, serviceName: labels.service_name || labels.service, timestamp: String(row.timestamp), value: Number(row.value), attributes: labels, resourceAttributes: labels } satisfies NormalizedMetricPoint; }); return { fields: [{ name: "timestamp", type: "time" }, { name: "value", type: "number" }, { name: "metricName", type: "string" }, { name: "attributes", type: "map" }], rows, meta: { latencyMs: result.metadata?.executionTimeMs, freshness: "live" } }; }+ async listMetricSeries(query: OpenLITQuery) { return this.query(query, "metric-list", selector(query)); }+ async metricTimeSeries(query: OpenLITQuery) { let statement = selector(query); const aggregation = query.aggregations?.[0]; const groups = (query.groupBy || []).map(labelName); if (aggregation?.fn && ["sum", "avg", "min", "max", "count"].includes(aggregation.fn)) statement = `${aggregation.fn}${groups.length ? ` by (${groups.join(",")})` : ""} (${statement})`; if (aggregation?.fn === "count" && aggregation.field === "rate") { const rateRange = intervalMsToLabel(rateIntervalMs(computeIntervalMs(query))); statement = `sum${groups.length ? ` by (${groups.join(",")})` : ""} (rate(${selector(query)}[${rateRange}]))`; } return this.query(query, "metric-series", statement); }+ async metricNames(window: QueryTimeRange): Promise<string[]> {+ return this.discoverLabelValues("__name__", window);+ }+ async attributeKeys(signal: Signal, window: QueryTimeRange): Promise<string[]> {+ if (signal !== "metrics") return [];+ return this.discoverLabels(window);+ }+ async distinctValues(key: string, query: OpenLITQuery): Promise<string[]> {+ return this.discoverLabelValues(labelName(key), query.timeRange);+ }++ /** Prometheus label APIs accept unix-second start/end; pass the UI window. */+ private async discoverLabels(window: QueryTimeRange): Promise<string[]> {+ const connection = await this.openPlaitConnection();+ const url = new URL("api/v1/labels", `${this.baseUrl}/`);+ url.searchParams.set("start", String(Math.floor(window.start.getTime() / 1000)));+ url.searchParams.set("end", String(Math.ceil(window.end.getTime() / 1000)));+ const response = await connection.fetch(url.toString(), { headers: connection.headers });+ if (!response.ok) {+ throw new SourceResponseError(response.status, await response.text());+ }+ const body = (await response.json()) as { data?: unknown };+ return Array.isArray(body.data)+ ? body.data.filter((item): item is string => typeof item === "string")+ : [];+ }++ private async discoverLabelValues(label: string, window: QueryTimeRange): Promise<string[]> {+ const connection = await this.openPlaitConnection();+ const url = new URL(+ `api/v1/label/${encodeURIComponent(label)}/values`,+ `${this.baseUrl}/`+ );+ url.searchParams.set("start", String(Math.floor(window.start.getTime() / 1000)));+ url.searchParams.set("end", String(Math.ceil(window.end.getTime() / 1000)));+ const response = await connection.fetch(url.toString(), { headers: connection.headers });+ if (!response.ok) {+ throw new SourceResponseError(response.status, await response.text());+ }+ const body = (await response.json()) as { data?: unknown };+ return Array.isArray(body.data)+ ? body.data.filter((item): item is string => typeof item === "string")+ : [];+ }+}++export const prometheusAdapterFactory = { type: "prometheus", create: (descriptor: TelemetrySourceDescriptor) => new PrometheusAdapter(descriptor), describe: (): SourceTypeDescriptor => ({ type: "prometheus", displayName: "Prometheus", description: "Metrics from a Prometheus-compatible query API.", declaredSignals: ["metrics"], capabilities: { traceTree: false, spanEvents: false, serverAggregation: true, spanMutation: false, distinctValues: true, crossTraceSession: false, rawQuery: false }, correlation: { crossSignal: false, keys: ["service"] }, configFields: [...httpVendorFields({ placeholder: "http://localhost:9090", tenant: true }), { key: "defaultStep", label: "Default query step", kind: "text", group: "settings", placeholder: "30s", defaultValue: "30s" }, { key: "maxTimeRangeMs", label: "Maximum query range (ms)", kind: "text", group: "settings" }], authStyle: "http", authHelp: getMessage().DATA_SOURCE_AUTH_HELP_HTTP }) };diff --git a/src/client/src/lib/platform/connectors/datasource/query-observability.ts b/src/client/src/lib/platform/connectors/datasource/query-observability.tsnew file mode 100644index 00000000..4cb4fa27--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/query-observability.ts@@ -0,0 +1,71 @@+/**+ * Lightweight query observability for external telemetry reads.+ *+ * Emits a single structured debug line per adapter read (source type, signal,+ * mode, latency, rows, freshness, degraded caps) so operators can reason about+ * quota/latency without a heavy tracing dependency. Built-in ClickHouse reads+ * are skipped to avoid noise. Controlled by `OPENLIT_QUERY_OBSERVABILITY`.+ */++import type { DataFrameMeta } from "./types";++export interface QueryObservabilityContext {+ sourceType: string;+ signal: string;+ mode?: string;+ isBuiltIn?: boolean;+}++function enabled(): boolean {+ // Default on outside production so LGTM smoke tests surface query stats;+ // opt-in in production via the env flag.+ const flag = process.env.OPENLIT_QUERY_OBSERVABILITY;+ if (flag === "1" || flag === "true") return true;+ if (flag === "0" || flag === "false") return false;+ return process.env.NODE_ENV !== "production";+}++/** Strip CR/LF so log lines cannot be forged via user fields (CodeQL js/log-injection). */+function sanitizeLogToken(value: unknown, max = 64): string {+ return String(value ?? "")+ .replace(/[\n\r]/g, "")+ .replace(/[\u0000-\u001F\u007F]/g, "")+ .slice(0, max);+}++/** Log a one-line summary of an adapter query result. Never throws. */+export function logQueryObservability(+ ctx: QueryObservabilityContext,+ meta: DataFrameMeta | undefined,+ rowCount: number+): void {+ try {+ if (ctx.isBuiltIn || !enabled()) return;+ const payload = {+ source: sanitizeLogToken(ctx.sourceType),+ signal: sanitizeLogToken(ctx.signal),+ mode: ctx.mode ? sanitizeLogToken(ctx.mode) : undefined,+ rows: Number(rowCount) || 0,+ latencyMs:+ meta?.latencyMs !== undefined ? Number(meta.latencyMs) || 0 : undefined,+ scanned:+ meta?.rowsScanned !== undefined+ ? Number(meta.rowsScanned) || 0+ : undefined,+ freshness: meta?.freshness+ ? sanitizeLogToken(meta.freshness)+ : undefined,+ truncated: meta?.truncated ? 1 : undefined,+ degraded: meta?.degraded?.length+ ? sanitizeLogToken(meta.degraded.join(","), 128)+ : undefined,+ };+ // JSON.stringify plus an explicit newline strip is the CodeQL-recognized+ // sanitizer for js/log-injection; do not interpolate raw tokens.+ const line = JSON.stringify(payload).replace(/[\n\r]/g, "");+ // eslint-disable-next-line no-console+ console.debug("[telemetry-query] %s", line);+ } catch {+ // Observability must never affect the read path.+ }+}diff --git a/src/client/src/lib/platform/connectors/datasource/query-planner.ts b/src/client/src/lib/platform/connectors/datasource/query-planner.tsnew file mode 100644index 00000000..287e8185--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/query-planner.ts@@ -0,0 +1,157 @@+import { dateTruncGroupingLogic } from "@/helpers/server/platform";+import type { DataFrame, DataSourceAdapter, OpenLITQuery } from "./types";+import { UnsupportedCapabilityError } from "./types";+import {+ computeAggregateSpansL1,+ computeDistinctValuesL1,+ computeSpanTimeSeriesL1,+} from "./l1-compute";+import { clampQueryToSource } from "./http/limits";++export type QueryTier = "native" | "sample" | "rollup";+export type QueryFreshness = "live" | "sampled" | "accelerated";++/** Map a time window to an OpenLITQuery interval via dateTruncGroupingLogic. */+export function intervalFromTimeRange(start: Date, end: Date): string {+ const trunc = dateTruncGroupingLogic(end, start);+ if (trunc === "hour") return "1h";+ if (trunc === "month") return "1M";+ return "1d";+}++function ensureMeta(+ frame: DataFrame,+ opts: { degraded?: string; freshness?: QueryFreshness }+): DataFrame {+ const degraded = new Set(frame.meta?.degraded || []);+ if (opts.degraded) degraded.add(opts.degraded);+ return {+ ...frame,+ meta: {+ ...frame.meta,+ degraded: Array.from(degraded),+ // An adapter knows when its own native-looking method had to fall+ // back to a sample. Preserve that stronger signal.+ freshness: frame.meta?.freshness || opts.freshness,+ },+ };+}++function hasServerAggregation(adapter: DataSourceAdapter): boolean {+ try {+ return !!adapter.capabilities()?.serverAggregation;+ } catch {+ return false;+ }+}++async function tryRollup(+ query: OpenLITQuery,+ opts?: {+ preferRollup?: boolean;+ readRollup?: (q: OpenLITQuery) => Promise<DataFrame | null>;+ }+): Promise<DataFrame | null> {+ if (!opts?.preferRollup || !opts.readRollup) return null;+ try {+ const frame = await opts.readRollup(query);+ if (!frame) return null;+ return ensureMeta(frame, {+ degraded: "rollup",+ freshness: "accelerated",+ });+ } catch {+ return null;+ }+}++/**+ * Plan aggregateSpans: L0 native (when available) → L2 rollup → L1 sample.+ */+export async function planAndAggregateSpans(+ adapter: DataSourceAdapter,+ query: OpenLITQuery,+ opts?: {+ preferRollup?: boolean;+ readRollup?: (q: OpenLITQuery) => Promise<DataFrame | null>;+ }+): Promise<DataFrame> {+ const effectiveQuery = clampQueryToSource(adapter, query).query;+ if (hasServerAggregation(adapter)) {+ return ensureMeta(await adapter.aggregateSpans(effectiveQuery), {+ freshness: "live",+ });+ }++ const rollup = await tryRollup(effectiveQuery, opts);+ if (rollup) return rollup;++ try {+ return ensureMeta(await adapter.aggregateSpans(effectiveQuery), {+ freshness: "live",+ });+ } catch (err) {+ if (err instanceof UnsupportedCapabilityError) {+ return ensureMeta(await computeAggregateSpansL1(adapter, effectiveQuery), {+ degraded: "sample",+ freshness: "sampled",+ });+ }+ throw err;+ }+}++/**+ * Plan spanTimeSeries: L0 native (when available) → L2 rollup → L1 sample.+ */+export async function planAndSpanTimeSeries(+ adapter: DataSourceAdapter,+ query: OpenLITQuery,+ opts?: {+ preferRollup?: boolean;+ readRollup?: (q: OpenLITQuery) => Promise<DataFrame | null>;+ }+): Promise<DataFrame> {+ const effectiveQuery = clampQueryToSource(adapter, query).query;+ if (hasServerAggregation(adapter)) {+ return ensureMeta(await adapter.spanTimeSeries(effectiveQuery), {+ freshness: "live",+ });+ }++ const rollup = await tryRollup(effectiveQuery, opts);+ if (rollup) return rollup;++ try {+ return ensureMeta(await adapter.spanTimeSeries(effectiveQuery), {+ freshness: "live",+ });+ } catch (err) {+ if (err instanceof UnsupportedCapabilityError) {+ return ensureMeta(await computeSpanTimeSeriesL1(adapter, effectiveQuery), {+ degraded: "sample",+ freshness: "sampled",+ });+ }+ throw err;+ }+}++/**+ * Plan distinctValues: L0 native → adapter method → L1 sample.+ */+export async function planAndDistinctValues(+ adapter: DataSourceAdapter,+ key: string,+ query: OpenLITQuery+): Promise<string[]> {+ const effectiveQuery = clampQueryToSource(adapter, query).query;+ try {+ return await adapter.distinctValues(key, effectiveQuery);+ } catch (err) {+ if (err instanceof UnsupportedCapabilityError) {+ return computeDistinctValuesL1(adapter, key, effectiveQuery);+ }+ throw err;+ }+}diff --git a/src/client/src/lib/platform/connectors/datasource/registry.ts b/src/client/src/lib/platform/connectors/datasource/registry.tsnew file mode 100644index 00000000..6c64d0f9--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/registry.ts@@ -0,0 +1,100 @@+/**+ * Telemetry data source adapter registry (CE).+ *+ * Holds adapter factories keyed by source type. `bootstrap.ts` registers+ * ClickHouse plus the atomic vendor factories. Extra private factories can+ * still be contributed through `getExternalDataSourceAdapters()` without an+ * `@/ee/**` import in CE.+ */++import type {+ DataSourceAdapter,+ DataSourceAdapterFactory,+ SourceTypeDescriptor,+ TelemetrySourceDescriptor,+} from "./types";+import { getExternalDataSourceAdapters } from "@/lib/platform/connectors/datasource/enterprise";++const factories = new Map<string, DataSourceAdapterFactory>();+let externalLoaded = false;++/** Register (or replace) an adapter factory. */+export function registerAdapterFactory(factory: DataSourceAdapterFactory): void {+ factories.set(factory.type, factory);+}++/** Load enterprise-provided factories exactly once. */+function ensureExternalLoaded(): void {+ if (externalLoaded) return;+ externalLoaded = true;+ for (const factory of getExternalDataSourceAdapters()) {+ // CE-registered factories (e.g. clickhouse) take precedence only if the+ // enterprise repo has not explicitly overridden them; here external+ // factories register their own distinct types.+ if (!factories.has(factory.type)) {+ factories.set(factory.type, factory);+ }+ }+}++/** Whether a factory for the given source type is available. */+export function hasAdapterFactory(type: string): boolean {+ ensureExternalLoaded();+ return factories.has(type);+}++/** List all registered source types. */+export function listAdapterTypes(): string[] {+ ensureExternalLoaded();+ return Array.from(factories.keys());+}++/** Get a factory by type, or undefined when unavailable (e.g. EE type in CE). */+export function getAdapterFactory(+ type: string+): DataSourceAdapterFactory | undefined {+ ensureExternalLoaded();+ return factories.get(type);+}++/** Get the static type descriptor for a source type, when registered. */+export function getSourceTypeDescriptor(+ type: string+): SourceTypeDescriptor | undefined {+ ensureExternalLoaded();+ return factories.get(type)?.describe();+}++/**+ * List static type descriptors for all registered source types.+ */+export function listSourceTypeDescriptors(+ _opts: { includeInternal?: boolean } = {}+): SourceTypeDescriptor[] {+ ensureExternalLoaded();+ const out: SourceTypeDescriptor[] = [];+ for (const factory of Array.from(factories.values())) {+ const descriptor = factory.describe();+ if (descriptor.internal) continue;+ out.push(descriptor);+ }+ return out;+}++/**+ * Build a concrete adapter for a resolved descriptor. Returns undefined when+ * no factory is registered for the descriptor's type (e.g. an externally+ * configured source running on CE).+ */+export function createAdapter(+ descriptor: TelemetrySourceDescriptor+): DataSourceAdapter | undefined {+ const factory = getAdapterFactory(descriptor.type);+ return factory?.create(descriptor);+}++/** Test-only: clear registry state. */+export function __resetRegistryForTests(): void {+ factories.clear();+ externalLoaded = false;+}diff --git a/src/client/src/lib/platform/connectors/datasource/rollup-policy.ts b/src/client/src/lib/platform/connectors/datasource/rollup-policy.tsnew file mode 100644index 00000000..b1fa4afa--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/rollup-policy.ts@@ -0,0 +1,35 @@+/**+ * Decide when L2 app-store rollups are safe to use.+ *+ * Multi-dimensional rollups carry service/environment, so scoped agent and+ * telemetry KPI reads may prefer L2. Version-hash filters are still not in+ * the rollup key — those fall back to L1.+ */++import type { MetricParams } from "@/lib/platform/common";+import type { OpenLITQuery } from "@/lib/platform/connectors/datasource/types";++function hasVersionScope(cfg: Record<string, unknown> | undefined): boolean {+ if (!cfg) return false;+ const version = cfg.versionFilter as { versionHash?: string } | undefined;+ return !!version?.versionHash;+}++/** True when QueryPlanner may prefer L2 rollups for this dashboard filter. */+export function shouldPreferRollup(params: MetricParams): boolean {+ return !hasVersionScope(+ (params.selectedConfig || {}) as Record<string, unknown>+ );+}++/** True when an OpenLITQuery already carries service/env/version filters. */+export function queryHasScopeFilters(query: OpenLITQuery): boolean {+ return (query.filters || []).some((f) => {+ if (f.target !== "attribute" || !f.key) return false;+ return (+ f.key === "service.name" ||+ f.key === "deployment.environment" ||+ f.key === "openlit.agent.version_hash"+ );+ });+}diff --git a/src/client/src/lib/platform/connectors/datasource/selector-match.ts b/src/client/src/lib/platform/connectors/datasource/selector-match.tsnew file mode 100644index 00000000..0598f248--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/selector-match.ts@@ -0,0 +1,65 @@+/**+ * In-process evaluation of the AI telemetry selector against a NormalizedSpan.+ *+ * Some backends (Jaeger, and any tag-only search) cannot express the full+ * OR-of-AND selector server-side. Those adapters fetch a bounded set of traces+ * and filter locally with this matcher, which mirrors the ClickHouse/TraceQL/+ * NRQL translations exactly so results stay consistent across sources.+ */++import {+ buildAITelemetrySelector,+ type AITelemetrySelector,+ type SelectorCondition,+} from "./ai-selector";+import type { NormalizedSpan } from "./types";++function attrValue(span: NormalizedSpan, cond: SelectorCondition): string | undefined {+ const key = cond.key || "";+ if (cond.scope === "resource") return span.resourceAttributes[key];+ if (cond.scope === "span") return span.spanAttributes[key];+ // Unscoped: check span attributes first, then resource attributes.+ return span.spanAttributes[key] ?? span.resourceAttributes[key];+}++function matchesCondition(span: NormalizedSpan, cond: SelectorCondition): boolean {+ if (cond.target === "spanName") {+ const values = Array.isArray(cond.value) ? cond.value : [cond.value || ""];+ return values.map(String).includes(span.name);+ }+ const value = attrValue(span, cond);+ switch (cond.op) {+ case "exists":+ return value !== undefined && value !== "";+ case "eq":+ return value === String(cond.value ?? "");+ case "in": {+ const values = Array.isArray(cond.value) ? cond.value : [cond.value || ""];+ return value !== undefined && values.map(String).includes(value);+ }+ default:+ return false;+ }+}++/** True when a span matches any predicate of the AI selector. */+export function spanMatchesAISelector(+ span: NormalizedSpan,+ selector: AITelemetrySelector = buildAITelemetrySelector()+): boolean {+ return selector.anyOf.some((predicate) =>+ predicate.allOf.every((cond) => matchesCondition(span, cond))+ );+}++/**+ * True when *any* span in a trace matches the AI selector. Used by trace-level+ * backends: if a trace has one AI-relevant span, the whole trace is kept so the+ * chat view / graph has full context.+ */+export function traceMatchesAISelector(+ spans: NormalizedSpan[],+ selector: AITelemetrySelector = buildAITelemetrySelector()+): boolean {+ return spans.some((s) => spanMatchesAISelector(s, selector));+}diff --git a/src/client/src/lib/platform/connectors/datasource/types.ts b/src/client/src/lib/platform/connectors/datasource/types.tsnew file mode 100644index 00000000..27e7d6eb--- /dev/null+++ b/src/client/src/lib/platform/connectors/datasource/types.ts@@ -0,0 +1,463 @@+/**+ * Pluggable telemetry source contract (CE).+ *+ * Raw-telemetry reads that have been wired through the adapter layer use a+ * `DataSourceAdapter`. ClickHouse is the reference/default implementation.+ * Atomic vendor adapters (Datadog, Tempo, Loki, Mimir, New Relic, Jaeger,+ * VictoriaLogs, VictoriaMetrics) are registered in CE via `bootstrap.ts`.+ *+ * The normalized adapter methods below model portable telemetry reads.+ * Derived/app data (evals, agent summaries, dashboards metadata, controller,+ * vault, rules, prompts) remains in OpenLIT's ClickHouse app store, but all of+ * its read-only SQL is executed through the shared OpenPlait runtime boundary.+ */++/** The three telemetry signals OpenLIT reads. */+export type Signal = "traces" | "logs" | "metrics" | "intelligence";++/** A normalized time range. `start`/`end` are absolute instants. */+export interface QueryTimeRange {+ start: Date;+ end: Date;+}++/**+ * A normalized span, vendor-agnostic. Attribute maps use OTel semantic+ * convention keys (e.g. `gen_ai.operation.name`) exactly as the product's+ * `TraceMapping` expects, so surfaces never assume ClickHouse `Map` columns.+ */+export interface NormalizedSpan {+ traceId: string;+ spanId: string;+ parentSpanId: string;+ name: string;+ serviceName: string;+ /** ISO-8601 timestamp string. */+ timestamp: string;+ /** Span duration in nanoseconds (OTel canonical unit). */+ durationNs: number;+ statusCode: string;+ statusMessage?: string;+ spanKind?: string;+ spanAttributes: Record<string, string>;+ resourceAttributes: Record<string, string>;+ events?: NormalizedSpanEvent[];+ /** Optional pre-resolved cost (USD) applied by cost overlay / vendor. */+ cost?: number;+}++export interface NormalizedSpanEvent {+ name: string;+ /** ISO-8601 timestamp string. */+ timestamp?: string;+ attributes: Record<string, string>;+}++/** A normalized log record. */+export interface NormalizedLog {+ timestamp: string;+ traceId?: string;+ spanId?: string;+ severityText?: string;+ severityNumber?: number;+ body: string;+ serviceName?: string;+ logAttributes: Record<string, string>;+ resourceAttributes: Record<string, string>;+ scopeAttributes?: Record<string, string>;+}++/** A single metric data point. */+export interface NormalizedMetricPoint {+ metricName: string;+ description?: string;+ unit?: string;+ serviceName?: string;+ /** ISO-8601 timestamp string. */+ timestamp: string;+ value: number;+ attributes: Record<string, string>;+ resourceAttributes: Record<string, string>;+}++/** Attribute scope for a filter/selector condition. */+export type AttributeScope = "span" | "resource" | "log" | "metric";++/** Comparison operators supported by normalized filters. */+export type FilterOp =+ | "exists"+ | "notExists"+ | "eq"+ | "neq"+ | "in"+ | "notIn"+ | "gt"+ | "gte"+ | "lt"+ | "lte"+ | "contains";++/**+ * A normalized attribute predicate. `target: "spanName"` matches on the span+ * name (or metric name for the metrics signal) rather than an attribute map.+ */+export interface NormalizedFilter {+ target: "attribute" | "spanName" | "status" | "duration";+ scope?: AttributeScope;+ key?: string;+ op: FilterOp;+ value?: string | string[] | number;+}++/** Aggregation functions the query layer can request. */+export type AggregationFn =+ | "count"+ | "sum"+ | "avg"+ | "min"+ | "max"+ | "p50"+ | "p90"+ | "p95"+ | "p99"+ | "cardinality";++export interface Aggregation {+ fn: AggregationFn;+ /** Attribute/field to aggregate; omit for count. */+ field?: string;+ /** Result column alias. */+ as?: string;+}++export interface QuerySort {+ field: string;+ direction: "asc" | "desc";+}++/**+ * A vendor-agnostic query. Adapters translate this into their native query+ * language (ClickHouse SQL, TraceQL, PromQL, NRQL, Datadog query, etc.).+ */+export interface OpenLITQuery {+ signal: Signal;+ timeRange: QueryTimeRange;+ filters?: NormalizedFilter[];+ groupBy?: string[];+ aggregations?: Aggregation[];+ sort?: QuerySort[];+ limit?: number;+ offset?: number;+ /**+ * When true, the adapter MUST push the AI-only selector down to the vendor+ * so only AI-relevant telemetry is returned. Defaults to true for the+ * intelligence surfaces.+ */+ aiSelector?: boolean;+ /**+ * Optional time-bucket size for time-series requests, e.g. "1m", "1h".+ * Adapters map this to their native bucketing.+ */+ interval?: string;+ /**+ * Target maximum number of data points for a series (Grafana `maxDataPoints`,+ * typically the panel pixel width). Adapters compute a step of+ * `max((end-start)/maxDataPoints, minInterval)` so a wide time range never+ * returns more points than the surface can render. When unset, adapters use a+ * sensible default.+ */+ maxDataPoints?: number;+}++/** A normalized columnar result. */+export interface DataFrame<TRow = unknown> {+ fields: DataFrameField[];+ rows: TRow[];+ meta?: DataFrameMeta;+}++export interface DataFrameField {+ name: string;+ type: "string" | "number" | "boolean" | "time" | "map" | "json";+}++export interface DataFrameMeta {+ /** True when the vendor truncated results (hit a row/scan cap). */+ truncated?: boolean;+ /** Estimated vendor query cost/credits where the vendor reports it. */+ vendorCost?: number;+ /** Capabilities that were degraded/emulated for this query. */+ degraded?: string[];+ /** Adapter query latency in ms (for query observability). */+ latencyMs?: number;+ /** Number of rows scanned/returned by the vendor where reported. */+ rowsScanned?: number;+ /**+ * How the result was produced for UI freshness badges:+ * live (vendor native), sampled (L1), accelerated (L2 rollup/cache).+ */+ freshness?: "live" | "sampled" | "accelerated";+}++/**+ * Declares what a source can and cannot do. Surfaces read this to render+ * first-class "not supported by this data source" states instead of failing.+ */+export interface SourceCapabilities {+ /** Which signals this source can serve. */+ signals: Signal[];+ /** Can reconstruct a full parent/child trace tree. */+ traceTree: boolean;+ /** Exposes OTel span events (prompt/completion payloads). */+ spanEvents: boolean;+ /** Supports server-side aggregation (group by + aggregate fns). */+ serverAggregation: boolean;+ /** Supports mutating stored spans (e.g. in-place cost backfill). */+ spanMutation: boolean;+ /** Can enumerate distinct attribute values / attribute keys. */+ distinctValues: boolean;+ /** Can stitch coding-agent sessions across multiple trace ids. */+ crossTraceSession: boolean;+ /** Max lookback window in milliseconds, if the vendor caps it. */+ maxLookbackMs?: number;+ /** Maximum duration of one query window in milliseconds, if capped. */+ maxTimeRangeMs?: number;+ /** Whether raw vendor-native query strings are supported (CH only). */+ rawQuery: boolean;+}++/**+ * Correlation keys a source type can expose so cross-signal joins (traces to+ * logs, agent snapshots to tool-definition logs, etc.) are possible with data+ * living in a *different* source. Grafana calls this "trace-to-logs"/derived+ * fields; OpenLIT declares it up front so surfaces degrade honestly.+ */+export type CorrelationKey = "traceId" | "spanId" | "service" | "session";++/** How a source type participates in cross-signal correlation. */+export interface SourceCorrelation {+ /**+ * True when the source exposes at least one correlation key that lets its+ * data be joined with another signal living in a different source. Built-in+ * ClickHouse is fully correlatable; metrics-only backends usually are not.+ */+ crossSignal: boolean;+ /** The correlation keys this source type reliably carries. */+ keys: CorrelationKey[];+}++/** Grouping for a source config field: connection settings vs secrets. */+export type FieldGroup = "settings" | "credentials";++/**+ * A single self-describing config field for a source type's add/edit form.+ * Adapters declare their own fields in `describe()` so the shared form needs+ * no per-vendor knowledge (Grafana-style plugin config schema).+ */+export interface FieldDef {+ key: string;+ label: string;+ kind: "text" | "password" | "url" | "switch" | "select";+ group: FieldGroup;+ placeholder?: string;+ /** Optional help text shown under the control. */+ description?: string;+ options?: { value: string; label: string }[];+ defaultValue?: string | boolean;+ /** Render this field only when another form field has the given value. */+ visibleWhen?: { key: string; value: string | boolean };+}++/**+ * How a source type authenticates. Used only for presentation hints (e.g. the+ * HTTP 401 troubleshooting nudge); the actual auth wiring lives in the adapter.+ */+export type AuthStyle = "none" | "http" | "api-key" | "custom";++/**+ * Static, type-level description of a source type (Grafana-style plugin+ * descriptor). Unlike `SourceCapabilities`, which is resolved per configured+ * instance, this is the maximal profile the *type* supports and is used to+ * populate source pickers, validate configured signals, and reason about+ * correlation before any instance exists.+ */+export interface SourceTypeDescriptor {+ /** Source type key, e.g. "clickhouse", "tempo". */+ type: string;+ /** Human-readable name for source pickers. */+ displayName: string;+ /** Short explanation shown in connector catalogs and configuration forms. */+ description?: string;+ /** Reserved for private/internal connector implementations. */+ internal?: boolean;+ /** Local icon path used by connector pickers. */+ icon?: string;+ /** Maximal set of signals a source of this type can serve. */+ declaredSignals: Signal[];+ /** Maximal capability profile for the type (signals declared separately). */+ capabilities: Omit<SourceCapabilities, "signals">;+ /** Cross-signal correlation profile for the type. */+ correlation: SourceCorrelation;+ /**+ * Self-describing config schema for the add/edit form. The single source of+ * truth for what a source of this type needs — the shared form renders these+ * directly, so a new type needs zero UI edits.+ */+ configFields: FieldDef[];+ /** How this type authenticates (presentation hints only). */+ authStyle?: AuthStyle;+ /** Optional resolved help text shown under the credentials section. */+ authHelp?: string;+ /** Optional documentation URL for connecting this source type. */+ docsUrl?: string;+}++/** Result of a health check against a configured source. */+export interface HealthCheckResult {+ ok: boolean;+ message?: string;+ latencyMs?: number;+}++/** Result of validating that a source actually carries AI telemetry. */+export interface AISignalValidation {+ ok: boolean;+ /** Sampled span count matching the AI selector in the probe window. */+ sampleCount: number;+ /** Missing-but-recommended attributes found absent during the probe. */+ missingAttributes: string[];+ message?: string;+ /**+ * False when the source type cannot validate AI spans (e.g. Loki/Mimir).+ * Connectivity can still be confirmed via healthCheck alone.+ */+ supported?: boolean;+}++/** A discovered service/workload row used by agent materialization. */+export interface DiscoveredService {+ serviceName: string;+ environment: string;+ clusterId: string;+ workloadKey?: string;+ sdkName?: string;+ sdkLanguage?: string;+ sdkVersion?: string;+ firstSeen?: string;+ lastSeen?: string;+}++/** A per-service rollup used by agent materialization. */+export interface ServiceRollup {+ serviceName: string;+ environment: string;+ clusterId: string;+ requestCount: number;+ models: string[];+ providers: string[];+}++/**+ * The complete adapter contract. Method groups mirror the product surfaces.+ * Adapters implement every method their `capabilities()` advertises; methods+ * for unsupported capabilities should throw `UnsupportedCapabilityError`.+ */+export interface DataSourceAdapter {+ readonly type: string;+ capabilities(): SourceCapabilities;+ healthCheck(): Promise<HealthCheckResult>;+ validateAISignal(window: QueryTimeRange): Promise<AISignalValidation>;++ // Traces+ listSpans(query: OpenLITQuery): Promise<DataFrame<NormalizedSpan>>;+ getSpan(spanId: string): Promise<NormalizedSpan | null>;+ getTraceSpans(traceId: string): Promise<NormalizedSpan[]>;+ getSpansBySession(sessionId: string): Promise<NormalizedSpan[]>;+ aggregateSpans(query: OpenLITQuery): Promise<DataFrame>;+ /** Optional exact matching-span count for paginated list surfaces. */+ countSpans?(query: OpenLITQuery): Promise<number | null>;+ /** Optional trace-row count for paginated trace list surfaces. */+ countTraces?(+ query: OpenLITQuery+ ): Promise<{ total: number; truncated: boolean }>;+ /**+ * Optional trace-level time series built from backend trace summaries.+ * This avoids treating a bounded full-span sample as the trace volume.+ */+ traceTimeSeries?(query: OpenLITQuery): Promise<DataFrame>;+ spanTimeSeries(query: OpenLITQuery): Promise<DataFrame>;+ distinctValues(key: string, query: OpenLITQuery): Promise<string[]>;+ attributeKeys(signal: Signal, window: QueryTimeRange): Promise<string[]>;++ // Logs+ listLogs(query: OpenLITQuery): Promise<DataFrame<NormalizedLog>>;+ getLog(+ logId: string,+ opts?: { aroundTimestamp?: string | Date; timeRange?: QueryTimeRange }+ ): Promise<NormalizedLog | null>;+ logTimeSeries(query: OpenLITQuery): Promise<DataFrame>;++ // Metrics+ listMetricSeries(query: OpenLITQuery): Promise<DataFrame<NormalizedMetricPoint>>;+ metricTimeSeries(query: OpenLITQuery): Promise<DataFrame<NormalizedMetricPoint>>;+ metricNames(window: QueryTimeRange): Promise<string[]>;++ // Discovery (agents / clusters)+ discoverServices(window: QueryTimeRange): Promise<DiscoveredService[]>;+ aggregateByService(window: QueryTimeRange): Promise<ServiceRollup[]>;+ sampleTracesForGraph(+ query: OpenLITQuery,+ maxTraces: number+ ): Promise<NormalizedSpan[]>;+}++/** Descriptor of a resolved telemetry source (before adapter binding). */+export interface TelemetrySourceDescriptor {+ /** Source type, e.g. "clickhouse", "datadog", "tempo". */+ type: string;+ /** Stable id: built-in sources use `builtin:<dbConfigId>`. */+ id: string;+ /** Whether this is the implicit built-in ClickHouse source. */+ isBuiltIn: boolean;+ /** Non-secret settings (URLs, site, orgId, etc.). */+ settings: Record<string, unknown>;+ /** Vault secret id holding credentials, if any. */+ secretRef?: string | null;+ /** Backing OpenLIT DatabaseConfig for native reads or app-state/vault access. */+ dbConfigId?: string;+ /** Signals this source is configured to serve. */+ signals: Signal[];+ /** Project this source belongs to (null for orphaned/built-in). */+ projectId?: string | null;+ /** Human-readable name. */+ name: string;+ /** Environment partition for this connector instance. */+ environment?: string;+}++/**+ * Factory that binds a resolved descriptor to a concrete adapter instance.+ * CE registers the ClickHouse factory; EE registers vendor factories via the+ * neutral `getExternalDataSourceAdapters()` hook.+ */+export interface DataSourceAdapterFactory {+ type: string;+ create(descriptor: TelemetrySourceDescriptor): DataSourceAdapter;+ /** Static, type-level descriptor used before any instance is bound. */+ describe(): SourceTypeDescriptor;+}++/** Thrown by adapters when a surface requests an unsupported capability. */+export class UnsupportedCapabilityError extends Error {+ readonly capability: string;+ readonly sourceType: string;+ constructor(sourceType: string, capability: string, message?: string) {+ super(+ message ||+ `Capability "${capability}" is not supported by data source "${sourceType}".`+ );+ this.name = "UnsupportedCapabilityError";+ this.capability = capability;+ this.sourceType = sourceType;+ }+}diff --git a/src/client/src/lib/platform/connectors/descriptions.ts b/src/client/src/lib/platform/connectors/descriptions.tsnew file mode 100644index 00000000..66d9c8a3--- /dev/null+++ b/src/client/src/lib/platform/connectors/descriptions.ts@@ -0,0 +1,12 @@+const CONNECTOR_DESCRIPTIONS: Record<string, string> = {+ clickhouse: "ClickHouse telemetry and platform data store.",+ tempo: "Distributed traces from Grafana Tempo.",+ loki: "Logs from Grafana Loki.",+ prometheus: "Metrics from Prometheus or a compatible query API.",+ jaeger: "Distributed traces from a Jaeger endpoint.",+};++export function connectorDescription(type: string, displayName?: string): string {+ return CONNECTOR_DESCRIPTIONS[type.toLowerCase()] ||+ `${displayName || type} telemetry connector for OpenLIT.`;+}diff --git a/src/client/src/lib/platform/connectors/icons.ts b/src/client/src/lib/platform/connectors/icons.tsnew file mode 100644index 00000000..c175feb6--- /dev/null+++ b/src/client/src/lib/platform/connectors/icons.ts@@ -0,0 +1,12 @@+/** Local SVG brand assets for connector catalog and connected-instance views. */+const CONNECTOR_ICONS: Record<string, string> = {+ clickhouse: "/images/connectors/clickhouse.svg",+ tempo: "/images/connectors/grafana.svg",+ loki: "/images/connectors/grafana.svg",+ prometheus: "/images/connectors/grafana.svg",+ jaeger: "/images/connectors/jaeger.svg",+};++export function connectorIconPath(type: string): string | undefined {+ return CONNECTOR_ICONS[type.toLowerCase()];+}diff --git a/src/client/src/lib/platform/connectors/index.ts b/src/client/src/lib/platform/connectors/index.tsnew file mode 100644index 00000000..e983c574--- /dev/null+++ b/src/client/src/lib/platform/connectors/index.ts@@ -0,0 +1,3 @@+export * from "./types";+export * from "./registry";+diff --git a/src/client/src/lib/platform/connectors/instances.ts b/src/client/src/lib/platform/connectors/instances.tsnew file mode 100644index 00000000..1274d133--- /dev/null+++ b/src/client/src/lib/platform/connectors/instances.ts@@ -0,0 +1,112 @@+import prisma from "@/lib/prisma";+import type { DatabaseConfig, TelemetrySource } from "@prisma/client";++const DATABASE_CONNECTOR_PREFIX = "database:";+const TELEMETRY_CONNECTOR_PREFIX = "telemetry:";++function databaseConnectorId(id: string) {+ return `${DATABASE_CONNECTOR_PREFIX}${id}`;+}++function telemetryConnectorId(id: string) {+ return `${TELEMETRY_CONNECTOR_PREFIX}${id}`;+}++/**+ * Keep the generic connector registry in sync with the legacy ClickHouse+ * record. DatabaseConfig remains a compatibility store for platform features+ * that still require its relations, while ConnectorInstance is the canonical+ * integration identity exposed to connector consumers.+ */+export async function syncDatabaseConfigConnector(config: DatabaseConfig) {+ const settings = JSON.stringify({+ username: config.username,+ host: config.host,+ port: config.port,+ database: config.database,+ query: config.query || "",+ });++ return prisma.connectorInstance.upsert({+ where: { id: databaseConnectorId(config.id) },+ create: {+ id: databaseConnectorId(config.id),+ category: "datasource",+ type: "clickhouse",+ name: config.name,+ environment: config.environment || "production",+ projectId: config.projectId,+ settings,+ status: "active",+ metadata: JSON.stringify({ legacyKind: "database-config", legacyId: config.id }),+ },+ update: {+ name: config.name,+ environment: config.environment || "production",+ projectId: config.projectId,+ settings,+ status: "active",+ },+ });+}++/** Keep a TelemetrySource row visible through the generic connector registry. */+export async function syncTelemetrySourceConnector(source: TelemetrySource) {+ return prisma.connectorInstance.upsert({+ where: { id: telemetryConnectorId(source.id) },+ create: {+ id: telemetryConnectorId(source.id),+ category: "datasource",+ type: source.type,+ name: source.name,+ environment: source.environment || "production",+ projectId: source.projectId,+ settings: source.settings || "{}",+ secretRef: source.secretRef,+ status: "active",+ metadata: JSON.stringify({ legacyKind: "telemetry-source", legacyId: source.id, signals: source.signals, isDefault: source.isDefault }),+ },+ update: {+ type: source.type,+ name: source.name,+ environment: source.environment || "production",+ projectId: source.projectId,+ settings: source.settings || "{}",+ secretRef: source.secretRef,+ status: "active",+ metadata: JSON.stringify({ legacyKind: "telemetry-source", legacyId: source.id, signals: source.signals, isDefault: source.isDefault }),+ },+ });+}++export async function removeLegacyConnector(kind: "database-config" | "telemetry-source", id: string) {+ await prisma.connectorInstance.deleteMany({+ where: { id: kind === "database-config" ? databaseConnectorId(id) : telemetryConnectorId(id) },+ });+}++/** Backfill and return the current project's unified connector instances. */+export async function listProjectConnectorInstances(projectId: string) {+ const [databases, sources] = await Promise.all([+ prisma.databaseConfig.findMany({ where: { projectId } }),+ prisma.telemetrySource.findMany({ where: { projectId } }),+ ]);+ await Promise.all([+ ...databases.map(syncDatabaseConfigConnector),+ ...sources.map(syncTelemetrySourceConnector),+ ]);+ const connectors = await prisma.connectorInstance.findMany({+ where: { projectId, category: "datasource" },+ orderBy: [{ environment: "asc" }, { createdAt: "asc" }],+ });+ return connectors.map((connector) => {+ let metadata: Record<string, unknown> = {};+ try { metadata = JSON.parse(connector.metadata || "{}") || {}; } catch { /* keep defaults */ }+ return {+ ...connector,+ signals: connector.type === "clickhouse" ? "traces,logs,metrics,intelligence" : metadata.signals || "traces,logs,metrics",+ isDefault: metadata.isDefault === true,+ hasSecret: !!connector.secretRef,+ };+ });+}diff --git a/src/client/src/lib/platform/connectors/registry.ts b/src/client/src/lib/platform/connectors/registry.tsnew file mode 100644index 00000000..cb3631f1--- /dev/null+++ b/src/client/src/lib/platform/connectors/registry.ts@@ -0,0 +1,44 @@+import type {+ ConnectorCategory,+ ConnectorTypeDescriptor,+ ConnectorTypeRegistration,+} from "./types";++const registrations = new Map<string, ConnectorTypeRegistration>();++export function registerConnectorType(+ registration: ConnectorTypeRegistration+): void {+ registrations.set(+ `${registration.descriptor.category}:${registration.descriptor.type}`,+ registration+ );+}++export function getConnectorType(+ category: ConnectorCategory,+ type: string+): ConnectorTypeRegistration | undefined {+ return registrations.get(`${category}:${type}`);+}++export function listConnectorTypes(+ category?: ConnectorCategory+): ConnectorTypeDescriptor[] {+ return Array.from(registrations.values())+ .filter((registration) => !category || registration.descriptor.category === category)+ .map((registration) => registration.descriptor)+ .filter((descriptor) => !descriptor.internal);+}++export function createConnectorRuntime(+ instance: Parameters<NonNullable<ConnectorTypeRegistration["create"]>>[0]+) {+ const registration = getConnectorType(instance.category, instance.type);+ return registration?.create?.(instance);+}++export function __resetConnectorRegistryForTests(): void {+ registrations.clear();+}+diff --git a/src/client/src/lib/platform/connectors/types.ts b/src/client/src/lib/platform/connectors/types.tsnew file mode 100644index 00000000..c4a07780--- /dev/null+++ b/src/client/src/lib/platform/connectors/types.ts@@ -0,0 +1,74 @@+/**+ * Neutral connector contracts shared by all integration categories.+ *+ * Connector instances are configuration records. Category-specific adapters+ * (datasource, notification, memory, etc.) own their runtime methods.+ */++export type ConnectorCategory =+ | "datasource"+ | "notification"+ | "memory"+ | "vector-store"+ | "model-provider";++export type ConnectorScope = "organisation" | "project";++export type ConnectorStatus = "active" | "disabled" | "error";++export interface ConnectorField {+ key: string;+ label: string;+ kind: "text" | "password" | "url" | "email" | "number" | "boolean" | "select";+ secret?: boolean;+ required?: boolean;+ placeholder?: string;+ options?: { value: string; label: string }[];+ defaultValue?: string | number | boolean;+}++export interface ConnectorCapabilities {+ [key: string]: boolean | string | number | string[] | undefined;+}++export interface ConnectorTypeDescriptor {+ type: string;+ category: ConnectorCategory;+ displayName: string;+ description?: string;+ icon?: string;+ scope: ConnectorScope | "either";+ configFields: ConnectorField[];+ capabilities: ConnectorCapabilities;+ internal?: boolean;+ /** Commercial availability marker; CE defaults to free when omitted. */+ plan?: "free" | "enterprise";+}++export interface ConnectorInstanceDescriptor {+ id: string;+ type: string;+ category: ConnectorCategory;+ name: string;+ organisationId?: string | null;+ projectId?: string | null;+ environment?: string;+ settings: Record<string, unknown>;+ secretRef?: string | null;+ status?: ConnectorStatus;+}++export interface ConnectorHealthResult {+ ok: boolean;+ message?: string;+ latencyMs?: number;+}++export interface ConnectorRuntime {+ healthCheck(): Promise<ConnectorHealthResult>;+}++export interface ConnectorTypeRegistration {+ descriptor: ConnectorTypeDescriptor;+ create?: (instance: ConnectorInstanceDescriptor) => ConnectorRuntime;+}diff --git a/src/client/src/lib/platform/connectors/visible-types.ts b/src/client/src/lib/platform/connectors/visible-types.tsnew file mode 100644index 00000000..9206c087--- /dev/null+++ b/src/client/src/lib/platform/connectors/visible-types.ts@@ -0,0 +1,6 @@+/** Connector types currently exposed in the CE UI. */+export const VISIBLE_CONNECTOR_TYPES = ["clickhouse", "tempo", "loki", "prometheus", "jaeger"] as const;++export function isVisibleConnectorType(type: unknown): boolean {+ return VISIBLE_CONNECTOR_TYPES.includes(String(type) as (typeof VISIBLE_CONNECTOR_TYPES)[number]);+}diff --git a/src/client/src/lib/platform/cron-log/index.ts b/src/client/src/lib/platform/cron-log/index.tsindex 73e6636d..9c0792a3 100644--- a/src/client/src/lib/platform/cron-log/index.ts+++ b/src/client/src/lib/platform/cron-log/index.ts@@ -117,14 +117,21 @@ export async function getCronLogs({};}-export async function getLastRunCronLogByCronId(cronId: string) {+export async function getLastRunCronLogByCronId(+ cronId: string,+ databaseConfigId?: string+) {const query = `SELECT * FROM ${OPENLIT_CRON_LOG_TABLE_NAME} WHERE cron_id = '${cronId}' AND run_status = '${CronRunStatus.SUCCESS}'ORDER BY started_at DESCLIMIT 1`;- const { data, err } = await dataCollector({ query }, "query");+ const { data, err } = await dataCollector(+ { query },+ "query",+ databaseConfigId+ );if (err || !data) {return null;diff --git a/src/client/src/lib/platform/evaluation/index.ts b/src/client/src/lib/platform/evaluation/index.tsindex 17da3fea..5c1bfa90 100644--- a/src/client/src/lib/platform/evaluation/index.ts+++ b/src/client/src/lib/platform/evaluation/index.ts@@ -7,6 +7,7 @@ import { OPENLIT_EVALUATION_TABLE_NAME } from "./table-details";import { runEvaluation } from "./run-evaluation";import { getEvaluationConfig, getEvaluationConfigById } from "./config";import asaw from "@/utils/asaw";+import { evaluateRules } from "@/lib/platform/rule-engine/evaluate";import {AutoEvaluationConfig,Evaluation,@@ -15,13 +16,11 @@ import {EvaluationResponse,} from "@/types/evaluation";import { consoleLog } from "@/utils/log";-import { getRequestViaSpanId } from "../request";+import { getTraceSpanRecord } from "../traces/read";import { getTraceMappingKeyFullPath } from "@/helpers/server/trace";import {getContextFromRuleEngineForTrace,getContextFromRulesWithPriority,- getContextFromFields,- getContextFromRulesWithPriorityForFields,} from "./rule-engine-context";import { TraceRow } from "@/types/trace";import { get } from "lodash";@@ -47,60 +46,11 @@ import { differenceInSeconds } from "date-fns";import { getFilterPreviousParams } from "@/helpers/server/platform";import { ProviderRegistry } from "@/lib/platform/providers/provider-registry";import { getChatModelCostPerM } from "@/lib/platform/pricing/chat-cost";-import {- resolveEvaluationType,- type EvaluationTypeRef,-} from "@/helpers/client/evaluation-type";-/**- * Collects rule links (with priority) across a set of enabled evaluation- * types. Shared by the real-time/auto and offline evaluation paths so both- * resolve rule-engine context identically for the same configured types.- */-function collectRulesWithPriority(- types: Array<{- rules?: Array<{ ruleId: string; priority: number }>;- ruleId?: string;- priority?: number;- }>-): Array<{ ruleId: string; priority: number }> {- const rulesWithPriority: Array<{ ruleId: string; priority: number }> = [];- for (const t of types) {- if (t.rules?.length) {- rulesWithPriority.push(- ...t.rules.filter((r) => r.ruleId && r.ruleId.trim())- );- } else if (t.ruleId) {- rulesWithPriority.push({- ruleId: t.ruleId,- priority: t.priority ?? 0,- });- }- }- return rulesWithPriority;-}--function applyPerTypeThresholds(- results: Evaluation[],- enabledTypes: Array<{ id: string; label?: string; thresholdScore?: number }>,- fallbackThreshold: number-): Evaluation[] {- return results.map((evaluation) => {- const type = enabledTypes.find(- (t) =>- t.id.toLowerCase() === evaluation.evaluation.toLowerCase() ||- t.label?.toLowerCase() === evaluation.evaluation.toLowerCase()- );- const threshold =- typeof type?.thresholdScore === "number"- ? type.thresholdScore- : fallbackThreshold;- return {- ...evaluation,- verdict: evaluation.score > threshold ? "yes" : "no",- };- });-}+export type EvaluationTraceLookupOptions = {+ traceId?: string;+ environment?: string;+};/*** Cost from Manage Models (`openlit_provider_models`) — same source as@@ -145,28 +95,130 @@ async function estimateEvaluationCost(}}+/** Span ids already handled by auto-eval (evaluated or sampling-skipped). */+async function loadAutoHandledSpanIds(+ databaseConfigId: string+): Promise<Set<string>> {+ const autoHandledSources = AUTO_EVALUATION_HANDLED_SOURCES.map(+ (source) => `'${source}'`+ ).join(", ");+ const query = `+ SELECT DISTINCT span_id+ FROM ${OPENLIT_EVALUATION_TABLE_NAME}+ WHERE meta['source'] IN (${autoHandledSources})+ `;+ const { data, err } = await dataCollector(+ { query },+ "query",+ databaseConfigId+ );+ if (err) return new Set();+ return new Set(+ ((data as { span_id?: string }[]) || [])+ .map((r) => r.span_id)+ .filter((id): id is string => !!id)+ );+}+/**- * Remap judge output names to configured type labels when possible so- * ClickHouse stores canonical names (Hallucination) instead of variants- * or inventons that still resolve (e.g. "Hallucination evaluation context").- * Unmapped labels (TypeA) are left as-is and filtered from analytics.+ * Candidate spans for auto-evaluation. Built-in ClickHouse keeps the+ * historical JOIN against otel_traces; external sources list via the+ * traces adapter and exclude already-handled ids from the app-store eval table.*/-function normalizeEvaluationResults(- evaluation: Evaluation[],- customTypes: EvaluationTypeRef[] = []-): Evaluation[] {- return evaluation.map((item) => {- const resolved = resolveEvaluationType(item.evaluation, customTypes);- if (!resolved) return item;- return { ...item, evaluation: resolved.label };- });-}+async function fetchAutoEvalCandidateSpans({+ databaseConfigId,+ lastRunTime,+}: {+ databaseConfigId: string;+ lastRunTime?: string | Date | null;+}): Promise<{ data: TraceRow[]; err?: string }> {+ const keyPath = `SpanAttributes['${getTraceMappingKeyFullPath("type")}']`;+ const autoHandledSources = AUTO_EVALUATION_HANDLED_SOURCES.map(+ (source) => `'${source}'`+ ).join(", ");+ const lastRunIso =+ lastRunTime instanceof Date+ ? lastRunTime.toISOString()+ : lastRunTime || null;-export async function getEvaluationSummaryForSpanId(spanId: string, dbConfigId?: string) {- if (!dbConfigId) {- const user = await getCurrentUser();- if (!user) return null;+ try {+ const { getTelemetryAdapterForDbConfig } = await import(+ "@/lib/telemetry-source"+ );+ const { denormalizeSpanToTraceRow } = await import(+ "@/lib/platform/connectors/datasource/clickhouse/normalize"+ );+ const resolved = await getTelemetryAdapterForDbConfig(+ databaseConfigId,+ "traces"+ );+ if (!resolved.isBuiltIn) {+ const end = new Date();+ const start = lastRunIso+ ? new Date(lastRunIso)+ : new Date(end.getTime() - 24 * 60 * 60 * 1000);+ const handled = await loadAutoHandledSpanIds(databaseConfigId);+ const frame = await resolved.adapter.listSpans({+ signal: "traces",+ timeRange: { start, end },+ aiSelector: true,+ limit: 500,+ filters: [+ {+ target: "attribute",+ scope: "span",+ key: "gen_ai.operation.name",+ op: "in",+ value: [...SUPPORTED_EVALUATION_OPERATIONS],+ },+ ],+ });+ const rows = frame.rows+ .filter((span) => !handled.has(span.spanId))+ .map(+ (span) =>+ denormalizeSpanToTraceRow(span) as unknown as TraceRow+ );+ return { data: rows };+ }+ } catch (err) {+ return {+ data: [],+ err: String((err as Error)?.message || err),+ };}++ const query = `+ SELECT t.*+ FROM ${OTEL_TRACES_TABLE_NAME} AS t+ LEFT JOIN (+ SELECT span_id FROM ${OPENLIT_EVALUATION_TABLE_NAME}+ WHERE meta['source'] IN (${autoHandledSources})+ ) AS e ON t.SpanId = e.span_id+ WHERE ${keyPath} IN (${SUPPORTED_EVALUATION_OPERATIONS.map(+ (operation) => `'${operation}'`+ ).join(", ")})+ AND (e.span_id = '' OR e.span_id IS NULL)+ ${+ lastRunIso+ ? `AND t.Timestamp >= parseDateTimeBestEffort('${lastRunIso}')`+ : ""+ }+ ORDER BY t.Timestamp;+ `;++ const { data, err } = await dataCollector(+ { query },+ "query",+ databaseConfigId+ );+ if (err) return { data: [], err: String(err) };+ return { data: (data as TraceRow[]) || [] };+}++export async function getEvaluationSummaryForSpanId(spanId: string) {+ const user = await getCurrentUser();+ if (!user) return null;const sanitizedSpanId = Sanitizer.sanitizeValue(spanId);const query = `@@ -178,7 +230,7 @@ export async function getEvaluationSummaryForSpanId(spanId: string, dbConfigId?:WHERE span_id = '${sanitizedSpanId}' AND meta['source'] NOT IN ('${EVALUATION_SOURCE.MANUAL_FEEDBACK}', '${EVALUATION_SOURCE.AUTO_SKIPPED}');`;- const { data, err } = await dataCollector({ query }, "query", dbConfigId);+ const { data, err } = await dataCollector({ query });if (err || !(data as any[])?.[0]) return null;const row = (data as any[])[0];@@ -189,7 +241,15 @@ export async function getEvaluationSummaryForSpanId(spanId: string, dbConfigId?:};}-export async function getEvaluationsForSpanId(spanId: string) {+export async function getEvaluationsForSpanId(+ spanId: string,+ opts?: EvaluationTraceLookupOptions+) {+ consoleLog("[evaluation] load span evaluations", {+ spanId,+ traceId: opts?.traceId || null,+ environment: opts?.environment || null,+ });const user = await getCurrentUser();throwIfError(!user, getMessage().UNAUTHORIZED_USER);@@ -217,6 +277,7 @@ export async function getEvaluationsForSpanId(spanId: string) {const { data, err } = await dataCollector({ query });if (err) {+ consoleLog("[evaluation] evaluation store query failed", { spanId, error: String(err) });return { err };}@@ -266,8 +327,15 @@ export async function getEvaluationsForSpanId(spanId: string) {}// Include rule context that would be applied (for UI to show before running)- const { record: traceRecord } = await getRequestViaSpanId(sanitizedSpanId);- const trace = traceRecord as TraceRow;+ const traceResult = await getTraceSpanRecord(sanitizedSpanId, opts);+ consoleLog("[evaluation] trace lookup for evaluation", {+ spanId,+ traceId: opts?.traceId || null,+ found: !!traceResult.record,+ error: traceResult.err || null,+ });+ const { record: traceRecord } = traceResult;+ const trace = traceRecord as unknown as TraceRow;let ruleContext: {matchingRuleIds: string[];contextApplied: boolean;@@ -329,8 +397,8 @@ export async function getEvaluationsForSpanId(spanId: string) {contextEntityIds: [],};if (!evaluationConfigErr && evaluationConfigTyped?.id) {- const { record: traceRecord } = await getRequestViaSpanId(sanitizedSpanId);- const trace = traceRecord as TraceRow;+ const { record: traceRecord } = await getTraceSpanRecord(sanitizedSpanId, opts);+ const trace = traceRecord as unknown as TraceRow;if (trace?.SpanId) {const { matchingRuleIds, contextEntityIds } =await getContextFromRuleEngineForTrace(@@ -368,12 +436,10 @@ async function storeEvaluation(spanId: string,evaluation: Evaluation[],meta: any,- dbConfigId?: string,- customTypes: EvaluationTypeRef[] = []+ dbConfigId?: string) {const now = new Date();const createdAt = toClickHouseDateTime(now);- const normalized = normalizeEvaluationResults(evaluation, customTypes);// Ensure meta values are strings (Map(LowCardinality(String), String))const metaStrings =meta && typeof meta === "object"@@ -390,13 +456,13 @@ async function storeEvaluation(span_id: spanId,created_at: createdAt,meta: metaStrings,- "evaluationData.evaluation": normalized.map((e) => e.evaluation),- "evaluationData.classification": normalized.map(+ "evaluationData.evaluation": evaluation.map((e) => e.evaluation),+ "evaluationData.classification": evaluation.map((e) => e.classification),- "evaluationData.explanation": normalized.map((e) => e.explanation),- "evaluationData.verdict": normalized.map((e) => e.verdict),- scores: normalized.reduce((acc: Record<string, number>, e) => {+ "evaluationData.explanation": evaluation.map((e) => e.explanation),+ "evaluationData.verdict": evaluation.map((e) => e.verdict),+ scores: evaluation.reduce((acc: Record<string, number>, e) => {acc[e.evaluation] = e.score;return acc;}, {}),@@ -419,6 +485,14 @@ async function storeAutoEvaluationSkip(sampleRate: number,dbConfigId: string) {+ const serviceName =+ String(+ trace.ServiceName ||+ (trace.ResourceAttributes as Record<string, unknown> | undefined)?.[+ "service.name"+ ] ||+ ""+ ) || "";return storeEvaluation(trace.SpanId,[],@@ -426,6 +500,7 @@ async function storeAutoEvaluationSkip(source: EVALUATION_SOURCE.AUTO_SKIPPED,traceTimeStamp: String(trace.Timestamp ?? ""),sampleRate: String(sampleRate),+ ...(serviceName ? { "service.name": serviceName } : {}),},dbConfigId);@@ -435,14 +510,31 @@ export async function storeManualFeedback(spanId: string,rating: "positive" | "negative" | "neutral",comment?: string,- dbConfigId?: string+ dbConfigId?: string,+ opts?: EvaluationTraceLookupOptions) {+ consoleLog("[evaluation] save manual feedback", {+ spanId,+ rating,+ traceId: opts?.traceId || null,+ environment: opts?.environment || null,+ });const user = await getCurrentUser();throwIfError(!user, getMessage().UNAUTHORIZED_USER);const sanitizedSpanId = Sanitizer.sanitizeValue(spanId);- const { record: spanData } = await getRequestViaSpanId(sanitizedSpanId);- throwIfError(!(spanData as any).SpanId, getMessage().TRACE_NOT_FOUND);+ const traceResult = await getTraceSpanRecord(sanitizedSpanId, opts);+ consoleLog("[evaluation] feedback trace lookup", {+ spanId,+ traceId: opts?.traceId || null,+ found: !!traceResult.record,+ error: traceResult.err || null,+ });+ const { record: spanData } = traceResult;+ throwIfError(+ !(spanData as any)?.SpanId,+ (typeof traceResult.err === "string" ? traceResult.err : getMessage().TRACE_NOT_FOUND)+ );const meta: Record<string, string> = {source: "manual_feedback",@@ -497,22 +589,40 @@ export async function storeManualFeedback();if (err) {- consoleLog(err);+ consoleLog("[evaluation] feedback insert failed", { spanId, error: String(err) });return { err };}return { data: true };}-export async function setEvaluationsForSpanId(spanId: string) {+export async function setEvaluationsForSpanId(+ spanId: string,+ opts?: EvaluationTraceLookupOptions+) {+ consoleLog("[evaluation] run evaluation", {+ spanId,+ traceId: opts?.traceId || null,+ environment: opts?.environment || null,+ });const user = await getCurrentUser();throwIfError(!user, getMessage().UNAUTHORIZED_USER);const sanitizedSpanId = Sanitizer.sanitizeValue(spanId);- let { record: spanData } = await getRequestViaSpanId(sanitizedSpanId);- const spanDataTyped = spanData as TraceRow;+ const traceResult = await getTraceSpanRecord(sanitizedSpanId, opts);+ consoleLog("[evaluation] run trace lookup", {+ spanId,+ traceId: opts?.traceId || null,+ found: !!traceResult.record,+ error: traceResult.err || null,+ });+ const { record: spanData } = traceResult;+ const spanDataTyped = spanData as unknown as TraceRow;- throwIfError(!(spanData as any).SpanId, getMessage().TRACE_NOT_FOUND);+ throwIfError(+ !(spanData as any)?.SpanId,+ (typeof traceResult.err === "string" ? traceResult.err : getMessage().TRACE_NOT_FOUND)+ );const evaluationConfig = await getEvaluationConfig(undefined, false);return await getEvaluationConfigForTrace(@@ -523,28 +633,75 @@ export async function setEvaluationsForSpanId(spanId: string) {);}+/**+ * Extract prompt/completion for evaluation, attribute-first for portability.+ *+ * The trace-detail chat view reads prompt/completion from span *attributes*+ * (`gen_ai.input.messages` / `gen_ai.output.messages` + legacy variants), but+ * evals historically read them from OTel span *events*+ * (`Events.Attributes[0]['gen_ai.prompt']` / `[1]['gen_ai.completion']`).+ * External observability backends (Datadog, New Relic, ...) do not reliably+ * expose OTel span events, so we prefer span attributes and fall back to+ * events. This also makes evals consistent with what the chat view renders.+ */+export function extractEvalPromptCompletion(trace: unknown): {+ prompt: string;+ response: string;+} {+ const attrs =+ ((trace as { SpanAttributes?: Record<string, string> })?.SpanAttributes ||+ {}) as Record<string, string>;+ const firstNonEmpty = (keys: string[]): string => {+ for (const k of keys) {+ const v = attrs[k];+ if (typeof v === "string" && v.trim() !== "") return v;+ }+ return "";+ };+ const promptFromAttr = firstNonEmpty([+ "gen_ai.input.messages",+ "gen_ai.prompt",+ "gen_ai.content.prompt",+ "gen_ai.request.input",+ ]);+ const responseFromAttr = firstNonEmpty([+ "gen_ai.output.messages",+ "gen_ai.completion",+ "gen_ai.content.completion",+ "gen_ai.response.output",+ ]);+ const promptFromEvent = get(+ trace,+ getTraceMappingKeyFullPath("prompt", true)+ ) as string | undefined;+ const responseFromEvent = get(+ trace,+ getTraceMappingKeyFullPath("response", true)+ ) as string | undefined;+ return {+ prompt: promptFromAttr || promptFromEvent || "",+ response: responseFromAttr || responseFromEvent || "",+ };+}+async function getEvaluationConfigForTrace(trace: TraceRow,evaluationConfig: EvaluationConfigWithSecret,dbConfigId?: string,source: "manual" | "auto" = "auto") {- const response = get(trace, getTraceMappingKeyFullPath("response", true));- const prompt = get(trace, getTraceMappingKeyFullPath("prompt", true));+ const { prompt, response } = extractEvalPromptCompletion(trace);const dbConfig = dbConfigId ?? evaluationConfig.databaseConfigId;- const DEFAULT_THRESHOLD = 0.5;const evaluationTypes =((evaluationConfig as any).evaluationTypes || []) as Array<{id: string;enabled: boolean;- label?: string;rules?: Array<{ ruleId: string; priority: number }>;ruleId?: string;priority?: number;prompt?: string;defaultPrompt?: string;- thresholdScore?: number;}>;// Default: Hallucination, Bias, Toxicity enabled@@ -559,7 +716,19 @@ async function getEvaluationConfigForTrace().map((t) => ({ ...t, enabled: true }));// Collect rules with priority from enabled types- const rulesWithPriority = collectRulesWithPriority(enabledTypes);+ const rulesWithPriority: Array<{ ruleId: string; priority: number }> = [];+ for (const t of enabledTypes) {+ if (t.rules?.length) {+ rulesWithPriority.push(+ ...t.rules.filter((r) => r.ruleId && r.ruleId.trim())+ );+ } else if (t.ruleId) {+ rulesWithPriority.push({+ ruleId: t.ruleId,+ priority: t.priority ?? 0,+ });+ }+ }let contextContents: string[];let matchingRuleIds: string[];@@ -602,7 +771,7 @@ async function getEvaluationConfigForTrace(prompt: prompt ?? "",contexts: finalContextString,response: response ?? "",- thresholdScore: DEFAULT_THRESHOLD,+ thresholdScore: 0.5,});@@ -610,14 +779,14 @@ async function getEvaluationConfigForTrace(return { success: false, error: data.error };}- // Recompute verdicts with per-type thresholds, matching the offline- // path so dashboard/auto/manual and SDK runs honor the same config.- const evaluations = applyPerTypeThresholds(- data.result || [],- enabledTypes,- DEFAULT_THRESHOLD- );-+ const serviceName =+ String(+ trace.ServiceName ||+ (trace.ResourceAttributes as Record<string, unknown> | undefined)?.[+ "service.name"+ ] ||+ ""+ ) || "";const metaBase: Record<string, string> = {model: `${evaluationConfig.provider}/${evaluationConfig.model}`,traceTimeStamp: String(trace.Timestamp ?? ""),@@ -625,6 +794,7 @@ async function getEvaluationConfigForTrace(contextIds: contextEntityIds.join(","),contextApplied: contextContents.length > 0 ? "yes" : "no",source,+ ...(serviceName ? { "service.name": serviceName } : {}),};if (data.usage) {metaBase.promptTokens = String(data.usage.promptTokens);@@ -641,10 +811,9 @@ async function getEvaluationConfigForTrace(const storeResult = await storeEvaluation(trace.SpanId,- evaluations,+ data.result || [],metaBase,- dbConfig,- evaluationTypes.map((t) => ({ id: t.id, label: (t as any).label || t.id }))+ dbConfig);if (storeResult.err) {consoleLog(storeResult.err);@@ -689,41 +858,15 @@ export async function autoEvaluate(autoEvaluationConfig: AutoEvaluationConfig) {}const lastRunTime = await getLastRunCronLogByCronId(- autoEvaluationConfig.cronId- );-- const keyPath = `SpanAttributes['${getTraceMappingKeyFullPath("type")}']`;-- // Exclude spans already handled by auto evaluation (evaluated or sampling-skipped).- // Manual feedback does not block auto evaluation. Manual runs are always allowed.- const autoHandledSources = AUTO_EVALUATION_HANDLED_SOURCES.map(- (source) => `'${source}'`- ).join(", ");- const query = `- SELECT t.*- FROM ${OTEL_TRACES_TABLE_NAME} AS t- LEFT JOIN (- SELECT span_id FROM ${OPENLIT_EVALUATION_TABLE_NAME}- WHERE meta['source'] IN (${autoHandledSources})- ) AS e ON t.SpanId = e.span_id- WHERE ${keyPath} IN (${SUPPORTED_EVALUATION_OPERATIONS.map(- (operation) => `'${operation}'`- ).join(", ")})- AND (e.span_id = '' OR e.span_id IS NULL)- ${- lastRunTime- ? `AND t.Timestamp >= parseDateTimeBestEffort('${lastRunTime}')`- : ""- }- ORDER BY t.Timestamp;- `;-- const { data, err } = await dataCollector(- { query },- "query",+ autoEvaluationConfig.cronId,databaseConfig.id);+ const { data, err } = await fetchAutoEvalCandidateSpans({+ databaseConfigId: databaseConfig.id,+ lastRunTime,+ });+if (err) {const finishedAt = new Date();const { err: cronLogErr } = await insertCronLog(@@ -841,6 +984,22 @@ export async function getEvaluationDetectedByType(const currentWhereParams = params;const previousWhereParams = getFilterPreviousParams(currentWhereParams);+ const serviceNames = Array.isArray(+ (params.selectedConfig as { serviceNames?: unknown } | undefined)+ ?.serviceNames+ )+ ? (+ (params.selectedConfig as { serviceNames?: string[] }).serviceNames ||+ []+ ).filter((s): s is string => typeof s === "string" && s.length > 0)+ : [];+ const serviceScopeSql =+ serviceNames.length > 0+ ? `AND meta['service.name'] IN (${serviceNames+ .map((s) => `'${Sanitizer.sanitizeValue(s)}'`)+ .join(", ")})`+ : "";+const commonQuery = (parameters: MetricParams) => `SELECTCOUNT(DISTINCT span_id) AS total_evaluation_detected,@@ -852,6 +1011,7 @@ export async function getEvaluationDetectedByType(AND evaluationData.verdict = 'yes'AND parseDateTimeBestEffort(meta['traceTimeStamp']) >= parseDateTimeBestEffort('${parameters.timeLimit.start}')AND parseDateTimeBestEffort(meta['traceTimeStamp']) <= parseDateTimeBestEffort('${parameters.timeLimit.end}')+ ${serviceScopeSql}`;const query = `@@ -924,10 +1084,17 @@ export async function runOfflineEvaluation(priority?: number;prompt?: string;defaultPrompt?: string;- thresholdScore?: number;}>;- let enabledTypes: typeof allTypes;+ let enabledTypes = requestedTypes?.length+ ? allTypes.filter((t) => requestedTypes.includes(t.id))+ : allTypes.filter((t) => t.enabled);++ if (enabledTypes.length === 0) {+ enabledTypes = allTypes+ .filter((t) => ["hallucination", "bias", "toxicity"].includes(t.id))+ .map((t) => ({ ...t, enabled: true }));+ }if (requestedTypes?.length) {const allTypeIds = new Set(allTypes.map((t) => t.id));@@ -938,50 +1105,39 @@ export async function runOfflineEvaluation(error: `Unknown eval types: ${unknown.join(", ")}`,};}-- const disabled = requestedTypes.filter((id) => {- const type = allTypes.find((t) => t.id === id);- return type && !type.enabled;- });- if (disabled.length > 0) {- return {- success: false,- error: `Disabled eval types: ${disabled.join(", ")}`,- };- }-- enabledTypes = allTypes.filter((t) => requestedTypes.includes(t.id));- } else {- enabledTypes = allTypes.filter((t) => t.enabled);-- if (enabledTypes.length === 0) {- enabledTypes = allTypes- .filter((t) => ["hallucination", "bias", "toxicity"].includes(t.id))- .map((t) => ({ ...t, enabled: true }));- }}- // Collect rules with priority from enabled types, same as the real-time/- // auto path, so offline evals resolve rule-engine context identically for- // the same configured evaluation types.- const rulesWithPriority = collectRulesWithPriority(enabledTypes);-let contextContents: string[] = [];let matchingRuleIds: string[] = [];let contextEntityIds: string[] = [];if (attributes && Object.keys(attributes).length > 0) {- const result =- rulesWithPriority.length > 0- ? await getContextFromRulesWithPriorityForFields(- attributes,- rulesWithPriority,- databaseConfigId- )- : await getContextFromFields(attributes, databaseConfigId);- contextContents = result.contextContents;- matchingRuleIds = result.matchingRuleIds;- contextEntityIds = result.contextEntityIds || [];+ try {+ const ruleResult = await evaluateRules(+ {+ fields: attributes,+ entity_type: "context" as any,+ include_entity_data: true,+ },+ databaseConfigId+ );++ matchingRuleIds = ruleResult.matchingRuleIds || [];++ if (ruleResult.entity_data) {+ for (const [key, entityData] of Object.entries(+ ruleResult.entity_data+ )) {+ if (entityData?.content) {+ contextContents.push(String(entityData.content));+ const match = key.match(/^context:(.+)$/);+ if (match) contextEntityIds.push(match[1]);+ }+ }+ }+ } catch (e) {+ consoleLog("Rule engine evaluation failed for offline eval:", e);+ }}const userContextsCount = userContexts?.length || 0;@@ -1013,12 +1169,6 @@ export async function runOfflineEvaluation(return { success: false, error: data.error };}- const evaluations = applyPerTypeThresholds(- data.result || [],- enabledTypes,- thresholdScore- );-const metaBase: Record<string, string> = {model: `${evaluationConfig.provider}/${evaluationConfig.model}`,ruleIds: matchingRuleIds.join(","),@@ -1049,16 +1199,11 @@ export async function runOfflineEvaluation(if (storeResults) {const spanId = `offline_${crypto.randomUUID()}`;- const customTypes = allTypes.map((t) => ({- id: t.id,- label: (t as any).label || t.id,- }));const storeResult = await storeEvaluation(spanId,- evaluations,+ data.result || [],metaBase,- databaseConfigId,- customTypes+ databaseConfigId);if (storeResult?.err) {consoleLog("Failed to store offline eval results:", storeResult.err);@@ -1067,7 +1212,7 @@ export async function runOfflineEvaluation(return {success: true,- evaluations,+ evaluations: data.result || [],contextApplied: {ruleMatched: matchingRuleIds.length > 0,matchingRuleIds,diff --git a/src/client/src/lib/platform/gpu/external.ts b/src/client/src/lib/platform/gpu/external.tsnew file mode 100644index 00000000..a3671200--- /dev/null+++ b/src/client/src/lib/platform/gpu/external.ts@@ -0,0 +1,211 @@+/**+ * External-metrics path for GPU dashboard widgets.+ * Built-in ClickHouse keeps SQL in sibling modules; this routes through the+ * metrics signal binding so Prometheus / Mimir / etc. can serve gpu.* gauges.+ */++import type { GPUMetricParams } from "@/lib/platform/common";+import { metricParamsToOpenLITQuery } from "@/lib/platform/connectors/datasource/clickhouse/query-map";+import type { OpenLITQuery } from "@/lib/platform/connectors/datasource/types";++async function resolveExternalMetrics(environment?: string) {+ const { getTelemetryAdapter, resolveTelemetrySourceDescriptor } =+ await import("@/lib/telemetry-source");+ const descriptor = await resolveTelemetrySourceDescriptor({+ signal: "metrics",+ environment,+ });+ if (descriptor.isBuiltIn || descriptor.type === "clickhouse") {+ return null;+ }+ const adapter = await getTelemetryAdapter({ signal: "metrics", environment });+ return { adapter, descriptor };+}++function asError(err: unknown): string {+ return err instanceof Error ? err.message : String(err);+}++function gpuMetricQuery(+ params: GPUMetricParams,+ metricNames: string[],+ aggregations?: OpenLITQuery["aggregations"]+): OpenLITQuery {+ const base = metricParamsToOpenLITQuery(params, "metrics");+ return {+ ...base,+ filters: [+ ...(base.filters || []),+ { target: "spanName", op: "in", value: metricNames },+ ],+ aggregations,+ };+}++function averageOf(rows: Record<string, unknown>[]): number {+ const values = rows+ .map((row) => Number(row.value ?? row.count ?? 0))+ .filter((value) => Number.isFinite(value));+ if (values.length === 0) return 0;+ return Math.round((values.reduce((sum, value) => sum + value, 0) / values.length) * 100) / 100;+}++function bucketByTime(+ rows: Record<string, unknown>[],+ valueKey: string+): Array<Record<string, unknown>> {+ const buckets = new Map<string, number[]>();+ for (const row of rows) {+ const label = String(row.request_time ?? row.timestamp ?? row.label ?? "");+ if (!label) continue;+ const value = Number(row.value ?? 0);+ if (!Number.isFinite(value)) continue;+ const list = buckets.get(label) || [];+ list.push(value);+ buckets.set(label, list);+ }+ return Array.from(buckets.entries())+ .sort(([a], [b]) => a.localeCompare(b))+ .map(([request_time, values]) => ({+ request_time,+ [valueKey]:+ Math.round(+ (values.reduce((sum, value) => sum + value, 0) / values.length) * 100+ ) / 100,+ }));+}++async function externalGpuAverage(+ params: GPUMetricParams,+ metricName: string,+ valueKey: string+) {+ const resolved = await resolveExternalMetrics(params.environment);+ if (!resolved) return null;+ try {+ const frame = await resolved.adapter.metricTimeSeries(+ gpuMetricQuery(params, [metricName], [{ fn: "avg", as: valueKey }])+ );+ return {+ err: null,+ data: [{ [valueKey]: averageOf(frame.rows as unknown as Record<string, unknown>[]) }],+ };+ } catch (err) {+ return { err: asError(err), data: [] };+ }+}++async function externalGpuSeries(+ params: GPUMetricParams,+ metricNames: string[],+ mapName: (metricName: string) => string+) {+ const resolved = await resolveExternalMetrics(params.environment);+ if (!resolved) return null;+ try {+ const frame = await resolved.adapter.metricTimeSeries(+ gpuMetricQuery(params, metricNames, [{ fn: "avg", as: "value" }])+ );+ const byTime = new Map<string, Record<string, unknown>>();+ for (const row of frame.rows as unknown as Record<string, unknown>[]) {+ const request_time = String(+ row.request_time ?? row.timestamp ?? row.label ?? ""+ );+ if (!request_time) continue;+ const metricName = String(row.metricName ?? row.__name__ ?? "");+ const key = mapName(metricName);+ const value = Number(row.value ?? 0);+ if (!key || !Number.isFinite(value)) continue;+ const bucket = byTime.get(request_time) || { request_time };+ bucket[key] = value;+ byTime.set(request_time, bucket);+ }+ return {+ err: null,+ data: Array.from(byTime.values()).sort((a, b) =>+ String(a.request_time).localeCompare(String(b.request_time))+ ),+ };+ } catch (err) {+ return { err: asError(err), data: [] };+ }+}++export async function externalAverageUtilization(params: GPUMetricParams) {+ return externalGpuAverage(params, "gpu.utilization", "utilization");+}++export async function externalUtilizationParamsPerTime(params: GPUMetricParams) {+ return externalGpuSeries(+ params,+ ["gpu.utilization", "gpu.enc.utilization", "gpu.dec.utilization"],+ (name) => name.replace(/^gpu\./, "").replaceAll(".", "_")+ );+}++export async function externalAverageTemperature(params: GPUMetricParams) {+ return externalGpuAverage(params, "gpu.temperature", "temperature");+}++export async function externalAverageTemperatureParamsPerTime(+ params: GPUMetricParams+) {+ const resolved = await resolveExternalMetrics(params.environment);+ if (!resolved) return null;+ try {+ const frame = await resolved.adapter.metricTimeSeries(+ gpuMetricQuery(params, ["gpu.temperature"], [{ fn: "avg", as: "temperature" }])+ );+ return {+ err: null,+ data: bucketByTime(frame.rows as unknown as Record<string, unknown>[], "temperature"),+ };+ } catch (err) {+ return { err: asError(err), data: [] };+ }+}++export async function externalAveragePowerDraw(params: GPUMetricParams) {+ return externalGpuAverage(params, "gpu.power.draw", "power_draw");+}++export async function externalPowerParamsPerTime(params: GPUMetricParams) {+ return externalGpuSeries(+ params,+ ["gpu.power.draw", "gpu.power.limit"],+ (name) => name.replace(/^gpu\./, "").replaceAll(".", "_")+ );+}++export async function externalAverageMemoryUsage(params: GPUMetricParams) {+ return externalGpuAverage(params, "gpu.memory.used", "memory_used");+}++export async function externalMemoryParamsPerTime(params: GPUMetricParams) {+ return externalGpuSeries(+ params,+ [+ "gpu.memory.available",+ "gpu.memory.total",+ "gpu.memory.used",+ "gpu.memory.free",+ ],+ (name) => name.replace(/^gpu\./, "").replaceAll(".", "_")+ );+}++export async function externalFanspeedParamsPerTime(params: GPUMetricParams) {+ const resolved = await resolveExternalMetrics(params.environment);+ if (!resolved) return null;+ try {+ const frame = await resolved.adapter.metricTimeSeries(+ gpuMetricQuery(params, ["gpu.fan_speed"], [{ fn: "avg", as: "fan_speed" }])+ );+ return {+ err: null,+ data: bucketByTime(frame.rows as unknown as Record<string, unknown>[], "fan_speed"),+ };+ } catch (err) {+ return { err: asError(err), data: [] };+ }+}diff --git a/src/client/src/lib/platform/gpu/fanspeed.ts b/src/client/src/lib/platform/gpu/fanspeed.tsindex c56f004a..fd7324ae 100644--- a/src/client/src/lib/platform/gpu/fanspeed.ts+++ b/src/client/src/lib/platform/gpu/fanspeed.ts@@ -7,8 +7,12 @@ import {OTEL_GPUS_TABLE_NAME,GPUMetricParams,} from "../common";+import { externalFanspeedParamsPerTime } from "./external";export async function getFanspeedParamsPerTime(params: GPUMetricParams) {+ const external = await externalFanspeedParamsPerTime(params);+ if (external) return external;+const { start, end } = params.timeLimit;const dateTrunc = dateTruncGroupingLogic(end as Date, start as Date);diff --git a/src/client/src/lib/platform/gpu/memory.ts b/src/client/src/lib/platform/gpu/memory.tsindex 685576e7..8602fdc9 100644--- a/src/client/src/lib/platform/gpu/memory.ts+++ b/src/client/src/lib/platform/gpu/memory.ts@@ -7,8 +7,15 @@ import {OTEL_GPUS_TABLE_NAME,GPUMetricParams,} from "../common";+import {+ externalAverageMemoryUsage,+ externalMemoryParamsPerTime,+} from "./external";export async function getMemoryParamsPerTime(params: GPUMetricParams) {+ const external = await externalMemoryParamsPerTime(params);+ if (external) return external;+const keys = ["memory.available","memory.total",@@ -59,6 +66,9 @@ export async function getMemoryParamsPerTime(params: GPUMetricParams) {}export async function getAverageMemoryUsage(params: GPUMetricParams) {+ const external = await externalAverageMemoryUsage(params);+ if (external) return external;+const query = `SELECTROUND(AVG(Value), 2) as memory_useddiff --git a/src/client/src/lib/platform/gpu/power.ts b/src/client/src/lib/platform/gpu/power.tsindex c395f391..d6b4930e 100644--- a/src/client/src/lib/platform/gpu/power.ts+++ b/src/client/src/lib/platform/gpu/power.ts@@ -7,8 +7,15 @@ import {OTEL_GPUS_TABLE_NAME,dataCollector,} from "../common";+import {+ externalAveragePowerDraw,+ externalPowerParamsPerTime,+} from "./external";export async function getAveragePowerDraw(params: GPUMetricParams) {+ const external = await externalAveragePowerDraw(params);+ if (external) return external;+const query = `SELECTROUND(AVG(Value), 2) as power_draw@@ -23,6 +30,9 @@ export async function getAveragePowerDraw(params: GPUMetricParams) {}export async function getPowerParamsPerTime(params: GPUMetricParams) {+ const external = await externalPowerParamsPerTime(params);+ if (external) return external;+const keys = ["power.draw", "power.limit"];const { start, end } = params.timeLimit;const dateTrunc = dateTruncGroupingLogic(end as Date, start as Date);diff --git a/src/client/src/lib/platform/gpu/temperature.ts b/src/client/src/lib/platform/gpu/temperature.tsindex 4bae6117..5328839b 100644--- a/src/client/src/lib/platform/gpu/temperature.ts+++ b/src/client/src/lib/platform/gpu/temperature.ts@@ -7,8 +7,15 @@ import {OTEL_GPUS_TABLE_NAME,dataCollector,} from "../common";+import {+ externalAverageTemperature,+ externalAverageTemperatureParamsPerTime,+} from "./external";export async function getAverageTemperature(params: GPUMetricParams) {+ const external = await externalAverageTemperature(params);+ if (external) return external;+const query = `SELECTROUND(AVG(Value), 2) as temperature@@ -27,6 +34,9 @@ export async function getAverageTemperature(params: GPUMetricParams) {export async function getAverageTemperatureParamsPerTime(params: GPUMetricParams) {+ const external = await externalAverageTemperatureParamsPerTime(params);+ if (external) return external;+const { start, end } = params.timeLimit;const dateTrunc = dateTruncGroupingLogic(end as Date, start as Date);diff --git a/src/client/src/lib/platform/gpu/utilization.ts b/src/client/src/lib/platform/gpu/utilization.tsindex 9443af7e..cf00c6e1 100644--- a/src/client/src/lib/platform/gpu/utilization.ts+++ b/src/client/src/lib/platform/gpu/utilization.ts@@ -7,8 +7,15 @@ import {OTEL_GPUS_TABLE_NAME,dataCollector,} from "../common";+import {+ externalAverageUtilization,+ externalUtilizationParamsPerTime,+} from "./external";export async function getAverageUtilization(params: GPUMetricParams) {+ const external = await externalAverageUtilization(params);+ if (external) return external;+const query = `SELECTROUND(AVG(Value), 2) as utilization@@ -25,6 +32,9 @@ export async function getAverageUtilization(params: GPUMetricParams) {}export async function getUtilizationParamsPerTime(params: GPUMetricParams) {+ const external = await externalUtilizationParamsPerTime(params);+ if (external) return external;+const keys = ["utilization", "enc.utilization", "dec.utilization"];const { start, end } = params.timeLimit;const dateTrunc = dateTruncGroupingLogic(end as Date, start as Date);diff --git a/src/client/src/lib/platform/llm/category.ts b/src/client/src/lib/platform/llm/category.tsindex 59873d01..ac637898 100644--- a/src/client/src/lib/platform/llm/category.ts+++ b/src/client/src/lib/platform/llm/category.ts@@ -1,8 +1,12 @@import { getTraceMappingKeyFullPath } from "@/helpers/server/trace";import { MetricParams, dataCollector, OTEL_TRACES_TABLE_NAME } from "../common";import { getFilterWhereCondition } from "@/helpers/server/platform";+import { externalGenerationByCategories } from "./external";export async function getResultGenerationByCategories(params: MetricParams) {+ const external = await externalGenerationByCategories(params);+ if (external) return external;+const keyPath = `SpanAttributes['${getTraceMappingKeyFullPath("type")}']`;const query = `SELECT${keyPath} AS category,diff --git a/src/client/src/lib/platform/llm/cost.ts b/src/client/src/lib/platform/llm/cost.tsindex 90cb193c..01296b5f 100644--- a/src/client/src/lib/platform/llm/cost.ts+++ b/src/client/src/lib/platform/llm/cost.ts@@ -8,6 +8,12 @@ import {getTraceMappingKeyFullPath,getTraceMappingKeyFullPaths,} from "@/helpers/server/trace";+import {+ externalAverageCost,+ externalCostByApplication,+ externalCostByEnvironment,+ externalTotalCost,+} from "./external";function getProviderKeyPath() {const paths = (getTraceMappingKeyFullPaths("provider") as string[]).map(@@ -23,6 +29,9 @@ function getProviderKeyPath() {}export async function getTotalCost(params: MetricParams) {+ const external = await externalTotalCost(params);+ if (external) return external;+const keyPath = `SpanAttributes['${getTraceMappingKeyFullPath("cost")}']`;const currentWhereParams = { ...params, notEmpty: [{ key: keyPath }] };const previousWhereParams = getFilterPreviousParams(currentWhereParams);@@ -55,6 +64,9 @@ export async function getTotalCost(params: MetricParams) {}export async function getAverageCost(params: MetricParams) {+ const external = await externalAverageCost(params);+ if (external) return external;+const keyPath = `SpanAttributes['${getTraceMappingKeyFullPath("cost")}']`;const currentWhereParams = { ...params, notEmpty: [{ key: keyPath }] };@@ -88,6 +100,17 @@ export async function getAverageCost(params: MetricParams) {}export async function getCostByApplication(params: MetricParams) {+ const external = await externalCostByApplication(params);+ if (external) {+ return {+ err: external.err,+ data: (external.data || []).map((row: any) => ({+ applicationName: row.application,+ cost: row.total_cost,+ })),+ };+ }+// Prefer OTel ServiceName / service.name (what instrumented apps emit).// Legacy spans may still carry gen_ai.application_name. The previous// path wrapped the SpanAttributes key in ResourceAttributes[...], which@@ -117,8 +140,9 @@ export async function getCostByApplication(params: MetricParams) {}export async function getCostByEnvironment(params: MetricParams) {- // See `helpers/server/platform.ts` — environment lives at- // `ResourceAttributes['deployment.environment']` (OTel standard).+ const external = await externalCostByEnvironment(params);+ if (external) return external;+const keyPathEnvironment = `ResourceAttributes['deployment.environment']`;const keyPathCost = `SpanAttributes['${getTraceMappingKeyFullPath("cost")}']`;const query = `SELECTdiff --git a/src/client/src/lib/platform/llm/endpoint.ts b/src/client/src/lib/platform/llm/endpoint.tsindex 2c7f1240..5c65a2e1 100644--- a/src/client/src/lib/platform/llm/endpoint.ts+++ b/src/client/src/lib/platform/llm/endpoint.ts@@ -1,6 +1,7 @@import { getFilterWhereCondition } from "@/helpers/server/platform";import { MetricParams, dataCollector, OTEL_TRACES_TABLE_NAME } from "../common";import { getTraceMappingKeyFullPaths } from "@/helpers/server/trace";+import { externalGenerationByProvider } from "./external";function getProviderKeyPath() {const paths = (getTraceMappingKeyFullPaths("provider") as string[]).map(@@ -16,6 +17,9 @@ function getProviderKeyPath() {}export async function getResultGenerationByEndpoint(params: MetricParams) {+ const external = await externalGenerationByProvider(params);+ if (external) return external;+const { paths, keyPath } = getProviderKeyPath();const query = `SELECTdiff --git a/src/client/src/lib/platform/llm/external.ts b/src/client/src/lib/platform/llm/external.tsnew file mode 100644index 00000000..57e41196--- /dev/null+++ b/src/client/src/lib/platform/llm/external.ts@@ -0,0 +1,424 @@+/**+ * External-traces path for LLM dashboard widgets.+ * Built-in ClickHouse keeps SQL in sibling modules; this routes through the+ * datasource QueryPlanner (L0/L1/L2) so Tempo/Jaeger/Datadog/etc. all work.+ */++import type { MetricParams } from "@/lib/platform/common";+import { metricParamsToOpenLITQuery } from "@/lib/platform/connectors/datasource/clickhouse/query-map";+import {+ intervalFromTimeRange,+ planAndAggregateSpans,+ planAndSpanTimeSeries,+} from "@/lib/platform/connectors/datasource/query-planner";+import {+ readLlmRollup,+ readSignalBucketRollup,+} from "@/lib/platform/telemetry/rollups";+import { shouldPreferRollup } from "@/lib/platform/connectors/datasource/rollup-policy";+import { getFilterPreviousParams } from "@/helpers/server/platform";+import type { OpenLITQuery } from "@/lib/platform/connectors/datasource/types";++async function resolveExternalTraces(environment?: string) {+ const { getTelemetryAdapter, resolveTelemetrySourceDescriptor } =+ await import("@/lib/telemetry-source");+ const descriptor = await resolveTelemetrySourceDescriptor({+ signal: "traces",+ environment,+ });+ if (descriptor.isBuiltIn || descriptor.type === "clickhouse") {+ return null;+ }+ const adapter = await getTelemetryAdapter({ signal: "traces", environment });+ return { adapter, descriptor };+}++function asError(err: unknown): string {+ return err instanceof Error ? err.message : String(err);+}++export async function externalTotalCost(params: MetricParams) {+ const resolved = await resolveExternalTraces(params.environment);+ if (!resolved) return null;+ const { adapter, descriptor } = resolved;+ try {+ const agg = async (p: MetricParams) => {+ const frame = await planAndAggregateSpans(+ adapter,+ {+ ...metricParamsToOpenLITQuery(p, "traces"),+ aggregations: [+ { fn: "sum", field: "gen_ai.usage.cost", as: "total_usage_cost" },+ ],+ },+ {+ preferRollup: shouldPreferRollup(params),+ readRollup: async (q) => {+ const series = await readSignalBucketRollup(q, {+ sourceId: descriptor.id,+ dbConfigId: descriptor.dbConfigId,+ });+ if (!series) return null;+ const total = (series.rows as Record<string, unknown>[]).reduce(+ (s, r) => s + Number(r.cost ?? 0),+ 0+ );+ return {+ fields: [],+ rows: [{ total_usage_cost: total }],+ meta: series.meta,+ };+ },+ }+ );+ return Number(+ (frame.rows[0] as Record<string, unknown> | undefined)+ ?.total_usage_cost ?? 0+ );+ };+ const total_usage_cost = await agg(params);+ const previous_total_usage_cost = await agg(getFilterPreviousParams(params));+ return {+ err: null,+ data: [{ total_usage_cost, previous_total_usage_cost }],+ };+ } catch (err) {+ return { err: asError(err), data: [] };+ }+}++export async function externalAverageCost(params: MetricParams) {+ const resolved = await resolveExternalTraces(params.environment);+ if (!resolved) return null;+ const { adapter } = resolved;+ try {+ const frame = await planAndAggregateSpans(adapter, {+ ...metricParamsToOpenLITQuery(params, "traces"),+ aggregations: [+ { fn: "avg", field: "gen_ai.usage.cost", as: "average_usage_cost" },+ ],+ });+ return {+ err: null,+ data: [+ {+ average_usage_cost: Number(+ (frame.rows[0] as Record<string, unknown> | undefined)+ ?.average_usage_cost ?? 0+ ),+ },+ ],+ };+ } catch (err) {+ return { err: asError(err), data: [] };+ }+}++export async function externalCostPerTime(params: MetricParams) {+ const resolved = await resolveExternalTraces(params.environment);+ if (!resolved) return null;+ const { adapter, descriptor } = resolved;+ try {+ const base = metricParamsToOpenLITQuery(params, "traces");+ const frame = await planAndSpanTimeSeries(+ adapter,+ {+ ...base,+ interval: intervalFromTimeRange(+ base.timeRange.start,+ base.timeRange.end+ ),+ aggregations: [+ { fn: "sum", field: "gen_ai.usage.cost", as: "total_cost" },+ ],+ },+ {+ preferRollup: shouldPreferRollup(params),+ readRollup: (q) =>+ readSignalBucketRollup(q, {+ sourceId: descriptor.id,+ dbConfigId: descriptor.dbConfigId,+ }),+ }+ );+ return {+ err: null,+ data: (frame.rows as Record<string, unknown>[]).map((row) => ({+ total_cost: Number(row.total_cost ?? row.cost ?? 0),+ request_time: String(row.request_time ?? row.label ?? row.bucket ?? ""),+ })),+ };+ } catch (err) {+ return { err: asError(err), data: [] };+ }+}++export async function externalAverageTokens(+ params: MetricParams & { type?: string }+) {+ const resolved = await resolveExternalTraces(params.environment);+ if (!resolved) return null;+ const { adapter } = resolved;+ const field =+ params.type === "prompt"+ ? "gen_ai.usage.input_tokens"+ : params.type === "completion"+ ? "gen_ai.usage.output_tokens"+ : "gen_ai.usage.total_tokens";+ try {+ const frame = await planAndAggregateSpans(adapter, {+ ...metricParamsToOpenLITQuery(params, "traces"),+ aggregations: [{ fn: "avg", field, as: "total_tokens" }],+ });+ const total_tokens = Number(+ (frame.rows[0] as Record<string, unknown> | undefined)?.total_tokens ?? 0+ );+ if (params.type === "total") {+ const previous = await planAndAggregateSpans(adapter, {+ ...metricParamsToOpenLITQuery(getFilterPreviousParams(params), "traces"),+ aggregations: [{ fn: "avg", field, as: "total_tokens" }],+ });+ return {+ err: null,+ data: [+ {+ total_tokens,+ previous_total_tokens: Number(+ (previous.rows[0] as Record<string, unknown> | undefined)+ ?.total_tokens ?? 0+ ),+ },+ ],+ };+ }+ return { err: null, data: [{ total_tokens }] };+ } catch (err) {+ return { err: asError(err), data: [] };+ }+}++export async function externalTokensPerTime(params: MetricParams) {+ const resolved = await resolveExternalTraces(params.environment);+ if (!resolved) return null;+ const { adapter, descriptor } = resolved;+ try {+ const base = metricParamsToOpenLITQuery(params, "traces");+ const frame = await planAndSpanTimeSeries(+ adapter,+ {+ ...base,+ interval: intervalFromTimeRange(+ base.timeRange.start,+ base.timeRange.end+ ),+ aggregations: [+ { fn: "sum", field: "gen_ai.usage.total_tokens", as: "total_tokens" },+ ],+ },+ {+ preferRollup: shouldPreferRollup(params),+ readRollup: (q) =>+ readSignalBucketRollup(q, {+ sourceId: descriptor.id,+ dbConfigId: descriptor.dbConfigId,+ }),+ }+ );+ return {+ err: null,+ data: (frame.rows as Record<string, unknown>[]).map((row) => ({+ total_tokens: Number(row.total_tokens ?? row.tokens ?? 0),+ request_time: String(row.request_time ?? row.label ?? row.bucket ?? ""),+ })),+ };+ } catch (err) {+ return { err: asError(err), data: [] };+ }+}++async function externalGroupBy(+ params: MetricParams,+ field: string,+ dimension: string,+ valueKey: string+) {+ const resolved = await resolveExternalTraces(params.environment);+ if (!resolved) return null;+ const { adapter, descriptor } = resolved;+ try {+ const query: OpenLITQuery = {+ ...metricParamsToOpenLITQuery(params, "traces"),+ groupBy: [field],+ aggregations: [{ fn: "count", as: "count" }],+ };+ const frame = await planAndAggregateSpans(adapter, query, {+ preferRollup: shouldPreferRollup(params),+ readRollup: (q) =>+ readLlmRollup(q, {+ sourceId: descriptor.id,+ dbConfigId: descriptor.dbConfigId,+ dimension,+ }),+ });+ return {+ err: null,+ data: (frame.rows as Record<string, unknown>[]).map((row) => ({+ [valueKey]: String(row.group_value ?? row.g0 ?? row[field] ?? ""),+ count: Number(row.count ?? 0),+ })),+ };+ } catch (err) {+ return { err: asError(err), data: [] };+ }+}++export async function externalGenerationByCategories(params: MetricParams) {+ return externalGroupBy(+ params,+ "gen_ai.operation.name",+ "category",+ "category"+ );+}++export async function externalGenerationByProvider(params: MetricParams) {+ return externalGroupBy(params, "gen_ai.system", "provider", "provider");+}++export async function externalTopModels(params: MetricParams) {+ return externalGroupBy(params, "gen_ai.request.model", "model", "model");+}++export async function externalCostByApplication(params: MetricParams) {+ const resolved = await resolveExternalTraces(params.environment);+ if (!resolved) return null;+ const { adapter, descriptor } = resolved;+ try {+ const frame = await planAndAggregateSpans(+ adapter,+ {+ ...metricParamsToOpenLITQuery(params, "traces"),+ groupBy: ["service.name"],+ aggregations: [+ { fn: "sum", field: "gen_ai.usage.cost", as: "total_cost" },+ ],+ },+ {+ preferRollup: shouldPreferRollup(params),+ readRollup: (q) =>+ readLlmRollup(q, {+ sourceId: descriptor.id,+ dbConfigId: descriptor.dbConfigId,+ dimension: "service",+ }),+ }+ );+ return {+ err: null,+ data: (frame.rows as Record<string, unknown>[]).map((row) => ({+ application: String(+ row.group_value ?? row.g0 ?? row["service.name"] ?? ""+ ),+ total_cost: Number(row.total_cost ?? 0),+ })),+ };+ } catch (err) {+ return { err: asError(err), data: [] };+ }+}++export async function externalCostByEnvironment(params: MetricParams) {+ const resolved = await resolveExternalTraces(params.environment);+ if (!resolved) return null;+ const { adapter, descriptor } = resolved;+ try {+ const frame = await planAndAggregateSpans(+ adapter,+ {+ ...metricParamsToOpenLITQuery(params, "traces"),+ groupBy: ["deployment.environment"],+ aggregations: [+ { fn: "sum", field: "gen_ai.usage.cost", as: "total_cost" },+ ],+ },+ {+ preferRollup: shouldPreferRollup(params),+ readRollup: (q) =>+ readLlmRollup(q, {+ sourceId: descriptor.id,+ dbConfigId: descriptor.dbConfigId,+ dimension: "environment",+ }),+ }+ );+ return {+ err: null,+ data: (frame.rows as Record<string, unknown>[]).map((row) => ({+ environment: String(+ row.group_value ??+ row.g0 ??+ row["deployment.environment"] ??+ ""+ ),+ cost: Number(row.total_cost ?? 0),+ })),+ };+ } catch (err) {+ return { err: asError(err), data: [] };+ }+}++export async function externalModelsPerTime(params: MetricParams) {+ const resolved = await resolveExternalTraces(params.environment);+ if (!resolved) return null;+ const { adapter } = resolved;+ try {+ const base = metricParamsToOpenLITQuery(params, "traces");+ const frame = await planAndSpanTimeSeries(adapter, {+ ...base,+ interval: intervalFromTimeRange(+ base.timeRange.start,+ base.timeRange.end+ ),+ groupBy: ["gen_ai.request.model"],+ aggregations: [{ fn: "count", as: "model_count" }],+ });+ const byTime = new Map<+ string,+ { models: string[]; model_counts: number[]; total: number }+ >();+ for (const row of frame.rows as Record<string, unknown>[]) {+ const request_time = String(+ row.request_time ?? row.label ?? row.bucket ?? ""+ );+ const model = String(+ row.group_value ?? row.g0 ?? row["gen_ai.request.model"] ?? ""+ );+ const count = Number(row.model_count ?? row.count ?? 0);+ const entry = byTime.get(request_time) || {+ models: [],+ model_counts: [],+ total: 0,+ };+ if (model) {+ entry.models.push(model);+ entry.model_counts.push(count);+ }+ entry.total += count;+ byTime.set(request_time, entry);+ }+ return {+ err: null,+ data: Array.from(byTime.entries())+ .sort(([a], [b]) => a.localeCompare(b))+ .map(([request_time, entry]) => ({+ request_time,+ models: entry.models,+ model_counts: entry.model_counts,+ total_model_count: entry.total,+ })),+ };+ } catch (err) {+ return { err: asError(err), data: [] };+ }+}diff --git a/src/client/src/lib/platform/llm/model.ts b/src/client/src/lib/platform/llm/model.tsindex 91bac559..741d2bfc 100644--- a/src/client/src/lib/platform/llm/model.ts+++ b/src/client/src/lib/platform/llm/model.ts@@ -4,12 +4,27 @@ import {dateTruncGroupingLogic,getFilterWhereCondition,} from "@/helpers/server/platform";+import { externalModelsPerTime, externalTopModels } from "./external";export type ModelMetricParams = MetricParams & {top: number;};export async function getTopModels(params: ModelMetricParams) {+ const external = await externalTopModels(params);+ if (external) {+ return {+ err: external.err,+ data: (external.data || [])+ .slice(0, params.top)+ .map((row: any) => ({+ model: row.model,+ model_count: row.count,+ total: row.count,+ })),+ };+ }+const keyPath = `SpanAttributes['${getTraceMappingKeyFullPath("model")}']`;const query = `SELECT${keyPath} AS model,@@ -33,6 +48,9 @@ export async function getTopModels(params: ModelMetricParams) {}export async function getModelsPerTime(params: MetricParams) {+ const external = await externalModelsPerTime(params);+ if (external) return external;+const { start, end } = params.timeLimit;const dateTrunc = dateTruncGroupingLogic(end as Date, start as Date);diff --git a/src/client/src/lib/platform/llm/token.ts b/src/client/src/lib/platform/llm/token.tsindex f6750600..f02106a1 100644--- a/src/client/src/lib/platform/llm/token.ts+++ b/src/client/src/lib/platform/llm/token.ts@@ -5,6 +5,7 @@ import {getFilterPreviousParams,getFilterWhereCondition,} from "@/helpers/server/platform";+import { externalAverageTokens, externalTokensPerTime } from "./external";export type TOKEN_TYPE = "total" | "prompt" | "completion";@@ -23,6 +24,9 @@ function getFirstNonEmptyPath(paths: string[]) {}export async function getAverageTokensPerRequest(params: TokenParams) {+ const external = await externalAverageTokens(params);+ if (external) return external;+const tokenKey =params.type === "total"? "totalTokens"@@ -70,6 +74,9 @@ export async function getAverageTokensPerRequest(params: TokenParams) {}export async function getTokensPerTime(params: MetricParams) {+ const external = await externalTokensPerTime(params);+ if (external) return external;+const { start, end } = params.timeLimit;const dateTrunc = dateTruncGroupingLogic(end as Date, start as Date);diff --git a/src/client/src/lib/platform/logs/read.ts b/src/client/src/lib/platform/logs/read.tsnew file mode 100644index 00000000..b7692c70--- /dev/null+++ b/src/client/src/lib/platform/logs/read.ts@@ -0,0 +1,141 @@+/**+ * Logs read facade — choke point for Telemetry logs list/detail/summary.+ *+ * Every source resolves through the shared signal facade and executes the+ * DataSourceAdapter contract (including built-in ClickHouse).+ */++import type { MetricParams } from "@/lib/platform/common";+import { metricParamsToOpenLITQuery } from "@/lib/platform/connectors/datasource/clickhouse/query-map";+import { denormalizeLogToClickHouseRow } from "@/lib/platform/connectors/datasource/clickhouse/normalize";+import {+ facadeErrorMessage,+ resolveSignalReadContext,+ rethrowIfSourceFailure,+} from "@/lib/platform/connectors/datasource/facade";++export async function getLogs(params: MetricParams) {+ const { adapter } = await resolveSignalReadContext("logs", params);+ try {+ const frame = await adapter.listLogs(+ metricParamsToOpenLITQuery(params, "logs")+ );+ const rows = (frame.rows || []).map(denormalizeLogToClickHouseRow);+ const offset = params.offset || 0;+ const limit = params.limit || 25;+ return {+ err: null,+ records: rows.slice(offset, offset + limit),+ total: Number(frame.meta?.rowsScanned) || rows.length,+ freshness: frame.meta?.freshness || "live",+ };+ } catch (error) {+ rethrowIfSourceFailure(error);+ return { err: facadeErrorMessage(error), records: [], total: 0 };+ }+}++export async function getLogByRowId(+ rowId: string,+ params: Pick<+ MetricParams,+ "databaseConfigId" | "sourceId" | "environment"+ > & {+ aroundTimestamp?: string | Date;+ } = {}+) {+ const contextParams = {+ timeLimit: { start: new Date(0), end: new Date(), type: "all" },+ ...params,+ } as MetricParams;+ const { adapter } = await resolveSignalReadContext("logs", contextParams);+ try {+ const log = await adapter.getLog(rowId, {+ aroundTimestamp: params.aroundTimestamp,+ });+ return {+ err: null,+ record: log ? denormalizeLogToClickHouseRow(log) : undefined,+ };+ } catch (error) {+ rethrowIfSourceFailure(error);+ return { err: facadeErrorMessage(error), record: undefined };+ }+}++export async function getLogsConfig(params: MetricParams) {+ const { adapter } = await resolveSignalReadContext("logs", params);+ try {+ const query = metricParamsToOpenLITQuery(params, "logs");+ const services = await adapter+ .distinctValues("service.name", query)+ .catch(() => [] as string[]);+ return {+ err: null,+ data: [{ services, severities: [], totalRows: 0 }],+ };+ } catch (error) {+ rethrowIfSourceFailure(error);+ return { err: facadeErrorMessage(error), data: [] };+ }+}++export async function getLogAttributeKeys(params: MetricParams) {+ const { adapter } = await resolveSignalReadContext("logs", params);+ try {+ const keys = await adapter.attributeKeys(+ "logs",+ metricParamsToOpenLITQuery(params, "logs").timeRange+ );+ return {+ err: null,+ spanAttributeKeys: [],+ resourceAttributeKeys: keys,+ logAttributeKeys: keys,+ scopeAttributeKeys: [],+ };+ } catch (error) {+ rethrowIfSourceFailure(error);+ return {+ err: facadeErrorMessage(error),+ spanAttributeKeys: [],+ resourceAttributeKeys: [],+ logAttributeKeys: [],+ scopeAttributeKeys: [],+ };+ }+}++export async function getLogsSummary(params: MetricParams) {+ const { adapter } = await resolveSignalReadContext("logs", params);+ try {+ const frame = await adapter.logTimeSeries(+ metricParamsToOpenLITQuery(params, "logs")+ );+ const buckets = (frame.rows || []).map((row) => {+ const record = row as Record<string, unknown>;+ const count = Number(record.count ?? record.value ?? 0);+ return {+ ...record,+ label: String(record.label || record.timestamp || ""),+ count: Number.isFinite(count) ? count : 0,+ };+ });+ return {+ err: null,+ bucket: "auto",+ buckets,+ total: buckets.reduce((sum, row) => sum + row.count, 0),+ peak: buckets.reduce((max, row) => Math.max(max, row.count), 0),+ };+ } catch (error) {+ rethrowIfSourceFailure(error);+ return {+ err: facadeErrorMessage(error),+ bucket: "auto",+ buckets: [],+ total: 0,+ peak: 0,+ };+ }+}diff --git a/src/client/src/lib/platform/manage-dashboard/board-format.ts b/src/client/src/lib/platform/manage-dashboard/board-format.tsnew file mode 100644index 00000000..5aacd18e--- /dev/null+++ b/src/client/src/lib/platform/manage-dashboard/board-format.ts@@ -0,0 +1,235 @@+import { jsonParse } from "@/utils/json";+import type { Widget } from "@/types/manage-dashboard";++/** Legacy / unversioned dashboard JSON (seed files and pre-v2 exports). */+export const DASHBOARD_FORMAT_VERSION_V1 = 1;+/** Current portable dashboard export format. */+export const DASHBOARD_FORMAT_VERSION_V2 = 2;+export const CURRENT_DASHBOARD_FORMAT_VERSION = DASHBOARD_FORMAT_VERSION_V2;++export type DashboardLayoutItem = {+ i: string;+ x: number;+ y: number;+ w: number;+ h: number;+};++export type NormalizedDashboardImport = {+ formatVersion: number;+ title: string;+ description: string;+ isPinned: boolean;+ isMainDashboard: boolean;+ tags: string[];+ layouts: { lg: DashboardLayoutItem[] };+ widgets: Record<string, Widget>;+};++function asRecord(value: unknown): Record<string, unknown> | null {+ if (!value || typeof value !== "object" || Array.isArray(value)) return null;+ return value as Record<string, unknown>;+}++/**+ * Detect dashboard JSON format. Missing / unknown version is treated as v1+ * so older exported layouts and seed files keep working.+ */+export function detectDashboardFormatVersion(data: unknown): number {+ const record = asRecord(data);+ if (!record) return DASHBOARD_FORMAT_VERSION_V1;+ const version = record.formatVersion;+ if (version === 2 || version === "2" || version === "v2") {+ return DASHBOARD_FORMAT_VERSION_V2;+ }+ if (version === 1 || version === "1" || version === "v1") {+ return DASHBOARD_FORMAT_VERSION_V1;+ }+ return DASHBOARD_FORMAT_VERSION_V1;+}++/** Normalize tags whether they arrived as an array, JSON string, or missing. */+export function normalizeDashboardTags(tags: unknown): string[] {+ if (Array.isArray(tags)) {+ return tags+ .map((tag) => String(tag ?? "").trim())+ .filter((tag) => tag.length > 0);+ }+ if (typeof tags === "string") {+ const trimmed = tags.trim();+ if (!trimmed) return [];+ const parsed = jsonParse(trimmed);+ if (Array.isArray(parsed)) {+ return normalizeDashboardTags(parsed);+ }+ return [trimmed];+ }+ return [];+}++function normalizeLayoutItem(+ item: unknown,+ fallbackId: string+): DashboardLayoutItem | null {+ const record = asRecord(item);+ if (!record) return null;+ const i = typeof record.i === "string" && record.i ? record.i : fallbackId;+ const x = Number(record.x);+ const y = Number(record.y);+ const w = Number(record.w);+ const h = Number(record.h);+ if (![x, y, w, h].every((n) => Number.isFinite(n))) return null;+ return { i, x, y, w: Math.max(1, w), h: Math.max(1, h) };+}++function normalizeWidgets(widgets: unknown): Record<string, Widget> {+ const record = asRecord(widgets);+ if (!record) return {};+ const out: Record<string, Widget> = Object.create(null);+ for (const [key, value] of Object.entries(record)) {+ const widget = asRecord(value);+ if (!widget) continue;+ const id =+ typeof widget.id === "string" && widget.id ? widget.id : key;+ if (+ id === "__proto__" ||+ id === "constructor" ||+ id === "prototype" ||+ !/^[A-Za-z0-9_.:-]+$/.test(id)+ ) {+ continue;+ }+ out[id] = {+ ...(widget as unknown as Widget),+ id,+ title: typeof widget.title === "string" ? widget.title : "",+ description:+ typeof widget.description === "string" ? widget.description : "",+ type: typeof widget.type === "string" ? widget.type : "",+ properties:+ widget.properties && typeof widget.properties === "object"+ ? (widget.properties as Record<string, unknown>)+ : {},+ config:+ widget.config && typeof widget.config === "object"+ ? (widget.config as Widget["config"])+ : {},+ createdAt:+ typeof widget.createdAt === "string" ? widget.createdAt : "",+ updatedAt:+ typeof widget.updatedAt === "string" ? widget.updatedAt : "",+ };+ }+ return out;+}++function normalizeLayouts(+ layouts: unknown,+ widgetIds: string[]+): { lg: DashboardLayoutItem[] } {+ const record = asRecord(layouts);+ const rawLg = Array.isArray(record?.lg) ? record!.lg : [];+ const lg: DashboardLayoutItem[] = [];+ rawLg.forEach((item, index) => {+ const normalized = normalizeLayoutItem(+ item,+ widgetIds[index] || `widget-${index}`+ );+ if (normalized) lg.push(normalized);+ });++ // When layouts are missing (corrupt export), place widgets in a simple grid.+ if (lg.length === 0 && widgetIds.length > 0) {+ widgetIds.forEach((id, index) => {+ lg.push({+ i: id,+ x: (index % 4) * 3,+ y: Math.floor(index / 4) * 2,+ w: 3,+ h: 2,+ });+ });+ }++ return { lg };+}++/**+ * Validate and normalize an imported dashboard payload (v1 or v2) into the+ * internal shape used by `importBoardLayout`.+ */+export function normalizeImportedDashboard(+ data: unknown+): { data: NormalizedDashboardImport } | { err: string } {+ const record = asRecord(data);+ if (!record) {+ return { err: "Dashboard import must be a JSON object." };+ }++ const title = typeof record.title === "string" ? record.title.trim() : "";+ if (!title) {+ return { err: "Dashboard import requires a title." };+ }++ const widgets = normalizeWidgets(record.widgets);+ const widgetIds = Object.keys(widgets);+ const layouts = normalizeLayouts(record.layouts, widgetIds);++ // Drop layout entries that point at missing widgets; keep widgets that+ // lack a layout cell by synthesizing one later in normalizeLayouts.+ const knownWidgetIds = new Set(widgetIds);+ const filteredLg = layouts.lg.filter((item) => knownWidgetIds.has(item.i));+ const laidOut = new Set(filteredLg.map((item) => item.i));+ widgetIds.forEach((id, index) => {+ if (!laidOut.has(id)) {+ filteredLg.push({+ i: id,+ x: (index % 4) * 3,+ y: Math.floor(index / 4) * 2,+ w: 3,+ h: 2,+ });+ }+ });++ return {+ data: {+ formatVersion: detectDashboardFormatVersion(record),+ title,+ description:+ typeof record.description === "string" ? record.description : "",+ isPinned: Boolean(record.isPinned),+ isMainDashboard: Boolean(record.isMainDashboard),+ tags: normalizeDashboardTags(record.tags),+ layouts: { lg: filteredLg },+ widgets,+ },+ };+}++/**+ * Build a v2 export payload from `getBoardLayout` output. Older clients that+ * ignore `formatVersion` can still import the shared title/layouts/widgets+ * fields via the v1 path.+ */+export function toExportDashboardPayload(+ boardLayout: Record<string, unknown>+): Record<string, unknown> {+ const widgets = normalizeWidgets(boardLayout.widgets);+ const layouts = normalizeLayouts(boardLayout.layouts, Object.keys(widgets));++ return {+ formatVersion: CURRENT_DASHBOARD_FORMAT_VERSION,+ title:+ typeof boardLayout.title === "string" ? boardLayout.title : "Dashboard",+ description:+ typeof boardLayout.description === "string"+ ? boardLayout.description+ : "",+ isPinned: Boolean(boardLayout.isPinned),+ isMainDashboard: Boolean(boardLayout.isMainDashboard),+ tags: normalizeDashboardTags(boardLayout.tags),+ layouts,+ widgets,+ };+}diff --git a/src/client/src/lib/platform/manage-dashboard/board.ts b/src/client/src/lib/platform/manage-dashboard/board.tsindex 38557cc1..35c2d146 100644--- a/src/client/src/lib/platform/manage-dashboard/board.ts+++ b/src/client/src/lib/platform/manage-dashboard/board.ts@@ -8,7 +8,8 @@ import getMessage from "@/constants/messages";import Sanitizer from "@/utils/sanitizer";import { createWidget, getWidgets } from "./widget";import { pluck } from "lodash/fp";-import { jsonParse, jsonStringify } from "@/utils/json";+import { jsonStringify } from "@/utils/json";+import { normalizeImportedDashboard } from "./board-format";export function getBoardById(id: string) {const query = `@@ -467,9 +468,8 @@ export async function getMainDashboard(layout?: boolean, databaseConfigId?: strireturn { data: (mainDashboardData as any[])[0], err: null };}-// TODO: fix the type of dataexport async function importBoardLayout(- data: any,+ data: unknown,databaseConfigId?: string,options?: {// When true, widget ids from `data.widgets` are kept verbatim@@ -492,35 +492,36 @@ export async function importBoardLayout(preserveWidgetIds?: boolean;}) {+ const normalized = normalizeImportedDashboard(data);+ if ("err" in normalized) {+ return { err: normalized.err };+ }+const mainDashboard = await getMainDashboard(false, databaseConfigId);+ const imported = normalized.data;const boardData: Partial<Board> = {- title: data.title,- description: data.description,- isPinned: data.isPinned,- isMainDashboard: mainDashboard.data?.isMainDashboard ? false : data.isMainDashboard,- tags: data.tags ? jsonParse(data.tags) : [],+ title: imported.title,+ description: imported.description,+ isPinned: imported.isPinned,+ isMainDashboard: mainDashboard.data?.isMainDashboard+ ? false+ : imported.isMainDashboard,+ tags: imported.tags as unknown as string,};// Create the board firstconst boardResult = await createBoard(boardData as Board, databaseConfigId);- if ('err' in boardResult) {+ if ("err" in boardResult) {return { err: boardResult.err };}const newBoardId = boardResult.data.id;-- // Update the board layout with widgets and their positions- const layoutConfig = {- layouts: data.layouts,- widgets: data.widgets- };-const preserveWidgetIds = options?.preserveWidgetIds === true;- const widgetIdMap = new Map();+ const widgetIdMap = new Map<string, string>();- const updatedWidgets = Object.values(layoutConfig.widgets).map((widget: any) => {+ const updatedWidgets = Object.values(imported.widgets).map((widget) => {const nextWidgetId = preserveWidgetIds? widget.id: crypto.randomUUID();@@ -532,30 +533,41 @@ export async function importBoardLayout(};});- const updatedLayouts = layoutConfig.layouts.lg.map((layout: any) => {- return {- ...layout,- i: widgetIdMap.get(layout.i)- }- });+ const updatedLayouts = imported.layouts.lg+ .map((layout) => {+ const nextId = widgetIdMap.get(layout.i);+ if (!nextId) return null;+ return {+ ...layout,+ i: nextId,+ };+ })+ .filter((layout): layout is NonNullable<typeof layout> => !!layout);- await Promise.all(updatedWidgets.map(async (widget: any) => {- return await createWidget(widget, databaseConfigId);- }));+ await Promise.all(+ updatedWidgets.map(async (widget) => {+ return await createWidget(widget, databaseConfigId);+ })+ );const layoutConfigData = {layouts: {- lg: updatedLayouts+ lg: updatedLayouts,},- widgets: updatedWidgets.reduce((acc: any, widget: any) => {- acc[widget.id] = widget;- return acc;- }, {}),- }--- const { data: layoutData, err: layoutErr } = await updateBoardLayout(newBoardId, layoutConfigData, databaseConfigId);+ widgets: updatedWidgets.reduce(+ (acc: Record<string, (typeof updatedWidgets)[number]>, widget) => {+ acc[widget.id] = widget;+ return acc;+ },+ {}+ ),+ };+ const { data: layoutData, err: layoutErr } = await updateBoardLayout(+ newBoardId,+ layoutConfigData,+ databaseConfigId+ );if (layoutData) {return { data: boardResult.data };diff --git a/src/client/src/lib/platform/manage-dashboard/widget-sql-bridge.ts b/src/client/src/lib/platform/manage-dashboard/widget-sql-bridge.tsnew file mode 100644index 00000000..ecc3a5da--- /dev/null+++ b/src/client/src/lib/platform/manage-dashboard/widget-sql-bridge.ts@@ -0,0 +1,757 @@+/**+ * Bridge legacy ClickHouse SQL widgets onto external traces sources.+ * Seeded LLM dashboards store raw `otel_traces` SQL; when the project traces+ * binding is Tempo/Jaeger/etc., we infer a structured OpenLITQuery and run it+ * through the QueryPlanner instead of silently querying empty ClickHouse.+ */++import type { MetricParams } from "@/lib/platform/common";+import { metricParamsToOpenLITQuery } from "@/lib/platform/connectors/datasource/clickhouse/query-map";+import {+ intervalFromTimeRange,+ planAndAggregateSpans,+ planAndSpanTimeSeries,+} from "@/lib/platform/connectors/datasource/query-planner";+import { shouldPreferRollup } from "@/lib/platform/connectors/datasource/rollup-policy";+import type {+ Aggregation,+ DataSourceAdapter,+ OpenLITQuery,+} from "@/lib/platform/connectors/datasource/types";+import { getFilterPreviousParams } from "@/helpers/server/platform";++export type InferredWidgetMode = "aggregate" | "timeseries";++/**+ * `default` is the synthetic environment used by the built-in ClickHouse+ * setup. It must not be sent to an external telemetry backend as a real+ * deployment.environment filter; doing so makes otherwise valid dashboard+ * queries return zero rows.+ */+export function stripSyntheticDefaultEnvironment(+ query: OpenLITQuery+): OpenLITQuery {+ const filters = query.filters?.filter((filter) => {+ if (+ filter.target !== "attribute" ||+ filter.key !== "deployment.environment"+ ) {+ return true;+ }+ const values = Array.isArray(filter.value) ? filter.value : [filter.value];+ return !(values.length === 1 && String(values[0]) === "default");+ });+ return { ...query, filters: filters?.length ? filters : undefined };+}++export interface InferredWidgetQuery {+ mode: InferredWidgetMode;+ aggregations: Aggregation[];+ groupBy?: string[];+ /** Merge previous-period values + rate (stat cards). */+ includePrevious: boolean;+ primaryAlias: string;+ previousAlias?: string;+ rateAlias?: string;+}++/** True when SQL targets otel_traces and is not an evaluation-table query. */+export function isLegacyOtelTracesSql(sql: string): boolean {+ if (!/\botel_traces\b/i.test(sql)) return false;+ if (/\bopenlit_evaluation\b/i.test(sql)) return false;+ return true;+}++function firstAlias(sql: string, patterns: RegExp[]): string | undefined {+ for (const re of patterns) {+ const m = sql.match(re);+ if (m?.[1]) return m[1];+ }+ return undefined;+}++function escapeRegExp(value: string): string {+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");+}++/**+ * Heuristic SQL → structured query for common seeded LLM widgets.+ * Returns null when the query cannot be safely mapped (caller should error).+ */+export function inferStructuredFromClickHouseSql(+ sql: string+): InferredWidgetQuery | null {+ if (!isLegacyOtelTracesSql(sql)) return null;++ const includePrevious =+ /prev_start_time|_previous\b/i.test(sql) &&+ !/DATE_TRUNC|request_time\b/i.test(sql);++ const isTimeseries =+ /DATE_TRUNC|AS request_time|toStartOf/i.test(sql) &&+ !includePrevious;++ const aggregations: Aggregation[] = [];+ const groupBy: string[] = [];++ // Cost+ if (/gen_ai\.usage\.cost/i.test(sql) && /sum/i.test(sql)) {+ const as =+ firstAlias(sql, [+ /AS\s+(total_usage_cost)\b/i,+ /AS\s+(total_cost)\b/i,+ ]) || "total_cost";+ aggregations.push({+ fn: "sum",+ field: "gen_ai.usage.cost",+ as,+ });+ } else if (/gen_ai\.usage\.cost/i.test(sql) && /avg/i.test(sql)) {+ aggregations.push({+ fn: "avg",+ field: "gen_ai.usage.cost",+ as:+ firstAlias(sql, [/AS\s+(average_usage_cost)\b/i]) ||+ "average_usage_cost",+ });+ }++ // Tokens+ if (/gen_ai\.usage\.total_tokens/i.test(sql) && /avg/i.test(sql)) {+ aggregations.push({+ fn: "avg",+ field: "gen_ai.usage.total_tokens",+ as: firstAlias(sql, [/AS\s+(total_tokens)\b/i]) || "total_tokens",+ });+ } else if (/gen_ai\.usage\.input_tokens|prompt.?token/i.test(sql) && /avg/i.test(sql)) {+ aggregations.push({+ fn: "avg",+ field: "gen_ai.usage.input_tokens",+ as: firstAlias(sql, [/AS\s+(\w+)\b/i]) || "total_tokens",+ });+ } else if (+ /gen_ai\.usage\.output_tokens|completion.?token/i.test(sql) &&+ /avg/i.test(sql)+ ) {+ aggregations.push({+ fn: "avg",+ field: "gen_ai.usage.output_tokens",+ as: firstAlias(sql, [/AS\s+(\w+)\b/i]) || "total_tokens",+ });+ } else if (/gen_ai\.usage\.total_tokens/i.test(sql) && /sum/i.test(sql)) {+ aggregations.push({+ fn: "sum",+ field: "gen_ai.usage.total_tokens",+ as: firstAlias(sql, [/AS\s+(total_tokens)\b/i]) || "total_tokens",+ });+ // Seeded "Tokens Usage" area chart needs prompt + completion series too.+ if (/AS\s+prompt_tokens\b|gen_ai\.usage\.input_tokens/i.test(sql)) {+ aggregations.push({+ fn: "sum",+ field: "gen_ai.usage.input_tokens",+ as: "prompt_tokens",+ });+ }+ if (/AS\s+completion_tokens\b|gen_ai\.usage\.output_tokens/i.test(sql)) {+ aggregations.push({+ fn: "sum",+ field: "gen_ai.usage.output_tokens",+ as: "completion_tokens",+ });+ }+ }++ // Duration (seconds)+ if (/\bDuration\b/i.test(sql) && /avg/i.test(sql)) {+ aggregations.push({+ fn: "avg",+ field: "duration",+ as:+ firstAlias(sql, [/AS\s+(average_duration)\b/i]) ||+ "average_duration",+ });+ }++ // Counts (default for request totals / group-by charts)+ if (+ aggregations.length === 0 ||+ (/\bcount\s*\(/i.test(sql) || /\bcountIf\s*\(/i.test(sql))+ ) {+ if (+ aggregations.length === 0 ||+ (!/gen_ai\.usage\.cost/i.test(sql) &&+ !/gen_ai\.usage\.\w+_tokens/i.test(sql) &&+ !/\bDuration\b/i.test(sql))+ ) {+ const as =+ firstAlias(sql, [+ /AS\s+(total_model_count)\b/i,+ /AS\s+(total_request)\b/i,+ /AS\s+(total)\b/i,+ /AS\s+(count)\b/i,+ /AS\s+(model_count)\b/i,+ ]) || (isTimeseries ? "total" : "count");+ if (!aggregations.some((a) => a.fn === "count")) {+ aggregations.push({+ fn: "count",+ // Inner model×time buckets use model_count; fold later to total_model_count.+ as: as === "total_model_count" ? "model_count" : as,+ });+ }+ }+ }++ if (/gen_ai\.request\.model/i.test(sql) && /GROUP\s+BY/i.test(sql)) {+ groupBy.push("gen_ai.request.model");+ }+ if (/gen_ai\.system/i.test(sql) && /GROUP\s+BY/i.test(sql)) {+ groupBy.push("gen_ai.system");+ }+ if (/gen_ai\.operation\.name/i.test(sql) && /GROUP\s+BY/i.test(sql)) {+ groupBy.push("gen_ai.operation.name");+ }+ if (+ /ResourceAttributes\s*\[\s*'service\.name'\s*\]|service\.name/i.test(sql) &&+ /GROUP\s+BY/i.test(sql)+ ) {+ groupBy.push("service.name");+ }+ if (+ /deployment\.environment/i.test(sql) &&+ /GROUP\s+BY/i.test(sql)+ ) {+ groupBy.push("deployment.environment");+ }++ if (!aggregations.length) return null;++ const wantsModelsPerTime =+ isTimeseries &&+ (/AS\s+total_model_count\b/i.test(sql) ||+ /ARRAY_AGG\s*\(\s*model\b/i.test(sql));++ if (wantsModelsPerTime && !groupBy.includes("gen_ai.request.model")) {+ groupBy.push("gen_ai.request.model");+ }++ const primaryAlias = wantsModelsPerTime+ ? "total_model_count"+ : safeAlias(aggregations[0].as || "count", "count");+ const previousAlias = includePrevious+ ? firstAlias(sql, [+ /AS\s+(\w+_previous)\b/i,+ new RegExp(+ `AS\\s+(${escapeRegExp(primaryAlias)}_previous)\\b`,+ "i"+ ),+ ]) || `${primaryAlias}_previous`+ : undefined;++ return {+ mode: isTimeseries ? "timeseries" : "aggregate",+ aggregations,+ groupBy: groupBy.length ? groupBy : undefined,+ includePrevious,+ primaryAlias,+ previousAlias,+ rateAlias: includePrevious ? "rate" : undefined,+ };+}++/** Match seed SQL: NULL rate when there is no previous-period baseline. */+export function percentChange(+ current: number,+ previous: number+): number | null {+ if (previous === 0) return null;+ return Number((((current - previous) / previous) * 100).toFixed(4));+}++function rowNumber(+ row: Record<string, unknown> | undefined,+ keys: string[]+): number {+ if (!row) return 0;+ for (const key of keys) {+ const n = Number(row[key]);+ if (Number.isFinite(n)) return n;+ }+ return 0;+}++function renameGroupColumns(+ rows: Record<string, unknown>[],+ groupBy: string[] | undefined+): Record<string, unknown>[] {+ if (!groupBy?.length) return rows;+ const field = groupBy[0];+ const alias =+ field === "gen_ai.request.model"+ ? "model"+ : field === "gen_ai.system"+ ? "provider"+ : field === "gen_ai.operation.name"+ ? "category"+ : field === "service.name"+ ? "application"+ : field === "deployment.environment"+ ? "environment"+ : "group_value";+ return rows.map((row) => {+ const value =+ row[alias] ??+ row.group_value ??+ row.g0 ??+ row[field] ??+ "";+ const next: Record<string, unknown> = { ...row, [alias]: String(value) };+ if (field === "service.name") {+ next.applicationName = String(value);+ next.application = String(value);+ }+ return next;+ });+}++function escapeSql(value: string): string {+ return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'");+}++const SAFE_CH_IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/;+const ALLOWED_CH_FIELDS = new Set([+ "SpanId",+ "TraceId",+ "ParentSpanId",+ "SpanName",+ "ServiceName",+ "Duration",+ "Timestamp",+ "StatusCode",+ "StatusMessage",+ "Kind",+ "TraceType",+]);++function isSafeAlias(alias: string): boolean {+ return (+ alias !== "__proto__" &&+ alias !== "constructor" &&+ alias !== "prototype" &&+ SAFE_CH_IDENTIFIER.test(alias)+ );+}++function safeAlias(value: unknown, fallback: string): string {+ const alias = String(value || fallback);+ if (isSafeAlias(alias)) return alias;+ if (isSafeAlias(fallback)) return fallback;+ throw new Error("Invalid dashboard aggregation alias");+}++function fieldToClickHouseExpr(field: string, scope?: string): string {+ if (field === "SpanName" || field === "duration" || field === "Duration") {+ return field === "duration" ? "Duration" : field;+ }+ if (field === "service.name" || field === "ServiceName") {+ return "ServiceName";+ }+ if (scope === "resource" || field.startsWith("deployment.") || field === "telemetry.sdk.name") {+ return `ResourceAttributes['${escapeSql(field)}']`;+ }+ if (field.includes(".")) {+ return `SpanAttributes['${escapeSql(field)}']`;+ }+ if (!ALLOWED_CH_FIELDS.has(field)) {+ throw new Error(`Unsupported ClickHouse field: ${field}`);+ }+ return field;+}++function filterToClickHouseSql(filter: {+ target?: string;+ scope?: string;+ key?: string;+ op?: string;+ value?: string | string[] | number;+}): string | null {+ const op = filter.op || "eq";+ if (filter.target === "status") {+ const values = Array.isArray(filter.value)+ ? filter.value.map(String)+ : [String(filter.value ?? "")];+ const list = values+ .filter(Boolean)+ .map((v) => `'${escapeSql(v)}'`)+ .join(", ");+ return list ? `StatusCode IN (${list})` : null;+ }+ if (filter.target === "spanName") {+ const values = Array.isArray(filter.value)+ ? filter.value.map(String)+ : [String(filter.value ?? "")];+ if (op === "contains") {+ return `positionCaseInsensitive(SpanName, '${escapeSql(values[0] || "")}') > 0`;+ }+ const list = values+ .filter(Boolean)+ .map((v) => `'${escapeSql(v)}'`)+ .join(", ");+ return list ? `SpanName IN (${list})` : null;+ }+ if (filter.target === "duration") {+ const n = Number(filter.value);+ if (!Number.isFinite(n)) return null;+ if (op === "gt") return `Duration > ${n}`;+ if (op === "gte") return `Duration >= ${n}`;+ if (op === "lt") return `Duration < ${n}`;+ if (op === "lte") return `Duration <= ${n}`;+ return `Duration = ${n}`;+ }+ const key = filter.key || "";+ if (!key) return null;+ const expr = fieldToClickHouseExpr(key, filter.scope);+ if (op === "exists") return `notEmpty(toString(${expr}))`;+ if (op === "notExists") return `empty(toString(${expr}))`;+ if (op === "contains") {+ return `positionCaseInsensitive(toString(${expr}), '${escapeSql(+ String(filter.value ?? "")+ )}') > 0`;+ }+ if (op === "in" || op === "notIn") {+ const values = Array.isArray(filter.value)+ ? filter.value.map(String)+ : String(filter.value ?? "")+ .split(",")+ .map((v) => v.trim())+ .filter(Boolean);+ const list = values.map((v) => `'${escapeSql(v)}'`).join(", ");+ if (!list) return null;+ return op === "notIn"+ ? `toString(${expr}) NOT IN (${list})`+ : `toString(${expr}) IN (${list})`;+ }+ const cmp =+ op === "neq"+ ? "!="+ : op === "gt"+ ? ">"+ : op === "gte"+ ? ">="+ : op === "lt"+ ? "<"+ : op === "lte"+ ? "<="+ : "=";+ const raw = filter.value;+ if (typeof raw === "number" || (typeof raw === "string" && /^-?\d+(\.\d+)?$/.test(raw))) {+ return `toFloat64OrZero(toString(${expr})) ${cmp} ${Number(raw)}`;+ }+ return `toString(${expr}) ${cmp} '${escapeSql(String(raw ?? ""))}'`;+}++const AGG_SQL: Record<string, (field: string) => string> = {+ count: () => "count()",+ sum: (f) => `sum(toFloat64OrZero(${f}))`,+ avg: (f) => `avg(toFloat64OrZero(${f}))`,+ min: (f) => `min(toFloat64OrZero(${f}))`,+ max: (f) => `max(toFloat64OrZero(${f}))`,+ p50: (f) => `quantile(0.5)(toFloat64OrZero(${f}))`,+ p90: (f) => `quantile(0.9)(toFloat64OrZero(${f}))`,+ p95: (f) => `quantile(0.95)(toFloat64OrZero(${f}))`,+ p99: (f) => `quantile(0.99)(toFloat64OrZero(${f}))`,+ cardinality: (f) => `uniqExact(${f})`,+};++function resolveAggSql(fn: string): (field: string) => string {+ // Explicit allow-list dispatch — never index AGG_SQL with a raw user string.+ switch (fn) {+ case "sum":+ return AGG_SQL.sum;+ case "avg":+ return AGG_SQL.avg;+ case "min":+ return AGG_SQL.min;+ case "max":+ return AGG_SQL.max;+ case "p50":+ return AGG_SQL.p50;+ case "p90":+ return AGG_SQL.p90;+ case "p95":+ return AGG_SQL.p95;+ case "p99":+ return AGG_SQL.p99;+ case "cardinality":+ return AGG_SQL.cardinality;+ case "count":+ default:+ return AGG_SQL.count;+ }+}++function intervalToTrunc(interval?: string): string {+ if (!interval) return "hour";+ if (/d$/i.test(interval)) return "day";+ if (/m$/i.test(interval) && !/mo/i.test(interval)) return "minute";+ if (/s$/i.test(interval)) return "second";+ return "hour";+}++/**+ * Inverse of `inferStructuredFromClickHouseSql`: build a readable ClickHouse+ * SELECT from an OpenLITQuery for the widget builder "view generated query"+ * toggle and native built-in execution previews.+ */+export function openLITQueryToClickHouseSql(+ query: Partial<OpenLITQuery> & { includePrevious?: boolean },+ mode: "list" | "aggregate" | "timeseries" = "aggregate"+): string {+ const filterSql = (query.filters || [])+ .map((f) => filterToClickHouseSql(f))+ .filter(Boolean) as string[];+ const whereParts = [+ "Timestamp >= parseDateTimeBestEffort('{{filter.timeLimit.start}}')",+ "Timestamp <= parseDateTimeBestEffort('{{filter.timeLimit.end}}')",+ ...filterSql,+ ];+ if (query.aiSelector !== false) {+ whereParts.push(+ "(ResourceAttributes['telemetry.sdk.name'] = 'openlit' OR notEmpty(SpanAttributes['gen_ai.operation.name']) OR notEmpty(SpanAttributes['gen_ai.request.model']))"+ );+ }+ const where = whereParts.join("\n\t\tAND ");++ if (mode === "list") {+ const limit = Number(query.limit || 100);+ return `SELECT *\nFROM otel_traces\nWHERE ${where}\nORDER BY Timestamp DESC\nLIMIT ${limit}`;+ }++ const aggs = (query.aggregations?.length+ ? query.aggregations+ : [{ fn: "count" as const, as: "count" }]+ ).map((a, i) => {+ const render = resolveAggSql(String(a.fn || "count"));+ const field = a.field ? fieldToClickHouseExpr(a.field) : "";+ return `${render(field)} AS ${safeAlias(a.as, `agg${i}`)}`;+ });++ if (mode === "timeseries") {+ const unit = intervalToTrunc(query.interval);+ return `SELECT+ formatDateTime(DATE_TRUNC('${unit}', Timestamp), '%Y/%m/%d %R') AS request_time,+ ${aggs.join(",\n\t")}+FROM otel_traces+WHERE ${where}+GROUP BY request_time+ORDER BY request_time`;+ }++ const groupBy = query.groupBy || [];+ const groupSelects = groupBy.map(+ (g, i) => `${fieldToClickHouseExpr(g)} AS g${i}`+ );+ const selects = [...groupSelects, ...aggs].join(",\n\t");+ const groupClause = groupBy.length+ ? `\nGROUP BY ${groupBy.map((_, i) => `g${i}`).join(", ")}`+ : "";+ return `SELECT\n\t${selects}\nFROM otel_traces\nWHERE ${where}${groupClause}`;+}++/** Convert SQL-inferred shape into the widget `structuredQuery` config. */+export function inferredToStructuredQuery(inferred: InferredWidgetQuery): {+ mode: InferredWidgetMode;+ query: Record<string, unknown>;+} {+ return {+ mode: inferred.mode,+ query: {+ signal: "traces",+ aiSelector: true,+ aggregations: inferred.aggregations,+ ...(inferred.groupBy?.length ? { groupBy: inferred.groupBy } : {}),+ ...(inferred.mode === "timeseries" ? { interval: "1h" } : {}),+ ...(inferred.includePrevious+ ? { includePrevious: true, rateAlias: inferred.rateAlias || "rate" }+ : {}),+ },+ };+}++function foldModelsPerTime(+ rows: Record<string, unknown>[]+): Record<string, unknown>[] {+ const byTime = new Map<+ string,+ { models: string[]; model_counts: number[]; total: number }+ >();+ for (const row of rows) {+ const request_time = String(+ row.label ?? row.request_time ?? row.bucket ?? ""+ );+ const model = String(+ row.model ??+ row.group_value ??+ row.g0 ??+ row["gen_ai.request.model"] ??+ ""+ );+ const count = Number(row.model_count ?? row.count ?? row.total ?? 0);+ const entry = byTime.get(request_time) || {+ models: [],+ model_counts: [],+ total: 0,+ };+ if (model) {+ entry.models.push(model);+ entry.model_counts.push(Number.isFinite(count) ? count : 0);+ }+ entry.total += Number.isFinite(count) ? count : 0;+ byTime.set(request_time, entry);+ }+ return Array.from(byTime.entries())+ .sort(([a], [b]) => a.localeCompare(b))+ .map(([request_time, entry]) => ({+ request_time,+ models: entry.models,+ model_counts: entry.model_counts,+ total_model_count: entry.total,+ total: entry.total,+ }));+}++function remapTimeseriesRows(+ rows: Record<string, unknown>[],+ inferred: InferredWidgetQuery+): Record<string, unknown>[] {+ const wantsModelsPerTime =+ inferred.primaryAlias === "total_model_count" ||+ (inferred.groupBy || []).includes("gen_ai.request.model");++ if (wantsModelsPerTime) {+ return foldModelsPerTime(rows);+ }++ const primary = safeAlias(inferred.primaryAlias || "total", "total");+ return rows.map((row) => {+ const primaryValue = Number(+ row[primary] ?? row.total ?? row.count ?? row.model_count ?? 0+ );+ const safePrimary = Number.isFinite(primaryValue) ? primaryValue : 0;+ const next: Record<string, unknown> = Object.assign(+ Object.create(null),+ row+ );+ next[primary] = safePrimary;+ next.total = Number(row.total ?? safePrimary);+ next.request_time = String(+ row.label ?? row.request_time ?? row.bucket ?? ""+ );+ next.total_tokens = Number(row.total_tokens ?? row.tokens ?? 0);+ next.prompt_tokens = Number(row.prompt_tokens ?? 0);+ next.completion_tokens = Number(row.completion_tokens ?? 0);+ next.total_cost = Number(row.total_cost ?? row.cost ?? 0);+ return next;+ });+}++/** Execute an inferred widget query via the QueryPlanner. */+export async function executeInferredWidgetQuery(+ adapter: DataSourceAdapter,+ inferred: InferredWidgetQuery,+ filter: MetricParams+): Promise<{ data?: unknown[]; err?: string }> {+ const preferRollup = shouldPreferRollup(filter);++ const buildQuery = (params: MetricParams): OpenLITQuery => {+ const mapped = metricParamsToOpenLITQuery(params, "traces");+ return stripSyntheticDefaultEnvironment({+ ...mapped,+ aggregations: inferred.aggregations,+ groupBy: inferred.groupBy,+ interval:+ inferred.mode === "timeseries"+ ? intervalFromTimeRange(+ mapped.timeRange.start,+ mapped.timeRange.end+ )+ : mapped.interval,+ aiSelector: true,+ });+ };++ try {+ if (inferred.mode === "timeseries") {+ const frame = await planAndSpanTimeSeries(adapter, buildQuery(filter), {+ preferRollup,+ });+ const data = remapTimeseriesRows(+ frame.rows as Record<string, unknown>[],+ inferred+ );+ return { data };+ }++ const currentFrame = await planAndAggregateSpans(+ adapter,+ buildQuery(filter),+ { preferRollup }+ );+ let rows = renameGroupColumns(+ currentFrame.rows as Record<string, unknown>[],+ inferred.groupBy+ );++ // Ensure primary alias is always present for bar/pie widgets.+ rows = rows.map((row) => {+ const primary = safeAlias(inferred.primaryAlias || "count", "count");+ const value = Number(+ row[primary] ?? row.count ?? row.total ?? row.model_count ?? 0+ );+ const next: Record<string, unknown> = Object.assign(+ Object.create(null),+ row+ );+ next[primary] = Number.isFinite(value) ? value : 0;+ return next;+ });++ if (inferred.includePrevious && inferred.previousAlias) {+ const previousFrame = await planAndAggregateSpans(+ adapter,+ buildQuery(getFilterPreviousParams(filter)),+ { preferRollup: false }+ );+ const currentVal = rowNumber(rows[0] as Record<string, unknown>, [+ inferred.primaryAlias,+ "count",+ "total",+ ]);+ const previousVal = rowNumber(+ previousFrame.rows[0] as Record<string, unknown>,+ [inferred.primaryAlias, "count", "total"]+ );+ const rate = percentChange(currentVal, previousVal);+ const primaryKey = safeAlias(inferred.primaryAlias, "count");+ const previousKey = inferred.previousAlias+ ? safeAlias(inferred.previousAlias, "previous")+ : undefined;+ const rateKey = inferred.rateAlias+ ? safeAlias(inferred.rateAlias, "rate")+ : undefined;+ const nextRow: Record<string, unknown> = Object.assign(+ Object.create(null),+ rows[0] || {}+ );+ nextRow[primaryKey] = currentVal;+ if (previousKey) nextRow[previousKey] = previousVal;+ if (rateKey && rate !== null) nextRow[rateKey] = rate;+ rows = [nextRow];+ }++ return { data: rows };+ } catch (err) {+ return {+ err: err instanceof Error ? err.message : String(err),+ };+ }+}diff --git a/src/client/src/lib/platform/manage-dashboard/widget.ts b/src/client/src/lib/platform/manage-dashboard/widget.tsindex c79dad4a..1c4360c1 100644--- a/src/client/src/lib/platform/manage-dashboard/widget.ts+++ b/src/client/src/lib/platform/manage-dashboard/widget.ts@@ -9,6 +9,36 @@ import {escapeSingleQuotes,} from "@/helpers/server/widget";import { jsonStringify } from "@/utils/json";+// NOTE: `@/lib/telemetry-source` is imported lazily inside `runWidgetQuery`+// (see below). `widget.ts` sits in a pre-existing common <-> board <-> widget+// import cycle; importing telemetry-source eagerly here would pull the+// datasource adapter graph (-> clickhouse/adapter -> @/lib/platform/common and+// -> observability) into that cycle's scope-hoisting group and reintroduce a+// "Cannot access X before initialization" TDZ in production builds.+import type {+ DataFrameMeta,+ DataSourceAdapter,+ OpenLITQuery,+ Signal,+} from "@/lib/platform/connectors/datasource/types";+import { UnsupportedCapabilityError } from "@/lib/platform/connectors/datasource/types";+import { logQueryObservability } from "@/lib/platform/connectors/datasource/query-observability";+import {+ clampQueryBudget,+ DEFAULT_QUERY_BUDGET,+} from "@/lib/platform/connectors/datasource/http/limits";+import { metricParamsToOpenLITQuery } from "@/lib/platform/connectors/datasource/clickhouse/query-map";+import {+ executeInferredWidgetQuery,+ inferStructuredFromClickHouseSql,+ isLegacyOtelTracesSql,+ stripSyntheticDefaultEnvironment,+} from "@/lib/platform/manage-dashboard/widget-sql-bridge";+import {+ planAndAggregateSpans,+ planAndSpanTimeSeries,+} from "@/lib/platform/connectors/datasource/query-planner";+import { shouldPreferRollup } from "@/lib/platform/connectors/datasource/rollup-policy";/*** Resolve a dotted path under the filter object (e.g. `timeLimit.start`).@@ -306,8 +336,11 @@ function validateSafeQueryContent(value: string): { valid: boolean; error?: strireturn { valid: false, error: "Query contains disallowed functions" };}+ // Do not match bare `SYSTEM` — attribute keys like `gen_ai.system` are common+ // in OTel SQL. ClickHouse admin commands are `SYSTEM <verb>`; system tables+ // are already blocked by the `system.` check above.const dangerousKeywords =- /\b(DROP|ALTER|TRUNCATE|INSERT|UPDATE|DELETE|CREATE|GRANT|REVOKE|INTO\s+OUTFILE|ATTACH|DETACH|RENAME|OPTIMIZE|SYSTEM)\b/i;+ /\b(DROP|ALTER|TRUNCATE|INSERT|UPDATE|DELETE|CREATE|GRANT|REVOKE|INTO\s+OUTFILE|ATTACH|DETACH|RENAME|OPTIMIZE)\b|\bSYSTEM\s+\w+/i;if (dangerousKeywords.test(scannable)) {return { valid: false, error: "Query contains disallowed operations" };}@@ -337,43 +370,270 @@ function validateFilterValues(value: unknown): { valid: boolean; error?: stringreturn { valid: true };}+/** Run raw ClickHouse SQL for a widget (built-in source path). */+async function runRawClickHouseWidgetQuery(+ query: string,+ filter: MetricParams,+ dbConfigId?: string+) {+ const filterValidation = validateFilterValues(filter);+ if (!filterValidation.valid) {+ return { err: filterValidation.error || "Invalid filter" };+ }++ const exactQuery = renderFilterPlaceholders(query, filter);++ const validation = validateQuery(exactQuery);+ if (!validation.valid) {+ return { err: validation.error || "Invalid query" };+ }++ const { data, err } = await dataCollector(+ { query: exactQuery, enable_readonly: true },+ "query",+ dbConfigId+ );++ if (err) {+ return { err: "Query execution failed" };+ }++ return { data };+}++/** Build an OpenLITQuery time range from the dashboard filter's time limit. */+function timeRangeFromFilter(filter: MetricParams): { start: Date; end: Date } {+ const limit = (filter as { timeLimit?: { start?: unknown; end?: unknown } })+ ?.timeLimit;+ const end = limit?.end ? new Date(limit.end as string) : new Date();+ const start = limit?.start+ ? new Date(limit.start as string)+ : new Date(end.getTime() - 24 * 60 * 60 * 1000);+ return { start, end };+}++/**+ * Execute a structured widget query against an external adapter, dispatching by+ * signal + mode. Returns the DataFrame rows in the flat array shape the widget+ * renderers already consume.+ */+async function executeStructuredWidgetQuery(+ adapter: DataSourceAdapter,+ structured: NonNullable<Widget["config"]["structuredQuery"]>,+ filter: MetricParams+) {+ const base = (structured.query || {}) as Partial<OpenLITQuery>;+ const signal = (base.signal || "traces") as Signal;+ const fromFilter = metricParamsToOpenLITQuery(filter, signal);+ const rawQuery = stripSyntheticDefaultEnvironment({+ ...base,+ signal,+ timeRange: timeRangeFromFilter(filter),+ filters: [...(fromFilter.filters || []), ...(base.filters || [])],+ aiSelector: base.aiSelector ?? fromFilter.aiSelector,+ } as OpenLITQuery);+ // Enforce per-query budgets so a widget can never ask a vendor for an+ // unbounded scan (max rows + max time range + the vendor's lookback window).+ const maxLookbackMs =+ typeof adapter.capabilities === "function"+ ? adapter.capabilities().maxLookbackMs+ : undefined;+ const { query } = clampQueryBudget(rawQuery, {+ ...DEFAULT_QUERY_BUDGET,+ ...(maxLookbackMs !== undefined ? { maxLookbackMs } : {}),+ });++ const mode = structured.mode || "timeseries";+ const observe = (frame: { rows: unknown[]; meta?: DataFrameMeta }) => {+ logQueryObservability(+ {+ sourceType: adapter.type,+ signal,+ mode,+ isBuiltIn: adapter.type === "clickhouse",+ },+ frame.meta,+ frame.rows.length+ );+ return { data: frame.rows };+ };+ try {+ if (signal === "logs") {+ const frame =+ mode === "list"+ ? await adapter.listLogs(query)+ : await adapter.logTimeSeries(query);+ return observe(frame);+ }+ if (signal === "metrics") {+ const frame = await adapter.metricTimeSeries(query);+ return observe(frame);+ }+ // Traces: prefer QueryPlanner so agent/dashboard filters + L1/L2 apply.+ if (mode === "list") {+ const frame = await adapter.listSpans(query);+ return observe(frame);+ }+ const preferRollup = shouldPreferRollup(filter);+ const frame =+ mode === "aggregate"+ ? await planAndAggregateSpans(adapter, query, { preferRollup })+ : await planAndSpanTimeSeries(adapter, query, { preferRollup });+ return observe(frame);+ } catch (e) {+ if (e instanceof UnsupportedCapabilityError) {+ return { err: e.message };+ }+ return { err: getMessage().WIDGET_STRUCTURED_QUERY_FAILED };+ }+}++/**+ * Legacy SQL widgets with no sourceId still hit ClickHouse unless the project+ * traces binding is external — then infer a structured query and run it there.+ *+ * Coding-agent seed widgets are an exception: their SQL depends on ClickHouse+ * session-rollup semantics and must never go through the Tempo/Jaeger bridge+ * (which collapses them to a generic count and invents huge % deltas).+ */+async function runLegacyWidgetQuery(sql: string, filter: MetricParams) {+ if (!isLegacyOtelTracesSql(sql)) {+ return runRawClickHouseWidgetQuery(sql, filter);+ }++ const {+ isCodingAgentClickHouseSql,+ resolveCodingAgentsClickHouseDbConfigId,+ } = await import("@/lib/platform/coding-agents/source");++ if (isCodingAgentClickHouseSql(sql)) {+ const dbConfigId = await resolveCodingAgentsClickHouseDbConfigId({+ environment: filter.environment,+ });+ if (!dbConfigId) {+ return { err: getMessage().CODING_AGENTS_REQUIRES_CLICKHOUSE };+ }+ return runRawClickHouseWidgetQuery(sql, filter, dbConfigId);+ }++ const {+ getTelemetryAdapter,+ resolveTelemetrySourceDescriptor,+ sourceSupportsNativeSql,+ } = await import("@/lib/telemetry-source");++ let descriptor;+ try {+ descriptor = await resolveTelemetrySourceDescriptor({ signal: "traces", environment: filter.environment });+ } catch {+ return runRawClickHouseWidgetQuery(sql, filter);+ }++ if (sourceSupportsNativeSql(descriptor)) {+ return runRawClickHouseWidgetQuery(sql, filter, descriptor.dbConfigId);+ }++ const inferred = inferStructuredFromClickHouseSql(sql);+ if (!inferred) {+ return { err: getMessage().WIDGET_RAW_SQL_SOURCE_ONLY(descriptor.name) };+ }++ const adapter = await getTelemetryAdapter({ signal: "traces", environment: filter.environment });+ return executeInferredWidgetQuery(adapter, inferred, filter);+}+export async function runWidgetQuery(widgetId: string,{userQuery,filter,+ sourceId: sourceIdOverride,+ signal: signalOverride,+ structuredQuery: structuredOverride,}: {userQuery?: string;filter: MetricParams;+ sourceId?: string | null;+ signal?: Signal;+ structuredQuery?: Widget["config"]["structuredQuery"];}-) {+): Promise<{ data?: unknown; err?: string }> {const { data: widget, err: widgetErr } = await getWidgetById(widgetId);if (widgetErr || !widget) {return { err: getMessage().WIDGET_FETCH_FAILED };}- const query = userQuery- ? userQuery- : widget.config?.query || "";-- const filterValidation = validateFilterValues(filter);- if (!filterValidation.valid) {- return { err: filterValidation.error || "Invalid filter" };+ const config = widget.config || {};+ const structured = structuredOverride ?? config.structuredQuery;+ const requestedSql = userQuery || config.query || "";+ const signal = (+ signalOverride ??+ config.signal ??+ structured?.query?.signal ??+ (isLegacyOtelTracesSql(requestedSql) ? "traces" : undefined)+ ) as Signal | undefined;+ // Prefer an explicit override (query-run body / panel config). When unset,+ // signal widgets follow the project's current per-signal binding.+ const sourceId =+ sourceIdOverride !== undefined+ ? sourceIdOverride+ : (config.sourceId ?? null);++ // Legacy trace SQL also follows the current traces binding. Built-in+ // ClickHouse keeps raw SQL; external bindings use the SQL→structured bridge.+ if (!structured && isLegacyOtelTracesSql(requestedSql)) {+ return runLegacyWidgetQuery(requestedSql, filter);}- const exactQuery = renderFilterPlaceholders(query, filter);-- const validation = validateQuery(exactQuery);- if (!validation.valid) {- return { err: validation.error || "Invalid query" };+ // Non-signal legacy widgets retain their raw ClickHouse path.+ if (!sourceId && !signal && !structured) {+ return runLegacyWidgetQuery(+ requestedSql,+ filter+ );}- const { data, err } = await dataCollector({ query: exactQuery, enable_readonly: true });+ // Lazy import breaks the common <-> board <-> widget cycle (see top-of-file+ // note): the datasource graph is only pulled in at call time, off the+ // static concatenation path.+ const {+ getTelemetryAdapter,+ resolveTelemetrySourceDescriptor,+ sourceSupportsNativeSql,+ } = await import("@/lib/telemetry-source");++ const descriptor = await resolveTelemetrySourceDescriptor({ sourceId, signal, environment: filter.environment });++ // Built-in ClickHouse: prefer structured OpenLITQuery when present so the+ // Grafana-style builder works natively; otherwise fall back to raw SQL.+ if (sourceSupportsNativeSql(descriptor)) {+ if (structured && !userQuery) {+ const adapter = await getTelemetryAdapter({ sourceId, signal, environment: filter.environment });+ return executeStructuredWidgetQuery(adapter, structured, filter);+ }+ const sql =+ userQuery ||+ config.query ||+ (structured+ ? (+ await import("./widget-sql-bridge")+ ).openLITQueryToClickHouseSql(+ (structured.query || {}) as any,+ structured.mode || "aggregate"+ )+ : "");+ return runRawClickHouseWidgetQuery(sql, filter, descriptor.dbConfigId);+ }- if (err) {- return { err: "Query execution failed" };+ // External source: raw SQL is not supported; require a structured query.+ if (userQuery || (config.query && !structured)) {+ return { err: getMessage().WIDGET_RAW_SQL_SOURCE_ONLY(descriptor.name) };+ }+ if (!structured) {+ return { err: getMessage().WIDGET_NO_STRUCTURED_QUERY };}- return { data };+ const adapter = await getTelemetryAdapter({ sourceId, signal, environment: filter.environment });+ return executeStructuredWidgetQuery(adapter, structured, filter);}diff --git a/src/client/src/lib/platform/metrics/read.ts b/src/client/src/lib/platform/metrics/read.tsnew file mode 100644index 00000000..5a17262b--- /dev/null+++ b/src/client/src/lib/platform/metrics/read.ts@@ -0,0 +1,210 @@+/**+ * Metrics read facade — the choke point for the Metrics observability page.+ *+ * Every source resolves through the shared signal facade. Point-level results+ * are folded into the grouped list rows the existing page renders.+ */++import type { MetricParams } from "@/lib/platform/common";+import { getSummaryBucket } from "@/lib/platform/observability";+import { metricParamsToOpenLITQuery } from "@/lib/platform/connectors/datasource/clickhouse/query-map";+import { denormalizeMetricPointsToListRows } from "@/lib/platform/connectors/datasource/clickhouse/normalize";+import {+ facadeErrorMessage,+ resolveSignalReadContext,+ rethrowIfSourceFailure,+} from "@/lib/platform/connectors/datasource/facade";+import type { NormalizedMetricPoint } from "@/lib/platform/connectors/datasource/types";++/** List grouped metric series (same shape as `getMetrics`). */+export async function listMetricRecords(params: MetricParams) {+ const { adapter } = await resolveSignalReadContext("metrics", params);++ try {+ const query = metricParamsToOpenLITQuery(params, "metrics");+ const frame = await adapter.listMetricSeries(query);+ const records = denormalizeMetricPointsToListRows(+ frame.rows as NormalizedMetricPoint[]+ );+ const limit = params.limit || 25;+ const offset = params.offset || 0;+ return {+ err: null,+ records: records.slice(offset, offset + limit),+ total: Number(frame.meta?.rowsScanned) || records.length,+ };+ } catch (err) {+ rethrowIfSourceFailure(err);+ return { err: facadeErrorMessage(err) };+ }+}++/** Filter-bar config (services / metricNames / metricTypes). */+export async function getMetricsFilterConfig(params: MetricParams) {+ const { adapter } = await resolveSignalReadContext("metrics", params);++ const emptyRow = {+ services: [] as string[],+ metricNames: [] as string[],+ metricTypes: [] as string[],+ totalRows: 0,+ };+ try {+ const query = metricParamsToOpenLITQuery(params, "metrics");+ const metricNames = await adapter+ .metricNames(query.timeRange)+ .catch(() => [] as string[]);+ let services: string[] = [];+ if (adapter.capabilities().distinctValues) {+ services = await adapter+ .distinctValues("service.name", query)+ .catch(() => [] as string[]);+ // Prometheus self-scrapes often only expose `job` until apps set+ // service_name; fall back so the Services filter is not empty.+ if (!services.length) {+ services = await adapter+ .distinctValues("job", query)+ .catch(() => [] as string[]);+ }+ }+ return {+ err: null,+ data: [+ {+ ...emptyRow,+ services: Array.from(new Set(services.map(String).filter(Boolean))).sort(+ (a, b) => a.localeCompare(b)+ ),+ metricNames: Array.from(+ new Set(metricNames.map(String).filter(Boolean))+ ).sort((a, b) => a.localeCompare(b)),+ },+ ],+ };+ } catch (err) {+ rethrowIfSourceFailure(err);+ return { err: facadeErrorMessage(err), data: [emptyRow] };+ }+}++/** Attribute-key discovery for the custom-filter builder. */+export async function getMetricAttributeKeysRecord(params: MetricParams) {+ const { adapter } = await resolveSignalReadContext("metrics", params);++ const empty = {+ err: null,+ spanAttributeKeys: [] as string[],+ resourceAttributeKeys: [] as string[],+ metricAttributeKeys: [] as string[],+ scopeAttributeKeys: [] as string[],+ };+ try {+ const query = metricParamsToOpenLITQuery(params, "metrics");+ const keys = await adapter.attributeKeys("metrics", query.timeRange);+ // Prometheus labels are shared across metric/resource scopes in the UI+ // filter builder; expose them under both buckets so either dropdown works.+ return {+ ...empty,+ metricAttributeKeys: keys,+ resourceAttributeKeys: keys,+ };+ } catch (err) {+ rethrowIfSourceFailure(err);+ return { ...empty, err: facadeErrorMessage(err) };+ }+}++/** Metric detail (time series + latest raw points). */+export async function getMetricDetailRecord(+ metricName: string,+ metricType?: string,+ serviceName?: string,+ params?: MetricParams+) {+ const { adapter } = await resolveSignalReadContext("metrics", params);++ try {+ const base = metricParamsToOpenLITQuery(+ params || { timeLimit: {} as MetricParams["timeLimit"] },+ "metrics"+ );+ const query = {+ ...base,+ filters: [+ ...(base.filters || []),+ { target: "spanName" as const, op: "in" as const, value: [metricName] },+ ],+ };+ const frame = await adapter.metricTimeSeries(query);+ const points = frame.rows as NormalizedMetricPoint[];+ const series = points+ .map((p) => ({ request_time: p.timestamp, value: p.value }))+ .sort((a, b) => a.request_time.localeCompare(b.request_time));+ const rawPoints = points.map((p) => ({+ MetricName: p.metricName,+ metric_type: metricType || "gauge",+ metric_value: p.value,+ metric_sample_count: 1,+ TimeUnix: p.timestamp,+ MetricUnit: p.unit || "",+ MetricDescription: p.description || "",+ ServiceName: p.serviceName || "",+ Attributes: p.attributes || {},+ ResourceAttributes: p.resourceAttributes || {},+ }));+ return { err: null, series, points: rawPoints };+ } catch (err) {+ rethrowIfSourceFailure(err);+ return { err: facadeErrorMessage(err), series: [], points: [] };+ }+}++/** Metrics summary bar-chart series (same shape as `getSignalSummary(_, "metrics")`). */+export async function getMetricsSummary(params: MetricParams) {+ const { adapter } = await resolveSignalReadContext("metrics", params);++ const bucket = getSummaryBucket(params);+ const empty = { err: null, bucket, buckets: [], total: 0, peak: 0 };+ try {+ const base = metricParamsToOpenLITQuery(params, "metrics");+ // Prefer a server-side count aggregation so Prometheus/Loki-style sources+ // return one series instead of every raw sample (which blows up the chart).+ const query = {+ ...base,+ aggregations: base.aggregations?.length+ ? base.aggregations+ : [{ fn: "count" as const, field: "value" }],+ };+ const frame = await adapter.metricTimeSeries(query);+ const merged = new Map<+ string,+ { count: number; metrics: number; services: number }+ >();+ for (const row of frame.rows as unknown as Record<string, unknown>[]) {+ const label = String(row.label ?? row.timestamp ?? row.request_time ?? "");+ if (!label) continue;+ const count = Number(row.count ?? row.value ?? 0);+ if (!Number.isFinite(count)) continue;+ const prev = merged.get(label) || { count: 0, metrics: 0, services: 0 };+ merged.set(label, {+ count: prev.count + count,+ metrics: prev.metrics + (Number(row.metrics) || 0),+ services: prev.services + (Number(row.services) || 0),+ });+ }+ const buckets = Array.from(merged.entries())+ .sort(([a], [b]) => a.localeCompare(b))+ .map(([label, stats]) => ({+ label,+ count: stats.count,+ metrics: stats.metrics,+ services: stats.services,+ }));+ const total = buckets.reduce((sum, b) => sum + b.count, 0);+ const peak = buckets.reduce((max, b) => Math.max(max, b.count), 0);+ return { err: null, bucket, buckets, total, peak };+ } catch (err) {+ rethrowIfSourceFailure(err);+ return { ...empty, err: facadeErrorMessage(err) };+ }+}diff --git a/src/client/src/lib/platform/observability-deprecation.ts b/src/client/src/lib/platform/observability-deprecation.tsnew file mode 100644index 00000000..eea28036--- /dev/null+++ b/src/client/src/lib/platform/observability-deprecation.ts@@ -0,0 +1,31 @@+/**+ * Compatibility helpers for legacy `/api/observability/*` twins.+ * Prefer `/api/telemetry/*`. These routes keep `observability.read` auth while+ * marking responses deprecated toward the telemetry successors.+ */++export function deprecatedObservabilityResponse(+ response: Response,+ successorPath: string+): Response {+ const headers = new Headers(response.headers);+ headers.set("Deprecation", "true");+ headers.set("Link", `<${successorPath}>; rel="successor-version"`);+ headers.set("X-OpenLIT-Deprecated-Alias", "observability→telemetry");+ return new Response(response.body, {+ status: response.status,+ statusText: response.statusText,+ headers,+ });+}++export async function jsonWithObservabilityDeprecation(+ body: unknown,+ successorPath: string,+ init?: ResponseInit+): Promise<Response> {+ return deprecatedObservabilityResponse(+ Response.json(body, init),+ successorPath+ );+}diff --git a/src/client/src/lib/platform/observability.ts b/src/client/src/lib/platform/observability.tsindex 61a0cb69..da7ac65f 100644--- a/src/client/src/lib/platform/observability.ts+++ b/src/client/src/lib/platform/observability.ts@@ -57,7 +57,7 @@ function escapeClickHouseString(value: string) {return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'");}-function getSummaryBucket(params: MetricParams) {+export function getSummaryBucket(params: MetricParams) {const start = new Date(params.timeLimit.start as Date | string);const end = new Date(params.timeLimit.end as Date | string);const days = Math.max(1, (end.getTime() - start.getTime()) / 86400000);@@ -118,8 +118,27 @@ function buildWhere(params: MetricParams, table: FilterTable) {}const selected = params.selectedConfig || {};- if (selected.services?.length) {- where.push(`ServiceName IN (${inList(selected.services)})`);+ const serviceNames = Array.from(+ new Set([+ ...(selected.services || []),+ ...(selected.serviceNames || []),+ ...(selected.applicationNames || []),+ ].map(String).filter(Boolean))+ );+ if (serviceNames.length) {+ where.push(`ServiceName IN (${inList(serviceNames)})`);+ }+ const environments = (selected.environments || [])+ .map(String)+ .filter(Boolean);+ // Synthetic OpenLIT "default" means unspecified — don't hard-filter.+ if (+ environments.length &&+ !(environments.length === 1 && environments[0] === "default")+ ) {+ where.push(+ `ResourceAttributes['deployment.environment'] IN (${inList(environments)})`+ );}if (table === "logs" && selected.severities?.length) {const severities = selected.severities.map((severity: string) => severity.toLowerCase());@@ -225,7 +244,7 @@ export async function getLogs(params: MetricParams) {const { data: countData, err: countErr } = await dataCollector({ query: countQuery }, "query", params.databaseConfigId);if (countErr) return { err: countErr };- const orderBy = params.sorting?.type && params.sorting?.direction+ const orderBy = params.sorting?.type? `${params.sorting.type.replace(/[^A-Za-z0-9_.]/g, "")} ${params.sorting.direction}`: "Timestamp desc";const query = `@@ -258,7 +277,7 @@ export async function getSignalSummary(const traceParams: MetricParams = {...params,...(signal === "exceptions"- ? { statusCode: ["STATUS_CODE_ERROR", "Error"] }+ ? { statusCode: ["STATUS_CODE_ERROR", "Error", "ERROR"] }: {}),};const where = getFilterWhereCondition(traceParams, true);@@ -314,7 +333,7 @@ export async function getSignalSummary(};}-export async function getLogByRowId(rowId: string, dbConfigId?: string) {+export async function getLogByRowId(rowId: string, databaseConfigId?: string) {const safeRowId = rowId.replace(/[^0-9]/g, "");const query = `SELECT@@ -324,7 +343,7 @@ export async function getLogByRowId(rowId: string, dbConfigId?: string) {WHERE cityHash64(toString(Timestamp), TraceId, SpanId, SeverityText, Body) = ${safeRowId || "0"}LIMIT 1`;- const { data, err } = await dataCollector({ query }, "query", dbConfigId);+ const { data, err } = await dataCollector({ query }, "query", databaseConfigId);return { err, record: (data as any[])?.[0] };}@@ -423,6 +442,7 @@ export async function getMetricDetail(metricName: string, metricType?: string, s...(serviceName ? { services: [serviceName] } : {}),};const effectiveParams: MetricParams = {+ ...params,timeLimit: params?.timeLimit || {type: "24H",start: new Date(Date.now() - 24 * 60 * 60 * 1000),@@ -454,8 +474,8 @@ export async function getMetricDetail(metricName: string, metricType?: string, sLIMIT 100`;const [series, points] = await Promise.all([- dataCollector({ query: seriesQuery }, "query", params?.databaseConfigId),- dataCollector({ query: pointsQuery }, "query", params?.databaseConfigId),+ dataCollector({ query: seriesQuery }, "query", effectiveParams.databaseConfigId),+ dataCollector({ query: pointsQuery }, "query", effectiveParams.databaseConfigId),]);return {err: series.err || points.err,diff --git a/src/client/src/lib/platform/openplait/features.ts b/src/client/src/lib/platform/openplait/features.tsnew file mode 100644index 00000000..ecac3641--- /dev/null+++ b/src/client/src/lib/platform/openplait/features.ts@@ -0,0 +1,35 @@+/**+ * ClickHouse-backed OpenLIT feature families covered by the shared OpenPlait+ * read boundary. New ClickHouse read features are covered automatically when+ * they use `dataCollector`; this catalog makes that architectural promise+ * explicit and testable.+ */+export const OPENPLAIT_CLICKHOUSE_READ_FEATURES = [+ "traces",+ "logs",+ "metrics",+ "telemetry",+ "openground",+ "rule-engine",+ "evaluations",+ "dashboards",+ "alerts",+ "prompts",+ "models-and-pricing",+ "management-data",+] as const;++export type OpenPlaitClickHouseReadFeature =+ (typeof OPENPLAIT_CLICKHOUSE_READ_FEATURES)[number];++/**+ * OpenLIT-owned intelligence/materialization reads intentionally stay on the+ * native ClickHouse client. They consume internal state rather than a routed+ * traces/logs/metrics datasource contract.+ */+export const DIRECT_INTELLIGENCE_READ_FEATURES = [+ "otter",+ "ai-analysis",+ "coding-agents",+ "telemetry-rollups",+] as const;diff --git a/src/client/src/lib/platform/openplait/frames.ts b/src/client/src/lib/platform/openplait/frames.tsnew file mode 100644index 00000000..2d00ecd3--- /dev/null+++ b/src/client/src/lib/platform/openplait/frames.ts@@ -0,0 +1,16 @@+import type { DataFrame } from "@openplait/core";++/** Convert OpenPlait's columnar frames to the row shape legacy OpenLIT reads use. */+export function openPlaitFramesToRows(+ frames: readonly DataFrame[]+): Record<string, unknown>[] {+ const rows: Record<string, unknown>[] = [];+ for (const frame of frames) {+ for (let index = 0; index < frame.length; index += 1) {+ const row: Record<string, unknown> = {};+ for (const field of frame.fields) row[field.name] = field.values[index];+ rows.push(row);+ }+ }+ return rows;+}diff --git a/src/client/src/lib/platform/openplait/index.ts b/src/client/src/lib/platform/openplait/index.tsnew file mode 100644index 00000000..befcf524--- /dev/null+++ b/src/client/src/lib/platform/openplait/index.ts@@ -0,0 +1,275 @@+import { createHash, randomUUID } from "node:crypto";+import type { DatabaseConfig } from "@prisma/client";+import {+ OPENPLAIT_API_VERSION,+ type NativeQuery,+} from "@openplait/core";+import {+ ClickHouseAdapter,+ OPENLIT_CLICKHOUSE_DATASETS,+ type ClickHouseAdapterConfig,+} from "@openplait/adapter-clickhouse";+import { DatasourceRegistry, OpenPlaitRuntime } from "@openplait/runtime";+import { constructURL, parseQueryStringToObject } from "@/utils/parser";++export {+ DIRECT_INTELLIGENCE_READ_FEATURES,+ OPENPLAIT_CLICKHOUSE_READ_FEATURES,+ type OpenPlaitClickHouseReadFeature,+} from "./features";+export { openPlaitFramesToRows } from "./frames";+import { openPlaitFramesToRows } from "./frames";+export { normalizeOpenPlaitReadStatement } from "./native";+import { normalizeOpenPlaitReadStatement } from "./native";++const DATASOURCE_KIND = "ClickHouseDatasource";+const DEFAULT_TIMEOUT_MS = 60_000;+const DEFAULT_MAX_RESULT_ROWS = 100_000;+const DEFAULT_MAX_ROWS_TO_READ = 10_000_000;++interface RuntimeEntry {+ fingerprint: string;+ adapter: ClickHouseAdapter;+ runtime: OpenPlaitRuntime;+ datasourceName: string;+}++const runtimeEntries = new Map<string, RuntimeEntry>();++export interface OpenPlaitClickHouseConnection {+ host: string;+ port: string;+ database: string;+ username: string;+ password?: string | null;+ query?: string | null;+}++function positiveInteger(value: string | undefined, fallback: number): number {+ const parsed = Number(value);+ return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;+}++function configFingerprint(dbConfig: DatabaseConfig): string {+ // Cache-bust key for connection settings — not a password hash.+ // Password rotations bump `updatedAt`, so we never digest the secret itself.+ return createHash("sha256")+ .update(+ JSON.stringify({+ host: dbConfig.host,+ port: dbConfig.port,+ database: dbConfig.database,+ username: dbConfig.username,+ query: dbConfig.query || "",+ updatedAt:+ dbConfig.updatedAt instanceof Date+ ? dbConfig.updatedAt.toISOString()+ : String(dbConfig.updatedAt || ""),+ })+ )+ .digest("hex");+}++function datasourceName(dbConfig: DatabaseConfig): string {+ return `openlit-clickhouse-${dbConfig.id}`;+}++function connectionConfig(+ connection: OpenPlaitClickHouseConnection+): ClickHouseAdapterConfig {+ return {+ url: constructURL(connection.host, connection.port),+ username: connection.username,+ password: connection.password || "",+ database: connection.database,+ httpHeaders: parseQueryStringToObject(connection.query || ""),+ applicationName: "openlit-openplait",+ allowNativeQueries: true,+ requireTimeRange: false,+ queryTimeoutMs: positiveInteger(+ process.env.OPENPLAIT_CLICKHOUSE_QUERY_TIMEOUT_MS,+ DEFAULT_TIMEOUT_MS+ ),+ maxResultRows: positiveInteger(+ process.env.OPENPLAIT_CLICKHOUSE_MAX_RESULT_ROWS,+ DEFAULT_MAX_RESULT_ROWS+ ),+ maxRowsToRead: positiveInteger(+ process.env.OPENPLAIT_CLICKHOUSE_MAX_ROWS_TO_READ,+ DEFAULT_MAX_ROWS_TO_READ+ ),+ datasets: [...OPENLIT_CLICKHOUSE_DATASETS],+ };+}++async function runtimeFor(dbConfig: DatabaseConfig): Promise<RuntimeEntry> {+ const fingerprint = configFingerprint(dbConfig);+ const current = runtimeEntries.get(dbConfig.id);+ if (current?.fingerprint === fingerprint) return current;+ if (current) await current.adapter.close();++ const adapter = new ClickHouseAdapter(connectionConfig(dbConfig));+ const name = datasourceName(dbConfig);+ const registry = new DatasourceRegistry().register({+ name,+ kind: DATASOURCE_KIND,+ scope: "workspace",+ config: {+ url: constructURL(dbConfig.host, dbConfig.port),+ username: dbConfig.username,+ password: dbConfig.password || "",+ database: dbConfig.database,+ httpHeaders: parseQueryStringToObject(dbConfig.query || ""),+ allowNativeQueries: true,+ },+ adapter,+ });+ const entry = {+ fingerprint,+ adapter,+ runtime: new OpenPlaitRuntime(registry, {+ defaultTimeoutMs: positiveInteger(+ process.env.OPENPLAIT_CLICKHOUSE_QUERY_TIMEOUT_MS,+ DEFAULT_TIMEOUT_MS+ ),+ }),+ datasourceName: name,+ };+ runtimeEntries.set(dbConfig.id, entry);+ return entry;+}++function deepestErrorMessage(error: unknown): string {+ let current = error;+ let message = error instanceof Error ? error.message : String(error);+ for (let index = 0; index < 5 && current instanceof Error; index += 1) {+ message = current.message || message;+ current = current.cause;+ }+ return message;+}++function isTransientConnectionError(error: unknown): boolean {+ let current: unknown = error;+ for (let index = 0; index < 8 && current; index += 1) {+ const message =+ current instanceof Error ? current.message : String(current || "");+ const code =+ typeof current === "object" && current && "code" in current+ ? String((current as { code?: unknown }).code || "")+ : "";+ if (+ /ECONNRESET|ECONNREFUSED|ETIMEDOUT|EPIPE|UND_ERR_SOCKET|socket hang up|connection (?:closed|reset)/i.test(+ `${code} ${message}`+ )+ ) {+ return true;+ }+ current = current instanceof Error ? current.cause : undefined;+ }+ return false;+}++async function evictRuntime(+ dbConfigId: string,+ expected: RuntimeEntry+): Promise<void> {+ if (runtimeEntries.get(dbConfigId) !== expected) return;+ runtimeEntries.delete(dbConfigId);+ await expected.adapter.close();+}++/** Construct and validate a ClickHouse datasource through OpenPlait before persistence. */+export async function validateOpenPlaitClickHouseConnection(+ connection: OpenPlaitClickHouseConnection+): Promise<void> {+ const adapter = new ClickHouseAdapter(connectionConfig(connection));+ const name = `openlit-connector-validation-${randomUUID()}`;+ const registry = new DatasourceRegistry().register({+ name,+ kind: DATASOURCE_KIND,+ scope: "workspace",+ config: connectionConfig(connection),+ adapter,+ });+ const runtime = new OpenPlaitRuntime(registry, {+ defaultTimeoutMs: positiveInteger(+ process.env.OPENPLAIT_CLICKHOUSE_QUERY_TIMEOUT_MS,+ DEFAULT_TIMEOUT_MS+ ),+ });+ const resource: NativeQuery = {+ apiVersion: OPENPLAIT_API_VERSION,+ kind: "Query",+ metadata: { name: "openlit-connector-validation" },+ spec: {+ mode: "native",+ datasource: { kind: DATASOURCE_KIND, name, scope: "workspace" },+ native: { language: "sql", statement: "SELECT 1 AS openplait_connection_ok" },+ },+ };++ try {+ await runtime.execute({+ queries: [resource],+ audit: { requestId: `openlit:connector-validation:${randomUUID()}` },+ });+ } catch (error) {+ throw new Error(+ `OpenPlait ClickHouse validation failed: ${deepestErrorMessage(error)}`,+ { cause: error }+ );+ } finally {+ await adapter.close();+ }+}++/** Execute a project-scoped, read-only ClickHouse query through OpenPlait. */+export async function executeOpenPlaitRead({+ query,+ dbConfig,+}: {+ query: string;+ dbConfig: DatabaseConfig;+}): Promise<Record<string, unknown>[]> {+ const statement = normalizeOpenPlaitReadStatement(query);+ const execute = async (entry: RuntimeEntry) => {+ const resource: NativeQuery = {+ apiVersion: OPENPLAIT_API_VERSION,+ kind: "Query",+ metadata: { name: "openlit-read" },+ spec: {+ mode: "native",+ datasource: {+ kind: DATASOURCE_KIND,+ name: entry.datasourceName,+ scope: "workspace",+ },+ native: { language: "sql", statement },+ },+ };+ const response = await entry.runtime.execute({+ queries: [resource],+ audit: { requestId: `openlit:${dbConfig.id}:${randomUUID()}` },+ });+ return openPlaitFramesToRows(response.result.frames);+ };++ const first = await runtimeFor(dbConfig);+ try {+ return await execute(first);+ } catch (error) {+ if (!isTransientConnectionError(error)) throw error;+ // @clickhouse/client connections are pooled inside the cached adapter.+ // Recreate that adapter once when the peer resets a stale socket.+ await evictRuntime(dbConfig.id, first);+ return execute(await runtimeFor(dbConfig));+ }+}++/** Test/server-shutdown hook for clients owned by cached OpenPlait adapters. */+export async function closeOpenPlaitRuntimes(): Promise<void> {+ const entries = Array.from(runtimeEntries.values());+ runtimeEntries.clear();+ await Promise.all(entries.map(({ adapter }) => adapter.close()));+}diff --git a/src/client/src/lib/platform/openplait/native.ts b/src/client/src/lib/platform/openplait/native.tsnew file mode 100644index 00000000..10269b7a--- /dev/null+++ b/src/client/src/lib/platform/openplait/native.ts@@ -0,0 +1,4 @@+/** Remove one optional trailing SQL terminator; OpenPlait owns the format. */+export function normalizeOpenPlaitReadStatement(query: string): string {+ return query.trim().replace(/;\s*$/, "");+}diff --git a/src/client/src/lib/platform/pricing/index.ts b/src/client/src/lib/platform/pricing/index.tsindex d140ae18..6614ccc1 100644--- a/src/client/src/lib/platform/pricing/index.ts+++ b/src/client/src/lib/platform/pricing/index.ts@@ -8,7 +8,7 @@ import {} from "@/helpers/server/trace";import { SUPPORTED_EVALUATION_OPERATIONS } from "@/constants/traces";import { getDBConfigByIdInternal } from "@/lib/db-config";-import { getRequestViaSpanId } from "@/lib/platform/request";+import { getTraceSpanRecord } from "@/lib/platform/traces/read";import { ProviderRegistry } from "@/lib/platform/providers/provider-registry";import { getPricingConfigById } from "./config";import { getLastRunCronLogByCronId, insertCronLog } from "@/lib/platform/cron-log";@@ -16,6 +16,7 @@ import { CronRunStatus, CronType } from "@/types/cron";import { differenceInSeconds } from "date-fns";import Sanitizer from "@/utils/sanitizer";import asaw from "@/utils/asaw";+import { consoleLog } from "@/utils/log";import {getChatModelCostPerM,promptTokensIncludeCache,@@ -166,21 +167,39 @@ async function writeCostToTrace(* Manually recalculate + persist cost for a single span.* Exposed via POST /api/pricing/[spanId].*/-export async function setPricingForSpanId(spanId: string) {+export async function setPricingForSpanId(+ spanId: string,+ opts?: { environment?: string; traceId?: string }+) {const user = await getCurrentUser();throwIfError(!user, getMessage().UNAUTHORIZED_USER);const sanitizedSpanId = Sanitizer.sanitizeValue(spanId);- const { record: spanData, err: traceErr } = await getRequestViaSpanId(- sanitizedSpanId+ const { resolveTelemetrySourceDescriptor } = await import("@/lib/telemetry-source");+ const descriptor = await resolveTelemetrySourceDescriptor({+ signal: "traces",+ environment: opts?.environment,+ });+ const { record: spanData, err: traceErr } = await getTraceSpanRecord(+ sanitizedSpanId,+ { environment: opts?.environment, traceId: opts?.traceId });+ consoleLog("[pricing] routed span lookup", {+ spanId: sanitizedSpanId,+ environment: opts?.environment || null,+ traceId: opts?.traceId || null,+ descriptorId: descriptor.id,+ type: descriptor.type,+ found: !!(spanData as any)?.SpanId,+ error: traceErr || null,+ });throwIfError(!!traceErr, getMessage().TRACE_NOT_FOUND);throwIfError(!(spanData as any)?.SpanId,getMessage().TRACE_NOT_FOUND);- const trace = spanData as TraceRow;+ const trace = spanData as unknown as TraceRow;// The trace itself doesn't tell us the dbConfig; fall back to the defaultconst { default: prisma } = await import("@/lib/prisma");@@ -207,6 +226,17 @@ export async function setPricingForSpanId(spanId: string) {};}+ // External telemetry backends are read-only from OpenLIT's perspective.+ // The cost is still calculated using the project's ClickHouse intelligence+ // store, but must not be written to the external vendor's trace backend.+ if (!descriptor.isBuiltIn && descriptor.type !== "clickhouse") {+ return {+ success: true,+ data: { spanId: trace.SpanId, cost, persisted: false },+ message: `Cost calculated from the ${descriptor.type} connector. External telemetry sources are read-only, so the source span was not modified.`,+ };+ }+const { err } = await writeCostToTrace(trace.SpanId, cost, dbConfigId);if (err) return { success: false, err };@@ -218,34 +248,65 @@ interface AutoPricingPayload {cronId: string;}-/**- * Auto pricing: fetch traces in the window since last cron run and- * recompute + persist cost for each. Called by the /api/pricing/auto cron.- */-export async function autoUpdatePricing(payload: AutoPricingPayload) {- const startedAt = new Date();- const cronLogObject = {- cronId: payload.cronId,- cronType: CronType.SPAN_PRICING,- metaProperties: { ...payload },- startedAt,- };-- const pricingConfig = await getPricingConfigById(payload.pricingConfigId);- if (!pricingConfig) {- return { success: false, err: "Pricing config not found" };- }-- const [dbConfigErr, dbConfig] = await asaw(- getDBConfigByIdInternal({ id: pricingConfig.databaseConfigId })- );-- if (dbConfigErr || !dbConfig?.id) {- return { success: false, err: getMessage().DATABASE_CONFIG_NOT_FOUND };+async function fetchAutoPricingCandidateSpans({+ databaseConfigId,+ lastRunTime,+}: {+ databaseConfigId: string;+ lastRunTime?: string | Date | null;+}): Promise<{ traces: TraceRow[]; err?: string; persistCosts: boolean }> {+ const lastRunIso =+ lastRunTime instanceof Date+ ? lastRunTime.toISOString()+ : lastRunTime || null;++ try {+ const { getTelemetryAdapterForDbConfig } = await import(+ "@/lib/telemetry-source"+ );+ const { denormalizeSpanToTraceRow } = await import(+ "@/lib/platform/connectors/datasource/clickhouse/normalize"+ );+ const resolved = await getTelemetryAdapterForDbConfig(+ databaseConfigId,+ "traces"+ );+ if (!resolved.isBuiltIn && resolved.descriptor.type !== "clickhouse") {+ const end = new Date();+ const start = lastRunIso+ ? new Date(lastRunIso)+ : new Date(end.getTime() - 24 * 60 * 60 * 1000);+ const frame = await resolved.adapter.listSpans({+ signal: "traces",+ timeRange: { start, end },+ aiSelector: true,+ limit: 500,+ filters: [+ {+ target: "attribute",+ scope: "span",+ key: TYPE_KEY,+ op: "in",+ value: [...SUPPORTED_EVALUATION_OPERATIONS],+ },+ ],+ });+ const traces = frame.rows+ .map((span) => denormalizeSpanToTraceRow(span) as unknown as TraceRow)+ .filter((trace) => {+ const existing = Number(getAttr(trace, COST_KEY) || 0);+ return !Number.isFinite(existing) || existing === 0;+ });+ return { traces, persistCosts: false };+ }+ } catch (err) {+ return {+ traces: [],+ persistCosts: false,+ err: String((err as Error)?.message || err),+ };}- const lastRunTime = await getLastRunCronLogByCronId(payload.cronId);-// Only LLM-type spans with tokens recorded. Crucially, we skip// any span that already carries a `gen_ai.usage.cost` from the// instrumentation/hook. Vendor-emitted cost is authoritative@@ -266,14 +327,57 @@ export async function autoUpdatePricing(payload: AutoPricingPayload) {WHERE ${typeKeyPath} IN (${operationList})AND (${costKeyPath} = '' OR toFloat64OrZero(${costKeyPath}) = 0)${- lastRunTime- ? `AND Timestamp >= parseDateTimeBestEffort('${lastRunTime}')`+ lastRunIso+ ? `AND Timestamp >= parseDateTimeBestEffort('${lastRunIso}')`: ""}ORDER BY Timestamp`;- const { data, err } = await dataCollector({ query }, "query", dbConfig.id);+ const { data, err } = await dataCollector(+ { query },+ "query",+ databaseConfigId+ );+ if (err) return { traces: [], persistCosts: true, err: String(err) };+ return { traces: (data as TraceRow[]) || [], persistCosts: true };+}++/**+ * Auto pricing: fetch traces in the window since last cron run and+ * recompute + persist cost for each. Called by the /api/pricing/auto cron.+ */+export async function autoUpdatePricing(payload: AutoPricingPayload) {+ const startedAt = new Date();+ const cronLogObject = {+ cronId: payload.cronId,+ cronType: CronType.SPAN_PRICING,+ metaProperties: { ...payload },+ startedAt,+ };++ const pricingConfig = await getPricingConfigById(payload.pricingConfigId);+ if (!pricingConfig) {+ return { success: false, err: "Pricing config not found" };+ }++ const [dbConfigErr, dbConfig] = await asaw(+ getDBConfigByIdInternal({ id: pricingConfig.databaseConfigId })+ );++ if (dbConfigErr || !dbConfig?.id) {+ return { success: false, err: getMessage().DATABASE_CONFIG_NOT_FOUND };+ }++ const lastRunTime = await getLastRunCronLogByCronId(+ payload.cronId,+ dbConfig.id+ );++ const { traces, err, persistCosts } = await fetchAutoPricingCandidateSpans({+ databaseConfigId: dbConfig.id,+ lastRunTime,+ });if (err) {const finishedAt = new Date();@@ -292,7 +396,6 @@ export async function autoUpdatePricing(payload: AutoPricingPayload) {return { success: false, err: err as string };}- const traces = (data as TraceRow[]) || [];let errorCount = 0;let updatedCount = 0;const errorObject: Record<string, string> = {};@@ -305,6 +408,9 @@ export async function autoUpdatePricing(payload: AutoPricingPayload) {// Skip traces we can't price (no counting as error)continue;}+ if (!persistCosts) {+ continue;+ }const { err: writeErr } = await writeCostToTrace(trace.SpanId,cost,diff --git a/src/client/src/lib/platform/request/index.ts b/src/client/src/lib/platform/request/index.tsindex 7d69c05a..be1969f1 100644--- a/src/client/src/lib/platform/request/index.ts+++ b/src/client/src/lib/platform/request/index.ts@@ -82,7 +82,7 @@ export async function getRequestPerTime(params: MetricParams) {request_time;`;- return dataCollector({ query });+ return dataCollector({ query }, "query", params.databaseConfigId);}export async function getTotalRequests(params: MetricParams) {@@ -111,7 +111,7 @@ export async function getTotalRequests(params: MetricParams) {current_data.start_date = previous_day.start_date;`;- return dataCollector({ query });+ return dataCollector({ query }, "query", params.databaseConfigId);}export async function getAverageRequestDuration(params: MetricParams) {@@ -144,7 +144,7 @@ export async function getAverageRequestDuration(params: MetricParams) {current_data.start_date = previous_day.start_date;`;- return dataCollector({ query });+ return dataCollector({ query }, "query", params.databaseConfigId);}export async function getRequestsConfig(params: MetricParams) {@@ -260,7 +260,11 @@ export async function getRequests(params: MetricParams) {LIMIT ${limit}OFFSET ${offset}`;- const { data, err } = await dataCollector({ query }, "query", params.databaseConfigId);+ const { data, err } = await dataCollector(+ { query },+ "query",+ params.databaseConfigId+ );return {err,records: data,@@ -268,32 +272,32 @@ export async function getRequests(params: MetricParams) {};}-export async function getRequestViaSpanId(spanId: string, dbConfigId?: string) {+export async function getRequestViaSpanId(spanId: string, databaseConfigId?: string) {const safeSpanId = escapeClickHouseString(String(spanId ?? ""));const query = `SELECT * FROM ${OTEL_TRACES_TABLE_NAME}WHERE SpanId='${safeSpanId}'`;- const { data, err } = await dataCollector({ query }, "query", dbConfigId);+ const { data, err } = await dataCollector({ query }, "query", databaseConfigId);return {err,record: (data as unknown[])?.[0],};}-export async function getRequestViaTraceId(traceId: string, dbConfigId?: string) {+export async function getRequestViaTraceId(traceId: string, databaseConfigId?: string) {const safeTraceId = escapeClickHouseString(String(traceId ?? ""));const query = `SELECT * FROM ${OTEL_TRACES_TABLE_NAME} WHERE ${getTraceMappingKeyFullPath("id")}='${safeTraceId}'`;- const { data, err } = await dataCollector({ query }, "query", dbConfigId);+ const { data, err } = await dataCollector({ query }, "query", databaseConfigId);return {err,record: (data as unknown[])?.[0],};}-export async function getHeirarchyViaSpanId(spanId: string, dbConfigId?: string) {+export async function getHeirarchyViaSpanId(spanId: string, databaseConfigId?: string) {// Step 1: resolve the source span. We need:// - TraceId (the usual "show every span in the trace" path)// - coding_agent.session.id (so coding-agent sessions whose CLI@@ -320,7 +324,7 @@ export async function getHeirarchyViaSpanId(spanId: string, dbConfigId?: string)const { data: sourceData, err: sourceErr } = await dataCollector({ query: sourceSpanQuery },"query",- dbConfigId+ databaseConfigId);if (sourceErr || !Array.isArray(sourceData) || sourceData.length === 0) {@@ -415,7 +419,7 @@ export async function getHeirarchyViaSpanId(spanId: string, dbConfigId?: string)const { data: allSpansRaw, err: allSpansErr } = await dataCollector({ query: allSpansQuery },"query",- dbConfigId+ databaseConfigId);const allSpans = Array.isArray(allSpansRaw)@@ -540,9 +544,9 @@ function buildCodingSessionHierarchy(spans: any[], sessionId: string) {return root;}-export async function getRequestExist() {+export async function getRequestExist(databaseConfigId?: string) {const query = `SELECT COUNT(*) AS total_requests FROM ${OTEL_TRACES_TABLE_NAME}`;- return dataCollector({ query });+ return dataCollector({ query }, "query", databaseConfigId);}export async function getAttributeKeys(params: MetricParams) {@@ -563,8 +567,12 @@ export async function getAttributeKeys(params: MetricParams) {`;const [spanResult, resourceResult] = await Promise.all([- dataCollector({ query: spanKeysQuery }),- dataCollector({ query: resourceKeysQuery }),+ dataCollector({ query: spanKeysQuery }, "query", params.databaseConfigId),+ dataCollector(+ { query: resourceKeysQuery },+ "query",+ params.databaseConfigId+ ),]);return {@@ -594,5 +602,5 @@ export async function getGroupedRequests(params: MetricParams, groupBy: string)GROUP BY group_valueORDER BY count DESC`;- return dataCollector({ query });+ return dataCollector({ query }, "query", params.databaseConfigId);}diff --git a/src/client/src/lib/platform/rule-engine/index.ts b/src/client/src/lib/platform/rule-engine/index.tsindex 55136e35..d22e0b79 100644--- a/src/client/src/lib/platform/rule-engine/index.ts+++ b/src/client/src/lib/platform/rule-engine/index.ts@@ -23,6 +23,7 @@ import { emitManagementAlertSignalSafe } from "@/lib/platform/alerts/signals";type RuleMutationOptions = {emitAlert?: boolean;+ databaseConfigId?: string;};function shouldEmitAlert(options?: RuleMutationOptions) {@@ -109,7 +110,11 @@ export async function createRule(ruleInputParams: Partial<RuleInput>, options?:('${ruleId}', '${ruleInput.name}', '${ruleInput.description || ""}', '${ruleInput.group_operator || "AND"}', '${ruleInput.status || "ACTIVE"}', '${user!.email}');`;- const { err } = await dataCollector({ query: insertQuery }, "exec");+ const { err } = await dataCollector(+ { query: insertQuery },+ "exec",+ options?.databaseConfigId+ );throwIfError(!!err,@@ -161,7 +166,11 @@ export async function updateRule(id: string, ruleInputParams: Partial<RuleInput>UPDATE ${updateValues.filter(Boolean).join(", ")}WHERE id = '${safeId}'`;- const { err, data } = await dataCollector({ query: updateQuery }, "exec");+ const { err, data } = await dataCollector(+ { query: updateQuery },+ "exec",+ options?.databaseConfigId+ );throwIfError(!!(err || !(data as { query_id: unknown })?.query_id),@@ -202,19 +211,23 @@ export async function deleteRule(id: string, options?: RuleMutationOptions) {const [ruleResult, groupsResult, conditionsResult, entitiesResult] = await Promise.all([dataCollector({ query: `DELETE FROM ${OPENLIT_RULES_TABLE_NAME} WHERE id = '${safeId}';` },- "exec"+ "exec",+ options?.databaseConfigId),dataCollector({ query: `DELETE FROM ${OPENLIT_RULE_CONDITION_GROUPS_TABLE_NAME} WHERE rule_id = '${safeId}';` },- "exec"+ "exec",+ options?.databaseConfigId),dataCollector({ query: `DELETE FROM ${OPENLIT_RULE_CONDITIONS_TABLE_NAME} WHERE rule_id = '${safeId}';` },- "exec"+ "exec",+ options?.databaseConfigId),dataCollector({ query: `DELETE FROM ${OPENLIT_RULE_ENTITIES_TABLE_NAME} WHERE rule_id = '${safeId}';` },- "exec"+ "exec",+ options?.databaseConfigId),]);@@ -259,11 +272,13 @@ export async function addConditionGroupsToRule(await Promise.all([dataCollector({ query: `DELETE FROM ${OPENLIT_RULE_CONDITION_GROUPS_TABLE_NAME} WHERE rule_id = '${safeRuleId}';` },- "exec"+ "exec",+ options?.databaseConfigId),dataCollector({ query: `DELETE FROM ${OPENLIT_RULE_CONDITIONS_TABLE_NAME} WHERE rule_id = '${safeRuleId}';` },- "exec"+ "exec",+ options?.databaseConfigId),]);@@ -280,7 +295,11 @@ export async function addConditionGroupsToRule(('${groupId}', '${safeRuleId}', '${sanitizedGroup.condition_operator || "AND"}');`;- const { err: groupErr } = await dataCollector({ query: insertGroupQuery }, "exec");+ const { err: groupErr } = await dataCollector(+ { query: insertGroupQuery },+ "exec",+ options?.databaseConfigId+ );throwIfError(!!groupErr,typeof groupErr?.toString === "function"@@ -305,7 +324,11 @@ export async function addConditionGroupsToRule(${conditionValues};`;- const { err: condErr } = await dataCollector({ query: insertConditionsQuery }, "exec");+ const { err: condErr } = await dataCollector(+ { query: insertConditionsQuery },+ "exec",+ options?.databaseConfigId+ );throwIfError(!!condErr,typeof condErr?.toString === "function"@@ -355,7 +378,8 @@ export async function addRuleEntity(entityInputParams: Partial<RuleEntityInput>,},],},- "insert"+ "insert",+ options?.databaseConfigId);throwIfError(@@ -395,7 +419,8 @@ export async function deleteRuleEntity(id: string, options?: RuleMutationOptionsconst { err } = await dataCollector({ query: `DELETE FROM ${OPENLIT_RULE_ENTITIES_TABLE_NAME} WHERE id = '${safeId}';` },- "exec"+ "exec",+ options?.databaseConfigId);if (err) {@@ -425,7 +450,7 @@ export async function getRuleEntities(filters: {entity_type?: string;entity_id?: string;id?: string;-} = {}) {+} = {}, databaseConfigId?: string) {const user = await getCurrentUser();throwIfError(!user, getMessage().UNAUTHORIZED_USER);@@ -449,5 +474,5 @@ export async function getRuleEntities(filters: {ORDER BY created_at DESC;`;- return await dataCollector({ query }, "query");+ return await dataCollector({ query }, "query", databaseConfigId);}diff --git a/src/client/src/lib/platform/rule-engine/source.ts b/src/client/src/lib/platform/rule-engine/source.tsnew file mode 100644index 00000000..69520494--- /dev/null+++ b/src/client/src/lib/platform/rule-engine/source.ts@@ -0,0 +1,47 @@+import {+ getRequestEnvironment,+ OPENLIT_CONTEXT_HEADERS,+} from "@/constants/openlit-context";+import { getDBConfigByUser } from "@/lib/db-config";+import { resolveSignalSource } from "@/lib/telemetry-source";++/** Resolve the ClickHouse that owns rule-engine state for this request. */+export async function resolveRuleEngineDatabaseConfigId(+ request: Request+): Promise<string> {+ const requestedId = request.headers.get(+ OPENLIT_CONTEXT_HEADERS.databaseConfigId+ );+ if (requestedId) {+ // Never trust the header by itself. getDBConfigByUser is already scoped to+ // the authenticated user's current project, which prevents cross-project+ // connector IDs from becoming a Rule Engine IDOR.+ const available = await getDBConfigByUser();+ const selected = Array.isArray(available)+ ? available.find((database) => database.id === requestedId)+ : undefined;+ if (!selected?.id) {+ throw new Error(+ "The selected ClickHouse datasource is not available in the current project."+ );+ }+ return selected.id;+ }++ const resolution = await resolveSignalSource("intelligence", {+ environment: getRequestEnvironment(request),+ });+ const { descriptor } = resolution;++ if (+ !resolution.hasSource ||+ descriptor.type !== "clickhouse" ||+ !descriptor.dbConfigId+ ) {+ throw new Error(+ "Rule Engine requires a ClickHouse datasource for the selected environment."+ );+ }++ return descriptor.dbConfigId;+}diff --git a/src/client/src/lib/platform/rule-engine/telemetry.ts b/src/client/src/lib/platform/rule-engine/telemetry.tsnew file mode 100644index 00000000..4e064616--- /dev/null+++ b/src/client/src/lib/platform/rule-engine/telemetry.ts@@ -0,0 +1,71 @@+import type { MetricParams } from "../common";+import { listTraceRecords } from "../traces/read";++/**+ * Rule-engine telemetry reads must use the same signal router as Telemetry.+ * This keeps rule previews and field discovery working when traces are stored+ * in Tempo, Datadog, Jaeger, or another configured connector.+ */+export async function listRecentRuleTraces(limit = 100, environment?: string) {+ const end = new Date();+ // Rule authoring needs a representative sample even when telemetry is not+ // continuously ingested (local/dev projects commonly have week-long gaps).+ // Keep the query bounded, but use a 30-day window rather than silently+ // returning no field values or preview candidates after seven days.+ const start = new Date(end.getTime() - 30 * 24 * 60 * 60 * 1000);+ const params: MetricParams = {+ // Keep timestamps in the same ISO-8601 form used by telemetry API+ // payloads. Interpolating Date objects produces locale text such as+ // "Thu Jul 30 ... (India Standard Time)", which ClickHouse's+ // parseDateTimeBestEffort cannot parse.+ timeLimit: {+ type: "CUSTOM",+ start: start.toISOString(),+ end: end.toISOString(),+ },+ limit,+ offset: 0,+ sorting: { type: "Timestamp", direction: "desc" },+ environment,+ };+ const result = await listTraceRecords(params);+ if (result.err) throw new Error(String(result.err));+ return (result.records || []) as Record<string, any>[];+}++export function getRuleTraceFieldValue(+ trace: Record<string, any>,+ field: string+): string {+ const attributeFields: Record<string, ["SpanAttributes" | "ResourceAttributes", string]> = {+ "service.name": ["ResourceAttributes", "service.name"],+ "gen_ai.system": ["SpanAttributes", "gen_ai.system"],+ "gen_ai.request.model": ["SpanAttributes", "gen_ai.request.model"],+ "gen_ai.usage.input_tokens": ["SpanAttributes", "gen_ai.usage.input_tokens"],+ "gen_ai.usage.output_tokens": ["SpanAttributes", "gen_ai.usage.output_tokens"],+ "gen_ai.usage.total_cost": ["SpanAttributes", "gen_ai.usage.total_cost"],+ "gen_ai.request.temperature": ["SpanAttributes", "gen_ai.request.temperature"],+ };+ if (field === "deployment.environment") {+ return String(+ trace.ResourceAttributes?.["deployment.environment"] ??+ trace.SpanAttributes?.["deployment.environment"] ??+ trace.SpanAttributes?.["gen_ai.environment"] ??+ ""+ );+ }+ const attribute = attributeFields[field];+ if (attribute) {+ const [scope, key] = attribute;+ return String(trace[scope]?.[key] ?? "");+ }++ const directFields: Record<string, string> = {+ ServiceName: "ServiceName",+ SpanName: "SpanName",+ SpanKind: "SpanKind",+ Duration: "Duration",+ StatusCode: "StatusCode",+ };+ return String(trace[directFields[field] || field] ?? "");+}diff --git a/src/client/src/lib/platform/telemetry/rollups.ts b/src/client/src/lib/platform/telemetry/rollups.tsnew file mode 100644index 00000000..ea97136f--- /dev/null+++ b/src/client/src/lib/platform/telemetry/rollups.ts@@ -0,0 +1,560 @@+/**+ * L2 telemetry rollups — OpenLIT-owned derived tables for dashboard KPIs.+ *+ * Background materialization pulls bounded samples via any DataSourceAdapter+ * and writes constant-latency aggregates. QueryPlanner prefers these when+ * fresh; detail/list still hit the live vendor (or the recent-span hot cache).+ *+ * Retention: rollups TTL 90d; hot cache TTL 2h + row cap. Never a full mirror+ * of vendor o11y data.+ */++import type {+ DataFrame,+ DataSourceAdapter,+ NormalizedFilter,+ NormalizedSpan,+ OpenLITQuery,+} from "@/lib/platform/connectors/datasource/types";+import {+ collapseToRootSpans,+ fetchSpansForAggregation,+} from "@/lib/platform/connectors/datasource/graph/sample-fetch";+import {+ aggregateSpansInProcess,+ bucketSpansByInterval,+} from "@/lib/platform/connectors/datasource/graph/sample-aggregate";++export const SIGNAL_BUCKETS_TABLE = "openlit_signal_buckets";+export const LLM_ROLLUPS_TABLE = "openlit_llm_rollups";+export const SPAN_HOT_CACHE_TABLE = "openlit_external_span_cache";++/** Max age for a rollup row to be considered fresh enough for L2 reads. */+export const ROLLUP_FRESHNESS_MS = 5 * 60 * 1000;+/** Soft row cap for the recent-span hot cache per source. */+export const SPAN_HOT_CACHE_MAX_ROWS = 2_000;+/** One background pull powers every derived rollup for an external source. */+const MATERIALIZATION_SAMPLE_TRACE_CAP = 50;++function escape(value: string): string {+ return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'");+}++function escapeJson(value: unknown): string {+ return escape(JSON.stringify(value ?? {}));+}++async function collector(+ query: { query: string },+ type: "query" | "exec",+ dbConfigId?: string+) {+ const { intelligenceDataCollector } = await import("@/lib/platform/common");+ return intelligenceDataCollector(query, type, dbConfigId);+}++async function logError(event: string, meta: Record<string, unknown>) {+ try {+ const { agentsLogger } = await import("@/lib/platform/agents/logger");+ agentsLogger.error(event, meta);+ } catch {+ // ignore+ }+}++function filterValues(+ filters: NormalizedFilter[] | undefined,+ key: string+): string[] {+ const out: string[] = [];+ for (const f of filters || []) {+ if (f.target !== "attribute" || f.key !== key) continue;+ if (Array.isArray(f.value)) {+ out.push(...f.value.map(String).filter(Boolean));+ } else if (f.value !== undefined && f.value !== "") {+ out.push(String(f.value));+ }+ }+ return Array.from(new Set(out));+}++function inListSql(column: string, values: string[]): string {+ if (!values.length) return "";+ return `AND ${column} IN (${values.map((v) => `'${escape(v)}'`).join(", ")})`;+}++function isFresh(updatedAt: unknown): boolean {+ const t = new Date(String(updatedAt || 0)).getTime();+ return Number.isFinite(t) && Date.now() - t <= ROLLUP_FRESHNESS_MS;+}++/**+ * Read time-bucket rollups for spanTimeSeries-shaped queries.+ * Returns null when no fresh rows cover the window.+ */+export async function readSignalBucketRollup(+ query: OpenLITQuery,+ opts?: { sourceId?: string; dbConfigId?: string }+): Promise<DataFrame | null> {+ const sourceId = opts?.sourceId || "";+ const start = query.timeRange.start.toISOString();+ const end = query.timeRange.end.toISOString();+ const as = query.aggregations?.[0]?.as || "count";+ const services = filterValues(query.filters, "service.name");+ const environments = filterValues(query.filters, "deployment.environment");+ const sql = `+ SELECT+ bucket_start AS bucket,+ bucket_start AS label,+ bucket_start AS request_time,+ sum(request_count) AS ${as},+ sum(request_count) AS count,+ sum(request_count) AS total,+ avg(avg_duration_seconds) AS avgDuration,+ sum(total_cost) AS cost,+ sum(total_tokens) AS tokens,+ max(updated_at) AS updated_at+ FROM ${SIGNAL_BUCKETS_TABLE}+ WHERE source_id = '${escape(sourceId)}'+ AND bucket_start >= parseDateTimeBestEffort('${escape(start)}')+ AND bucket_start <= parseDateTimeBestEffort('${escape(end)}')+ ${inListSql("service", services)}+ ${inListSql("environment", environments)}+ GROUP BY bucket_start+ ORDER BY bucket_start ASC+ `;+ try {+ const { data, err } = await collector(+ { query: sql },+ "query",+ opts?.dbConfigId+ );+ if (err || !Array.isArray(data) || data.length === 0) return null;+ const rows = data as Record<string, unknown>[];+ const newest = rows.reduce((max, r) => {+ const t = new Date(String(r.updated_at || 0)).getTime();+ return Number.isFinite(t) ? Math.max(max, t) : max;+ }, 0);+ if (!newest || !isFresh(newest)) return null;+ return {+ fields: [],+ rows: rows.map(({ updated_at: _u, ...rest }) => rest),+ meta: { degraded: ["rollup"], freshness: "accelerated" },+ };+ } catch {+ return null;+ }+}++/**+ * Read LLM groupBy rollups (model/provider/category/service/environment).+ * Scoped service/env filters are applied against multi-dimensional columns.+ */+export async function readLlmRollup(+ query: OpenLITQuery,+ opts?: { sourceId?: string; dbConfigId?: string; dimension?: string }+): Promise<DataFrame | null> {+ const sourceId = opts?.sourceId || "";+ const dimension = opts?.dimension || query.groupBy?.[0] || "model";+ const start = query.timeRange.start.toISOString();+ const end = query.timeRange.end.toISOString();+ const services = filterValues(query.filters, "service.name");+ const environments = filterValues(query.filters, "deployment.environment");+ const sql = `+ SELECT+ group_value AS g0,+ group_value AS group_value,+ sum(request_count) AS count,+ sum(total_cost) AS total_cost,+ sum(total_tokens) AS total_tokens,+ avg(avg_duration_seconds) AS avg_duration_seconds,+ max(updated_at) AS updated_at+ FROM ${LLM_ROLLUPS_TABLE}+ WHERE source_id = '${escape(sourceId)}'+ AND dimension = '${escape(dimension)}'+ AND window_start >= parseDateTimeBestEffort('${escape(start)}')+ AND window_end <= parseDateTimeBestEffort('${escape(end)}')+ ${inListSql("service", services)}+ ${inListSql("environment", environments)}+ GROUP BY group_value+ ORDER BY count DESC+ LIMIT ${Number(query.limit || 100)}+ `;+ try {+ const { data, err } = await collector(+ { query: sql },+ "query",+ opts?.dbConfigId+ );+ if (err || !Array.isArray(data) || data.length === 0) return null;+ const rows = data as Record<string, unknown>[];+ const newest = rows.reduce((max, r) => {+ const t = new Date(String(r.updated_at || 0)).getTime();+ return Number.isFinite(t) ? Math.max(max, t) : max;+ }, 0);+ if (!newest || !isFresh(newest)) return null;+ return {+ fields: [],+ rows: rows.map(({ updated_at: _u, ...rest }) => rest),+ meta: { degraded: ["rollup"], freshness: "accelerated" },+ };+ } catch {+ return null;+ }+}++function parseAttrs(raw: unknown): Record<string, string> {+ if (raw && typeof raw === "object" && !Array.isArray(raw)) {+ return Object.fromEntries(+ Object.entries(raw as Record<string, unknown>).map(([k, v]) => [+ k,+ String(v ?? ""),+ ])+ );+ }+ if (typeof raw !== "string" || !raw) return {};+ try {+ const parsed = JSON.parse(raw) as Record<string, unknown>;+ return Object.fromEntries(+ Object.entries(parsed).map(([k, v]) => [k, String(v ?? "")])+ );+ } catch {+ return {};+ }+}++function rowToSpan(row: Record<string, unknown>): NormalizedSpan {+ return {+ traceId: String(row.trace_id || ""),+ spanId: String(row.span_id || ""),+ parentSpanId: String(row.parent_span_id || ""),+ name: String(row.name || ""),+ serviceName: String(row.service_name || ""),+ timestamp: String(row.timestamp || ""),+ durationNs: Number(row.duration_ns || 0),+ statusCode: String(row.status_code || ""),+ statusMessage: String(row.status_message || "") || undefined,+ spanKind: String(row.span_kind || "") || undefined,+ spanAttributes: parseAttrs(row.span_attributes),+ resourceAttributes: parseAttrs(row.resource_attributes),+ cost: Number(row.cost || 0) || undefined,+ };+}++/**+ * Read bounded recent root spans from the hot cache (L2 list path).+ */+export async function readSpanHotCache(+ query: OpenLITQuery,+ opts: { sourceId: string; dbConfigId?: string; maxRows?: number }+): Promise<{ spans: NormalizedSpan[]; truncated: boolean } | null> {+ const start = query.timeRange.start.toISOString();+ const end = query.timeRange.end.toISOString();+ const services = filterValues(query.filters, "service.name");+ const environments = filterValues(query.filters, "deployment.environment");+ const limit = Math.min(+ opts.maxRows ?? query.limit ?? 100,+ SPAN_HOT_CACHE_MAX_ROWS+ );+ const sql = `+ SELECT+ trace_id, span_id, parent_span_id, name, service_name, environment,+ timestamp, duration_ns, status_code, status_message, span_kind,+ span_attributes, resource_attributes, cost, updated_at+ FROM ${SPAN_HOT_CACHE_TABLE}+ WHERE source_id = '${escape(opts.sourceId)}'+ AND timestamp >= parseDateTimeBestEffort('${escape(start)}')+ AND timestamp <= parseDateTimeBestEffort('${escape(end)}')+ ${inListSql("service_name", services)}+ ${inListSql("environment", environments)}+ ORDER BY timestamp DESC+ LIMIT ${limit + 1}+ `;+ try {+ const { data, err } = await collector(+ { query: sql },+ "query",+ opts.dbConfigId+ );+ if (err || !Array.isArray(data) || data.length === 0) return null;+ const rows = data as Record<string, unknown>[];+ const newest = rows.reduce((max, r) => {+ const t = new Date(String(r.updated_at || 0)).getTime();+ return Number.isFinite(t) ? Math.max(max, t) : max;+ }, 0);+ if (!newest || !isFresh(newest)) return null;+ const truncated = rows.length > limit;+ return {+ spans: rows.slice(0, limit).map(rowToSpan),+ truncated,+ };+ } catch {+ return null;+ }+}++/**+ * Upsert stratified list roots into the hot cache (called from background sync).+ */+export async function writeSpanHotCache(opts: {+ sourceId: string;+ dbConfigId?: string;+ spans: NormalizedSpan[];+}): Promise<number> {+ const spans = opts.spans.slice(0, SPAN_HOT_CACHE_MAX_ROWS);+ if (!spans.length) return 0;+ const values = spans.map((span) => {+ const env =+ span.resourceAttributes?.["deployment.environment"] ||+ span.spanAttributes?.["deployment.environment"] ||+ "";+ const ts = span.timestamp+ ? new Date(span.timestamp).toISOString()+ : new Date().toISOString();+ return `(+ '${escape(opts.sourceId)}',+ '${escape(span.traceId)}',+ '${escape(span.spanId)}',+ '${escape(span.parentSpanId || "")}',+ '${escape(span.name || "")}',+ '${escape(span.serviceName || "")}',+ '${escape(env)}',+ parseDateTimeBestEffort('${escape(ts)}'),+ ${Number(span.durationNs || 0)},+ '${escape(span.statusCode || "")}',+ '${escape(span.statusMessage || "")}',+ '${escape(span.spanKind || "")}',+ '${escapeJson(span.spanAttributes)}',+ '${escapeJson(span.resourceAttributes)}',+ ${Number(span.cost || 0)},+ now()+ )`;+ });+ await collector(+ {+ query: `+ INSERT INTO ${SPAN_HOT_CACHE_TABLE}+ (source_id, trace_id, span_id, parent_span_id, name, service_name,+ environment, timestamp, duration_ns, status_code, status_message,+ span_kind, span_attributes, resource_attributes, cost, updated_at)+ VALUES ${values.join(",")}+ `,+ },+ "exec",+ opts.dbConfigId+ );+ return values.length;+}++/**+ * Materialize rollups + hot cache for one traces adapter into the app store.+ * Safe to call from cron; uses L1 sample compute under the hood.+ */+export async function materializeTelemetryRollups(opts: {+ adapter: DataSourceAdapter;+ sourceId: string;+ dbConfigId?: string;+ windowHours?: number;+}): Promise<{ buckets: number; llmRows: number; hotCacheRows: number }> {+ const end = new Date();+ const hours = opts.windowHours ?? 24;+ const start = new Date(end.getTime() - hours * 60 * 60 * 1000);+ const baseQuery: OpenLITQuery = {+ signal: "traces",+ timeRange: { start, end },+ aiSelector: true,+ interval: "1h",+ limit: 100,+ };++ let buckets = 0;+ let llmRows = 0;+ let hotCacheRows = 0;+ let traceRoots: NormalizedSpan[];+ try {+ const sample = await fetchSpansForAggregation(opts.adapter, baseQuery, {+ maxTraces: MATERIALIZATION_SAMPLE_TRACE_CAP,+ });+ traceRoots = collapseToRootSpans(sample.spans);+ } catch (err) {+ // Fail once for this materialization cycle. Re-running the same rejected+ // Tempo search for every dimension creates a retry storm and prolongs+ // upstream throttling.+ await logError("telemetry_materialization_sample_failed", { err });+ return { buckets, llmRows, hotCacheRows };+ }++ const discoveredServices =+ typeof opts.adapter.discoverServices === "function"+ ? (+ await opts.adapter.discoverServices({ start, end }).catch(() => [])+ )+ .map((s) => s.serviceName)+ .filter(Boolean)+ .slice(0, 24)+ : [];+ const sampledServices = traceRoots+ .map((span) => span.serviceName || span.resourceAttributes["service.name"])+ .filter(Boolean);+ const services = Array.from(+ new Set([...discoveredServices, ...sampledServices])+ ).slice(0, 24);+ const serviceScopes = services.length ? services : [""];++ try {+ for (const service of serviceScopes) {+ const serviceSpans = service+ ? traceRoots.filter(+ (span) =>+ (span.serviceName || span.resourceAttributes["service.name"]) ===+ service+ )+ : traceRoots;+ const series = bucketSpansByInterval(+ serviceSpans,+ baseQuery.interval || "1h",+ [+ { fn: "count", as: "count" },+ { fn: "avg", field: "duration", as: "avgDuration" },+ { fn: "sum", field: "gen_ai.usage.cost", as: "cost" },+ { fn: "sum", field: "gen_ai.usage.total_tokens", as: "tokens" },+ ],+ baseQuery.timeRange+ );+ const values = (series.rows as Record<string, unknown>[])+ .map((row) => {+ const bucket = String(+ row.bucket || row.label || row.request_time || ""+ );+ if (!bucket) return null;+ return `(+ '${escape(opts.sourceId)}',+ '${escape(service)}',+ '',+ parseDateTimeBestEffort('${escape(bucket)}'),+ ${Number(row.count || 0)},+ ${Number(row.avgDuration || 0)},+ ${Number(row.cost || 0)},+ ${Number(row.tokens || 0)},+ now()+ )`;+ })+ .filter(Boolean);+ if (values.length) {+ await collector(+ {+ query: `+ INSERT INTO ${SIGNAL_BUCKETS_TABLE}+ (source_id, service, environment, bucket_start, request_count,+ avg_duration_seconds, total_cost, total_tokens, updated_at)+ VALUES ${values.join(",")}+ `,+ },+ "exec",+ opts.dbConfigId+ );+ buckets += values.length;+ }+ }+ } catch (err) {+ await logError("telemetry_rollup_buckets_failed", { err });+ }++ const dimensions: { dimension: string; field: string }[] = [+ { dimension: "model", field: "gen_ai.request.model" },+ { dimension: "provider", field: "gen_ai.system" },+ { dimension: "category", field: "gen_ai.operation.name" },+ { dimension: "service", field: "service.name" },+ { dimension: "environment", field: "deployment.environment" },+ ];++ for (const service of serviceScopes) {+ const serviceSpans = service+ ? traceRoots.filter(+ (span) =>+ (span.serviceName || span.resourceAttributes["service.name"]) ===+ service+ )+ : traceRoots;+ for (const { dimension, field } of dimensions) {+ try {+ const frame = aggregateSpansInProcess(+ serviceSpans,+ [field],+ [+ { fn: "count", as: "count" },+ { fn: "sum", field: "gen_ai.usage.cost", as: "total_cost" },+ {+ fn: "sum",+ field: "gen_ai.usage.total_tokens",+ as: "total_tokens",+ },+ { fn: "avg", field: "duration", as: "avg_duration_seconds" },+ ]+ );+ const values = (frame.rows as Record<string, unknown>[])+ .map((row) => {+ const groupValue = String(+ row.group_value ?? row.g0 ?? row[field] ?? ""+ );+ if (!groupValue) return null;+ const model =+ dimension === "model"+ ? groupValue+ : String(row.model || row["gen_ai.request.model"] || "");+ const provider =+ dimension === "provider"+ ? groupValue+ : String(row.provider || row["gen_ai.system"] || "");+ return `(+ '${escape(opts.sourceId)}',+ '${escape(dimension)}',+ '${escape(groupValue)}',+ '${escape(service)}',+ '',+ '${escape(model)}',+ '${escape(provider)}',+ parseDateTimeBestEffort('${escape(start.toISOString())}'),+ parseDateTimeBestEffort('${escape(end.toISOString())}'),+ ${Number(row.count || 0)},+ ${Number(row.total_cost || 0)},+ ${Number(row.total_tokens || 0)},+ ${Number(row.avg_duration_seconds || 0)},+ now()+ )`;+ })+ .filter(Boolean);+ if (values.length) {+ await collector(+ {+ query: `+ INSERT INTO ${LLM_ROLLUPS_TABLE}+ (source_id, dimension, group_value, service, environment, model, provider,+ window_start, window_end, request_count, total_cost, total_tokens,+ avg_duration_seconds, updated_at)+ VALUES ${values.join(",")}+ `,+ },+ "exec",+ opts.dbConfigId+ );+ llmRows += values.length;+ }+ } catch (err) {+ await logError("telemetry_rollup_llm_failed", { err, dimension, service });+ }+ }+ }++ try {+ hotCacheRows = await writeSpanHotCache({+ sourceId: opts.sourceId,+ dbConfigId: opts.dbConfigId,+ spans: traceRoots.slice(0, SPAN_HOT_CACHE_MAX_ROWS),+ });+ } catch (err) {+ await logError("telemetry_hot_cache_failed", { err });+ }++ return { buckets, llmRows, hotCacheRows };+}diff --git a/src/client/src/lib/platform/traces/read.ts b/src/client/src/lib/platform/traces/read.tsnew file mode 100644index 00000000..5765fffb--- /dev/null+++ b/src/client/src/lib/platform/traces/read.ts@@ -0,0 +1,637 @@+/**+ * Traces read facade — the choke point for Telemetry list/detail/hierarchy.+ *+ * Every source, including built-in ClickHouse, resolves through the shared+ * signal facade and executes the DataSourceAdapter contract.+ */++import type { MetricParams } from "@/lib/platform/common";+import { getSummaryBucket } from "@/lib/platform/observability";+import { buildHierarchy } from "@/helpers/server/trace";+import { getFilterPreviousParams } from "@/helpers/server/platform";+import { metricParamsToOpenLITQuery } from "@/lib/platform/connectors/datasource/clickhouse/query-map";+import { denormalizeSpanToTraceRow } from "@/lib/platform/connectors/datasource/clickhouse/normalize";+import type {+ NormalizedSpan,+ OpenLITQuery,+} from "@/lib/platform/connectors/datasource/types";+import { UnsupportedCapabilityError } from "@/lib/platform/connectors/datasource/types";+import {+ intervalFromTimeRange,+ planAndAggregateSpans,+ planAndSpanTimeSeries,+} from "@/lib/platform/connectors/datasource/query-planner";+import {+ resolveSignalReadContext,+ rethrowIfSourceFailure,+} from "@/lib/platform/connectors/datasource/facade";+import getMessage from "@/constants/messages";+import { consoleLog } from "@/utils/log";+import { AdapterError } from "@openplait/adapter-sdk";+import { pickRootSpan } from "@/lib/platform/connectors/datasource/graph/sample-fetch";+import { normalizeOtlpId } from "@/lib/platform/connectors/datasource/otlp-json";++async function resolveTracesAdapter(sourceId?: string, environment?: string) {+ const { adapter, descriptor } = await resolveSignalReadContext("traces", {+ sourceId,+ environment,+ });+ consoleLog("[traces] resolved read adapter", {+ sourceId: sourceId || null,+ environment: environment || null,+ descriptorId: descriptor.id,+ type: descriptor.type,+ isBuiltIn: descriptor.isBuiltIn,+ });+ return { adapter, descriptor };+}++/** Drop the synthetic ClickHouse "default" environment when querying Tempo. */+function externalTraceQuery(params: MetricParams, opts?: { aiSelector?: boolean }) {+ const query = metricParamsToOpenLITQuery(params, "traces", opts);+ const filters = query.filters?.filter((filter) => {+ if (filter.target !== "attribute" || filter.key !== "deployment.environment") {+ return true;+ }+ const values = Array.isArray(filter.value) ? filter.value : [filter.value];+ return !(values.length === 1 && String(values[0]) === "default");+ });+ return { ...query, filters: filters?.length ? filters : undefined };+}++function asErrorMessage(err: unknown): string {+ if (err instanceof UnsupportedCapabilityError) return err.message;+ if (err instanceof AdapterError) {+ const body = err.details?.body;+ if (typeof body === "string" && body.trim()) {+ // Tempo's response body contains the actionable parser/range reason;+ // auth headers are never included in OpenPlait error details.+ return `${err.message} ${body.trim().slice(0, 500)}`;+ }+ }+ if (err instanceof Error) return err.message;+ return typeof err === "string" ? err : getMessage().WIDGET_RUN_FAILED;+}++/** List spans for the Telemetry table (same shape as `getRequests`). */+export async function listTraceRecords(params: MetricParams) {+ const { adapter, descriptor } = await resolveTracesAdapter(params.sourceId, params.environment);++ try {+ const query = externalTraceQuery(params, { aiSelector: false });+ // Interactive lists always query the selected adapter directly. Sampling+ // caches are reserved for aggregate/intelligence computation.+ // Run list + count in parallel: Tempo/Jaeger count paths are often+ // cheaper (search summaries) and must not wait on full-trace downloads.+ const countPromise = adapter.countTraces+ ? adapter.countTraces(query).catch(() => null)+ : Promise.resolve(null);+ const [frame, count] = await Promise.all([+ adapter.listSpans(query),+ countPromise,+ ]);+ const spans = frame.rows || [];+ const truncated =+ !!frame.meta?.truncated || spans.length >= (params.limit || 25);+ const records = spans.map((row) => denormalizeSpanToTraceRow(row));+ let total = truncated+ ? records.length + (params.offset || 0) + 1+ : records.length + (params.offset || 0);+ let totalIsSampled = true;+ if (count) {+ total = count.total;+ totalIsSampled = count.truncated;+ } else if (typeof frame.meta?.rowsScanned === "number") {+ total = frame.meta.rowsScanned;+ totalIsSampled = false;+ }+ consoleLog("[traces] external list result", {+ descriptorId: descriptor.id,+ type: descriptor.type,+ spanCount: spans.length,+ recordCount: records.length,+ truncated,+ total,+ totalIsSampled,+ });+ return {+ err: null,+ records,+ total,+ freshness: totalIsSampled ? ("sampled" as const) : ("live" as const),+ };+ } catch (err) {+ rethrowIfSourceFailure(err);+ return { err: asErrorMessage(err) };+ }+}++/**+ * Tempo list rows reuse `traceId` as `spanId` because search summaries have no+ * cheap real span id. Treat that sentinel as "open the trace root".+ */+function findSpanInTrace(+ spans: NormalizedSpan[],+ spanId: string,+ traceId: string+): NormalizedSpan | null {+ const wantedSpan = normalizeOtlpId(spanId) || spanId;+ const wantedTrace = normalizeOtlpId(traceId) || traceId;+ const exact =+ spans.find((span) => (normalizeOtlpId(span.spanId) || span.spanId) === wantedSpan) ||+ null;+ if (exact) return exact;+ if (wantedSpan && wantedSpan === wantedTrace) {+ return pickRootSpan(spans) || spans[0] || null;+ }+ return null;+}++/** Single span by id (same shape as `getRequestViaSpanId`). */+export async function getTraceSpanRecord(+ spanId: string,+ opts?: { traceId?: string; environment?: string }+) {+ const startedAt = Date.now();+ consoleLog("[traces] span detail start", {+ spanId,+ traceId: opts?.traceId || null,+ environment: opts?.environment || null,+ });+ const { adapter, descriptor } = await resolveTracesAdapter(+ undefined,+ opts?.environment+ );+ try {+ // Grafana Explore already knows the TraceId from search metadata. Fetch+ // that trace first; a span-id-only TraceQL search is eventually+ // consistent and can intermittently return an empty result immediately+ // after the list request.+ let span: NormalizedSpan | null = null;+ if (opts?.traceId) {+ consoleLog("[traces] span detail fetching trace", {+ descriptorId: descriptor.id,+ traceId: opts.traceId,+ spanId,+ });+ const spans = await adapter.getTraceSpans(opts.traceId);+ consoleLog("[traces] span detail trace fetched", {+ descriptorId: descriptor.id,+ traceId: opts.traceId,+ requestedSpanId: spanId,+ spanCount: spans.length,+ matched: spans.some((s) => s.spanId === spanId),+ });+ span = findSpanInTrace(spans, spanId, opts.traceId);+ if (!span) {+ return {+ err: "Span not found in the selected trace and telemetry source",+ record: undefined,+ };+ }+ }+ if (!opts?.traceId) span = await adapter.getSpan(spanId);+ if (!span) {+ consoleLog("[traces] span not found", {+ descriptorId: descriptor.id,+ type: descriptor.type,+ spanId,+ traceId: opts?.traceId || null,+ elapsedMs: Date.now() - startedAt,+ });+ return { err: "Span not found in the selected telemetry source", record: undefined };+ }+ consoleLog("[traces] span detail success", {+ descriptorId: descriptor.id,+ spanId,+ traceId: span.traceId,+ elapsedMs: Date.now() - startedAt,+ });+ return { err: null, record: denormalizeSpanToTraceRow(span) };+ } catch (err) {+ rethrowIfSourceFailure(err);+ consoleLog("[traces] span detail failed", {+ descriptorId: descriptor.id,+ spanId,+ traceId: opts?.traceId || null,+ elapsedMs: Date.now() - startedAt,+ error: asErrorMessage(err),+ });+ return { err: asErrorMessage(err), record: undefined };+ }+}++/** First span for a trace id (same shape as `getRequestViaTraceId`). */+export async function getTraceRecordByTraceId(traceId: string, environment?: string) {+ const { adapter } = await resolveTracesAdapter(undefined, environment);++ try {+ const spans = await adapter.getTraceSpans(traceId);+ const first = spans[0];+ if (!first) return { err: null, record: undefined };+ return { err: null, record: denormalizeSpanToTraceRow(first) };+ } catch (err) {+ rethrowIfSourceFailure(err);+ return { err: asErrorMessage(err), record: undefined };+ }+}++/**+ * Trace hierarchy for the detail tree. External sources get a ParentSpanId+ * tree from `getTraceSpans` (and session spans when the adapter supports it).+ * Coding-agent multi-trace folding stays ClickHouse-native for now.+ */+export async function getTraceHierarchy(+ spanId: string,+ opts?: { traceId?: string; environment?: string }+) {+ const { adapter } = await resolveTracesAdapter(undefined, opts?.environment);++ try {+ let span: NormalizedSpan | null = null;+ if (opts?.traceId) {+ const spans = await adapter.getTraceSpans(opts.traceId);+ span =+ spans.find((s) => s.spanId === spanId) ||+ null;+ if (!span) {+ return {+ err: "Span not found in the selected trace and telemetry source",+ record: {},+ };+ }+ }+ if (!opts?.traceId) span = await adapter.getSpan(spanId);+ if (!span) return { err: "Span not found", record: {} };++ let spans = await adapter.getTraceSpans(span.traceId);+ const sessionId =+ span.spanAttributes["coding_agent.session.id"] ||+ span.resourceAttributes["coding_agent.session.id"];+ if (sessionId && adapter.capabilities().crossTraceSession) {+ try {+ const sessionSpans = await adapter.getSpansBySession(sessionId);+ const byId = new Map<string, NormalizedSpan>();+ for (const s of [...spans, ...sessionSpans]) byId.set(s.spanId, s);+ spans = Array.from(byId.values());+ } catch {+ // Session expansion is best-effort on external sources.+ }+ }++ if (spans.length === 0) {+ return { err: "Failed to fetch trace spans", record: {} };+ }++ const rows = spans.map(denormalizeSpanToTraceRow);+ const heirarchy = buildHierarchy(rows);+ if (!heirarchy) return { err: "Error building hierarchy", record: {} };+ return { err: null, record: heirarchy };+ } catch (err) {+ rethrowIfSourceFailure(err);+ return { err: asErrorMessage(err), record: {} };+ }+}++const BUCKET_INTERVAL: Record<string, string> = {+ hour: "1h",+ day: "1d",+ week: "1w",+ month: "1M",+};++/** Friendly filter-bar groupBy keys -> the attribute/field an adapter groups on. */+const GROUP_BY_FIELD: Record<string, string> = {+ model: "gen_ai.request.model",+ provider: "gen_ai.system",+ spanName: "SpanName",+ applicationName: "service.name",+};++function groupByToField(groupBy: string): string {+ if (groupBy in GROUP_BY_FIELD) return GROUP_BY_FIELD[groupBy];+ const sep = groupBy.indexOf(":");+ return sep === -1 ? groupBy : groupBy.slice(sep + 1);+}++/**+ * Filter-bar config (models / providers / span names / environments / maxCost).+ * Built-in ClickHouse computes it in one pass; external sources enumerate+ * distinct values per field when the adapter supports it, and return an empty+ * config (dropdowns render empty) when it does not.+ */+export async function getTraceFilterConfig(params: MetricParams) {+ const { adapter } = await resolveTracesAdapter(params.sourceId, params.environment);++ const emptyRow = {+ providers: [] as string[],+ maxCost: 0,+ models: [] as string[],+ traceTypes: [] as string[],+ totalRows: 0,+ applicationNames: [] as string[],+ spanNames: [] as string[],+ environments: [] as string[],+ };+ if (!adapter.capabilities().distinctValues) {+ // Still try planner L1 — adapters may implement via sample compute.+ }++ try {+ const query = externalTraceQuery(params, { aiSelector: false });+ // Prefer native service discovery for the Application filter (avoids+ // single-service bias from an unstratified L1 sample).+ let applicationNames: string[] = [];+ try {+ const discovered = await adapter.discoverServices(query.timeRange);+ applicationNames = discovered+ .map((d) => d.serviceName)+ .filter(Boolean);+ } catch {+ applicationNames = [];+ }++ // One shared L1 sample powers the remaining distinct probes (models,+ // providers, span names, …) instead of five separate 100-trace downloads.+ const { fetchSpansForAggregation } = await import(+ "@/lib/platform/connectors/datasource/graph/sample-fetch"+ );+ const { distinctFromSpans } = await import(+ "@/lib/platform/connectors/datasource/graph/sample-aggregate"+ );+ const { spans } = await fetchSpansForAggregation(adapter, query);+ const models = distinctFromSpans(spans, "gen_ai.request.model");+ const providers = distinctFromSpans(spans, "gen_ai.system");+ // Prefer adapter-native enums (Jaeger /api/services/{svc}/operations) when+ // available so Span Names match the Jaeger Search UI instead of a sample.+ let spanNames: string[] = [];+ try {+ spanNames = await adapter.distinctValues("SpanName", query);+ } catch {+ spanNames = distinctFromSpans(spans, "SpanName");+ }+ if (!spanNames.length) {+ spanNames = distinctFromSpans(spans, "SpanName");+ }+ const traceTypes = distinctFromSpans(spans, "gen_ai.operation.type");+ if (!applicationNames.length) {+ applicationNames = distinctFromSpans(spans, "service.name");+ }+ return {+ err: null,+ data: [+ {+ ...emptyRow,+ models,+ providers,+ spanNames,+ applicationNames,+ traceTypes,+ },+ ],+ };+ } catch (err) {+ rethrowIfSourceFailure(err);+ return { err: asErrorMessage(err), data: [emptyRow] };+ }+}++/** Attribute-key discovery for the custom-filter builder. */+export async function getTraceAttributeKeys(params: MetricParams) {+ const { adapter } = await resolveTracesAdapter(params.sourceId, params.environment);++ const empty = { err: null, spanAttributeKeys: [], resourceAttributeKeys: [] };+ try {+ const query = externalTraceQuery(params, { aiSelector: false });+ const keys = await adapter.attributeKeys("traces", query.timeRange);+ return { err: null, spanAttributeKeys: keys, resourceAttributeKeys: [] };+ } catch {+ return empty;+ }+}++/** Grouped rollup (count / cost / tokens / avg duration) for a groupBy key. */+export async function getTraceGrouped(params: MetricParams, groupBy: string) {+ const { adapter } = await resolveTracesAdapter(params.sourceId, params.environment);++ try {+ const base = externalTraceQuery(params, { aiSelector: false });+ const field = groupByToField(groupBy);+ const query: OpenLITQuery = {+ ...base,+ groupBy: [field],+ aggregations: [+ { fn: "count", as: "count" },+ { fn: "sum", field: "gen_ai.usage.cost", as: "total_cost" },+ { fn: "sum", field: "gen_ai.usage.total_tokens", as: "total_tokens" },+ { fn: "avg", field: "duration", as: "avg_duration_seconds" },+ ],+ };+ const frame = await planAndAggregateSpans(adapter, query);+ const data = (frame.rows as Record<string, unknown>[]).map((row) => ({+ group_value: String(row.group_value ?? row[field] ?? row.g0 ?? ""),+ count: Number(row.count ?? 0),+ total_cost: Number(row.total_cost ?? 0),+ total_tokens: Number(row.total_tokens ?? 0),+ avg_duration_seconds: Number(row.avg_duration_seconds ?? 0),+ }));+ return { err: null, data };+ } catch (err) {+ rethrowIfSourceFailure(err);+ return { err: asErrorMessage(err), data: [] };+ }+}++/**+ * Signal summary bar-chart series. `signal` is "traces" or "exceptions" (the+ * latter adds an error-status filter). Built-in ClickHouse keeps its exact+ * bucketed SQL; external sources use the query planner (L0/L1/L2).+ */+export async function getTraceSummary(+ params: MetricParams,+ signal: "traces" | "exceptions" = "traces"+) {+ const { adapter } = await resolveTracesAdapter(params.sourceId, params.environment);++ const bucket = getSummaryBucket(params);+ const empty = { err: null, bucket, buckets: [], total: 0, peak: 0 };++ try {+ const base = externalTraceQuery(params, { aiSelector: false });+ const query: OpenLITQuery = {+ ...base,+ interval: BUCKET_INTERVAL[bucket] || intervalFromTimeRange(+ base.timeRange.start,+ base.timeRange.end+ ),+ filters:+ signal === "exceptions"+ ? [+ ...(base.filters || []),+ { target: "status", op: "in", value: ["STATUS_CODE_ERROR", "Error", "ERROR"] },+ ]+ : base.filters,+ aggregations: [+ { fn: "count", as: "count" },+ { fn: "avg", field: "duration", as: "avgDuration" },+ { fn: "sum", field: "gen_ai.usage.cost", as: "cost" },+ { fn: "sum", field: "gen_ai.usage.total_tokens", as: "tokens" },+ ],+ };+ // Trace backends such as Tempo can return lightweight trace summaries+ // without downloading every span. Prefer that trace-level series when+ // available so a 200-trace L1 sample is never presented as the volume.+ const frame = adapter.traceTimeSeries+ ? await adapter.traceTimeSeries(query)+ : await planAndSpanTimeSeries(adapter, query);+ const buckets = (frame.rows as Record<string, unknown>[]).map((row) => ({+ label: String(row.label ?? row.request_time ?? row.bucket ?? ""),+ count: Number(row.count ?? 0),+ avgDuration: Number(row.avgDuration ?? 0),+ cost: Number(row.cost ?? 0),+ tokens: Number(row.tokens ?? 0),+ }));+ const total = buckets.reduce((sum, b) => sum + b.count, 0);+ const peak = buckets.reduce((max, b) => Math.max(max, b.count), 0);+ return {+ err: null,+ bucket,+ buckets,+ total,+ peak,+ freshness: frame.meta?.freshness || "sampled",+ truncated: frame.meta?.truncated || false,+ };+ } catch (err) {+ rethrowIfSourceFailure(err);+ return { ...empty, err: asErrorMessage(err) };+ }+}++/** Total request count with previous-period comparison (dashboard graphs). */+export async function getTraceTotalRequests(params: MetricParams) {+ const { adapter } = await resolveTracesAdapter(params.sourceId, params.environment);++ try {+ const current = await planAndAggregateSpans(+ adapter,+ {+ ...externalTraceQuery(params, { aiSelector: false }),+ aggregations: [{ fn: "count", as: "total_requests" }],+ },+ {}+ );+ const previousParams = getFilterPreviousParams(params);+ const previous = await planAndAggregateSpans(adapter, {+ ...externalTraceQuery(previousParams, { aiSelector: false }),+ aggregations: [{ fn: "count", as: "total_requests" }],+ });+ const currentTotal = Number(+ (current.rows[0] as Record<string, unknown> | undefined)?.total_requests ??+ (current.rows[0] as Record<string, unknown> | undefined)?.count ??+ 0+ );+ const previousTotal = Number(+ (previous.rows[0] as Record<string, unknown> | undefined)+ ?.total_requests ??+ (previous.rows[0] as Record<string, unknown> | undefined)?.count ??+ 0+ );+ return {+ err: null,+ data: [+ {+ total_requests: currentTotal,+ previous_total_requests: previousTotal,+ },+ ],+ };+ } catch (err) {+ rethrowIfSourceFailure(err);+ return { err: asErrorMessage(err), data: [] };+ }+}++/** Requests-over-time series for dashboard graphs. */+export async function getTraceRequestPerTime(params: MetricParams) {+ const { adapter } = await resolveTracesAdapter(params.sourceId, params.environment);++ try {+ const query = externalTraceQuery(params, { aiSelector: false });+ const interval =+ query.interval ||+ intervalFromTimeRange(query.timeRange.start, query.timeRange.end);+ const frame = await planAndSpanTimeSeries(+ adapter,+ {+ ...query,+ interval,+ aggregations: [{ fn: "count", as: "total" }],+ },+ {}+ );+ const data = (frame.rows as Record<string, unknown>[]).map((row) => ({+ total: Number(row.total ?? row.count ?? 0),+ request_time: String(row.request_time ?? row.label ?? row.bucket ?? ""),+ }));+ return { err: null, data };+ } catch (err) {+ rethrowIfSourceFailure(err);+ return { err: asErrorMessage(err), data: [] };+ }+}++/** Average request duration with previous-period comparison. */+export async function getTraceAverageDuration(params: MetricParams) {+ const { adapter } = await resolveTracesAdapter(params.sourceId, params.environment);++ try {+ const current = await planAndAggregateSpans(adapter, {+ ...externalTraceQuery(params, { aiSelector: false }),+ aggregations: [{ fn: "avg", field: "duration", as: "average_duration" }],+ });+ const previousParams = getFilterPreviousParams(params);+ const previous = await planAndAggregateSpans(adapter, {+ ...externalTraceQuery(previousParams, { aiSelector: false }),+ aggregations: [{ fn: "avg", field: "duration", as: "average_duration" }],+ });+ const average_duration = Number(+ (current.rows[0] as Record<string, unknown> | undefined)+ ?.average_duration ?? 0+ );+ const previous_average_duration = Number(+ (previous.rows[0] as Record<string, unknown> | undefined)+ ?.average_duration ?? 0+ );+ return {+ err: null,+ data: [{ average_duration, previous_average_duration }],+ };+ } catch (err) {+ rethrowIfSourceFailure(err);+ return { err: asErrorMessage(err), data: [] };+ }+}++/** Whether any AI traces exist in the bound traces source (onboarding gate). */+export async function getTraceExist(environment?: string) {+ const { adapter } = await resolveTracesAdapter(undefined, environment);++ try {+ const end = new Date();+ const start = new Date(end.getTime() - 7 * 24 * 60 * 60 * 1000);+ const frame = await adapter.listSpans({+ signal: "traces",+ timeRange: { start, end },+ aiSelector: true,+ limit: 1,+ });+ return {+ err: null,+ data: [{ total_requests: frame.rows.length > 0 ? 1 : 0 }],+ };+ } catch (err) {+ rethrowIfSourceFailure(err);+ return { err: asErrorMessage(err), data: [{ total_requests: 0 }] };+ }+}diff --git a/src/client/src/lib/platform/vault/index.ts b/src/client/src/lib/platform/vault/index.tsindex a2cee43f..3573c0da 100644--- a/src/client/src/lib/platform/vault/index.ts+++ b/src/client/src/lib/platform/vault/index.ts@@ -12,9 +12,11 @@ import Sanitizer from "@/utils/sanitizer";import { OPENLIT_VAULT_TABLE_NAME } from "./table-details";import { dataCollector } from "../common";import { jsonStringify } from "@/utils/json";-import { getAPIKeyInfo } from "../api-keys";+import { getAPIKeyInfo, type APIKeyInfo } from "../api-keys";import { decryptValue, encryptValue } from "@/utils/crypto";import { emitManagementAlertSignalSafe } from "@/lib/platform/alerts/signals";+import prisma from "@/lib/prisma";+import { invalidateSourceSecretCache } from "@/lib/platform/connectors/datasource/http/secret";function escapeClickHouseString(value: string) {return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'");@@ -76,7 +78,21 @@ export async function upsertSecret(secretInputParams: Partial<SecretInput>) {throwIfError(!user, getMessage().UNAUTHORIZED_USER);- const secretInput = Sanitizer.sanitizeObject(secretInputParams);+ // Preserve the secret value before SQL sanitization. `sqlString.escape`+ // turns JSON quotes into \" which survives encryption and then fails+ // JSON.parse on read — adapters see empty credentials and Grafana returns+ // "authentication error: no credentials provided".+ const rawValue =+ typeof secretInputParams.value === "string"+ ? secretInputParams.value+ : undefined;+ const secretInput = Sanitizer.sanitizeObject({+ ...secretInputParams,+ value: undefined,+ }) as Partial<SecretInput>;+ if (rawValue !== undefined) {+ secretInput.value = rawValue;+ }const verifiedSecretObj = verifySecretInput(secretInput);throwIfError(!verifiedSecretObj.success, verifiedSecretObj.err!);@@ -145,6 +161,7 @@ export async function upsertSecret(secretInputParams: Partial<SecretInput>) {},});+ invalidateSourceSecretCache(secretInput.id);return getMessage().SECRET_SAVED;} else {createdSecretId = randomUUID();@@ -185,6 +202,8 @@ export async function upsertSecret(secretInputParams: Partial<SecretInput>) {},});+ invalidateSourceSecretCache(createdSecretId);+return {data: createdSecretId ? { id: createdSecretId } : {},id: createdSecretId,@@ -210,6 +229,8 @@ export async function deleteSecret(secretIdParam: string) {return [getMessage().SECRET_NOT_DELETED];}+ invalidateSourceSecretCache(secretId);+emitManagementAlertSignalSafe({triggerType: "vault_secret_change",event: "vault_secret_deleted",@@ -230,14 +251,17 @@ export async function getSecrets(filters: SecretGetFilters,{ selectValue }: { selectValue?: boolean } = {}) {- let user: Awaited<ReturnType<typeof getCurrentUser>> | null = null;- if (!filters.databaseConfigId) {- user = await getCurrentUser();- throwIfError(!user, getMessage().UNAUTHORIZED_USER);+ const user = await getCurrentUser();+ if (!user && !filters.createdBy) {+ throwIfError(true, getMessage().UNAUTHORIZED_USER);}const filteredConditions: string[] = [- user ? getOwnerEmailCondition(user, "v") : "",+ user+ ? getOwnerEmailCondition(user, "v")+ : filters.createdBy+ ? `v.created_by = '${escapeClickHouseString(Sanitizer.sanitizeValue(filters.createdBy))}'`+ : "",filters.key? "v.key = '" +escapeClickHouseString(Sanitizer.sanitizeValue(filters.key)) +@@ -288,10 +312,12 @@ export async function getSecretsFromDatabaseId(err || getMessage().NO_API_KEY);+ const apiInfoForSecrets = apiInfo as APIKeyInfo | null | undefined;const { err: secretErr, data: secretData } = await getSecrets({...filters,- databaseConfigId: apiInfo.databaseConfigId,+ databaseConfigId: apiInfoForSecrets?.databaseConfigId || undefined,+ createdBy: apiInfoForSecrets?.createdByUser?.email,},{ selectValue: true });@@ -308,11 +334,27 @@ export async function getSecretById(id: string,databaseConfigId?: string,excludeVaultValue: boolean = true,- { logDecryptErrors = true }: { logDecryptErrors?: boolean } = {}+ {+ logDecryptErrors = true,+ projectId,+ }: { logDecryptErrors?: boolean; projectId?: string } = {}) {+ const safeId = escapeClickHouseString(Sanitizer.sanitizeValue(id));+ let ownerCondition = "";+ if (projectId) {+ const source = await prisma.telemetrySource.findFirst({+ where: { secretRef: id, projectId },+ select: { id: true },+ });+ if (!source) return { data: [] };+ } else {+ const user = await getCurrentUser();+ throwIfError(!user, getMessage().UNAUTHORIZED_USER);+ ownerCondition = ` AND ${getOwnerEmailCondition(user!, "v")}`;+ }const query = `SELECT * ${!!excludeVaultValue ? "EXCEPT value" : ""- } FROM ${OPENLIT_VAULT_TABLE_NAME} v WHERE v.id = '${id}';`;+ } FROM ${OPENLIT_VAULT_TABLE_NAME} v WHERE v.id = '${safeId}'${ownerCondition};`;const result = await dataCollector({ query }, "query", databaseConfigId);diff --git a/src/client/src/lib/platform/vector/application.ts b/src/client/src/lib/platform/vector/application.tsindex d56c5208..0be74149 100644--- a/src/client/src/lib/platform/vector/application.ts+++ b/src/client/src/lib/platform/vector/application.ts@@ -1,8 +1,12 @@import { getTraceMappingKeyFullPath } from "@/helpers/server/trace";import { MetricParams, OTEL_TRACES_TABLE_NAME, dataCollector } from "../common";import { getFilterWhereCondition } from "@/helpers/server/platform";+import { externalResultGenerationByApplication } from "./external";export async function getResultGenerationByApplication(params: MetricParams) {+ const external = await externalResultGenerationByApplication(params);+ if (external) return external;+const key = `ResourceAttributes['${getTraceMappingKeyFullPath("applicationName")}']`;diff --git a/src/client/src/lib/platform/vector/environment.ts b/src/client/src/lib/platform/vector/environment.tsindex fa793fe2..77ee42e1 100644--- a/src/client/src/lib/platform/vector/environment.ts+++ b/src/client/src/lib/platform/vector/environment.ts@@ -1,8 +1,11 @@-import { getTraceMappingKeyFullPath } from "@/helpers/server/trace";import { MetricParams, OTEL_TRACES_TABLE_NAME, dataCollector } from "../common";import { getFilterWhereCondition } from "@/helpers/server/platform";+import { externalResultGenerationByEnvironment } from "./external";export async function getResultGenerationByEnvironment(params: MetricParams) {+ const external = await externalResultGenerationByEnvironment(params);+ if (external) return external;+// See `helpers/server/platform.ts` — environment lives at// `ResourceAttributes['deployment.environment']` (OTel standard).const keyPathEnvironment = `ResourceAttributes['deployment.environment']`;diff --git a/src/client/src/lib/platform/vector/external.ts b/src/client/src/lib/platform/vector/external.tsnew file mode 100644index 00000000..18ee905d--- /dev/null+++ b/src/client/src/lib/platform/vector/external.ts@@ -0,0 +1,75 @@+/**+ * External-traces path for Vector DB dashboard widgets.+ * Built-in ClickHouse keeps SQL in sibling modules; this routes through the+ * datasource QueryPlanner so Tempo/Jaeger/Datadog/etc. all work.+ */++import type { MetricParams } from "@/lib/platform/common";+import { metricParamsToOpenLITQuery } from "@/lib/platform/connectors/datasource/clickhouse/query-map";+import { planAndAggregateSpans } from "@/lib/platform/connectors/datasource/query-planner";+import type { OpenLITQuery } from "@/lib/platform/connectors/datasource/types";++async function resolveExternalTraces(environment?: string) {+ const { getTelemetryAdapter, resolveTelemetrySourceDescriptor } =+ await import("@/lib/telemetry-source");+ const descriptor = await resolveTelemetrySourceDescriptor({+ signal: "traces",+ environment,+ });+ if (descriptor.isBuiltIn || descriptor.type === "clickhouse") {+ return null;+ }+ const adapter = await getTelemetryAdapter({ signal: "traces", environment });+ return { adapter, descriptor };+}++function asError(err: unknown): string {+ return err instanceof Error ? err.message : String(err);+}++function vectordbParams(params: MetricParams): MetricParams {+ return { ...params, operationType: "vectordb" };+}++async function externalGroupBy(+ params: MetricParams,+ field: string,+ valueKey: string+) {+ const resolved = await resolveExternalTraces(params.environment);+ if (!resolved) return null;+ const { adapter } = resolved;+ try {+ const query: OpenLITQuery = {+ ...metricParamsToOpenLITQuery(vectordbParams(params), "traces"),+ groupBy: [field],+ aggregations: [{ fn: "count", as: "count" }],+ };+ const frame = await planAndAggregateSpans(adapter, query);+ return {+ err: null,+ data: (frame.rows as Record<string, unknown>[]).map((row) => ({+ [valueKey]: String(row.group_value ?? row.g0 ?? row[field] ?? ""),+ count: Number(row.count ?? 0),+ })),+ };+ } catch (err) {+ return { err: asError(err), data: [] };+ }+}++export async function externalResultGenerationByOperation(params: MetricParams) {+ return externalGroupBy(params, "db.operation", "operation");+}++export async function externalResultGenerationBySystem(params: MetricParams) {+ return externalGroupBy(params, "db.system", "system");+}++export async function externalResultGenerationByEnvironment(params: MetricParams) {+ return externalGroupBy(params, "deployment.environment", "environment");+}++export async function externalResultGenerationByApplication(params: MetricParams) {+ return externalGroupBy(params, "service.name", "applicationName");+}diff --git a/src/client/src/lib/platform/vector/operation.ts b/src/client/src/lib/platform/vector/operation.tsindex 56d8a815..f04a2d5f 100644--- a/src/client/src/lib/platform/vector/operation.ts+++ b/src/client/src/lib/platform/vector/operation.ts@@ -1,8 +1,12 @@import { getTraceMappingKeyFullPath } from "@/helpers/server/trace";import { MetricParams, dataCollector, OTEL_TRACES_TABLE_NAME } from "../common";import { getFilterWhereCondition } from "@/helpers/server/platform";+import { externalResultGenerationByOperation } from "./external";export async function getResultGenerationByOperation(params: MetricParams) {+ const external = await externalResultGenerationByOperation(params);+ if (external) return external;+const keyPath = `SpanAttributes['${getTraceMappingKeyFullPath("operation")}']`;const query = `SELECT${keyPath} AS operation,diff --git a/src/client/src/lib/platform/vector/system.ts b/src/client/src/lib/platform/vector/system.tsindex 1311aae9..6f2ece19 100644--- a/src/client/src/lib/platform/vector/system.ts+++ b/src/client/src/lib/platform/vector/system.ts@@ -1,8 +1,12 @@import { getTraceMappingKeyFullPath } from "@/helpers/server/trace";import { MetricParams, dataCollector, OTEL_TRACES_TABLE_NAME } from "../common";import { getFilterWhereCondition } from "@/helpers/server/platform";+import { externalResultGenerationBySystem } from "./external";export async function getResultGenerationBySystem(params: MetricParams) {+ const external = await externalResultGenerationBySystem(params);+ if (external) return external;+const keyPath = `SpanAttributes['${getTraceMappingKeyFullPath("system")}']`;const query = `SELECT${keyPath} AS system,diff --git a/src/client/src/lib/project-environment.ts b/src/client/src/lib/project-environment.tsnew file mode 100644index 00000000..60581ddc--- /dev/null+++ b/src/client/src/lib/project-environment.ts@@ -0,0 +1,39 @@+import prisma from "./prisma";+import { getCurrentOrganisation, getCurrentProjectForOrganisation } from "./organisation";++const ENVIRONMENT_PATTERN = /^[a-z0-9][a-z0-9._-]{0,62}$/;++async function requireProjectId() {+ const organisation = await getCurrentOrganisation();+ if (!organisation?.id) throw new Error("Organisation is required");+ const project = await getCurrentProjectForOrganisation(organisation.id);+ if (!project?.id) throw new Error("Project is required");+ return project.id;+}++export function normalizeProjectEnvironment(value: unknown) {+ const name = String(value || "").trim().toLowerCase();+ if (!ENVIRONMENT_PATTERN.test(name)) {+ throw new Error("Environment must use lowercase letters, numbers, dots, hyphens, or underscores");+ }+ return name;+}++export async function listProjectEnvironments() {+ const projectId = await requireProjectId();+ return prisma.projectEnvironment.findMany({ where: { projectId }, orderBy: { name: "asc" } });+}++export async function createProjectEnvironment(value: unknown) {+ const projectId = await requireProjectId();+ const name = normalizeProjectEnvironment(value);+ // Some isolated CE unit tests provide a minimal Prisma mock from before+ // environments became first-class. Keep environment registration additive+ // for those callers while production always has the generated delegate.+ if (!prisma.projectEnvironment?.upsert) return { id: "", projectId, name };+ return prisma.projectEnvironment.upsert({+ where: { projectId_name: { projectId, name } },+ create: { projectId, name },+ update: {},+ });+}diff --git a/src/client/src/lib/telemetry-source-crud.ts b/src/client/src/lib/telemetry-source-crud.tsnew file mode 100644index 00000000..7b2c411e--- /dev/null+++ b/src/client/src/lib/telemetry-source-crud.ts@@ -0,0 +1,723 @@+/**+ * Project-scoped CRUD for `TelemetrySource` rows plus health/AI-signal probes.+ *+ * Every operation is scoped to the caller's *current* project (resolved from+ * their current organisation membership), so a user can never read or mutate a+ * source in a project they are not a member of. Adapter health checks and AI+ * signal validation bind a concrete adapter for the row and call into the+ * datasource layer, so the UI can show honest reachability/capability state.+ */++import { randomUUID } from "crypto";+import prisma from "./prisma";+import { createProjectEnvironment } from "./project-environment";+import {+ getCurrentOrganisation,+ getCurrentProjectForOrganisation,+} from "@/lib/organisation";+import { getSecretById, upsertSecret } from "./platform/vault";+import {+ parseSignals,+ toDescriptor,+ resolveTelemetrySourceDescriptor,+} from "./telemetry-source";+import { ensureAdaptersRegistered } from "./platform/connectors/datasource/bootstrap";+import {+ createAdapter,+ getSourceTypeDescriptor,+ hasAdapterFactory,+ listSourceTypeDescriptors,+} from "./platform/connectors/datasource/registry";+import type { TelemetrySource } from "@prisma/client";+import type {+ AISignalValidation,+ HealthCheckResult,+ QueryTimeRange,+ Signal,+ SourceCapabilities,+} from "./platform/connectors/datasource/types";+import { UnsupportedCapabilityError } from "./platform/connectors/datasource/types";+import { connectorDescription } from "./platform/connectors/descriptions";+import { connectorIconPath } from "./platform/connectors/icons";+import {+ removeLegacyConnector,+ syncTelemetrySourceConnector,+} from "@/lib/platform/connectors/instances";+import { assertPremiumConnectorAllowed } from "@/lib/access/connector-entitlement";+import {+ TELEMETRY_SOURCE_NAME_REQUIRED,+ TELEMETRY_SOURCE_NAME_TAKEN,+ TELEMETRY_SOURCE_TYPE_UNKNOWN,+ TELEMETRY_SOURCE_NO_PROJECT,+ TELEMETRY_SOURCE_NOT_FOUND,+ TELEMETRY_SOURCE_INVALID_SETTINGS,+ TELEMETRY_SOURCE_SIGNAL_NOT_IN_TYPE,+ TELEMETRY_SOURCE_NO_SIGNALS,+ TELEMETRY_SOURCE_BINDING_SIGNAL_UNSERVED,+ TELEMETRY_SOURCE_BINDING_ENVIRONMENT_MISMATCH,+ TELEMETRY_SOURCE_INVALID_SIGNAL,+ TELEMETRY_SOURCE_AI_VALIDATION_UNSUPPORTED,+} from "@/constants/messages/en";+import { normalizeDatasourceEndpointUrl } from "./platform/connectors/datasource/http/endpoint-url";+import { clearQueryCache } from "./platform/connectors/datasource/http/cache";+import { invalidateSourceSecretCache } from "./platform/connectors/datasource/http/secret";+import { getDBConfigByUser } from "@/lib/db-config";+import type { DatabaseConfig } from "@prisma/client";++/** Strip connector-registry `telemetry:` prefixes down to the TelemetrySource id. */+export function normalizeTelemetrySourceId(sourceId: string): string {+ let id = String(sourceId || "").trim();+ while (id.startsWith("telemetry:")) {+ id = id.slice("telemetry:".length);+ }+ return id;+}++const ALL_SIGNALS: Signal[] = ["traces", "logs", "metrics", "intelligence"];++function validateSignal(signal: unknown): Signal {+ const s = String(signal || "").trim();+ if (!ALL_SIGNALS.includes(s as Signal)) {+ throw new Error(TELEMETRY_SOURCE_INVALID_SIGNAL(String(signal)));+ }+ return s as Signal;+}++export interface TelemetrySourceInput {+ name?: unknown;+ environment?: unknown;+ type?: unknown;+ signals?: unknown;+ settings?: unknown;+ secretRef?: unknown;+ /**+ * Inline credentials (e.g. `{ apiKey, appKey }` or `{ token }`). When+ * present and non-empty, the server persists them as an `openlit_vault`+ * secret and stores only the returned secret id as `secretRef`. Raw+ * credentials are never stored on the source row and never returned to the+ * client.+ */+ credentials?: unknown;+ isDefault?: unknown;+}++/**+ * Persist inline credentials to the vault and return the new secret id.+ * Returns undefined when no non-empty credential values are supplied. Blank+ * values are stripped so an empty edit never overwrites stored credentials.+ */+async function credentialsToSecretRef(+ credentials: unknown,+ sourceName: string,+ type: string+): Promise<string | undefined> {+ if (!credentials || typeof credentials !== "object" || Array.isArray(credentials)) {+ return undefined;+ }+ const entries = Object.entries(credentials as Record<string, unknown>).filter(+ ([, v]) => typeof v === "string" && v.trim() !== ""+ );+ if (entries.length === 0) return undefined;+ const value = JSON.stringify(Object.fromEntries(entries));+ const key = `telemetry-source/${type}/${sourceName}/${randomUUID().slice(0, 8)}`;+ const result = (await upsertSecret({ key, value })) as+ | { id?: string }+ | string;+ const id = typeof result === "object" ? result?.id : undefined;+ return id || undefined;+}++/** Resolve the caller's current project id, enforcing membership. */+async function requireCurrentProjectId(): Promise<string> {+ const org = await getCurrentOrganisation();+ if (!org?.id) throw new Error(TELEMETRY_SOURCE_NO_PROJECT);+ const project = await getCurrentProjectForOrganisation(org.id);+ if (!project?.id) throw new Error(TELEMETRY_SOURCE_NO_PROJECT);+ return project.id;+}++/** Drop the vault secret reference from an API-facing row. */+function sanitize(row: TelemetrySource) {+ const { secretRef, ...rest } = row;+ return { ...rest, hasSecret: !!secretRef };+}++function normalizeEnvironment(value: unknown): string {+ const environment = String(value || "production").trim().toLowerCase();+ if (!/^[a-z0-9][a-z0-9._-]{0,62}$/.test(environment)) {+ throw new Error("Environment must use letters, numbers, dots, hyphens, or underscores.");+ }+ return environment;+}++/** Drop adapter query/secret caches after routing or credential changes. */+function invalidateTelemetryReadCaches(secretRef?: string | null) {+ clearQueryCache();+ invalidateSourceSecretCache(secretRef || undefined);+}++function rawSignals(signals: unknown): Signal[] {+ if (Array.isArray(signals)) return parseSignals(signals.join(","));+ if (typeof signals === "string") return parseSignals(signals);+ return parseSignals(undefined);+}++/**+ * Normalize the requested signals for a source type, enforcing that they are a+ * subset of the type's declared signals. When the caller did not specify any+ * signals, defaults to the type's full declared set. Throws when the caller+ * explicitly asked for a signal the type cannot serve.+ */+function normalizeSignalsForType(signals: unknown, type: string): string {+ const declared = getSourceTypeDescriptor(type)?.declaredSignals ?? [+ "traces",+ "logs",+ "metrics",+ ];+ const declaredSet = new Set<string>(declared);+ // No explicit request -> default to the type's declared signals.+ if (signals === undefined || signals === null || signals === "") {+ return declared.join(",");+ }+ const requested = rawSignals(signals);+ const invalid = requested.filter((s) => !declaredSet.has(s));+ if (invalid.length > 0) {+ throw new Error(+ TELEMETRY_SOURCE_SIGNAL_NOT_IN_TYPE(invalid.join(", "), type)+ );+ }+ const allowed = requested.filter((s) => declaredSet.has(s));+ if (allowed.length === 0) throw new Error(TELEMETRY_SOURCE_NO_SIGNALS);+ return allowed.join(",");+}++function normalizeSettingsObject(settings: Record<string, unknown>): Record<string, unknown> {+ const next = { ...settings };+ if (typeof next.url === "string" && next.url.trim()) {+ next.url = normalizeDatasourceEndpointUrl(next.url);+ }+ return next;+}++function normalizeSettings(settings: unknown): string {+ if (settings === undefined || settings === null) return "{}";+ if (typeof settings === "string") {+ // Validate it parses to an object.+ try {+ const parsed = JSON.parse(settings);+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {+ throw new Error(TELEMETRY_SOURCE_INVALID_SETTINGS);+ }+ return JSON.stringify(normalizeSettingsObject(parsed as Record<string, unknown>));+ } catch (error) {+ if (error instanceof Error && error.message === TELEMETRY_SOURCE_INVALID_SETTINGS) {+ throw error;+ }+ throw new Error(TELEMETRY_SOURCE_INVALID_SETTINGS);+ }+ }+ if (typeof settings === "object" && !Array.isArray(settings)) {+ return JSON.stringify(normalizeSettingsObject(settings as Record<string, unknown>));+ }+ throw new Error(TELEMETRY_SOURCE_INVALID_SETTINGS);+}++async function validateSecretReference(secretRef: string | null) {+ if (!secretRef) return;+ const result = await getSecretById(secretRef);+ if (!(result.data as unknown[] | undefined)?.length) {+ throw new Error("The selected vault secret is not owned by the current user.");+ }+}++function validateType(type: unknown): string {+ ensureAdaptersRegistered();+ const t = String(type || "").trim();+ if (!t || !hasAdapterFactory(t)) {+ throw new Error(TELEMETRY_SOURCE_TYPE_UNKNOWN(String(type)));+ }+ // Only registered datasource connector types can be created.+ if (getSourceTypeDescriptor(t)?.internal) {+ throw new Error(TELEMETRY_SOURCE_TYPE_UNKNOWN(t));+ }+ return t;+}++function isUniqueConstraintError(error: unknown): boolean {+ const message = error instanceof Error ? error.message : String(error || "");+ const code =+ typeof error === "object" && error && "code" in error+ ? String((error as { code?: unknown }).code || "")+ : "";+ return (+ code === "P2002" ||+ message.includes("Unique constraint failed") ||+ message.includes("unique constraint")+ );+}++/** List all telemetry sources in the current project (secrets stripped). */+export async function listTelemetrySources() {+ const projectId = await requireCurrentProjectId();+ const rows = await prisma.telemetrySource.findMany({+ where: { projectId },+ orderBy: [{ isDefault: "desc" }, { createdAt: "asc" }],+ });+ return rows.map(sanitize);+}++/** The adapter source types this build can serve (atomic types only). */+export function availableSourceTypes(): string[] {+ ensureAdaptersRegistered();+ return listSourceTypeDescriptors().map((d) => d.type);+}++/** Full static descriptors for the atomic source types this build can serve. */+export function availableSourceTypeDescriptors() {+ ensureAdaptersRegistered();+ return listSourceTypeDescriptors().map((descriptor) => ({+ ...descriptor,+ description: descriptor.description || connectorDescription(descriptor.type, descriptor.displayName),+ icon: descriptor.icon || connectorIconPath(descriptor.type),+ }));+}++/** Resolved per-signal capabilities for the active project's routed sources. */+export interface ResolvedSignalCapability {+ sourceType: string;+ sourceName: string;+ isBuiltIn: boolean;+ capabilities: Omit<SourceCapabilities, "signals"> | null;+}++/**+ * Resolve the concrete, per-signal capability profile for the current project's+ * routed sources. The UI uses this to gate surfaces honestly (Grafana-style):+ * e.g. hide the trace tree or aggregation ops a bound source cannot serve+ * rather than erroring. Resolution mirrors query routing (binding -> default ->+ * built-in) and never throws for a single signal — an unresolvable signal is+ * reported as `null` capabilities.+ */+export async function resolveProjectSignalCapabilities(+ environment?: string+): Promise<Record<Signal, ResolvedSignalCapability | null>> {+ ensureAdaptersRegistered();+ const out = {} as Record<Signal, ResolvedSignalCapability | null>;+ for (const signal of ALL_SIGNALS) {+ try {+ const descriptor = await resolveTelemetrySourceDescriptor({+ signal,+ environment,+ });+ const adapter = createAdapter(descriptor);+ const caps = adapter ? adapter.capabilities() : null;+ out[signal] = {+ sourceType: descriptor.type,+ sourceName: descriptor.name,+ isBuiltIn:+ descriptor.isBuiltIn || descriptor.type === "clickhouse",+ capabilities: caps+ ? (() => {+ // Drop the per-instance `signals` list; the UI keys off+ // the capability booleans only.+ const { signals: _signals, ...rest } = caps;+ return rest;+ })()+ : null,+ };+ } catch {+ out[signal] = null;+ }+ }+ return out;+}++/** Create a project-scoped telemetry source. */+export async function createTelemetrySource(input: TelemetrySourceInput) {+ const projectId = await requireCurrentProjectId();+ const name = String(input.name || "").trim();+ if (!name) throw new Error(TELEMETRY_SOURCE_NAME_REQUIRED);+ const type = validateType(input.type);+ await assertPremiumConnectorAllowed(type);+ const environment = normalizeEnvironment(input.environment);+ await createProjectEnvironment(environment);+ const signals = normalizeSignalsForType(input.signals, type);+ const settings = normalizeSettings(input.settings);+ const isDefault = input.isDefault === true;+ const credentialSecretRef = await credentialsToSecretRef(+ input.credentials,+ name,+ type+ );+ const secretRef =+ credentialSecretRef ??+ (typeof input.secretRef === "string" ? input.secretRef : null);+ await validateSecretReference(secretRef);++ const existing = await prisma.telemetrySource.findFirst({+ where: { projectId, name, environment },+ select: { id: true },+ });+ if (existing) {+ throw new Error(TELEMETRY_SOURCE_NAME_TAKEN(name, environment));+ }++ try {+ const row = await prisma.$transaction(async (tx) => {+ if (isDefault) {+ await tx.telemetrySource.updateMany({+ where: { projectId, isDefault: true },+ data: { isDefault: false },+ });+ }+ const created = await tx.telemetrySource.create({+ data: {+ projectId,+ name,+ environment,+ type,+ signals,+ settings,+ secretRef,+ isDefault,+ },+ });+ await tx.connectorInstance.upsert({+ where: { id: `telemetry:${created.id}` },+ create: {+ id: `telemetry:${created.id}`,+ category: "datasource",+ type: created.type,+ name: created.name,+ environment: created.environment || "production",+ projectId: created.projectId,+ settings: created.settings || "{}",+ secretRef: created.secretRef,+ status: "active",+ metadata: JSON.stringify({+ legacyKind: "telemetry-source",+ legacyId: created.id,+ signals: created.signals,+ isDefault: created.isDefault,+ }),+ },+ update: {+ type: created.type,+ name: created.name,+ environment: created.environment || "production",+ projectId: created.projectId,+ settings: created.settings || "{}",+ secretRef: created.secretRef,+ status: "active",+ metadata: JSON.stringify({+ legacyKind: "telemetry-source",+ legacyId: created.id,+ signals: created.signals,+ isDefault: created.isDefault,+ }),+ },+ });+ return created;+ });+ invalidateTelemetryReadCaches(row.secretRef);+ return sanitize(row);+ } catch (error) {+ if (isUniqueConstraintError(error)) {+ throw new Error(TELEMETRY_SOURCE_NAME_TAKEN(name, environment));+ }+ throw error;+ }+}++async function requireSourceInProject(id: string): Promise<TelemetrySource> {+ const projectId = await requireCurrentProjectId();+ const row = await prisma.telemetrySource.findFirst({+ where: { id: normalizeTelemetrySourceId(id), projectId },+ });+ if (!row) throw new Error(TELEMETRY_SOURCE_NOT_FOUND);+ return row;+}++/** Update a telemetry source that belongs to the current project. */+export async function updateTelemetrySource(+ id: string,+ input: TelemetrySourceInput+) {+ const existing = await requireSourceInProject(id);+ const data: Record<string, unknown> = {};+ if (input.name !== undefined) {+ const name = String(input.name || "").trim();+ if (!name) throw new Error(TELEMETRY_SOURCE_NAME_REQUIRED);+ data.name = name;+ }+ if (input.environment !== undefined) {+ data.environment = normalizeEnvironment(input.environment);+ await createProjectEnvironment(data.environment);+ }+ const effectiveType =+ input.type !== undefined ? validateType(input.type) : existing.type;+ if (input.type !== undefined) {+ await assertPremiumConnectorAllowed(effectiveType);+ data.type = effectiveType;+ }+ if (input.signals !== undefined) {+ data.signals = normalizeSignalsForType(input.signals, effectiveType);+ } else if (input.type !== undefined) {+ // Type changed but signals not provided: re-validate stored signals+ // against the new type's declared set, dropping any it cannot serve.+ data.signals = normalizeSignalsForType(existing.signals, effectiveType);+ }+ if (input.settings !== undefined) data.settings = normalizeSettings(input.settings);+ // Inline credentials take precedence: persist them to the vault and repoint+ // secretRef. Blank credentials are ignored so an edit that leaves the+ // credential fields empty keeps the existing secret.+ const effectiveName =+ typeof data.name === "string" ? (data.name as string) : existing.name;+ const credentialSecretRef = await credentialsToSecretRef(+ input.credentials,+ effectiveName,+ effectiveType+ );+ if (credentialSecretRef) {+ data.secretRef = credentialSecretRef;+ } else if (input.secretRef !== undefined) {+ data.secretRef =+ typeof input.secretRef === "string" ? input.secretRef : null;+ }+ await validateSecretReference(+ (data.secretRef as string | null | undefined) ?? existing.secretRef+ );+ const makeDefault = input.isDefault === true;++ const row = await prisma.$transaction(async (tx) => {+ if (makeDefault) {+ await tx.telemetrySource.updateMany({+ where: {+ projectId: existing.projectId,+ isDefault: true,+ NOT: { id },+ },+ data: { isDefault: false },+ });+ data.isDefault = true;+ } else if (input.isDefault === false) {+ data.isDefault = false;+ }+ return tx.telemetrySource.update({ where: { id }, data });+ });+ await syncTelemetrySourceConnector(row);+ invalidateTelemetryReadCaches(+ (data.secretRef as string | null | undefined) ?? row.secretRef+ );+ return sanitize(row);+}++/** Delete a telemetry source that belongs to the current project. */+export async function deleteTelemetrySource(id: string) {+ const existing = await requireSourceInProject(id);+ await prisma.telemetrySource.delete({ where: { id } });+ await removeLegacyConnector("telemetry-source", id);+ invalidateTelemetryReadCaches(existing.secretRef);+ return { id };+}++async function adapterForSource(row: TelemetrySource) {+ ensureAdaptersRegistered();+ // Vault secrets for connectors are scoped by the active database config.+ // Interactive reads attach this in resolveSignalSource; health/test must too.+ const activeDatabase = (await getDBConfigByUser(true)) as+ | DatabaseConfig+ | null+ | undefined;+ const descriptor = {+ ...toDescriptor(row),+ ...(activeDatabase?.id ? { dbConfigId: activeDatabase.id } : {}),+ };+ return createAdapter(descriptor);+}++/** Health-check a telemetry source by binding its adapter. */+export async function healthCheckTelemetrySource(+ id: string+): Promise<HealthCheckResult> {+ const row = await requireSourceInProject(id);+ await assertPremiumConnectorAllowed(row.type);+ const adapter = await adapterForSource(row);+ if (!adapter) {+ return { ok: false, message: TELEMETRY_SOURCE_TYPE_UNKNOWN(row.type) };+ }+ return adapter.healthCheck();+}++/** Probe a telemetry source for AI telemetry over a window. */+export async function validateTelemetrySourceAISignal(+ id: string,+ window: QueryTimeRange+): Promise<AISignalValidation> {+ const row = await requireSourceInProject(id);+ const adapter = await adapterForSource(row);+ if (!adapter) {+ return {+ ok: false,+ sampleCount: 0,+ missingAttributes: [],+ supported: false,+ message: TELEMETRY_SOURCE_TYPE_UNKNOWN(row.type),+ };+ }+ try {+ const result = await adapter.validateAISignal(window);+ return { supported: true, ...result };+ } catch (err) {+ // Logs/metrics-only sources (Loki, Mimir, …) correctly refuse AI-span+ // validation. Test-connection must still succeed on health alone.+ if (err instanceof UnsupportedCapabilityError) {+ return {+ ok: true,+ sampleCount: 0,+ missingAttributes: [],+ supported: false,+ message: TELEMETRY_SOURCE_AI_VALIDATION_UNSUPPORTED(row.type),+ };+ }+ throw err;+ }+}++// ---- Per-signal bindings (Grafana-style per-signal routing) --------------++function publicBindingSourceId(row: {+ sourceId: string | null;+ databaseConfigId: string | null;+} | null | undefined): string | null {+ if (!row) return null;+ if (row.sourceId) return row.sourceId;+ if (row.databaseConfigId) return `builtin:${row.databaseConfigId}`;+ return null;+}++/** List the current project's per-signal source bindings. */+export async function listTelemetrySourceBindings(environmentInput?: unknown) {+ const projectId = await requireCurrentProjectId();+ const environment = normalizeEnvironment(environmentInput);+ const rows = await prisma.telemetrySourceBinding.findMany({+ where: { projectId, environment },+ include: { source: true, databaseConfig: true },+ orderBy: { signal: "asc" },+ });+ return rows.map((b) => ({+ id: b.id,+ signal: b.signal,+ sourceId: b.sourceId || (b.databaseConfigId ? `builtin:${b.databaseConfigId}` : ""),+ sourceName: b.source?.name ?? b.databaseConfig?.name ?? null,+ sourceType: b.source?.type ?? "clickhouse",+ environment: b.environment,+ }));+}++/**+ * Bind a signal to a source in the current project. Enforces membership, that+ * the source belongs to the project, and that the source actually serves the+ * signal (never bind a signal to a source that cannot serve it).+ */+export async function setTelemetrySourceBinding(+ signalInput: unknown,+ sourceId: string,+ environmentInput?: unknown+) {+ const projectId = await requireCurrentProjectId();+ const signal = validateSignal(signalInput);+ const environment = normalizeEnvironment(environmentInput);+ const existing = await prisma.telemetrySourceBinding.findUnique({+ where: { projectId_signal_environment: { projectId, signal, environment } },+ });+ const previousSourceId = publicBindingSourceId(existing);+ const normalizedSourceId = sourceId.startsWith("builtin:")+ ? sourceId+ : normalizeTelemetrySourceId(sourceId);+ const source = await prisma.telemetrySource.findFirst({+ where: { id: normalizedSourceId, projectId },+ });+ if (!source && normalizedSourceId.startsWith("builtin:")) {+ const databaseConfigId = normalizedSourceId.slice("builtin:".length);+ const databaseConfig = await prisma.databaseConfig.findFirst({+ where: { id: databaseConfigId, projectId },+ });+ if (!databaseConfig) throw new Error(TELEMETRY_SOURCE_NOT_FOUND);+ const dbEnvironment = normalizeEnvironment(databaseConfig.environment);+ if (dbEnvironment !== environment) {+ throw new Error(+ TELEMETRY_SOURCE_BINDING_ENVIRONMENT_MISMATCH(+ databaseConfig.name,+ dbEnvironment,+ environment+ )+ );+ }+ const binding = await prisma.telemetrySourceBinding.upsert({+ where: { projectId_signal_environment: { projectId, signal, environment } },+ create: { projectId, signal, environment, sourceId: null, databaseConfigId },+ update: { sourceId: null, databaseConfigId },+ });+ invalidateTelemetryReadCaches();+ return {+ id: binding.id,+ signal: binding.signal,+ sourceId: normalizedSourceId,+ environment,+ previousSourceId,+ };+ }+ if (!source) throw new Error(TELEMETRY_SOURCE_NOT_FOUND);+ await assertPremiumConnectorAllowed(source.type);+ const sourceEnvironment = normalizeEnvironment(source.environment);+ if (sourceEnvironment !== environment) {+ throw new Error(+ TELEMETRY_SOURCE_BINDING_ENVIRONMENT_MISMATCH(+ source.name,+ sourceEnvironment,+ environment+ )+ );+ }+ if (!parseSignals(source.signals).includes(signal)) {+ throw new Error(+ TELEMETRY_SOURCE_BINDING_SIGNAL_UNSERVED(signal, source.name)+ );+ }+ const binding = await prisma.telemetrySourceBinding.upsert({+ where: { projectId_signal_environment: { projectId, signal, environment } },+ create: {+ projectId,+ signal,+ environment,+ sourceId: normalizedSourceId,+ databaseConfigId: null,+ },+ update: { sourceId: normalizedSourceId, databaseConfigId: null },+ });+ invalidateTelemetryReadCaches(source.secretRef);+ return {+ id: binding.id,+ signal: binding.signal,+ sourceId: binding.sourceId,+ environment: binding.environment,+ previousSourceId,+ };+}++/** Remove a signal binding, reverting that signal to capability/built-in routing. */+export async function deleteTelemetrySourceBinding(signalInput: unknown, environmentInput?: unknown) {+ const projectId = await requireCurrentProjectId();+ const signal = validateSignal(signalInput);+ const environment = normalizeEnvironment(environmentInput);+ const existing = await prisma.telemetrySourceBinding.findFirst({+ where: { projectId, signal, environment },+ });+ const previousSourceId = publicBindingSourceId(existing);+ await prisma.telemetrySourceBinding.deleteMany({+ where: { projectId, signal, environment },+ });+ invalidateTelemetryReadCaches();+ return { signal, environment, previousSourceId };+}diff --git a/src/client/src/lib/telemetry-source.ts b/src/client/src/lib/telemetry-source.tsnew file mode 100644index 00000000..68877e8e--- /dev/null+++ b/src/client/src/lib/telemetry-source.ts@@ -0,0 +1,592 @@+/**+ * Telemetry source resolution (CE).+ *+ * Resolves which raw-telemetry source should power a read:+ * 1. An explicit source id (must belong to the caller's current project).+ * 2. The project's per-signal binding / default `TelemetrySource`.+ * 3. The implicit built-in ClickHouse source (the project `DatabaseConfig`).+ *+ * Adapters for external vendors are registered in CE. Product surfaces still+ * need to call into this resolver (and the adapter) to leave ClickHouse —+ * configuring a source alone does not reroute Telemetry/Agents/Evals.+ */++import prisma from "./prisma";+import {+ getDBConfigByUser,+ getDBConfigById,+ getDBConfigByIdInternal,+} from "@/lib/db-config";+import {+ getCurrentOrganisation,+ getCurrentProjectForOrganisation,+} from "@/lib/organisation";+import type { DatabaseConfig, TelemetrySource } from "@prisma/client";+import type {+ DataSourceAdapter,+ Signal,+ TelemetrySourceDescriptor,+} from "./platform/connectors/datasource/types";+import { ensureAdaptersRegistered } from "./platform/connectors/datasource/bootstrap";+import { createAdapter } from "./platform/connectors/datasource/registry";+import { consoleLog } from "@/utils/log";++const ALL_SIGNALS: Signal[] = ["traces", "logs", "metrics", "intelligence"];+const VALID_SIGNALS = new Set<string>(ALL_SIGNALS);++/** Parse the comma-separated `signals` column into a typed list. */+export function parseSignals(raw: string | null | undefined): Signal[] {+ if (!raw) return [...ALL_SIGNALS];+ const parsed = raw+ .split(",")+ .map((s) => s.trim())+ .filter((s): s is Signal => VALID_SIGNALS.has(s));+ return parsed.length > 0 ? parsed : [...ALL_SIGNALS];+}++/** Safely parse the JSON `settings` column. */+export function parseSettings(raw: string | null | undefined): Record<string, unknown> {+ if (!raw) return {};+ try {+ const value = JSON.parse(raw);+ return value && typeof value === "object" ? value : {};+ } catch {+ return {};+ }+}++/** Build the implicit built-in ClickHouse descriptor from a DatabaseConfig. */+export function builtInDescriptor(+ dbConfig: Pick<DatabaseConfig, "id" | "name" | "projectId" | "environment">+): TelemetrySourceDescriptor {+ return {+ type: "clickhouse",+ id: `builtin:${dbConfig.id}`,+ isBuiltIn: true,+ settings: {},+ secretRef: null,+ dbConfigId: dbConfig.id,+ signals: [...ALL_SIGNALS],+ projectId: dbConfig.projectId ?? null,+ name: dbConfig.name,+ environment: dbConfig.environment,+ };+}++/** Map a stored TelemetrySource row to a descriptor. */+export function toDescriptor(row: TelemetrySource): TelemetrySourceDescriptor {+ return {+ type: row.type,+ id: row.id,+ isBuiltIn: false,+ settings: parseSettings(row.settings),+ secretRef: row.secretRef,+ signals: parseSignals(row.signals),+ projectId: row.projectId ?? null,+ name: row.name,+ environment: row.environment,+ };+}++async function getCurrentProjectId(): Promise<string | null> {+ const currentOrg = await getCurrentOrganisation();+ if (!currentOrg?.id) return null;+ const currentProject = await getCurrentProjectForOrganisation(currentOrg.id);+ return currentProject?.id ?? null;+}++export async function resolveBuiltInDescriptor(+ dbConfigId?: string+): Promise<TelemetrySourceDescriptor> {+ let dbConfig: DatabaseConfig | null | undefined;+ if (dbConfigId) {+ dbConfig = await getDBConfigById({ id: dbConfigId });+ } else {+ dbConfig = (await getDBConfigByUser(true)) as DatabaseConfig | undefined;+ }+ if (!dbConfig?.id) {+ // No configured ClickHouse yet — return a descriptor with an empty id so+ // callers can surface "configure a data source" rather than crash.+ return {+ type: "clickhouse",+ id: "builtin:none",+ isBuiltIn: true,+ settings: {},+ secretRef: null,+ dbConfigId: undefined,+ signals: [...ALL_SIGNALS],+ projectId: null,+ name: "ClickHouse",+ };+ }+ return builtInDescriptor(dbConfig);+}++/**+ * Whether a source supports OpenLIT's raw ClickHouse-SQL paths (the+ * natural-language chat / Otter NL feature and raw-SQL custom widgets). Only+ * the built-in ClickHouse source can execute arbitrary SQL against the OTel+ * schema; external sources speak their own query languages and are gated.+ */+export function sourceSupportsNativeSql(+ descriptor: TelemetrySourceDescriptor+): boolean {+ return descriptor.type === "clickhouse";+}++export interface ResolveTelemetrySourceOptions {+ /** A descriptor already resolved by the caller, avoiding duplicate lookup. */+ descriptor?: TelemetrySourceDescriptor;+ /** Environment partition used to select a connector and binding. */+ environment?: string;+ /** Explicit source id override (e.g. dashboard widget `sourceId`). */+ sourceId?: string | null;+ /**+ * The signal being read. When set, resolution is signal-aware: it follows+ * the per-signal binding -> capability -> built-in precedence and NEVER+ * returns a source that does not serve this signal.+ */+ signal?: Signal;+ /** Backing DatabaseConfig id when resolving the built-in source directly. */+ dbConfigId?: string;+ /** Project id override; defaults to the caller's current project. */+ projectId?: string | null;+}++function normalizeEnvironment(value: unknown): string {+ const environment = String(value || "production").trim().toLowerCase();+ return /^[a-z0-9][a-z0-9._-]{0,62}$/.test(environment)+ ? environment+ : "production";+}++/** How a signal's source was chosen (for observability / honest UI). */+export type SignalSourceVia =+ | "override"+ | "binding"+ | "capability"+ | "default"+ | "builtin"+ | "none";++/** Typed result of signal-aware resolution. */+export interface SignalSourceResolution {+ descriptor: TelemetrySourceDescriptor;+ /** True when the resolved source actually serves the requested signal. */+ servesSignal: boolean;+ /** True when a real, reachable source backs this signal (built-in or not). */+ hasSource: boolean;+ via: SignalSourceVia;+}++function descriptorServesSignal(+ descriptor: TelemetrySourceDescriptor,+ signal: Signal+): boolean {+ return descriptor.signals.includes(signal);+}++/**+ * Load a TelemetrySource by id only when it belongs to `projectId`.+ * Cross-project ids must never resolve — that would be an IDOR on the+ * source's endpoint + vault secret.+ */+async function findSourceInProject(+ sourceId: string,+ projectId: string | null | undefined,+ environment?: string+): Promise<TelemetrySource | null> {+ if (!projectId) return null;+ return prisma.telemetrySource.findFirst({+ where: { id: sourceId, projectId, environment: normalizeEnvironment(environment) },+ });+}++/**+ * Signal-aware resolution following Grafana's per-signal datasource model:+ * 1. explicit sourceId override (must belong to the current project)+ * 2. the project's per-signal binding (if it serves the signal)+ * 3. any project source that advertises the signal (default first)+ * 4. the built-in ClickHouse source (serves all signals)+ * 5. a typed "no source" state+ * It never returns a source that lacks the requested signal, and never+ * returns a source from another project.+ */+export async function resolveSignalSource(+ signal: Signal,+ options: ResolveTelemetrySourceOptions = {}+): Promise<SignalSourceResolution> {+ const hasRequestedEnvironment =+ options.environment !== undefined &&+ options.environment !== null &&+ String(options.environment).trim().length > 0;+ const projectId =+ options.projectId !== undefined+ ? options.projectId+ : await getCurrentProjectId();+ // External connector secrets live in the active OpenLIT DatabaseConfig's+ // vault. Explicit environment selection must not discard that credential+ // store: doing so makes the adapter fall back to an implicit DB lookup and+ // leaves background/parallel reads unable to resolve authentication+ // consistently. Background callers pass projectId + dbConfigId explicitly;+ // interactive callers can safely resolve the current project-scoped DB.+ const activeDatabase =+ options.projectId === undefined && !options.dbConfigId+ ? ((await getDBConfigByUser(true)) as+ DatabaseConfig | null | undefined)+ : null;+ const credentialDatabaseConfigId =+ options.dbConfigId || activeDatabase?.id;+ const environment = normalizeEnvironment(options.environment || activeDatabase?.environment || "production");+ const externalDescriptor = (row: TelemetrySource) => ({+ ...toDescriptor(row),+ ...(credentialDatabaseConfigId+ ? { dbConfigId: credentialDatabaseConfigId }+ : {}),+ });++ // 1. Explicit override — project-scoped only (external source or builtin:dbId).+ if (options.sourceId) {+ if (options.sourceId.startsWith("builtin:")) {+ const databaseConfigId = options.sourceId.slice("builtin:".length);+ if (projectId && databaseConfigId) {+ const databaseConfig = await prisma.databaseConfig.findFirst({+ where: {+ id: databaseConfigId,+ projectId,+ ...(hasRequestedEnvironment ? { environment } : {}),+ },+ });+ if (databaseConfig) {+ return {+ descriptor: builtInDescriptor(databaseConfig),+ servesSignal: true,+ hasSource: true,+ via: "override",+ };+ }+ }+ consoleLog(+ `resolveSignalSource: builtin sourceId ${options.sourceId} not found in project; continuing`+ );+ } else {+ const row = await findSourceInProject(+ options.sourceId,+ projectId,+ environment+ );+ if (row) {+ const descriptor = externalDescriptor(row);+ return {+ descriptor,+ servesSignal: descriptorServesSignal(descriptor, signal),+ hasSource: true,+ via: "override",+ };+ }+ consoleLog(+ `resolveSignalSource: sourceId ${options.sourceId} not found in project; continuing`+ );+ }+ }++ if (projectId) {+ // 2. Per-signal binding.+ const binding = await prisma.telemetrySourceBinding.findUnique({+ where: { projectId_signal_environment: { projectId, signal, environment } },+ include: { source: true, databaseConfig: true },+ });+ if (binding?.databaseConfig) {+ const descriptor = builtInDescriptor(binding.databaseConfig);+ return { descriptor, servesSignal: true, hasSource: true, via: "binding" };+ }+ if (binding?.source) {+ const descriptor = externalDescriptor(binding.source);+ if (descriptorServesSignal(descriptor, signal)) {+ return { descriptor, servesSignal: true, hasSource: true, via: "binding" };+ }+ consoleLog(+ `resolveSignalSource: binding for ${signal} points at source "${binding.source.name}" which does not serve ${signal}; continuing`+ );+ }++ // Do not pick an arbitrary connector just because it advertises this+ // signal. A connector is active only after an explicit signal binding;+ // otherwise changing environments can silently read another backend.+ }++ // 3. Preserve the legacy implicit ClickHouse behavior only when the caller+ // did not explicitly select an environment. Environment-scoped requests+ // must fail closed when that environment has no routed connector.+ if (hasRequestedEnvironment) {+ return {+ descriptor: {+ type: "clickhouse",+ id: "builtin:none",+ isBuiltIn: true,+ settings: {},+ secretRef: null,+ dbConfigId: undefined,+ signals: [...ALL_SIGNALS],+ projectId,+ name: "ClickHouse",+ environment,+ },+ servesSignal: false,+ hasSource: false,+ via: "none",+ };+ }++ // 4. Built-in ClickHouse (serves all signals when configured).+ const builtin = activeDatabase+ ? builtInDescriptor(activeDatabase)+ : await resolveBuiltInDescriptor(options.dbConfigId);+ const hasBuiltin = !!builtin.dbConfigId;+ return {+ descriptor: builtin,+ servesSignal: hasBuiltin,+ hasSource: hasBuiltin,+ via: hasBuiltin ? "builtin" : "none",+ };+}++/**+ * Correlation boundary helper: whether a signal is served by the built-in+ * ClickHouse store (the only source that is fully correlated with the other+ * built-in signals). Cross-signal intelligence that runs natively against+ * OpenLIT's ClickHouse (e.g. enriching trace-derived agent snapshots from+ * `otel_logs`) must consult this and degrade gracefully when the signal lives+ * in a different backend, instead of silently querying the wrong store.+ */+export async function isSignalServedByBuiltInClickHouse(+ signal: Signal,+ options: ResolveTelemetrySourceOptions = {}+): Promise<boolean> {+ const resolution = await resolveSignalSource(signal, options);+ return (+ resolution.hasSource === true &&+ resolution.servesSignal === true &&+ resolution.descriptor.isBuiltIn === true &&+ resolution.descriptor.type === "clickhouse" &&+ Boolean(resolution.descriptor.dbConfigId)+ );+}++/**+ * Resolve the telemetry source descriptor for the current request. When a+ * `signal` is supplied, resolution is signal-aware (see `resolveSignalSource`).+ * Otherwise it follows the legacy precedence (explicit id -> project default ->+ * built-in). Never throws for a missing external source; falls back to the+ * built-in ClickHouse source and logs.+ */+export async function resolveTelemetrySourceDescriptor(+ options: ResolveTelemetrySourceOptions = {}+): Promise<TelemetrySourceDescriptor> {+ if (options.signal) {+ const resolution = await resolveSignalSource(options.signal, options);+ return resolution.descriptor;+ }++ const projectId =+ options.projectId !== undefined+ ? options.projectId+ : await getCurrentProjectId();++ // 1. Explicit source id override — project-scoped only (no cross-project IDOR).+ if (options.sourceId) {+ if (options.sourceId.startsWith("builtin:")) {+ const databaseConfigId = options.sourceId.slice("builtin:".length);+ if (projectId && databaseConfigId) {+ const databaseConfig = await prisma.databaseConfig.findFirst({+ where: { id: databaseConfigId, projectId },+ });+ if (databaseConfig) return builtInDescriptor(databaseConfig);+ }+ } else {+ const row = await findSourceInProject(options.sourceId, projectId);+ if (row) return toDescriptor(row);+ }+ consoleLog(+ `resolveTelemetrySource: sourceId ${options.sourceId} not found in project; falling back to default`+ );+ }++ // 2. Current project's default TelemetrySource.+ if (projectId) {+ const row = await prisma.telemetrySource.findFirst({+ where: { projectId, isDefault: true },+ orderBy: { createdAt: "asc" },+ });+ if (row) return toDescriptor(row);+ }++ // 3. Built-in ClickHouse source.+ return resolveBuiltInDescriptor(options.dbConfigId);+}++/**+ * Resolve and bind a concrete telemetry adapter for the current request.+ * Always returns a usable adapter for the built-in ClickHouse path. For an+ * explicitly configured external source, fails closed when no factory is+ * registered — never silently reads the wrong store.+ */+export async function getTelemetryAdapter(+ options: ResolveTelemetrySourceOptions = {}+): Promise<DataSourceAdapter> {+ ensureAdaptersRegistered();+ let resolution: SignalSourceResolution | undefined;+ if (!options.descriptor && options.signal) {+ resolution = await resolveSignalSource(options.signal, options);+ }+ const descriptor =+ options.descriptor ||+ resolution?.descriptor ||+ (await resolveTelemetrySourceDescriptor(options));+ if ((resolution && !resolution.hasSource) || descriptor.id === "builtin:none") {+ const { TELEMETRY_SOURCE_NO_SOURCE_FOR_SIGNAL } = await import(+ "@/constants/messages/en"+ );+ throw new Error(TELEMETRY_SOURCE_NO_SOURCE_FOR_SIGNAL(options.signal!));+ }+ const adapter = createAdapter(descriptor);+ if (adapter) return adapter;++ if (!descriptor.isBuiltIn && descriptor.type !== "clickhouse") {+ const { TELEMETRY_SOURCE_ADAPTER_UNAVAILABLE } = await import(+ "@/constants/messages/en"+ );+ throw new Error(TELEMETRY_SOURCE_ADAPTER_UNAVAILABLE(descriptor.type));+ }++ consoleLog(+ `getTelemetryAdapter: no adapter registered for source type "${descriptor.type}"; falling back to built-in ClickHouse`+ );+ const builtin = await resolveBuiltInDescriptor(options.dbConfigId);+ const fallback = createAdapter(builtin);+ if (!fallback) {+ throw new Error(+ "No telemetry adapter available (built-in ClickHouse factory missing)."+ );+ }+ return fallback;+}++/**+ * Resolve the traces adapter for a DatabaseConfig (cron / materializer path).+ * Uses the config's project bindings rather than the interactive session.+ */+export async function getTelemetryAdapterForDbConfig(+ dbConfigId: string,+ signal: Signal = "traces"+): Promise<{+ adapter: DataSourceAdapter;+ descriptor: TelemetrySourceDescriptor;+ isBuiltIn: boolean;+}> {+ ensureAdaptersRegistered();+ // Cron / job callers have a DatabaseConfig id but no user session.+ // `getDBConfigById` requires getCurrentUser() and throws UNAUTHORIZED_USER,+ // which aborted every materializer tick before Jaeger/Tempo was reached.+ const dbConfig = await getDBConfigByIdInternal({ id: dbConfigId });+ const projectId = dbConfig?.projectId ?? null;+ const resolution = await resolveSignalSource(signal, {+ projectId,+ dbConfigId,+ environment: dbConfig?.environment,+ });+ if (!resolution.hasSource) {+ const { TELEMETRY_SOURCE_NO_SOURCE_FOR_SIGNAL } = await import(+ "@/constants/messages/en"+ );+ throw new Error(TELEMETRY_SOURCE_NO_SOURCE_FOR_SIGNAL(signal));+ }+ // External connector credentials live in the OpenLIT vault table of this+ // DatabaseConfig. Interactive reads can infer that database from session+ // state; cron/materializer reads cannot, so bind it explicitly while keeping+ // the project-scoped source descriptor intact.+ const descriptor = resolution.descriptor.isBuiltIn+ ? resolution.descriptor+ : { ...resolution.descriptor, dbConfigId };+ const adapter = createAdapter(descriptor);+ if (!adapter) {+ if (+ !descriptor.isBuiltIn &&+ descriptor.type !== "clickhouse"+ ) {+ const { TELEMETRY_SOURCE_ADAPTER_UNAVAILABLE } = await import(+ "@/constants/messages/en"+ );+ throw new Error(+ TELEMETRY_SOURCE_ADAPTER_UNAVAILABLE(descriptor.type)+ );+ }+ const builtin = dbConfig+ ? builtInDescriptor(dbConfig)+ : await resolveBuiltInDescriptor(dbConfigId);+ const fallback = createAdapter(builtin);+ if (!fallback) {+ throw new Error(+ "No telemetry adapter available (built-in ClickHouse factory missing)."+ );+ }+ return {+ adapter: fallback,+ descriptor: builtin,+ isBuiltIn: true,+ };+ }+ return {+ adapter,+ descriptor,+ isBuiltIn:+ descriptor.isBuiltIn || descriptor.type === "clickhouse",+ };+}++/**+ * Resolve whether the natural-language SQL chat feature is available for the+ * current request. Chat generates and runs raw ClickHouse SQL, so it is gated+ * to the built-in ClickHouse source; external sources return a finished,+ * explained unsupported state instead of silently failing.+ */+export async function isNativeSqlChatAvailable(+ options: ResolveTelemetrySourceOptions = {}+ ): Promise<{+ available: boolean;+ sourceType: string;+ sourceName: string;+ databaseConfigId?: string;+ }> {+ const signal = options.signal || "intelligence";+ const resolution = await resolveSignalSource(signal, options);+ let descriptor = resolution.descriptor;+ // With no explicit environment, preserve the legacy "default source"+ // behavior for an external project default. Otherwise a project with an+ // external telemetry default and no intelligence binding would incorrectly+ // appear to have native SQL available through the fallback ClickHouse DB.+ if (+ !options.environment &&+ resolution.via === "builtin" &&+ descriptor.isBuiltIn &&+ descriptor.dbConfigId+ ) {+ const defaultDescriptor = await resolveTelemetrySourceDescriptor({+ ...options,+ signal: undefined,+ });+ if (!defaultDescriptor.isBuiltIn && defaultDescriptor.type !== "clickhouse") {+ descriptor = defaultDescriptor;+ }+ }+ const isConfiguredClickHouse =+ descriptor.id !== "builtin:none" &&+ (sourceSupportsNativeSql(descriptor) || descriptor.isBuiltIn) &&+ Boolean(descriptor.dbConfigId);+ return {+ available: isConfiguredClickHouse,+ sourceType: descriptor.type,+ sourceName: descriptor.name,+ databaseConfigId: descriptor.dbConfigId,+ };+}diff --git a/src/client/src/middleware.ts b/src/client/src/middleware.tsindex 041afc35..80452e03 100644--- a/src/client/src/middleware.ts+++ b/src/client/src/middleware.ts@@ -33,19 +33,37 @@ export const config = {"/observability/:path*","/requests","/openground",+ "/openground/:path*","/exceptions","/prompt-hub",+ "/prompt-hub/:path*","/vault",+ "/vault/:path*","/settings/:path*","/telemetry-enabled","/home","/dashboards/:path*","/d/:path*","/fleet-hub",+ "/fleet-hub/:path*","/agents","/agents/:path*","/onboarding","/chat",- "/chat/:path*"+ "/chat/:path*",+ "/organisation",+ "/organisation/:path*",+ "/connectors",+ "/costs",+ "/evaluations",+ "/evaluations/:path*",+ "/pricing",+ "/manage-models",+ "/rule-engine",+ "/rule-engine/:path*",+ "/context",+ "/context/:path*",+ "/coding-agents",+ "/coding-agents/:path*",],};diff --git a/src/client/src/selectors/project.ts b/src/client/src/selectors/project.tsindex 964bacbf..6b2210e2 100644--- a/src/client/src/selectors/project.ts+++ b/src/client/src/selectors/project.ts@@ -4,4 +4,7 @@ export const getProjectList = (state: RootStore) => state.project.list;export const getCurrentProject = (state: RootStore) => state.project.current;+export const getCurrentProjectEnvironment = (state: RootStore) =>+ state.project.currentEnvironment;+export const getProjectIsLoading = (state: RootStore) => state.project.isLoading;diff --git a/src/client/src/store/filter.ts b/src/client/src/store/filter.tsindex 24eacb9f..cba2b0bc 100644--- a/src/client/src/store/filter.ts+++ b/src/client/src/store/filter.ts@@ -1,5 +1,5 @@import { merge, set } from "lodash";-import { addDays, addMonths, addWeeks } from "date-fns";+import { addDays, addWeeks } from "date-fns";import { lens } from "@dhmk/zustand-lens";import {AttributeKeys,@@ -52,11 +52,13 @@ export function getTimeLimitObject(set(object, `${keyPrefix}end`, currentDate);} else if (value === TIME_RANGE_TYPE["1M"]) {const currentDate = new Date();- set(object, `${keyPrefix}start`, addMonths(currentDate, -1));+ // Treat observability presets as fixed query durations. A calendar month+ // can be 31 days, which exceeds the common 30-day Tempo search ceiling.+ set(object, `${keyPrefix}start`, addDays(currentDate, -30));set(object, `${keyPrefix}end`, currentDate);} else if (value === TIME_RANGE_TYPE["3M"]) {const currentDate = new Date();- set(object, `${keyPrefix}start`, addMonths(currentDate, -3));+ set(object, `${keyPrefix}start`, addDays(currentDate, -90));set(object, `${keyPrefix}end`, currentDate);} else if (value === TIME_RANGE_TYPE["CUSTOM"]) {const start = extraParams?.start;@@ -146,7 +148,13 @@ export const filterStoreSlice: FilterStore = lens((setStore, getStore) => ({// invalidate the *available options* cache (`config` below)// while the user's chosen filters survive.selectedConfig: extraParams?.clearFilter- ? {}+ ? // Allow callers to wipe and seed in one update (e.g. preserve+ // agent-scoped serviceNames across signal changes). An empty+ // object still means a full clear.+ object.selectedConfig &&+ Object.keys(object.selectedConfig).length > 0+ ? object.selectedConfig+ : {}: object.selectedConfig? // Merge instead of replace so fields managed out-of-band// — notably `serviceNames`, which AgentScopeProvider setsdiff --git a/src/client/src/store/project.ts b/src/client/src/store/project.tsindex 51e16443..3d428aa3 100644--- a/src/client/src/store/project.ts+++ b/src/client/src/store/project.ts@@ -6,6 +6,7 @@ import { ProjectStore } from "@/types/store/project";const initialState = {list: undefined,current: undefined,+ currentEnvironment: undefined,isLoading: false,};@@ -35,6 +36,11 @@ export const projectStoreSlice: ProjectStore = lens((setStore, getStore) => ({current: updatedList.find((item) => item.id === project?.id),};}),+ setCurrentEnvironment: (environment) =>+ setStore(() => ({+ ...getStore(),+ currentEnvironment: environment,+ })),setIsLoading: (isLoading) =>setStore(() => ({...getStore(),diff --git a/src/client/src/types/form.ts b/src/client/src/types/form.tsindex 2efe7875..0655c8b2 100644--- a/src/client/src/types/form.ts+++ b/src/client/src/types/form.ts@@ -15,6 +15,7 @@ export interface CustomSelectProps {hasOtherOption?: boolean;name: string;id?: string;+ disabled?: boolean;}export type FieldTypes =diff --git a/src/client/src/types/manage-dashboard.ts b/src/client/src/types/manage-dashboard.tsindex efc14902..4027396c 100644--- a/src/client/src/types/manage-dashboard.ts+++ b/src/client/src/types/manage-dashboard.ts@@ -39,13 +39,42 @@ export type BoardWidget = {widget: Widget;};+/** Execution mode for a structured (non-SQL) widget query. */+export type WidgetStructuredMode = "list" | "aggregate" | "timeseries";++/**+ * Optional per-widget telemetry source reference (Grafana-style per-panel+ * datasource). When `sourceId` is set, the widget queries that source (must+ * belong to the current project / environment). When omitted, the widget+ * follows the project's per-signal binding. Raw `config.query` SQL runs only+ * on the built-in ClickHouse source; external sources must provide+ * `structuredQuery`. Use `builtin:<databaseConfigId>` to pin ClickHouse.+ */+export interface WidgetSourceConfig {+ /** Raw ClickHouse SQL (built-in source only). */+ query?: string;+ /** Explicit TelemetrySource id override, or `builtin:<dbConfigId>`. */+ sourceId?: string | null;+ /** Signal used for signal-aware routing and structured dispatch. */+ signal?: "traces" | "logs" | "metrics";+ /** Structured query for external (non-SQL) sources. */+ structuredQuery?: {+ mode?: WidgetStructuredMode;+ /** A vendor-agnostic OpenLITQuery (timeRange is injected at run time). */+ query: Record<string, any>;+ /** UI-only filter rows; incomplete drafts never enter the executable query. */+ draftFilters?: Array<Record<string, unknown>>;+ };+ [key: string]: any;+}+export interface Widget {id: string;title: string;description: string;type: string;properties: Record<string, any>;- config: Record<string, any>;+ config: WidgetSourceConfig & Record<string, any>;createdAt: string;updatedAt: string;totalBoards?: number;diff --git a/src/client/src/types/store/project.ts b/src/client/src/types/store/project.tsindex 9d3bb8b0..07213257 100644--- a/src/client/src/types/store/project.ts+++ b/src/client/src/types/store/project.ts@@ -11,9 +11,11 @@ export type ProjectWithMeta = {export type ProjectStore = {list?: ProjectWithMeta[];current?: ProjectWithMeta;+ currentEnvironment?: string;isLoading: boolean;setList: (list: ProjectWithMeta[]) => void;setCurrent: (project?: ProjectWithMeta) => void;+ setCurrentEnvironment: (environment?: string) => void;setIsLoading: (loading: boolean) => void;reset: () => void;};diff --git a/src/client/src/types/vault.ts b/src/client/src/types/vault.tsindex 952e75a0..3c27951e 100644--- a/src/client/src/types/vault.ts+++ b/src/client/src/types/vault.ts@@ -15,6 +15,7 @@ export interface SecretInput extends Omit<Secret, "id"> {export interface SecretGetFilters {databaseConfigId?: string;+ createdBy?: string;tags?: string[];key?: string;}diff --git a/src/client/src/utils/api.ts b/src/client/src/utils/api.tsindex 9c01169a..62ede9e6 100644--- a/src/client/src/utils/api.ts+++ b/src/client/src/utils/api.ts@@ -1,5 +1,6 @@import { useRootStore } from "@/store";import { OPENLIT_CONTEXT_HEADERS } from "@/constants/openlit-context";+import { isCacheableTelemetryUrl, withTelemetryRequestCache } from "@/utils/telemetry-request-cache";type GET_DATA = {body?: string;@@ -9,13 +10,27 @@ type GET_DATA = {};function getActiveDatabaseConfigId() {- const databaseConfigList = useRootStore.getState().databaseConfig.list || [];+ const list = useRootStore.getState().databaseConfig.list;+ const databaseConfigList = Array.isArray(list) ? list : [];return (databaseConfigList.find((item) => item.isCurrent)?.id ||databaseConfigList[0]?.id);}+/** Parse fetch body; successful responses must be JSON (not HTML login pages). */+function parseFetchBody(raw: string, ok: boolean) {+ if (!raw.trim()) return null;+ try {+ return JSON.parse(raw);+ } catch {+ if (ok) {+ throw new Error("Unexpected non-JSON response from server.");+ }+ return raw;+ }+}+function getOpenLitContextHeaders() {if (typeof window === "undefined") return {};@@ -24,15 +39,17 @@ function getOpenLitContextHeaders() {const organisationId = state.organisation.current?.id;const projectId = state.project.current?.id;const databaseConfigId = getActiveDatabaseConfigId();+ const environment = state.project.currentEnvironment;if (organisationId) headers[OPENLIT_CONTEXT_HEADERS.organisationId] = organisationId;if (projectId) headers[OPENLIT_CONTEXT_HEADERS.projectId] = projectId;if (databaseConfigId) headers[OPENLIT_CONTEXT_HEADERS.databaseConfigId] = databaseConfigId;+ if (environment) headers[OPENLIT_CONTEXT_HEADERS.environment] = environment;return headers;}-function getRequestHeaders(headers?: Record<string, string>) {+export function getRequestHeaders(headers?: Record<string, string>) {return {...getOpenLitContextHeaders(),...(headers || {}),@@ -40,22 +57,41 @@ function getRequestHeaders(headers?: Record<string, string>) {}export async function getData({ body, method = "POST", url, data }: GET_DATA) {- const hasBody = !!(body || data);- const res = await fetch(url, {- body: body || (data ? JSON.stringify(data) : undefined),- method,- headers: getRequestHeaders(- hasBody ? { "Content-Type": "application/json" } : undefined- ),- });- if (!res.ok) {- const error = await res.json();- throw new Error(- typeof error === "string" ? error : error?.error || error?.message || `Request failed (${res.status})`- );- }+ const payload = body || (data ? JSON.stringify(data) : undefined);+ const hasBody = !!payload;+ const environment = getOpenLitContextHeaders()[OPENLIT_CONTEXT_HEADERS.environment];+ // Keep environment out of the request URL, but include it in the client cache+ // identity so switching environments cannot reuse another environment's data.+ const cacheUrl = environment ? `${url}::${environment}` : url;- return res.json();+ return withTelemetryRequestCache(cacheUrl, payload, async () => {+ const controller = isCacheableTelemetryUrl(url) ? new AbortController() : undefined;+ const timeout = controller ? setTimeout(() => controller.abort(), 15_000) : undefined;+ try {+ const res = await fetch(url, {+ body: payload,+ method,+ signal: controller?.signal,+ headers: getRequestHeaders(+ hasBody ? { "Content-Type": "application/json" } : undefined+ ),+ });+ const raw = await res.text();+ const parsed = parseFetchBody(raw, res.ok);+ if (!res.ok) {+ const error = parsed as any;+ throw new Error(+ typeof error === "string"+ ? error+ : error?.err || error?.error || error?.message || `Request failed (${res.status})`+ );+ }+ if (parsed === null) throw new Error("The telemetry service returned an empty response.");+ return parsed;+ } finally {+ if (timeout !== undefined) clearTimeout(timeout);+ }+ });}type POST_DATA = {@@ -69,14 +105,16 @@ export async function postData({ url, data }: POST_DATA) {headers: getRequestHeaders({ "Content-Type": "application/json" }),body: JSON.stringify(data),});+ const raw = await res.text();+ const parsed = parseFetchBody(raw, res.ok);if (!res.ok) {- const error = await res.json();+ const error = parsed as any;throw new Error(- typeof error === "string" ? error : error?.error || error?.message || `Request failed (${res.status})`+ typeof error === "string" ? error : error?.err || error?.error || error?.message || `Request failed (${res.status})`);}-- return res.json();+ if (parsed === null) throw new Error("The telemetry service returned an empty response.");+ return parsed;}type DELETE_DATA = {@@ -88,12 +126,14 @@ export async function deleteData({ url }: DELETE_DATA) {method: "DELETE",headers: getRequestHeaders(),});+ const raw = await res.text();+ const parsed = parseFetchBody(raw, res.ok);if (!res.ok) {- const error = await res.json();+ const error = parsed as any;throw new Error(- typeof error === "string" ? error : error?.error || error?.message || `Request failed (${res.status})`+ typeof error === "string" ? error : error?.err || error?.error || error?.message || `Request failed (${res.status})`);}-- return res.json();+ if (parsed === null) throw new Error("The telemetry service returned an empty response.");+ return parsed;}diff --git a/src/client/src/utils/breadcrumbs.ts b/src/client/src/utils/breadcrumbs.tsindex 437c5d9a..cdda498f 100644--- a/src/client/src/utils/breadcrumbs.ts+++ b/src/client/src/utils/breadcrumbs.ts@@ -76,6 +76,27 @@ export const ROUTE_CONFIGS: RouteConfig[] = [getTitle: () => "Home",getBreadcrumbs: () => [],},++ // Costs+ {+ regex: /^\/costs$/,+ getTitle: () => getMessage().COSTS_TITLE,+ getBreadcrumbs: () => [],+ },+ {+ regex: /^\/pricing$/,+ getTitle: () => getMessage().COSTS_TAB_CONFIGURATION,+ getBreadcrumbs: () => [+ { title: getMessage().COSTS_TITLE, href: "/costs" },+ ],+ },+ {+ regex: /^\/manage-models$/,+ getTitle: () => getMessage().OPENGROUND_MANAGE_MODELS,+ getBreadcrumbs: () => [+ { title: getMessage().COSTS_TITLE, href: "/costs" },+ ],+ },// Costs{diff --git a/src/client/src/utils/hooks/useFetchWrapper.ts b/src/client/src/utils/hooks/useFetchWrapper.tsindex 38676333..45c5261c 100644--- a/src/client/src/utils/hooks/useFetchWrapper.ts+++ b/src/client/src/utils/hooks/useFetchWrapper.ts@@ -1,13 +1,35 @@import { get } from "lodash";-import { useCallback, useState } from "react";+import { useCallback, useRef, useState } from "react";import { deleteData, getData } from "@/utils/api";import { FetchWrapperProps } from "@/types/fetch-wrapper";+import { peekTelemetryRequestCache } from "@/utils/telemetry-request-cache";+/**+ * Fetch hook with Grafana-style stale-while-revalidate semantics:+ *+ * - A monotonic `requestId` guard means only the newest in-flight request may+ * write state, so a slow older response can never overwrite a newer one. We+ * intentionally do NOT abort the underlying fetch: the telemetry request+ * cache coalesces identical requests and shares a single promise, so+ * aborting one consumer would reject that shared promise for everyone+ * (this was the source of the "signal is aborted without reason" toast).+ * - On refetch (polling, filter re-stamps) we keep the previously loaded data+ * visible and revalidate in the background instead of blanking the widget.+ * - The skeleton (`isLoading`) is only shown on the very first load or when a+ * fresh cache seed is unavailable, so interval polling never empties the UI.+ */export default function useFetchWrapper<T>() {const [data, setData] = useState<T | null>(null);const [isLoading, setIsLoading] = useState<boolean>(false);const [error, setError] = useState<unknown>(null);const [isFetched, setIsFetched] = useState<boolean>(false);+ const requestIdRef = useRef(0);+ const dataRef = useRef<T | null>(null);++ const applyData = useCallback((value: T | null) => {+ dataRef.current = value;+ setData(value);+ }, []);const fireRequest = useCallback(async ({@@ -18,10 +40,29 @@ export default function useFetchWrapper<T>() {responseDataKey = "",successCb,}: FetchWrapperProps) => {+ const requestId = ++requestIdRef.current;+ const isCurrent = () => requestId === requestIdRef.current;+let response;let error;- setIsLoading(true);++ // Stale-while-revalidate: seed synchronously from the short-lived+ // telemetry cache when possible; otherwise keep whatever we already+ // have and only show the skeleton on the very first load.+ const cached =+ requestType !== "DELETE"+ ? peekTelemetryRequestCache<T>(url, body)+ : null;+ if (cached != null) {+ const seeded = get(cached, responseDataKey, cached) as T;+ applyData(seeded);+ setIsFetched(true);+ setIsLoading(false);+ } else if (dataRef.current == null) {+ setIsLoading(true);+ }setError(null);+try {if (requestType === "GET" ||@@ -40,35 +81,56 @@ export default function useFetchWrapper<T>() {});}+ // A newer request superseded us — let it own the state.+ if (!isCurrent()) return { response, error };+if (response.err) {- setData(null);setError(response.err);+ // Preserve last-good data on transient errors so the page+ // doesn't blank; only clear when we have nothing to show.+ if (dataRef.current == null) applyData(null);if (typeof failureCb === "function") failureCb(response.err);} else {const finalResponse = get(response, responseDataKey, response);- setData(finalResponse);+ applyData(finalResponse);if (typeof successCb === "function") successCb(finalResponse);}} catch (errorResp) {+ if (!isCurrent()) return { response, error };if (typeof errorResp === "string") {error = errorResp;- } else if (typeof errorResp === "object" && errorResp !== null){+ } else if (typeof errorResp === "object" && errorResp !== null) {error = (errorResp as any).message || (errorResp as any).error;}-- const updatedError = (error as any).toString().replaceAll("Error:", "");++ const updatedError = (error as any)?.toString().replaceAll("Error:", "");setError(updatedError);- setData(null);+ if (dataRef.current == null) applyData(null);if (typeof failureCb === "function") failureCb(updatedError);+ } finally {+ // Only the newest request settles the shared loading flags, so a+ // superseded/late response can never leave the UI stuck loading.+ if (isCurrent()) {+ setIsLoading(false);+ setIsFetched(true);+ }}- setIsLoading(false);- setIsFetched(true);-return { response, error };},- [setData, setError, setIsFetched, setIsLoading]+ [applyData]);- return { data, fireRequest, error, isFetched, isLoading };+ const reset = useCallback(() => {+ // A context change (project/environment) must not reuse the previous+ // context's rows or hide the next request behind stale-while-revalidate.+ requestIdRef.current += 1;+ dataRef.current = null;+ setData(null);+ setError(null);+ setIsFetched(false);+ setIsLoading(true);+ }, []);++ return { data, fireRequest, reset, error, isFetched, isLoading };}diff --git a/src/client/src/utils/hooks/useSignalCapabilities.ts b/src/client/src/utils/hooks/useSignalCapabilities.tsnew file mode 100644index 00000000..900d5e62--- /dev/null+++ b/src/client/src/utils/hooks/useSignalCapabilities.ts@@ -0,0 +1,109 @@+"use client";++import { useEffect, useState } from "react";++export type Signal = "traces" | "logs" | "metrics";++export interface ResolvedSignalCapability {+ sourceType: string;+ sourceName: string;+ isBuiltIn: boolean;+ capabilities: {+ traceTree?: boolean;+ spanEvents?: boolean;+ serverAggregation?: boolean;+ spanMutation?: boolean;+ distinctValues?: boolean;+ crossTraceSession?: boolean;+ maxLookbackMs?: number;+ maxTimeRangeMs?: number;+ rawQuery?: boolean;+ } | null;+}++export type SignalCapabilities = Record<Signal, ResolvedSignalCapability | null>;++const TTL_MS = 60_000;+const cache = new Map<+ string,+ { value: SignalCapabilities; expiresAt: number }+>();+const inFlight = new Map<string, Promise<SignalCapabilities | null>>();++function getCachedCapabilities(+ environment?: string+): SignalCapabilities | null {+ const entry = cache.get(environment || "current");+ return entry && entry.expiresAt > Date.now() ? entry.value : null;+}++async function fetchSignalCapabilities(+ environment?: string+): Promise<SignalCapabilities | null> {+ const cacheKey = environment || "current";+ const now = Date.now();+ const cached = cache.get(cacheKey);+ if (cached && cached.expiresAt > now) return cached.value;+ const pending = inFlight.get(cacheKey);+ if (pending) return pending;+ const request = (async () => {+ try {+ const params = new URLSearchParams();+ if (environment) params.set("environment", environment);+ const res = await fetch(+ `/api/telemetry-source${params.size ? `?${params.toString()}` : ""}`+ );+ if (!res.ok) return null;+ const body = await res.json();+ const value = (body?.signalCapabilities ?? null) as SignalCapabilities | null;+ if (value) {+ cache.set(cacheKey, { value, expiresAt: Date.now() + TTL_MS });+ }+ return value;+ } catch {+ return null;+ } finally {+ inFlight.delete(cacheKey);+ }+ })();+ inFlight.set(cacheKey, request);+ return request;+}++/**+ * Fetch (once, cached) the current project's resolved per-signal capabilities+ * so UI surfaces can gate honestly — showing a "not supported by this source"+ * state instead of erroring on an operation the bound source cannot serve.+ */+export function useSignalCapabilities(environment?: string): {+ capabilities: SignalCapabilities | null;+ loading: boolean;+} {+ const [capabilities, setCapabilities] = useState<SignalCapabilities | null>(+ () => getCachedCapabilities(environment)+ );+ const [loading, setLoading] = useState(!capabilities);++ useEffect(() => {+ let active = true;+ const cached = getCachedCapabilities(environment);+ setCapabilities(cached);+ setLoading(!cached);+ fetchSignalCapabilities(environment).then((value) => {+ if (!active) return;+ setCapabilities(value);+ setLoading(false);+ });+ return () => {+ active = false;+ };+ }, [environment]);++ return { capabilities, loading };+}++/** Test-only. */+export function __clearSignalCapabilitiesCache() {+ cache.clear();+ inFlight.clear();+}diff --git a/src/client/src/utils/log.ts b/src/client/src/utils/log.tsindex 8864c591..893756c1 100644--- a/src/client/src/utils/log.ts+++ b/src/client/src/utils/log.ts@@ -1,3 +1,18 @@-export function consoleLog(...rest: any) {- console.log(rest);+export function consoleLog(...rest: unknown[]) {+ // JSON.stringify escapes control characters so user-controlled values cannot+ // forge log lines (CodeQL js/log-injection). Fall back when args contain+ // circular structures (e.g. jsonStringify error path).+ try {+ console.log(JSON.stringify(rest));+ } catch {+ console.log(+ rest.map((value) => {+ try {+ return JSON.stringify(value);+ } catch {+ return Object.prototype.toString.call(value);+ }+ })+ );+ }}diff --git a/src/client/src/utils/telemetry-request-cache.ts b/src/client/src/utils/telemetry-request-cache.tsnew file mode 100644index 00000000..d5d0baf2--- /dev/null+++ b/src/client/src/utils/telemetry-request-cache.ts@@ -0,0 +1,140 @@+/**+ * Short-lived browser cache for read-only Telemetry / metrics POSTs.+ * Tab remounts and 1m poll windows reuse the last response instead of+ * re-triggering expensive external L1 sampling on every navigation.+ */++interface CacheEntry {+ value: unknown;+ expiresAt: number;+}++const store = new Map<string, CacheEntry>();+const inFlight = new Map<string, Promise<unknown>>();++const DEFAULT_TTL_MS = 45_000;+/** Floor relative-range timestamps so remounts within the poll window share a key. */+const TIME_QUANTUM_MS = 30_000;+const MAX_ENTRIES = 80;++const CACHEABLE_URL =+ /^\/api\/(metrics|telemetry)\b/;++function prune(now: number) {+ for (const [key, entry] of Array.from(store.entries())) {+ if (entry.expiresAt <= now) store.delete(key);+ }+ while (store.size > MAX_ENTRIES) {+ const oldest = store.keys().next().value;+ if (oldest === undefined) break;+ store.delete(oldest);+ }+}++/** True for Telemetry/metrics read routes that are safe to cache briefly. */+export function isCacheableTelemetryUrl(url: string): boolean {+ try {+ const path = url.startsWith("http") ? new URL(url).pathname : url;+ return CACHEABLE_URL.test(path);+ } catch {+ return false;+ }+}++function quantizeIso(value: unknown): unknown {+ if (typeof value !== "string" && !(value instanceof Date)) return value;+ const d = value instanceof Date ? value : new Date(value);+ if (Number.isNaN(d.getTime())) return value;+ const floored = Math.floor(d.getTime() / TIME_QUANTUM_MS) * TIME_QUANTUM_MS;+ return new Date(floored).toISOString();+}++/**+ * Normalize request bodies so relative time-range remounts (end=now()) share+ * a cache key within TIME_QUANTUM_MS. CUSTOM ranges with fixed ends are left+ * as-is after ISO normalization.+ */+export function normalizeTelemetryCacheBody(body: string | undefined): string {+ if (!body) return "";+ try {+ const parsed = JSON.parse(body) as Record<string, unknown>;+ const timeLimit = parsed.timeLimit as Record<string, unknown> | undefined;+ if (timeLimit && typeof timeLimit === "object") {+ const type = String(timeLimit.type || "");+ if (type && type !== "CUSTOM") {+ parsed.timeLimit = {+ ...timeLimit,+ start: quantizeIso(timeLimit.start),+ end: quantizeIso(timeLimit.end),+ };+ } else if (type === "CUSTOM") {+ parsed.timeLimit = {+ ...timeLimit,+ start:+ typeof timeLimit.start === "string" || timeLimit.start instanceof Date+ ? new Date(timeLimit.start as string | Date).toISOString()+ : timeLimit.start,+ end:+ typeof timeLimit.end === "string" || timeLimit.end instanceof Date+ ? new Date(timeLimit.end as string | Date).toISOString()+ : timeLimit.end,+ };+ }+ }+ return JSON.stringify(parsed);+ } catch {+ return body;+ }+}++function cacheKeyFor(url: string, body: string | undefined): string {+ return `${url}::${normalizeTelemetryCacheBody(body)}`;+}++/** Synchronous peek for stale-while-revalidate UI seeding. */+export function peekTelemetryRequestCache<T = unknown>(+ url: string,+ body: string | undefined+): T | null {+ if (!isCacheableTelemetryUrl(url)) return null;+ const key = cacheKeyFor(url, body);+ const hit = store.get(key);+ if (!hit || hit.expiresAt <= Date.now()) return null;+ return hit.value as T;+}++export async function withTelemetryRequestCache<T>(+ url: string,+ body: string | undefined,+ loader: () => Promise<T>,+ ttlMs: number = DEFAULT_TTL_MS+): Promise<T> {+ if (!isCacheableTelemetryUrl(url)) return loader();++ const key = cacheKeyFor(url, body);+ const now = Date.now();+ const hit = store.get(key);+ if (hit && hit.expiresAt > now) return hit.value as T;++ const pending = inFlight.get(key);+ if (pending) return pending as Promise<T>;++ const promise = (async () => {+ try {+ const value = await loader();+ store.set(key, { value, expiresAt: Date.now() + ttlMs });+ prune(Date.now());+ return value;+ } finally {+ inFlight.delete(key);+ }+ })();+ inFlight.set(key, promise);+ return promise;+}++/** Test-only. */+export function __clearTelemetryRequestCache() {+ store.clear();+ inFlight.clear();+}diff --git a/src/client/src/utils/validation.ts b/src/client/src/utils/validation.tsindex 3a0a4808..58b753b9 100644--- a/src/client/src/utils/validation.ts+++ b/src/client/src/utils/validation.ts@@ -1,6 +1,9 @@import net from "net";-export function validateDatabaseHost(host: string): {+export function validateDatabaseHost(+ host: string,+ options: { allowPrivateNetwork?: boolean } = {}+): {valid: boolean;error?: string;} {@@ -32,7 +35,7 @@ export function validateDatabaseHost(host: string): {const hostPart = trimmed.replace(/:\d+$/, "");if (net.isIP(hostPart)) {- if (!isAllowedIP(hostPart)) {+ if (!isAllowedIP(hostPart, options.allowPrivateNetwork === true)) {return {valid: false,error: "Private, loopback, and link-local IP addresses are not allowed",@@ -50,6 +53,9 @@ export function validateDatabaseHost(host: string): {ipv6.startsWith("fc00:") ||ipv6.startsWith("fd")) {+ if (options.allowPrivateNetwork === true && (ipv6 === "::1" || ipv6.startsWith("fc00:") || ipv6.startsWith("fd"))) {+ return { valid: true };+ }return {valid: false,error: "Private, loopback, and link-local IP addresses are not allowed",@@ -65,6 +71,9 @@ export function validateDatabaseHost(host: string): {];if (blockedHosts.includes(lowerHost)) {+ if (options.allowPrivateNetwork === true && lowerHost === "localhost") {+ return { valid: true };+ }return { valid: false, error: `Host "${hostPart}" is not allowed` };}@@ -75,14 +84,11 @@ export function validateDatabaseHost(host: string): {return { valid: true };}-function isAllowedIP(ip: string): boolean {+function isAllowedIP(ip: string, allowPrivateNetwork = false): boolean {const parts = ip.split(".").map(Number);if (parts.length !== 4) return true;- if (parts[0] === 127) return false;- if (parts[0] === 10) return false;- if (parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) return false;- if (parts[0] === 192 && parts[1] === 168) return false;+ if (parts[0] === 127 || parts[0] === 10 || (parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) || (parts[0] === 192 && parts[1] === 168)) return allowPrivateNetwork;if (parts[0] === 169 && parts[1] === 254) return false;if (parts.every((part) => part === 0)) return false;@@ -206,8 +212,13 @@ export function sanitizeErrorMessage(): string {if (!err) return fallback;- const message = typeof err === "string" ? err : (err as Error)?.message;+ let message = typeof err === "string" ? err : (err as Error)?.message;if (!message) return fallback;+ // `asaw` stringifies thrown Errors as "Error: …"; strip the redundant prefix+ // so API clients and toasts show the underlying message once.+ if (message.startsWith("Error: ")) {+ message = message.slice("Error: ".length);+ }if (message.includes("PrismaClient") ||diff --git a/src/dev-docker-compose.yml b/src/dev-docker-compose.ymlindex 8fbc238d..29397cdd 100644--- a/src/dev-docker-compose.yml+++ b/src/dev-docker-compose.yml@@ -28,6 +28,10 @@ services:context: .dockerfile: ./Dockerfilecontainer_name: openlit+ # Reach host-published Loki/Tempo/Prometheus via host.docker.internal+ # (Linux needs host-gateway; Docker Desktop already provides the alias).+ extra_hosts:+ - "host.docker.internal:host-gateway"environment:TELEMETRY_ENABLED: "true"INIT_DB_HOST: clickhouse