Skip to content

Commit d318828

Browse files
committed
optimize attribute serilaization
1 parent 7939e3e commit d318828

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

packages/core/src/attributes.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ export function isAttributeObject(maybeObj: unknown): maybeObj is AttributeObjec
6161
);
6262
}
6363

64-
export function attributeValueToTypedAttributeValue(rawValue: unknown, useFallback?: true): TypedAttributeValue;
65-
6664
/**
6765
* Converts an attribute value to a typed attribute value.
6866
*
@@ -77,7 +75,7 @@ export function attributeValueToTypedAttributeValue(rawValue: unknown, useFallba
7775
*/
7876
export function attributeValueToTypedAttributeValue(
7977
rawValue: unknown,
80-
useFallback = false,
78+
useFallback?: boolean,
8179
): TypedAttributeValue | void {
8280
const { value, unit } = isAttributeObject(rawValue) ? rawValue : { value: rawValue, unit: undefined };
8381
const attributeValue = getTypedAttributeValue(value);
@@ -106,6 +104,26 @@ export function attributeValueToTypedAttributeValue(
106104
};
107105
}
108106

107+
/**
108+
* Serializes raw attributes to typed attributes as expected in our envelopes.
109+
*
110+
* @param attributes The raw attributes to serialize.
111+
* @param fallback If true, unsupported values will be stringified to a string attribute value.
112+
* Defaults to false. In this case, `undefined` is returned for unsupported values.
113+
*
114+
* @returns The serialized attributes.
115+
*/
116+
export function serializeAttributes<T>(attributes: RawAttributes<T>, fallback: boolean = false): Attributes {
117+
const serializedAttributes: Attributes = {};
118+
for (const [key, value] of Object.entries(attributes)) {
119+
const typedValue = attributeValueToTypedAttributeValue(value, fallback);
120+
if (typedValue) {
121+
serializedAttributes[key] = typedValue;
122+
}
123+
}
124+
return serializedAttributes;
125+
}
126+
109127
/**
110128
* NOTE: We intentionally do not return anything for non-primitive values:
111129
* - array support will come in the future but if we stringify arrays now,

packages/core/src/logs/internal.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { attributeValueToTypedAttributeValue } from '../attributes';
1+
import { Attributes, attributeValueToTypedAttributeValue, RawAttributes, serializeAttributes } from '../attributes';
22
import { getGlobalSingleton } from '../carrier';
33
import type { Client } from '../client';
44
import { getClient, getCurrentScope, getGlobalScope, getIsolationScope } from '../currentScopes';
@@ -155,25 +155,15 @@ export function _INTERNAL_captureLog(
155155

156156
const { level, message, attributes: logAttributes = {}, severityNumber } = log;
157157

158-
const serializedScopeAttributes = Object.fromEntries(
159-
Object.entries(scopeAttributes)
160-
.map(([key, value]) => [key, attributeValueToTypedAttributeValue(value)])
161-
.filter(([, value]) => value != null),
162-
);
163-
164-
const serializedLogAttributes = Object.fromEntries(
165-
Object.entries(logAttributes).map(([key, value]) => [key, attributeValueToTypedAttributeValue(value, true)]),
166-
);
167-
168158
const serializedLog: SerializedLog = {
169159
timestamp: timestampInSeconds(),
170160
level,
171161
body: message,
172162
trace_id: traceContext?.trace_id,
173163
severity_number: severityNumber ?? SEVERITY_TEXT_TO_SEVERITY_NUMBER[level],
174164
attributes: {
175-
...serializedScopeAttributes,
176-
...serializedLogAttributes,
165+
...serializeAttributes(scopeAttributes),
166+
...serializeAttributes(logAttributes, true),
177167
},
178168
};
179169

0 commit comments

Comments
 (0)