mirror of
https://github.com/alibaba/higress.git
synced 2026-06-02 17:17:27 +08:00
support default value (#1873)
This commit is contained in:
@@ -31,6 +31,7 @@ Attribute 配置说明:
|
|||||||
| `key` | string | 必填 | - | attrribute 名称 |
|
| `key` | string | 必填 | - | attrribute 名称 |
|
||||||
| `value_source` | string | 必填 | - | attrribute 取值来源,可选值为 `fixed_value`, `request_header`, `request_body`, `response_header`, `response_body`, `response_streaming_body` |
|
| `value_source` | string | 必填 | - | attrribute 取值来源,可选值为 `fixed_value`, `request_header`, `request_body`, `response_header`, `response_body`, `response_streaming_body` |
|
||||||
| `value` | string | 必填 | - | attrribute 取值 key value/path |
|
| `value` | string | 必填 | - | attrribute 取值 key value/path |
|
||||||
|
| `default_value` | string | 非必填 | - | attrribute 默认值 |
|
||||||
| `rule` | string | 非必填 | - | 从流式响应中提取 attrribute 的规则,可选值为 `first`, `replace`, `append`|
|
| `rule` | string | 非必填 | - | 从流式响应中提取 attrribute 的规则,可选值为 `first`, `replace`, `append`|
|
||||||
| `apply_to_log` | bool | 非必填 | false | 是否将提取的信息记录在日志中 |
|
| `apply_to_log` | bool | 非必填 | false | 是否将提取的信息记录在日志中 |
|
||||||
| `apply_to_span` | bool | 非必填 | false | 是否将提取的信息记录在链路追踪span中 |
|
| `apply_to_span` | bool | 非必填 | false | 是否将提取的信息记录在链路追踪span中 |
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ Attribute Configuration instructions:
|
|||||||
| `key` | string | required | - | attrribute key |
|
| `key` | string | required | - | attrribute key |
|
||||||
| `value_source` | string | required | - | attrribute value source, optional values are `fixed_value`, `request_header`, `request_body`, `response_header`, `response_body`, `response_streaming_body` |
|
| `value_source` | string | required | - | attrribute value source, optional values are `fixed_value`, `request_header`, `request_body`, `response_header`, `response_body`, `response_streaming_body` |
|
||||||
| `value` | string | required | - | how to get attrribute value |
|
| `value` | string | required | - | how to get attrribute value |
|
||||||
|
| `default_value` | string | optional | - | default value for attribute |
|
||||||
| `rule` | string | optional | - | Rule to extract attribute from streaming response, optional values are `first`, `replace`, `append`|
|
| `rule` | string | optional | - | Rule to extract attribute from streaming response, optional values are `first`, `replace`, `append`|
|
||||||
| `apply_to_log` | bool | optional | false | Whether to record the extracted information in the log |
|
| `apply_to_log` | bool | optional | false | Whether to record the extracted information in the log |
|
||||||
| `apply_to_span` | bool | optional | false | Whether to record the extracted information in the link tracking span |
|
| `apply_to_span` | bool | optional | false | Whether to record the extracted information in the link tracking span |
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ type Attribute struct {
|
|||||||
Key string `json:"key"`
|
Key string `json:"key"`
|
||||||
ValueSource string `json:"value_source"`
|
ValueSource string `json:"value_source"`
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
|
DefaultValue string `json:"default_value,omitempty"`
|
||||||
Rule string `json:"rule,omitempty"`
|
Rule string `json:"rule,omitempty"`
|
||||||
ApplyToLog bool `json:"apply_to_log,omitempty"`
|
ApplyToLog bool `json:"apply_to_log,omitempty"`
|
||||||
ApplyToSpan bool `json:"apply_to_span,omitempty"`
|
ApplyToSpan bool `json:"apply_to_span,omitempty"`
|
||||||
@@ -294,10 +295,14 @@ func getUsage(data []byte) (model string, inputTokenUsage int64, outputTokenUsag
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
modelObj := gjson.GetBytes(chunk, "model")
|
modelObj := gjson.GetBytes(chunk, "model")
|
||||||
|
if modelObj.Exists() {
|
||||||
|
model = modelObj.String()
|
||||||
|
} else {
|
||||||
|
model = "unknown"
|
||||||
|
}
|
||||||
inputTokenObj := gjson.GetBytes(chunk, "usage.prompt_tokens")
|
inputTokenObj := gjson.GetBytes(chunk, "usage.prompt_tokens")
|
||||||
outputTokenObj := gjson.GetBytes(chunk, "usage.completion_tokens")
|
outputTokenObj := gjson.GetBytes(chunk, "usage.completion_tokens")
|
||||||
if modelObj.Exists() && inputTokenObj.Exists() && outputTokenObj.Exists() {
|
if inputTokenObj.Exists() && outputTokenObj.Exists() {
|
||||||
model = modelObj.String()
|
|
||||||
inputTokenUsage = inputTokenObj.Int()
|
inputTokenUsage = inputTokenObj.Int()
|
||||||
outputTokenUsage = outputTokenObj.Int()
|
outputTokenUsage = outputTokenObj.Int()
|
||||||
ok = true
|
ok = true
|
||||||
@@ -329,6 +334,9 @@ func setAttributeBySource(ctx wrapper.HttpContext, config AIStatisticsConfig, so
|
|||||||
value = gjson.GetBytes(body, attribute.Value).Value()
|
value = gjson.GetBytes(body, attribute.Value).Value()
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
if (value == nil || value == "") && attribute.DefaultValue != "" {
|
||||||
|
value = attribute.DefaultValue
|
||||||
|
}
|
||||||
log.Debugf("[attribute] source type: %s, key: %s, value: %+v", source, key, value)
|
log.Debugf("[attribute] source type: %s, key: %s, value: %+v", source, key, value)
|
||||||
if attribute.ApplyToLog {
|
if attribute.ApplyToLog {
|
||||||
ctx.SetUserAttribute(key, value)
|
ctx.SetUserAttribute(key, value)
|
||||||
|
|||||||
Reference in New Issue
Block a user