1515use axum:: http:: HeaderMap ;
1616use databend_driver:: Client ;
1717use log:: { info, warn} ;
18+ use once_cell:: sync:: Lazy ;
1819use serde:: Deserialize ;
1920
2021use crate :: {
@@ -32,6 +33,12 @@ pub(crate) const MAX_LIMIT: u64 = 5_000;
3233pub ( crate ) const DEFAULT_LOOKBACK_NS : i64 = 5 * 60 * 1_000_000_000 ;
3334pub const PROXY_DSN_HEADER : & str = "x-databend-dsn" ;
3435pub const PROXY_SCHEMA_HEADER : & str = "x-databend-schema" ;
36+ pub const PROXY_USER_AGENT_HEADER : & str = "user-agent" ;
37+
38+ static VERSION : Lazy < String > = Lazy :: new ( || {
39+ let version = option_env ! ( "CARGO_PKG_VERSION" ) . unwrap_or ( "unknown" ) ;
40+ version. to_string ( )
41+ } ) ;
3542
3643#[ derive( Clone ) ]
3744pub struct AppState {
@@ -57,7 +64,8 @@ impl AppState {
5764 info ! ( "resolving table reference for `{table}`" ) ;
5865 let table = resolve_table_ref ( & dsn, & table) ?;
5966 info ! ( "table resolved to {}" , table. fq_name( ) ) ;
60- let client = Client :: new ( dsn. clone ( ) ) ;
67+ let ua = format ! ( "databend-loki-adapter/{}" , VERSION . as_str( ) ) ;
68+ let client = Client :: new ( dsn. clone ( ) ) . with_name ( ua) ;
6169 verify_connection ( & client) . await ?;
6270 info ! (
6371 "loading {} schema metadata for {}" ,
@@ -145,6 +153,7 @@ impl ProxyState {
145153 fn resolve ( & self , headers : & HeaderMap ) -> Result < RequestContext , AppError > {
146154 let dsn_header = parse_header ( headers, PROXY_DSN_HEADER ) ?;
147155 let schema_header = parse_header ( headers, PROXY_SCHEMA_HEADER ) ?;
156+ let ua_header = parse_header ( headers, PROXY_USER_AGENT_HEADER ) ?;
148157 let schema_payload: ProxySchemaPayload =
149158 serde_json:: from_str ( schema_header) . map_err ( |err| {
150159 AppError :: BadRequest ( format ! (
@@ -159,8 +168,13 @@ impl ProxyState {
159168 let table = resolve_table_ref ( dsn_header, schema_payload. table . trim ( ) ) ?;
160169 let definition = schema_payload. into_definition ( ) ?;
161170 let schema = schema_from_definition ( definition) ;
171+ let ua = if ua_header. is_empty ( ) {
172+ format ! ( "databend-loki-adapter/{}" , VERSION . as_str( ) )
173+ } else {
174+ ua_header. to_string ( )
175+ } ;
162176 Ok ( RequestContext {
163- client : Client :: new ( dsn_header. to_string ( ) ) ,
177+ client : Client :: new ( dsn_header. to_string ( ) ) . with_name ( ua ) ,
164178 table,
165179 schema,
166180 } )
0 commit comments