File tree Expand file tree Collapse file tree 3 files changed +29
-0
lines changed
Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ class SampleOpenAI {
3131 prompt : `Tell me a joke about ${ jokeSubject } ` ,
3232 model : "gpt-3.5-turbo-instruct" ,
3333 } ) ;
34+ traceloop . reportCustomMetric ( "test_metric" , 50.2 ) ;
3435
3536 return completion . choices [ 0 ] . text ;
3637 }
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ export * from "./tracing/decorators";
88export * from "./tracing/manual" ;
99export * from "./tracing/association" ;
1010export * from "./tracing/score" ;
11+ export * from "./tracing/custom-metric" ;
1112export * from "./prompts" ;
1213
1314initInstrumentations ( ) ;
Original file line number Diff line number Diff line change 1+ import { context , diag , trace } from "@opentelemetry/api" ;
2+
3+ /**
4+ * Reports a custom metric to the current active span.
5+ *
6+ * This function allows you to add a custom metric to the current span in the trace.
7+ * If there is no active span, a warning will be logged.
8+ *
9+ * @param {string } metricName - The name of the custom metric.
10+ * @param {number } metricValue - The numeric value of the custom metric.
11+ *
12+ * @example
13+ * reportCustomMetric('processing_time', 150);
14+ */
15+ export const reportCustomMetric = ( metricName : string , metricValue : number ) => {
16+ const currentContext = context . active ( ) ;
17+ const currentSpan = trace . getSpan ( currentContext ) ;
18+
19+ if ( currentSpan ) {
20+ currentSpan . setAttribute (
21+ `traceloop.custom_metric.${ metricName } ` ,
22+ metricValue ,
23+ ) ;
24+ } else {
25+ diag . warn ( `No active span found to report custom metric: ${ metricName } ` ) ;
26+ }
27+ } ;
You can’t perform that action at this time.
0 commit comments