Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ratelimit metrics in the ai-token-ratelimit plugin #1917

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugins/wasm-go/extensions/ai-token-ratelimit/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
"strings"

"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
Expand Down Expand Up @@ -59,6 +60,7 @@ type ClusterKeyRateLimitConfig struct {
rejectedCode uint32 // 当请求超过阈值被拒绝时,返回的HTTP状态码
rejectedMsg string // 当请求超过阈值被拒绝时,返回的响应体
redisClient wrapper.RedisClient
counterMetrics map[string]proxywasm.MetricCounter // Metrics
}

type LimitRuleItem struct {
Expand Down
49 changes: 49 additions & 0 deletions plugins/wasm-go/extensions/ai-token-ratelimit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ func parseConfig(json gjson.Result, config *ClusterKeyRateLimitConfig, log wrapp
if err != nil {
return err
}
// Metric settings
config.counterMetrics = make(map[string]proxywasm.MetricCounter)
return nil
}

Expand Down Expand Up @@ -304,9 +306,56 @@ func getDownStreamIp(rule LimitRuleItem) (net.IP, error) {
return realIP, nil
}

func (config *ClusterKeyRateLimitConfig) incrementCounter(metricName string, inc uint64) {
if inc == 0 {
return
}
counter, ok := config.counterMetrics[metricName]
if !ok {
counter = proxywasm.DefineCounterMetric(metricName)
config.counterMetrics[metricName] = counter
}
counter.Increment(inc)
}

func generateMetricName(route, cluster, model, consumer, metricName string) string {
return fmt.Sprintf("route.%s.upstream.%s.model.%s.consumer.%s.metric.%s", route, cluster, model, consumer, metricName)
}

func getRouteName() (string, error) {
if raw, err := proxywasm.GetProperty([]string{"route_name"}); err != nil {
return "-", err
} else {
return string(raw), nil
}
}

func getClusterName() (string, error) {
if raw, err := proxywasm.GetProperty([]string{"cluster_name"}); err != nil {
return "-", err
} else {
return string(raw), nil
}
}

func rejected(config ClusterKeyRateLimitConfig, context LimitContext) {
headers := make(map[string][]string)
headers[RateLimitResetHeader] = []string{strconv.Itoa(context.reset)}
_ = proxywasm.SendHttpResponseWithDetail(
config.rejectedCode, "ai-token-ratelimit.rejected", reconvertHeaders(headers), []byte(config.rejectedMsg), -1)

route, err := getRouteName()
if err != nil {
route = "none"
}
cluster, err := getClusterName()
if err != nil {
cluster = "none"
}
consumer, err := proxywasm.GetHttpRequestHeader(ConsumerHeader)
if err != nil {
consumer = "none"
}
metricsName := generateMetricName(route, cluster, "none", consumer, "ai-token-ratelimit")
config.incrementCounter(metricsName, 1)
}