File tree Expand file tree Collapse file tree
packages/core/src/metrics Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -14,6 +14,50 @@ import { createMetricEnvelope } from './envelope';
1414
1515const MAX_METRIC_BUFFER_SIZE = 1000 ;
1616
17+ /**
18+ * Converts a metric attribute to a serialized metric attribute.
19+ *
20+ * @param value - The value of the metric attribute.
21+ * @returns The serialized metric attribute.
22+ */
23+ export function metricAttributeToSerializedMetricAttribute ( value : unknown ) : SerializedMetricAttributeValue {
24+ switch ( typeof value ) {
25+ case 'number' :
26+ if ( Number . isInteger ( value ) ) {
27+ return {
28+ value,
29+ type : 'integer' ,
30+ } ;
31+ }
32+ return {
33+ value,
34+ type : 'double' ,
35+ } ;
36+ case 'boolean' :
37+ return {
38+ value,
39+ type : 'boolean' ,
40+ } ;
41+ case 'string' :
42+ return {
43+ value,
44+ type : 'string' ,
45+ } ;
46+ default : {
47+ let stringValue = '' ;
48+ try {
49+ stringValue = JSON . stringify ( value ) ?? '' ;
50+ } catch {
51+ // Do nothing
52+ }
53+ return {
54+ value : stringValue ,
55+ type : 'string' ,
56+ } ;
57+ }
58+ }
59+ }
60+
1761/**
1862 * Sets a metric attribute if the value exists and the attribute key is not already present.
1963 *
You can’t perform that action at this time.
0 commit comments