@@ -64,12 +64,13 @@ const (
64
64
65
65
// TracingSpan is the tracing span configuration.
66
66
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"`
73
74
}
74
75
75
76
type AIStatisticsConfig struct {
@@ -294,10 +295,14 @@ func getUsage(data []byte) (model string, inputTokenUsage int64, outputTokenUsag
294
295
continue
295
296
}
296
297
modelObj := gjson .GetBytes (chunk , "model" )
298
+ if modelObj .Exists () {
299
+ model = modelObj .String ()
300
+ } else {
301
+ model = "unknown"
302
+ }
297
303
inputTokenObj := gjson .GetBytes (chunk , "usage.prompt_tokens" )
298
304
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 () {
301
306
inputTokenUsage = inputTokenObj .Int ()
302
307
outputTokenUsage = outputTokenObj .Int ()
303
308
ok = true
@@ -329,6 +334,9 @@ func setAttributeBySource(ctx wrapper.HttpContext, config AIStatisticsConfig, so
329
334
value = gjson .GetBytes (body , attribute .Value ).Value ()
330
335
default :
331
336
}
337
+ if (value == nil || value == "" ) && attribute .DefaultValue != "" {
338
+ value = attribute .DefaultValue
339
+ }
332
340
log .Debugf ("[attribute] source type: %s, key: %s, value: %+v" , source , key , value )
333
341
if attribute .ApplyToLog {
334
342
ctx .SetUserAttribute (key , value )
0 commit comments