Skip to content

Commit 5560cba

Browse files
authored
feat: configure Databricks client user agent (#12)
Add AppKit version and dev mode flag to Databricks client user agent. 🤖 Generated with Claude Code Signed-off-by: Fabian Jakobs <[email protected]>
1 parent b5c143c commit 5560cba

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

packages/app-kit/src/utils/databricks-client-middleware.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { AsyncLocalStorage } from "node:async_hooks";
2-
import { type sql, WorkspaceClient } from "@databricks/sdk-experimental";
2+
import {
3+
type ClientOptions,
4+
type sql,
5+
WorkspaceClient,
6+
} from "@databricks/sdk-experimental";
37
import type express from "express";
8+
import {
9+
name as productName,
10+
version as productVersion,
11+
} from "../../package.json";
412

513
export type RequestContext = {
614
userDatabricksClient?: WorkspaceClient;
@@ -14,9 +22,22 @@ export type RequestContext = {
1422

1523
const asyncLocalStorage = new AsyncLocalStorage<RequestContext>();
1624

17-
export async function databricksClientMiddleware(): Promise<express.RequestHandler> {
18-
const serviceDatabricksClient = new WorkspaceClient({});
25+
function getClientOptions(): ClientOptions {
26+
const isDev = process.env.NODE_ENV === "development";
27+
const normalizedVersion = productVersion
28+
.split(".")
29+
.slice(0, 3)
30+
.join(".") as ClientOptions["productVersion"];
31+
32+
return {
33+
product: productName,
34+
productVersion: normalizedVersion,
35+
...(isDev && { userAgentExtra: { mode: "dev" } }),
36+
};
37+
}
1938

39+
export async function databricksClientMiddleware(): Promise<express.RequestHandler> {
40+
const serviceDatabricksClient = new WorkspaceClient({}, getClientOptions());
2041
const warehouseId = getWarehouseId(serviceDatabricksClient);
2142
const workspaceId = getWorkspaceId(serviceDatabricksClient);
2243
const serviceUserId = (await serviceDatabricksClient.currentUser.me()).id;
@@ -34,11 +55,14 @@ export async function databricksClientMiddleware(): Promise<express.RequestHandl
3455
let userDatabricksClient: WorkspaceClient | undefined;
3556
const host = process.env.DATABRICKS_HOST;
3657
if (userToken && host) {
37-
userDatabricksClient = new WorkspaceClient({
38-
token: userToken,
39-
host,
40-
authType: "pat",
41-
});
58+
userDatabricksClient = new WorkspaceClient(
59+
{
60+
token: userToken,
61+
host,
62+
authType: "pat",
63+
},
64+
getClientOptions(),
65+
);
4266
} else if (process.env.NODE_ENV === "development") {
4367
// in local development service and no user token are the same
4468
// TODO: use `databricks apps run-local` to fix this

0 commit comments

Comments
 (0)