Skip to content

[Cosmos .NET SDK] - Adds User Agent to Identify Usage#3876

Merged
eerhardt merged 5 commits intomainfrom
users/kundadebdatta/3789_aspire_introduce_user_agent
Apr 23, 2024
Merged

[Cosmos .NET SDK] - Adds User Agent to Identify Usage#3876
eerhardt merged 5 commits intomainfrom
users/kundadebdatta/3789_aspire_introduce_user_agent

Conversation

@kundadebdatta
Copy link
Copy Markdown
Member

@kundadebdatta kundadebdatta commented Apr 23, 2024

Pull Request Description:

The purpose of this PR is to populate the ApplicationName parameter in the CosmosClientOptions so that the user agent can be generated with the necessary suffix. This will help the cosmos db team to identify if the request was originated from Aspire. A sample of the user agent will look like the below:

cosmos-netstandard-sdk/3.39.1|0|X64|Microsoft Windows 10.0.22635|.NET 6.0.29|N|F 00000111|AspireMicrosoftAzureCosmosDBClient|

Microsoft Reviewers: Open in CodeFlow

@ghost ghost added the area-integrations Issues pertaining to Aspire Integrations packages label Apr 23, 2024
@kundadebdatta kundadebdatta self-assigned this Apr 23, 2024
@mitchdenny
Copy link
Copy Markdown
Member

LGTM. @eerhardt

Comment thread src/Shared/Cosmos/CosmosConstants.cs Outdated
configureSettings?.Invoke(settings);

var clientOptions = new CosmosClientOptions();
clientOptions.ApplicationName = CosmosConstants.CosmosApplicationName;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to set the same for the the EntityFramework component: https://github.com/dotnet/aspire/tree/main/src/Components/Aspire.Microsoft.EntityFrameworkCore.Cosmos?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if we really need to set this. When I look at the definition of CosmosDbContextOptionsBuilder, I do not see an option to set the application name in the entity framework library. Cc: @kirankumarkolli .

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var cosmosApplicationName = CosmosConstants.CosmosApplicationName;
if (!string.IsNullOrEmpty(clientOptions.ApplicationName))
{
cosmosApplicationName += clientOptions.ApplicationName;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Add a separator

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the | separator at the moment.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the | the correct separator?

https://azure.github.io/azure-sdk/general_azurecore.html#telemetry-policy

Seems to suggest using a slash might be preferred?

Comment thread src/Shared/Cosmos/CosmosConstants.cs Outdated
/// Defines the application name used to interact with the aszure cosmos db. This will be suffixed to the
/// cosmos user-agent to include with every Azure Cosmos DB service interaction.
/// </summary>
internal const string CosmosApplicationName = "AspireCosmosDBClient";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an upper limit on the User Agent? If so, we might want to consider shortening this if possible. Maybe just Aspire as the User Agent already contains the fact that it's a Cosmos DB Client.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a check in place for validating and trimming the Suffix within the Microsoft.Azure.Documents.UserAgentContainer:

    public string Suffix
    {
        get
        {
            return suffix;
        }
        set
        {
            suffix = value;
            if (suffix.Length > 64)
            {
                suffix = suffix.Substring(0, 64);
            }

            userAgent = BaseUserAgent + suffix;
            userAgentUTF8 = Encoding.UTF8.GetBytes(userAgent);
        }
    }

So, in case it's larger than the defined size, we will fit it within the 64 character window, which would still contain the required application name prefix for us to track.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly the point. Because there is a limit, any character we used for the fixed part takes away from the potential user app name.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agree with @ealsur. Just by using Aspire doesn't make much difference, in terms of identification. With this, we would make extra room for the user provided app name, if any.

Addressed with a new commit.

@eerhardt eerhardt merged commit 989b816 into main Apr 23, 2024
@eerhardt eerhardt deleted the users/kundadebdatta/3789_aspire_introduce_user_agent branch April 23, 2024 22:17
@eerhardt
Copy link
Copy Markdown
Member

/backport to release/8.0

@github-actions
Copy link
Copy Markdown
Contributor

Started backporting to release/8.0: https://github.com/dotnet/aspire/actions/runs/8807930224

@github-actions github-actions Bot locked and limited conversation to collaborators May 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-integrations Issues pertaining to Aspire Integrations packages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants