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 {
9+ name as productName ,
10+ version as productVersion ,
11+ } from "../../package.json" ;
412
513export type RequestContext = {
614 userDatabricksClient ?: WorkspaceClient ;
@@ -14,9 +22,22 @@ export type RequestContext = {
1422
1523const 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