Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Allow to modify source_url property in Loaf script attributions #3325

Merged
merged 7 commits into from
Feb 7, 2025

Conversation

thomas-lebeau
Copy link
Collaborator

@thomas-lebeau thomas-lebeau commented Feb 5, 2025

Motivation

We want to be able to modify source_url and invoker properties of Loaf script attributions from the beforeSend function. This allow customer to scrub urls for potential PII.

Changes

Refactor limitModification to allow arrays to be present in the path. (e.g. long_task.scripts[].source_url will allow to modify source_url in long tasks (LoaF) events

Allow modification on long_task.scripts[].source_url and long_task.scripts[].invoker

Testing

  • Local
  • Staging
  • Unit
  • End to end

I have gone over the contributing documentation.

@thomas-lebeau thomas-lebeau force-pushed the thomas.lebeau/limit-modifications-support-array branch from 075b77b to b386e8b Compare February 5, 2025 15:19
@codecov-commenter
Copy link

codecov-commenter commented Feb 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.96%. Comparing base (0365297) to head (7660dcd).
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3325      +/-   ##
==========================================
+ Coverage   92.94%   92.96%   +0.01%     
==========================================
  Files         297      297              
  Lines        7840     7833       -7     
  Branches     1785     1785              
==========================================
- Hits         7287     7282       -5     
+ Misses        553      551       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@thomas-lebeau thomas-lebeau force-pushed the thomas.lebeau/limit-modifications-support-array branch from b386e8b to db7d41f Compare February 5, 2025 15:26
Copy link

cit-pr-commenter bot commented Feb 5, 2025

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 147.04 KiB 147.06 KiB 18 B +0.01%
Logs 51.33 KiB 51.33 KiB 0 B 0.00%
Rum Slim 105.77 KiB 105.79 KiB 18 B +0.02%
Worker 24.50 KiB 24.50 KiB 0 B 0.00%
🚀 CPU Performance
Action Name Base Average Cpu Time (ms) Local Average Cpu Time (ms) 𝚫
addglobalcontext 0.002 0.002 0.000
addaction 0.039 0.035 -0.005
addtiming 0.001 0.001 -0.000
adderror 0.049 0.045 -0.003
startstopsessionreplayrecording 0.009 0.012 0.003
startview 0.358 0.357 -0.001
logmessage 0.020 0.020 0.000
🧠 Memory Performance
Action Name Base Consumption Memory (bytes) Local Consumption Memory (bytes) 𝚫 (bytes)
addglobalcontext 28.90 KiB 27.31 KiB -1627 B
addaction 56.42 KiB 53.58 KiB -2906 B
addtiming 27.15 KiB 26.14 KiB -1036 B
adderror 58.56 KiB 55.43 KiB -3210 B
startstopsessionreplayrecording 26.08 KiB 26.07 KiB -10 B
startview 419.23 KiB 420.73 KiB 1.50 KiB
logmessage 62.94 KiB 61.83 KiB -1139 B

🔗 RealWorld

@thomas-lebeau thomas-lebeau changed the title Thomas.lebeau/limit modifications support array ✨ Allow to modify source_url property in Loaf script attributions Feb 5, 2025
@thomas-lebeau thomas-lebeau marked this pull request as ready for review February 5, 2025 16:49
@thomas-lebeau thomas-lebeau requested a review from a team as a code owner February 5, 2025 16:49
const newType = getType(newValue)
if (newType === fieldType) {
set(object, fieldPath, sanitize(newValue))
const pathSegments = fieldPath.split('.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the approach! The one suggestion I have is to tweak the implementation strategy slightly. If you change this line to:

Suggested change
const pathSegments = fieldPath.split('.')
const pathSegments = fieldPath.split(/\.|(?=\[\])/)

Then instead of foo.bar[].baz being split into ['foo', 'bar[]', 'baz'], it will be split into ['foo', 'bar', '[]', 'baz']. This should lead to a cleaner implementation of getValueFromPath(), where you can treat [] as a special field name that maps over all elements of an array. With the appropriate changes to getValueFromPath(), it will also mean that foo.bar[][].baz will work as expected with no special effort, so that we can support modifications of nested arrays.


function isValidType(actualType: string | string[], expectedType: 'string' | 'object'): boolean {
if (Array.isArray(actualType)) {
return actualType.length > 0 && actualType.every((type) => type === expectedType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't validate here that the length matches; is that expected? I may be misreading the code, but it seems like this will allow people to remove items from the array while still passing the isValidType() check; the result looks like it would be that you'd get undefined for some elements in the output array. That may be OK, but I just wanted to make sure it was intentional. (Or that it's handled somehow and I'm just missing it.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I haven't consider this. Actually, it also means that every property of the clone is a valid type. If only some are, none will be copied to the original object.

I've come up with a better algorithm, that should be resilient to such inconsistencies in the clone: traverse both, the object and the clone recursively at the same time, up to the end of the path where I can check the path individually.

At the end, I think the code is simpler and even more efficient as we now do the get and set traversal at once

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I really like the final result! Nicely done.

Comment on lines 30 to 37
if (field === '[]') {
if (!Array.isArray(object) || !Array.isArray(clone)) {
return
}
current = current[field]

object.forEach((item, i) => setValueAtPath(item, clone[i], restPathSegments, fieldType))
return
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥜 nitpick:

if (field === '[]') {
  if (Array.isArray(object) && Array.isArray(clone)) {
    object.forEach((item, i) => setValueAtPath(item, clone[i], restPathSegments, fieldType));
  }
  return;
}

Copy link
Contributor

@sethfowler-datadog sethfowler-datadog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I really like the final solution you came up with; very clean!

@thomas-lebeau thomas-lebeau merged commit e30555e into main Feb 7, 2025
19 checks passed
@thomas-lebeau thomas-lebeau deleted the thomas.lebeau/limit-modifications-support-array branch February 7, 2025 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants