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 {
31
31
prompt : `Tell me a joke about ${ jokeSubject } ` ,
32
32
model : "gpt-3.5-turbo-instruct" ,
33
33
} ) ;
34
+ traceloop . reportCustomMetric ( "test_metric" , 50.2 ) ;
34
35
35
36
return completion . choices [ 0 ] . text ;
36
37
}
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ export * from "./tracing/decorators";
8
8
export * from "./tracing/manual" ;
9
9
export * from "./tracing/association" ;
10
10
export * from "./tracing/score" ;
11
+ export * from "./tracing/custom-metric" ;
11
12
export * from "./prompts" ;
12
13
13
14
initInstrumentations ( ) ;
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