| | | 1 | | using System.Threading; |
| | | 2 | | |
| | | 3 | | using OpenTelemetry; |
| | | 4 | | using OpenTelemetry.Metrics; |
| | | 5 | | using OpenTelemetry.Trace; |
| | | 6 | | |
| | | 7 | | namespace NexusLabs.Needlr.AgentFramework.Langfuse; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Active <see cref="ILangfuseSession"/> backed by OpenTelemetry tracer and (optionally) meter |
| | | 11 | | /// providers that export to Langfuse over OTLP/HTTP. |
| | | 12 | | /// </summary> |
| | | 13 | | internal sealed class LangfuseSession : ILangfuseSession |
| | | 14 | | { |
| | | 15 | | private readonly TracerProvider _tracerProvider; |
| | | 16 | | private readonly MeterProvider? _meterProvider; |
| | | 17 | | private readonly HttpClient _httpClient; |
| | | 18 | | private readonly LangfuseScoreRecorder _recorder; |
| | | 19 | | private readonly LangfuseScoreFailureSink _failureSink; |
| | | 20 | | private readonly LangfuseCommentRecorder _commentRecorder; |
| | | 21 | | private readonly LangfuseApiClient _apiClient; |
| | | 22 | | private readonly Action<string>? _diagnostics; |
| | | 23 | | private int _disposed; |
| | | 24 | | |
| | 0 | 25 | | public LangfuseSession( |
| | 0 | 26 | | TracerProvider tracerProvider, |
| | 0 | 27 | | MeterProvider? meterProvider, |
| | 0 | 28 | | HttpClient httpClient, |
| | 0 | 29 | | LangfuseScoreRecorder recorder, |
| | 0 | 30 | | LangfuseScoreFailureSink failureSink, |
| | 0 | 31 | | LangfuseCommentRecorder commentRecorder, |
| | 0 | 32 | | LangfuseApiClient apiClient, |
| | 0 | 33 | | Action<string>? diagnostics) |
| | | 34 | | { |
| | 0 | 35 | | ArgumentNullException.ThrowIfNull(tracerProvider); |
| | 0 | 36 | | ArgumentNullException.ThrowIfNull(httpClient); |
| | 0 | 37 | | ArgumentNullException.ThrowIfNull(recorder); |
| | 0 | 38 | | ArgumentNullException.ThrowIfNull(failureSink); |
| | 0 | 39 | | ArgumentNullException.ThrowIfNull(commentRecorder); |
| | 0 | 40 | | ArgumentNullException.ThrowIfNull(apiClient); |
| | | 41 | | |
| | 0 | 42 | | _tracerProvider = tracerProvider; |
| | 0 | 43 | | _meterProvider = meterProvider; |
| | 0 | 44 | | _httpClient = httpClient; |
| | 0 | 45 | | _recorder = recorder; |
| | 0 | 46 | | _failureSink = failureSink; |
| | 0 | 47 | | _commentRecorder = commentRecorder; |
| | 0 | 48 | | _apiClient = apiClient; |
| | 0 | 49 | | _diagnostics = diagnostics; |
| | | 50 | | |
| | 0 | 51 | | Datasets = new LangfuseDatasetClient(apiClient); |
| | 0 | 52 | | ScoreConfigs = new LangfuseScoreConfigClient(apiClient); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | 0 | 56 | | public bool IsEnabled => true; |
| | | 57 | | |
| | | 58 | | /// <inheritdoc /> |
| | 0 | 59 | | public int ScoresFailed => _failureSink.FailedCount; |
| | | 60 | | |
| | | 61 | | /// <inheritdoc /> |
| | 0 | 62 | | public ILangfuseDatasetClient Datasets { get; } |
| | | 63 | | |
| | | 64 | | /// <inheritdoc /> |
| | 0 | 65 | | public ILangfuseScoreConfigClient ScoreConfigs { get; } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc /> |
| | | 68 | | public bool Flush(TimeSpan? timeout = null) |
| | | 69 | | { |
| | 0 | 70 | | var timeoutMs = ToTimeoutMilliseconds(timeout); |
| | 0 | 71 | | var traces = _tracerProvider.ForceFlush(timeoutMs); |
| | 0 | 72 | | var metrics = _meterProvider?.ForceFlush(timeoutMs) ?? true; |
| | 0 | 73 | | return traces && metrics; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc /> |
| | | 77 | | public ILangfuseScenario BeginScenario( |
| | | 78 | | string name, |
| | | 79 | | string? sessionId = null, |
| | | 80 | | string? userId = null, |
| | | 81 | | IEnumerable<string>? tags = null, |
| | | 82 | | IReadOnlyDictionary<string, string>? metadata = null) => |
| | 0 | 83 | | new LangfuseScenario(_recorder, name, sessionId, userId, tags, metadata); |
| | | 84 | | |
| | | 85 | | /// <inheritdoc /> |
| | | 86 | | public ILangfuseExperimentRun BeginExperimentRun(string datasetName, string runName, string? runDescription = null) |
| | | 87 | | { |
| | 0 | 88 | | ArgumentException.ThrowIfNullOrWhiteSpace(datasetName); |
| | 0 | 89 | | ArgumentException.ThrowIfNullOrWhiteSpace(runName); |
| | | 90 | | |
| | 0 | 91 | | return new LangfuseExperimentRun( |
| | 0 | 92 | | _apiClient, |
| | 0 | 93 | | _recorder, |
| | 0 | 94 | | datasetName, |
| | 0 | 95 | | runName, |
| | 0 | 96 | | runDescription, |
| | 0 | 97 | | _diagnostics); |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <inheritdoc /> |
| | | 101 | | public Task AddTraceCommentAsync(string traceId, string content, CancellationToken cancellationToken = default) => |
| | 0 | 102 | | _commentRecorder.AddTraceCommentAsync(traceId, content, cancellationToken); |
| | | 103 | | |
| | | 104 | | /// <inheritdoc /> |
| | | 105 | | public void Dispose() |
| | | 106 | | { |
| | 0 | 107 | | if (Interlocked.Exchange(ref _disposed, 1) != 0) |
| | | 108 | | { |
| | 0 | 109 | | return; |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | Flush(); |
| | 0 | 113 | | _tracerProvider.Dispose(); |
| | 0 | 114 | | _meterProvider?.Dispose(); |
| | 0 | 115 | | _httpClient.Dispose(); |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | private static int ToTimeoutMilliseconds(TimeSpan? timeout) |
| | | 119 | | { |
| | 0 | 120 | | if (timeout is null) |
| | | 121 | | { |
| | 0 | 122 | | return Timeout.Infinite; |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | if (timeout.Value == Timeout.InfiniteTimeSpan) |
| | | 126 | | { |
| | 0 | 127 | | return Timeout.Infinite; |
| | | 128 | | } |
| | | 129 | | |
| | 0 | 130 | | var ms = (long)timeout.Value.TotalMilliseconds; |
| | 0 | 131 | | return ms < 0 ? Timeout.Infinite : (int)Math.Min(ms, int.MaxValue); |
| | | 132 | | } |
| | | 133 | | } |