From 0199d8e2c4e3b22791cb917e273ea39053311f20 Mon Sep 17 00:00:00 2001 From: Rajat Agrawal Date: Fri, 21 Mar 2025 16:47:16 +0530 Subject: [PATCH] chore: Add spans to rts (#39848) ## Description > [!TIP] > _Add a TL;DR when the description is longer than 500 words or extremely technical (helps the content, marketing, and DevRel team)._ > > _Please also include relevant motivation and context. List any dependencies that are required for this change. Add links to Notion, Figma or any other documents that might be relevant to the PR._ Fixes #`Issue Number` _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="" ### :mag: Cypress test results > [!WARNING] > Tests have not run on the HEAD 9c7cd35dec40eb23a0ad9542ad5103997ae63620 yet >
Fri, 21 Mar 2025 11:06:06 UTC ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [ ] No ## Summary by CodeRabbit - **New Features** - Enhanced observability in key operations to improve error management and performance monitoring. These improvements help ensure smoother operation and quicker resolution of issues. - Introduced structured tracing functionality to monitor specific operations effectively. - Added new functions for managing tracing spans, enabling better monitoring and debugging capabilities. --- .../rts/src/controllers/Dsl/DslController.ts | 14 ++++++ app/client/packages/rts/src/utils/tracing.ts | 46 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 app/client/packages/rts/src/utils/tracing.ts diff --git a/app/client/packages/rts/src/controllers/Dsl/DslController.ts b/app/client/packages/rts/src/controllers/Dsl/DslController.ts index 7f0f062d47..5a6ec060cc 100644 --- a/app/client/packages/rts/src/controllers/Dsl/DslController.ts +++ b/app/client/packages/rts/src/controllers/Dsl/DslController.ts @@ -2,6 +2,7 @@ import type { Response, Request } from "express"; import BaseController from "@controllers/BaseController"; import { latestDSLVersion, migrateDSLToLatest } from "@services/DslService"; import { StatusCodes } from "http-status-codes"; +import { startSpan, endSpan } from "../../utils/tracing"; export default class DSLController extends BaseController { constructor() { @@ -9,11 +10,15 @@ export default class DSLController extends BaseController { } async migrateDSL(req: Request, res: Response) { + const span = startSpan("dsl-migration"); + try { const latestDSL = await migrateDSLToLatest(req.body); super.sendResponse(res, latestDSL); } catch (err) { + endSpan(span, err); + return super.sendError( res, this.serverErrorMessage, @@ -21,12 +26,19 @@ export default class DSLController extends BaseController { StatusCodes.INTERNAL_SERVER_ERROR, ); } + endSpan(span); } getLatestDSLVersion(req: Request, res: Response) { + const span = startSpan("get-latest-dsl-version"); + const childSpan = startSpan("version-check", {}, span); + try { super.sendResponse(res, { version: latestDSLVersion }); } catch (err) { + endSpan(childSpan, err); + endSpan(span, err); + return super.sendError( res, this.serverErrorMessage, @@ -34,5 +46,7 @@ export default class DSLController extends BaseController { StatusCodes.INTERNAL_SERVER_ERROR, ); } + endSpan(childSpan); + endSpan(span); } } diff --git a/app/client/packages/rts/src/utils/tracing.ts b/app/client/packages/rts/src/utils/tracing.ts new file mode 100644 index 0000000000..c77ab0ceb3 --- /dev/null +++ b/app/client/packages/rts/src/utils/tracing.ts @@ -0,0 +1,46 @@ +import { trace, context, SpanStatusCode } from "@opentelemetry/api"; +import type { Span } from "@opentelemetry/api"; + +const tracer = trace.getTracer("rts-tracer"); + +/** + * Creates and starts a span. If parentSpan is provided, creates a child span. + * @param name Name of the operation to trace + * @param attributes Optional attributes for the span + * @param parentSpan Optional parent span to create child span + * @returns Span object that must be ended when operation completes + */ +export function startSpan( + name: string, + attributes?: Record, + parentSpan?: Span, +): Span { + const ctx = parentSpan + ? trace.setSpan(context.active(), parentSpan) + : undefined; + const span = tracer.startSpan(name, {}, ctx); + + if (attributes) { + Object.entries(attributes).forEach(([key, value]) => { + span.setAttribute(key, value); + }); + } + + return span; +} + +/** + * Ends a span and sets error status if error is provided + * @param span Span to end + * @param error Optional error to set on span + */ +export function endSpan(span: Span, error?: Error): void { + if (error) { + span.setStatus({ + code: SpanStatusCode.ERROR, + message: error.message, + }); + } + + span.end(); +}