@@ -14,78 +14,41 @@ import (
1414 tracing "github.com/envoyproxy/ai-gateway/internal/tracing/api"
1515)
1616
17- type responseRecorder [RespT any ] interface {
18- RecordResponse (trace.Span , * RespT )
19- RecordResponseOnError (trace.Span , int , []byte )
20- }
21-
22- type streamResponseRecorder [RespT , ChunkT any ] interface {
23- responseRecorder [RespT ]
24- RecordResponseChunks (trace.Span , []* ChunkT )
25- }
26-
27- type noopChunkRecorder [ChunkT any ] struct {}
28-
29- func (noopChunkRecorder [ChunkT ]) RecordResponseChunk (* ChunkT ) {}
30-
31- type streamingSpan [RespT , ChunkT any ] struct {
17+ type span [RespT , ChunkT any ] struct {
3218 span trace.Span
33- recorder streamResponseRecorder [RespT , ChunkT ]
19+ recorder tracing. SpanResponseRecorder [RespT , ChunkT ]
3420 chunks []* ChunkT
3521}
3622
37- func (s * streamingSpan [RespT , ChunkT ]) RecordResponseChunk (resp * ChunkT ) {
23+ // RecordResponseChunk implements [tracing.Span.RecordResponseChunk]
24+ func (s * span [RespT , ChunkT ]) RecordResponseChunk (resp * ChunkT ) {
3825 s .chunks = append (s .chunks , resp )
3926}
4027
41- func (s * streamingSpan [RespT , ChunkT ]) RecordResponse (resp * RespT ) {
28+ // RecordResponse implements [tracing.Span.RecordResponse]
29+ func (s * span [RespT , ChunkT ]) RecordResponse (resp * RespT ) {
4230 s .recorder .RecordResponse (s .span , resp )
4331}
4432
45- func (s * streamingSpan [RespT , ChunkT ]) EndSpan () {
33+ // EndSpan implements [tracing.Span.EndSpan]
34+ func (s * span [RespT , ChunkT ]) EndSpan () {
4635 if len (s .chunks ) > 0 {
4736 s .recorder .RecordResponseChunks (s .span , s .chunks )
4837 }
4938 s .span .End ()
5039}
5140
52- func (s * streamingSpan [RespT , ChunkT ]) EndSpanOnError (statusCode int , body []byte ) {
53- s .recorder .RecordResponseOnError (s .span , statusCode , body )
54- s .span .End ()
55- }
56-
57- type responseSpan [RespT , ChunkT any ] struct {
58- noopChunkRecorder [ChunkT ]
59- span trace.Span
60- recorder streamResponseRecorder [RespT , ChunkT ]
61- }
62-
63- func (s * responseSpan [RespT , ChunkT ]) RecordResponse (resp * RespT ) {
64- s .recorder .RecordResponse (s .span , resp )
65- }
66-
67- func (s * responseSpan [RespT , ChunkT ]) EndSpan () {
68- s .span .End ()
69- }
70-
71- func (s * responseSpan [RespT , ChunkT ]) EndSpanOnError (statusCode int , body []byte ) {
41+ // EndSpanOnError implements [tracing.Span.EndSpanOnError]
42+ func (s * span [RespT , ChunkT ]) EndSpanOnError (statusCode int , body []byte ) {
7243 s .recorder .RecordResponseOnError (s .span , statusCode , body )
7344 s .span .End ()
7445}
7546
7647// Type aliases tying generic implementations to concrete recorder contracts.
7748type (
78- chatCompletionSpan = streamingSpan [openai.ChatCompletionResponse , openai.ChatCompletionResponseChunk ]
79- completionSpan = streamingSpan [openai.CompletionResponse , openai.CompletionResponse ]
80- embeddingsSpan = responseSpan [openai.EmbeddingResponse , struct {}]
81- imageGenerationSpan = responseSpan [openaisdk.ImagesResponse , struct {}]
82- rerankSpan = responseSpan [cohereschema.RerankV2Response , struct {}]
83- )
84-
85- var (
86- _ tracing.ChatCompletionSpan = (* chatCompletionSpan )(nil )
87- _ tracing.CompletionSpan = (* completionSpan )(nil )
88- _ tracing.EmbeddingsSpan = (* embeddingsSpan )(nil )
89- _ tracing.ImageGenerationSpan = (* imageGenerationSpan )(nil )
90- _ tracing.RerankSpan = (* rerankSpan )(nil )
49+ chatCompletionSpan = span [openai.ChatCompletionResponse , openai.ChatCompletionResponseChunk ]
50+ completionSpan = span [openai.CompletionResponse , openai.CompletionResponse ]
51+ embeddingsSpan = span [openai.EmbeddingResponse , struct {}]
52+ imageGenerationSpan = span [openaisdk.ImagesResponse , struct {}]
53+ rerankSpan = span [cohereschema.RerankV2Response , struct {}]
9154)
0 commit comments