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

⚗️[Logs] POC dependency injection #2520

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft

⚗️[Logs] POC dependency injection #2520

wants to merge 13 commits into from

Conversation

bcaudan
Copy link
Contributor

@bcaudan bcaudan commented Dec 1, 2023

Motivation

Play with a dependency injection approach to see how it could look like

Changes

  • bootstrap simple dependency injector
  • use it for logs components
  • re-use it for unit tests

Testing

  • Local
  • Staging
  • Unit
  • End to end

I have gone over the contributing documentation.

Base automatically changed from bcaudan/logs-refacto to main December 8, 2023 09:19
@codecov-commenter
Copy link

codecov-commenter commented Dec 15, 2023

Codecov Report

Attention: 22 lines in your changes are missing coverage. Please review.

Comparison is base (603d6a0) 93.22% compared to head (b45abf7) 93.09%.
Report is 23 commits behind head on main.

Files Patch % Lines
packages/core/src/tools/injector.ts 83.87% 5 Missing ⚠️
packages/logs/src/domain/logsTelemetry.ts 78.26% 5 Missing ⚠️
...ges/core/src/domain/configuration/configuration.ts 0.00% 4 Missing ⚠️
packages/logs/src/domain/configuration.ts 0.00% 4 Missing ⚠️
packages/logs/src/domain/commonContext.ts 0.00% 2 Missing ⚠️
packages/logs/src/domain/reportError.ts 50.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2520      +/-   ##
==========================================
- Coverage   93.22%   93.09%   -0.13%     
==========================================
  Files         222      228       +6     
  Lines        6654     6736      +82     
  Branches     1460     1465       +5     
==========================================
+ Hits         6203     6271      +68     
- Misses        451      465      +14     

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

Comment on lines +26 to 33
if (canUseEventBridge()) {
injector.override(startLogsSessionManager, startLogsSessionManagerStub)
injector.override(startLogsBatch, startLogsBridge)
}
const pageExitObservable = createPageExitObservable(configuration)

const session =
configuration.sessionStoreStrategyType && !canUseEventBridge() && !willSyntheticsInjectRum()
? startLogsSessionManager(configuration)
: startLogsSessionManagerStub(configuration)

const { telemetry, stop: stopLogsTelemetry } = startLogsTelemetry(
configuration,
reportError,
pageExitObservable,
session.expireObservable
)
cleanupTasks.push(() => stopLogsTelemetry())
telemetry.setContextProvider(() => ({
application: {
id: getRUMInternalContext()?.application_id,
},
session: {
id: session.findTrackedSession()?.id,
},
view: {
id: (getRUMInternalContext()?.view as Context)?.id,
},
action: {
id: (getRUMInternalContext()?.user_action as Context)?.id,
},
}))

startNetworkErrorCollection(configuration, lifeCycle)
startRuntimeErrorCollection(configuration, lifeCycle)
startConsoleCollection(configuration, lifeCycle)
startReportCollection(configuration, lifeCycle)
const { handleLog } = startLoggerCollection(lifeCycle)

startLogsAssembly(session, configuration, lifeCycle, buildCommonContext, reportError)

if (!canUseEventBridge()) {
const { stop: stopLogsBatch } = startLogsBatch(
configuration,
lifeCycle,
reportError,
pageExitObservable,
session.expireObservable
)
cleanupTasks.push(() => stopLogsBatch())
} else {
startLogsBridge(lifeCycle)
if (!configuration.sessionStoreStrategyType || willSyntheticsInjectRum()) {
injector.override(startLogsSessionManager, startLogsSessionManagerStub)
}
Copy link
Member

Choose a reason for hiding this comment

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

Note: those "override" in the wild could probably be moved in components, ex:

const startLogsTransport: Component<...> = (injector) => {
    if (canUseEventBridge()) {
       return injector.run(startLogsBridge)
    } else {
       return injector.run(startLogsBatch)
    }
}

const injector: Injector = {
run(component) {
if (instances.has(component)) {
throw new Error(`Component ${component.name} already started`)
Copy link
Member

Choose a reason for hiding this comment

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

Note: This assertion and the following ones (in get and override) are not strictly needed, but helps to debug weird injection cases.

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.

None yet

3 participants