| | | 1 | | using System.Diagnostics; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | |
| | | 5 | | namespace OpenAiIntegration; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Utilities for propagating shared Langfuse trace context from a root activity to child observations. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class LangfuseActivityPropagation |
| | | 11 | | { |
| | 1 | 12 | | private static readonly ConcurrentDictionary<string, ConcurrentDictionary<string, string>> TraceMetadataByTraceId = |
| | | 13 | | |
| | | 14 | | public static void SetEnvironment(Activity? activity, string? environment) |
| | | 15 | | { |
| | 1 | 16 | | SetTagAndBaggage(activity, "langfuse.environment", environment); |
| | 1 | 17 | | } |
| | | 18 | | |
| | | 19 | | public static void SetSessionId(Activity? activity, string? sessionId) |
| | | 20 | | { |
| | 1 | 21 | | SetTagAndBaggage(activity, "langfuse.session.id", sessionId); |
| | 1 | 22 | | } |
| | | 23 | | |
| | | 24 | | public static void SetTraceTags(Activity? activity, IReadOnlyCollection<string> tags) |
| | | 25 | | { |
| | 1 | 26 | | if (activity is null || tags.Count == 0) |
| | | 27 | | { |
| | 1 | 28 | | return; |
| | | 29 | | } |
| | | 30 | | |
| | 1 | 31 | | var serializedTags = JsonSerializer.Serialize(tags); |
| | 1 | 32 | | SetTagAndBaggage(activity, "langfuse.trace.tags", serializedTags); |
| | 1 | 33 | | } |
| | | 34 | | |
| | | 35 | | public static void SetTraceMetadata(Activity? activity, string metadataKey, string? value, bool propagateToObservati |
| | | 36 | | { |
| | 1 | 37 | | if (activity is null || string.IsNullOrWhiteSpace(metadataKey) || string.IsNullOrWhiteSpace(value)) |
| | | 38 | | { |
| | 1 | 39 | | return; |
| | | 40 | | } |
| | | 41 | | |
| | 1 | 42 | | activity.SetTag($"langfuse.trace.metadata.{metadataKey}", value); |
| | | 43 | | |
| | 1 | 44 | | if (propagateToObservations) |
| | | 45 | | { |
| | 1 | 46 | | activity.AddBaggage($"langfuse.observation.metadata.{metadataKey}", value); |
| | 1 | 47 | | RegisterTraceMetadata(activity, metadataKey, value); |
| | | 48 | | } |
| | 1 | 49 | | } |
| | | 50 | | |
| | | 51 | | public static IEnumerable<KeyValuePair<string, string>> GetObservationMetadata(Activity activity) |
| | | 52 | | { |
| | 1 | 53 | | if (activity.TraceId == default) |
| | | 54 | | { |
| | 0 | 55 | | return []; |
| | | 56 | | } |
| | | 57 | | |
| | 1 | 58 | | return TraceMetadataByTraceId.TryGetValue(activity.TraceId.ToString(), out var metadata) |
| | 1 | 59 | | ? metadata.Select(pair => new KeyValuePair<string, string>($"langfuse.observation.metadata.{pair.Key}", pair |
| | 1 | 60 | | : []; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public static void ClearTraceMetadata(Activity activity) |
| | | 64 | | { |
| | 1 | 65 | | if (activity.TraceId == default) |
| | | 66 | | { |
| | 0 | 67 | | return; |
| | | 68 | | } |
| | | 69 | | |
| | 1 | 70 | | TraceMetadataByTraceId.TryRemove(activity.TraceId.ToString(), out _); |
| | 1 | 71 | | } |
| | | 72 | | |
| | | 73 | | private static void RegisterTraceMetadata(Activity activity, string metadataKey, string value) |
| | | 74 | | { |
| | 1 | 75 | | if (activity.TraceId == default) |
| | | 76 | | { |
| | 0 | 77 | | return; |
| | | 78 | | } |
| | | 79 | | |
| | 1 | 80 | | var traceMetadata = TraceMetadataByTraceId.GetOrAdd( |
| | 1 | 81 | | activity.TraceId.ToString(), |
| | 1 | 82 | | _ => new ConcurrentDictionary<string, string>(StringComparer.Ordinal)); |
| | | 83 | | |
| | 1 | 84 | | traceMetadata[metadataKey] = value; |
| | 1 | 85 | | } |
| | | 86 | | |
| | | 87 | | private static void SetTagAndBaggage(Activity? activity, string attributeName, string? value) |
| | | 88 | | { |
| | 1 | 89 | | if (activity is null || string.IsNullOrWhiteSpace(value)) |
| | | 90 | | { |
| | 1 | 91 | | return; |
| | | 92 | | } |
| | | 93 | | |
| | 1 | 94 | | activity.SetTag(attributeName, value); |
| | 1 | 95 | | activity.AddBaggage(attributeName, value); |
| | 1 | 96 | | } |
| | | 97 | | } |