Skip to content

Commit cd133ba

Browse files
committed
add traces
1 parent 6edb605 commit cd133ba

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/servers/openai-mcp-server.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { z } from "zod";
88
import { getActiveSpan, WithSpan } from "../metrics/tracing/tracing-utils";
99
import zodToJsonSchema from "zod-to-json-schema";
1010
import { ToolSchema } from "@modelcontextprotocol/sdk/types.js";
11+
import { context, type Span, SpanStatusCode, trace } from "@opentelemetry/api";
1112

1213
const ToolInputSchema = ToolSchema.shape.inputSchema;
1314
export type ToolInput = z.infer<typeof ToolInputSchema>;
@@ -104,11 +105,17 @@ export class OpenAIDeepResearchMCPServer extends BaseMCPServer {
104105
// First check if the query is of the form "datasource:<id> <query-with-spaces>. The id is a string of numbers, letters, and hyphens."
105106
const dsregex = /^(?:datasource:(?<id>[A-Za-z0-9-]+)\s+)?(.+)$/;
106107
const deepResearchRegex = /^(?:is_deep_research:(?<isDeepResearch>true)\s+)?(.+)$/;
108+
const span = getActiveSpan();
107109
const match = dsregex.exec(query);
108110
const datasourceId = match?.groups?.id;
109111
const queryWithoutDatasourceId = match![2];
110112
const isDeepResearch = deepResearchRegex.exec(query)?.groups?.isDeepResearch;
111113
console.log("[DEBUG] callSearch: isDeepResearch: ", isDeepResearch, "datasourceId: ", datasourceId, "queryWithoutDatasourceId: ", queryWithoutDatasourceId);
114+
span?.setAttributes({
115+
is_deep_research: isDeepResearch,
116+
datasource_id: datasourceId,
117+
query_without_datasource_id: queryWithoutDatasourceId,
118+
});
112119

113120
// if (!isDeepResearch) {
114121
// // If the query is not for deep research, get the answer for the question
@@ -131,12 +138,15 @@ export class OpenAIDeepResearchMCPServer extends BaseMCPServer {
131138
// }
132139

133140
if (datasourceId) {
141+
span?.addEvent("get-relevant-questions-for-datasource-openai");
134142
const relevantQuestions = await this.getThoughtSpotService().getRelevantQuestions(queryWithoutDatasourceId, [datasourceId], "");
135143
if (relevantQuestions.error) {
144+
span?.setStatus({ code: SpanStatusCode.ERROR, message: relevantQuestions.error.message });
136145
return this.createErrorResponse(relevantQuestions.error.message, `Error getting relevant questions ${relevantQuestions.error.message}`);
137146
}
138147

139148
if (relevantQuestions.questions.length === 0) {
149+
span?.setStatus({ code: SpanStatusCode.OK, message: "No relevant questions found" });
140150
return this.createSuccessResponse("No relevant questions found");
141151
}
142152

@@ -146,6 +156,8 @@ export class OpenAIDeepResearchMCPServer extends BaseMCPServer {
146156
url: "",
147157
}));
148158

159+
span?.setStatus({ code: SpanStatusCode.OK, message: "Relevant questions found" });
160+
149161
return this.createStructuredContentSuccessResponse({ results }, "Relevant questions found");
150162
}
151163
// Search for datasources in case the query is not of the form "datasource:<id> <query-with-spaces>"
@@ -174,8 +186,10 @@ export class OpenAIDeepResearchMCPServer extends BaseMCPServer {
174186
const { id } = FetchInputSchema.parse(request.params.arguments);
175187
// id is of the form "<datasource-id>:<question>"
176188
const [datasourceId, question = ""] = id.split(":");
189+
const span = getActiveSpan();
177190
const answer = await this.getThoughtSpotService().getAnswerForQuestion(question, datasourceId, false);
178191
if (answer.error) {
192+
span?.setStatus({ code: SpanStatusCode.ERROR, message: answer.error.message });
179193
return this.createErrorResponse(answer.error.message, `Error getting answer ${answer.error.message}`);
180194
}
181195

@@ -185,6 +199,7 @@ export class OpenAIDeepResearchMCPServer extends BaseMCPServer {
185199
text: answer.data,
186200
url: `${this.ctx.props.instanceUrl}/#/insights/conv-assist?query=${question.trim()}&worksheet=${datasourceId}&executeSearch=true`,
187201
}
202+
span?.setStatus({ code: SpanStatusCode.OK, message: "Answer found" });
188203

189204
return this.createStructuredContentSuccessResponse(result, "Answer found");
190205
}

0 commit comments

Comments
 (0)