Skip to content

Commit 1cdb564

Browse files
committed
updates to langwatch sdk
1 parent 47f5396 commit 1cdb564

File tree

3 files changed

+79
-48
lines changed

3 files changed

+79
-48
lines changed

libraries/langwatch/attributes.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package langwatch
22

3+
import "go.opentelemetry.io/otel/attribute"
4+
35
const (
4-
AttributeLangWatchInputKey = "langwatch.input"
5-
AttributeLangWatchOutputKey = "langwatch.output"
6-
AttributeLangWatchSpanTypeKey = "langwatch.span.type"
7-
AttributeLangWatchRAGContextsKey = "langwatch.rag_contexts"
8-
AttributeLangWatchSDKVersionKey = "langwatch.sdk.version"
9-
AttributeLangWatchSDKNameKey = "langwatch.sdk.name"
10-
AttributeLangWatchSDKLanguageKey = "langwatch.sdk.language"
11-
AttributeLangWatchTimestampsKey = "langwatch.timestamps"
12-
AttributeLangWatchParamsKey = "langwatch.params"
13-
AttributeLangWatchMetricsKey = "langwatch.metrics"
14-
AttributeLangWatchCustomerIdKey = "langwatch.customer.id"
15-
AttributeLangWatchThreadIdKey = "langwatch.thread.id"
6+
AttributeLangWatchInput = attribute.Key("langwatch.input")
7+
AttributeLangWatchOutput = attribute.Key("langwatch.output")
8+
AttributeLangWatchSpanType = attribute.Key("langwatch.span.type")
9+
AttributeLangWatchRAGContexts = attribute.Key("langwatch.contexts")
10+
AttributeLangWatchSDKVersion = attribute.Key("langwatch.sdk.version")
11+
AttributeLangWatchSDKName = attribute.Key("langwatch.sdk.name")
12+
AttributeLangWatchSDKLanguage = attribute.Key("langwatch.sdk.language")
13+
AttributeLangWatchTimestamps = attribute.Key("langwatch.timestamps")
14+
AttributeLangWatchParams = attribute.Key("langwatch.params")
15+
AttributeLangWatchCustomerId = attribute.Key("langwatch.customer.id")
16+
AttributeLangWatchThreadId = attribute.Key("langwatch.thread.id")
17+
AttributeLangWatchStreaming = attribute.Key("langwatch.gen_ai.streaming")
1618
)

libraries/langwatch/span.go

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"encoding/json"
55
"log"
66

7-
"go.opentelemetry.io/otel/attribute"
87
"go.opentelemetry.io/otel/trace"
98

109
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
1110
)
1211

1312
type SpanType string
13+
type typeWrapperType string
1414

1515
const (
1616
SpanTypeSpan SpanType = "span"
@@ -30,13 +30,10 @@ const (
3030
SpanTypeConsumer SpanType = "consumer"
3131
SpanTypeTask SpanType = "task"
3232
SpanTypeUnknown SpanType = "unknown"
33-
)
3433

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+
)
4037

4138
type SpanTimestamps struct {
4239
StartedAtUnix int64 `json:"started_at"`
@@ -50,67 +47,99 @@ type SpanRAGContextChunk struct {
5047
Content any `json:"content"`
5148
}
5249

53-
type LangWatchSpan struct {
50+
type typeWrapper struct {
51+
Type typeWrapperType `json:"type"`
52+
Value any `json:"value"`
53+
}
54+
55+
type Span struct {
5456
trace.Span
5557
}
5658

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+
})
5964
if err != nil {
6065
log.Default().Printf("error marshalling input: %v", err)
6166
}
6267

63-
s.SetAttributes(attribute.String(AttributeLangWatchInputKey, string(jsonStr)))
68+
s.SetAttributes(AttributeLangWatchInput.String(string(jsonStr)))
6469
}
6570

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+
})
6876
if err != nil {
69-
log.Default().Printf("error marshalling output: %v", err)
77+
log.Default().Printf("error marshalling input: %v", err)
7078
}
7179

72-
s.SetAttributes(attribute.String(AttributeLangWatchOutputKey, string(jsonStr)))
80+
s.SetAttributes(AttributeLangWatchInput.String(string(jsonStr)))
7381
}
7482

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)))
7793
}
7894

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)))
81105
}
82106

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)))
85109
}
86110

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+
}
92114

93-
s.SetAttributes(attribute.String(AttributeLangWatchMetricsKey, string(jsonStr)))
115+
func (s *Span) SetResponseModel(model string) {
116+
s.SetAttributes(semconv.GenAiResponseModelKey.String(model))
94117
}
95118

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+
})
98124
if err != nil {
99125
log.Default().Printf("error marshalling timestamps: %v", err)
100126
}
101127

102-
s.SetAttributes(attribute.String(AttributeLangWatchTimestampsKey, string(jsonStr)))
128+
s.SetAttributes(AttributeLangWatchTimestamps.String(string(jsonStr)))
103129
}
104130

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+
})
107136
if err != nil {
108137
log.Default().Printf("error marshalling contexts: %v", err)
109138
}
110139

111-
s.SetAttributes(attribute.String(AttributeLangWatchRAGContextsKey, string(jsonStr)))
140+
s.SetAttributes(AttributeLangWatchRAGContexts.String(string(jsonStr)))
112141
}
113142

114-
func (s *LangWatchSpan) SetRAGContextChunk(context SpanRAGContextChunk) {
143+
func (s *Span) SetRAGContextChunk(context SpanRAGContextChunk) {
115144
s.SetRAGContextChunks([]SpanRAGContextChunk{context})
116145
}

libraries/langwatch/tracer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func Tracer(name string, options ...trace.TracerOption) *LangWatchTracer {
1717
}
1818
}
1919

20-
func (t *LangWatchTracer) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, *LangWatchSpan) {
20+
func (t *LangWatchTracer) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, *Span) {
2121
ctx, span := t.tracer.Start(ctx, name, opts...)
22-
return ctx, &LangWatchSpan{span}
22+
return ctx, &Span{span}
2323
}

0 commit comments

Comments
 (0)