Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit a3b0bd6

Browse files
committed
Add prometheus support for tracing
1 parent 1d483db commit a3b0bd6

File tree

6 files changed

+64
-18
lines changed

6 files changed

+64
-18
lines changed

Cargo.lock

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

bin/node/cli/src/command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ where
7979
// Setup tracing.
8080
if let Some(tracing_targets) = cli_args.import_params.tracing_targets.as_ref() {
8181
let subscriber = sc_tracing::ProfilingSubscriber::new(
82-
cli_args.import_params.tracing_receiver.into(), tracing_targets
82+
cli_args.import_params.tracing_receiver.into(), tracing_targets, None
8383
);
8484
if let Err(e) = tracing::subscriber::set_global_default(subscriber) {
8585
return Err(

client/cli/src/arg_enums.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ arg_enum! {
6262
pub enum TracingReceiver {
6363
Log,
6464
Telemetry,
65+
Prometheus
6566
}
6667
}
6768

@@ -70,6 +71,7 @@ impl Into<sc_tracing::TracingReceiver> for TracingReceiver {
7071
match self {
7172
TracingReceiver::Log => sc_tracing::TracingReceiver::Log,
7273
TracingReceiver::Telemetry => sc_tracing::TracingReceiver::Telemetry,
74+
TracingReceiver::Prometheus => sc_tracing::TracingReceiver::Prometheus,
7375
}
7476
}
7577
}

client/service/src/builder.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,24 +1032,25 @@ ServiceBuilder<
10321032
}
10331033

10341034
// Prometheus metrics
1035-
let metrics = if let Some((registry, port)) = prometheus_registry_and_port {
1036-
let metrics = ServiceMetrics::register(&registry)?;
1035+
let metrics = match prometheus_registry_and_port.clone() {
1036+
Some((registry, port)) => {
1037+
let metrics = ServiceMetrics::register(&registry)?;
10371038

1038-
metrics.node_roles.set(u64::from(config.roles.bits()));
1039+
metrics.node_roles.set(u64::from(config.roles.bits()));
10391040

1040-
let future = select(
1041-
prometheus_exporter::init_prometheus(port, registry).boxed(),
1042-
exit.clone()
1043-
).map(drop);
1041+
let future = select(
1042+
prometheus_exporter::init_prometheus(port, registry).boxed(),
1043+
exit.clone()
1044+
).map(drop);
10441045

1045-
let _ = to_spawn_tx.unbounded_send((
1046-
Box::pin(future),
1047-
From::from("prometheus-endpoint")
1048-
));
1046+
let _ = to_spawn_tx.unbounded_send((
1047+
Box::pin(future),
1048+
From::from("prometheus-endpoint")
1049+
));
10491050

1050-
Some(metrics)
1051-
} else {
1052-
None
1051+
Some(metrics)
1052+
}
1053+
None => None,
10531054
};
10541055
// Periodically notify the telemetry.
10551056
let transaction_pool_ = transaction_pool.clone();
@@ -1271,7 +1272,11 @@ ServiceBuilder<
12711272
// Instrumentation
12721273
if let Some(tracing_targets) = config.tracing_targets.as_ref() {
12731274
let subscriber = sc_tracing::ProfilingSubscriber::new(
1274-
config.tracing_receiver, tracing_targets
1275+
config.tracing_receiver, tracing_targets,
1276+
match prometheus_registry_and_port {
1277+
Some((reg, _)) => Some(sc_tracing::Metrics::register(&reg)?),
1278+
None => None
1279+
}
12751280
);
12761281
match tracing::subscriber::set_global_default(subscriber) {
12771282
Ok(_) => (),

client/tracing/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ slog = { version = "2.5.2", features = ["nested-values"] }
1717
tracing-core = "0.1.7"
1818

1919
sc-telemetry = { version = "2.0.0-dev", path = "../telemetry" }
20+
prometheus-exporter = { version = "0.8.0-dev", path = "../../utils/prometheus" }
2021

2122
[dev-dependencies]
2223
tracing = "0.1.10"

client/tracing/src/lib.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ use tracing_core::{
5353
subscriber::Subscriber
5454
};
5555

56-
use sc_telemetry::{telemetry, SUBSTRATE_INFO};
56+
use sc_telemetry::{telemetry, SUBSTRATE_INFO};
57+
use prometheus_exporter::{register, U64, Registry, PrometheusError, Opts, GaugeVec};
5758

5859
/// Used to configure how to receive the metrics
5960
#[derive(Debug, Clone)]
@@ -62,6 +63,8 @@ pub enum TracingReceiver {
6263
Log,
6364
/// Output to telemetry
6465
Telemetry,
66+
/// Output to prometheus
67+
Prometheus
6568
}
6669

6770
impl Default for TracingReceiver {
@@ -149,19 +152,21 @@ pub struct ProfilingSubscriber {
149152
targets: Vec<(String, Level)>,
150153
receiver: TracingReceiver,
151154
span_data: Mutex<HashMap<u64, SpanDatum>>,
155+
prometheus_metrics: Option<Metrics>
152156
}
153157

154158
impl ProfilingSubscriber {
155159
/// Takes a `Receiver` and a comma separated list of targets,
156160
/// either with a level: "pallet=trace"
157161
/// or without: "pallet".
158-
pub fn new(receiver: TracingReceiver, targets: &str) -> Self {
162+
pub fn new(receiver: TracingReceiver, targets: &str, prometheus_metrics: Option<Metrics>) -> Self {
159163
let targets: Vec<_> = targets.split(',').map(|s| parse_target(s)).collect();
160164
ProfilingSubscriber {
161165
next_id: AtomicU64::new(1),
162166
targets,
163167
receiver,
164168
span_data: Mutex::new(HashMap::new()),
169+
prometheus_metrics,
165170
}
166171
}
167172
}
@@ -252,6 +257,9 @@ impl ProfilingSubscriber {
252257
match self.receiver {
253258
TracingReceiver::Log => print_log(span_datum),
254259
TracingReceiver::Telemetry => send_telemetry(span_datum),
260+
TracingReceiver::Prometheus => send_prometheus(
261+
span_datum, self.prometheus_metrics.as_ref().expect("prometheus_metrics needs to be passed to trace to prometheus")
262+
)
255263
}
256264
}
257265
}
@@ -287,3 +295,32 @@ fn send_telemetry(span_datum: SpanDatum) {
287295
);
288296
}
289297

298+
#[derive(Clone)]
299+
pub struct Metrics {
300+
profiling: GaugeVec<U64>
301+
}
302+
303+
impl fmt::Debug for Metrics {
304+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
305+
write!(f, "Metrics")
306+
}
307+
}
308+
309+
impl Metrics {
310+
pub fn register(registry: &Registry) -> Result<Self, PrometheusError> {
311+
Ok(Self {
312+
profiling: register(GaugeVec::new(
313+
Opts::new("profiling", "Profiling from tracing"),
314+
&["name", "target", "line", "values"]
315+
)?, registry)?,
316+
})
317+
}
318+
}
319+
320+
fn send_prometheus(span_datum: SpanDatum, metrics: &Metrics) {
321+
metrics.profiling
322+
.with_label_values(&[
323+
span_datum.name, span_datum.target, &span_datum.line.to_string(), &span_datum.values.to_string()
324+
])
325+
.set(span_datum.overall_time.as_nanos() as u64);
326+
}

0 commit comments

Comments
 (0)