Skip to content

Commit

Permalink
Added value tagging for headers (#1139)
Browse files Browse the repository at this point in the history
Fix #790
  • Loading branch information
lonelycode authored and buger committed Oct 23, 2017
1 parent ec643a9 commit c604f1a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
42 changes: 42 additions & 0 deletions analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,45 @@ func TestURLReplacer(t *testing.T) {
t.Error(recordCust.Path)
}
}

func TestTagHeaders(t *testing.T) {
req := testReq(t, "GET", "/tagmeplease", nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Tag-Me", "1")
req.Header.Set("X-Tag-Me2", "2")
req.Header.Set("X-Tag-Me3", "3")
req.Header.Set("X-Ignore-Me", "4")

existingTags := []string{"first", "second"}
existingTags = tagHeaders(req, []string{
"x-tag-me",
"x-tag-me2",
"x-tag-me3"},
existingTags)

if len(existingTags) == 2 {
t.Fatal("Existing tags have not been expanded")
}

if len(existingTags) != 5 {
t.Fatalf("Wrong number of tags, got %v, wanted %v", len(existingTags), 5)
}

check := map[string]bool{
"x-tag-me-1": true,
"x-tag-me2-2": true,
"x-tag-me3-3": true,
}

for _, t := range existingTags {
_, ok := check[t]
if ok {
delete(check, t)
}
}

if len(check) != 0 {
t.Fatalf("Header values not proerly set, got: %v, remnainder: %v", existingTags, check)
}

}
10 changes: 10 additions & 0 deletions api_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ func processSpec(spec *APISpec, apisByListen map[string]int,
"api_name": spec.Name,
}).Info("Loading API")

if len(spec.TagHeaders) > 0 {
// Ensure all headers marked for tagging are lowercase
lowerCaseHeaders := make([]string, len(spec.TagHeaders))
for i, k := range spec.TagHeaders {
lowerCaseHeaders[i] = strings.ToLower(k)

}
spec.TagHeaders = lowerCaseHeaders
}

if skipSpecBecauseInvalid(spec) {
log.WithFields(logrus.Fields{
"prefix": "main",
Expand Down
1 change: 1 addition & 0 deletions apidef/api_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ type APIDefinition struct {
Tags []string `bson:"tags" json:"tags"`
EnableContextVars bool `bson:"enable_context_vars" json:"enable_context_vars"`
ConfigData map[string]interface{} `bson:"config_data" json:"config_data"`
TagHeaders []string `bson:"tag_headers" json:"tag_headers"`
GlobalRateLimit GlobalRateLimit `bson:"global_rate_limit" json:"global_rate_limit"`
}

Expand Down
4 changes: 4 additions & 0 deletions handler_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func (e *ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, errMs
tags = session.Tags
}

if len(e.Spec.TagHeaders) > 0 {
tags = tagHeaders(r, e.Spec.TagHeaders, tags)
}

rawRequest := ""
rawResponse := ""
if recordDetail(r) {
Expand Down
28 changes: 28 additions & 0 deletions handler_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

cache "github.com/pmylund/go-cache"

"fmt"

"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/user"
)
Expand Down Expand Up @@ -43,6 +45,28 @@ type SuccessHandler struct {
BaseMiddleware
}

func tagHeaders(r *http.Request, th []string, tags []string) []string {
for k, v := range r.Header {
cleanK := strings.ToLower(k)
ok := false
for _, hname := range th {
if hname == cleanK {
ok = true
break
}
}

if ok {
for _, val := range v {
tagName := fmt.Sprintf("%s-%s", cleanK, val)
tags = append(tags, tagName)
}
}
}

return tags
}

func (s *SuccessHandler) RecordHit(r *http.Request, timing int64, code int, requestCopy *http.Request, responseCopy *http.Response) {

if s.Spec.DoNotTrack {
Expand Down Expand Up @@ -75,6 +99,10 @@ func (s *SuccessHandler) RecordHit(r *http.Request, timing int64, code int, requ
alias = session.Alias
}

if len(s.Spec.TagHeaders) > 0 {
tags = tagHeaders(r, s.Spec.TagHeaders, tags)
}

rawRequest := ""
rawResponse := ""
if recordDetail(r) {
Expand Down

0 comments on commit c604f1a

Please sign in to comment.