Skip to content

Commit 8b5ac3c

Browse files
authored
feat(query): add user agent and metrics for jwks request (#18535)
* feat(query): add user agent for jwks request * z * z * z * z * z * z * z * z * z * z * z * z * z
1 parent dbd6f53 commit 8b5ac3c

File tree

14 files changed

+160
-40
lines changed

14 files changed

+160
-40
lines changed

.typos.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"Tke" = "Tke"
1616
"typ" = "typ"
1717
"dows" = "dows"
18+
# created_ons & updated_ons
19+
"ons" = "ons"
1820

1921
[files]
2022
extend-exclude = [

Cargo.lock

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

src/common/metrics/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod metrics;
2222

2323
pub type VecLabels = Vec<(&'static str, String)>;
2424

25+
pub use crate::metrics::auth;
2526
pub use crate::metrics::cache;
2627
pub use crate::metrics::cluster;
2728
pub use crate::metrics::external_server;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2021 Datafuse Labs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::sync::LazyLock;
16+
use std::time::Duration;
17+
18+
use databend_common_base::runtime::metrics::register_counter_family;
19+
use databend_common_base::runtime::metrics::register_histogram_family_in_milliseconds;
20+
use databend_common_base::runtime::metrics::FamilyCounter;
21+
use databend_common_base::runtime::metrics::FamilyHistogram;
22+
23+
use crate::VecLabels;
24+
25+
static AUTH_JWKS_REQUESTS_COUNT: LazyLock<FamilyCounter<VecLabels>> =
26+
LazyLock::new(|| register_counter_family("auth_jwks_requests_count"));
27+
static AUTH_JWKS_REFRESH_DURATION: LazyLock<FamilyHistogram<VecLabels>> =
28+
LazyLock::new(|| register_histogram_family_in_milliseconds("auth_jwks_refresh_duration_ms"));
29+
30+
pub fn metrics_incr_auth_jwks_requests_count(url: String, reason: String, status: u16) {
31+
let labels = vec![
32+
("url", url),
33+
("reason", reason),
34+
("status", status.to_string()),
35+
];
36+
AUTH_JWKS_REQUESTS_COUNT.get_or_create(&labels).inc();
37+
}
38+
39+
pub fn metrics_observe_auth_jwks_refresh_duration(url: String, duration: Duration) {
40+
let labels = vec![("url", url)];
41+
AUTH_JWKS_REFRESH_DURATION
42+
.get_or_create(&labels)
43+
.observe(duration.as_millis() as f64);
44+
}

src/common/metrics/src/metrics/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
pub mod auth;
1516
pub mod cache;
1617
pub mod cluster;
1718
pub mod external_server;

src/query/config/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,7 @@ pub struct QueryConfig {
17781778
pub jwt_key_file: String,
17791779

17801780
/// Interval in seconds to refresh jwks
1781-
#[clap(long, value_name = "VALUE", default_value = "600")]
1781+
#[clap(long, value_name = "VALUE", default_value = "86400")]
17821782
pub jwks_refresh_interval: u64,
17831783

17841784
/// Timeout in seconds to refresh jwks

src/query/config/src/inner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl Default for QueryConfig {
305305
max_storage_io_requests: None,
306306
jwt_key_file: "".to_string(),
307307
jwt_key_files: Vec::new(),
308-
jwks_refresh_interval: 600,
308+
jwks_refresh_interval: 86400,
309309
jwks_refresh_timeout: 10,
310310
default_storage_format: "auto".to_string(),
311311
default_compression: "auto".to_string(),

src/query/config/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ pub use builtin::*;
3838
pub use config::CacheStorageTypeConfig;
3939
pub use config::Commands;
4040
pub use config::Config;
41-
pub use config::QueryConfig;
4241
pub use config::StorageConfig;
4342
pub use config::StorageNetworkConfig;
4443
pub use global::GlobalConfig;
@@ -50,5 +49,6 @@ pub use inner::DiskCacheConfig as DiskCacheInnerConfig;
5049
pub use inner::DiskCacheKeyReloadPolicy;
5150
pub use inner::InnerConfig;
5251
pub use inner::MetaConfig;
52+
pub use inner::QueryConfig;
5353
pub use inner::SpillConfig;
5454
pub use inner::ThriftProtocol;

src/query/service/src/auth.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,7 @@ impl AuthMgr {
8383

8484
fn create(cfg: &InnerConfig) -> Arc<AuthMgr> {
8585
Arc::new(AuthMgr {
86-
jwt_auth: JwtAuthenticator::create(
87-
cfg.query.jwt_key_file.clone(),
88-
cfg.query.jwt_key_files.clone(),
89-
cfg.query.jwks_refresh_interval,
90-
cfg.query.jwks_refresh_timeout,
91-
),
86+
jwt_auth: JwtAuthenticator::create(&cfg.query),
9287
})
9388
}
9489

src/query/service/tests/it/storages/testdata/configs_table_basic.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ DB.Table: 'system'.'configs', Table: configs-table_id:1, ver:0, Engine: SystemCo
182182
| 'query' | 'http_session_timeout_secs' | '14400' | '' |
183183
| 'query' | 'internal_enable_sandbox_tenant' | 'false' | '' |
184184
| 'query' | 'internal_merge_on_read_mutation' | 'false' | '' |
185-
| 'query' | 'jwks_refresh_interval' | '600' | '' |
185+
| 'query' | 'jwks_refresh_interval' | '86400' | '' |
186186
| 'query' | 'jwks_refresh_timeout' | '10' | '' |
187187
| 'query' | 'jwt_key_file' | '' | '' |
188188
| 'query' | 'jwt_key_files' | '' | '' |

0 commit comments

Comments
 (0)