| | | 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 | | { |
| | 0 | 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 SetExperimentName(Activity? activity, string? experimentName) |
| | | 36 | | { |
| | 1 | 37 | | SetTagAndBaggage(activity, "langfuse.experiment.name", experimentName); |
| | 1 | 38 | | } |
| | | 39 | | |
| | | 40 | | public static void SetExperimentDescription(Activity? activity, string? description) |
| | | 41 | | { |
| | 1 | 42 | | SetTagAndBaggage(activity, "langfuse.experiment.description", description); |
| | 1 | 43 | | } |
| | | 44 | | |
| | | 45 | | public static void SetExperimentItemId(Activity? activity, string? itemId) |
| | | 46 | | { |
| | 1 | 47 | | SetTagAndBaggage(activity, "langfuse.experiment.item.id", itemId); |
| | 1 | 48 | | } |
| | | 49 | | |
| | | 50 | | public static void SetExperimentItemRootObservationId(Activity? activity, string? rootObservationId) |
| | | 51 | | { |
| | 1 | 52 | | SetTagAndBaggage(activity, "langfuse.experiment.item.root_observation_id", rootObservationId); |
| | 1 | 53 | | } |
| | | 54 | | |
| | | 55 | | public static void SetExperimentRunId(Activity? activity, string? datasetRunId) |
| | | 56 | | { |
| | 1 | 57 | | SetTagAndBaggage(activity, "langfuse.experiment.id", datasetRunId); |
| | 1 | 58 | | } |
| | | 59 | | |
| | | 60 | | public static void SetTraceMetadata(Activity? activity, string metadataKey, string? value, bool propagateToObservati |
| | | 61 | | { |
| | 1 | 62 | | if (activity is null || string.IsNullOrWhiteSpace(metadataKey) || string.IsNullOrWhiteSpace(value)) |
| | | 63 | | { |
| | 1 | 64 | | return; |
| | | 65 | | } |
| | | 66 | | |
| | 1 | 67 | | activity.SetTag($"langfuse.trace.metadata.{metadataKey}", value); |
| | | 68 | | |
| | 1 | 69 | | if (propagateToObservations) |
| | | 70 | | { |
| | 1 | 71 | | activity.AddBaggage($"langfuse.observation.metadata.{metadataKey}", value); |
| | 1 | 72 | | RegisterTraceMetadata(activity, metadataKey, value); |
| | | 73 | | } |
| | 1 | 74 | | } |
| | | 75 | | |
| | | 76 | | public static IEnumerable<KeyValuePair<string, string>> GetObservationMetadata(Activity activity) |
| | | 77 | | { |
| | 1 | 78 | | if (activity.TraceId == default) |
| | | 79 | | { |
| | 1 | 80 | | return []; |
| | | 81 | | } |
| | | 82 | | |
| | 1 | 83 | | return TraceMetadataByTraceId.TryGetValue(activity.TraceId.ToString(), out var metadata) |
| | 1 | 84 | | ? metadata.Select(pair => new KeyValuePair<string, string>($"langfuse.observation.metadata.{pair.Key}", pair |
| | 1 | 85 | | : []; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | public static void ClearTraceMetadata(Activity activity) |
| | | 89 | | { |
| | 1 | 90 | | if (activity.TraceId == default) |
| | | 91 | | { |
| | 0 | 92 | | return; |
| | | 93 | | } |
| | | 94 | | |
| | 1 | 95 | | TraceMetadataByTraceId.TryRemove(activity.TraceId.ToString(), out _); |
| | 1 | 96 | | } |
| | | 97 | | |
| | | 98 | | private static void RegisterTraceMetadata(Activity activity, string metadataKey, string value) |
| | | 99 | | { |
| | 1 | 100 | | if (activity.TraceId == default) |
| | | 101 | | { |
| | 0 | 102 | | return; |
| | | 103 | | } |
| | | 104 | | |
| | 1 | 105 | | var traceMetadata = TraceMetadataByTraceId.GetOrAdd( |
| | 1 | 106 | | activity.TraceId.ToString(), |
| | 1 | 107 | | _ => new ConcurrentDictionary<string, string>(StringComparer.Ordinal)); |
| | | 108 | | |
| | 1 | 109 | | traceMetadata[metadataKey] = value; |
| | 1 | 110 | | } |
| | | 111 | | |
| | | 112 | | private static void SetTagAndBaggage(Activity? activity, string attributeName, string? value) |
| | | 113 | | { |
| | 1 | 114 | | if (activity is null || string.IsNullOrWhiteSpace(value)) |
| | | 115 | | { |
| | 1 | 116 | | return; |
| | | 117 | | } |
| | | 118 | | |
| | 1 | 119 | | activity.SetTag(attributeName, value); |
| | 1 | 120 | | activity.AddBaggage(attributeName, value); |
| | 1 | 121 | | } |
| | | 122 | | } |