Skip to content

Commit

Permalink
feat: add initial otlp tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegec committed Mar 17, 2024
1 parent 3396083 commit 76a6969
Show file tree
Hide file tree
Showing 8 changed files with 423 additions and 19 deletions.
381 changes: 369 additions & 12 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion buildit-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ octocrab = "0.35.0"
jsonwebtoken = "9"
anyhow = "1"
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread", "process", "sync", "fs"] }
log = "0.4"
gix = { version = "0.58", default-features = false, features = ["max-performance-safe", "revision"] }
gix-features = { version = "0.38.0", optional = true }
walkdir = "2.4.0"
abbs-meta-apml = { git = "https://github.com/AOSC-Dev/abbs-meta-rs", package = "abbs-meta-apml", rev = "4a592937b44e8bb93103edd34eff384169a3248a" }
fancy-regex = "0.13"
reqwest = "0.11.24"
thiserror ="1.0"
tracing = "0.1.40"

[features]
default = ["gix-max-perf"]
Expand Down
3 changes: 2 additions & 1 deletion buildit-utils/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use gix::{
prelude::ObjectIdExt, sec, sec::trust::DefaultForLevel, Repository, ThreadSafeRepository,
};
use jsonwebtoken::EncodingKey;
use log::{debug, error, info};
use octocrab::{models::pulls::PullRequest, params};
use std::{
borrow::Cow,
Expand All @@ -15,6 +14,7 @@ use std::{
process::Output,
};
use tokio::{process, task};
use tracing::{debug, error, info};
use walkdir::WalkDir;

use crate::{
Expand Down Expand Up @@ -269,6 +269,7 @@ fn get_commits(path: &Path) -> anyhow::Result<Vec<Commit>> {
}

/// Update ABBS tree commit logs
#[tracing::instrument(skip(abbs_path))]
pub async fn update_abbs<P: AsRef<Path>>(git_ref: &str, abbs_path: P) -> anyhow::Result<()> {
info!("Running git checkout -b stable ...");

Expand Down
6 changes: 5 additions & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ jsonwebtoken = "9.2.0"
size = "0.4.1"
dickens = { git = "https://github.com/AOSC-Dev/dickens.git", version = "0.1.0" }
axum = "0.7.4"
tracing-subscriber = "0.3.18"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tracing = "0.1.40"
tower-http = { version = "0.5.2", features = ["trace", "fs", "cors"] }
diesel = { version = "2.1.4", features = ["postgres", "chrono", "r2d2", "bigdecimal", "numeric"] }
bigdecimal = { version = "0.4.3", features = ["serde"] }
opentelemetry = "0.22.0"
tracing-opentelemetry = "0.23.0"
opentelemetry-otlp = { version = "0.15.0", features = ["http-proto", "reqwest-client"] }
opentelemetry_sdk = { version = "0.22.1", features = ["rt-tokio"] }
1 change: 1 addition & 0 deletions server/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum JobSource {
Manual,
}

#[tracing::instrument(skip(pool))]
pub async fn pipeline_new(
pool: DbPool,
git_branch: &str,
Expand Down
2 changes: 1 addition & 1 deletion server/src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use buildit_utils::github::{get_archs, OpenPRError, OpenPRRequest};
use chrono::Local;
use diesel::{Connection, ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl};
use serde::Deserialize;
use tracing::warn;
use tracing::{info_span, warn};

Check warning on line 13 in server/src/bot.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `info_span`

use std::borrow::Cow;
use teloxide::{
Expand Down
4 changes: 4 additions & 0 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ pub struct Args {
/// Development mode
#[arg(env = "BUILDIT_DEVELOPMENT")]
pub development_mode: Option<bool>,

/// OpenTelemetry
#[arg(env = "BUILDIT_OTLP")]
pub otlp_url: Option<String>,
}

pub static ARGS: Lazy<Args> = Lazy::new(Args::parse);
Expand Down
43 changes: 40 additions & 3 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ use axum::{routing::get, Router};
use diesel::pg::PgConnection;
use diesel::r2d2::ConnectionManager;
use diesel::r2d2::Pool;
use opentelemetry::KeyValue;
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::trace;
use opentelemetry_sdk::Resource;
use server::bot::{answer, Command};
use server::recycler::recycler_worker;
use server::routes::{
Expand All @@ -17,12 +21,43 @@ use server::{DbPool, ARGS};
use teloxide::prelude::*;
use tower_http::cors::{Any, CorsLayer};
use tower_http::services::{ServeDir, ServeFile};
use tracing::info_span;
use tracing::{info_span, Instrument};
use tracing_subscriber::prelude::*;
use tracing_subscriber::EnvFilter;
use tracing_subscriber::Registry;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenv::dotenv()?;
tracing_subscriber::fmt::init();
// setup opentelemetry
if let Some(otlp_url) = &ARGS.otlp_url {
// setup otlp
let exporter = opentelemetry_otlp::new_exporter()
.http()
.with_endpoint(otlp_url);
let otlp_tracer =
opentelemetry_otlp::new_pipeline()
.tracing()
.with_trace_config(trace::config().with_resource(Resource::new(vec![
KeyValue::new("service.name", "buildit"),
])))
.with_exporter(exporter)
.install_batch(opentelemetry_sdk::runtime::Tokio)?;

// let tracing crate output to opentelemetry
let tracing_leyer = tracing_opentelemetry::layer().with_tracer(otlp_tracer);
let subscriber = Registry::default();
// respect RUST_LOG
let env_filter = EnvFilter::try_from_default_env().unwrap_or(EnvFilter::new("INFO"));
subscriber
.with(env_filter)
.with(tracing_leyer)
.with(tracing_subscriber::fmt::Layer::default())
.init();
} else {
// fallback to stdout
tracing_subscriber::fmt::init();
}

tracing::info!("Connecting to database");
let manager = ConnectionManager::<PgConnection>::new(&ARGS.database_url);
Expand All @@ -36,7 +71,9 @@ async fn main() -> anyhow::Result<()> {
let handler =
Update::filter_message().branch(dptree::entry().filter_command::<Command>().endpoint(
|bot: Bot, pool: DbPool, msg: Message, cmd: Command| async move {
answer(bot, msg, cmd, pool).await
answer(bot, msg, cmd, pool)
.instrument(info_span!("answer_telegram_message"))
.await
},
));

Expand Down

0 comments on commit 76a6969

Please sign in to comment.