chore(telemetry): register after fork hooks on initialization
Description
app-started, app-closed, and app-heartbeat telemetry events should not be sent in a child processes. To avoid sending these events in child processes forksafe.register(TelemetryWriter._fork_writer) needs to be called as early as possible.
Currently TelemetryWriter._fork_writer is registered in TelemetryWriter.enabled() and this method is called right before the first trace is sent to the agent. Registering TelemetryWriter._fork_writer in TelemetryWriter.__init__() instead will reduce the likelihood that telemetry events get sent in child processes since the telemetry_writer is initialized on import ddtrace (ddtrace/__init__.py imports ddtrace/tracer.py which import ddtrace/internal/writer.py which imports ddtrace/internal/telemetry/__init__.py).
Note - This does not prevent the ddtrace library from sending telemetry in all child processes. If ddtrace is imported after the main process forks this issue can still occur.
Checklist
- [ ] Title must conform to conventional commit.
- [ ] Add additional sections for
featandfixpull requests. - [ ] Ensure tests are passing for affected code.
- [ ] Library documentation and/or Datadog's documentation site is updated. Link to doc PR in description.
Motivation
Design
Testing strategy
Relevant issue(s)
Testing strategy
Reviewer Checklist
- [ ] Title is accurate.
- [ ] Description motivates each change.
- [ ] No unnecessary changes were introduced in this PR.
- [ ] PR cannot be broken up into smaller PRs.
- [ ] Avoid breaking API changes unless absolutely necessary.
- [ ] Tests provided or description of manual testing performed is included in the code or PR.
- [ ] Release note has been added for fixes and features, or else
changelog/no-changeloglabel added. - [ ] All relevant GitHub issues are correctly linked.
- [ ] Backports are identified and tagged with Mergifyio.
- [ ] Add to milestone.