Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(tracing): add a sql editor to the tracing sql tab (#105082)

Jon McCallum5b1b702ad410
diff --git a/products/tracing/frontend/TracingScene.tsx b/products/tracing/frontend/TracingScene.tsx
index 95feb0fe3b0a..6247f65ab22d 100644
--- a/products/tracing/frontend/TracingScene.tsx
+++ b/products/tracing/frontend/TracingScene.tsx
@@ -22,6 +22,7 @@ import { ProductIntentContext, ProductKey } from '~/queries/schema/schema-genera
import { ComparisonBar } from './components/Comparison/ComparisonBar'
import { FacetRail } from './components/FacetRail/FacetRail'
import { TraceDrawer } from './components/TraceDrawer/TraceDrawer'
+import { TracingSqlEditor } from './components/TracingSqlEditor/TracingSqlEditor'
import { VirtualizedSpanList } from './components/VirtualizedSpanList/VirtualizedSpanList'
import { TRACING_DISPLAY_TIMEZONE } from './dateFormats'
import { tracingEmptyState } from './emptyState/tracingEmptyState'
@@ -327,6 +328,7 @@ function TracingSceneContents(): JSX.Element {
</div>
</div>
</div>
+ {activeSceneTab === 'sql' && <TracingSqlEditor id={TRACING_SCENE_VIEWER_ID} />}
<TraceDrawer
isOpen={isTraceOpen}
traceId={selectedTraceId}
diff --git a/products/tracing/frontend/components/TracingSqlEditor/TracingSqlEditor.tsx b/products/tracing/frontend/components/TracingSqlEditor/TracingSqlEditor.tsx
new file mode 100644
index 000000000000..511f0c6d5c0e
--- /dev/null
+++ b/products/tracing/frontend/components/TracingSqlEditor/TracingSqlEditor.tsx
@@ -0,0 +1,55 @@
+import { useActions, useValues } from 'kea'
+import { useEffect } from 'react'
+
+import { SQLEditor } from 'scenes/data-warehouse/editor/SQLEditor'
+import { sqlEditorLogic } from 'scenes/data-warehouse/editor/sqlEditorLogic'
+import { SQLEditorMode } from 'scenes/data-warehouse/editor/sqlEditorModes'
+
+import { NodeKind } from '~/queries/schema/schema-general'
+import { ChartDisplayType } from '~/types'
+
+import { getTracingSqlEditorTabId, tracingSceneLogic } from '../../tracingSceneLogic'
+
+// Spans are only registered under the `posthog.` namespace, so the table name must stay qualified.
+const DEFAULT_TRACING_QUERY = `SELECT timestamp, service_name, name, duration_nano / 1000000 AS duration_ms, status_code, trace_id, span_id
+FROM posthog.trace_spans
+WHERE timestamp > now() - INTERVAL 1 HOUR
+ORDER BY timestamp DESC
+LIMIT 100`
+
+export interface TracingSqlEditorProps {
+ id: string
+}
+
+export const TracingSqlEditor = ({ id }: TracingSqlEditorProps): JSX.Element => {
+ const sqlEditorTabId = getTracingSqlEditorTabId(id)
+ const { keepSqlEditorMounted } = useActions(tracingSceneLogic)
+ const logic = sqlEditorLogic({ tabId: sqlEditorTabId, mode: SQLEditorMode.Embedded })
+ const { queryInput } = useValues(logic)
+ const { setQueryInput, setSourceQuery, runQuery } = useActions(logic)
+
+ useEffect(() => {
+ keepSqlEditorMounted(sqlEditorTabId)
+ }, [sqlEditorTabId]) // eslint-disable-line react-hooks/exhaustive-deps
+
+ useEffect(() => {
+ if (queryInput === null) {
+ setQueryInput(DEFAULT_TRACING_QUERY)
+ setSourceQuery({
+ kind: NodeKind.DataVisualizationNode,
+ source: {
+ kind: NodeKind.HogQLQuery,
+ query: DEFAULT_TRACING_QUERY,
+ },
+ display: ChartDisplayType.ActionsTable,
+ })
+ runQuery(DEFAULT_TRACING_QUERY)
+ }
+ }, [queryInput]) // eslint-disable-line react-hooks/exhaustive-deps
+
+ return (
+ <div className="flex flex-col flex-1 min-h-0 min-w-0 border rounded overflow-hidden">
+ <SQLEditor tabId={sqlEditorTabId} mode={SQLEditorMode.Embedded} defaultShowDatabaseTree={false} />
+ </div>
+ )
+}
diff --git a/products/tracing/frontend/components/TracingSqlEditor/tracingSqlEditorTrackingLogic.ts b/products/tracing/frontend/components/TracingSqlEditor/tracingSqlEditorTrackingLogic.ts
new file mode 100644
index 000000000000..52ab854a05d5
--- /dev/null
+++ b/products/tracing/frontend/components/TracingSqlEditor/tracingSqlEditorTrackingLogic.ts
@@ -0,0 +1,100 @@
+import { MakeLogicType, connect, kea, key, listeners, path, props } from 'kea'
+import posthog from 'posthog-js'
+
+import { SaveAsMenuItem } from 'scenes/data-warehouse/editor/editorSceneLogic'
+import { sqlEditorLogic } from 'scenes/data-warehouse/editor/sqlEditorLogic'
+import { SQLEditorMode } from 'scenes/data-warehouse/editor/sqlEditorModes'
+
+export interface TracingSqlEditorTrackingLogicProps {
+ sqlEditorTabId: string
+}
+
+const captureSaved = (target: SaveAsMenuItem['action']): void => {
+ posthog.capture('tracing sql query saved', { target })
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface tracingSqlEditorTrackingLogicActions {
+ sqlEditorRunQuery: (
+ queryOverride?: string | undefined,
+ switchTab?: boolean | undefined
+ ) => {
+ queryOverride: string | undefined
+ switchTab: boolean | undefined
+ } // sqlEditorLogic
+ sqlEditorSaveAsEndpointSubmit: (
+ name: string,
+ description?: string | undefined,
+ queryOverride?: string | undefined
+ ) => {
+ description: string | undefined
+ name: string
+ queryOverride: string | undefined
+ } // sqlEditorLogic
+ sqlEditorSaveAsInsightSubmit: (
+ name: string,
+ queryOverride?: string | undefined
+ ) => {
+ name: string
+ queryOverride: string | undefined
+ } // sqlEditorLogic
+ sqlEditorSaveAsViewSubmit: (
+ name: string,
+ materializeAfterSave?: any,
+ fromDraft?: string | undefined,
+ folderId?: string | null | undefined,
+ isTest?: any,
+ queryOverride?: string | undefined,
+ incremental?: import('~/types').DataWarehouseSavedQueryIncremental | undefined
+ ) => {
+ folderId: string | null | undefined
+ fromDraft: string | undefined
+ incremental: import('~/types').DataWarehouseSavedQueryIncremental | undefined
+ isTest: any
+ materializeAfterSave: any
+ name: string
+ queryOverride: string | undefined
+ } // sqlEditorLogic
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface tracingSqlEditorTrackingLogicMeta {
+ key: string
+}
+
+export type tracingSqlEditorTrackingLogicType = MakeLogicType<
+ {},
+ tracingSqlEditorTrackingLogicActions,
+ TracingSqlEditorTrackingLogicProps,
+ tracingSqlEditorTrackingLogicMeta
+>
+
+export const tracingSqlEditorTrackingLogic = kea<tracingSqlEditorTrackingLogicType>([
+ path(['products', 'tracing', 'frontend', 'components', 'TracingSqlEditor', 'tracingSqlEditorTrackingLogic']),
+ props({} as TracingSqlEditorTrackingLogicProps),
+ key((props) => props.sqlEditorTabId),
+ connect((props: TracingSqlEditorTrackingLogicProps) => ({
+ actions: [
+ sqlEditorLogic({ tabId: props.sqlEditorTabId, mode: SQLEditorMode.Embedded }),
+ [
+ 'runQuery as sqlEditorRunQuery',
+ 'saveAsViewSubmit as sqlEditorSaveAsViewSubmit',
+ 'saveAsInsightSubmit as sqlEditorSaveAsInsightSubmit',
+ 'saveAsEndpointSubmit as sqlEditorSaveAsEndpointSubmit',
+ ],
+ ],
+ })),
+ listeners(({ cache }) => ({
+ sqlEditorRunQuery: () => {
+ // Skips the editor's auto-run with the default query on first mount.
+ if (!cache.firstRunSeen) {
+ cache.firstRunSeen = true
+ return
+ }
+ posthog.capture('tracing sql query run')
+ },
+ sqlEditorSaveAsViewSubmit: () => captureSaved('view'),
+ sqlEditorSaveAsInsightSubmit: () => captureSaved('insight'),
+ sqlEditorSaveAsEndpointSubmit: () => captureSaved('endpoint'),
+ })),
+])
diff --git a/products/tracing/frontend/tracingSceneLogic.ts b/products/tracing/frontend/tracingSceneLogic.ts
index a46e237f3090..853bdd0dff8f 100644
--- a/products/tracing/frontend/tracingSceneLogic.ts
+++ b/products/tracing/frontend/tracingSceneLogic.ts
@@ -2,18 +2,22 @@ import { deepEqual as equal } from 'fast-equals'
import { MakeLogicType, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { router, urlToAction } from 'kea-router'
import { subscriptions } from 'kea-subscriptions'
+import posthog from 'posthog-js'
import { DEFAULT_UNIVERSAL_GROUP_FILTER } from 'lib/components/UniversalFilters/constants'
import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic, type FeatureFlagsSet } from 'lib/logic/featureFlagLogic'
import { trackedActionToUrl } from 'lib/logic/scenes/trackedActionToUrl'
import { parseTagsFilter } from 'lib/utils/url'
+import { sqlEditorLogic } from 'scenes/data-warehouse/editor/sqlEditorLogic'
+import { SQLEditorMode } from 'scenes/data-warehouse/editor/sqlEditorModes'
import { Params } from 'scenes/sceneTypes'
import { Breadcrumb } from '~/types'
import type { AggregatedSpanRow, DateRange, SpanTreeNode } from '../../../frontend/src/queries/schema/schema-general'
import type { UniversalFiltersGroup } from '../../../frontend/src/types'
+import { tracingSqlEditorTrackingLogic } from './components/TracingSqlEditor/tracingSqlEditorTrackingLogic'
import type { TracingDurationHistogramData, VisibleDurationRange } from './durationBuckets'
import type { TracingLatencyHeatmapData } from './durationBuckets'
import type { ErrorScope } from './errorCorrelation'
@@ -54,6 +58,8 @@ const TRACING_SCENE_TABS = ['viewer', 'sql'] as const
export type TracingSceneTab = (typeof TRACING_SCENE_TABS)[number]
const DEFAULT_SCENE_TAB: TracingSceneTab = 'viewer'
+export const getTracingSqlEditorTabId = (id: string): string => `tracing-sql-editor-${id}`
+
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface tracingSceneLogicValues {
featureFlags: FeatureFlagsSet // featureFlagLogic
@@ -254,6 +260,9 @@ export interface tracingSceneLogicActions {
selectSpan: (spanId: string | null) => {
spanId: string | null
} // tracingViewerLogic
+ keepSqlEditorMounted: (editorTabId: string) => {
+ editorTabId: string
+ }
selectSceneTab: (tab: TracingSceneTab) => {
tab: 'sql' | 'viewer'
}
@@ -390,6 +399,7 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
setDisplayMode: (mode: TracingDisplayMode) => ({ mode }),
setSceneTab: (tab: TracingSceneTab) => ({ tab }),
selectSceneTab: (tab: TracingSceneTab) => ({ tab }),
+ keepSqlEditorMounted: (editorTabId: string) => ({ editorTabId }),
}),
reducers({
@@ -444,8 +454,26 @@ export const tracingSceneLogic = kea<tracingSceneLogicType>([
],
}),
- listeners(({ actions, values }) => ({
+ listeners(({ actions, values, cache }) => ({
+ keepSqlEditorMounted: ({ editorTabId }) => {
+ if (cache.sqlEditorTabId === editorTabId) {
+ return
+ }
+ cache.unmountSqlEditor?.()
+ cache.sqlEditorTabId = editorTabId
+ // Never unmounted, so the user's query survives leaving and re-entering /tracing.
+ const unmountEditor = sqlEditorLogic({ tabId: editorTabId, mode: SQLEditorMode.Embedded }).mount()
+ // Mounted with the editor, so its skip of the one auto-run survives tab switches.
+ const unmountTracking = tracingSqlEditorTrackingLogic({ sqlEditorTabId: editorTabId }).mount()
+ cache.unmountSqlEditor = () => {
+ unmountTracking()
+ unmountEditor()
+ }
+ },
selectSceneTab: ({ tab }) => {
+ if (tab === 'sql' && values.sceneTab !== 'sql') {
+ posthog.capture('tracing sql tab opened')
+ }
actions.setSceneTab(tab)
actions.syncUrl()
},