Skip to content

Commit 807b162

Browse files
committed
fix
1 parent a779cf8 commit 807b162

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

src/api/Elastic.Documentation.Api.Core/AskAi/AskAiUsecase.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@ public class AskAiUsecase(
1616

1717
public async Task<Stream> AskAi(AskAiRequest askAiRequest, Cancel ctx)
1818
{
19-
using var activity = AskAiActivitySource.StartActivity("gen_ai.agent");
19+
// Start with a placeholder model name - will be updated by transformer
20+
using var activity = AskAiActivitySource.StartActivity("chat unknown", ActivityKind.Client);
2021

21-
// We'll determine the actual agent name after we know which provider is being used
22-
_ = (activity?.SetTag("gen_ai.request.input", askAiRequest.Message));
22+
// Set required GenAI semantic convention attributes
23+
_ = (activity?.SetTag("gen_ai.operation.name", "chat"));
24+
_ = (activity?.SetTag("gen_ai.request.model", "unknown")); // Will be updated by transformer
2325
_ = (activity?.SetTag("gen_ai.conversation.id", askAiRequest.ThreadId ?? "pending")); // Will be updated when we receive ConversationStart
26+
_ = (activity?.SetTag("gen_ai.usage.input_tokens", askAiRequest.Message.Length)); // Approximate token count
2427

25-
// Add GenAI inference operation details event
26-
_ = (activity?.AddEvent(new ActivityEvent("gen_ai.client.inference.operation.details",
28+
// Add GenAI prompt event
29+
_ = (activity?.AddEvent(new ActivityEvent("gen_ai.content.prompt",
2730
timestamp: DateTimeOffset.UtcNow,
2831
tags:
2932
[
30-
new KeyValuePair<string, object?>("gen_ai.operation.name", "chat"),
31-
new KeyValuePair<string, object?>("gen_ai.input.messages", $"[{{\"role\":\"user\",\"content\":\"{askAiRequest.Message}\"}}]"),
32-
new KeyValuePair<string, object?>("gen_ai.system_instructions", $"[{{\"type\":\"text\",\"content\":\"{AskAiRequest.SystemPrompt}\"}}]")
33+
new KeyValuePair<string, object?>("gen_ai.prompt", askAiRequest.Message),
34+
new KeyValuePair<string, object?>("gen_ai.system_instructions", AskAiRequest.SystemPrompt)
3335
])));
3436

3537
logger.LogDebug("Processing AskAiRequest: {Request}", askAiRequest);

src/api/Elastic.Documentation.Api.Infrastructure/Adapters/AskAi/StreamTransformerBase.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public abstract class StreamTransformerBase(ILogger logger) : IStreamTransformer
4242

4343
public Task<Stream> TransformAsync(Stream rawStream, CancellationToken cancellationToken = default)
4444
{
45-
using var activity = StreamTransformerActivitySource.StartActivity("gen_ai.agent");
45+
using var activity = StreamTransformerActivitySource.StartActivity($"chat {GetAgentId()}", ActivityKind.Client);
46+
_ = (activity?.SetTag("gen_ai.operation.name", "chat"));
47+
_ = (activity?.SetTag("gen_ai.request.model", GetAgentId()));
4648
_ = (activity?.SetTag("gen_ai.agent.name", GetAgentId()));
4749
_ = (activity?.SetTag("gen_ai.provider.name", GetAgentProvider()));
4850

@@ -74,7 +76,7 @@ public Task<Stream> TransformAsync(Stream rawStream, CancellationToken cancellat
7476
/// </summary>
7577
private async Task ProcessPipeAsync(PipeReader reader, PipeWriter writer, Activity? parentActivity, CancellationToken cancellationToken)
7678
{
77-
using var activity = StreamTransformerActivitySource.StartActivity("StreamTransformer.ProcessPipeAsync");
79+
using var activity = StreamTransformerActivitySource.StartActivity("gen_ai.agent.pipe");
7880
_ = (activity?.SetTag("transformer.type", GetType().Name));
7981

8082
try
@@ -204,14 +206,12 @@ protected async Task WriteEventAsync(AskAiEvent? transformedEvent, PipeWriter wr
204206
_ = (activity?.SetTag("gen_ai.provider.name", GetAgentProvider()));
205207
_ = (activity?.SetTag("gen_ai.response.token_type", transformedEvent.GetType().Name));
206208

207-
// Add GenAI response event for each token/chunk
208-
_ = (activity?.AddEvent(new ActivityEvent("gen_ai.client.inference.operation.details",
209+
// Add GenAI completion event for each token/chunk
210+
_ = (activity?.AddEvent(new ActivityEvent("gen_ai.content.completion",
209211
timestamp: DateTimeOffset.UtcNow,
210212
tags:
211213
[
212-
new KeyValuePair<string, object?>("gen_ai.operation.name", "chat"),
213-
new KeyValuePair<string, object?>("gen_ai.response.model", GetAgentId()),
214-
new KeyValuePair<string, object?>("gen_ai.output.messages", JsonSerializer.Serialize(transformedEvent, AskAiEventJsonContext.Default.AskAiEvent))
214+
new KeyValuePair<string, object?>("gen_ai.completion", JsonSerializer.Serialize(transformedEvent, AskAiEventJsonContext.Default.AskAiEvent))
215215
])));
216216

217217
// Serialize as base AskAiEvent type to include the type discriminator

src/api/Elastic.Documentation.Api.Lambda/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
{
3434
_ = tracing
3535
.AddAspNetCoreInstrumentation()
36-
.AddHttpClientInstrumentation();
36+
.AddHttpClientInstrumentation()
37+
.AddSource("Elastic.Documentation.Api.AskAi")
38+
.AddSource("Elastic.Documentation.Api.StreamTransformer");
3739
})
3840
.WithElasticLogging()
3941
.WithElasticMetrics(metrics =>

0 commit comments

Comments
 (0)