Skip to content
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

Made User Agent configurable post Cosmos client creation #32325

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion sdk/cosmosdb/cosmos/review/cosmos.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ export class ClientContext {
diagnosticNode: DiagnosticNodeInternal;
}): Promise<Response_2<T & Resource>>;
// (undocumented)
updateHostFramework(hostFramework: string): void;
// (undocumented)
upsert<T, U = T>({ body, path, resourceType, resourceId, options, partitionKey, diagnosticNode, }: {
body: T;
path: string;
Expand Down Expand Up @@ -699,21 +701,24 @@ export class CosmosClient {
getWriteEndpoints(): Promise<readonly string[]>;
offer(id: string): Offer;
readonly offers: Offers;
updateHostFramework(hostFramework: string): Promise<void>;
}

// @public (undocumented)
export interface CosmosClientOptions {
aadCredentials?: TokenCredential;
agent?: Agent;
connectionPolicy?: ConnectionPolicy;
connectionString?: string;
consistencyLevel?: keyof typeof ConsistencyLevel;
// Warning: (ae-forgotten-export) The symbol "CosmosHeaders_2" needs to be exported by the entry point index.d.ts
//
// (undocumented)
defaultHeaders?: CosmosHeaders_2;
// (undocumented)
diagnosticLevel?: CosmosDbDiagnosticLevel;
endpoint: string;
endpoint?: string;
hostFramework?: string;
httpClient?: HttpClient;
key?: string;
permissionFeed?: PermissionDefinition[];
Expand Down
14 changes: 14 additions & 0 deletions sdk/cosmosdb/cosmos/src/ClientContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import type { DiagnosticFormatter } from "./diagnostics/DiagnosticFormatter";
import { DefaultDiagnosticFormatter } from "./diagnostics/DiagnosticFormatter";
import { CosmosDbDiagnosticLevel } from "./diagnostics/CosmosDbDiagnosticLevel";
import { randomUUID } from "@azure/core-util";
import { getUserAgent } from "./common/platform";

const logger: AzureLogger = createClientLogger("ClientContext");

Expand Down Expand Up @@ -978,4 +979,17 @@ export class ClientContext {
public getClientConfig(): ClientConfigDiagnostic {
return this.clientConfig;
}

private refreshUserAgent(): void {
const updatedUserAgent = getUserAgent(
this.cosmosClientOptions.userAgentSuffix,
this.cosmosClientOptions.hostFramework,
);
this.cosmosClientOptions.defaultHeaders[Constants.HttpHeaders.UserAgent] = updatedUserAgent;
}

public updateHostFramework(hostFramework: string): void {
this.cosmosClientOptions.hostFramework = hostFramework;
this.refreshUserAgent();
}
}
18 changes: 17 additions & 1 deletion sdk/cosmosdb/cosmos/src/CosmosClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ export class CosmosClient {
constructor(optionsOrConnectionString: string | CosmosClientOptions) {
if (typeof optionsOrConnectionString === "string") {
optionsOrConnectionString = parseConnectionString(optionsOrConnectionString);
} else if (optionsOrConnectionString.connectionString) {
const { endpoint, key } = parseConnectionString(optionsOrConnectionString.connectionString);
optionsOrConnectionString.endpoint = endpoint;
optionsOrConnectionString.key = key;
}
if (!optionsOrConnectionString.endpoint) {
throw new Error("Invalid endpoint specified");
}

const endpoint = checkURL(optionsOrConnectionString.endpoint);
if (!endpoint) {
throw new Error("Invalid endpoint specified");
Expand All @@ -97,6 +103,7 @@ export class CosmosClient {

optionsOrConnectionString.defaultHeaders[Constants.HttpHeaders.UserAgent] = getUserAgent(
optionsOrConnectionString.userAgentSuffix,
optionsOrConnectionString.hostFramework,
);

const globalEndpointManager = new GlobalEndpointManager(
Expand Down Expand Up @@ -272,4 +279,13 @@ export class CosmosClient {
this.endpointRefresher.unref();
}
}

/**
* Update the host framework. If provided host framework will be used to generate the defualt SDK user agent.
* @param hostFramework - A custom string.
* @hidden
*/
public async updateHostFramework(hostFramework: string): Promise<void> {
this.clientContext.updateHostFramework(hostFramework);
}
}
6 changes: 5 additions & 1 deletion sdk/cosmosdb/cosmos/src/CosmosClientOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface Agent {

export interface CosmosClientOptions {
/** The service endpoint to use to create the client. */
endpoint: string;
endpoint?: string;
/** The account master or readonly key */
key?: string;
/** An object that contains resources tokens.
Expand Down Expand Up @@ -60,4 +60,8 @@ export interface CosmosClientOptions {
diagnosticLevel?: CosmosDbDiagnosticLevel;
/** @internal */
plugins?: PluginConfig[];
/** An optional parameter that represents the connection string. Your database connection string can be found in the Azure Portal. */
connectionString?: string;
/** A custom string used to generate the default SDK user agent. */
hostFramework?: string;
}
10 changes: 7 additions & 3 deletions sdk/cosmosdb/cosmos/src/common/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import { Constants } from "./constants";
/**
* @hidden
*/
export function getUserAgent(suffix?: string): string {
const ua = `${userAgentDetails()} ${Constants.SDKName}/${Constants.SDKVersion}`;
export function getUserAgent(suffix?: string, hostFramework?: string): string {
let ua = `${userAgentDetails()} ${Constants.SDKName}/${Constants.SDKVersion}`;
if (hostFramework) {
ua = ua + " " + hostFramework;
}
if (suffix) {
return ua + " " + suffix;
ua = ua + " " + suffix;
}

return ua;
}

Expand Down
Loading