Skip to content

Commit 5a5af4e

Browse files
authored
support default value (alibaba#1873)
1 parent d172cf4 commit 5a5af4e

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

plugins/wasm-go/extensions/ai-statistics/README.md

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

plugins/wasm-go/extensions/ai-statistics/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Attribute Configuration instructions:
3131
| `key` | string | required | - | attrribute key |
3232
| `value_source` | string | required | - | attrribute value source, optional values ​​are `fixed_value`, `request_header`, `request_body`, `response_header`, `response_body`, `response_streaming_body` |
3333
| `value` | string | required | - | how to get attrribute value |
34+
| `default_value` | string | optional | - | default value for attribute |
3435
| `rule` | string | optional | - | Rule to extract attribute from streaming response, optional values ​​are `first`, `replace`, `append`|
3536
| `apply_to_log` | bool | optional | false | Whether to record the extracted information in the log |
3637
| `apply_to_span` | bool | optional | false | Whether to record the extracted information in the link tracking span |

plugins/wasm-go/extensions/ai-statistics/main.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ const (
6464

6565
// TracingSpan is the tracing span configuration.
6666
type Attribute struct {
67-
Key string `json:"key"`
68-
ValueSource string `json:"value_source"`
69-
Value string `json:"value"`
70-
Rule string `json:"rule,omitempty"`
71-
ApplyToLog bool `json:"apply_to_log,omitempty"`
72-
ApplyToSpan bool `json:"apply_to_span,omitempty"`
67+
Key string `json:"key"`
68+
ValueSource string `json:"value_source"`
69+
Value string `json:"value"`
70+
DefaultValue string `json:"default_value,omitempty"`
71+
Rule string `json:"rule,omitempty"`
72+
ApplyToLog bool `json:"apply_to_log,omitempty"`
73+
ApplyToSpan bool `json:"apply_to_span,omitempty"`
7374
}
7475

7576
type AIStatisticsConfig struct {
@@ -294,10 +295,14 @@ func getUsage(data []byte) (model string, inputTokenUsage int64, outputTokenUsag
294295
continue
295296
}
296297
modelObj := gjson.GetBytes(chunk, "model")
298+
if modelObj.Exists() {
299+
model = modelObj.String()
300+
} else {
301+
model = "unknown"
302+
}
297303
inputTokenObj := gjson.GetBytes(chunk, "usage.prompt_tokens")
298304
outputTokenObj := gjson.GetBytes(chunk, "usage.completion_tokens")
299-
if modelObj.Exists() && inputTokenObj.Exists() && outputTokenObj.Exists() {
300-
model = modelObj.String()
305+
if inputTokenObj.Exists() && outputTokenObj.Exists() {
301306
inputTokenUsage = inputTokenObj.Int()
302307
outputTokenUsage = outputTokenObj.Int()
303308
ok = true
@@ -329,6 +334,9 @@ func setAttributeBySource(ctx wrapper.HttpContext, config AIStatisticsConfig, so
329334
value = gjson.GetBytes(body, attribute.Value).Value()
330335
default:
331336
}
337+
if (value == nil || value == "") && attribute.DefaultValue != "" {
338+
value = attribute.DefaultValue
339+
}
332340
log.Debugf("[attribute] source type: %s, key: %s, value: %+v", source, key, value)
333341
if attribute.ApplyToLog {
334342
ctx.SetUserAttribute(key, value)

0 commit comments

Comments
 (0)