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

✨ [RUM-4014] DD_LOGS: add handling stack in beforeSend context #2786

Merged
merged 37 commits into from
Jun 11, 2024

Conversation

thomas-lebeau
Copy link
Collaborator

@thomas-lebeau thomas-lebeau commented May 28, 2024

Motivation

Create a stack trace and expose it in the beforeSend's domainContext argument.
It allow customers to initialise the browser SDK with something like:

const SERVICE_REGEX = /(?:browser-sdk-test-playground\/module-federation\/|webpack:\/\/)(?<service>\w*)\//;
  
DD_LOGS.init({
  beforeSend: (event, context) => {
    const service =
      context?.handlingStack?.match(SERVICE_REGEX)?.groups?.service;

    if (service) {
      event.service = service; //TODO: this is not yet implemented
    }
    return true;
  },
});

Changes

  • support the following APIs
    • console.*()
    • logger.*()
    • fetch console errors?
    • xhr console errors?
  • moved isAuthorized to a new file to avoid cyclic dependencies

Testing

  • Local
  • Staging
  • Unit
  • End to end

I have gone over the contributing documentation.

@codecov-commenter
Copy link

codecov-commenter commented May 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.63%. Comparing base (81981c0) to head (ef8878c).
Report is 8 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2786   +/-   ##
=======================================
  Coverage   93.63%   93.63%           
=======================================
  Files         243      244    +1     
  Lines        7112     7121    +9     
  Branches     1583     1588    +5     
=======================================
+ Hits         6659     6668    +9     
  Misses        453      453           

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

@thomas-lebeau thomas-lebeau changed the title Thomas.lebeau/micro frontend service logs ✨ [RUM-4014] DD_LOGS: add handling stack in beforeSend context May 28, 2024
@thomas-lebeau thomas-lebeau changed the base branch from main to thomas.lebeau/micro-frontend-rum May 28, 2024 14:07
Copy link

cit-pr-commenter bot commented May 28, 2024

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum 158.74 KiB 158.74 KiB 0 B 0.00%
Logs 57.67 KiB 57.90 KiB 244 B +0.41%
Rum Slim 105.21 KiB 105.21 KiB 0 B 0.00%
Worker 25.21 KiB 25.21 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.038 0.040 0.002
adderror 0.035 0.037 0.003
addtiming 0.001 0.001 -0.000
startview 1.270 1.206 -0.064
startstopsessionreplayrecording 0.942 1.211 0.269
logmessage 0.006 0.029 0.023
🧠 Memory Performance
Action Name Base Consumption Memory (bytes) Local Consumption Memory (bytes) 𝚫 (bytes)
addglobalcontext 19.68 KiB 18.52 KiB -1186 B
addaction 69.82 KiB 69.25 KiB -577 B
adderror 86.51 KiB 84.04 KiB -2536 B
addtiming 17.55 KiB 16.64 KiB -932 B
startview 312.30 KiB 316.66 KiB 4.36 KiB
startstopsessionreplayrecording 12.19 KiB 12.43 KiB 245 B
logmessage 67.54 KiB 69.47 KiB 1.94 KiB

@thomas-lebeau thomas-lebeau force-pushed the thomas.lebeau/micro-frontend-service-logs branch from 0b4c23a to a070bbb Compare May 28, 2024 15:00
Base automatically changed from thomas.lebeau/micro-frontend-rum to main May 30, 2024 08:29
@thomas-lebeau thomas-lebeau force-pushed the thomas.lebeau/micro-frontend-service-logs branch from dcca3e8 to 23ae6a2 Compare May 30, 2024 15:42
Copy link
Collaborator Author

@thomas-lebeau thomas-lebeau May 31, 2024

Choose a reason for hiding this comment

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

♻️ refactor: this file contains function and types that have been moved here untouched to avoid cyclic dependency

@thomas-lebeau thomas-lebeau marked this pull request as ready for review June 5, 2024 12:58
@thomas-lebeau thomas-lebeau requested a review from a team as a code owner June 5, 2024 12:58
Comment on lines 63 to 71
ok(message: string, messageContext?: object, error?: Error) {
this.log(message, messageContext, StatusType.ok, error)
let handlingStack: string | undefined

if (isAuthorized(StatusType.ok, HandlerType.http, this)) {
handlingStack = createHandlingStack()
}

this.logImplementation(message, messageContext, StatusType.ok, error, handlingStack)
}
Copy link
Member

Choose a reason for hiding this comment

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

💬 suggestion: ‏Maybe it could be interesting to use .bind to avoid duplicating so much code? Something like:

class Logger {
  constructor(...) {
    ...
    this.ok = logImplementation.bind(this, StatusType.ok)
  }
}

This could be a minor breaking change though (ex: if a customer has a class that extends Logger and calls super.ok())

Else, I would suggest to always create the handlingstack, as it would reduce a bit the amount of boilerplate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

always create the handlingstack

Indeed, this would save ~660 Bytes as per my calculations. However generating a stack trace could be expensive, especially if used a lot.

Another solution is to skip 3 frame instead of 2 for logs. Customers can always increase the stack size if needed.

wdyt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Apply the prototype trick we talked during the daily. +240B vs +909B before 🎉
Thank you @BenoitZugmeyer for the suggestion

test/e2e/scenario/microfrontend.scenario.ts Outdated Show resolved Hide resolved
test/e2e/scenario/microfrontend.scenario.ts Outdated Show resolved Hide resolved
@thomas-lebeau thomas-lebeau force-pushed the thomas.lebeau/micro-frontend-service-logs branch from 6f225a9 to 383b43a Compare June 11, 2024 12:49
@thomas-lebeau
Copy link
Collaborator Author

/to-staging

@dd-devflow
Copy link
Contributor

dd-devflow bot commented Jun 11, 2024

🚂 Branch Integration: starting soon, median merge time is 10m

Commit ef8878c0d6 will soon be integrated into staging-24.

Use /to-staging -c to cancel this operation!

dd-mergequeue bot added a commit that referenced this pull request Jun 11, 2024
…ing-24

Integrated commit sha: ef8878c

Co-authored-by: Thomas Lebeau <thomas.lebeau@datadoghq.com>
@dd-devflow
Copy link
Contributor

dd-devflow bot commented Jun 11, 2024

🚂 Branch Integration: This commit was successfully integrated

Commit ef8878c0d6 has been merged into staging-24 in merge commit c724037f68.

Check out the triggered pipeline on Gitlab 🦊

@thomas-lebeau thomas-lebeau merged commit b839f8c into main Jun 11, 2024
21 checks passed
@thomas-lebeau thomas-lebeau deleted the thomas.lebeau/micro-frontend-service-logs branch June 11, 2024 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants