|
| 1 | +import { ClickhouseClient } from '@hyperdx/common-utils/dist/clickhouse/node'; |
| 2 | +import { isRawSqlChartConfig } from '@hyperdx/common-utils/dist/guards'; |
| 3 | +import { SourceKind } from '@hyperdx/common-utils/dist/types'; |
| 4 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 5 | + |
| 6 | +import * as config from '@/config'; |
| 7 | +import { |
| 8 | + bulkInsertLogs, |
| 9 | + DEFAULT_DATABASE, |
| 10 | + DEFAULT_LOGS_TABLE, |
| 11 | + getLoggedInAgent, |
| 12 | + getServer, |
| 13 | +} from '@/fixtures'; |
| 14 | +import Connection from '@/models/connection'; |
| 15 | +import { LogSource, Source, SourceDocument } from '@/models/source'; |
| 16 | + |
| 17 | +import { callTool, createTestClient, getFirstText } from './mcpTestUtils'; |
| 18 | + |
| 19 | +describe('log body fallback for bare-text MCP filters', () => { |
| 20 | + const server = getServer(); |
| 21 | + let client: Client; |
| 22 | + let source: SourceDocument; |
| 23 | + let connectionId: string; |
| 24 | + let now: number; |
| 25 | + |
| 26 | + beforeAll(async () => { |
| 27 | + await server.start(); |
| 28 | + }); |
| 29 | + |
| 30 | + beforeEach(async () => { |
| 31 | + const { team, user } = await getLoggedInAgent(server); |
| 32 | + const connection = await Connection.create({ |
| 33 | + team: team._id, |
| 34 | + name: 'Body fallback', |
| 35 | + host: config.CLICKHOUSE_HOST, |
| 36 | + username: config.CLICKHOUSE_USER, |
| 37 | + password: config.CLICKHOUSE_PASSWORD, |
| 38 | + }); |
| 39 | + connectionId = connection.id; |
| 40 | + source = await Source.create({ |
| 41 | + kind: SourceKind.Log, |
| 42 | + team: team._id, |
| 43 | + connection: connection._id, |
| 44 | + name: 'Logs', |
| 45 | + from: { databaseName: DEFAULT_DATABASE, tableName: DEFAULT_LOGS_TABLE }, |
| 46 | + timestampValueExpression: 'Timestamp', |
| 47 | + bodyExpression: 'Body', |
| 48 | + }); |
| 49 | + client = await createTestClient({ |
| 50 | + teamId: team._id.toString(), |
| 51 | + userId: user._id.toString(), |
| 52 | + }); |
| 53 | + now = Date.now(); |
| 54 | + await bulkInsertLogs( |
| 55 | + [2, 7].flatMap(minutes => [ |
| 56 | + { |
| 57 | + Body: 'needle in body', |
| 58 | + ServiceName: 'api', |
| 59 | + SeverityText: 'error', |
| 60 | + LogAttributes: { message: 'other' }, |
| 61 | + Timestamp: new Date(now - minutes * 60_000), |
| 62 | + }, |
| 63 | + { |
| 64 | + Body: 'other', |
| 65 | + ServiceName: 'needle', |
| 66 | + SeverityText: 'info', |
| 67 | + LogAttributes: { message: 'needle in map' }, |
| 68 | + Timestamp: new Date(now - minutes * 60_000), |
| 69 | + }, |
| 70 | + ]), |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + afterEach(async () => { |
| 75 | + await client.close(); |
| 76 | + await server.clearDBs(); |
| 77 | + }); |
| 78 | + |
| 79 | + afterAll(async () => { |
| 80 | + await server.stop(); |
| 81 | + }); |
| 82 | + |
| 83 | + const currentWindow = () => ({ |
| 84 | + startTime: new Date(now - 5 * 60_000).toISOString(), |
| 85 | + endTime: new Date(now).toISOString(), |
| 86 | + }); |
| 87 | + |
| 88 | + it.each([ |
| 89 | + { implicitColumnExpression: undefined, expectedBody: 'needle in body' }, |
| 90 | + { implicitColumnExpression: '', expectedBody: 'needle in body' }, |
| 91 | + { implicitColumnExpression: 'ServiceName', expectedBody: 'other' }, |
| 92 | + ])( |
| 93 | + 'searches the correct field with implicitColumnExpression=$implicitColumnExpression', |
| 94 | + async ({ implicitColumnExpression, expectedBody }) => { |
| 95 | + await LogSource.updateOne( |
| 96 | + { _id: source._id }, |
| 97 | + implicitColumnExpression === undefined |
| 98 | + ? { $unset: { implicitColumnExpression: '' } } |
| 99 | + : { $set: { implicitColumnExpression } }, |
| 100 | + ); |
| 101 | + const result = await callTool(client, 'clickstack_search', { |
| 102 | + sourceId: source._id.toString(), |
| 103 | + where: 'needle', |
| 104 | + ...currentWindow(), |
| 105 | + }); |
| 106 | + expect(result.isError).toBeFalsy(); |
| 107 | + const output = JSON.parse(getFirstText(result)); |
| 108 | + expect(output.result.data).toEqual([ |
| 109 | + expect.objectContaining({ Body: expectedBody }), |
| 110 | + ]); |
| 111 | + }, |
| 112 | + ); |
| 113 | + |
| 114 | + it('uses a map-valued body expression for bare-text search', async () => { |
| 115 | + await LogSource.updateOne( |
| 116 | + { _id: source._id }, |
| 117 | + { $set: { bodyExpression: "LogAttributes['message']" } }, |
| 118 | + ); |
| 119 | + const result = await callTool(client, 'clickstack_search', { |
| 120 | + sourceId: source._id.toString(), |
| 121 | + where: 'needle', |
| 122 | + ...currentWindow(), |
| 123 | + }); |
| 124 | + expect(result.isError).toBeFalsy(); |
| 125 | + expect(JSON.parse(getFirstText(result)).result.data).toEqual([ |
| 126 | + expect.objectContaining({ Body: 'other' }), |
| 127 | + ]); |
| 128 | + }); |
| 129 | + |
| 130 | + it('rejects bare-text search without an implicit column or body expression', async () => { |
| 131 | + await LogSource.updateOne( |
| 132 | + { _id: source._id }, |
| 133 | + { $unset: { implicitColumnExpression: '', bodyExpression: '' } }, |
| 134 | + ); |
| 135 | + const result = await callTool(client, 'clickstack_search', { |
| 136 | + sourceId: source._id.toString(), |
| 137 | + where: 'needle', |
| 138 | + ...currentWindow(), |
| 139 | + }); |
| 140 | + expect(result.isError).toBe(true); |
| 141 | + expect(getFirstText(result)).toContain( |
| 142 | + 'Can not search bare text without an implicit column set.', |
| 143 | + ); |
| 144 | + }); |
| 145 | + |
| 146 | + it('does not introduce a body fallback for MCP trace searches', async () => { |
| 147 | + const traceSource = await Source.create({ |
| 148 | + kind: SourceKind.Trace, |
| 149 | + team: source.team, |
| 150 | + connection: source.connection, |
| 151 | + name: 'Traces', |
| 152 | + from: { databaseName: 'default', tableName: 'otel_traces' }, |
| 153 | + timestampValueExpression: 'Timestamp', |
| 154 | + spanNameExpression: 'SpanName', |
| 155 | + }); |
| 156 | + const result = await callTool(client, 'clickstack_search', { |
| 157 | + sourceId: traceSource._id.toString(), |
| 158 | + where: 'needle', |
| 159 | + ...currentWindow(), |
| 160 | + }); |
| 161 | + expect(result.isError).toBe(true); |
| 162 | + expect(getFirstText(result)).toContain( |
| 163 | + 'Can not search bare text without an implicit column set.', |
| 164 | + ); |
| 165 | + }); |
| 166 | + |
| 167 | + it('uses raw SQL tile hydration to filter by the log body expression', async () => { |
| 168 | + const saved = await callTool(client, 'clickstack_save_dashboard', { |
| 169 | + name: 'Body fallback', |
| 170 | + tiles: [ |
| 171 | + { |
| 172 | + name: 'Matching logs', |
| 173 | + config: { |
| 174 | + configType: 'sql', |
| 175 | + displayType: 'table', |
| 176 | + connectionId, |
| 177 | + sourceId: source._id.toString(), |
| 178 | + sqlTemplate: |
| 179 | + 'SELECT Body FROM $__sourceTable WHERE $__timeFilter(Timestamp) AND $__filters', |
| 180 | + }, |
| 181 | + }, |
| 182 | + ], |
| 183 | + }); |
| 184 | + expect(saved.isError).toBeFalsy(); |
| 185 | + const dashboard = JSON.parse(getFirstText(saved)); |
| 186 | + const query = jest.spyOn(ClickhouseClient.prototype, 'queryChartConfig'); |
| 187 | + try { |
| 188 | + const result = await callTool(client, 'clickstack_query_tile', { |
| 189 | + dashboardId: dashboard.id, |
| 190 | + tileId: dashboard.tiles[0].id, |
| 191 | + ...currentWindow(), |
| 192 | + }); |
| 193 | + expect(result.isError).toBeFalsy(); |
| 194 | + expect(query).toHaveBeenCalledTimes(1); |
| 195 | + const [queryOptions] = query.mock.calls[0]; |
| 196 | + if (!isRawSqlChartConfig(queryOptions.config)) { |
| 197 | + throw new Error('Expected a raw SQL tile query'); |
| 198 | + } |
| 199 | + const clickhouseClient = new ClickhouseClient({ |
| 200 | + host: config.CLICKHOUSE_HOST, |
| 201 | + username: config.CLICKHOUSE_USER, |
| 202 | + password: config.CLICKHOUSE_PASSWORD, |
| 203 | + }); |
| 204 | + // query_tile exposes variables but not runtime dashboard filters. |
| 205 | + const filtered = await clickhouseClient.queryChartConfig({ |
| 206 | + ...queryOptions, |
| 207 | + config: { |
| 208 | + ...queryOptions.config, |
| 209 | + filters: [{ type: 'lucene', condition: 'needle' }], |
| 210 | + }, |
| 211 | + }); |
| 212 | + expect(filtered.data).toEqual([{ Body: 'needle in body' }]); |
| 213 | + } finally { |
| 214 | + query.mockRestore(); |
| 215 | + } |
| 216 | + }); |
| 217 | + |
| 218 | + it('uses the fallback for pattern samples, counts and drill-down filters', async () => { |
| 219 | + const result = await callTool(client, 'clickstack_event_patterns', { |
| 220 | + sourceId: source._id.toString(), |
| 221 | + where: 'needle', |
| 222 | + ...currentWindow(), |
| 223 | + }); |
| 224 | + expect(result.isError).toBeFalsy(); |
| 225 | + const output = JSON.parse(getFirstText(result)); |
| 226 | + expect(output.summary.totalCount).toBe(1); |
| 227 | + expect(output.summary.sampledCount).toBe(1); |
| 228 | + expect(output.patterns).toHaveLength(1); |
| 229 | + expect(output.patterns[0].whereSnippet).toContain('Body'); |
| 230 | + const drilldown = await callTool(client, 'clickstack_search', { |
| 231 | + sourceId: source._id.toString(), |
| 232 | + where: output.patterns[0].whereSnippet, |
| 233 | + whereLanguage: 'sql', |
| 234 | + ...currentWindow(), |
| 235 | + }); |
| 236 | + expect(drilldown.isError).toBeFalsy(); |
| 237 | + expect(JSON.parse(getFirstText(drilldown)).result.data).toEqual([ |
| 238 | + expect.objectContaining({ Body: 'needle in body' }), |
| 239 | + ]); |
| 240 | + }); |
| 241 | + |
| 242 | + it('uses the fallback in both emerging-signals windows', async () => { |
| 243 | + const result = await callTool(client, 'clickstack_emerging_signals', { |
| 244 | + sourceId: source._id.toString(), |
| 245 | + where: 'needle', |
| 246 | + currentStartTime: currentWindow().startTime, |
| 247 | + currentEndTime: currentWindow().endTime, |
| 248 | + baselineStartTime: new Date(now - 10 * 60_000).toISOString(), |
| 249 | + baselineEndTime: currentWindow().startTime, |
| 250 | + }); |
| 251 | + expect(result.isError).toBeFalsy(); |
| 252 | + const output = JSON.parse(getFirstText(result)); |
| 253 | + expect(output.summary.currentWindow.total).toBe(1); |
| 254 | + expect(output.summary.currentWindow.sampled).toBe(1); |
| 255 | + expect(output.summary.baselineWindow.total).toBe(1); |
| 256 | + expect(output.summary.baselineWindow.sampled).toBe(1); |
| 257 | + }); |
| 258 | + |
| 259 | + it('uses the fallback in both event-deltas samples', async () => { |
| 260 | + const result = await callTool(client, 'clickstack_event_deltas', { |
| 261 | + sourceId: source._id.toString(), |
| 262 | + target: { ...currentWindow(), where: 'needle' }, |
| 263 | + baseline: { |
| 264 | + startTime: new Date(now - 10 * 60_000).toISOString(), |
| 265 | + endTime: currentWindow().startTime, |
| 266 | + where: 'needle', |
| 267 | + }, |
| 268 | + }); |
| 269 | + expect(result.isError).toBeFalsy(); |
| 270 | + const output = JSON.parse(getFirstText(result)); |
| 271 | + expect(output.summary.targetSampleCount).toBe(1); |
| 272 | + expect(output.summary.baselineSampleCount).toBe(1); |
| 273 | + }); |
| 274 | +}); |
0 commit comments