Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
凌波微步_大先生
dashboard
提交
e5eac7b6
D
dashboard
项目概览
凌波微步_大先生
/
dashboard
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dashboard
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e5eac7b6
编写于
1月 20, 2016
作者:
P
Piotr Bryk
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #239 from maciaszczykm/request-logging
Request and response logging in the backend
上级
5eeae11f
66db0f12
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
163 addition
and
7 deletion
+163
-7
src/app/backend/apihandler.go
src/app/backend/apihandler.go
+54
-0
src/test/backend/apihandler_test.go
src/test/backend/apihandler_test.go
+102
-0
src/test/backend/replicasetlist_test.go
src/test/backend/replicasetlist_test.go
+7
-7
未找到文件。
src/app/backend/apihandler.go
浏览文件 @
e5eac7b6
...
...
@@ -15,20 +15,70 @@
package
main
import
(
"fmt"
"log"
"net/http"
"strconv"
"strings"
restful
"github.com/emicklei/go-restful"
client
"k8s.io/kubernetes/pkg/client/unversioned"
)
const
(
Colon
=
":"
RequestLogString
=
"Incoming %s %s %s request from %s:
\n
%v"
ResponseLogString
=
"Outcoming response to %s with %d status code"
)
// Web-service filter function used for request and response logging.
func
wsLogger
(
req
*
restful
.
Request
,
resp
*
restful
.
Response
,
chain
*
restful
.
FilterChain
)
{
remoteAddr
:=
GetRemoteAddr
(
req
)
log
.
Printf
(
FormatRequestLog
(
req
,
remoteAddr
))
chain
.
ProcessFilter
(
req
,
resp
)
log
.
Printf
(
FormatResponseLog
(
resp
,
remoteAddr
))
}
// Returns remote address of the request (without port number).
func
GetRemoteAddr
(
req
*
restful
.
Request
)
string
{
if
strings
.
Contains
(
req
.
Request
.
RemoteAddr
,
Colon
)
{
return
strings
.
Split
(
req
.
Request
.
RemoteAddr
,
Colon
)[
0
]
}
else
{
return
req
.
Request
.
RemoteAddr
}
}
// Formats request log string.
func
FormatRequestLog
(
req
*
restful
.
Request
,
remoteAddr
string
)
string
{
reqBody
:=
make
([]
byte
,
req
.
Request
.
ContentLength
)
if
req
.
Request
.
Body
!=
nil
{
req
.
Request
.
Body
.
Read
(
reqBody
)
}
reqURI
:=
""
if
req
.
Request
.
URL
!=
nil
{
reqURI
=
req
.
Request
.
URL
.
RequestURI
()
}
return
fmt
.
Sprintf
(
RequestLogString
,
req
.
Request
.
Proto
,
req
.
Request
.
Method
,
reqURI
,
remoteAddr
,
reqBody
)
}
// Formats response log string.
// TODO(maciaszczykm): Display response content too.
func
FormatResponseLog
(
resp
*
restful
.
Response
,
remoteAddr
string
)
string
{
return
fmt
.
Sprintf
(
ResponseLogString
,
remoteAddr
,
resp
.
StatusCode
())
}
// Creates a new HTTP handler that handles all requests to the API of the backend.
func
CreateHttpApiHandler
(
client
*
client
.
Client
)
http
.
Handler
{
apiHandler
:=
ApiHandler
{
client
}
wsContainer
:=
restful
.
NewContainer
()
deployWs
:=
new
(
restful
.
WebService
)
deployWs
.
Filter
(
wsLogger
)
deployWs
.
Path
(
"/api/appdeployments"
)
.
Consumes
(
restful
.
MIME_JSON
)
.
Produces
(
restful
.
MIME_JSON
)
...
...
@@ -45,6 +95,7 @@ func CreateHttpApiHandler(client *client.Client) http.Handler {
wsContainer
.
Add
(
deployWs
)
replicaSetWs
:=
new
(
restful
.
WebService
)
replicaSetWs
.
Filter
(
wsLogger
)
replicaSetWs
.
Path
(
"/api/replicasets"
)
.
Consumes
(
restful
.
MIME_JSON
)
.
Produces
(
restful
.
MIME_JSON
)
...
...
@@ -70,6 +121,7 @@ func CreateHttpApiHandler(client *client.Client) http.Handler {
wsContainer
.
Add
(
replicaSetWs
)
namespacesWs
:=
new
(
restful
.
WebService
)
namespacesWs
.
Filter
(
wsLogger
)
namespacesWs
.
Path
(
"/api/namespaces"
)
.
Consumes
(
restful
.
MIME_JSON
)
.
Produces
(
restful
.
MIME_JSON
)
...
...
@@ -85,6 +137,7 @@ func CreateHttpApiHandler(client *client.Client) http.Handler {
wsContainer
.
Add
(
namespacesWs
)
logsWs
:=
new
(
restful
.
WebService
)
logsWs
.
Filter
(
wsLogger
)
logsWs
.
Path
(
"/api/logs"
)
.
Produces
(
restful
.
MIME_JSON
)
logsWs
.
Route
(
...
...
@@ -98,6 +151,7 @@ func CreateHttpApiHandler(client *client.Client) http.Handler {
wsContainer
.
Add
(
logsWs
)
eventsWs
:=
new
(
restful
.
WebService
)
eventsWs
.
Filter
(
wsLogger
)
eventsWs
.
Path
(
"/api/events"
)
.
Produces
(
restful
.
MIME_JSON
)
eventsWs
.
Route
(
...
...
src/test/backend/apihandler_test.go
0 → 100644
浏览文件 @
e5eac7b6
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package
main
import
(
"fmt"
"net/http"
"reflect"
"testing"
restful
"github.com/emicklei/go-restful"
)
func
TestGetRemoteAddr
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
request
*
restful
.
Request
expected
string
}{
{
&
restful
.
Request
{
Request
:
&
http
.
Request
{
RemoteAddr
:
"192.168.1.1:8080"
,
},
},
"192.168.1.1"
,
},
{
&
restful
.
Request
{
Request
:
&
http
.
Request
{
RemoteAddr
:
"192.168.1.2"
,
},
},
"192.168.1.2"
,
},
}
for
_
,
c
:=
range
cases
{
actual
:=
GetRemoteAddr
(
c
.
request
)
if
!
reflect
.
DeepEqual
(
actual
,
c
.
expected
)
{
t
.
Errorf
(
"GetRemoteAddr(%#v) == %#v, expected %#v"
,
c
.
request
,
actual
,
c
.
expected
)
}
}
}
func
TestFormatRequestLog
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
request
*
restful
.
Request
remoteAddr
string
expected
string
}{
{
&
restful
.
Request
{
Request
:
&
http
.
Request
{
RemoteAddr
:
"192.168.1.1:8080"
,
Proto
:
"HTTP 1.1"
,
Method
:
"GET"
,
},
},
"192.168.1.1"
,
fmt
.
Sprintf
(
RequestLogString
,
"HTTP 1.1"
,
"GET"
,
""
,
"192.168.1.1"
,
"[]"
),
},
}
for
_
,
c
:=
range
cases
{
actual
:=
FormatRequestLog
(
c
.
request
,
c
.
remoteAddr
)
if
!
reflect
.
DeepEqual
(
actual
,
c
.
expected
)
{
t
.
Errorf
(
"GetRemoteAddr(%#v, %#v) == %#v, expected %#v"
,
c
.
request
,
c
.
remoteAddr
,
actual
,
c
.
expected
)
}
}
}
func
TestFormatResponseLog
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
response
*
restful
.
Response
remoteAddr
string
expected
string
}{
{
&
restful
.
Response
{},
"192.168.1.1"
,
fmt
.
Sprintf
(
ResponseLogString
,
"192.168.1.1"
,
http
.
StatusOK
),
},
}
for
_
,
c
:=
range
cases
{
actual
:=
FormatResponseLog
(
c
.
response
,
c
.
remoteAddr
)
if
!
reflect
.
DeepEqual
(
actual
,
c
.
expected
)
{
t
.
Errorf
(
"GetRemoteAddr(%#v, %#v) == %#v, expected %#v"
,
c
.
response
,
c
.
remoteAddr
,
actual
,
c
.
expected
)
}
}
}
src/test/backend/replicasetlist_test.go
浏览文件 @
e5eac7b6
...
...
@@ -134,7 +134,7 @@ func TestGetReplicaSetList(t *testing.T) {
},
},
[]
api
.
Pod
{
api
.
Pod
{
{
ObjectMeta
:
api
.
ObjectMeta
{
Namespace
:
"namespace-1"
,
Labels
:
map
[
string
]
string
{
"app"
:
"my-name-1"
},
...
...
@@ -143,7 +143,7 @@ func TestGetReplicaSetList(t *testing.T) {
Phase
:
api
.
PodFailed
,
},
},
api
.
Pod
{
{
ObjectMeta
:
api
.
ObjectMeta
{
Namespace
:
"namespace-1"
,
Labels
:
map
[
string
]
string
{
"app"
:
"my-name-1"
},
...
...
@@ -152,7 +152,7 @@ func TestGetReplicaSetList(t *testing.T) {
Phase
:
api
.
PodFailed
,
},
},
api
.
Pod
{
{
ObjectMeta
:
api
.
ObjectMeta
{
Namespace
:
"namespace-1"
,
Labels
:
map
[
string
]
string
{
"app"
:
"my-name-1"
},
...
...
@@ -161,7 +161,7 @@ func TestGetReplicaSetList(t *testing.T) {
Phase
:
api
.
PodPending
,
},
},
api
.
Pod
{
{
ObjectMeta
:
api
.
ObjectMeta
{
Namespace
:
"namespace-2"
,
Labels
:
map
[
string
]
string
{
"app"
:
"my-name-1"
},
...
...
@@ -170,7 +170,7 @@ func TestGetReplicaSetList(t *testing.T) {
Phase
:
api
.
PodPending
,
},
},
api
.
Pod
{
{
ObjectMeta
:
api
.
ObjectMeta
{
Namespace
:
"namespace-1"
,
Labels
:
map
[
string
]
string
{
"app"
:
"my-name-1"
},
...
...
@@ -179,7 +179,7 @@ func TestGetReplicaSetList(t *testing.T) {
Phase
:
api
.
PodRunning
,
},
},
api
.
Pod
{
{
ObjectMeta
:
api
.
ObjectMeta
{
Namespace
:
"namespace-1"
,
Labels
:
map
[
string
]
string
{
"app"
:
"my-name-1"
},
...
...
@@ -188,7 +188,7 @@ func TestGetReplicaSetList(t *testing.T) {
Phase
:
api
.
PodSucceeded
,
},
},
api
.
Pod
{
{
ObjectMeta
:
api
.
ObjectMeta
{
Namespace
:
"namespace-1"
,
Labels
:
map
[
string
]
string
{
"app"
:
"my-name-1"
},
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录