Skip to content

[TT-4023] Track 404 logs not showing in listening port when changing control api port#7976

Merged
radkrawczyk merged 14 commits into
masterfrom
TT-4023-track-404-logs-not-showing-in-listening-port-when-changing-control-api-port
May 15, 2026
Merged

[TT-4023] Track 404 logs not showing in listening port when changing control api port#7976
radkrawczyk merged 14 commits into
masterfrom
TT-4023-track-404-logs-not-showing-in-listening-port-when-changing-control-api-port

Conversation

@MaciekMis

@MaciekMis MaciekMis commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

Description

Related Issue

Motivation and Context

How This Has Been Tested

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring or add test (improvements in base code or adds test coverage to functionality)

Checklist

  • I ensured that the documentation is up to date
  • I explained why this PR updates go.mod in detail with reasoning why it's required
  • I would like a code coverage CI quality gate exception and have explained why

Ticket Details

TT-4023
Status In Test
Summary Track 404 logs not showing in listening port when changing control api port

Generated at: 2026-05-15 06:36:31

@probelabs

probelabs Bot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

This PR addresses an issue where 404 (Not Found) logs were not being generated on the main gateway listening port when the control API was configured on a separate port. The root cause was that the NotFoundHandler, responsible for this logging, was only being assigned to the control API's router, leaving other listeners without it.

The fix centralizes the assignment of the NotFoundHandler into the proxyMux.setRouter method. This ensures that any router configured through this common function—whether for the main gateway listener or the control API—is consistently equipped with the 404 logging handler. To support this, the proxyMux struct is now explicitly initialized with the track404Logs configuration setting at startup.

Additionally, the 404 log entry has been enhanced to include the Host header from the request, providing better context for debugging. This new field is added conditionally, only when a non-legacy log formatter is in use, to ensure backward compatibility with existing log parsing setups. A new helper function, log.IsLegacyFormatter, has been introduced to detect the formatter type.

Files Changed Analysis

  • gateway/api_loader.go: Removed the specific NotFoundHandler assignment for the control API router, as this is now handled centrally.
  • gateway/proxy_muxer.go: The setRouter function now assigns the handle404 method to every router it configures. The handle404 logic was updated to use a new getLogEntryFor404 function, which conditionally includes the request's Host header based on the log formatter.
  • gateway/server.go: The proxyMux struct is now initialized with the track404Logs configuration value in both NewGateway and startServer to ensure the setting is consistently applied.
  • log/log.go: Added the IsLegacyFormatter helper function to check if the legacy text formatter is active.
  • gateway/proxy_muxer_test.go & log/log_test.go: New tests were added to verify the conditional logging logic and the correctness of the IsLegacyFormatter function.

Architecture & Impact Assessment

  • What this PR accomplishes: It ensures consistent 404 logging behavior across all gateway listeners, resolving a logging gap that occurred with specific port configurations.
  • Key technical changes introduced: The responsibility for setting the 404 handler has been moved to the centralized proxyMux.setRouter function. The proxyMux struct now holds the track404Logs state, and 404 logging is enhanced with a conditional Host header to improve observability while maintaining backward compatibility.
  • Affected system components: The changes affect the gateway's core HTTP server setup, request routing, and error handling logic, primarily within the gateway and log packages.
graph TD
    subgraph "Before"
        A["loadApps() for Control API"] --> B["Control API Router"];
        B -- sets handler --> C("router.NotFoundHandler = handle404");
        D["startServer() for Main Listener"] --> E["Main Listener Router"];
        E -- handler not set --> F((No 404 Logging));
    end

    subgraph "After"
        G["loadApps() for Control API"] --> H["Control API Router"];
        H --> I["proxyMux.setRouter()"];
        J["startServer() for Main Listener"] --> K["Main Listener Router"];
        K --> I;
        I -- sets handler on ALL routers --> L("router.NotFoundHandler = handle404");
    end
Loading

Scope Discovery & Context Expansion

The change is strategically placed in proxyMux.setRouter, a function that serves as a single point of configuration for all listeners managed by the gateway. This approach ensures that any listener will automatically inherit the correct 404 logging behavior. The introduction of IsLegacyFormatter to conditionally add the Host header is a key detail that prevents breaking changes for users with established log parsing pipelines. The impact is well-contained and effectively addresses the root cause of the bug.

References

  • gateway/proxy_muxer.go: Centralizes the NotFoundHandler assignment and enhances the log message.
  • gateway/api_loader.go: Removes the old, specific NotFoundHandler assignment.
  • gateway/server.go: Updates proxyMux initialization to include the track404Logs configuration.
  • log/log.go: Introduces IsLegacyFormatter to support conditional logging fields.
Metadata
  • Review Effort: 2 / 5
  • Primary Label: bug

Powered by Visor from Probelabs

Last updated: 2026-05-15T06:37:50.824Z | Triggered by: pr_updated | Commit: 940b316

💡 TIP: You can chat with Visor using /visor ask <your question>

@probelabs

probelabs Bot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

✅ Security Check Passed

No security issues found – changes LGTM.

✅ Security Check Passed

No security issues found – changes LGTM.

\n\n

Architecture Issues (2)

Severity Location Issue
🟡 Warning gateway/server.go:2446-2448
The `proxyMux` struct is initialized in multiple places (`NewGateway`, `startServer`, `loadApps`). This scattered initialization can lead to inconsistencies if a new field is added to `proxyMux` but not updated in all instantiation sites. A constructor function would centralize the creation logic.
💡 SuggestionCreate a constructor function for `proxyMux`, for example `newProxyMux(conf config.Config)`, to ensure all instances are created consistently. This function would centralize the logic for setting fields like `track404Logs` and `again`.
🟡 Warning log/log.go:135
The `IsLegacyFormatter` function relies on checking the `TimestampFormat` field of `logrus.TextFormatter` to identify the legacy formatter. This is a brittle approach as it depends on an internal implementation detail (`LegacyTimestampFormat`) of the formatter's creation and not on its type. If the legacy formatter's timestamp format were to change or be configured differently, this check would fail.
💡 SuggestionA more robust approach would be to introduce a dedicated type for the legacy formatter, e.g., `type LegacyFormatter struct { *logrus.TextFormatter }`. The `newFormatterLegacy` function would return this type, and `IsLegacyFormatter` could then use a type assertion `_, ok := formatter.(*LegacyFormatter)` for a more reliable check.

✅ Performance Check Passed

No performance issues found – changes LGTM.

Quality Issues (1)

Severity Location Issue
🟡 Warning gateway/proxy_muxer_test.go:376-394
The test for `getLogEntryFor404` verifies the presence of log fields but does not validate their content. The test request is created without setting `RemoteAddr` and `Host`, which are used by the function under test. This makes the test less thorough as it doesn't confirm that values are correctly propagated from the request to the log entry.
💡 SuggestionTo improve test robustness: 1. Populate the `RemoteAddr` and `Host` fields on the `httptest.NewRequest` object. 2. Assert that the values of the `origin` and `host` fields in the resulting `logrus.Entry` match the values set on the request. 3. Consider restructuring the test with sub-tests for each formatter type to improve readability and make assertions more explicit.

Powered by Visor from Probelabs

Last updated: 2026-05-15T06:37:23.921Z | Triggered by: pr_updated | Commit: 940b316

💡 TIP: You can chat with Visor using /visor ask <your question>

@github-actions

github-actions Bot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

API Changes

no api changes detected

@MFCaballero MFCaballero 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.

Hi @MaciekMis can you please add tests?

@MaciekMis

Copy link
Copy Markdown
Contributor Author

Hi @MaciekMis can you please add tests?

Hey,
I did modify logging logic for 404 error due to comments in ticket. I have also included new test verifying that a proper log is logged.

@sonarqubecloud

sonarqubecloud Bot commented May 8, 2026

Copy link
Copy Markdown

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
84.6% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

MaciekMis and others added 2 commits May 12, 2026 13:31
@github-actions

github-actions Bot commented May 14, 2026

Copy link
Copy Markdown
Contributor

🎯 Recommended Merge Targets

Based on JIRA ticket TT-4023: Track 404 logs not showing in listening port when changing control api port

Fix Version: Tyk 5.8.15

Required:

  • release-5.8 - Minor version branch for 5.8.x patches - required for creating Tyk 5.8.15
  • master - Main development branch - ensures fix is in all future releases

Fix Version: Tyk 5.13.1

Required:

  • release-5.13 - Minor version branch for 5.13.x patches - required for creating Tyk 5.13.1
  • master - Main development branch - ensures fix is in all future releases

Fix Version: Tyk 5.14.0

⚠️ Warning: Expected release branches not found in repository

Required:

  • master - No matching release branches found. Fix will be included in future releases.

📋 Workflow

  1. Merge this PR to master first

  2. Cherry-pick to release branches by commenting on the merged PR:

    • /release to release-5.8
    • /release to release-5.13
  3. Automated backport - The bot will automatically create backport PRs to the specified release branches

@radkrawczyk radkrawczyk reopened this May 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🚨 Jira Linter Failed

Commit: 940b316
Failed at: 2026-05-15 06:36:33 UTC

The Jira linter failed to validate your PR. Please check the error details below:

🔍 Click to view error details
failed to validate Jira issue: jira ticket TT-4023 has status 'In Test' but must be one of: Ready For Dev, Dod Check, In Dev, In Code Review

Next Steps

  • Ensure your branch name contains a valid Jira ticket ID (e.g., ABC-123)
  • Verify your PR title matches the branch's Jira ticket ID
  • Check that the Jira ticket exists and is accessible

This comment will be automatically deleted once the linter passes.

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
95.6% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

@radkrawczyk
radkrawczyk merged commit 557c0fb into master May 15, 2026
60 of 125 checks passed
@radkrawczyk
radkrawczyk deleted the TT-4023-track-404-logs-not-showing-in-listening-port-when-changing-control-api-port branch May 15, 2026 09:16
@radkrawczyk

Copy link
Copy Markdown
Contributor

/release to release-5.8

@radkrawczyk

Copy link
Copy Markdown
Contributor

/release to release-5.13

@probelabs

probelabs Bot commented May 15, 2026

Copy link
Copy Markdown
Contributor

✅ Cherry-pick successful. A PR was created: #8225

@probelabs

probelabs Bot commented May 15, 2026

Copy link
Copy Markdown
Contributor

✅ Cherry-pick successful. A PR was created: #8226

radkrawczyk added a commit that referenced this pull request May 15, 2026
…ening port when changing control api port (#7976) (#8225)

[TT-4023] Track 404 logs not showing in listening port when changing
control api port (#7976)

<!-- Provide a general summary of your changes in the Title above -->

## Description

<!-- Describe your changes in detail -->

## Related Issue

<!-- This project only accepts pull requests related to open issues. -->
<!-- If suggesting a new feature or change, please discuss it in an
issue first. -->
<!-- If fixing a bug, there should be an issue describing it with steps
to reproduce. -->
<!-- OSS: Please link to the issue here. Tyk: please create/link the
JIRA ticket. -->

## Motivation and Context

<!-- Why is this change required? What problem does it solve? -->

## How This Has Been Tested

<!-- Please describe in detail how you tested your changes -->
<!-- Include details of your testing environment, and the tests -->
<!-- you ran to see how your change affects other areas of the code,
etc. -->
<!-- This information is helpful for reviewers and QA. -->

## Screenshots (if appropriate)

## Types of changes

<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Refactoring or add test (improvements in base code or adds test
coverage to functionality)

## Checklist

<!-- Go over all the following points, and put an `x` in all the boxes
that apply -->
<!-- If there are no documentation updates required, mark the item as
checked. -->
<!-- Raise up any additional concerns not covered by the checklist. -->

- [ ] I ensured that the documentation is up to date
- [ ] I explained why this PR updates go.mod in detail with reasoning
why it's required
- [ ] I would like a code coverage CI quality gate exception and have
explained why









<!---TykTechnologies/jira-linter starts here-->

### Ticket Details

<details>
<summary>
<a href="https://tyktech.atlassian.net/browse/TT-4023" title="TT-4023"
target="_blank">TT-4023</a>
</summary>

|         |    |
|---------|----|
| Status  | In Test |
| Summary | Track 404 logs not showing in listening port when changing
control api port |

Generated at: 2026-05-15 09:20:51

</details>

<!---TykTechnologies/jira-linter ends here-->


---------

Co-authored-by: Radosław Krawczyk
<[email protected]>

[TT-4023]:
https://tyktech.atlassian.net/browse/TT-4023?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

Co-authored-by: Maciej Miś <[email protected]>
Co-authored-by: Radosław Krawczyk <[email protected]>
radkrawczyk added a commit that referenced this pull request May 15, 2026
…ning port when changing control api port (#7976) (#8226)

[TT-4023] Track 404 logs not showing in listening port when changing
control api port (#7976)

<!-- Provide a general summary of your changes in the Title above -->

## Description

<!-- Describe your changes in detail -->

## Related Issue

<!-- This project only accepts pull requests related to open issues. -->
<!-- If suggesting a new feature or change, please discuss it in an
issue first. -->
<!-- If fixing a bug, there should be an issue describing it with steps
to reproduce. -->
<!-- OSS: Please link to the issue here. Tyk: please create/link the
JIRA ticket. -->

## Motivation and Context

<!-- Why is this change required? What problem does it solve? -->

## How This Has Been Tested

<!-- Please describe in detail how you tested your changes -->
<!-- Include details of your testing environment, and the tests -->
<!-- you ran to see how your change affects other areas of the code,
etc. -->
<!-- This information is helpful for reviewers and QA. -->

## Screenshots (if appropriate)

## Types of changes

<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Refactoring or add test (improvements in base code or adds test
coverage to functionality)

## Checklist

<!-- Go over all the following points, and put an `x` in all the boxes
that apply -->
<!-- If there are no documentation updates required, mark the item as
checked. -->
<!-- Raise up any additional concerns not covered by the checklist. -->

- [ ] I ensured that the documentation is up to date
- [ ] I explained why this PR updates go.mod in detail with reasoning
why it's required
- [ ] I would like a code coverage CI quality gate exception and have
explained why









<!---TykTechnologies/jira-linter starts here-->

### Ticket Details

<details>
<summary>
<a href="https://tyktech.atlassian.net/browse/TT-4023" title="TT-4023"
target="_blank">TT-4023</a>
</summary>

|         |    |
|---------|----|
| Status  | In Test |
| Summary | Track 404 logs not showing in listening port when changing
control api port |

Generated at: 2026-05-15 09:20:51

</details>

<!---TykTechnologies/jira-linter ends here-->


---------

Co-authored-by: Radosław Krawczyk
<[email protected]>

[TT-4023]:
https://tyktech.atlassian.net/browse/TT-4023?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

Co-authored-by: Maciej Miś <[email protected]>
Co-authored-by: Radosław Krawczyk <[email protected]>
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