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=""
### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results -->
> [!WARNING]
> Tests have not run on the HEAD
9c7cd35dec40eb23a0ad9542ad5103997ae63620 yet
> <hr>Fri, 21 Mar 2025 11:06:06 UTC
<!-- end of auto-generated comment: Cypress test results -->
## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## 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.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
parent
09d8a4deab
commit
0199d8e2c4
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
46
app/client/packages/rts/src/utils/tracing.ts
Normal file
46
app/client/packages/rts/src/utils/tracing.ts
Normal file
|
|
@ -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<string, string | number | boolean>,
|
||||
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();
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user