Skip to content

Commit 61587a9

Browse files
committed
use reduce to improve performance
1 parent 683a895 commit 61587a9

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

packages/core/src/integrations/mcp-server/piiFiltering.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ const PII_ATTRIBUTES = new Set([
2626
MCP_TOOL_RESULT_CONTENT_ATTRIBUTE,
2727
]);
2828

29+
/**
30+
* Checks if an attribute key should be considered PII
31+
* @internal
32+
*/
33+
function isPiiAttribute(key: string): boolean {
34+
return PII_ATTRIBUTES.has(key) || key.startsWith(`${MCP_REQUEST_ARGUMENT}.`);
35+
}
36+
2937
/**
3038
* Removes PII attributes from span data when sendDefaultPii is false
3139
* @param spanData - Raw span attributes
@@ -35,15 +43,18 @@ const PII_ATTRIBUTES = new Set([
3543
export function filterMcpPiiFromSpanData(
3644
spanData: Record<string, unknown>,
3745
sendDefaultPii: boolean,
38-
): Record<string, SpanAttributeValue | undefined> {
46+
): Record<string, SpanAttributeValue> {
3947
if (sendDefaultPii) {
40-
return spanData as Record<string, SpanAttributeValue | undefined>;
48+
return spanData as Record<string, SpanAttributeValue>;
4149
}
4250

43-
return Object.fromEntries(
44-
Object.entries(spanData).filter(([key]) => {
45-
const isPiiAttribute = PII_ATTRIBUTES.has(key) || key.startsWith(`${MCP_REQUEST_ARGUMENT}.`);
46-
return !isPiiAttribute;
47-
}),
48-
) as Record<string, SpanAttributeValue | undefined>;
51+
return Object.entries(spanData).reduce(
52+
(acc, [key, value]) => {
53+
if (!isPiiAttribute(key)) {
54+
acc[key] = value as SpanAttributeValue;
55+
}
56+
return acc;
57+
},
58+
{} as Record<string, SpanAttributeValue>,
59+
);
4960
}

0 commit comments

Comments
 (0)