Skip to content

Commit 2a0e090

Browse files
committed
1. 修复自动重定向插件bug
2. loki新增输出字段 3. influxdb新增输出字段
1 parent a9be828 commit 2a0e090

File tree

9 files changed

+116
-29
lines changed

9 files changed

+116
-29
lines changed

ai-convert/message.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,12 @@ type Error struct {
6161
Code string `json:"code"`
6262
}
6363

64-
func getTokens(text string) int {
65-
tkm, _ := tiktoken.GetEncoding("cl100k_base") // 使用 OpenAI 的分词模型
64+
func getTokens(text string, model string) int {
65+
tkm, err := tiktoken.EncodingForModel(model)
66+
if err != nil {
67+
tkm, _ = tiktoken.GetEncoding("cl100k_base") // 使用 OpenAI 的分词模型
68+
}
69+
6670
tokens := tkm.Encode(text, nil, nil)
6771
return len(tokens)
6872
}

ai-convert/openai.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (o *OpenAIConvert) RequestConvert(ctx eoscContext.EoContext, extender map[s
7878
chatRequest.Config.Model = GetAIModel(ctx)
7979
}
8080
for _, msg := range chatRequest.Config.Messages {
81-
promptToken += getTokens(msg.Content)
81+
promptToken += getTokens(msg.Content, chatRequest.Config.Model)
8282
}
8383
SetAIModelInputToken(httpContext, promptToken)
8484
httpContext.Proxy().AppendStreamBodyHandle(o.streamHandler)
@@ -232,7 +232,7 @@ func (o *OpenAIConvert) streamHandler(ctx http_service.IHttpContext, p []byte) (
232232
return p, nil
233233
}
234234
if len(resp.Choices) > 0 {
235-
outputToken += getTokens(resp.Choices[0].Delta.Content)
235+
outputToken += getTokens(resp.Choices[0].Delta.Content, resp.Model)
236236
totalToken += outputToken
237237
}
238238
if resp.Usage != nil {

drivers/output/loki/output.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ func (o *Output) doLoop() {
160160
return
161161
}
162162
data, _ := json.Marshal(entry)
163+
log.Infof("send data to loki: %s", string(data))
163164
req, err := o.genRequest(data)
164165
if err != nil {
165166
log.Errorf("gen request error: %v,data is %s", err, string(data))

drivers/plugins/auto-redirect/handler.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ func (r *handler) DoHttpFilter(ctx http_service.IHttpContext, next eocontext.ICh
5252
err := next.DoChain(ctx)
5353
if err != nil {
5454
log.Error(err)
55-
return err
55+
if !r.autoRedirect || !fasthttp.StatusCodeIsRedirect(ctx.Response().StatusCode()) {
56+
return err
57+
}
5658
}
5759
}
5860
if !r.autoRedirect {
@@ -73,6 +75,9 @@ func (r *handler) DoHttpFilter(ctx http_service.IHttpContext, next eocontext.ICh
7375
return err
7476
}
7577
}
78+
if !fasthttp.StatusCodeIsRedirect(ctx.Response().StatusCode()) {
79+
return nil
80+
}
7681
return fmt.Errorf("too many redirects")
7782
}
7883

@@ -96,7 +101,10 @@ func redirect(ctx http_service.IHttpContext) error {
96101
ctx.SetBalance(balanceHandler)
97102
}
98103
ctx.Proxy().URI().SetPath(u.Path)
99-
ctx.Proxy().URI().SetRawQuery(u.Query().Encode())
104+
for k, v := range u.Query() {
105+
ctx.Proxy().URI().SetQuery(k, v[0])
106+
}
107+
//ctx.Proxy().URI().SetRawQuery(u.Query().Encode())
100108
//ctx.Proxy().URI().SetPath(u.RawPath)
101109

102110
err = ctx.GetComplete().Complete(ctx)

drivers/plugins/monitor/monitor.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,27 @@ func (l *worker) DoFilter(ctx eocontext.EoContext, next eocontext.IChain) (err e
2121
return http_service.DoHttpFilter(l, ctx, next)
2222
}
2323

24+
func (l *worker) bodyFinish(ctx http_service.IHttpContext) {
25+
points := monitor_entry.ReadProxy(ctx)
26+
points = append(points, monitor_entry.ReadRequest(ctx)...)
27+
monitorManager.Output(l.Id(), points)
28+
return
29+
}
30+
2431
func (l *worker) DoHttpFilter(ctx http_service.IHttpContext, next eocontext.IChain) (err error) {
32+
ctx.Proxy().AppendBodyFinish(l.bodyFinish)
2533
log.Debug("start monitor...")
2634
apiID := ctx.GetLabel("api_id")
2735
monitorManager.ConcurrencyAdd(apiID, 1)
2836
err = next.DoChain(ctx)
2937
if err != nil {
3038
log.Error(err)
3139
}
32-
points := monitor_entry.ReadProxy(ctx)
33-
points = append(points, monitor_entry.ReadRequest(ctx)...)
34-
monitorManager.Output(l.Id(), points)
40+
41+
if ctx.Response().IsBodyStream() {
42+
return nil
43+
}
44+
l.bodyFinish(ctx)
3545
return nil
3646
}
3747

entries/http-entry/reader.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,21 @@ var (
274274
}),
275275
"body": Fields{
276276
"": ReadFunc(func(name string, ctx http_service.IHttpContext) (interface{}, bool) {
277-
return string(ctx.Response().GetBody()), true
277+
body := string(ctx.Response().GetBody())
278+
encoding := ctx.Response().Headers().Get("Content-Encoding")
279+
if encoding == "gzip" {
280+
reader, err := gzip.NewReader(bytes.NewReader([]byte(body)))
281+
if err != nil {
282+
return "", false
283+
}
284+
defer reader.Close()
285+
data, err := io.ReadAll(reader)
286+
if err != nil {
287+
return "", false
288+
}
289+
return string(data), true
290+
}
291+
return body, true
278292
}),
279293
},
280294
"header": ReadFunc(func(name string, ctx http_service.IHttpContext) (interface{}, bool) {

entries/monitor-entry/label.go

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,43 @@
11
package monitor_entry
22

3+
import http_context "github.com/eolinker/eosc/eocontext/http-context"
4+
35
var (
4-
LabelApi = "api"
5-
LabelApp = "app"
6-
LabelUpstream = "upstream"
7-
LabelHandler = "handler"
8-
LabelProvider = "provider"
6+
LabelApi = "api"
7+
LabelApp = "app"
8+
LabelUpstream = "upstream"
9+
LabelHandler = "handler"
10+
LabelProvider = "provider"
11+
LabelAPIKind = "api_kind"
12+
LabelStatusCode = "status_code"
913
)
1014

1115
var labels = map[string]string{
12-
LabelApi: "api",
13-
LabelApp: "application",
14-
LabelHandler: "handler",
15-
LabelUpstream: "service",
16-
LabelProvider: "provider",
16+
LabelApi: "api",
17+
LabelApp: "application",
18+
LabelHandler: "handler",
19+
LabelUpstream: "service",
20+
LabelProvider: "provider",
21+
LabelAPIKind: "api_kind",
22+
LabelStatusCode: "status_code",
23+
}
24+
25+
type GetLabel func(ctx http_context.IHttpContext) string
26+
27+
var readLabel = map[string]GetLabel{
28+
"status_code": func(ctx http_context.IHttpContext) string {
29+
statusCode := ctx.Response().StatusCode()
30+
switch {
31+
case statusCode >= 200 && statusCode < 300:
32+
return "2xx"
33+
case statusCode >= 300 && statusCode < 400:
34+
return "3xx"
35+
case statusCode >= 400 && statusCode < 500:
36+
return "4xx"
37+
case statusCode >= 500 && statusCode < 600:
38+
return "5xx"
39+
default:
40+
return "other"
41+
}
42+
},
1743
}

entries/monitor-entry/request-reader.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ var requestFields = []string{
2020
"retry",
2121
"timing",
2222
"status",
23+
"input_token",
24+
"output_token",
25+
"total_token",
2326
}
2427

2528
type RequestReadFunc func(ctx http_context.IHttpContext) (interface{}, bool)
@@ -31,6 +34,10 @@ func ReadRequest(ctx http_context.IHttpContext) []IPoint {
3134
}
3235

3336
for key, label := range labels {
37+
if v, ok := readLabel[key]; ok {
38+
tags[label] = v(ctx)
39+
continue
40+
}
3441
value := ctx.GetLabel(label)
3542
if value == "" {
3643
value = "-"
@@ -73,23 +80,39 @@ var request = map[string]RequestReadFunc{
7380
"method": func(ctx http_context.IHttpContext) (interface{}, bool) {
7481
return ctx.Request().Method(), true
7582
},
76-
//"path": func(ctx http_context.IHttpContext) (interface{}, bool) {
77-
// return ctx.Request().URI().Path(), true
78-
//},
79-
//"ip": func(ctx http_context.IHttpContext) (interface{}, bool) {
80-
// return ctx.GetLabel("ip"), true
81-
//},
8283
"status": func(ctx http_context.IHttpContext) (interface{}, bool) {
8384
return ctx.Response().StatusCode(), true
8485
},
8586
"timing": func(ctx http_context.IHttpContext) (interface{}, bool) {
8687
return time.Now().Sub(ctx.AcceptTime()).Milliseconds(), true
8788
},
89+
"input_token": func(ctx http_context.IHttpContext) (interface{}, bool) {
90+
value := ctx.Value("ai_model_input_token")
91+
if v, ok := value.(int); ok {
92+
return v, true
93+
}
94+
return 0, true
95+
},
96+
"output_token": func(ctx http_context.IHttpContext) (interface{}, bool) {
97+
value := ctx.Value("ai_model_output_token")
98+
if v, ok := value.(int); ok {
99+
return v, true
100+
}
101+
return 0, true
102+
},
103+
"total_token": func(ctx http_context.IHttpContext) (interface{}, bool) {
104+
value := ctx.Value("ai_model_total_token")
105+
if v, ok := value.(int); ok {
106+
return v, true
107+
}
108+
return 0, true
109+
},
110+
88111
"request": func(ctx http_context.IHttpContext) (interface{}, bool) {
89112
return ctx.Request().ContentLength(), true
90113
},
91114
"response": func(ctx http_context.IHttpContext) (interface{}, bool) {
92-
return ctx.Response().ContentLength(), true
115+
return len(ctx.Response().GetBody()), true
93116
},
94117
"retry": func(ctx http_context.IHttpContext) (interface{}, bool) {
95118
length := len(ctx.Proxies())

utils/response/response.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package response
22

33
import (
44
"fmt"
5+
"net/http"
6+
57
http_entry "github.com/eolinker/apinto/entries/http-entry"
68
"github.com/eolinker/eosc/eocontext"
79
http_context "github.com/eolinker/eosc/eocontext/http-context"
810
"github.com/eolinker/eosc/metrics"
9-
"net/http"
1011
)
1112

1213
type IResponse interface {
@@ -17,7 +18,7 @@ type Response struct {
1718
StatusCode int `json:"status_code" label:"HTTP状态码"`
1819
ContentType string `json:"content_type" label:"Content-Type"`
1920
Charset string `json:"charset" label:"Charset"`
20-
Headers []Header `json:"headers" label:"Header参数"` //key:value
21+
Headers []Header `json:"header" label:"Header参数"` //key:value
2122
Body string `json:"body" label:"Body" description:"body模版, 支持 #{label} 语法"`
2223
}
2324

0 commit comments

Comments
 (0)