Skip to content

Commit 677b0dc

Browse files
authored
feat: pass user agent on proxy mode (databendlabs#23)
* feat: pass user agent on proxy mode * chore: bump version v0.3.3
1 parent 2d5ff08 commit 677b0dc

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "databend-loki-adapter"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
edition = "2024"
55
description = "Loki-compatible HTTP adapter that runs LogQL queries against Databend"
66
license = "Apache-2.0"

src/app/state.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use axum::http::HeaderMap;
1616
use databend_driver::Client;
1717
use log::{info, warn};
18+
use once_cell::sync::Lazy;
1819
use serde::Deserialize;
1920

2021
use crate::{
@@ -32,6 +33,12 @@ pub(crate) const MAX_LIMIT: u64 = 5_000;
3233
pub(crate) const DEFAULT_LOOKBACK_NS: i64 = 5 * 60 * 1_000_000_000;
3334
pub const PROXY_DSN_HEADER: &str = "x-databend-dsn";
3435
pub 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)]
3744
pub 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

Comments
 (0)