@@ -4,13 +4,13 @@ import (
4
4
"encoding/json"
5
5
"log"
6
6
7
- "go.opentelemetry.io/otel/attribute"
8
7
"go.opentelemetry.io/otel/trace"
9
8
10
9
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
11
10
)
12
11
13
12
type SpanType string
13
+ type typeWrapperType string
14
14
15
15
const (
16
16
SpanTypeSpan SpanType = "span"
@@ -30,13 +30,10 @@ const (
30
30
SpanTypeConsumer SpanType = "consumer"
31
31
SpanTypeTask SpanType = "task"
32
32
SpanTypeUnknown SpanType = "unknown"
33
- )
34
33
35
- type SpanMetrics struct {
36
- PromptTokens * int `json:"prompt_tokens"`
37
- CompletionTokens * int `json:"completion_tokens"`
38
- Cost * float64 `json:"cost"`
39
- }
34
+ typeWrapperTypeJSON typeWrapperType = "json"
35
+ typeWrapperTypeText typeWrapperType = "text"
36
+ )
40
37
41
38
type SpanTimestamps struct {
42
39
StartedAtUnix int64 `json:"started_at"`
@@ -50,67 +47,99 @@ type SpanRAGContextChunk struct {
50
47
Content any `json:"content"`
51
48
}
52
49
53
- type LangWatchSpan struct {
50
+ type typeWrapper struct {
51
+ Type typeWrapperType `json:"type"`
52
+ Value any `json:"value"`
53
+ }
54
+
55
+ type Span struct {
54
56
trace.Span
55
57
}
56
58
57
- func (s * LangWatchSpan ) RecordInput (input any ) {
58
- jsonStr , err := json .Marshal (input )
59
+ func (s * Span ) RecordInput (input any ) {
60
+ jsonStr , err := json .Marshal (typeWrapper {
61
+ Type : typeWrapperTypeJSON ,
62
+ Value : input ,
63
+ })
59
64
if err != nil {
60
65
log .Default ().Printf ("error marshalling input: %v" , err )
61
66
}
62
67
63
- s .SetAttributes (attribute .String (AttributeLangWatchInputKey , string (jsonStr )))
68
+ s .SetAttributes (AttributeLangWatchInput .String (string (jsonStr )))
64
69
}
65
70
66
- func (s * LangWatchSpan ) RecordOutput (output any ) {
67
- jsonStr , err := json .Marshal (output )
71
+ func (s * Span ) RecordInputString (input string ) {
72
+ jsonStr , err := json .Marshal (typeWrapper {
73
+ Type : typeWrapperTypeText ,
74
+ Value : input ,
75
+ })
68
76
if err != nil {
69
- log .Default ().Printf ("error marshalling output : %v" , err )
77
+ log .Default ().Printf ("error marshalling input : %v" , err )
70
78
}
71
79
72
- s .SetAttributes (attribute .String (AttributeLangWatchOutputKey , string (jsonStr )))
80
+ s .SetAttributes (AttributeLangWatchInput .String (string (jsonStr )))
73
81
}
74
82
75
- func (s * LangWatchSpan ) SetType (spanType SpanType ) {
76
- s .SetAttributes (attribute .String (AttributeLangWatchSpanTypeKey , string (spanType )))
83
+ func (s * Span ) RecordOutput (output any ) {
84
+ jsonStr , err := json .Marshal (typeWrapper {
85
+ Type : typeWrapperTypeJSON ,
86
+ Value : output ,
87
+ })
88
+ if err != nil {
89
+ log .Default ().Printf ("error marshalling output: %v" , err )
90
+ }
91
+
92
+ s .SetAttributes (AttributeLangWatchOutput .String (string (jsonStr )))
77
93
}
78
94
79
- func (s * LangWatchSpan ) SetRequestModel (model string ) {
80
- s .SetAttributes (attribute .String (string (semconv .GenAiRequestModelKey ), model ))
95
+ func (s * Span ) RecordOutputString (output string ) {
96
+ jsonStr , err := json .Marshal (typeWrapper {
97
+ Type : typeWrapperTypeText ,
98
+ Value : output ,
99
+ })
100
+ if err != nil {
101
+ log .Default ().Printf ("error marshalling output: %v" , err )
102
+ }
103
+
104
+ s .SetAttributes (AttributeLangWatchOutput .String (string (jsonStr )))
81
105
}
82
106
83
- func (s * LangWatchSpan ) SetResponseModel ( model string ) {
84
- s .SetAttributes (attribute .String (string (semconv . GenAiResponseModelKey ), model ))
107
+ func (s * Span ) SetType ( spanType SpanType ) {
108
+ s .SetAttributes (AttributeLangWatchSpanType .String (string (spanType ) ))
85
109
}
86
110
87
- func (s * LangWatchSpan ) SetMetrics (metrics SpanMetrics ) {
88
- jsonStr , err := json .Marshal (metrics )
89
- if err != nil {
90
- log .Default ().Printf ("error marshalling metrics: %v" , err )
91
- }
111
+ func (s * Span ) SetRequestModel (model string ) {
112
+ s .SetAttributes (semconv .GenAiRequestModelKey .String (model ))
113
+ }
92
114
93
- s .SetAttributes (attribute .String (AttributeLangWatchMetricsKey , string (jsonStr )))
115
+ func (s * Span ) SetResponseModel (model string ) {
116
+ s .SetAttributes (semconv .GenAiResponseModelKey .String (model ))
94
117
}
95
118
96
- func (s * LangWatchSpan ) SetTimestamps (timestamps SpanTimestamps ) {
97
- jsonStr , err := json .Marshal (timestamps )
119
+ func (s * Span ) SetTimestamps (timestamps SpanTimestamps ) {
120
+ jsonStr , err := json .Marshal (typeWrapper {
121
+ Type : typeWrapperTypeJSON ,
122
+ Value : timestamps ,
123
+ })
98
124
if err != nil {
99
125
log .Default ().Printf ("error marshalling timestamps: %v" , err )
100
126
}
101
127
102
- s .SetAttributes (attribute .String (AttributeLangWatchTimestampsKey , string (jsonStr )))
128
+ s .SetAttributes (AttributeLangWatchTimestamps .String (string (jsonStr )))
103
129
}
104
130
105
- func (s * LangWatchSpan ) SetRAGContextChunks (contexts []SpanRAGContextChunk ) {
106
- jsonStr , err := json .Marshal (contexts )
131
+ func (s * Span ) SetRAGContextChunks (contexts []SpanRAGContextChunk ) {
132
+ jsonStr , err := json .Marshal (typeWrapper {
133
+ Type : typeWrapperTypeJSON ,
134
+ Value : contexts ,
135
+ })
107
136
if err != nil {
108
137
log .Default ().Printf ("error marshalling contexts: %v" , err )
109
138
}
110
139
111
- s .SetAttributes (attribute .String (AttributeLangWatchRAGContextsKey , string (jsonStr )))
140
+ s .SetAttributes (AttributeLangWatchRAGContexts .String (string (jsonStr )))
112
141
}
113
142
114
- func (s * LangWatchSpan ) SetRAGContextChunk (context SpanRAGContextChunk ) {
143
+ func (s * Span ) SetRAGContextChunk (context SpanRAGContextChunk ) {
115
144
s .SetRAGContextChunks ([]SpanRAGContextChunk {context })
116
145
}
0 commit comments