Skip to content

Commit

Permalink
Merge pull request #279 from NethermindEth/feat/metrics
Browse files Browse the repository at this point in the history
added metrics, migrated to Grafana Agent flow
  • Loading branch information
taco-paco authored Nov 8, 2024
2 parents 7c63f9f + e6b3835 commit 9e761ff
Show file tree
Hide file tree
Showing 31 changed files with 662 additions and 551 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/api-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ jobs:
runs-on: ubuntu-latest
needs: Build
steps:

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
Expand All @@ -117,9 +116,14 @@ jobs:
environment-variables: |
RUST_LOG=INFO
LOG_LEVEL=normal
ENVIRONMENT=dev
METRICS_PORT=8001
VITE_URL=https://cairo-remix-dev.nethermind.io
SERVICE_VERSION=v${{ needs.Build.outputs.image-version }}
PROMTAIL_USERNAME=${{secrets.PROMTAIL_USERNAME}}
PROMTAIL_PASSWORD=${{secrets.PROMTAIL_PASSWORD}}
PROMETHEUS_USERNAME=${{secrets.PROMETHEUS_USERNAME}}
PROMETHEUS_PASSWORD=${{secrets.PROMETHEUS_PASSWORD}}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
Expand All @@ -133,7 +137,6 @@ jobs:
runs-on: ubuntu-latest
needs: Build
steps:

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
Expand All @@ -156,9 +159,14 @@ jobs:
environment-variables: |
RUST_LOG=INFO
LOG_LEVEL=normal
ENVIRONMENT=prod
METRICS_PORT=8001
VITE_URL=https://cairo-remix-dev.nethermind.io
SERVICE_VERSION=v${{ needs.Build.outputs.image-version }}
PROMTAIL_USERNAME=${{secrets.PROMTAIL_USERNAME}}
PROMTAIL_PASSWORD=${{secrets.PROMTAIL_PASSWORD}}
PROMETHEUS_USERNAME=${{secrets.PROMETHEUS_USERNAME}}
PROMETHEUS_PASSWORD=${{secrets.PROMETHEUS_PASSWORD}}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/prod-build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,14 @@ jobs:
# inject the expected React package URL for CORS logic
environment-variables: |
RUST_LOG=INFO
ENVIRONMENT=dev
METRICS_PORT=8001
VITE_URL=https://cairo-remix.nethermind.io
SERVICE_VERSION=v${{ needs.Build.outputs.image-version }}
PROMTAIL_USERNAME=${{secrets.PROMTAIL_USERNAME}}
PROMTAIL_PASSWORD=${{secrets.PROMTAIL_PASSWORD}}
PROMETHEUS_USERNAME=${{secrets.PROMETHEUS_USERNAME}}
PROMETHEUS_PASSWORD=${{secrets.PROMETHEUS_PASSWORD}}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
Expand Down
12 changes: 4 additions & 8 deletions DockerfileRocket
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ ARG SCARB_VERSION
ENV DEBIAN_FRONTEND=noninteractive
ENV DEBCONF_NONINTERACTIVE_SEEN=true
ENV SCARB_VERSION=${SCARB_VERSION}

RUN apt-get clean

RUN apt-get update
Expand All @@ -24,8 +23,7 @@ RUN wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | tee /etc/apt/
RUN echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | tee -a /etc/apt/sources.list.d/grafana.list

RUN apt-get update

RUN apt-get install grafana-agent
RUN apt-get install grafana-agent-flow

WORKDIR /opt/app

Expand All @@ -35,15 +33,13 @@ SHELL ["/bin/bash", "-lc"]
RUN curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | bash -s -- -v $SCARB_VERSION
ENV PATH="/root/.local/bin:${PATH}"

# copy Remix plugin source and install Rust
COPY . /opt/app

# Install Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain stable -y

RUN whoami

ENV PATH="/root/.cargo/bin:${PATH}"

# copy Remix plugin source
COPY . /opt/app
RUN git submodule update --init

# Build the API service
Expand Down
29 changes: 29 additions & 0 deletions api/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.91"
rocket = { version = "0.5.1", features = ["json"] }
serde = { version = "1.0.189", features = ["derive"] }
tracing = "^0.1"
Expand All @@ -17,4 +18,5 @@ crossbeam-queue = "0.3.8"
crossbeam-skiplist = "0.1.1"
fmt = "0.1.0"
thiserror = "1.0.50"
chrono = "0.4.31"
chrono = "0.4.31"
prometheus = "0.13.4"
52 changes: 52 additions & 0 deletions api/configs/grafana-logs.config.river
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
local.file_match "logs" {
path_targets = [{
__address__ = "localhost",
__path__ = env("PROMTAIL_BASE_DIR") + "/logs/*",
host = "localhost",
job = "starknet-remix-docker-logs",
}]
}

loki.source.file "logs" {
targets = local.file_match.logs.targets
forward_to = [loki.write.logs.receiver]
}

loki.write "logs" {
external_labels = {
environment = env("ENVIRONMENT"),
service_version = env("SERVICE_VERSION"),
}
endpoint {
url = "https://logs-prod-us-central1.grafana.net/loki/api/v1/push"
basic_auth {
username = env("PROMTAIL_USERNAME")
password = env("PROMTAIL_PASSWORD")
}
}
}

prometheus.remote_write "metrics" {
endpoint {
name = "starknet-remix-metrics"
url = "https://prometheus-us-central1.grafana.net/api/prom/push"

basic_auth {
username = env("PROMETHEUS_USERNAME")
password = env("PROMETHEUS_PASSWORD")
}
}
}

prometheus.scrape "metrics" {
targets = [{
__address__ = "localhost:" + env("METRICS_PORT"),
__metrics_path__ = "/metrics",
environment = env("ENVIRONMENT"),
service_version = env("SERVICE_VERSION"),
}]
forward_to = [prometheus.remote_write.metrics.receiver]

job_name = "starknet-remix-metrics-job"
scrape_interval = "30s"
}
17 changes: 0 additions & 17 deletions api/configs/grafana-logs.config.yaml

This file was deleted.

12 changes: 11 additions & 1 deletion api/docker_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@

# Note: This script needs to run from inside /api dir
export PROMTAIL_BASE_DIR=$(pwd)
export METRICS_PORT=${METRICS_PORT-8001}

if [ -z ${ENVIRONMENT} ]; then
echo "ENVIRONMENT env var undefined"
exit 1
fi

grafana-agent --config.expand-env=true --config.file ./configs/grafana-logs.config.yaml &
if [ -z ${SERVICE_VERSION} ]; then
echo "SERVICE_VERSION env var undefined"
exit 1
fi

grafana-agent-flow run ./configs/grafana-logs.config.river &

cargo run --release
6 changes: 5 additions & 1 deletion api/example.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
VITE_URL=http://localhost:3000
METRICS_PORT=8001
PROMTAIL_USERNAME=LOKI_Grafana_Username
PROMTAIL_PASSWORD=LOKI_Grafana_password_API_Key
PROMTAIL_PASSWORD=LOKI_Grafana_password_API_Key
PROMETHEUS_USERNAME=PROMETHEUS_Grafana_Username
PROMETHEUS_PASSWORD=PROMETHEUS_Grafana_password_API_Key
4 changes: 2 additions & 2 deletions api/src/types.rs → api/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::io::Error as IoError;

pub type Result<T> = std::result::Result<T, ApiError>;

#[derive(Debug, thiserror::Error)]
pub enum ApiError {
#[error("Failed to execute command: {0}")]
Expand Down Expand Up @@ -35,3 +33,5 @@ pub enum ApiError {
#[error("Error while trying to unlock mutex")]
MutexUnlockError,
}

pub type Result<T, E = ApiError> = std::result::Result<T, E>;
45 changes: 37 additions & 8 deletions api/src/handlers/cairo_version.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
use rocket::tokio::fs::read_dir;
use rocket::State;
use std::path::Path;
use std::process::{Command, Stdio};
use tracing::{error, info, instrument};

use crate::errors::{ApiError, Result};
use crate::handlers::process::{do_process_command, fetch_process_result};
use crate::handlers::types::{ApiCommand, ApiCommandResult};
use crate::rate_limiter::RateLimited;
use crate::types::{ApiError, Result};
use crate::utils::lib::DEFAULT_CAIRO_DIR;
use crate::utils::lib::{CAIRO_COMPILERS_DIR, DEFAULT_CAIRO_DIR};
use crate::worker::WorkerEngine;
use rocket::State;
use std::process::{Command, Stdio};
use tracing::{error, info, instrument};

// Read the version from the cairo Cargo.toml file.
#[instrument]
#[instrument(skip(_rate_limited))]
#[get("/cairo_version")]
pub async fn cairo_version(_rate_limited: RateLimited) -> String {
info!("/cairo_version");
do_cairo_version().unwrap_or_else(|e| format!("Failed to get cairo version: {:?}", e))
}

// Read the version from the cairo Cargo.toml file.
#[instrument]
#[instrument(skip(engine, _rate_limited))]
#[get("/cairo_version_async")]
pub async fn cairo_version_async(
engine: &State<WorkerEngine>,
Expand All @@ -27,7 +30,7 @@ pub async fn cairo_version_async(
do_process_command(ApiCommand::CairoVersion, engine)
}

#[instrument]
#[instrument(skip(engine))]
#[get("/cairo_version_result/<process_id>")]
pub async fn get_cairo_version_result(process_id: String, engine: &State<WorkerEngine>) -> String {
fetch_process_result(process_id, engine, |result| match result {
Expand Down Expand Up @@ -66,3 +69,29 @@ pub fn do_cairo_version() -> Result<String> {
}
}
}

#[instrument]
#[get("/cairo_versions")]
pub async fn cairo_versions() -> String {
do_cairo_versions()
.await
.unwrap_or_else(|e| format!("Failed to get cairo versions: {:?}", e))
}

/// Get cairo versions
pub async fn do_cairo_versions() -> crate::errors::Result<String> {
let path = Path::new(CAIRO_COMPILERS_DIR);

let mut dir = read_dir(path).await.map_err(ApiError::FailedToReadDir)?;
let mut result = vec![];

while let Ok(Some(entry)) = dir.next_entry().await {
let entry = entry;
let path = entry.path();
if path.is_dir() {
result.push(entry.file_name().to_string_lossy().to_string());
}
}

Ok(format!("{:?}", result))
}
31 changes: 0 additions & 31 deletions api/src/handlers/cairo_versions.rs

This file was deleted.

Loading

0 comments on commit 9e761ff

Please sign in to comment.