diff --git a/examples/tracing.ts b/examples/tracing.ts index f816254..5e2415f 100644 --- a/examples/tracing.ts +++ b/examples/tracing.ts @@ -42,10 +42,9 @@ const happify = fn({ const span: Span = { uuid: uuid.v4(), name: "Translate", - start_time: new Date(), input: JSON.stringify(input), } - await client.traces.startSpan(span); + await client.spans.startSpan(span); // Call translate and happify like any other function const result = await translate(input); @@ -53,8 +52,7 @@ const happify = fn({ const happified = await happify(result); span.output = JSON.stringify(happified); - span.end_time = new Date(); - await client.traces.endSpan(span); + await client.spans.endSpan(span); console.log(happified); })(); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 862a962..689165c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "opperai", - "version": "0.3.1", + "version": "0.4.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "opperai", - "version": "0.3.1", + "version": "0.4.2", "license": "ISC", "dependencies": { "uuid": "^9.0.1", diff --git a/package.json b/package.json index f7ba0f5..0369cc2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opperai", - "version": "0.4.1", + "version": "0.4.2", "description": "The official Node library for the OpperAi API", "main": "dist/index.js", "types": "dist/index.js", diff --git a/src/spans.ts b/src/spans.ts index 73304e7..5fee680 100644 --- a/src/spans.ts +++ b/src/spans.ts @@ -19,14 +19,20 @@ export function getCurrentSpanId(): string | undefined { class Spans extends APIResource { /** - * This method is used to start a new span. - * It sends a POST request to the spans endpoint with the provided span data. + * Helper method to start a new span and set the current span context. * @param span - The span data to be created. * @returns A promise that resolves to the UUID of the created span. * @throws {APIError} If the response status is not 200. */ public async startSpan(span: Span) { const spanId = span.uuid; + if (!span.start_time) { + span.start_time = new Date(); + } + const currentSpanId = getCurrentSpanId(); + if (currentSpanId && !span.parent_uuid) { + span.parent_uuid = currentSpanId; + } spanContextStorage.enterWith({ spanId }); return this.create(span); } @@ -39,6 +45,12 @@ class Spans extends APIResource { * @throws {APIError} If the response status is not 200. */ public async endSpan(span: Span) { + if (!span.end_time) { + span.end_time = new Date(); + } + if (span.parent_uuid) { + spanContextStorage.run({ spanId: span.parent_uuid }, () => { }); + } spanContextStorage.run({ spanId: '' }, () => { }); return this.update(span.uuid, span); }