forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: Add Request Info Middleware (tikv#4526)
* close tikv#4494 Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * close tikv#4494: add priority comment Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * add request info middleware Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * close tikv#4494 Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * change service label getter and setter Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * add lock Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * add audit middleware Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * add audit middleware Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * tikv#4538 Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix check Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix check Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * change service label placement Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * merge master Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * add const service label and could be dynamically turned on and off service middleware Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix statics check Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix statics check Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix statics check Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix statics check Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix function name Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * add benchmark test Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix r.Body nil panic Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * add benchmark test Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * change export Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * change export Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix statics check Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix typo problem Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * change service label method Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix statics Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix router Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix router Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * add http proto Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * add http proto Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * change default value Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * use middleware func shortname Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix comment Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * change const to iota Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * change register method Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * for test Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * for test Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix race Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> * fix race Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> Co-authored-by: ShuNing <nolouch@gmail.com>
- Loading branch information
Showing
11 changed files
with
614 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2022 TiKV Project Authors. | ||
// | ||
// 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 requestutil | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// The key type is unexported to prevent collisions | ||
type key int | ||
|
||
const ( | ||
// requestInfoKey is the context key for the request compoenent. | ||
requestInfoKey key = iota | ||
) | ||
|
||
// WithRequestInfo returns a copy of parent in which the request info value is set | ||
func WithRequestInfo(parent context.Context, requestInfo RequestInfo) context.Context { | ||
return context.WithValue(parent, requestInfoKey, requestInfo) | ||
} | ||
|
||
// RequestInfoFrom returns the value of the request info key on the ctx | ||
func RequestInfoFrom(ctx context.Context) (RequestInfo, bool) { | ||
requestInfo, ok := ctx.Value(requestInfoKey).(RequestInfo) | ||
return requestInfo, ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2022 TiKV Project Authors. | ||
// | ||
// 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 requestutil | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "github.com/pingcap/check" | ||
) | ||
|
||
func Test(t *testing.T) { | ||
TestingT(t) | ||
} | ||
|
||
var _ = Suite(&testRequestContextSuite{}) | ||
|
||
type testRequestContextSuite struct { | ||
} | ||
|
||
func (s *testRequestContextSuite) TestRequestInfo(c *C) { | ||
ctx := context.Background() | ||
_, ok := RequestInfoFrom(ctx) | ||
c.Assert(ok, Equals, false) | ||
ctx = WithRequestInfo(ctx, | ||
RequestInfo{ | ||
ServiceLabel: "test label", | ||
Method: "POST", | ||
Component: "pdctl", | ||
IP: "localhost", | ||
URLParam: "{\"id\"=1}", | ||
BodyParam: "{\"state\"=\"Up\"}", | ||
TimeStamp: "2022", | ||
}) | ||
result, ok := RequestInfoFrom(ctx) | ||
c.Assert(result, NotNil) | ||
c.Assert(ok, Equals, true) | ||
c.Assert(result.ServiceLabel, Equals, "test label") | ||
c.Assert(result.Method, Equals, "POST") | ||
c.Assert(result.Component, Equals, "pdctl") | ||
c.Assert(result.IP, Equals, "localhost") | ||
c.Assert(result.URLParam, Equals, "{\"id\"=1}") | ||
c.Assert(result.BodyParam, Equals, "{\"state\"=\"Up\"}") | ||
c.Assert(result.TimeStamp, Equals, "2022") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2022 TiKV Project Authors. | ||
// | ||
// 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 requestutil | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/tikv/pd/pkg/apiutil" | ||
) | ||
|
||
// RequestInfo holds service information from http.Request | ||
type RequestInfo struct { | ||
ServiceLabel string | ||
Method string | ||
Component string | ||
IP string | ||
TimeStamp string | ||
URLParam string | ||
BodyParam string | ||
} | ||
|
||
// GetRequestInfo returns request info needed from http.Request | ||
func GetRequestInfo(r *http.Request) RequestInfo { | ||
return RequestInfo{ | ||
ServiceLabel: apiutil.GetRouteName(r), | ||
Method: fmt.Sprintf("%s/%s:%s", r.Proto, r.Method, r.URL.Path), | ||
Component: apiutil.GetComponentNameOnHTTP(r), | ||
IP: apiutil.GetIPAddrFromHTTPRequest(r), | ||
TimeStamp: time.Now().Local().String(), | ||
URLParam: getURLParam(r), | ||
BodyParam: getBodyParam(r), | ||
} | ||
} | ||
|
||
func getURLParam(r *http.Request) string { | ||
buf, err := json.Marshal(r.URL.Query()) | ||
if err != nil { | ||
return "" | ||
} | ||
return string(buf) | ||
} | ||
|
||
func getBodyParam(r *http.Request) string { | ||
if r.Body == nil { | ||
return "" | ||
} | ||
// http request body is a io.Reader between bytes.Reader and strings.Reader, it only has EOF error | ||
buf, _ := io.ReadAll(r.Body) | ||
r.Body.Close() | ||
bodyParam := string(buf) | ||
r.Body = io.NopCloser(bytes.NewBuffer(buf)) | ||
return bodyParam | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.