diff --git a/CHANGELOG.md b/CHANGELOG.md index 66222a33..190ff96a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [0.11.2](https://github.com/traceloop/openllmetry-js/compare/v0.11.1...v0.11.2) (2024-09-10) + +### Bug Fixes + +- **sdk:** support custom metrics ([#446](https://github.com/traceloop/openllmetry-js/issues/446)) ([7c77047](https://github.com/traceloop/openllmetry-js/commit/7c770478de41ac5fb934abf7cdeef2abf9c4e018)) + ## [0.11.1](https://github.com/traceloop/openllmetry-js/compare/v0.11.0...v0.11.1) (2024-08-31) ### Bug Fixes diff --git a/lerna.json b/lerna.json index 40edc7b8..068907e4 100644 --- a/lerna.json +++ b/lerna.json @@ -1,6 +1,6 @@ { "$schema": "node_modules/lerna/schemas/lerna-schema.json", - "version": "0.11.1", + "version": "0.11.2", "packages": ["packages/*"], "useNx": true } diff --git a/package-lock.json b/package-lock.json index 2185c519..b35dc5ce 100644 --- a/package-lock.json +++ b/package-lock.json @@ -30361,7 +30361,7 @@ }, "packages/traceloop-sdk": { "name": "@traceloop/node-server-sdk", - "version": "0.11.1", + "version": "0.11.2", "license": "Apache-2.0", "dependencies": { "@opentelemetry/exporter-trace-otlp-proto": "^0.53.0", diff --git a/packages/sample-app/src/sample_decorators.ts b/packages/sample-app/src/sample_decorators.ts index 235048df..389b2d2c 100644 --- a/packages/sample-app/src/sample_decorators.ts +++ b/packages/sample-app/src/sample_decorators.ts @@ -31,6 +31,7 @@ class SampleOpenAI { prompt: `Tell me a joke about ${jokeSubject}`, model: "gpt-3.5-turbo-instruct", }); + traceloop.reportCustomMetric("test_metric", 50.2); return completion.choices[0].text; } diff --git a/packages/traceloop-sdk/CHANGELOG.md b/packages/traceloop-sdk/CHANGELOG.md index 116c1d89..2c345b13 100644 --- a/packages/traceloop-sdk/CHANGELOG.md +++ b/packages/traceloop-sdk/CHANGELOG.md @@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [0.11.2](https://github.com/traceloop/openllmetry-js/compare/v0.11.1...v0.11.2) (2024-09-10) + +### Bug Fixes + +- **sdk:** support custom metrics ([#446](https://github.com/traceloop/openllmetry-js/issues/446)) ([7c77047](https://github.com/traceloop/openllmetry-js/commit/7c770478de41ac5fb934abf7cdeef2abf9c4e018)) + ## [0.11.1](https://github.com/traceloop/openllmetry-js/compare/v0.11.0...v0.11.1) (2024-08-31) ### Bug Fixes diff --git a/packages/traceloop-sdk/package.json b/packages/traceloop-sdk/package.json index 5e65005d..00adf235 100644 --- a/packages/traceloop-sdk/package.json +++ b/packages/traceloop-sdk/package.json @@ -1,6 +1,6 @@ { "name": "@traceloop/node-server-sdk", - "version": "0.11.1", + "version": "0.11.2", "description": "Traceloop Software Development Kit (SDK) for Node.js", "main": "dist/index.js", "module": "dist/index.mjs", diff --git a/packages/traceloop-sdk/src/lib/node-server-sdk.ts b/packages/traceloop-sdk/src/lib/node-server-sdk.ts index f09a33b3..7d9d27e9 100644 --- a/packages/traceloop-sdk/src/lib/node-server-sdk.ts +++ b/packages/traceloop-sdk/src/lib/node-server-sdk.ts @@ -8,6 +8,7 @@ export * from "./tracing/decorators"; export * from "./tracing/manual"; export * from "./tracing/association"; export * from "./tracing/score"; +export * from "./tracing/custom-metric"; export * from "./prompts"; initInstrumentations(); diff --git a/packages/traceloop-sdk/src/lib/tracing/custom-metric.ts b/packages/traceloop-sdk/src/lib/tracing/custom-metric.ts new file mode 100644 index 00000000..fd84db6e --- /dev/null +++ b/packages/traceloop-sdk/src/lib/tracing/custom-metric.ts @@ -0,0 +1,27 @@ +import { context, diag, trace } from "@opentelemetry/api"; + +/** + * Reports a custom metric to the current active span. + * + * This function allows you to add a custom metric to the current span in the trace. + * If there is no active span, a warning will be logged. + * + * @param {string} metricName - The name of the custom metric. + * @param {number} metricValue - The numeric value of the custom metric. + * + * @example + * reportCustomMetric('processing_time', 150); + */ +export const reportCustomMetric = (metricName: string, metricValue: number) => { + const currentContext = context.active(); + const currentSpan = trace.getSpan(currentContext); + + if (currentSpan) { + currentSpan.setAttribute( + `traceloop.custom_metric.${metricName}`, + metricValue, + ); + } else { + diag.warn(`No active span found to report custom metric: ${metricName}`); + } +};