Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### **User description** <details open> <summary><a href="https://tyktech.atlassian.net/browse/TT-2539" title="TT-2539" target="_blank">TT-2539</a></summary> <br /> <table> <tr> <th>Summary</th> <td>Transaction/Access Logs</td> </tr> <tr> <th>Type</th> <td> <img alt="Story" src="https://tyktech.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10315?size=medium" /> Story </td> </tr> <tr> <th>Status</th> <td>In Dev</td> </tr> <tr> <th>Points</th> <td>N/A</td> </tr> <tr> <th>Labels</th> <td><a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20A%20ORDER%20BY%20created%20DESC" title="A">A</a>, <a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20America's%20ORDER%20BY%20created%20DESC" title="America's">America's</a>, <a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20CSE%20ORDER%20BY%20created%20DESC" title="CSE">CSE</a>, <a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20Gold%20ORDER%20BY%20created%20DESC" title="Gold">Gold</a>, <a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20QA_Fail%20ORDER%20BY%20created%20DESC" title="QA_Fail">QA_Fail</a>, <a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20customer_request%20ORDER%20BY%20created%20DESC" title="customer_request">customer_request</a>, <a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20innersource%20ORDER%20BY%20created%20DESC" title="innersource">innersource</a>, <a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20jira_escalated%20ORDER%20BY%20created%20DESC" title="jira_escalated">jira_escalated</a></td> </tr> </table> </details> <!-- do not remove this marker as it will break jira-lint's functionality. added_by_jira_lint --> --- ### **FR Jira Ticket** <!-- Provide a general summary of your changes in the Title above --> https://tyktech.atlassian.net/browse/TT-2539 ## Description <!-- Describe your changes in detail --> - Added the `TYK_GW_ACCESSLOGS_ENABLED` Gateway config option - The Tyk Gateway will determine to print access logs to STDOUT for both success and error handling situations - If the `TYK_GW_ACCESSLOGS_ENABLED` is set to `true` then the Gateway will print access logs to STDOUT - If the `TYK_GW_ACCESSLOGS_ENABLED` is set to `false` then the Gateway will not print access logs to STDOUT - Added the `TYK_GW_ACCESSLOGS_TEMPLATE` Gateway config option - If the `TYK_GW_ACCESSLOGS_TEMPLATE` is contains the following options below then the Custom Log Template will be used to print out the access logs - `api_key` will include they obfuscated or hashed key. - `client_ip` will include the ip of the request. - `host` will include the host of the request. - `latency_total` will include the total latency of the request. - `method` will include the request method. - `path` will include the path of the request. - `protocol` will include the protocol of the request. - `remote_addr` will include the remote address of the request. - `status` will include the response status code. - `upstream_address` will include the upstream address (scheme, host and path) - `upstream_latency` will include the upstream latency of the request. - `use_agent` will include the user agent of the request. Note that this feature is off by default and that the `AccessLog` struct only contains the more common elements. Below are some examples of an access log ``` time="Jan 20 11:10:57" level=debug msg="Using CUSTOM access log template" api_id=bbcde1bde9a946af4695bb53b615de93 api_name=httpbin org_id=678e6771247d80fd2c435bf3 time="Jan 20 11:10:57" level=info api_id=bbcde1bde9a946af4695bb53b615de93 api_key=82ebbc4b34021b9f api_name=httpbin client_ip="::1" client_remote_addr="[::1]:65000" host="localhost:8080" method=GET org_id=678e6771247d80fd2c435bf3 prefix=access-log protocol=HTTP/1.1 request_url=/get status_code=200 total_latency=116 upstream_address="http://httpbin.org/get" upstream_latency=115 user_agent=PostmanRuntime/7.43.0 ``` ## 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? --> Today the Tyk Gateway does not print access logs for success API calls but instead only for error API calls. Providing access logs for both scenarios within the Tyk Gateway is extremely valuable especially if you are monitoring logs, capturing analytics or even debugging. Providing the option to turn on or off the Tyk Gateway access logs will provide clients more insights in for API calls in regards to success and error situations. ## 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. --> - Manual testing - Unit testing - Performance testing/benchmarks ## 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) - [x] 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 ___ ### **PR Type** Enhancement, Tests ___ ### **Description** - Introduced a new `AccessLogsConfig` for transaction logging. - Added functionality to log access details for success and error handlers. - Implemented a new `accesslog` package for structured logging. - Enhanced test coverage with unit and benchmark tests for access logs. ___ ### **Changes walkthrough** 📝 <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement</strong></td><td><details><summary>5 files</summary><table> <tr> <td><strong>config.go</strong><dd><code>Added `AccessLogsConfig` for transaction logging configuration.</code></dd></td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6841/files#diff-fe44f09c4d5977b5f5eaea29170b6a0748819c9d02271746a20d81a5f3efca17">+29/-0</a> </td> </tr> <tr> <td><strong>handler_error.go</strong><dd><code>Added access log handling for error responses.</code> </dd></td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6841/files#diff-d3b05530ad23401f3b8f33bb1a467cd807a29a6b09c7430d01d069f626b20f77">+7/-0</a> </td> </tr> <tr> <td><strong>handler_success.go</strong><dd><code>Added access log handling for successful responses.</code> </dd></td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6841/files#diff-45135957493eca37f2e3e9a81079577777133c53b27cf95ea2ff0329c05bd006">+7/-0</a> </td> </tr> <tr> <td><strong>middleware.go</strong><dd><code>Implemented `recordAccessLog` for structured transaction logging.</code></dd></td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6841/files#diff-703054910891a4db633eca0f42ed779d6b4fa75cd9b3aa4c503e681364201c1b">+53/-0</a> </td> </tr> <tr> <td><strong>access_log.go</strong><dd><code>Introduced `accesslog` package for structured access log handling.</code></dd></td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6841/files#diff-39a7ae68c9608f8e05ab9207081cb3be248d00d68a5a2412304b83b2340401b7">+170/-0</a> </td> </tr> </table></details></td></tr><tr><td><strong>Tests</strong></td><td><details><summary>2 files</summary><table> <tr> <td><strong>access_log_test.go</strong><dd><code>Added unit tests for `accesslog` package.</code> </dd></td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6841/files#diff-7e171bde9a23d0039c5dd0c1f0718a1da2dcf93211f997ce8798d4a9a14450f0">+50/-0</a> </td> </tr> <tr> <td><strong>access_log_test.go</strong><dd><code>Added benchmark tests for access log performance.</code> </dd></td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6841/files#diff-cf5779a4d01913d259d6e27cb951393a37fd06a003c5fc5b992238eafbb6c4b8">+85/-0</a> </td> </tr> </table></details></td></tr><tr><td><strong>Configuration changes</strong></td><td><details><summary>2 files</summary><table> <tr> <td><strong>schema.json</strong><dd><code>Updated schema to include `access_logs` configuration.</code> </dd></td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6841/files#diff-103cec746d3e61d391c5a67c171963f66fea65d651d704d5540e60aa5d574f46">+12/-0</a> </td> </tr> <tr> <td><strong>Taskfile.yml</strong><dd><code>Updated test coverage configuration for `accesslog` package.</code></dd></td> <td><a href="https://github.com/TykTechnologies/tyk/pull/6841/files#diff-31877aaad48d3baf442aa52077c28b873df2f2ac6c70941d409261460d7c3c8e">+2/-1</a> </td> </tr> </table></details></td></tr></tr></tbody></table> ___ > 💡 **PR-Agent usage**: Comment `/help "your question"` on any pull request to receive relevant information --------- Co-authored-by: Long Le <[email protected]> Co-authored-by: Tit Petric <[email protected]> Co-authored-by: Tit Petric <[email protected]>
- Loading branch information