Go API Documentation

github.com/TykTechnologies/tyk/internal/httputil/accesslog

No package summary is available.

Package

Files: 2. Third party imports: 1. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Types

Record

Record is a representation of a transaction log in the Gateway.

Field name Field type Comment
fields

logrus.Fields

No comment on field.
type Record struct {
	fields logrus.Fields
}

Functions

func Filter

Filter filters the input logrus fields and retains only the allowed fields. The function is case sensitive so keys have to match the case exactly.

func Filter(in logrus.Fields, allowedFields []string) logrus.Fields {
	if len(allowedFields) == 0 {
		return in
	}

	// Create a map to quickly check if a field is allowed.
	allowed := make(map[string]struct{}, len(allowedFields))
	for _, field := range allowedFields {
		allowed[field] = struct{}{}
	}

	result := make(logrus.Fields, len(allowedFields))

	// Add the "prefix" field by default, if it exists in the input
	if prefix, exists := in["prefix"]; exists {
		result["prefix"] = prefix
	}

	// Filter keys based on config
	for key, value := range in {
		if _, exists := allowed[key]; exists {
			result[key] = value
		}
	}

	return result
}

Cognitive complexity: 15, Cyclomatic complexity: 6

Uses: logrus.Fields.

func NewRecord

NewRecord returns a Record object.

func NewRecord() *Record {
	fields := logrus.Fields{
		"prefix": "access-log",
	}
	return &Record{
		fields: fields,
	}
}

Cognitive complexity: 2, Cyclomatic complexity: 1

Uses: logrus.Fields.

func (*Record) Fields

Fields returns a logrus.Fields intended for logging.

func (a *Record) Fields(allowedKeys []string) logrus.Fields {
	return Filter(a.fields, allowedKeys)
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Record) WithApiKey

WithApiKey sets the access token from the request under APIKey. The access token is obfuscated, or hashed depending on passed arguments.

func (a *Record) WithApiKey(req *http.Request, hashKeys bool, obfuscate func(string) string) *Record {
	token := ctx.GetAuthToken(req)
	if !hashKeys {
		a.fields["api_key"] = obfuscate(token)
	} else {
		a.fields["api_key"] = crypto.HashKey(token, hashKeys)
	}
	return a
}

Cognitive complexity: 4, Cyclomatic complexity: 2

Uses: crypto.HashKey, ctx.GetAuthToken.

func (*Record) WithRequest

WithRequest fills fields from the http request.

func (a *Record) WithRequest(req *http.Request, latency analytics.Latency) *Record {
	upstreamAddress := &url.URL{
		Scheme:	req.URL.Scheme,
		Host:	req.URL.Host,
		Path:	req.URL.Path,
	}

	// Keep the sort in sync with config.AccessLog.Template godoc.
	a.fields["client_ip"] = request.RealIP(req)
	a.fields["host"] = req.Host
	a.fields["latency_total"] = latency.Total
	a.fields["method"] = req.Method
	a.fields["path"] = req.URL.Path
	a.fields["protocol"] = req.Proto
	a.fields["remote_addr"] = req.RemoteAddr
	a.fields["upstream_addr"] = upstreamAddress.String()
	a.fields["upstream_latency"] = latency.Upstream
	a.fields["user_agent"] = req.UserAgent()
	return a
}

Cognitive complexity: 1, Cyclomatic complexity: 1

Uses: request.RealIP, url.URL.

func (*Record) WithResponse

WithResponse fills response details into the log fields.

func (a *Record) WithResponse(resp *http.Response) *Record {
	a.fields["status"] = resp.StatusCode
	return a
}

Cognitive complexity: 0, Cyclomatic complexity: 1


Tests

Files: 2. Third party imports: 2. Imports from organisation: 0. Tests: 2. Benchmarks: 0.

Test functions

TestFilter

References: accesslog.Filter, assert.Equal, logrus.Fields.

TestRecord

References: accesslog.NewRecord, analytics.Latency, assert.Equal, http.MethodGet, http.Response, http.StatusOK, httptest.NewRequest, logrus.Fields, request.RealIP.