2929import io .trino .gateway .ha .router .OAuth2GatewayCookie ;
3030import io .trino .gateway .ha .router .QueryHistoryManager ;
3131import io .trino .gateway .ha .router .RoutingManager ;
32+ import io .trino .gateway .ha .router .TrinoRequestUser ;
3233import io .trino .gateway .proxyserver .ProxyResponseHandler .ProxyResponse ;
3334import jakarta .annotation .PreDestroy ;
3435import jakarta .servlet .http .HttpServletRequest ;
5859import static io .airlift .http .client .Request .Builder .preparePost ;
5960import static io .airlift .http .client .StaticBodyGenerator .createStaticBodyGenerator ;
6061import static io .airlift .jaxrs .AsyncResponseHandler .bindAsyncResponse ;
61- import static io .trino .gateway .ha .handler .ProxyUtils .AUTHORIZATION ;
6262import static io .trino .gateway .ha .handler .ProxyUtils .QUERY_TEXT_LENGTH_FOR_HISTORY ;
6363import static io .trino .gateway .ha .handler .ProxyUtils .SOURCE_HEADER ;
64- import static io .trino .gateway .ha .handler .ProxyUtils .getQueryUser ;
65- import static io .trino .gateway .ha .handler .QueryIdCachingProxyHandler .USER_HEADER ;
6664import static jakarta .ws .rs .core .MediaType .TEXT_PLAIN_TYPE ;
6765import static jakarta .ws .rs .core .Response .Status .BAD_GATEWAY ;
6866import static jakarta .ws .rs .core .Response .Status .OK ;
@@ -86,6 +84,7 @@ public class ProxyRequestHandler
8684 private final boolean addXForwardedHeaders ;
8785 private final List <String > statementPaths ;
8886 private final boolean includeClusterInfoInResponse ;
87+ private final TrinoRequestUser .TrinoRequestUserProvider trinoRequestUserProvider ;
8988
9089 @ Inject
9190 public ProxyRequestHandler (
@@ -97,6 +96,7 @@ public ProxyRequestHandler(
9796 this .httpClient = requireNonNull (httpClient , "httpClient is null" );
9897 this .routingManager = requireNonNull (routingManager , "routingManager is null" );
9998 this .queryHistoryManager = requireNonNull (queryHistoryManager , "queryHistoryManager is null" );
99+ trinoRequestUserProvider = new TrinoRequestUser .TrinoRequestUserProvider (haGatewayConfiguration .getRequestAnalyzerConfig ());
100100 cookiesEnabled = GatewayCookieConfigurationPropertiesProvider .getInstance ().isEnabled ();
101101 asyncTimeout = haGatewayConfiguration .getRouting ().getAsyncTimeout ();
102102 addXForwardedHeaders = haGatewayConfiguration .getRouting ().isAddXForwardedHeaders ();
@@ -173,7 +173,8 @@ private void performRequest(
173173 FluentFuture <ProxyResponse > future = executeHttp (request );
174174
175175 if (statementPaths .stream ().anyMatch (request .getUri ().getPath ()::startsWith ) && request .getMethod ().equals (HttpMethod .POST )) {
176- future = future .transform (response -> recordBackendForQueryId (request , response ), executor );
176+ String username = trinoRequestUserProvider .getInstance (servletRequest ).getUser ().orElse ("Unknown" );
177+ future = future .transform (response -> recordBackendForQueryId (request , response , username ), executor );
177178 if (includeClusterInfoInResponse ) {
178179 cookieBuilder .add (new NewCookie .Builder ("trinoClusterHost" ).value (remoteUri .getHost ()).build ());
179180 }
@@ -250,11 +251,11 @@ private static WebApplicationException badRequest(String message)
250251 .build ());
251252 }
252253
253- private ProxyResponse recordBackendForQueryId (Request request , ProxyResponse response )
254+ private ProxyResponse recordBackendForQueryId (Request request , ProxyResponse response , String username )
254255 {
255256 log .debug ("For Request [%s] got Response [%s]" , request .getUri (), response .body ());
256257
257- QueryHistoryManager .QueryDetail queryDetail = getQueryDetailsFromRequest (request );
258+ QueryHistoryManager .QueryDetail queryDetail = getQueryDetailsFromRequest (request , username );
258259
259260 log .debug ("Extracting proxy destination : [%s] for request : [%s]" , queryDetail .getBackendUrl (), request .getUri ());
260261
@@ -276,12 +277,12 @@ private ProxyResponse recordBackendForQueryId(Request request, ProxyResponse res
276277 return response ;
277278 }
278279
279- public static QueryHistoryManager .QueryDetail getQueryDetailsFromRequest (Request request )
280+ public static QueryHistoryManager .QueryDetail getQueryDetailsFromRequest (Request request , String username )
280281 {
281282 QueryHistoryManager .QueryDetail queryDetail = new QueryHistoryManager .QueryDetail ();
282283 queryDetail .setBackendUrl (getRemoteTarget (request .getUri ()));
283284 queryDetail .setCaptureTime (System .currentTimeMillis ());
284- queryDetail .setUser (getQueryUser ( request . getHeader ( USER_HEADER ), request . getHeader ( AUTHORIZATION )) );
285+ queryDetail .setUser (username );
285286 queryDetail .setSource (request .getHeader (SOURCE_HEADER ));
286287
287288 String queryText = new String (((StaticBodyGenerator ) request .getBodyGenerator ()).getBody (), UTF_8 );
0 commit comments