| | | 1 | | using OpenTelemetry; |
| | | 2 | | using OpenTelemetry.Exporter; |
| | | 3 | | using OpenTelemetry.Metrics; |
| | | 4 | | using OpenTelemetry.Resources; |
| | | 5 | | using OpenTelemetry.Trace; |
| | | 6 | | |
| | | 7 | | namespace NexusLabs.Needlr.AgentFramework.Langfuse; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Entry point for exporting Needlr agent telemetry to Langfuse without requiring a generic |
| | | 11 | | /// host. Designed for evals, console apps, and test fixtures that build telemetry by hand. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// <para> |
| | | 15 | | /// <see cref="Start(LangfuseOptions)"/> constructs standalone OpenTelemetry tracer and meter |
| | | 16 | | /// providers that subscribe to Needlr's <c>gen_ai</c> activity source and meters and export them |
| | | 17 | | /// to Langfuse over OTLP/HTTP. Dispose the returned <see cref="ILangfuseSession"/> to flush and |
| | | 18 | | /// tear them down. |
| | | 19 | | /// </para> |
| | | 20 | | /// <para> |
| | | 21 | | /// For ASP.NET Core / generic-host applications that already call <c>AddOpenTelemetry()</c>, use |
| | | 22 | | /// <see cref="LangfuseServiceCollectionExtensions.AddNeedlrLangfuse(Microsoft.Extensions.DependencyInjection.IServiceCo |
| | | 23 | | /// instead so the providers participate in the host lifecycle. |
| | | 24 | | /// </para> |
| | | 25 | | /// <example> |
| | | 26 | | /// <code> |
| | | 27 | | /// using var langfuse = LangfuseTelemetry.Start(LangfuseOptions.FromEnvironment()); |
| | | 28 | | /// using (LangfuseTrace.BeginScenario("trip-planner: NYC -> Tokyo", sessionId: runId)) |
| | | 29 | | /// { |
| | | 30 | | /// var run = await runner.RunAsync(...); |
| | | 31 | | /// // ... evaluate and record scores ... |
| | | 32 | | /// } |
| | | 33 | | /// </code> |
| | | 34 | | /// </example> |
| | | 35 | | /// </remarks> |
| | | 36 | | public static class LangfuseTelemetry |
| | | 37 | | { |
| | | 38 | | /// <summary> |
| | | 39 | | /// Starts a Langfuse export session for the supplied options. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="options"> |
| | | 42 | | /// The export configuration. When <see cref="LangfuseOptions.IsConfigured"/> is |
| | | 43 | | /// <see langword="false"/> (for example, missing credentials), a disabled no-op session is |
| | | 44 | | /// returned so callers never need to branch on configuration state. |
| | | 45 | | /// </param> |
| | | 46 | | /// <returns> |
| | | 47 | | /// An <see cref="ILangfuseSession"/> that exports telemetry until disposed. |
| | | 48 | | /// </returns> |
| | | 49 | | /// <exception cref="ArgumentNullException"><paramref name="options"/> is <see langword="null"/>.</exception> |
| | | 50 | | public static ILangfuseSession Start(LangfuseOptions options) |
| | | 51 | | { |
| | 4 | 52 | | ArgumentNullException.ThrowIfNull(options); |
| | | 53 | | |
| | 4 | 54 | | if (!options.IsConfigured) |
| | | 55 | | { |
| | 4 | 56 | | LangfuseExportGuard.WarnIfCredentialsWithoutTarget(options); |
| | 4 | 57 | | return new DisabledLangfuseSession(); |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | var endpoints = LangfuseEndpoints.Resolve(options); |
| | 0 | 61 | | var resource = BuildResource(options); |
| | | 62 | | |
| | 0 | 63 | | var tracerProvider = Sdk.CreateTracerProviderBuilder() |
| | 0 | 64 | | .SetResourceBuilder(resource) |
| | 0 | 65 | | .SetSampler(BuildSampler(options.SamplingRatio)) |
| | 0 | 66 | | .AddSource(LangfuseActivitySource.Name) |
| | 0 | 67 | | .AddSource(options.AgentActivitySourceName) |
| | 0 | 68 | | .AddSource([.. options.AdditionalActivitySources]) |
| | 0 | 69 | | .AddProcessor(new LangfuseTraceAttributeProcessor(options.Environment, options.Release)) |
| | 0 | 70 | | .AddOtlpExporter(otlp => ConfigureOtlp(otlp, endpoints.TracesEndpoint, endpoints.Headers)) |
| | 0 | 71 | | .Build(); |
| | | 72 | | |
| | 0 | 73 | | MeterProvider? meterProvider = null; |
| | 0 | 74 | | if (options.IncludeMetrics) |
| | | 75 | | { |
| | 0 | 76 | | meterProvider = Sdk.CreateMeterProviderBuilder() |
| | 0 | 77 | | .SetResourceBuilder(resource) |
| | 0 | 78 | | .AddMeter(options.AgentMeterName) |
| | 0 | 79 | | .AddMeter(options.GenAiMeterName) |
| | 0 | 80 | | .AddMeter([.. options.AdditionalMeters]) |
| | 0 | 81 | | .AddOtlpExporter(otlp => ConfigureOtlp(otlp, endpoints.MetricsEndpoint, endpoints.Headers)) |
| | 0 | 82 | | .Build(); |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | var httpClient = new HttpClient(); |
| | 0 | 86 | | var scoreApiClient = new LangfuseScoreApiClient( |
| | 0 | 87 | | httpClient, |
| | 0 | 88 | | endpoints.ScoresEndpoint, |
| | 0 | 89 | | endpoints.AuthorizationHeaderValue); |
| | 0 | 90 | | var apiClient = new LangfuseApiClient( |
| | 0 | 91 | | httpClient, |
| | 0 | 92 | | endpoints.BaseUrl, |
| | 0 | 93 | | endpoints.AuthorizationHeaderValue); |
| | | 94 | | |
| | 0 | 95 | | var failureSink = new LangfuseScoreFailureSink(options.ScoreFailureMode, options.ScoreErrorCallback); |
| | 0 | 96 | | var recorder = new LangfuseScoreRecorder(scoreApiClient, failureSink, options.NormalizeScoreNames); |
| | 0 | 97 | | var commentRecorder = new LangfuseCommentRecorder(apiClient, options.DiagnosticsCallback); |
| | | 98 | | |
| | 0 | 99 | | return new LangfuseSession( |
| | 0 | 100 | | tracerProvider, |
| | 0 | 101 | | meterProvider, |
| | 0 | 102 | | httpClient, |
| | 0 | 103 | | recorder, |
| | 0 | 104 | | failureSink, |
| | 0 | 105 | | commentRecorder, |
| | 0 | 106 | | apiClient, |
| | 0 | 107 | | options.DiagnosticsCallback); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | private static ResourceBuilder BuildResource(LangfuseOptions options) |
| | | 111 | | { |
| | 0 | 112 | | var builder = ResourceBuilder.CreateDefault(); |
| | 0 | 113 | | return string.IsNullOrWhiteSpace(options.ServiceVersion) |
| | 0 | 114 | | ? builder.AddService(options.ServiceName) |
| | 0 | 115 | | : builder.AddService(options.ServiceName, serviceVersion: options.ServiceVersion); |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | private static Sampler BuildSampler(double ratio) |
| | | 119 | | { |
| | 0 | 120 | | if (ratio >= 1.0) |
| | | 121 | | { |
| | 0 | 122 | | return new AlwaysOnSampler(); |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | if (ratio <= 0.0) |
| | | 126 | | { |
| | 0 | 127 | | return new AlwaysOffSampler(); |
| | | 128 | | } |
| | | 129 | | |
| | 0 | 130 | | return new TraceIdRatioBasedSampler(ratio); |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | private static void ConfigureOtlp(OtlpExporterOptions otlp, Uri endpoint, string headers) |
| | | 134 | | { |
| | 0 | 135 | | otlp.Endpoint = endpoint; |
| | 0 | 136 | | otlp.Protocol = OtlpExportProtocol.HttpProtobuf; |
| | 0 | 137 | | otlp.Headers = headers; |
| | 0 | 138 | | } |
| | | 139 | | } |