Skip to content

Commit 47d6880

Browse files
feat: create embeddings tracing implementation (#1240)
**Description** This adds embeddings tracing per OpenInference OpenAI semantics, and adjusts docs accordingly. **Related Issues/PRs** Fixes #1085 --------- Signed-off-by: Adrian Cole <[email protected]>
1 parent 18bdc6d commit 47d6880

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

cmd/aigw/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@ Here are values we use for Ollama:
5151
docker compose run --rm embeddings
5252
```
5353

54-
4. **Shutdown the example stack**:
54+
4. **Create embeddings**:
55+
56+
The `create-embeddings` service uses `curl` to send an embeddings request
57+
to the AI Gateway CLI (aigw) which routes it to Ollama.
58+
```bash
59+
docker compose run --rm create-embeddings
60+
```
61+
62+
5. **Shutdown the example stack**:
5563

5664
`down` stops the containers and removes the volumes used by the stack.
5765
```bash

internal/tracing/api/api.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ func (NoopTracing) ChatCompletionTracer() ChatCompletionTracer {
5757
return NoopChatCompletionTracer{}
5858
}
5959

60+
// EmbeddingsTracer implements Tracing.EmbeddingsTracer.
61+
func (NoopTracing) EmbeddingsTracer() EmbeddingsTracer {
62+
return NoopEmbeddingsTracer{}
63+
}
64+
6065
// ImageGenerationTracer implements Tracing.ImageGenerationTracer.
6166
func (NoopTracing) ImageGenerationTracer() ImageGenerationTracer {
6267
return NoopImageGenerationTracer{}
@@ -142,6 +147,35 @@ func (NoopChatCompletionTracer) StartSpanAndInjectHeaders(context.Context, map[s
142147
return nil
143148
}
144149

150+
// EmbeddingsTracer creates spans for OpenAI embeddings requests.
151+
type EmbeddingsTracer interface {
152+
// StartSpanAndInjectHeaders starts a span and injects trace context into
153+
// the header mutation.
154+
//
155+
// Parameters:
156+
// - ctx: might include a parent span context.
157+
// - headers: Incoming HTTP headers used to extract parent trace context.
158+
// - headerMutation: The new Embeddings Span will have its context
159+
// written to these headers unless NoopTracing is used.
160+
// - req: The OpenAI embeddings request. Used to record request attributes.
161+
// - body: contains the original raw request body as a byte slice.
162+
//
163+
// Returns nil unless the span is sampled.
164+
StartSpanAndInjectHeaders(ctx context.Context, headers map[string]string, headerMutation *extprocv3.HeaderMutation, req *openai.EmbeddingRequest, body []byte) EmbeddingsSpan
165+
}
166+
167+
// EmbeddingsSpan represents an OpenAI embeddings request.
168+
type EmbeddingsSpan interface {
169+
// RecordResponse records the response attributes to the span.
170+
RecordResponse(resp *openai.EmbeddingResponse)
171+
172+
// EndSpanOnError finalizes and ends the span with an error status.
173+
EndSpanOnError(statusCode int, body []byte)
174+
175+
// EndSpan finalizes and ends the span.
176+
EndSpan()
177+
}
178+
145179
// ImageGenerationTracer creates spans for OpenAI image generation requests.
146180
type ImageGenerationTracer interface {
147181
// StartSpanAndInjectHeaders starts a span and injects trace context into

internal/tracing/tracing.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ func (t *tracingImpl) ChatCompletionTracer() tracing.ChatCompletionTracer {
3939
return t.chatCompletionTracer
4040
}
4141

42-
// ImageGenerationTracer implements the same method as documented on api.Tracing.
43-
func (t *tracingImpl) ImageGenerationTracer() tracing.ImageGenerationTracer {
44-
return t.imageGenerationTracer
45-
}
46-
4742
// EmbeddingsTracer implements the same method as documented on api.Tracing.
4843
func (t *tracingImpl) EmbeddingsTracer() tracing.EmbeddingsTracer {
4944
return t.embeddingsTracer
5045
}
5146

47+
// ImageGenerationTracer implements the same method as documented on api.Tracing.
48+
func (t *tracingImpl) ImageGenerationTracer() tracing.ImageGenerationTracer {
49+
return t.imageGenerationTracer
50+
}
51+
5252
func (t *tracingImpl) MCPTracer() tracing.MCPTracer {
5353
return t.mcpTracer
5454
}

0 commit comments

Comments
 (0)