Skip to content

[CONTP-1605] [DDGR] Use context based logging and reconcile once, no double get#2910

Merged
tbavelier merged 28 commits intomainfrom
tbavelier/generic-resource-improvements-context-logging
Apr 20, 2026
Merged

[CONTP-1605] [DDGR] Use context based logging and reconcile once, no double get#2910
tbavelier merged 28 commits intomainfrom
tbavelier/generic-resource-improvements-context-logging

Conversation

@tbavelier
Copy link
Copy Markdown
Member

@tbavelier tbavelier commented Apr 16, 2026

What does this PR do?

Transitions the GenericResource controller to context-based logging, consistent with the pattern applied to other controllers (e.g. datadogagentinternal). Also eliminates the double client.Get per reconcile that was mistakingly introduced at one point

Context-based logging

  • The request-scoped logger (enriched with namespace/name) is set up once in the outer Reconcile method in datadoggenericresource_controller.go via ctrl.LoggerInto, and flows down through the context
  • All internal methods (update, create, updateStatusIfNeeded, handleFinalizer, etc.) now take ctx context.Context instead of logger logr.Logger — same parameter count, but a strict improvement: ctx replaces context.TODO() in client calls with the real request context (correct deadline/cancellation propagation), and the logger retrieved via ctrl.LoggerFrom(ctx) automatically carries the request-scoped key/values
  • Removes individual logs from each <resources>.go since it would be twice the same information, so we can simplify:
{
  "level": "INFO",
  "ts": "2026-04-17T08:10:44.469Z",
  "logger": "controllers.DatadogGenericResource",
  "msg": "created a new dashboard",
  "namespace": "system",
  "name": "ddgr-test-dashboard",
  "reconcileID": "9c2e29ca-8f63-4674-81c2-07a0dad4c9bd",
  "dashboard Id": "shi-zuy-uz7"
}
{
  "level": "INFO",
  "ts": "2026-04-17T08:10:44.470Z",
  "logger": "controllers.DatadogGenericResource",
  "msg": "created a new DatadogGenericResource",
  "namespace": "system",
  "name": "ddgr-test-dashboard",
  "reconcileID": "9c2e29ca-8f63-4674-81c2-07a0dad4c9bd",
  "generic resource Id": "shi-zuy-uz7"
}

Eliminate double client.Get

reconcile.AsReconciler (used in SetupWithManager) already fetches the object and handles not-found before calling our reconciler. The old internal Reconcile(ctx, request) was doing a second client.Get on top of that.

The fix aligns with the pattern used by datadogmonitor, datadogagent, and other controllers: the internal reconciler's Reconcile accepts *DatadogGenericResource directly (no fetch inside), and the framework-owned fetch in reconcile.AsReconciler is the only one. The now-redundant ReconcileInstance indirection is removed.

Motivation

Consistency with the rest of the codebase. Using ctrl.LoggerFrom(ctx) is the idiomatic controller-runtime pattern and avoids threading a logger explicitly through every call. It also fixes the pre-existing issue of client operations using context.TODO() instead of the real request context.

Checklist

  • PR has at least one valid label: bug, enhancement, refactoring, documentation, tooling, and/or dependencies
  • PR has a milestone or the qa/skip-qa label
  • All commits are signed (see: signing commits)

wdhif and others added 11 commits April 15, 2026 12:04
Signed-off-by: Wassim DHIF <wassim.dhif@datadoghq.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Wassim DHIF <wassim.dhif@datadoghq.com>
MockHandler and its state (mockGetErr, mockUpdateErr, etc.) lived in
utils.go but were only referenced from test files, causing golangci-lint
to flag them as unused (and the error vars as violating errname). Moved
all mock code to mock_handler_test.go and registered the handler via
init(), with a testHandlers map in utils.go as the bridge.
Signed-off-by: Wassim DHIF <wassim.dhif@datadoghq.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Wassim DHIF <wassim.dhif@datadoghq.com>
Signed-off-by: Wassim DHIF <wassim.dhif@datadoghq.com>
@tbavelier tbavelier added this to the v1.27.0 milestone Apr 16, 2026
@tbavelier tbavelier added the enhancement New feature or request label Apr 16, 2026
@datadog-prod-us1-5
Copy link
Copy Markdown

datadog-prod-us1-5 Bot commented Apr 16, 2026

Code Coverage

Fix all issues with BitsAI

🛑 Gate Violations

🎯 1 Code Coverage issue detected

A Patch coverage percentage gate may be blocking this PR.

Patch coverage: 46.43% (threshold: 80.00%)

ℹ️ Info

🎯 Code Coverage (details)
Patch Coverage: 46.43%
Overall Coverage: 40.20% (+0.02%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: dbba436 | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback!

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 16, 2026

Codecov Report

❌ Patch coverage is 46.34146% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 40.08%. Comparing base (b0aad13) to head (dbba436).

Files with missing lines Patch % Lines
...al/controller/datadoggenericresource_controller.go 0.00% 10 Missing ⚠️
...al/controller/datadoggenericresource/controller.go 76.47% 3 Missing and 1 partial ⚠️
...al/controller/datadoggenericresource/synthetics.go 33.33% 4 Missing ⚠️
...al/controller/datadoggenericresource/dashboards.go 0.00% 1 Missing ⚠️
...nal/controller/datadoggenericresource/downtimes.go 0.00% 1 Missing ⚠️
...rnal/controller/datadoggenericresource/monitors.go 0.00% 1 Missing ⚠️
...nal/controller/datadoggenericresource/notebooks.go 0.00% 1 Missing ⚠️

❌ Your patch status has failed because the patch coverage (46.34%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2910      +/-   ##
==========================================
+ Coverage   40.06%   40.08%   +0.01%     
==========================================
  Files         320      320              
  Lines       28086    28075      -11     
==========================================
  Hits        11254    11254              
+ Misses      16022    16012      -10     
+ Partials      810      809       -1     
Flag Coverage Δ
unittests 40.08% <46.34%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...nternal/controller/datadoggenericresource/utils.go 82.22% <100.00%> (ø)
internal/controller/finalizer/finalizer.go 87.09% <100.00%> (ø)
...al/controller/datadoggenericresource/dashboards.go 16.66% <0.00%> (+0.66%) ⬆️
...nal/controller/datadoggenericresource/downtimes.go 0.00% <0.00%> (ø)
...rnal/controller/datadoggenericresource/monitors.go 0.00% <0.00%> (ø)
...nal/controller/datadoggenericresource/notebooks.go 11.32% <0.00%> (+0.41%) ⬆️
...al/controller/datadoggenericresource/controller.go 65.32% <76.47%> (+1.26%) ⬆️
...al/controller/datadoggenericresource/synthetics.go 26.80% <33.33%> (+1.29%) ⬆️
...al/controller/datadoggenericresource_controller.go 0.00% <0.00%> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b0aad13...dbba436. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tbavelier tbavelier changed the title [DDGR] Use context based logging [CONTP-1605] [DDGR] Use context based logging Apr 16, 2026
Base automatically changed from tbavelier/generic-resource-improvements to main April 17, 2026 07:05
@tbavelier tbavelier marked this pull request as ready for review April 17, 2026 08:26
@tbavelier tbavelier requested a review from a team April 17, 2026 08:26
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 55a8e431b1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/controller/datadoggenericresource_controller.go Outdated
tbavelier and others added 2 commits April 17, 2026 10:41
reconcile.AsReconciler already fetches the object before calling
Reconcile(ctx, instance). The previous implementation rebuilt a Request
and delegated to the internal Reconcile which performed a second Get,
resulting in two cache reads per reconcile loop.

Split internalReconcile into:
- Reconcile(ctx, req): keeps the Get for backward-compat/test use
- ReconcileInstance(ctx, instance): skips the Get, used by the outer
  controller which receives a pre-fetched object from AsReconciler

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@tbavelier
Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@tbavelier tbavelier changed the title [CONTP-1605] [DDGR] Use context based logging [CONTP-1605] [DDGR] Use context based logging and reconcile one Apr 17, 2026
@tbavelier tbavelier changed the title [CONTP-1605] [DDGR] Use context based logging and reconcile one [CONTP-1605] [DDGR] Use context based logging and reconcile once, no double get Apr 17, 2026
Copy link
Copy Markdown
Contributor

@gabedos gabedos left a comment

Choose a reason for hiding this comment

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

Updating logging + Reconcile tweak lgtm

@tbavelier tbavelier merged commit 6e2c0c9 into main Apr 20, 2026
37 of 39 checks passed
@tbavelier tbavelier deleted the tbavelier/generic-resource-improvements-context-logging branch April 20, 2026 09:04
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