-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sdk): support custom metrics (#446)
- Loading branch information
1 parent
f9ff60d
commit 7c77047
Showing
3 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
}; |