Skip to content

Commit

Permalink
Merge pull request #216 from GPeaky/207-implementing-metrics
Browse files Browse the repository at this point in the history
207 implementing metrics
  • Loading branch information
GPeaky authored Oct 16, 2024
2 parents dd04cd8 + e583028 commit 6f53114
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 25 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"crates/telemetry",
"crates/token_manager",
"crates/utils",
"crates/metrics",
]

default-members = ["crates/api"]
Expand All @@ -30,6 +31,7 @@ entities = { path = "crates/entities" }
error = { path = "crates/error" }
id-generator = { path = "crates/id-generator" }
intelli-core = { path = "crates/intelli-core" }
metrics = { path = "crates/metrics" }
password-hash = { path = "crates/password-hash" }
structs = { path = "crates/structs" }
telemetry = { path = "crates/telemetry" }
Expand All @@ -52,6 +54,7 @@ quick_cache = "0.6"
prost-build = "0.13"
postgres-derive = "0.4"
deadpool-postgres = "0.14"
tracing-opentelemetry = "0.24"
tokio = { version = "1", features = ["full"] }
chrono = { version = "0.4", features = ["serde"] }
reqwest = { version = "0.12", features = ["json"] }
Expand All @@ -65,8 +68,11 @@ ahash = { version = "0.8", features = ["compile-time-rng"] }
dashmap = { version = "6", features = ["inline", "raw-api"] }
refinery = { version = "0.8", features = ["tokio-postgres"] }
postgres-types = { version = "0.2", features = ["with-chrono-0_4"] }
opentelemetry = "0.23"
opentelemetry_sdk = { version = "0.23", features = ["rt-tokio"] }
parking_lot = { version = "0.12", features = ["arc_lock", "nightly"] }
garde = { version = "0.20", features = ["derive", "email", "email-idna"] }
opentelemetry-otlp = { version = "0.16", features = ["grpc-tonic", "metrics"] }
tracing-subscriber = { version = "0.3", features = [
"parking_lot",
"env-filter",
Expand Down
3 changes: 1 addition & 2 deletions crates/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ structs.workspace = true
entities.workspace = true
telemetry.workspace = true
intelli-core.workspace = true
metrics.workspace = true
ntex.workspace = true
tokio.workspace = true
garde.workspace = true
Expand All @@ -32,5 +33,3 @@ dashmap.workspace = true
tokio-stream.workspace = true
ntex-cors.workspace = true
openssl.workspace = true
tracing-log.workspace = true
tracing-subscriber.workspace = true
16 changes: 0 additions & 16 deletions crates/api/src/config/local_tracing.rs

This file was deleted.

3 changes: 0 additions & 3 deletions crates/api/src/config/mod.rs

This file was deleted.

5 changes: 2 additions & 3 deletions crates/api/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
mod config;
mod handlers;
mod middlewares;
mod routes;
mod states;

use config::initialize_tracing_subscriber;
use dashmap::DashMap;
use db::Database;
use dotenvy::{dotenv, var};
use metrics::initialize_tracing_and_telemetry;
use ntex::{http::header, web};
use ntex_cors::Cors;
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
Expand All @@ -20,7 +19,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
#[tokio::main]
async fn main() -> std::io::Result<()> {
dotenv().ok();
initialize_tracing_subscriber();
initialize_tracing_and_telemetry().expect("Error initializing metrics");

ntex::rt::System::new("intelli-api")
.run_local(async {
Expand Down
20 changes: 20 additions & 0 deletions crates/metrics/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "metrics"
version = "0.1.0"
edition = "2021"

[lints]
workspace = true

[lib]
path = "src/metrics.rs"
doctest = false

[dependencies]
opentelemetry.workspace = true
opentelemetry_sdk.workspace = true
tracing-opentelemetry.workspace = true
opentelemetry-otlp.workspace = true
tracing-subscriber.workspace = true
tracing-log.workspace = true
tracing.workspace = true
49 changes: 49 additions & 0 deletions crates/metrics/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use opentelemetry::global;
use opentelemetry::KeyValue;
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::{
runtime::Tokio,
trace::{self, Tracer},
Resource,
};
use tracing_log::LogTracer;
use tracing_subscriber::{fmt, prelude::*, EnvFilter, Registry};

pub fn initialize_tracing_and_telemetry(
) -> Result<Tracer, Box<dyn std::error::Error + Send + Sync + 'static>> {
global::set_text_map_propagator(opentelemetry_sdk::propagation::TraceContextPropagator::new());

let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint("http://localhost:4317"),
)
.with_trace_config(
trace::config().with_resource(Resource::new(vec![KeyValue::new(
"service.name",
"intelli-api",
)])),
)
.install_batch(Tokio)?;

let filter = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new("info"))
.unwrap();

let fmt_layer = fmt::layer().with_ansi(true).compact();

let telemetry_layer = tracing_opentelemetry::layer().with_tracer(tracer.clone());

let subscriber = Registry::default()
.with(filter)
.with(fmt_layer)
.with(telemetry_layer);

LogTracer::init()?;

tracing::subscriber::set_global_default(subscriber)?;

Ok(tracer)
}
2 changes: 1 addition & 1 deletion crates/telemetry/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,6 @@ fn cast<T>(bytes: &[u8]) -> AppResult<&T> {
if !mem::size_of::<T>() == bytes.len() {
Err(F1ServiceError::CastingError)?;
}

Ok(unsafe { &*(bytes.as_ptr() as *const T) })
}

0 comments on commit 6f53114

Please sign in to comment.