Skip to content

Commit

Permalink
fix(sdk): support custom metrics (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
doronkopit5 committed Sep 9, 2024
1 parent f9ff60d commit 7c77047
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/sample-app/src/sample_decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions packages/traceloop-sdk/src/lib/node-server-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
27 changes: 27 additions & 0 deletions packages/traceloop-sdk/src/lib/tracing/custom-metric.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
};

0 comments on commit 7c77047

Please sign in to comment.