Skip to content

Commit

Permalink
added tag headers to traffic logs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kofoworola committed Jan 6, 2025
1 parent f0fcb3f commit 99d87fd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
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 analytic 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
49 changes: 49 additions & 0 deletions apidef/oas/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,55 @@ func TestGlobal(t *testing.T) {
})
}

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

var emptyTrafficLogs TrafficLogs

var convertedAPI apidef.APIDefinition
convertedAPI.SetDisabledFlags()
emptyTrafficLogs.ExtractTo(&convertedAPI)

var resultTrafficLogs TrafficLogs
resultTrafficLogs.Fill(convertedAPI)

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

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

var convertedAPI apidef.APIDefinition
convertedAPI.SetDisabledFlags()
trafficLogs.ExtractTo(&convertedAPI)
assert.Equal(t, trafficLogs.TagHeaders, convertedAPI.TagHeaders)
assert.False(t, convertedAPI.DoNotTrack)

var resultTrafficLogs TrafficLogs
resultTrafficLogs.Fill(convertedAPI)
assert.Equal(t, trafficLogs.TagHeaders, resultTrafficLogs.TagHeaders)
assert.Equal(t, trafficLogs.Enabled, resultTrafficLogs.Enabled)
})

t.Run("enabled with no tag header", func(t *testing.T) {
t.Parallel()
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

0 comments on commit 99d87fd

Please sign in to comment.