Skip to content

Commit 76dc2f5

Browse files
chore(web): Server side search telemetry (#652)
1 parent 7fc068f commit 76dc2f5

File tree

16 files changed

+119
-8
lines changed

16 files changed

+119
-8
lines changed

.env.development

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ AUTH_URL="http://localhost:3000"
2121

2222
DATA_CACHE_DIR=${PWD}/.sourcebot # Path to the sourcebot cache dir (ex. ~/sourcebot/.sourcebot)
2323
SOURCEBOT_PUBLIC_KEY_PATH=${PWD}/public.pem
24-
# CONFIG_PATH=${PWD}/config.json # Path to the sourcebot config file (if one exists)
24+
CONFIG_PATH=${PWD}/config.json # Path to the sourcebot config file (if one exists)
2525

2626
# Email
2727
# EMAIL_FROM_ADDRESS="" # The from address for transactional emails.

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
<!-- @NOTE: release new MCP version on package release! -->
11+
1012
### Added
1113
- Added `ALWAYS_INDEX_FILE_PATTERNS` environment variable to allow specifying a comma seperated list of glob patterns matching file paths that should always be indexed, regardless of size or # of trigrams. [#631](https://github.com/sourcebot-dev/sourcebot/pull/631)
1214
- Added button to explore menu to toggle cross-repository search. [#647](https://github.com/sourcebot-dev/sourcebot/pull/647)
15+
- Added server side telemetry for search metrics. [#652](https://github.com/sourcebot-dev/sourcebot/pull/652)
1316

1417
### Fixed
1518
- Fixed issue where single quotes could not be used in search queries. [#629](https://github.com/sourcebot-dev/sourcebot/pull/629)

packages/mcp/src/client.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export const search = async (request: SearchRequest): Promise<SearchResponse | S
88
method: 'POST',
99
headers: {
1010
'Content-Type': 'application/json',
11-
'X-Org-Domain': '~',
1211
...(env.SOURCEBOT_API_KEY ? { 'X-Sourcebot-Api-Key': env.SOURCEBOT_API_KEY } : {})
1312
},
1413
body: JSON.stringify(request)
@@ -26,7 +25,6 @@ export const listRepos = async (): Promise<ListRepositoriesResponse | ServiceErr
2625
method: 'GET',
2726
headers: {
2827
'Content-Type': 'application/json',
29-
'X-Org-Domain': '~',
3028
...(env.SOURCEBOT_API_KEY ? { 'X-Sourcebot-Api-Key': env.SOURCEBOT_API_KEY } : {})
3129
},
3230
}).then(response => response.json());
@@ -43,7 +41,6 @@ export const getFileSource = async (request: FileSourceRequest): Promise<FileSou
4341
method: 'POST',
4442
headers: {
4543
'Content-Type': 'application/json',
46-
'X-Org-Domain': '~',
4744
...(env.SOURCEBOT_API_KEY ? { 'X-Sourcebot-Api-Key': env.SOURCEBOT_API_KEY } : {})
4845
},
4946
body: JSON.stringify(request)

packages/mcp/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ server.tool(
7676
contextLines: env.DEFAULT_CONTEXT_LINES,
7777
isRegexEnabled: true,
7878
isCaseSensitivityEnabled: caseSensitive,
79+
source: 'mcp'
7980
});
8081

8182
if (isServiceError(response)) {

packages/mcp/src/schemas.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const searchOptionsSchema = z.object({
3131

3232
export const searchRequestSchema = z.object({
3333
query: z.string(), // The zoekt query to execute.
34+
source: z.string().optional(), // The source of the search request.
3435
...searchOptionsSchema.shape,
3536
});
3637

packages/web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
"openai": "^4.98.0",
157157
"parse-diff": "^0.11.1",
158158
"posthog-js": "^1.161.5",
159+
"posthog-node": "^5.15.0",
159160
"pretty-bytes": "^6.1.1",
160161
"psl": "^1.15.0",
161162
"react": "^19.2.1",

packages/web/src/app/[domain]/components/searchBar/useSuggestionsData.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const useSuggestionsData = ({
5555
query: `file:${suggestionQuery}`,
5656
matches: 15,
5757
contextLines: 1,
58+
source: 'search-bar-file-suggestions'
5859
}),
5960
select: (data): Suggestion[] => {
6061
if (isServiceError(data)) {
@@ -75,6 +76,7 @@ export const useSuggestionsData = ({
7576
query: `sym:${suggestionQuery.length > 0 ? suggestionQuery : ".*"}`,
7677
matches: 15,
7778
contextLines: 1,
79+
source: 'search-bar-symbol-suggestions'
7880
}),
7981
select: (data): Suggestion[] => {
8082
if (isServiceError(data)) {

packages/web/src/app/[domain]/search/useStreamedSearch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ export const useStreamedSearch = ({ query, matches, contextLines, whole, isRegex
129129
whole,
130130
isRegexEnabled,
131131
isCaseSensitivityEnabled,
132-
}),
132+
source: 'sourcebot-web-client'
133+
} satisfies SearchRequest),
133134
signal: abortControllerRef.current.signal,
134135
});
135136

packages/web/src/app/api/(server)/search/route.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { search, searchRequestSchema } from "@/features/search";
44
import { isServiceError } from "@/lib/utils";
55
import { NextRequest } from "next/server";
66
import { schemaValidationError, serviceErrorResponse } from "@/lib/serviceError";
7+
import { captureEvent } from "@/lib/posthog";
78

89
export const POST = async (request: NextRequest) => {
910
const body = await request.json();
@@ -16,8 +17,14 @@ export const POST = async (request: NextRequest) => {
1617

1718
const {
1819
query,
20+
source,
1921
...options
2022
} = parsed.data;
23+
24+
await captureEvent('api_code_search_request', {
25+
source: source ?? 'unknown',
26+
type: 'blocking',
27+
});
2128

2229
const response = await search({
2330
queryType: 'string',
@@ -28,5 +35,6 @@ export const POST = async (request: NextRequest) => {
2835
if (isServiceError(response)) {
2936
return serviceErrorResponse(response);
3037
}
38+
3139
return Response.json(response);
3240
}

packages/web/src/app/api/(server)/stream_search/route.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use server';
22

33
import { streamSearch, searchRequestSchema } from '@/features/search';
4+
import { captureEvent } from '@/lib/posthog';
45
import { schemaValidationError, serviceErrorResponse } from '@/lib/serviceError';
56
import { isServiceError } from '@/lib/utils';
67
import { NextRequest } from 'next/server';
@@ -15,9 +16,15 @@ export const POST = async (request: NextRequest) => {
1516

1617
const {
1718
query,
19+
source,
1820
...options
1921
} = parsed.data;
2022

23+
await captureEvent('api_code_search_request', {
24+
source: source ?? 'unknown',
25+
type: 'streamed',
26+
});
27+
2128
const stream = await streamSearch({
2229
queryType: 'string',
2330
query,

0 commit comments

Comments
 (0)