-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TT-2539] Transaction logs #6841
Conversation
API Changes --- prev.txt 2025-01-22 13:47:12.142787349 +0000
+++ current.txt 2025-01-22 13:47:07.469731070 +0000
@@ -4904,6 +4904,33 @@
TYPES
+type AccessLogsConfig struct {
+ // Enabled controls enabling the transaction logs. Default: false.
+ Enabled bool `json:"enabled"`
+
+ // Template configures which fields to log into the access log.
+ // If unconfigured, all fields listed will be logged.
+ //
+ // Example: ["client_ip", "path"].
+ //
+ // Template Options:
+ //
+ // - `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.
+ // - `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.
+ // - `upstream_address` will include the upstream address (scheme, host and path)
+ // - `upstream_latency` will include the upstream latency of the request.
+ // - `latency_total` will include the total latency of the request.
+ // - `user_agent` will include the user agent of the request.
+ // - `status` will include the response status code.
+ Template []string `json:"template"`
+}
+ AccessLogsConfig defines the type of transactions logs printed to stdout.
+
type AnalyticsConfigConfig struct {
// Set empty for a Self-Managed installation or `rpc` for multi-cloud.
Type string `json:"type"`
@@ -5356,6 +5383,10 @@
// If not set or left empty, it will default to `standard`.
LogFormat string `json:"log_format"`
+ // AccessLogs configures the output for access logs.
+ // If not configured, the access log is disabled.
+ AccessLogs AccessLogsConfig `json:"access_logs"`
+
// Section for configuring OpenTracing support
// Deprecated: use OpenTelemetry instead.
Tracer Tracer `json:"tracing"`
@@ -8212,6 +8243,10 @@
func (t *BaseMiddleware) OrgSessionExpiry(orgid string) int64
+func (t *BaseMiddleware) RecordAccessLog(req *http.Request, resp *http.Response, latency analytics.Latency)
+ RecordAccessLog is used for Success/Error handler logging. It emits a log
+ entry with populated access log fields.
+
func (t *BaseMiddleware) SetName(name string)
func (t *BaseMiddleware) SetOrgExpiry(orgid string, expiry int64)
@@ -12607,6 +12642,8 @@
manipulate
func MyPluginReturningError(rw http.ResponseWriter, r *http.Request)
+# Package: ./tests/accesslog
+
# Package: ./tests/coprocess
package coprocess // import "github.com/TykTechnologies/tyk/tests/coprocess" |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
Co-authored-by: Tit Petric <[email protected]>
106aaa9
to
50f0820
Compare
Quality Gate passedIssues Measures |
User description
TT-2539
FR Jira Ticket
https://tyktech.atlassian.net/browse/TT-2539
Description
Added the
TYK_GW_ACCESSLOGS_ENABLED
Gateway config optionTYK_GW_ACCESSLOGS_ENABLED
is set totrue
then the Gateway will print access logs to STDOUTTYK_GW_ACCESSLOGS_ENABLED
is set tofalse
then the Gateway will not print access logs to STDOUTAdded the
TYK_GW_ACCESSLOGS_TEMPLATE
Gateway config optionTYK_GW_ACCESSLOGS_TEMPLATE
is contains the following options below then the Custom Log Template will be used to print out the access logsapi_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 logRelated Issue
Motivation and Context
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
Screenshots (if appropriate)
Types of changes
Checklist
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 📝
5 files
Added `AccessLogsConfig` for transaction logging configuration.
Added access log handling for error responses.
Added access log handling for successful responses.
Implemented `recordAccessLog` for structured transaction logging.
Introduced `accesslog` package for structured access log handling.
2 files
Added unit tests for `accesslog` package.
Added benchmark tests for access log performance.
2 files
Updated schema to include `access_logs` configuration.
Updated test coverage configuration for `accesslog` package.