Skip to content
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-11910]: added tag headers to traffic logs and tests #6818

Merged
merged 9 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apidef/oas/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -1553,16 +1553,21 @@ type TrafficLogs struct {
// Enabled enables traffic log analytics for the API.
// Tyk classic API definition: `do_not_track`.
Enabled bool `bson:"enabled" json:"enabled"`
// TagHeaders is a string array of HTTP headers that can be extracted
// and transformed into analytics tags (statistics aggregated by tag, per hour).
TagHeaders []string `bson:"tagHeaders" json:"tagHeaders,omitempty"`
}

// Fill fills *TrafficLogs from apidef.APIDefinition.
func (t *TrafficLogs) Fill(api apidef.APIDefinition) {
t.Enabled = !api.DoNotTrack
t.TagHeaders = api.TagHeaders
}

// ExtractTo extracts *TrafficLogs into *apidef.APIDefinition.
func (t *TrafficLogs) ExtractTo(api *apidef.APIDefinition) {
api.DoNotTrack = !t.Enabled
api.TagHeaders = t.TagHeaders
}

// ContextVariables holds the configuration related to Tyk context variables.
Expand Down
46 changes: 46 additions & 0 deletions apidef/oas/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,52 @@ func TestGlobal(t *testing.T) {
})
}

func TestTrafficLogs(t *testing.T) {
t.Run("empty", func(t *testing.T) {

var emptyTrafficLogs TrafficLogs
var convertedAPI apidef.APIDefinition
var resultTrafficLogs TrafficLogs

convertedAPI.SetDisabledFlags()
emptyTrafficLogs.ExtractTo(&convertedAPI)

resultTrafficLogs.Fill(convertedAPI)

assert.Equal(t, emptyTrafficLogs, resultTrafficLogs)
})

t.Run("enabled with tag header", func(t *testing.T) {
var convertedAPI apidef.APIDefinition
var resultTrafficLogs TrafficLogs
trafficLogs := TrafficLogs{
Enabled: true,
TagHeaders: []string{"X-Team-Name"},
}

convertedAPI.SetDisabledFlags()
trafficLogs.ExtractTo(&convertedAPI)

assert.Equal(t, trafficLogs.TagHeaders, convertedAPI.TagHeaders)
assert.False(t, convertedAPI.DoNotTrack)

resultTrafficLogs.Fill(convertedAPI)

assert.Equal(t, trafficLogs, resultTrafficLogs)
})

t.Run("enabled with no tag header", func(t *testing.T) {
trafficLogs := TrafficLogs{
Enabled: true,
TagHeaders: []string{},
}
var convertedAPI apidef.APIDefinition
convertedAPI.SetDisabledFlags()
trafficLogs.ExtractTo(&convertedAPI)
assert.Empty(t, convertedAPI.TagHeaders)
})
}

func TestPluginConfig(t *testing.T) {
t.Parallel()
t.Run("empty", func(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion apidef/oas/oas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ func TestOAS_ExtractTo_ResetAPIDefinition(t *testing.T) {
"APIDefinition.ExpireAnalyticsAfter",
"APIDefinition.ResponseProcessors[0].Name",
"APIDefinition.ResponseProcessors[0].Options",
"APIDefinition.TagHeaders[0]",
kofoworola marked this conversation as resolved.
Show resolved Hide resolved
"APIDefinition.GraphQL.Enabled",
"APIDefinition.GraphQL.ExecutionMode",
"APIDefinition.GraphQL.Version",
Expand Down
6 changes: 6 additions & 0 deletions apidef/oas/schema/x-tyk-api-gateway.json
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,12 @@
"properties": {
"enabled": {
"type": "boolean"
},
"tagHeaders": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
Expand Down
File renamed without changes.
Loading