Skip to content

Commit

Permalink
chore: add readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
MxmUrw committed Feb 14, 2025
1 parent 69b4694 commit c6022f6
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 80 deletions.
12 changes: 12 additions & 0 deletions api/bin/chainflip-elections-tracker/src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

# Chainflip Elections Tracker

This binary runs alongside a chainflip node, and tracks the state of elections.
It computes which parts of the state changed and pushes this information in form
of OTLP traces to a given OTLP endpoint.

## Configuration
There are two configurable environment variables:
- `CF_RPC_NODE`: Url of the chainflip node to connect to for the block stream. Default: `http://localhost:9944`.
- `OTLP_BACKEND`: Url of the OTLP backend to connect to for pushing traces to. Default: `http://localhost:4317`.

15 changes: 5 additions & 10 deletions api/bin/chainflip-elections-tracker/src/elections.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
use std::{
collections::BTreeMap,
fmt::Display,
};
use std::{collections::BTreeMap, fmt::Display};

use bitvec::prelude::*;
use codec::{Decode, Encode};
use pallet_cf_elections::{
ElectionIdentifierOf, ElectoralSystemTypes, IndividualComponentOf,
UniqueMonotonicIdentifier,
ElectionIdentifierOf, ElectoralSystemTypes, IndividualComponentOf, UniqueMonotonicIdentifier,
electoral_system::BitmapComponentOf,
};


#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode)]
pub struct ElectionData<ES: ElectoralSystemTypes> {
pub height: u32,
Expand Down Expand Up @@ -79,7 +74,6 @@ impl Display for Key {
}
}


pub fn cloned_vec<'a, XS: IntoIterator<Item = &'a X>, X>(xs: XS) -> Vec<X>
where
X: Clone + 'a,
Expand Down Expand Up @@ -168,8 +162,9 @@ where

// properties
let key3 = Category(extra.clone(), Properties);
trace.insert(cloned_vec([&key0, &key1, &key2, &key3]),
end.with_attribute("Properties".into(), format!("{properties:#?}"))
trace.insert(
cloned_vec([&key0, &key1, &key2, &key3]),
end.with_attribute("Properties".into(), format!("{properties:#?}")),
);

// no votes
Expand Down
139 changes: 70 additions & 69 deletions api/bin/chainflip-elections-tracker/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
//! The chainflip elections tracker.
#![feature(btree_extract_if)]

pub mod elections;
pub mod trace;

use std::collections::BTreeMap;

use chainflip_engine::state_chain_observer::client::StateChainClient;
use elections::{Key, TraceInit, make_traces, ElectionData};
use opentelemetry::trace::{
Span, TraceContextExt as _, Tracer, TracerProvider as _,
};
use state_chain_runtime::chainflip::solana_elections::SolanaElectoralSystemRunner;
use cf_utilities::task_scope::{self};
use chainflip_engine::state_chain_observer::client::storage_api::StorageApi;
use chainflip_engine::state_chain_observer::client::{StateChainClient, storage_api::StorageApi};
use elections::{ElectionData, Key, TraceInit, make_traces};
use futures::StreamExt;
use futures_util::FutureExt;
use opentelemetry::{Context, KeyValue, global};
use opentelemetry::{
Context, KeyValue, global,
trace::{Span, TraceContextExt as _, Tracer, TracerProvider as _},
};
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::{
Resource,
trace::{RandomIdGenerator, TracerProvider},
};
use opentelemetry_otlp::WithExportConfig;
use pallet_cf_elections::{
UniqueMonotonicIdentifier,
electoral_systems::composite::tuple_6_impls::*,
UniqueMonotonicIdentifier, electoral_systems::composite::tuple_6_impls::*,
};
use state_chain_runtime::{
Runtime, SolanaInstance, chainflip::solana_elections::SolanaElectoralSystemRunner,
};
use state_chain_runtime::{Runtime, SolanaInstance};
use std::env;
use trace::{NodeDiff, StateTree, diff, get_key_name, map_with_parent};

#[tokio::main(flavor = "multi_thread", worker_threads = 3)]
async fn main() {


// get env vars
let rpc_url = env::var("CF_RPC_NODE").expect("CF_RPC_NODE required");
let opentelemetry_backend_url = env::var("OPTL_BACKEND_URL")
.unwrap_or("http://localhost:4317".into());
let rpc_url = env::var("CF_RPC_NODE").unwrap_or("http://localhost:9944".into());
let opentelemetry_backend_url =
env::var("OTLP_BACKEND").unwrap_or("http://localhost:4317".into());

// setup opentelemetry tracer
let tracer_provider = opentelemetry_sdk::trace::TracerProvider::builder()
.with_batch_exporter(
opentelemetry_otlp::SpanExporter::builder()
.with_tonic()
.with_endpoint(&opentelemetry_backend_url)
.with_endpoint(&opentelemetry_backend_url)
.build()
.unwrap(),
opentelemetry_sdk::runtime::Tokio,
Expand All @@ -57,9 +56,11 @@ async fn main() {
observe_elections(tracer, tracer_provider, rpc_url).await;
}


async fn observe_elections<T: Tracer + Send>(tracer: T, tracer_provider: TracerProvider, rpc_url: String)
where
async fn observe_elections<T: Tracer + Send>(
tracer: T,
tracer_provider: TracerProvider,
rpc_url: String,
) where
T::Span: Span + Send + Sync + 'static,
{
task_scope::task_scope(|scope| async move {
Expand Down Expand Up @@ -161,53 +162,53 @@ fn push_traces<T: Tracer + Send>(
where
T::Span: Span + Send + Sync + 'static,
{
let traces =
map_with_parent(diff(current, new), |k, p: Option<&Option<Context>>, d: NodeDiff<Context, TraceInit>| {
match d {
trace::NodeDiff::Left(context) => {
println!("closing trace {k:?}");
context.span().end();
None
},
trace::NodeDiff::Right(TraceInit { end_immediately, attributes: values }) => {
let context = if let Some(Some(context)) = p {
let key = get_key_name(k);

let mut span = tracer.start_with_context(key, &context);
for (key, value) in values {
span.set_attribute(KeyValue::new(key, value));
}
let context = context.with_span(span);

println!("open trace {k:?}");

context
} else {
let key = get_key_name(k);

let context =
Context::new().with_value(KeyValue::new("key", format!("{key:?}")));
let mut span = tracer.start_with_context(key, &context);
for (key, value) in values {
span.set_attribute(KeyValue::new(key, value));
}
let context = context.with_span(span);
println!("open trace {k:?} [NO PARENT]");
context
};
if end_immediately {
context.span().end();
let traces = map_with_parent(
diff(current, new),
|k, p: Option<&Option<Context>>, d: NodeDiff<Context, TraceInit>| match d {
trace::NodeDiff::Left(context) => {
println!("closing trace {k:?}");
context.span().end();
None
},
trace::NodeDiff::Right(TraceInit { end_immediately, attributes: values }) => {
let context = if let Some(Some(context)) = p {
let key = get_key_name(k);

let mut span = tracer.start_with_context(key, &context);
for (key, value) in values {
span.set_attribute(KeyValue::new(key, value));
}
Some(context)
},
trace::NodeDiff::Both(x, _) => Some(x),
}
})
.into_iter()
.filter_map(|(k, v)| match v {
Some(v) => Some((k, v)),
None => None,
})
.collect();
let context = context.with_span(span);

println!("open trace {k:?}");

context
} else {
let key = get_key_name(k);

let context =
Context::new().with_value(KeyValue::new("key", format!("{key:?}")));
let mut span = tracer.start_with_context(key, &context);
for (key, value) in values {
span.set_attribute(KeyValue::new(key, value));
}
let context = context.with_span(span);
println!("open trace {k:?} [NO PARENT]");
context
};
if end_immediately {
context.span().end();
}
Some(context)
},
trace::NodeDiff::Both(x, _) => Some(x),
},
)
.into_iter()
.filter_map(|(k, v)| match v {
Some(v) => Some((k, v)),
None => None,
})
.collect();
traces
}
}
1 change: 0 additions & 1 deletion api/bin/chainflip-elections-tracker/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ pub fn get_key_name<'a, K: std::fmt::Display>(key: &'a Vec<K>) -> String {
key.last().map(|x| format!("{x}")).unwrap_or("root".into())
}


fn zip_with<K: Ord, V, W, X>(
x: BTreeMap<K, V>,
mut y: BTreeMap<K, W>,
Expand Down

0 comments on commit c6022f6

Please sign in to comment.