Skip to content

Commit

Permalink
Merge branch 'main' into feat/addClientAndFrontendTokenToOffline
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswk authored Sep 19, 2024
2 parents 0a01b15 + b124a63 commit 208e9f0
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*
!target/aarch64-unknown-linux-gnu/release/unleash-edge
!target/x86_64-unknown-linux-gnu/release/unleash-edge
!target/x86_64-unknown-linux-gnu/release/unleash-edge
!server
!Cargo.toml
!Cargo.lock
119 changes: 119 additions & 0 deletions .github/workflows/build-docker-with-chef.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
name: Build and Push Docker Image (Chef)

on:
push:
branches:
- main
paths:
- "**/*.rs"
- "**/*.toml"
- "**/*.lock"
- ".github/workflows/build-docker-with-chef.yaml"
- "Dockerfile.chef"
workflow_dispatch:

env:
REGISTRY_NAME: unleashorg/unleash-edge

permissions:
id-token: write
contents: read
packages: write
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
platform:
- linux/amd64
- linux/arm64

steps:
- name: Checkout
uses: actions/checkout@v4
- name: Prepare
run: |
platform=${{ matrix.platform }}
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
- name: Docker meta configuration (set image and tag)
uses: docker/metadata-action@v5
id: meta
with:
images: ${{ env.REGISTRY_NAME }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push by digest
id: build
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile.chef
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha,scope=${{ github.repository }}-${{ github.ref_name }}-${{ matrix.platform }}
cache-to: type=gha,scope=${{ github.repository }}-${{ github.ref_name }}-${{ matrix.platform }},mode=max
outputs: type=image,name=${{ env.REGISTRY_NAME }},push-by-digest=true,name-canonical=true,push=true
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v4
with:
name: digests-${{ env.PLATFORM_PAIR }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1

merge:
runs-on: ubuntu-latest
needs:
- build
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
path: /tmp/digests
pattern: digests-*
merge-multiple: true

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY_NAME }}
tags: |
type=raw,value=chef
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY_NAME }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.REGISTRY_NAME }}:${{ steps.meta.outputs.version }}
127 changes: 127 additions & 0 deletions Dockerfile.chef
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
FROM --platform=linux/amd64 lukemathwalker/cargo-chef:latest-rust-latest AS amd64-chef
FROM --platform=linux/arm64 lukemathwalker/cargo-chef:latest-rust-latest AS arm64-chef

# Base image for the build stage - this is a multi-stage build that uses cross-compilation (thanks to --platform switch)
FROM --platform=$BUILDPLATFORM lukemathwalker/cargo-chef:latest-rust-latest AS chef
WORKDIR /app

# Planner stage
FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

# Builder stage
FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json

ARG TARGETPLATFORM
ARG TARGETARCH

# Copy runtime dependencies for specific target platform/architecture
# ARM specific folders
WORKDIR /all-files/linux/arm64/lib/aarch64-linux-gnu

# AMD64 specific folders
WORKDIR /all-files/linux/amd64/lib/x86_64-linux-gnu
WORKDIR /all-files/linux/amd64/lib64

# Common folders
WORKDIR /all-files/${TARGETPLATFORM}/etc/ssl/certs
WORKDIR /all-files/${TARGETPLATFORM}/app

# ARM64
COPY --from=arm64-chef \
/lib/aarch64-linux-gnu/libssl.so.3 \
/lib/aarch64-linux-gnu/libcrypto.so.3 \
/lib/aarch64-linux-gnu/libgcc_s.so.1 \
/lib/aarch64-linux-gnu/libm.so.6 \
/lib/aarch64-linux-gnu/libc.so.6 \
/lib/aarch64-linux-gnu/libz.so.1 \
/all-files/linux/arm64/lib/aarch64-linux-gnu/

COPY --from=arm64-chef \
/lib/ld-linux-aarch64.so.1 \
/all-files/linux/arm64/lib

# AMD64
COPY --from=amd64-chef \
/lib/x86_64-linux-gnu/libssl.so.3 \
/lib/x86_64-linux-gnu/libcrypto.so.3 \
/lib/x86_64-linux-gnu/libgcc_s.so.1 \
/lib/x86_64-linux-gnu/libm.so.6 \
/lib/x86_64-linux-gnu/libc.so.6 \
/lib/x86_64-linux-gnu/libz.so.1 \
/all-files/linux/amd64/lib/x86_64-linux-gnu/

COPY --from=amd64-chef \
/lib64/ld-linux-x86-64.so.2 \
/all-files/linux/amd64/lib64/

# Common files - certs
COPY --from=amd64-chef \
/etc/ssl/certs/ca-certificates.crt \
/all-files/linux/amd64/etc/ssl/certs/
COPY --from=arm64-chef \
/etc/ssl/certs/ca-certificates.crt \
/all-files/linux/arm64/etc/ssl/certs/

WORKDIR /app

# Install dependencies for cross-compilation and protobuf
RUN dpkg --add-architecture arm64 \
&& apt-get update \
&& apt-get install -y \
protobuf-compiler \
g++-aarch64-linux-gnu \
libc6-dev-arm64-cross \
libssl-dev:arm64 \
libzip-dev:arm64 \
ca-certificates \
&& rustup target add aarch64-unknown-linux-gnu \
&& rustup toolchain install stable-aarch64-unknown-linux-gnu \
&& rm -rf /var/lib/apt/lists/*

# Build dependencies - this is the caching Docker layer!
RUN case ${TARGETARCH} in \
arm64) PKG_CONFIG_SYSROOT_DIR=/ CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc cargo chef cook --target=aarch64-unknown-linux-gnu --release --recipe-path recipe.json ;; \
amd64) cargo chef cook --release --recipe-path recipe.json ;; \
*) exit 1 ;; \
esac

# Copy the source code
COPY . /app

# Build application - this is the caching Docker layer!
RUN case ${TARGETARCH} in \
arm64) PKG_CONFIG_SYSROOT_DIR=/ CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc cargo build --target=aarch64-unknown-linux-gnu --release ;; \
amd64) cargo build --release ;; \
*) exit 1 ;; \
esac

# Copy all the dependencies to a separate folder
RUN set -ex; \
# Determine target (source folder for the binary and env files)
case ${TARGETARCH} in \
arm64) target='/app/target/aarch64-unknown-linux-gnu/release';; \
amd64) target='/app/target/release';; \
*) exit 1 ;; \
esac; \
# Copy files from the target folder to app folder
cp $target/unleash-edge /all-files/${TARGETPLATFORM}/app

# # Create a single layer image
FROM scratch AS runtime

# Make build arguments available in the runtime stage
ARG TARGETPLATFORM
ARG TARGETARCH

WORKDIR /app

# Copy the binary and the environment files from the pre-runtime stage as a single layer
COPY --from=builder /all-files/${TARGETPLATFORM} /

# Expose the port that the application listens on.
EXPOSE 3063

ENTRYPOINT ["/app/unleash-edge"]
2 changes: 1 addition & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ async fn clean_shutdown(
])
.await;
if res.iter().all(|save| save.is_ok()) {
tracing::info!("Successfully persisted data");
tracing::info!("Successfully persisted data to storage backend");
} else {
res.iter()
.filter(|save| save.is_err())
Expand Down
14 changes: 13 additions & 1 deletion server/src/metrics/client_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use chrono::{DateTime, Utc};
use dashmap::DashMap;
use iter_tools::Itertools;
use lazy_static::lazy_static;
use prometheus::{register_histogram, Histogram};
use prometheus::{register_histogram, register_int_counter_vec, Histogram, IntCounterVec};
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
Expand All @@ -25,6 +25,12 @@ lazy_static! {
vec![1000.0, 10000.0, 20000.0, 50000.0, 75000.0, 100000.0, 250000.0, 500000.0, 1000000.0]
)
.unwrap();
pub static ref FEATURE_TOGGLE_USAGE_TOTAL: IntCounterVec = register_int_counter_vec!(
"feature_toggle_usage_total",
"Number of times a feature flag has been used",
&["appName", "toggle", "active"]
)
.unwrap();
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
Expand Down Expand Up @@ -329,6 +335,12 @@ impl MetricsCache {
pub fn sink_metrics(&self, metrics: &[ClientMetricsEnv]) {
debug!("Sinking {} metrics", metrics.len());
for metric in metrics.iter() {
FEATURE_TOGGLE_USAGE_TOTAL
.with_label_values(&[&metric.app_name, &metric.feature_name, "true"])
.inc_by(metric.yes as u64);
FEATURE_TOGGLE_USAGE_TOTAL
.with_label_values(&[&metric.app_name, &metric.feature_name, "false"])
.inc_by(metric.no as u64);
self.metrics
.entry(MetricsKey {
app_name: metric.app_name.clone(),
Expand Down
5 changes: 5 additions & 0 deletions server/src/prom_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ fn register_custom_metrics(registry: &prometheus::Registry) {
crate::http::unleash_client::UPSTREAM_VERSION.clone(),
))
.unwrap();
registry
.register(Box::new(
crate::metrics::client_metrics::FEATURE_TOGGLE_USAGE_TOTAL.clone(),
))
.unwrap();
}

#[cfg(test)]
Expand Down

0 comments on commit 208e9f0

Please sign in to comment.