mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-17 17:16:38 +08:00
feat: 添加上游响应时间追踪功能
- 响应标头添加 `x-one-api-upstream-time`,单位为 ms
This commit is contained in:
parent
8df4a2670b
commit
7a9c4e1b93
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/songquanpeng/one-api/relay/meta"
|
"github.com/songquanpeng/one-api/relay/meta"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetupCommonRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) {
|
func SetupCommonRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) {
|
||||||
@ -39,7 +40,10 @@ func DoRequestHelper(a Adaptor, c *gin.Context, meta *meta.Meta, requestBody io.
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DoRequest(c *gin.Context, req *http.Request) (*http.Response, error) {
|
func DoRequest(c *gin.Context, req *http.Request) (*http.Response, error) {
|
||||||
|
start := time.Now()
|
||||||
resp, err := client.HTTPClient.Do(req)
|
resp, err := client.HTTPClient.Do(req)
|
||||||
|
duration := time.Since(start).Milliseconds()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -48,5 +52,9 @@ func DoRequest(c *gin.Context, req *http.Request) (*http.Response, error) {
|
|||||||
}
|
}
|
||||||
_ = req.Body.Close()
|
_ = req.Body.Close()
|
||||||
_ = c.Request.Body.Close()
|
_ = c.Request.Body.Close()
|
||||||
|
|
||||||
|
// 写入上游响应时间
|
||||||
|
c.Writer.Header().Set("x-one-api-upstream-time", fmt.Sprintf("%d", duration))
|
||||||
|
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user