Skip to content

Fix azure log query#2149

Merged
lionello merged 3 commits into
mainfrom
edw/fix-azure-log-query
Jun 25, 2026
Merged

Fix azure log query#2149
lionello merged 3 commits into
mainfrom
edw/fix-azure-log-query

Conversation

@edwardrf

@edwardrf edwardrf commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Description

azure defang log didn't work due to the logic requiring cd run id, updated to use etag to find the right run from azure.

Checklist

  • I have performed a self-review of my code
  • I have added appropriate tests
  • I have updated the Defang CLI docs and/or README to reflect my changes, if necessary

Summary by CodeRabbit

  • New Features

    • Added one-shot snapshot mode for service, CD, and build logs when not following, alongside continuous streaming.
  • Improvements

    • Enhanced log retrieval to discover CD runs via etag and warn instead of failing when metadata is missing.
    • Improved follow vs snapshot handling across CD, service, and build log watchers.
    • Refined service-name validation and “not found yet” warnings, including consistent handling of -image build service suffixes.
  • Tests

    • Updated/added unit tests for etag discovery, follow vs snapshot behavior, and service-name validation.

@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 29637308-5c91-41ec-b832-988b5cb53b3d

📥 Commits

Reviewing files that changed from the base of the PR and between 3e4d9cc and 9b6e211.

📒 Files selected for processing (14)
  • src/cmd/cli/command/compose.go
  • src/pkg/cli/client/byoc/azure/byoc.go
  • src/pkg/cli/client/byoc/azure/byoc_test.go
  • src/pkg/cli/tail.go
  • src/pkg/cli/tail_test.go
  • src/pkg/clouds/azure/aca/job.go
  • src/pkg/clouds/azure/aca/job_test.go
  • src/pkg/clouds/azure/aca/tail.go
  • src/pkg/clouds/azure/aca/tail_test.go
  • src/pkg/clouds/azure/acr/buildlogs.go
  • src/pkg/clouds/azure/acr/buildlogs_test.go
  • src/pkg/clouds/azure/cd/driver_test.go
  • src/pkg/clouds/azure/cd/setup.go
  • src/pkg/logs/log_type.go
🚧 Files skipped from review as they are similar to previous changes (14)
  • src/pkg/logs/log_type.go
  • src/pkg/cli/tail.go
  • src/pkg/clouds/azure/cd/setup.go
  • src/pkg/clouds/azure/acr/buildlogs_test.go
  • src/pkg/cli/client/byoc/azure/byoc_test.go
  • src/pkg/clouds/azure/aca/job.go
  • src/pkg/clouds/azure/aca/job_test.go
  • src/pkg/clouds/azure/aca/tail_test.go
  • src/pkg/clouds/azure/aca/tail.go
  • src/cmd/cli/command/compose.go
  • src/pkg/clouds/azure/cd/driver_test.go
  • src/pkg/clouds/azure/acr/buildlogs.go
  • src/pkg/cli/client/byoc/azure/byoc.go
  • src/pkg/cli/tail_test.go

📝 Walkthrough

Walkthrough

Adds a BuildServiceNameSuffix constant and EnvVarEtag constant to centralize previously hard-coded strings. Introduces follow/snapshot dual-mode to WatchLogs (ACA) and WatchBuildLogs (ACR). Adds FindExecutionByEtag for etag-based CD job execution discovery and ResourceGroupExists for RG existence checks. Rewrites QueryLogs to multiplex CD, ACA, and ACR log channels. De-duplicates build image service names in the CLI tail path.

Changes

Azure log tailing: follow/snapshot mode, etag-based run discovery, build service dedup

Layer / File(s) Summary
Shared constants
src/pkg/logs/log_type.go, src/pkg/clouds/azure/aca/job.go
Adds BuildServiceNameSuffix = "-image" to the logs package and EnvVarEtag = "ETAG" to the ACA job package, replacing previously hard-coded string literals.
ACA WatchLogs follow/snapshot mode
src/pkg/clouds/azure/aca/tail.go, src/pkg/clouds/azure/aca/tail_test.go
WatchLogs gains a follow bool parameter; per-app goroutines forward it to StreamLogs, and the ticker-based polling loop is skipped in non-follow mode so the channel closes after the initial poll. Tests updated and a new non-follow test added.
ACR WatchBuildLogs follow/snapshot mode
src/pkg/clouds/azure/acr/buildlogs.go, src/pkg/clouds/azure/acr/buildlogs_test.go
WatchBuildLogs gains a follow bool parameter; streamRunLog is updated to accept the follow flag and terminate early when follow is false after emitting available logs. Existing tests updated to pass true.
ACA etag execution discovery and RG existence check
src/pkg/clouds/azure/aca/job.go, src/pkg/clouds/azure/aca/job_test.go, src/pkg/clouds/azure/cd/setup.go, src/pkg/clouds/azure/cd/driver_test.go
FindExecutionByEtag iterates all CD job executions and returns the most-recent one whose template contains EnvVarEtag matching the input. templateHasEtag is the supporting helper. ResourceGroupExists checks RG existence via CheckExistence.
QueryLogs rewrite
src/pkg/cli/client/byoc/azure/byoc.go, src/pkg/cli/client/byoc/azure/byoc_test.go
QueryLogs resolves the CD run from cache or via FindExecutionByEtag, checks RG existence for non-follow mode, starts ACA/ACR watchers with the follow flag, then multiplexes CD/ACA/ACR channels in a select loop that drains each source by setting its channel to nil when closed. Tests rewritten to validate run-discovery behavior.
CLI tail build image service de-duplication
src/pkg/cli/tail.go, src/pkg/cli/tail_test.go, src/cmd/cli/command/compose.go
Tail trims BuildServiceNameSuffix from each requested service to derive a base name and uses a checked map to avoid duplicate GetService calls. handleLogsCmd uses logs.BuildServiceNameSuffix instead of the hard-coded "-image" literal. New tests validate dedup and missing-service warning behavior.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • DefangLabs/defang#1813: Both PRs modify the same log-command/service expansion logic in src/cmd/cli/command/compose.go to include build-related services when the log type includes build.
  • DefangLabs/defang#2027: Both PRs touch Azure BYOC log querying in src/pkg/cli/client/byoc/azure/byoc.go's QueryLogs follow/snapshot behavior and CD/etag-based log retrieval logic.

Suggested reviewers

  • lionello
  • jordanstephens

🐇 A rabbit hops through logs so deep,
CD, ACA, ACR — none shall sleep!
With etag discovery and follow=true,
The channels all drain when the context blew. 🌀
-image stripped, no duplicates remain,
Snapshot or stream — both paths are sane! ✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 4.17% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'Fix azure log query' is vague and does not clearly convey the specific change—using etag instead of cd run ID to identify Azure deployments. Consider a more descriptive title such as 'Use etag to identify CD runs in azure log query' or 'Fix azure log query by using etag instead of cd run ID' to better communicate the key technical change.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch edw/fix-azure-log-query

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=warning msg="The linter 'gomodguard' is deprecated (since v2.12.0) due to: new major version. Replaced by gomodguard_v2."
level=warning msg="Suggested new configuration:\nlinters:\n enable:\n - gomodguard_v2\n"
level=warning msg="[linters_context] running gomodguard failed: unable to read module file go.mod: current working directory must have a go.mod file: if you are not using go modules it is suggested to disable this linter"
level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (1)
src/pkg/cli/client/byoc/azure/byoc_test.go (1)

261-286: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Strengthen these tests to assert the intended etag-lookup failure path.

At Line 270 and Line 285, checking only err != nil can pass on unrelated failures. Assert the error contains the lookup context so regressions in etag-discovery behavior are caught.

As per coding guidelines, “Test behavior boundaries, not implementation mechanics” and “Add tests for new behavior and important failure modes.”

💡 Proposed assertion tightening
 	_, err := b.QueryLogs(ctx, &defangv1.TailRequest{Etag: "some-etag"})
-	if err == nil {
-		t.Error("QueryLogs should surface the lookup error when the run can't be discovered")
+	if err == nil || !strings.Contains(err.Error(), "failed to find CD deployment for etag") {
+		t.Errorf("expected etag lookup failure, got: %v", err)
 	}
@@
 	_, err := b.QueryLogs(ctx, &defangv1.TailRequest{Etag: "etag-B"})
-	if err == nil {
-		t.Error("QueryLogs should look up the mismatched etag and surface the lookup error")
+	if err == nil || !strings.Contains(err.Error(), "failed to find CD deployment for etag") {
+		t.Errorf("expected mismatched-etag lookup failure, got: %v", err)
 	}
// add to imports
import "strings"
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/pkg/cli/client/byoc/azure/byoc_test.go` around lines 261 - 286, The error
assertions in TestQueryLogsDiscoversRunByEtag and
TestQueryLogsEtagMismatchTriggersLookup are too weak—they only check if an error
occurred but don't validate the error contains the expected lookup context. Add
the strings package import to the test file, then strengthen both error checks
to verify the error message contains the expected lookup failure details (such
as the "denied" error from useFakeCred) rather than just asserting err != nil.
This ensures the tests actually verify the etag-discovery behavior and catch
regressions in how lookup errors are surfaced.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/pkg/cli/client/byoc/azure/byoc.go`:
- Around line 948-951: The code at line 948 where svc.Err is checked is silently
dropping Azure provider errors by only logging them at debug level and
continuing the loop. Instead of calling continue after the debug log, capture
these errors and return them wrapped with context about which service failed.
Apply the same fix to the similar error handling block at line 969 where
provider errors are also being silently swallowed. Both locations need to
surface the actual provider failures to the caller rather than allowing the
function to return an apparently successful but empty result.

In `@src/pkg/cli/tail.go`:
- Around line 164-173: The issue is that the code unconditionally trims the
service name suffix before checking if the service exists, which causes
incorrect warnings or deduping when the full name with suffix (like foo-image)
is an actual deployed service. Instead of trimming the suffix upfront, first
attempt to validate the exact service name using provider.GetService with the
original service value. Only if that returns a CodeNotFound error should you
then apply strings.TrimSuffix to get the base name and retry the GetService
lookup as a fallback. This ensures accurate service validation and proper
build-alias behavior on the second attempt.

In `@src/pkg/clouds/azure/aca/job.go`:
- Around line 578-580: The code iterates over Azure SDK slices without checking
if individual elements are nil before dereferencing them. In the loop iterating
over page.Value around line 578, add a nil check for exec itself before
accessing exec.Name or exec.Properties. Similarly, for the loops around lines
601-604 that iterate over containers and environment variables, add nil checks
for container and env var elements respectively before accessing their fields.
This prevents panic errors when the Azure SDK returns nil slice elements due to
partial or irregular API payloads.

In `@src/pkg/clouds/azure/acr/buildlogs.go`:
- Around line 149-154: In non-follow mode, the function returns early at the
!follow check but the goroutines started around line 140 continue executing
streamRunLog (lines 178-253) until runs reach a terminal status, causing
senders.Wait() to block indefinitely instead of returning promptly. To fix this,
use a context (such as ctx or a cancellation context) that gets cancelled when
the !follow condition is met, then pass this context to streamRunLog so those
goroutines will exit immediately rather than waiting for terminal status,
allowing senders.Wait() to complete and close the output channel without
hanging.

---

Nitpick comments:
In `@src/pkg/cli/client/byoc/azure/byoc_test.go`:
- Around line 261-286: The error assertions in TestQueryLogsDiscoversRunByEtag
and TestQueryLogsEtagMismatchTriggersLookup are too weak—they only check if an
error occurred but don't validate the error contains the expected lookup
context. Add the strings package import to the test file, then strengthen both
error checks to verify the error message contains the expected lookup failure
details (such as the "denied" error from useFakeCred) rather than just asserting
err != nil. This ensures the tests actually verify the etag-discovery behavior
and catch regressions in how lookup errors are surfaced.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c62dc6ea-a3fb-4cbe-88f6-611dfb58f2f9

📥 Commits

Reviewing files that changed from the base of the PR and between e8e48b2 and 55b076a.

📒 Files selected for processing (14)
  • src/cmd/cli/command/compose.go
  • src/pkg/cli/client/byoc/azure/byoc.go
  • src/pkg/cli/client/byoc/azure/byoc_test.go
  • src/pkg/cli/tail.go
  • src/pkg/cli/tail_test.go
  • src/pkg/clouds/azure/aca/job.go
  • src/pkg/clouds/azure/aca/job_test.go
  • src/pkg/clouds/azure/aca/tail.go
  • src/pkg/clouds/azure/aca/tail_test.go
  • src/pkg/clouds/azure/acr/buildlogs.go
  • src/pkg/clouds/azure/acr/buildlogs_test.go
  • src/pkg/clouds/azure/cd/driver_test.go
  • src/pkg/clouds/azure/cd/setup.go
  • src/pkg/logs/log_type.go

Comment thread src/pkg/cli/client/byoc/azure/byoc.go
Comment thread src/pkg/cli/tail.go Outdated
Comment thread src/pkg/clouds/azure/aca/job.go
Comment thread src/pkg/clouds/azure/acr/buildlogs.go
@lionello lionello changed the title Fix auzre log query Fix aure log query Jun 23, 2026
@lionello lionello changed the title Fix aure log query Fix azure log query Jun 23, 2026
@edwardrf edwardrf force-pushed the edw/fix-azure-log-query branch from fbf8bf8 to 9b6e211 Compare June 23, 2026 19:54
@lionello lionello merged commit 9b104ee into main Jun 25, 2026
15 checks passed
@lionello lionello deleted the edw/fix-azure-log-query branch June 25, 2026 21:32
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