Skip to content

Commit 466a0a0

Browse files
authored
add option status code log from origin (#4)
* add status-code log Signed-off-by: taniwa <taniwa@yahoo-corp.jp> * update Signed-off-by: taniwa <taniwa@yahoo-corp.jp> * add option for origin log Signed-off-by: taniwa <taniwa@yahoo-corp.jp> * update Signed-off-by: taniwa <taniwa@yahoo-corp.jp> * fix Signed-off-by: taniwa <taniwa@yahoo-corp.jp> * add handler_test Signed-off-by: taniwa <taniwa@yahoo-corp.jp> Signed-off-by: taniwa <taniwa@yahoo-corp.jp>
1 parent e1d32f8 commit 466a0a0

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

config/config.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ type Proxy struct {
154154

155155
// Transport exposes http.Transport parameters
156156
Transport Transport `yaml:"transport,omitempty"`
157+
158+
// OriginLog represents log configuration from origin
159+
OriginLog OriginLog `yaml:"originLog"`
157160
}
158161

159162
// Authorization represents the detail authorization configuration.
@@ -287,6 +290,17 @@ type Transport struct {
287290
ForceAttemptHTTP2 bool `yaml:"forceAttemptHTTP2,omitempty"`
288291
}
289292

293+
// OriginLog represents log configuration from origin
294+
type OriginLog struct {
295+
StatusCode StatusCode `yaml:"statusCode"`
296+
}
297+
298+
// StatusCode represents statuscode log configuration
299+
type StatusCode struct {
300+
Enable bool `yaml:"enable"`
301+
Exclude []int `yaml:"exclude"`
302+
}
303+
290304
// New returns the decoded configuration YAML file as *Config struct. Returns non-nil error if any.
291305
func New(path string) (*Config, error) {
292306
f, err := os.OpenFile(path, os.O_RDONLY, 0o600)

config/config_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ func TestNew(t *testing.T) {
127127
ReadBufferSize: 0,
128128
ForceAttemptHTTP2: true,
129129
},
130+
OriginLog: OriginLog{
131+
StatusCode: StatusCode{
132+
Enable: true,
133+
Exclude: []int{200},
134+
},
135+
},
130136
},
131137
Authorization: Authorization{
132138
PublicKey: PublicKey{

handler/handler.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ func New(cfg config.Proxy, bp httputil.BufferPool, prov service.Authorizationd)
4545

4646
host := fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
4747

48+
var modifyResponse func(res *http.Response) error = nil
49+
if cfg.OriginLog.StatusCode.Enable {
50+
modifyResponse = func(res *http.Response) error {
51+
for _, statusCode := range cfg.OriginLog.StatusCode.Exclude {
52+
if statusCode == res.StatusCode {
53+
return nil
54+
}
55+
}
56+
glg.Infof("Origin request: %s %s, Response: status code: %d", res.Request.Method, res.Request.URL, res.StatusCode)
57+
return nil
58+
}
59+
}
60+
4861
return &httputil.ReverseProxy{
4962
BufferPool: bp,
5063
Director: func(r *http.Request) {
@@ -71,6 +84,7 @@ func New(cfg config.Proxy, bp httputil.BufferPool, prov service.Authorizationd)
7184

7285
*r = *req
7386
},
87+
ModifyResponse: modifyResponse,
7488
Transport: &transport{
7589
prov: prov,
7690
RoundTripper: transportFromCfg(cfg.Transport),

handler/handler_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,25 @@ func TestNew(t *testing.T) {
534534
return nil
535535
},
536536
},
537+
{
538+
name: "check originlog is used",
539+
args: args{
540+
cfg: config.Proxy{
541+
OriginLog: config.OriginLog{
542+
StatusCode: config.StatusCode{
543+
Enable: true,
544+
Exclude: []int{},
545+
},
546+
},
547+
},
548+
},
549+
checkFunc: func(h http.Handler) error {
550+
if h.(*httputil.ReverseProxy).ModifyResponse == nil {
551+
return errors.Errorf("unexpected ModifyResponse")
552+
}
553+
return nil
554+
},
555+
},
537556
}
538557
for _, tt := range tests {
539558
t.Run(tt.name, func(t *testing.T) {

test/data/example_config.yaml

100755100644
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ proxy:
4444
writeBufferSize: 0
4545
readBufferSize: 0
4646
forceAttemptHTTP2: true
47+
originLog:
48+
statusCode:
49+
enable: true
50+
exclude:
51+
- 200
4752
authorization:
4853
athenzDomains:
4954
- provider-domain1

0 commit comments

Comments
 (0)