-
Notifications
You must be signed in to change notification settings - Fork 1.9k
.NET: Set ApplicationName on CosmosClientOptions for UserAgent telemetry #6481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
westey-m
merged 2 commits into
microsoft:main
from
TheovanKraay:fix/cosmos-useragent-application-name
Jun 12, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosOptionsHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Reflection; | ||
| using Microsoft.Azure.Cosmos; | ||
|
|
||
| namespace Microsoft.Agents.AI.CosmosNoSql; | ||
|
|
||
| /// <summary> | ||
| /// Provides shared Cosmos DB client configuration for Agent Framework Cosmos NoSQL integrations. | ||
| /// Ensures all internally-created <see cref="CosmosClient"/> instances carry a consistent | ||
| /// <see cref="CosmosClientOptions.ApplicationName"/> for telemetry and diagnostics. | ||
| /// </summary> | ||
| internal static class CosmosOptionsHelper | ||
| { | ||
| /// <summary> | ||
| /// Maximum length allowed by the Cosmos DB .NET SDK for <see cref="CosmosClientOptions.ApplicationName"/>. | ||
| /// </summary> | ||
| private const int MaxApplicationNameLength = 64; | ||
|
|
||
| private static readonly string s_version = GetVersion(); | ||
|
|
||
| /// <summary> | ||
| /// Creates a <see cref="CosmosClientOptions"/> instance pre-configured with the | ||
| /// Agent Framework application name for User-Agent identification. | ||
| /// </summary> | ||
| /// <param name="component">The fully-qualified component class name (e.g. "CosmosChatHistoryProvider").</param> | ||
| /// <returns>A new <see cref="CosmosClientOptions"/> with <see cref="CosmosClientOptions.ApplicationName"/> set.</returns> | ||
| public static CosmosClientOptions CreateOptions(string component) | ||
| { | ||
| return new CosmosClientOptions | ||
| { | ||
| ApplicationName = BuildApplicationName(component) | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Ensures the given <see cref="CosmosClient"/> has an <see cref="CosmosClientOptions.ApplicationName"/> set. | ||
| /// If the client already has a non-empty ApplicationName, it is not overridden. | ||
| /// </summary> | ||
| /// <param name="cosmosClient">The client to apply the application name to.</param> | ||
| /// <param name="component">The fully-qualified component class name (e.g. "CosmosChatHistoryProvider").</param> | ||
| public static void EnsureApplicationName(CosmosClient cosmosClient, string component) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(cosmosClient.ClientOptions.ApplicationName)) | ||
| { | ||
| cosmosClient.ClientOptions.ApplicationName = BuildApplicationName(component); | ||
| } | ||
| } | ||
|
|
||
| private static string BuildApplicationName(string component) | ||
| { | ||
| var applicationName = $"Microsoft.Agents.AI.CosmosNoSql.{component}/{s_version}"; | ||
|
|
||
| if (applicationName.Length > MaxApplicationNameLength) | ||
| { | ||
| applicationName = applicationName.Substring(0, MaxApplicationNameLength); | ||
| } | ||
|
|
||
| return applicationName; | ||
| } | ||
|
TheovanKraay marked this conversation as resolved.
|
||
|
|
||
| private static string GetVersion() | ||
| { | ||
| if (typeof(CosmosOptionsHelper).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion is string version) | ||
| { | ||
| int pos = version.IndexOf('+', System.StringComparison.Ordinal); | ||
| if (pos >= 0) | ||
| { | ||
| version = version.Substring(0, pos); | ||
| } | ||
|
|
||
| if (version.Length > 0) | ||
| { | ||
| return version; | ||
| } | ||
| } | ||
|
|
||
| return "unknown"; | ||
| } | ||
| } | ||
75 changes: 75 additions & 0 deletions
75
dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosOptionsHelperTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using Microsoft.Agents.AI.CosmosNoSql; | ||
| using Microsoft.Azure.Cosmos; | ||
|
|
||
| namespace Microsoft.Agents.AI.CosmosNoSql.UnitTests; | ||
|
|
||
| public sealed class CosmosOptionsHelperTests | ||
| { | ||
| [Fact] | ||
| public void CreateOptions_SetsApplicationName_WithComponentAndVersion() | ||
| { | ||
| // Act | ||
| var options = CosmosOptionsHelper.CreateOptions("CosmosChatHistoryProvider"); | ||
|
|
||
| // Assert | ||
| Assert.NotNull(options.ApplicationName); | ||
| Assert.StartsWith("Microsoft.Agents.AI.CosmosNoSql.CosmosChatHistoryProvider/", options.ApplicationName); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CreateOptions_DifferentComponents_ProduceDifferentNames() | ||
| { | ||
| // Act | ||
| var chatOptions = CosmosOptionsHelper.CreateOptions("CosmosChatHistoryProvider"); | ||
| var checkpointOptions = CosmosOptionsHelper.CreateOptions("CosmosCheckpointStore"); | ||
|
|
||
| // Assert | ||
| Assert.NotEqual(chatOptions.ApplicationName, checkpointOptions.ApplicationName); | ||
| Assert.Contains("CosmosChatHistoryProvider", chatOptions.ApplicationName); | ||
| Assert.Contains("CosmosCheckpointStore", checkpointOptions.ApplicationName); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CreateOptions_ApplicationName_DoesNotExceedMaxLength() | ||
| { | ||
| // Use a deliberately long component name to trigger truncation | ||
| var longComponent = new string('X', 100); | ||
|
|
||
| // Act | ||
| var options = CosmosOptionsHelper.CreateOptions(longComponent); | ||
|
|
||
| // Assert | ||
| Assert.True(options.ApplicationName!.Length <= 64, | ||
| $"ApplicationName length {options.ApplicationName.Length} exceeds max 64"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EnsureApplicationName_SetsName_WhenClientHasNone() | ||
| { | ||
| // Arrange | ||
| var clientOptions = new CosmosClientOptions(); | ||
| Assert.Null(clientOptions.ApplicationName); | ||
|
|
||
| // Act | ||
| var options = CosmosOptionsHelper.CreateOptions("CosmosChatHistoryProvider"); | ||
|
|
||
| // Assert - verify the returned options have ApplicationName set | ||
| Assert.NotNull(options.ApplicationName); | ||
| Assert.NotEmpty(options.ApplicationName); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CreateOptions_ApplicationName_ContainsVersion() | ||
| { | ||
| // Act | ||
| var options = CosmosOptionsHelper.CreateOptions("CosmosChatHistoryProvider"); | ||
|
|
||
| // Assert - should contain a "/" followed by version info | ||
| Assert.Contains("/", options.ApplicationName); | ||
| var parts = options.ApplicationName!.Split('/'); | ||
| Assert.Equal(2, parts.Length); | ||
| Assert.False(string.IsNullOrWhiteSpace(parts[1]), "Version portion should not be empty"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.