support default value (#1873)

This commit is contained in:
rinfx
2025-03-11 09:32:11 +08:00
committed by GitHub
parent d172cf4d19
commit 5a5af4ecbf
3 changed files with 18 additions and 8 deletions

View File

@@ -31,6 +31,7 @@ Attribute 配置说明:
| `key` | string | 必填 | - | attrribute 名称 |
| `value_source` | string | 必填 | - | attrribute 取值来源,可选值为 `fixed_value`, `request_header`, `request_body`, `response_header`, `response_body`, `response_streaming_body` |
| `value` | string | 必填 | - | attrribute 取值 key value/path |
| `default_value` | string | 非必填 | - | attrribute 默认值 |
| `rule` | string | 非必填 | - | 从流式响应中提取 attrribute 的规则,可选值为 `first`, `replace`, `append`|
| `apply_to_log` | bool | 非必填 | false | 是否将提取的信息记录在日志中 |
| `apply_to_span` | bool | 非必填 | false | 是否将提取的信息记录在链路追踪span中 |

View File

@@ -31,6 +31,7 @@ Attribute Configuration instructions:
| `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` | 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`|
| `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 |

View File

@@ -64,12 +64,13 @@ const (
// TracingSpan is the tracing span configuration.
type Attribute struct {
Key string `json:"key"`
ValueSource string `json:"value_source"`
Value string `json:"value"`
Rule string `json:"rule,omitempty"`
ApplyToLog bool `json:"apply_to_log,omitempty"`
ApplyToSpan bool `json:"apply_to_span,omitempty"`
Key string `json:"key"`
ValueSource string `json:"value_source"`
Value string `json:"value"`
DefaultValue string `json:"default_value,omitempty"`
Rule string `json:"rule,omitempty"`
ApplyToLog bool `json:"apply_to_log,omitempty"`
ApplyToSpan bool `json:"apply_to_span,omitempty"`
}
type AIStatisticsConfig struct {
@@ -294,10 +295,14 @@ func getUsage(data []byte) (model string, inputTokenUsage int64, outputTokenUsag
continue
}
modelObj := gjson.GetBytes(chunk, "model")
if modelObj.Exists() {
model = modelObj.String()
} else {
model = "unknown"
}
inputTokenObj := gjson.GetBytes(chunk, "usage.prompt_tokens")
outputTokenObj := gjson.GetBytes(chunk, "usage.completion_tokens")
if modelObj.Exists() && inputTokenObj.Exists() && outputTokenObj.Exists() {
model = modelObj.String()
if inputTokenObj.Exists() && outputTokenObj.Exists() {
inputTokenUsage = inputTokenObj.Int()
outputTokenUsage = outputTokenObj.Int()
ok = true
@@ -329,6 +334,9 @@ func setAttributeBySource(ctx wrapper.HttpContext, config AIStatisticsConfig, so
value = gjson.GetBytes(body, attribute.Value).Value()
default:
}
if (value == nil || value == "") && attribute.DefaultValue != "" {
value = attribute.DefaultValue
}
log.Debugf("[attribute] source type: %s, key: %s, value: %+v", source, key, value)
if attribute.ApplyToLog {
ctx.SetUserAttribute(key, value)