Skip to content
Merged
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
40 changes: 32 additions & 8 deletions packages/app-kit/src/utils/databricks-client-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { AsyncLocalStorage } from "node:async_hooks";
import { type sql, WorkspaceClient } from "@databricks/sdk-experimental";
import {
type ClientOptions,
type sql,
WorkspaceClient,
} from "@databricks/sdk-experimental";
import type express from "express";
import {
name as productName,
version as productVersion,
} from "../../package.json";

export type RequestContext = {
userDatabricksClient?: WorkspaceClient;
Expand All @@ -14,9 +22,22 @@ export type RequestContext = {

const asyncLocalStorage = new AsyncLocalStorage<RequestContext>();

export async function databricksClientMiddleware(): Promise<express.RequestHandler> {
const serviceDatabricksClient = new WorkspaceClient({});
function getClientOptions(): ClientOptions {
const isDev = process.env.NODE_ENV === "development";
const normalizedVersion = productVersion
.split(".")
.slice(0, 3)
.join(".") as ClientOptions["productVersion"];

return {
product: productName,
productVersion: normalizedVersion,
...(isDev && { userAgentExtra: { mode: "dev" } }),
Copy link
Member

Choose a reason for hiding this comment

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

we could also simply pass the NODE_ENV value directly, right? And attach that property for every request, also including prod for consistency?

Suggested change
...(isDev && { userAgentExtra: { mode: "dev" } }),
...(isDev && { userAgentExtra: { mode: process.env.NODE_ENV } }),

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This will add too much noise to the logs. Customers can add anything in NODE_ENV

Copy link
Member

@pkosiec pkosiec Dec 9, 2025

Choose a reason for hiding this comment

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

ok, but then why do we need the dev metadata then? just curious, why not production instead? prod is more important for us, right?

};
}

export async function databricksClientMiddleware(): Promise<express.RequestHandler> {
const serviceDatabricksClient = new WorkspaceClient({}, getClientOptions());
const warehouseId = getWarehouseId(serviceDatabricksClient);
const workspaceId = getWorkspaceId(serviceDatabricksClient);
const serviceUserId = (await serviceDatabricksClient.currentUser.me()).id;
Expand All @@ -34,11 +55,14 @@ export async function databricksClientMiddleware(): Promise<express.RequestHandl
let userDatabricksClient: WorkspaceClient | undefined;
const host = process.env.DATABRICKS_HOST;
if (userToken && host) {
userDatabricksClient = new WorkspaceClient({
token: userToken,
host,
authType: "pat",
});
userDatabricksClient = new WorkspaceClient(
{
token: userToken,
host,
authType: "pat",
},
getClientOptions(),
);
} else if (process.env.NODE_ENV === "development") {
// in local development service and no user token are the same
// TODO: use `databricks apps run-local` to fix this
Expand Down