From ce6a7ded71287e101de88396e110ed2c2a70f407 Mon Sep 17 00:00:00 2001 From: Muhamad Awad Date: Mon, 27 Feb 2023 14:00:45 +0100 Subject: [PATCH] Build a container for peer (#122) This also include changes to add tracker metrics --- .github/workflows/release.yaml | 50 +++++++++++++++++++++++++--------- Cargo.toml | 3 ++ build/peer/Dockerfile | 4 +++ build/{ => relay}/Dockerfile | 0 src/relay/api.rs | 3 ++ src/relay/switch/mod.rs | 13 +++++++++ 6 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 build/peer/Dockerfile rename build/{ => relay}/Dockerfile (100%) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index faeb5c9..ffa6c83 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -8,7 +8,8 @@ name: Create Release env: REGISTRY: ghcr.io - IMAGE_NAME: threefoldtech/rmb-relay + IMAGE_NAME_RELAY: threefoldtech/rmb-relay + IMAGE_NAME_PEER: threefoldtech/rmb-peer jobs: build: @@ -33,7 +34,13 @@ jobs: name: Build release with: command: build - args: --release --target=x86_64-unknown-linux-musl + # remove the tracker feature + # later again, this should be + # only in development + args: | + --release + --target=x86_64-unknown-linux-musl + --features tracker - name: Release uses: softprops/action-gh-release@v1 if: startsWith(github.ref, 'refs/tags/') @@ -41,7 +48,6 @@ jobs: files: | target/x86_64-unknown-linux-musl/release/rmb-relay target/x86_64-unknown-linux-musl/release/rmb-peer - - name: Log in to the Container registry uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 with: @@ -49,22 +55,40 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Extract metadata (tags, labels) for Docker - id: meta + - name: Extract metadata (tags, labels) for relay Docker + id: meta_relay uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 with: - images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_RELAY }} - - name: Copy build files for rmb-relay + - name: Extract metadata (tags, labels) for peer Docker + id: meta_peer + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_PEER }} + + - name: Copy build files for docker run: | - mkdir -p /tmp/build - cp ./build/Dockerfile /tmp/build - cp ./target/x86_64-unknown-linux-musl/release/rmb-relay /tmp/build + mkdir -p /tmp/relay /tmp/peer + + cp ./build/relay/Dockerfile /tmp/relay + cp ./target/x86_64-unknown-linux-musl/release/rmb-relay /tmp/relay + + cp ./build/peer/Dockerfile /tmp/peer + cp ./target/x86_64-unknown-linux-musl/release/rmb-peer /tmp/peer - name: Build and push Docker image for rmb-relay uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc with: - context: /tmp/build + context: /tmp/relay + push: true + tags: ${{ steps.meta_relay.outputs.tags }} + labels: ${{ steps.meta_relay.outputs.labels }} + + - name: Build and push Docker image for rmb-peer + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc + with: + context: /tmp/peer push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} + tags: ${{ steps.meta_peer.outputs.tags }} + labels: ${{ steps.meta_peer.outputs.labels }} diff --git a/Cargo.toml b/Cargo.toml index 3f41961..fbb459a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,3 +69,6 @@ protobuf-codegen = "3.2.0" [dev-dependencies] httpmock = "0.6" + +[features] +tracker = [] diff --git a/build/peer/Dockerfile b/build/peer/Dockerfile new file mode 100644 index 0000000..c3a2d5b --- /dev/null +++ b/build/peer/Dockerfile @@ -0,0 +1,4 @@ +FROM alpine + +COPY rmb-peer /usr/sbin +ENTRYPOINT [ "/usr/sbin/rmb-peer" ] diff --git a/build/Dockerfile b/build/relay/Dockerfile similarity index 100% rename from build/Dockerfile rename to build/relay/Dockerfile diff --git a/src/relay/api.rs b/src/relay/api.rs index f44821c..2b9f8d6 100644 --- a/src/relay/api.rs +++ b/src/relay/api.rs @@ -354,6 +354,9 @@ impl Stream { let envelope = Envelope::parse_from_bytes(&msg).context("failed to load input message")?; + #[cfg(feature = "tracker")] + super::switch::MESSAGE_RX_TWIN.with_label_values(&[&format!("{}", envelope.source.twin)]).inc(); + if !self.metrics.measure(msg.len()).await { log::trace!("twin with stream id {} exceeded its request limits, dropping message", self.id); self.send_error(envelope, "exceeded rate limits, dropping message").await; diff --git a/src/relay/switch/mod.rs b/src/relay/switch/mod.rs index 27f475c..fe85920 100644 --- a/src/relay/switch/mod.rs +++ b/src/relay/switch/mod.rs @@ -29,11 +29,15 @@ use tokio::time::{sleep, Duration}; use prometheus::{IntCounter, IntGaugeVec, Opts, Registry}; +#[cfg(feature = "tracker")] +use prometheus::IntCounterVec; + lazy_static::lazy_static! { static ref CON_PER_WORKER: IntGaugeVec = IntGaugeVec::new( Opts::new("relay_worker_connections", "number of connections handled by this worker"), &["worker"]).unwrap(); + static ref MESSAGE_RX: IntCounter = IntCounter::new("relay_message_rx", "number of messages received by relay").unwrap(); static ref MESSAGE_TX: IntCounter = IntCounter::new("relay_message_tx", "number of messages forwarded by relay").unwrap(); @@ -43,6 +47,13 @@ lazy_static::lazy_static! { static ref MESSAGE_TX_BYTES: IntCounter = IntCounter::new("relay_message_tx_bytes", "size of messages forwarded by relay in bytes").unwrap(); } +#[cfg(feature = "tracker")] +lazy_static::lazy_static! { + pub static ref MESSAGE_RX_TWIN: IntCounterVec = IntCounterVec::new( + Opts::new("relay_message_rx_twin", "number of messages received by relay per twin"), + &["twin"]).unwrap(); + +} pub const DEFAULT_WORKERS: u32 = 100; pub const DEFAULT_USERS: usize = 100_1000; @@ -220,6 +231,8 @@ where opts.registry.register(Box::new(MESSAGE_TX.clone()))?; opts.registry.register(Box::new(MESSAGE_RX_BYTES.clone()))?; opts.registry.register(Box::new(MESSAGE_TX_BYTES.clone()))?; + #[cfg(feature = "tracker")] + opts.registry.register(Box::new(MESSAGE_RX_TWIN.clone()))?; for id in 0..workers { // TODO: while workers are mostly ideal may be it's better in the