feat(metric): add dragonfly-client-metric module (#1362)

* feat(metric): add dragonfly-client-metric module

Signed-off-by: Gaius <gaius.qi@gmail.com>

* chore(deps): update dragonfly-client versions to 1.0.22

Signed-off-by: Gaius <gaius.qi@gmail.com>

---------

Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
Gaius 2025-09-22 18:23:09 +08:00 committed by GitHub
parent 160f51396f
commit c4fb049aac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 120 additions and 54 deletions

34
Cargo.lock generated
View File

@ -986,7 +986,7 @@ dependencies = [
[[package]]
name = "dragonfly-client"
version = "1.0.21"
version = "1.0.22"
dependencies = [
"anyhow",
"bytes",
@ -999,6 +999,7 @@ dependencies = [
"dragonfly-client-backend",
"dragonfly-client-config",
"dragonfly-client-core",
"dragonfly-client-metric",
"dragonfly-client-storage",
"dragonfly-client-util",
"fastrand",
@ -1025,7 +1026,6 @@ dependencies = [
"path-absolutize",
"percent-encoding",
"pprof",
"prometheus",
"prost-wkt-types",
"rcgen",
"reqwest",
@ -1060,7 +1060,7 @@ dependencies = [
[[package]]
name = "dragonfly-client-backend"
version = "1.0.21"
version = "1.0.22"
dependencies = [
"dragonfly-api",
"dragonfly-client-core",
@ -1091,7 +1091,7 @@ dependencies = [
[[package]]
name = "dragonfly-client-config"
version = "1.0.21"
version = "1.0.22"
dependencies = [
"bytesize",
"bytesize-serde",
@ -1121,7 +1121,7 @@ dependencies = [
[[package]]
name = "dragonfly-client-core"
version = "1.0.21"
version = "1.0.22"
dependencies = [
"headers 0.4.1",
"hyper 1.6.0",
@ -1140,7 +1140,7 @@ dependencies = [
[[package]]
name = "dragonfly-client-init"
version = "1.0.21"
version = "1.0.22"
dependencies = [
"anyhow",
"clap",
@ -1155,9 +1155,24 @@ dependencies = [
"url",
]
[[package]]
name = "dragonfly-client-metric"
version = "1.0.22"
dependencies = [
"dragonfly-api",
"dragonfly-client-config",
"dragonfly-client-util",
"fs2",
"lazy_static",
"prometheus",
"tokio",
"tracing",
"warp",
]
[[package]]
name = "dragonfly-client-storage"
version = "1.0.21"
version = "1.0.22"
dependencies = [
"bincode",
"bytes",
@ -1168,6 +1183,7 @@ dependencies = [
"dragonfly-api",
"dragonfly-client-config",
"dragonfly-client-core",
"dragonfly-client-metric",
"dragonfly-client-util",
"fs2",
"leaky-bucket",
@ -1186,7 +1202,7 @@ dependencies = [
[[package]]
name = "dragonfly-client-util"
version = "1.0.21"
version = "1.0.22"
dependencies = [
"base64 0.22.1",
"bytesize",
@ -1597,7 +1613,7 @@ dependencies = [
[[package]]
name = "hdfs"
version = "1.0.21"
version = "1.0.22"
dependencies = [
"dragonfly-client-backend",
"dragonfly-client-core",

View File

@ -9,10 +9,11 @@ members = [
"dragonfly-client-storage",
"dragonfly-client-util",
"dragonfly-client-backend/examples/plugin",
"dragonfly-client-metric",
]
[workspace.package]
version = "1.0.21"
version = "1.0.22"
authors = ["The Dragonfly Developers"]
homepage = "https://d7y.io/"
repository = "https://github.com/dragonflyoss/client.git"
@ -22,13 +23,14 @@ readme = "README.md"
edition = "2021"
[workspace.dependencies]
dragonfly-client = { path = "dragonfly-client", version = "1.0.21" }
dragonfly-client-core = { path = "dragonfly-client-core", version = "1.0.21" }
dragonfly-client-config = { path = "dragonfly-client-config", version = "1.0.21" }
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "1.0.21" }
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "1.0.21" }
dragonfly-client-util = { path = "dragonfly-client-util", version = "1.0.21" }
dragonfly-client-init = { path = "dragonfly-client-init", version = "1.0.21" }
dragonfly-client = { path = "dragonfly-client", version = "1.0.22" }
dragonfly-client-core = { path = "dragonfly-client-core", version = "1.0.22" }
dragonfly-client-config = { path = "dragonfly-client-config", version = "1.0.22" }
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "1.0.22" }
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "1.0.22" }
dragonfly-client-metric = { path = "dragonfly-client-metric", version = "1.0.22" }
dragonfly-client-util = { path = "dragonfly-client-util", version = "1.0.22" }
dragonfly-client-init = { path = "dragonfly-client-init", version = "1.0.22" }
dragonfly-api = "=2.1.68"
thiserror = "2.0"
futures = "0.3.31"

View File

@ -29,6 +29,9 @@ COPY dragonfly-client-backend/src ./dragonfly-client-backend/src
COPY dragonfly-client-backend/examples/plugin/Cargo.toml ./dragonfly-client-backend/examples/plugin/Cargo.toml
COPY dragonfly-client-backend/examples/plugin/src ./dragonfly-client-backend/examples/plugin/src
COPY dragonfly-client-metric/Cargo.toml ./dragonfly-client-metric/Cargo.toml
COPY dragonfly-client-metric/src ./dragonfly-client-backend/src
COPY dragonfly-client-util/Cargo.toml ./dragonfly-client-util/Cargo.toml
COPY dragonfly-client-util/src ./dragonfly-client-util/src

View File

@ -29,6 +29,9 @@ COPY dragonfly-client-backend/src ./dragonfly-client-backend/src
COPY dragonfly-client-backend/examples/plugin/Cargo.toml ./dragonfly-client-backend/examples/plugin/Cargo.toml
COPY dragonfly-client-backend/examples/plugin/src ./dragonfly-client-backend/examples/plugin/src
COPY dragonfly-client-metric/Cargo.toml ./dragonfly-client-metric/Cargo.toml
COPY dragonfly-client-metric/src ./dragonfly-client-backend/src
COPY dragonfly-client-util/Cargo.toml ./dragonfly-client-util/Cargo.toml
COPY dragonfly-client-util/src ./dragonfly-client-util/src

View File

@ -29,6 +29,9 @@ COPY dragonfly-client-backend/src ./dragonfly-client-backend/src
COPY dragonfly-client-backend/examples/plugin/Cargo.toml ./dragonfly-client-backend/examples/plugin/Cargo.toml
COPY dragonfly-client-backend/examples/plugin/src ./dragonfly-client-backend/examples/plugin/src
COPY dragonfly-client-metric/Cargo.toml ./dragonfly-client-metric/Cargo.toml
COPY dragonfly-client-metric/src ./dragonfly-client-backend/src
COPY dragonfly-client-util/Cargo.toml ./dragonfly-client-util/Cargo.toml
COPY dragonfly-client-util/src ./dragonfly-client-util/src

View File

@ -0,0 +1,22 @@
[package]
name = "dragonfly-client-metric"
description = "Metric collection for the dragonfly client"
version.workspace = true
authors.workspace = true
homepage.workspace = true
repository.workspace = true
keywords.workspace = true
license.workspace = true
readme.workspace = true
edition.workspace = true
[dependencies]
dragonfly-api.workspace = true
dragonfly-client-config.workspace = true
dragonfly-client-util.workspace = true
lazy_static.workspace = true
tokio.workspace = true
tracing.workspace = true
warp.workspace = true
fs2.workspace = true
prometheus = { version = "0.13", features = ["process"] }

View File

@ -1,5 +1,5 @@
/*
* Copyright 2023 The Dragonfly Authors
* Copyright 2025 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -12,6 +12,7 @@ edition.workspace = true
[dependencies]
dragonfly-client-core.workspace = true
dragonfly-client-config.workspace = true
dragonfly-client-metric.workspace = true
dragonfly-client-util.workspace = true
dragonfly-api.workspace = true
chrono.workspace = true

View File

@ -18,6 +18,9 @@ use crate::Storage;
use bytes::{Bytes, BytesMut};
use dragonfly_api::common::v2::TrafficType;
use dragonfly_client_core::{Error as ClientError, Result as ClientResult};
use dragonfly_client_metric::{
collect_upload_piece_failure_metrics, collect_upload_piece_started_metrics,
};
use dragonfly_client_util::{id_generator::IDGenerator, shutdown};
use leaky_bucket::RateLimiter;
use std::net::SocketAddr;
@ -157,7 +160,11 @@ impl TCPServerHandler {
Span::current().record("remote_address", remote_address.as_str());
Span::current().record("task_id", task_id);
Span::current().record("piece_id", piece_id.as_str());
// Collect upload piece started metrics.
collect_upload_piece_started_metrics();
info!("start upload piece content");
match self.handle_piece(piece_id.as_str(), task_id).await {
Ok((piece_content, mut content_reader)) => {
let piece_content_bytes: Bytes = piece_content.into();
@ -174,6 +181,9 @@ impl TCPServerHandler {
self.write_stream(&mut content_reader, &mut writer).await?;
}
Err(err) => {
// Collect upload piece failure metrics.
collect_upload_piece_failure_metrics();
let error_response: Bytes =
Vortex::Error(Header::new_error(err.len() as u32), err).into();
self.write_response(error_response, &mut writer).await?;
@ -203,7 +213,11 @@ impl TCPServerHandler {
Span::current().record("remote_address", remote_address.as_str());
Span::current().record("task_id", task_id);
Span::current().record("piece_id", piece_id.as_str());
// Collect upload piece started metrics.
collect_upload_piece_started_metrics();
info!("start upload persistent cache piece content");
match self
.handle_persistent_cache_piece(piece_id.as_str(), task_id)
.await
@ -227,6 +241,9 @@ impl TCPServerHandler {
self.write_stream(&mut content_reader, &mut writer).await?;
}
Err(err) => {
// Collect upload piece failure metrics.
collect_upload_piece_failure_metrics();
let error_response: Bytes =
Vortex::Error(Header::new_error(err.len() as u32), err).into();
self.write_response(error_response, &mut writer).await?;

View File

@ -27,6 +27,7 @@ dragonfly-client-core.workspace = true
dragonfly-client-config.workspace = true
dragonfly-client-storage.workspace = true
dragonfly-client-backend.workspace = true
dragonfly-client-metric.workspace = true
dragonfly-client-util.workspace = true
dragonfly-api.workspace = true
rcgen.workspace = true
@ -75,7 +76,6 @@ opentelemetry_sdk = { version = "0.29.0", default-features = false, features = [
opentelemetry-semantic-conventions = { version = "0.30.0", features = ["semconv_experimental"] }
rolling-file = "0.2.0"
pprof = { version = "0.15", features = ["flamegraph", "protobuf-codec"] }
prometheus = { version = "0.13", features = ["process"] }
tonic-health = "0.12.3"
tower = { version = "0.4.13", features = ["limit", "load-shed", "buffer"] }
indicatif = "0.18.0"

View File

@ -23,13 +23,13 @@ use dragonfly_client::grpc::{
manager::ManagerClient, scheduler::SchedulerClient,
};
use dragonfly_client::health::Health;
use dragonfly_client::metrics::Metrics;
use dragonfly_client::proxy::Proxy;
use dragonfly_client::resource::{persistent_cache_task::PersistentCacheTask, task::Task};
use dragonfly_client::stats::Stats;
use dragonfly_client::tracing::init_tracing;
use dragonfly_client_backend::BackendFactory;
use dragonfly_client_config::{dfdaemon, VersionValueParser};
use dragonfly_client_metric::Metrics;
use dragonfly_client_storage::{server::tcp::TCPServer, Storage};
use dragonfly_client_util::{id_generator::IDGenerator, net::Interface, shutdown};
use leaky_bucket::RateLimiter;

View File

@ -14,15 +14,6 @@
* limitations under the License.
*/
use crate::metrics::{
collect_delete_host_failure_metrics, collect_delete_host_started_metrics,
collect_delete_task_failure_metrics, collect_delete_task_started_metrics,
collect_download_task_failure_metrics, collect_download_task_finished_metrics,
collect_download_task_started_metrics, collect_list_task_entries_failure_metrics,
collect_list_task_entries_started_metrics, collect_stat_task_failure_metrics,
collect_stat_task_started_metrics, collect_upload_task_failure_metrics,
collect_upload_task_finished_metrics, collect_upload_task_started_metrics,
};
use crate::resource::{persistent_cache_task, task};
use dragonfly_api::common::v2::{CacheTask, PersistentCacheTask, Priority, Task, TaskType};
use dragonfly_api::dfdaemon::v2::{
@ -44,6 +35,15 @@ use dragonfly_client_core::{
error::{ErrorType, OrErr},
Error as ClientError, Result as ClientResult,
};
use dragonfly_client_metric::{
collect_delete_host_failure_metrics, collect_delete_host_started_metrics,
collect_delete_task_failure_metrics, collect_delete_task_started_metrics,
collect_download_task_failure_metrics, collect_download_task_finished_metrics,
collect_download_task_started_metrics, collect_list_task_entries_failure_metrics,
collect_list_task_entries_started_metrics, collect_stat_task_failure_metrics,
collect_stat_task_started_metrics, collect_upload_task_failure_metrics,
collect_upload_task_finished_metrics, collect_upload_task_started_metrics,
};
use dragonfly_client_util::{
digest::{verify_file_digest, Digest},
http::{get_range, hashmap_to_headermap, headermap_to_hashmap},

View File

@ -14,14 +14,6 @@
* limitations under the License.
*/
use crate::metrics::{
collect_delete_task_failure_metrics, collect_delete_task_started_metrics,
collect_download_task_failure_metrics, collect_download_task_finished_metrics,
collect_download_task_started_metrics, collect_stat_task_failure_metrics,
collect_stat_task_started_metrics, collect_update_task_failure_metrics,
collect_update_task_started_metrics, collect_upload_piece_failure_metrics,
collect_upload_piece_finished_metrics, collect_upload_piece_started_metrics,
};
use crate::resource::{persistent_cache_task, task};
use dragonfly_api::common::v2::{
CacheTask, Host, Network, PersistentCacheTask, Piece, Priority, Task, TaskType,
@ -46,6 +38,14 @@ use dragonfly_client_core::{
error::{ErrorType, OrErr},
Error as ClientError, Result as ClientResult,
};
use dragonfly_client_metric::{
collect_delete_task_failure_metrics, collect_delete_task_started_metrics,
collect_download_task_failure_metrics, collect_download_task_finished_metrics,
collect_download_task_started_metrics, collect_stat_task_failure_metrics,
collect_stat_task_started_metrics, collect_update_task_failure_metrics,
collect_update_task_started_metrics, collect_upload_piece_failure_metrics,
collect_upload_piece_finished_metrics, collect_upload_piece_started_metrics,
};
use dragonfly_client_util::{
http::{get_range, hashmap_to_headermap, headermap_to_hashmap},
id_generator::TaskIDParameter,

View File

@ -14,11 +14,11 @@
* limitations under the License.
*/
use crate::metrics::{
collect_prefetch_task_failure_metrics, collect_prefetch_task_started_metrics,
};
use dragonfly_api::dfdaemon::v2::DownloadTaskRequest;
use dragonfly_client_core::{Error as ClientError, Result as ClientResult};
use dragonfly_client_metric::{
collect_prefetch_task_failure_metrics, collect_prefetch_task_started_metrics,
};
use std::path::PathBuf;
use std::time::Duration;
use tonic::Request;

View File

@ -19,7 +19,6 @@ pub mod dynconfig;
pub mod gc;
pub mod grpc;
pub mod health;
pub mod metrics;
pub mod proxy;
pub mod resource;
pub mod stats;

View File

@ -15,10 +15,6 @@
*/
use crate::grpc::{dfdaemon_download::DfdaemonDownloadClient, REQUEST_TIMEOUT};
use crate::metrics::{
collect_proxy_request_failure_metrics, collect_proxy_request_started_metrics,
collect_proxy_request_via_dfdaemon_metrics,
};
use crate::resource::{piece::MIN_PIECE_LENGTH, task::Task};
use bytes::Bytes;
use dragonfly_api::common::v2::{Download, TaskType};
@ -29,6 +25,10 @@ use dragonfly_api::errordetails::v2::Backend;
use dragonfly_client_config::dfdaemon::{Config, Rule};
use dragonfly_client_core::error::{ErrorType, OrErr};
use dragonfly_client_core::{Error as ClientError, Result as ClientResult};
use dragonfly_client_metric::{
collect_proxy_request_failure_metrics, collect_proxy_request_started_metrics,
collect_proxy_request_via_dfdaemon_metrics,
};
use dragonfly_client_util::{
http::{hashmap_to_headermap, headermap_to_hashmap},
shutdown,

View File

@ -15,16 +15,16 @@
*/
use super::*;
use crate::metrics::{
collect_backend_request_failure_metrics, collect_backend_request_finished_metrics,
collect_backend_request_started_metrics, collect_download_piece_traffic_metrics,
collect_upload_piece_traffic_metrics,
};
use chrono::Utc;
use dragonfly_api::common::v2::{Hdfs, ObjectStorage, Range, TrafficType};
use dragonfly_client_backend::{BackendFactory, GetRequest};
use dragonfly_client_config::dfdaemon::Config;
use dragonfly_client_core::{error::BackendError, Error, Result};
use dragonfly_client_metric::{
collect_backend_request_failure_metrics, collect_backend_request_finished_metrics,
collect_backend_request_started_metrics, collect_download_piece_traffic_metrics,
collect_upload_piece_traffic_metrics,
};
use dragonfly_client_storage::{metadata, Storage};
use dragonfly_client_util::id_generator::IDGenerator;
use leaky_bucket::RateLimiter;

View File

@ -15,10 +15,6 @@
*/
use crate::grpc::{scheduler::SchedulerClient, REQUEST_TIMEOUT};
use crate::metrics::{
collect_backend_request_failure_metrics, collect_backend_request_finished_metrics,
collect_backend_request_started_metrics,
};
use dragonfly_api::common::v2::{
Download, Hdfs, ObjectStorage, Peer, Piece, SizeScope, Task as CommonTask, TaskType,
TrafficType,
@ -43,6 +39,10 @@ use dragonfly_client_core::{
error::{BackendError, DownloadFromParentFailed, ErrorType, OrErr},
Error, Result as ClientResult,
};
use dragonfly_client_metric::{
collect_backend_request_failure_metrics, collect_backend_request_finished_metrics,
collect_backend_request_started_metrics,
};
use dragonfly_client_storage::{metadata, Storage};
use dragonfly_client_util::{
http::{hashmap_to_headermap, headermap_to_hashmap},