From 8b1100df8f4aa87e0d953a74a1b20efca5af139d Mon Sep 17 00:00:00 2001 From: Ciprian Jichici Date: Wed, 11 Dec 2024 16:35:15 +0200 Subject: [PATCH 1/5] Update projects for OpenTelemetry dependencies --- src/dotnet/Agent/Agent.csproj | 1 - src/dotnet/AzureOpenAI/AzureOpenAI.csproj | 5 +- src/dotnet/Common/Common.csproj | 3 +- .../Constants/Data/TelemetryActivities.json | 5 ++ .../Common/Services/DependencyInjection.cs | 61 ++++++++++++------- .../Telemetry/TelemetryActivitySources.cs | 16 +++++ src/dotnet/Configuration/Configuration.csproj | 1 - src/dotnet/Core/Core.csproj | 7 ++- .../Controllers/CompletionsController.cs | 4 ++ src/dotnet/CoreAPI/CoreAPI.csproj | 5 +- src/dotnet/CoreWorker/CoreWorker.csproj | 6 +- src/dotnet/Gatekeeper/Gatekeeper.csproj | 4 -- src/dotnet/Gateway/Gateway.csproj | 3 +- src/dotnet/ManagementAPI/ManagementAPI.csproj | 3 +- src/dotnet/Orchestration/Orchestration.csproj | 2 - .../Orchestration/ExplodedObjectsManager.cs | 4 +- .../Orchestration/OrchestrationBuilder.cs | 2 - src/dotnet/Vectorization/Vectorization.csproj | 17 ------ .../VectorizationEngine.csproj | 1 - tests/dotnet/Common.Tests/Common.Tests.csproj | 4 +- .../Core.Client.Tests.csproj | 4 +- .../Core.Examples.LoadTests.csproj | 7 +-- .../dotnet/Core.Examples/Core.Examples.csproj | 4 +- tests/dotnet/Core.Tests/Core.Tests.csproj | 4 +- .../Gatekeeper.Tests/Gatekeeper.Tests.csproj | 4 +- .../Management.Client.Tests.csproj | 4 +- .../Orchestration.Tests.csproj | 4 +- .../SemanticKernel.Tests.csproj | 4 +- .../Vectorization.Tests.csproj | 4 +- 29 files changed, 108 insertions(+), 85 deletions(-) create mode 100644 src/dotnet/Common/Constants/Data/TelemetryActivities.json create mode 100644 src/dotnet/Common/Telemetry/TelemetryActivitySources.cs diff --git a/src/dotnet/Agent/Agent.csproj b/src/dotnet/Agent/Agent.csproj index d407bb578a..155df3b44a 100644 --- a/src/dotnet/Agent/Agent.csproj +++ b/src/dotnet/Agent/Agent.csproj @@ -11,7 +11,6 @@ - diff --git a/src/dotnet/AzureOpenAI/AzureOpenAI.csproj b/src/dotnet/AzureOpenAI/AzureOpenAI.csproj index 1ea5862d9e..ec64651bbe 100644 --- a/src/dotnet/AzureOpenAI/AzureOpenAI.csproj +++ b/src/dotnet/AzureOpenAI/AzureOpenAI.csproj @@ -9,7 +9,10 @@ - + + + + diff --git a/src/dotnet/Common/Common.csproj b/src/dotnet/Common/Common.csproj index 22199a93de..f0920927ca 100644 --- a/src/dotnet/Common/Common.csproj +++ b/src/dotnet/Common/Common.csproj @@ -62,12 +62,13 @@ - + + diff --git a/src/dotnet/Common/Constants/Data/TelemetryActivities.json b/src/dotnet/Common/Constants/Data/TelemetryActivities.json new file mode 100644 index 0000000000..e38961b107 --- /dev/null +++ b/src/dotnet/Common/Constants/Data/TelemetryActivities.json @@ -0,0 +1,5 @@ +[ + { + + } +] diff --git a/src/dotnet/Common/Services/DependencyInjection.cs b/src/dotnet/Common/Services/DependencyInjection.cs index 68ed1ff89a..592dcdb751 100644 --- a/src/dotnet/Common/Services/DependencyInjection.cs +++ b/src/dotnet/Common/Services/DependencyInjection.cs @@ -1,6 +1,8 @@ using Azure.Monitor.OpenTelemetry.AspNetCore; +using Azure.Monitor.OpenTelemetry.Exporter; using FoundationaLLM.Common.Authentication; using FoundationaLLM.Common.Constants; +using FoundationaLLM.Common.Constants.Authorization; using FoundationaLLM.Common.Constants.Configuration; using FoundationaLLM.Common.Interfaces; using FoundationaLLM.Common.Models.Configuration.CosmosDB; @@ -9,10 +11,10 @@ using FoundationaLLM.Common.Services.Azure; using FoundationaLLM.Common.Services.Security; using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.Azure.Cosmos.Fluent; using Microsoft.Azure.Cosmos; -using Microsoft.AspNetCore.Identity; +using Microsoft.Azure.Cosmos.Fluent; using Microsoft.Extensions.Azure; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -20,10 +22,7 @@ using Microsoft.Identity.Web; using OpenTelemetry.Resources; using OpenTelemetry.Trace; -using Microsoft.Extensions.Configuration; -using Microsoft.AspNetCore.Authentication; -using FoundationaLLM.Common.Constants.Authorization; -using Microsoft.Graph.Models; +using System.Diagnostics; namespace FoundationaLLM { @@ -60,23 +59,41 @@ public static void AddOpenTelemetry(this IHostApplicationBuilder builder, string connectionStringConfigurationKey, string serviceName) { - // Add the OpenTelemetry telemetry service and send telemetry data to Azure Monitor. - builder.Services.AddOpenTelemetry().UseAzureMonitor(options => - { - options.ConnectionString = builder.Configuration[connectionStringConfigurationKey]; - }); - - // Create a dictionary of resource attributes. - var resourceAttributes = new Dictionary { - { "service.name", serviceName }, - { "service.namespace", "FoundationaLLM" }, - { "service.instance.id", ValidatedEnvironment.MachineName } - }; + var resourceBuilder = ResourceBuilder + .CreateDefault() + .AddAttributes(new Dictionary + { + { "service.name", serviceName }, + { "service.namespace", "FoundationaLLM" }, + { "service.version", builder.Configuration[EnvironmentVariables.FoundationaLLM_Version]! }, + { "service.instance.id", ValidatedEnvironment.MachineName } + }); - // Configure the OpenTelemetry tracer provider to add the resource attributes to all traces. - builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => - builder.ConfigureResource(resourceBuilder => - resourceBuilder.AddAttributes(resourceAttributes))); + // Add the OpenTelemetry telemetry service and send telemetry data to Azure Monitor. + builder.Services.AddOpenTelemetry() + .WithTracing(traceProviderBuilder => traceProviderBuilder + .SetResourceBuilder(resourceBuilder) + .AddSource("Azure.*") + .AddSource(serviceName) + .AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation(httpOptions => httpOptions.FilterHttpRequestMessage = (_) => + { + // Azure SDKs create their own client span before calling the service using HttpClient + // In this case, we would see two spans corresponding to the same operation + // 1) created by Azure SDK 2) created by HttpClient + // To prevent this duplication we are filtering the span from HttpClient + // as span from Azure SDK contains all relevant information needed. + var parentActivity = Activity.Current?.Parent; + if (parentActivity != null && parentActivity.Source.Name.Equals("Azure.Core.Http")) + { + return false; + } + return true; + }) + .AddAzureMonitorTraceExporter(azureMonitorOptions => + { + azureMonitorOptions.ConnectionString = builder.Configuration[connectionStringConfigurationKey]; + })); } /// diff --git a/src/dotnet/Common/Telemetry/TelemetryActivitySources.cs b/src/dotnet/Common/Telemetry/TelemetryActivitySources.cs new file mode 100644 index 0000000000..ef60536dca --- /dev/null +++ b/src/dotnet/Common/Telemetry/TelemetryActivitySources.cs @@ -0,0 +1,16 @@ +using FoundationaLLM.Common.Constants; +using System.Diagnostics; + +namespace FoundationaLLM.Common.Telemetry +{ + /// + /// Provides predefined telemetry activity sources for the components of the platform. + /// + public class TelemetryActivitySources + { + /// + /// The activity source for the Core API. + /// + public static readonly ActivitySource CoreAPIActivitySource = new (ServiceNames.CoreAPI); + } +} diff --git a/src/dotnet/Configuration/Configuration.csproj b/src/dotnet/Configuration/Configuration.csproj index b2c7eec3db..df24638db7 100644 --- a/src/dotnet/Configuration/Configuration.csproj +++ b/src/dotnet/Configuration/Configuration.csproj @@ -49,7 +49,6 @@ - diff --git a/src/dotnet/Core/Core.csproj b/src/dotnet/Core/Core.csproj index 7463ec9a98..2d655d0426 100644 --- a/src/dotnet/Core/Core.csproj +++ b/src/dotnet/Core/Core.csproj @@ -10,13 +10,16 @@ - + - + + + + diff --git a/src/dotnet/CoreAPI/Controllers/CompletionsController.cs b/src/dotnet/CoreAPI/Controllers/CompletionsController.cs index 7683eb984f..274697d8e1 100644 --- a/src/dotnet/CoreAPI/Controllers/CompletionsController.cs +++ b/src/dotnet/CoreAPI/Controllers/CompletionsController.cs @@ -7,6 +7,7 @@ using FoundationaLLM.Common.Models.Orchestration.Request; using FoundationaLLM.Common.Models.ResourceProviders; using FoundationaLLM.Common.Models.ResourceProviders.Agent; +using FoundationaLLM.Common.Telemetry; using FoundationaLLM.Core.Interfaces; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; @@ -82,6 +83,9 @@ public async Task GetCompletion(string instanceId, [FromBody] Com [HttpPost("async-completions")] public async Task> StartCompletionOperation(string instanceId, CompletionRequest completionRequest) { + using var telemetryActivity = TelemetryActivitySources.CoreAPIActivitySource.StartActivity( + ) + var state = await _coreService.StartCompletionOperation(instanceId, completionRequest); return Accepted(state); } diff --git a/src/dotnet/CoreAPI/CoreAPI.csproj b/src/dotnet/CoreAPI/CoreAPI.csproj index d76dd36c29..089e507bc2 100644 --- a/src/dotnet/CoreAPI/CoreAPI.csproj +++ b/src/dotnet/CoreAPI/CoreAPI.csproj @@ -10,6 +10,7 @@ FoundationaLLM.Core.API FoundationaLLM.Core.API True + FoundationaLLM.Core.API.Program @@ -29,14 +30,14 @@ - + - + diff --git a/src/dotnet/CoreWorker/CoreWorker.csproj b/src/dotnet/CoreWorker/CoreWorker.csproj index 26c76421de..3db57311a5 100644 --- a/src/dotnet/CoreWorker/CoreWorker.csproj +++ b/src/dotnet/CoreWorker/CoreWorker.csproj @@ -13,12 +13,16 @@ - + + + + + diff --git a/src/dotnet/Gatekeeper/Gatekeeper.csproj b/src/dotnet/Gatekeeper/Gatekeeper.csproj index 83a5c56f84..84971e190f 100644 --- a/src/dotnet/Gatekeeper/Gatekeeper.csproj +++ b/src/dotnet/Gatekeeper/Gatekeeper.csproj @@ -9,10 +9,6 @@ True - - - - diff --git a/src/dotnet/Gateway/Gateway.csproj b/src/dotnet/Gateway/Gateway.csproj index 40ee56808d..3f69b7287a 100644 --- a/src/dotnet/Gateway/Gateway.csproj +++ b/src/dotnet/Gateway/Gateway.csproj @@ -9,7 +9,8 @@ - + + diff --git a/src/dotnet/ManagementAPI/ManagementAPI.csproj b/src/dotnet/ManagementAPI/ManagementAPI.csproj index 4496df9366..f2ea1482e1 100644 --- a/src/dotnet/ManagementAPI/ManagementAPI.csproj +++ b/src/dotnet/ManagementAPI/ManagementAPI.csproj @@ -27,7 +27,7 @@ - + @@ -39,7 +39,6 @@ - diff --git a/src/dotnet/Orchestration/Orchestration.csproj b/src/dotnet/Orchestration/Orchestration.csproj index 10e4427f3d..97863d6cb9 100644 --- a/src/dotnet/Orchestration/Orchestration.csproj +++ b/src/dotnet/Orchestration/Orchestration.csproj @@ -16,8 +16,6 @@ - - diff --git a/src/dotnet/Orchestration/Orchestration/ExplodedObjectsManager.cs b/src/dotnet/Orchestration/Orchestration/ExplodedObjectsManager.cs index 160e518889..dfb9f2df17 100644 --- a/src/dotnet/Orchestration/Orchestration/ExplodedObjectsManager.cs +++ b/src/dotnet/Orchestration/Orchestration/ExplodedObjectsManager.cs @@ -1,6 +1,4 @@ -using ZstdSharp.Unsafe; - -namespace FoundationaLLM.Orchestration.Core.Orchestration +namespace FoundationaLLM.Orchestration.Core.Orchestration { /// /// Manages the exploded objects dictionary ensuring consistency and integrity. diff --git a/src/dotnet/Orchestration/Orchestration/OrchestrationBuilder.cs b/src/dotnet/Orchestration/Orchestration/OrchestrationBuilder.cs index c2e8562326..d90f706ad6 100644 --- a/src/dotnet/Orchestration/Orchestration/OrchestrationBuilder.cs +++ b/src/dotnet/Orchestration/Orchestration/OrchestrationBuilder.cs @@ -1,5 +1,3 @@ -using AngleSharp.Common; -using ClosedXML.Excel; using FoundationaLLM.Common.Constants; using FoundationaLLM.Common.Constants.Agents; using FoundationaLLM.Common.Constants.ResourceProviders; diff --git a/src/dotnet/Vectorization/Vectorization.csproj b/src/dotnet/Vectorization/Vectorization.csproj index d28073c91a..c7c58592bd 100644 --- a/src/dotnet/Vectorization/Vectorization.csproj +++ b/src/dotnet/Vectorization/Vectorization.csproj @@ -10,23 +10,6 @@ True - - - - - - - - - - - - - - - - - diff --git a/src/dotnet/VectorizationEngine/VectorizationEngine.csproj b/src/dotnet/VectorizationEngine/VectorizationEngine.csproj index d9b8b58d8e..dc8a3dc4b2 100644 --- a/src/dotnet/VectorizationEngine/VectorizationEngine.csproj +++ b/src/dotnet/VectorizationEngine/VectorizationEngine.csproj @@ -18,7 +18,6 @@ - diff --git a/tests/dotnet/Common.Tests/Common.Tests.csproj b/tests/dotnet/Common.Tests/Common.Tests.csproj index 89662a91ac..4055f0599d 100644 --- a/tests/dotnet/Common.Tests/Common.Tests.csproj +++ b/tests/dotnet/Common.Tests/Common.Tests.csproj @@ -12,9 +12,9 @@ - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/tests/dotnet/Core.Client.Tests/Core.Client.Tests.csproj b/tests/dotnet/Core.Client.Tests/Core.Client.Tests.csproj index 7336d959d9..c3a9455e07 100644 --- a/tests/dotnet/Core.Client.Tests/Core.Client.Tests.csproj +++ b/tests/dotnet/Core.Client.Tests/Core.Client.Tests.csproj @@ -10,9 +10,9 @@ - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/tests/dotnet/Core.Examples.LoadTests/Core.Examples.LoadTests.csproj b/tests/dotnet/Core.Examples.LoadTests/Core.Examples.LoadTests.csproj index 2c9d731030..2f08de983c 100644 --- a/tests/dotnet/Core.Examples.LoadTests/Core.Examples.LoadTests.csproj +++ b/tests/dotnet/Core.Examples.LoadTests/Core.Examples.LoadTests.csproj @@ -13,11 +13,10 @@ - + - - - + + diff --git a/tests/dotnet/Core.Examples/Core.Examples.csproj b/tests/dotnet/Core.Examples/Core.Examples.csproj index 9b83587ccc..84f278a3fa 100644 --- a/tests/dotnet/Core.Examples/Core.Examples.csproj +++ b/tests/dotnet/Core.Examples/Core.Examples.csproj @@ -17,9 +17,9 @@ - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/tests/dotnet/Core.Tests/Core.Tests.csproj b/tests/dotnet/Core.Tests/Core.Tests.csproj index 992e9b737f..d48de6f90f 100644 --- a/tests/dotnet/Core.Tests/Core.Tests.csproj +++ b/tests/dotnet/Core.Tests/Core.Tests.csproj @@ -12,9 +12,9 @@ - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/tests/dotnet/Gatekeeper.Tests/Gatekeeper.Tests.csproj b/tests/dotnet/Gatekeeper.Tests/Gatekeeper.Tests.csproj index 431d92f219..d208d155ba 100644 --- a/tests/dotnet/Gatekeeper.Tests/Gatekeeper.Tests.csproj +++ b/tests/dotnet/Gatekeeper.Tests/Gatekeeper.Tests.csproj @@ -10,9 +10,9 @@ - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/tests/dotnet/Management.Client.Tests/Management.Client.Tests.csproj b/tests/dotnet/Management.Client.Tests/Management.Client.Tests.csproj index 351e9abe44..8e6e5e0f7e 100644 --- a/tests/dotnet/Management.Client.Tests/Management.Client.Tests.csproj +++ b/tests/dotnet/Management.Client.Tests/Management.Client.Tests.csproj @@ -10,9 +10,9 @@ - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/tests/dotnet/Orchestration.Tests/Orchestration.Tests.csproj b/tests/dotnet/Orchestration.Tests/Orchestration.Tests.csproj index bb60e0b9b4..52cb7d0480 100644 --- a/tests/dotnet/Orchestration.Tests/Orchestration.Tests.csproj +++ b/tests/dotnet/Orchestration.Tests/Orchestration.Tests.csproj @@ -12,9 +12,9 @@ - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/tests/dotnet/SemanticKernel.Tests/SemanticKernel.Tests.csproj b/tests/dotnet/SemanticKernel.Tests/SemanticKernel.Tests.csproj index 4f6e687f5c..1a2a1e35ca 100644 --- a/tests/dotnet/SemanticKernel.Tests/SemanticKernel.Tests.csproj +++ b/tests/dotnet/SemanticKernel.Tests/SemanticKernel.Tests.csproj @@ -10,9 +10,9 @@ - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/tests/dotnet/Vectorization.Tests/Vectorization.Tests.csproj b/tests/dotnet/Vectorization.Tests/Vectorization.Tests.csproj index ed175cb7a0..b26c11a41e 100644 --- a/tests/dotnet/Vectorization.Tests/Vectorization.Tests.csproj +++ b/tests/dotnet/Vectorization.Tests/Vectorization.Tests.csproj @@ -11,8 +11,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all From b97438182c1d172f2c28e857e4d0c856e323c92a Mon Sep 17 00:00:00 2001 From: Ciprian Jichici Date: Wed, 11 Dec 2024 18:02:29 +0200 Subject: [PATCH 2/5] Activate telemetry on completions endpoint --- src/dotnet/Common/Common.csproj | 9 +++++ .../Constants/Data/TelemetryActivities.json | 10 +++++- .../Templates/TelemetryActivityNames.cs | 19 ++++++++++ .../Templates/TelemetryActivityNames.tt | 36 +++++++++++++++++++ .../Controllers/CompletionsController.cs | 33 +++++++++++++++-- 5 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 src/dotnet/Common/Templates/TelemetryActivityNames.cs create mode 100644 src/dotnet/Common/Templates/TelemetryActivityNames.tt diff --git a/src/dotnet/Common/Common.csproj b/src/dotnet/Common/Common.csproj index f0920927ca..811102afb3 100644 --- a/src/dotnet/Common/Common.csproj +++ b/src/dotnet/Common/Common.csproj @@ -79,6 +79,10 @@ AuthorizableActionNames.cs TextTemplatingFileGenerator + + TelemetryActivityNames.cs + TextTemplatingFileGenerator + AuthorizationKeyVaultSecretNames.cs TextTemplatingFileGenerator @@ -166,6 +170,11 @@ True AuthorizableActionNames.tt + + TelemetryActivityNames.tt + True + True + AuthorizationKeyVaultSecretNames.tt True diff --git a/src/dotnet/Common/Constants/Data/TelemetryActivities.json b/src/dotnet/Common/Constants/Data/TelemetryActivities.json index e38961b107..f460b12d16 100644 --- a/src/dotnet/Common/Constants/Data/TelemetryActivities.json +++ b/src/dotnet/Common/Constants/Data/TelemetryActivities.json @@ -1,5 +1,13 @@ [ { - + "telemetry_activity_source": "CoreAPI", + "telemetry_activities": [ + { + "name": "AsyncCompletions_StartCompletionOperation" + }, + { + "name": "Completions_GetCompletion" + } + ] } ] diff --git a/src/dotnet/Common/Templates/TelemetryActivityNames.cs b/src/dotnet/Common/Templates/TelemetryActivityNames.cs new file mode 100644 index 0000000000..509478f6f0 --- /dev/null +++ b/src/dotnet/Common/Templates/TelemetryActivityNames.cs @@ -0,0 +1,19 @@ +namespace FoundationaLLM.Common.Constants.Telemetry +{ + /// + /// Defines constants for all telemetry activity names. + /// + public static class TelemetryActivityNames + { + /// + /// The telemetry activity name for the CoreAPI AsyncCompletions_StartCompletionOperation action. + /// + public const string CoreAPI_AsyncCompletions_StartCompletionOperation = "AsyncCompletions_StartCompletionOperation"; + + /// + /// The telemetry activity name for the CoreAPI Completions_GetCompletion action. + /// + public const string CoreAPI_Completions_GetCompletion = "Completions_GetCompletion"; + + } +} diff --git a/src/dotnet/Common/Templates/TelemetryActivityNames.tt b/src/dotnet/Common/Templates/TelemetryActivityNames.tt new file mode 100644 index 0000000000..6e2b93ee2f --- /dev/null +++ b/src/dotnet/Common/Templates/TelemetryActivityNames.tt @@ -0,0 +1,36 @@ +<#@ template debug="false" hostspecific="true" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.IO" #> +<#@ assembly name="System.Text.Json" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ assembly name="System.Memory" #> +<#@ import namespace="System.Text.Json" #> +<#@ import namespace="System.Text.Json.Nodes" #> +<#@ output extension=".cs" #> +<# + string inputFileName = this.Host.ResolvePath($"../Constants/Data/TelemetryActivities.json"); + string inputContent = File.ReadAllText(inputFileName); + var jsonObjects = JsonNode.Parse(inputContent)!; +#> +namespace FoundationaLLM.Common.Constants.Telemetry +{ + /// + /// Defines constants for all telemetry activity names. + /// + public static class TelemetryActivityNames + { +<# + foreach (var jsonObject in jsonObjects.AsArray()) + { + foreach (var telemetryActivity in jsonObject["telemetry_activities"].AsArray()) + {#> + /// + /// The telemetry activity name for the <#= jsonObject["telemetry_activity_source"] #> <#= telemetryActivity["name"] #> action. + /// + public const string <#= jsonObject["telemetry_activity_source"] #>_<#= telemetryActivity["name"] #> = "<#= telemetryActivity["name"] #>"; + +<#} + } +#> + } +} diff --git a/src/dotnet/CoreAPI/Controllers/CompletionsController.cs b/src/dotnet/CoreAPI/Controllers/CompletionsController.cs index 274697d8e1..1c269ac7cd 100644 --- a/src/dotnet/CoreAPI/Controllers/CompletionsController.cs +++ b/src/dotnet/CoreAPI/Controllers/CompletionsController.cs @@ -1,6 +1,7 @@ using FoundationaLLM.Common.Authentication; using FoundationaLLM.Common.Constants.Authorization; using FoundationaLLM.Common.Constants.ResourceProviders; +using FoundationaLLM.Common.Constants.Telemetry; using FoundationaLLM.Common.Exceptions; using FoundationaLLM.Common.Interfaces; using FoundationaLLM.Common.Models.Orchestration; @@ -12,6 +13,7 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using System.Diagnostics; namespace FoundationaLLM.Core.API.Controllers { @@ -69,10 +71,25 @@ public CompletionsController(ICoreService coreService, /// The instance ID of the current request. /// The user prompt for which to generate a completion. [HttpPost("completions", Name = "GetCompletion")] - public async Task GetCompletion(string instanceId, [FromBody] CompletionRequest completionRequest) => - !string.IsNullOrWhiteSpace(completionRequest.SessionId) + public async Task GetCompletion(string instanceId, [FromBody] CompletionRequest completionRequest) + { + using var telemetryActivity = TelemetryActivitySources.CoreAPIActivitySource.StartActivity( + TelemetryActivityNames.CoreAPI_Completions_GetCompletion, + ActivityKind.Consumer, + parentContext: default, + tags: new Dictionary + { + { "InstanceId", instanceId }, + { "ConversationId", completionRequest.SessionId ?? "N/A" }, + { "OperationId", completionRequest.OperationId ?? "N/A" }, + { "UPN", _callContext.CurrentUserIdentity?.UPN ?? "N/A" }, + { "UserId", _callContext.CurrentUserIdentity?.UPN ?? "N/A" } + }); + + return !string.IsNullOrWhiteSpace(completionRequest.SessionId) ? Ok(await _coreService.GetChatCompletionAsync(instanceId, completionRequest)) : Ok(await _coreService.GetCompletionAsync(instanceId, completionRequest)); + } /// /// Begins a completion operation. @@ -84,7 +101,17 @@ public async Task GetCompletion(string instanceId, [FromBody] Com public async Task> StartCompletionOperation(string instanceId, CompletionRequest completionRequest) { using var telemetryActivity = TelemetryActivitySources.CoreAPIActivitySource.StartActivity( - ) + TelemetryActivityNames.CoreAPI_AsyncCompletions_StartCompletionOperation, + ActivityKind.Consumer, + parentContext: default, + tags: new Dictionary + { + { "InstanceId", instanceId }, + { "ConversationId", completionRequest.SessionId ?? "N/A" }, + { "OperationId", completionRequest.OperationId ?? "N/A" }, + { "UPN", _callContext.CurrentUserIdentity?.UPN ?? "N/A" }, + { "UserId", _callContext.CurrentUserIdentity?.UPN ?? "N/A" } + }); var state = await _coreService.StartCompletionOperation(instanceId, completionRequest); return Accepted(state); From 138792f0280163fc4a30d8da56fdaeb98ae5dae5 Mon Sep 17 00:00:00 2001 From: Ciprian Jichici Date: Wed, 11 Dec 2024 20:13:23 +0200 Subject: [PATCH 3/5] Add OpenTelemetry logging provider --- src/dotnet/Common/Services/DependencyInjection.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/dotnet/Common/Services/DependencyInjection.cs b/src/dotnet/Common/Services/DependencyInjection.cs index 592dcdb751..0eba9c221f 100644 --- a/src/dotnet/Common/Services/DependencyInjection.cs +++ b/src/dotnet/Common/Services/DependencyInjection.cs @@ -69,6 +69,19 @@ public static void AddOpenTelemetry(this IHostApplicationBuilder builder, { "service.instance.id", ValidatedEnvironment.MachineName } }); + + // Add the OpenTelemetry logging provider and send logs to Azure Monitor. + builder.Logging.AddOpenTelemetry(openTelemetryLoggerOptions => + { + openTelemetryLoggerOptions.SetResourceBuilder(resourceBuilder); + openTelemetryLoggerOptions.IncludeFormattedMessage = true; + openTelemetryLoggerOptions.IncludeScopes = true; + openTelemetryLoggerOptions.AddAzureMonitorLogExporter(azureMonitorOptions => + { + azureMonitorOptions.ConnectionString = builder.Configuration[connectionStringConfigurationKey]; + }); + }); + // Add the OpenTelemetry telemetry service and send telemetry data to Azure Monitor. builder.Services.AddOpenTelemetry() .WithTracing(traceProviderBuilder => traceProviderBuilder From cfc300c6ea2b7d47335af401a115b612e48a9b07 Mon Sep 17 00:00:00 2001 From: Ciprian Jichici Date: Wed, 11 Dec 2024 20:33:49 +0200 Subject: [PATCH 4/5] Improve telemetry activity tag names --- .../Telemetry/TelemetryActivityTagNames.cs | 33 +++++++++++++++++++ .../Controllers/CompletionsController.cs | 20 +++++------ 2 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 src/dotnet/Common/Constants/Telemetry/TelemetryActivityTagNames.cs diff --git a/src/dotnet/Common/Constants/Telemetry/TelemetryActivityTagNames.cs b/src/dotnet/Common/Constants/Telemetry/TelemetryActivityTagNames.cs new file mode 100644 index 0000000000..136d8394cd --- /dev/null +++ b/src/dotnet/Common/Constants/Telemetry/TelemetryActivityTagNames.cs @@ -0,0 +1,33 @@ +namespace FoundationaLLM.Common.Constants.Telemetry +{ + /// + /// Provides the names of the tags used in telemetry activities. + /// + public static class TelemetryActivityTagNames + { + /// + /// The FoundationaLLM instance identifier tag. + /// + public const string InstanceId = "FoundationaLLM-InstanceId"; + + /// + /// The FoundationaLLM conversation identifier tag. + /// + public const string ConversationId = "FoundationaLLM-ConversationId"; + + /// + /// The FoundationaLLM operation identifier tag. + /// + public const string OperationId = "FoundationaLLM-OperationId"; + + /// + /// The FoundationaLLM user principal name tag. + /// + public const string UPN = "FoundationaLLM-UPN"; + + /// + /// The FoundationaLLM user identifier tag. + /// + public const string UserId = "FoundationaLLM-UserId"; + } +} diff --git a/src/dotnet/CoreAPI/Controllers/CompletionsController.cs b/src/dotnet/CoreAPI/Controllers/CompletionsController.cs index 1c269ac7cd..9f7df3f926 100644 --- a/src/dotnet/CoreAPI/Controllers/CompletionsController.cs +++ b/src/dotnet/CoreAPI/Controllers/CompletionsController.cs @@ -79,11 +79,11 @@ public async Task GetCompletion(string instanceId, [FromBody] Com parentContext: default, tags: new Dictionary { - { "InstanceId", instanceId }, - { "ConversationId", completionRequest.SessionId ?? "N/A" }, - { "OperationId", completionRequest.OperationId ?? "N/A" }, - { "UPN", _callContext.CurrentUserIdentity?.UPN ?? "N/A" }, - { "UserId", _callContext.CurrentUserIdentity?.UPN ?? "N/A" } + { TelemetryActivityTagNames.InstanceId, instanceId }, + { TelemetryActivityTagNames.ConversationId, completionRequest.SessionId ?? "N/A" }, + { TelemetryActivityTagNames.OperationId, completionRequest.OperationId ?? "N/A" }, + { TelemetryActivityTagNames.UPN, _callContext.CurrentUserIdentity?.UPN ?? "N/A" }, + { TelemetryActivityTagNames.UserId, _callContext.CurrentUserIdentity?.UserId ?? "N/A" } }); return !string.IsNullOrWhiteSpace(completionRequest.SessionId) @@ -106,11 +106,11 @@ public async Task> StartCompletionOperation(s parentContext: default, tags: new Dictionary { - { "InstanceId", instanceId }, - { "ConversationId", completionRequest.SessionId ?? "N/A" }, - { "OperationId", completionRequest.OperationId ?? "N/A" }, - { "UPN", _callContext.CurrentUserIdentity?.UPN ?? "N/A" }, - { "UserId", _callContext.CurrentUserIdentity?.UPN ?? "N/A" } + { TelemetryActivityTagNames.InstanceId, instanceId }, + { TelemetryActivityTagNames.ConversationId, completionRequest.SessionId ?? "N/A" }, + { TelemetryActivityTagNames.OperationId, completionRequest.OperationId ?? "N/A" }, + { TelemetryActivityTagNames.UPN, _callContext.CurrentUserIdentity?.UPN ?? "N/A" }, + { TelemetryActivityTagNames.UserId, _callContext.CurrentUserIdentity?.UserId ?? "N/A" } }); var state = await _coreService.StartCompletionOperation(instanceId, completionRequest); From 140f87ce3e06358bc40322bc754d760772711d0f Mon Sep 17 00:00:00 2001 From: Carey Payette Date: Wed, 11 Dec 2024 13:46:18 -0500 Subject: [PATCH 5/5] Changed OpenTelemetry.Instrumentation.Http and AspNetCore to 1.9.0 --- src/dotnet/AzureOpenAI/AzureOpenAI.csproj | 4 ++-- src/dotnet/Core/Core.csproj | 4 ++-- src/dotnet/CoreWorker/CoreWorker.csproj | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/dotnet/AzureOpenAI/AzureOpenAI.csproj b/src/dotnet/AzureOpenAI/AzureOpenAI.csproj index ec64651bbe..010e1b5e7d 100644 --- a/src/dotnet/AzureOpenAI/AzureOpenAI.csproj +++ b/src/dotnet/AzureOpenAI/AzureOpenAI.csproj @@ -10,8 +10,8 @@ - - + + diff --git a/src/dotnet/Core/Core.csproj b/src/dotnet/Core/Core.csproj index 2d655d0426..2892216408 100644 --- a/src/dotnet/Core/Core.csproj +++ b/src/dotnet/Core/Core.csproj @@ -16,8 +16,8 @@ - - + + diff --git a/src/dotnet/CoreWorker/CoreWorker.csproj b/src/dotnet/CoreWorker/CoreWorker.csproj index 3db57311a5..d917e07d75 100644 --- a/src/dotnet/CoreWorker/CoreWorker.csproj +++ b/src/dotnet/CoreWorker/CoreWorker.csproj @@ -20,8 +20,8 @@ - - + +