11import { 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" ;
37import type express from "express" ;
8+ import { name as productName , version as productVersion } from "../../package.json" ;
49
510export type RequestContext = {
611 userDatabricksClient ?: WorkspaceClient ;
@@ -14,9 +19,19 @@ export type RequestContext = {
1419
1520const asyncLocalStorage = new AsyncLocalStorage < RequestContext > ( ) ;
1621
17- export async function databricksClientMiddleware ( ) : Promise < express . RequestHandler > {
18- const serviceDatabricksClient = new WorkspaceClient ( { } ) ;
22+ function getClientOptions ( ) : ClientOptions {
23+ const isDev = process . env . NODE_ENV === "development" ;
24+ const normalizedVersion = productVersion . split ( "." ) . slice ( 0 , 3 ) . join ( "." ) as ClientOptions [ "productVersion" ] ;
25+
26+ return {
27+ product : productName ,
28+ productVersion : normalizedVersion ,
29+ ...( isDev && { userAgentExtra : { mode : "dev" } } ) ,
30+ } ;
31+ }
1932
33+ export async function databricksClientMiddleware ( ) : Promise < express . RequestHandler > {
34+ const serviceDatabricksClient = new WorkspaceClient ( { } , getClientOptions ( ) ) ;
2035 const warehouseId = getWarehouseId ( serviceDatabricksClient ) ;
2136 const workspaceId = getWorkspaceId ( serviceDatabricksClient ) ;
2237 const serviceUserId = ( await serviceDatabricksClient . currentUser . me ( ) ) . id ;
@@ -34,11 +49,14 @@ export async function databricksClientMiddleware(): Promise<express.RequestHandl
3449 let userDatabricksClient : WorkspaceClient | undefined ;
3550 const host = process . env . DATABRICKS_HOST ;
3651 if ( userToken && host ) {
37- userDatabricksClient = new WorkspaceClient ( {
38- token : userToken ,
39- host,
40- authType : "pat" ,
41- } ) ;
52+ userDatabricksClient = new WorkspaceClient (
53+ {
54+ token : userToken ,
55+ host,
56+ authType : "pat" ,
57+ } ,
58+ getClientOptions ( ) ,
59+ ) ;
4260 } else if ( process . env . NODE_ENV === "development" ) {
4361 // in local development service and no user token are the same
4462 // TODO: use `databricks apps run-local` to fix this
0 commit comments