| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 3 | | |
| | | 4 | | using OpenTelemetry.Exporter; |
| | | 5 | | using OpenTelemetry.Metrics; |
| | | 6 | | using OpenTelemetry.Resources; |
| | | 7 | | using OpenTelemetry.Trace; |
| | | 8 | | |
| | | 9 | | namespace NexusLabs.Needlr.AgentFramework.Langfuse; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Registers Langfuse OTLP export on an <see cref="IServiceCollection"/> for ASP.NET Core and |
| | | 13 | | /// generic-host applications. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// Unlike <see cref="LangfuseTelemetry.Start(LangfuseOptions)"/> — which builds standalone |
| | | 17 | | /// providers for evals and console apps — this integrates with the host's |
| | | 18 | | /// <c>AddOpenTelemetry()</c> pipeline so the tracer and meter providers share the application |
| | | 19 | | /// lifecycle. When the supplied options are not configured (for example, missing credentials), |
| | | 20 | | /// registration is skipped and the application starts normally without exporting. |
| | | 21 | | /// </remarks> |
| | | 22 | | public static class LangfuseServiceCollectionExtensions |
| | | 23 | | { |
| | | 24 | | /// <summary> |
| | | 25 | | /// Adds Langfuse OTLP/HTTP export of Needlr agent telemetry to the host's OpenTelemetry |
| | | 26 | | /// pipeline. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="services">The service collection to configure.</param> |
| | | 29 | | /// <param name="configure"> |
| | | 30 | | /// Optional callback to customise the <see cref="LangfuseOptions"/>. When <see langword="null"/>, |
| | | 31 | | /// options are read from the environment via <see cref="LangfuseOptions.FromEnvironment"/>. |
| | | 32 | | /// </param> |
| | | 33 | | /// <returns>The same <see cref="IServiceCollection"/> for chaining.</returns> |
| | | 34 | | /// <exception cref="ArgumentNullException"><paramref name="services"/> is <see langword="null"/>.</exception> |
| | | 35 | | public static IServiceCollection AddNeedlrLangfuse( |
| | | 36 | | this IServiceCollection services, |
| | | 37 | | Action<LangfuseOptions>? configure = null) |
| | | 38 | | { |
| | 3 | 39 | | ArgumentNullException.ThrowIfNull(services); |
| | | 40 | | |
| | 3 | 41 | | var options = LangfuseOptions.FromEnvironment(); |
| | 3 | 42 | | configure?.Invoke(options); |
| | | 43 | | |
| | 3 | 44 | | if (!options.IsConfigured) |
| | | 45 | | { |
| | 2 | 46 | | LangfuseExportGuard.WarnIfCredentialsWithoutTarget(options); |
| | 2 | 47 | | services.TryAddSingleton<ILangfuseScoreClient>(new DisabledLangfuseScoreClient()); |
| | 2 | 48 | | services.TryAddSingleton<ILangfuseDatasetClient>(new DisabledLangfuseDatasetClient()); |
| | 2 | 49 | | services.TryAddSingleton<ILangfuseScoreConfigClient>(new DisabledLangfuseScoreConfigClient()); |
| | 2 | 50 | | return services; |
| | | 51 | | } |
| | | 52 | | |
| | 1 | 53 | | var endpoints = LangfuseEndpoints.Resolve(options); |
| | | 54 | | |
| | 1 | 55 | | var otelBuilder = services |
| | 1 | 56 | | .AddOpenTelemetry() |
| | 1 | 57 | | .ConfigureResource(resource => |
| | 1 | 58 | | { |
| | 1 | 59 | | if (string.IsNullOrWhiteSpace(options.ServiceVersion)) |
| | 1 | 60 | | { |
| | 1 | 61 | | resource.AddService(options.ServiceName); |
| | 1 | 62 | | } |
| | 1 | 63 | | else |
| | 1 | 64 | | { |
| | 0 | 65 | | resource.AddService(options.ServiceName, serviceVersion: options.ServiceVersion); |
| | 1 | 66 | | } |
| | 0 | 67 | | }) |
| | 2 | 68 | | .WithTracing(tracing => tracing |
| | 2 | 69 | | .AddSource(LangfuseActivitySource.Name) |
| | 2 | 70 | | .AddSource(options.AgentActivitySourceName) |
| | 2 | 71 | | .AddSource([.. options.AdditionalActivitySources]) |
| | 2 | 72 | | .AddProcessor(new LangfuseTraceAttributeProcessor(options.Environment, options.Release)) |
| | 3 | 73 | | .AddOtlpExporter(otlp => ConfigureOtlp(otlp, endpoints.TracesEndpoint, endpoints.Headers))); |
| | | 74 | | |
| | 1 | 75 | | if (options.IncludeMetrics) |
| | | 76 | | { |
| | 0 | 77 | | otelBuilder.WithMetrics(metrics => metrics |
| | 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))); |
| | | 82 | | } |
| | | 83 | | |
| | 1 | 84 | | var httpClient = new HttpClient(); |
| | 1 | 85 | | var scoreApiClient = new LangfuseScoreApiClient( |
| | 1 | 86 | | httpClient, |
| | 1 | 87 | | endpoints.ScoresEndpoint, |
| | 1 | 88 | | endpoints.AuthorizationHeaderValue); |
| | 1 | 89 | | var apiClient = new LangfuseApiClient( |
| | 1 | 90 | | httpClient, |
| | 1 | 91 | | endpoints.BaseUrl, |
| | 1 | 92 | | endpoints.AuthorizationHeaderValue); |
| | 1 | 93 | | var failureSink = new LangfuseScoreFailureSink(options.ScoreFailureMode, options.ScoreErrorCallback); |
| | 1 | 94 | | var recorder = new LangfuseScoreRecorder(scoreApiClient, failureSink, options.NormalizeScoreNames); |
| | | 95 | | |
| | 1 | 96 | | services.TryAddSingleton<ILangfuseScoreClient>(new LangfuseScoreClient(recorder, failureSink)); |
| | 1 | 97 | | services.TryAddSingleton<ILangfuseDatasetClient>(new LangfuseDatasetClient(apiClient)); |
| | 1 | 98 | | services.TryAddSingleton<ILangfuseScoreConfigClient>(new LangfuseScoreConfigClient(apiClient)); |
| | | 99 | | |
| | 1 | 100 | | return services; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | private static void ConfigureOtlp(OtlpExporterOptions otlp, Uri endpoint, string headers) |
| | | 104 | | { |
| | 1 | 105 | | otlp.Endpoint = endpoint; |
| | 1 | 106 | | otlp.Protocol = OtlpExportProtocol.HttpProtobuf; |
| | 1 | 107 | | otlp.Headers = headers; |
| | 1 | 108 | | } |
| | | 109 | | } |