Skip to content

Commit

Permalink
Merge 899031c into adc1fb0
Browse files Browse the repository at this point in the history
  • Loading branch information
ashish-agg committed Mar 4, 2021
2 parents adc1fb0 + 899031c commit 15bb470
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
5 changes: 5 additions & 0 deletions runtime/constants.go
Expand Up @@ -51,6 +51,11 @@ const (
clientHTTPUnmarshalError = "client.http-unmarshal-error"
// clientTchannelReadError is the metric for tracking errors in reading tchannel response
clientTchannelUnmarshalError = "client.tchannel-unmarshal-error"

// shadow headers and environment
shadowHeader = "X-Shadow-Request"
shadowEnvironment = "shadow"
environmentKey = "env"
)

var knownMetrics = []string{
Expand Down
8 changes: 8 additions & 0 deletions runtime/server_http_request.go
Expand Up @@ -88,6 +88,7 @@ func NewServerHTTPRequest(
}
if endpoint.contextExtractor != nil {
headers := map[string]string{}

for k, v := range r.Header {
// TODO: this 0th element logic is probably not correct
headers[k] = v[0]
Expand All @@ -99,6 +100,13 @@ func NewServerHTTPRequest(

logFields = append(logFields, endpoint.contextExtractor.ExtractLogFields(ctx)...)
}

val := r.Header.Get(shadowHeader)
if val != "" {
scopeTags[environmentKey] = shadowEnvironment
logFields = append(logFields, zap.String(environmentKey, shadowEnvironment))
}

ctx = WithScopeTags(ctx, scopeTags)
ctx = WithLogFields(ctx, logFields...)

Expand Down
43 changes: 40 additions & 3 deletions runtime/server_http_request_test.go
Expand Up @@ -2238,7 +2238,7 @@ func TestSpanCreated(t *testing.T) {
assert.Equal(t, "200 OK", resp.Status)
}

func TestIncomingHTTPRequestServerLog(t *testing.T) {
func testIncomingHTTPRequestServerLog(t *testing.T, isShadowRequest bool, environment string) {
gateway, err := benchGateway.CreateGateway(
defaultTestConfig,
defaultTestOptions,
Expand Down Expand Up @@ -2273,13 +2273,20 @@ func TestIncomingHTTPRequestServerLog(t *testing.T) {
)
assert.NoError(t, err)

_, err = gateway.MakeRequest("GET", "/foo?bar=bar", nil, nil)
var headers map[string]string
if isShadowRequest {
headers = map[string]string{
"X-Shadow-Request": "true",
}
}
_, err = gateway.MakeRequest("GET", "/foo?bar=bar", headers, nil)
assert.NoError(t, err)

allLogs := bgateway.AllLogs()
assert.Equal(t, 1, len(allLogs["Finished an incoming server HTTP request with 200 status code"]))

tags := allLogs["Finished an incoming server HTTP request with 200 status code"][0]

dynamicHeaders := []string{
"requestUUID",
"remoteAddr",
Expand All @@ -2290,14 +2297,19 @@ func TestIncomingHTTPRequestServerLog(t *testing.T) {
"pid",
"timestamp-finished",
}

if isShadowRequest {
dynamicHeaders = append(dynamicHeaders, "X-Shadow-Request")
}

for _, dynamicValue := range dynamicHeaders {
assert.Contains(t, tags, dynamicValue)
delete(tags, dynamicValue)
}

expectedValues := map[string]interface{}{
"msg": "Finished an incoming server HTTP request with 200 status code",
"env": "test",
"env": environment,
"level": "debug",
"zone": "unknown",
"service": "example-gateway",
Expand All @@ -2319,3 +2331,28 @@ func TestIncomingHTTPRequestServerLog(t *testing.T) {
assert.Equal(t, expectedValue, tags[expectedKey], "unexpected header %q", expectedKey)
}
}

func TestIncomingHTTPRequestServerLogForDiffRequestTypes(t *testing.T) {
tests := []struct {
name string
isShadowRequest bool
expectedEnvironment string
}{
{
"Test incoming http request server log for normal request",
false,
"test",
},
{
"Test incoming http request server log for shadow request",
true,
"shadow",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testIncomingHTTPRequestServerLog(t, tt.isShadowRequest, tt.expectedEnvironment)
})
}
}

0 comments on commit 15bb470

Please sign in to comment.