From 5d15ca72bab7a861e2cc4ea3f5affae1312d719f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Sun, 20 Apr 2025 18:19:01 +0200 Subject: [PATCH 1/8] fix: update version and add native otlp sdk --- Cargo.toml | 41 ++++++++++++++++++++----------------- schemas_repo | 2 +- src/main.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 76 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1280774..885ae0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,26 +1,31 @@ [package] name = "misarch-order" -version = "0.1.0" -edition = "2021" +version = "0.3.0" +edition = "2024" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -async-graphql = { version = "6.0.11", features = ["bson", "chrono", "uuid", "log"] } -async-graphql-axum = "6.0.11" -tokio = { version = "1.8", features = ["macros", "rt-multi-thread"] } -axum = { version = "0.6.0", features = ["headers", "macros"] } -mongodb = "2.8.0" -serde = "1.0.193" -futures = "0.3.30" -bson = { version = "2.8.1", features = ["chrono"]} -clap = { version = "4.4.13", features = ["derive"] } -uuid = { version = "1.6.1", features = ["v4", "serde"] } +async-graphql = { version = "7.0.16", features = ["bson", "chrono", "uuid", "log"] } +async-graphql-axum = "7.0.16" +tokio = { version = "1.44.2", features = ["macros", "rt-multi-thread"] } +axum = { version = "0.8.3", features = ["macros"] } +mongodb = "2.8.2" +serde = "1.0.219" +futures = "0.3.31" +bson = { version = "2.14.0", features = ["chrono"]} +clap = { version = "4.5.37", features = ["derive"] } +uuid = { version = "1.16.0", features = ["v4", "serde"] } mongodb-cursor-pagination = "0.3.2" json = "0.12.4" -log = "0.4.20" -simple_logger = "4.3.3" -serde_json = "1.0.113" -graphql_client = "0.13.0" -reqwest = { version = "0.11.24", features = ["json"] } -chrono = { version = "0.4.33", features = ["serde"] } \ No newline at end of file +log = "0.4.27" +simple_logger = "5.0.0" +serde_json = "1.0.140" +graphql_client = "0.14.0" +reqwest = { version = "0.12.15", features = ["json"] } +chrono = { version = "0.4.40", features = ["serde"] } +opentelemetry = "0.28.0" +opentelemetry_sdk = { version = "0.28.0", features = ["rt-tokio"]} +opentelemetry-otlp = "0.28.0" +axum-otel-metrics = "0.10.0" +once_cell = "1.21.3" \ No newline at end of file diff --git a/schemas_repo b/schemas_repo index be410ef..1106b53 160000 --- a/schemas_repo +++ b/schemas_repo @@ -1 +1 @@ -Subproject commit be410ef2e1baa2e6bf2e4a2e84a7d31ad264b2ee +Subproject commit 1106b53cfe00c973b27873376be3f08bcb7106dc diff --git a/src/main.rs b/src/main.rs index 2292690..9c89abe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,9 +11,18 @@ use axum::{ http::{header::HeaderMap, StatusCode}, response::{self, IntoResponse}, routing::{get, post}, - Router, Server, + Router, }; +use once_cell::sync::Lazy; +use axum_otel_metrics::HttpMetricsLayerBuilder; +use axum_otel_metrics::HttpMetricsLayer; + +use opentelemetry::global; +use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider, Temporality}; +use opentelemetry_sdk::Resource; +use opentelemetry_otlp::WithExportConfig; + use clap::{arg, command, Parser}; use log::{info, Level}; @@ -136,7 +145,7 @@ struct Args { /// Activates logger and parses argument for optional schema generation. Otherwise starts gRPC and GraphQL server. #[tokio::main] async fn main() -> std::io::Result<()> { - simple_logger::init_with_level(Level::Warn).unwrap(); + simple_logger::init_with_level(Level::Debug).unwrap(); let args = Args::parse(); if args.generate_schema { @@ -172,6 +181,37 @@ async fn graphql_handler( schema.execute(req).await.into() } +static RESOURCE: Lazy = Lazy::new(|| { + Resource::builder() + .with_service_name("order") + .build() +}); + +/// Initializes OpenTelemetry metrics exporter and sets the global meter provider. +fn init_otlp() -> HttpMetricsLayer { + let exporter = opentelemetry_otlp::MetricExporter::builder() + .with_http() + .with_endpoint("http://otel-collector:4318/v1/metrics") + .with_temporality(Temporality::default()) + .build() + .unwrap(); + + let reader = PeriodicReader::builder(exporter) + .with_interval(std::time::Duration::from_secs(5)) + .build(); + + let provider = SdkMeterProvider::builder() + .with_reader(reader) + .with_resource(RESOURCE.clone()) + .build(); + + global::set_meter_provider(provider.clone()); + + HttpMetricsLayerBuilder::new() + .with_provider(provider.clone()) + .build() +} + /// Starts order service on port 8000. async fn start_service() { let client = db_connection().await; @@ -188,11 +228,18 @@ async fn start_service() { .route("/health", get(StatusCode::OK)) .with_state(schema); let dapr_router = build_dapr_router(db_client).await; - let app = Router::new().merge(graphiql).merge(dapr_router); + + let metrics = init_otlp(); + + let app = Router::new() + .merge(graphiql) + .merge(dapr_router) + .layer(metrics); info!("GraphiQL IDE: http://0.0.0.0:8080"); - Server::bind(&"0.0.0.0:8080".parse().unwrap()) - .serve(app.into_make_service()) + + let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap(); + axum::serve(listener, app) .await .unwrap(); } From ec305a0fe626f1c75aa6c0f8b621aecb6999a08d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Mon, 21 Apr 2025 14:39:54 +0200 Subject: [PATCH 2/8] chore: integrate axum-metrics and overwrite help text to fit semantic conventions --- Cargo.toml | 8 ++++---- axum-otel-metrics | 1 + base-dockerfile | 1 + 3 files changed, 6 insertions(+), 4 deletions(-) create mode 160000 axum-otel-metrics diff --git a/Cargo.toml b/Cargo.toml index 885ae0c..09c5b67 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,8 +24,8 @@ serde_json = "1.0.140" graphql_client = "0.14.0" reqwest = { version = "0.12.15", features = ["json"] } chrono = { version = "0.4.40", features = ["serde"] } -opentelemetry = "0.28.0" -opentelemetry_sdk = { version = "0.28.0", features = ["rt-tokio"]} -opentelemetry-otlp = "0.28.0" -axum-otel-metrics = "0.10.0" +opentelemetry = "0.29.0" +opentelemetry_sdk = { version = "0.29.0", features = ["rt-tokio"]} +opentelemetry-otlp = "0.29.0" +axum-otel-metrics = { path = "axum-otel-metrics" } once_cell = "1.21.3" \ No newline at end of file diff --git a/axum-otel-metrics b/axum-otel-metrics new file mode 160000 index 0000000..a3ddca1 --- /dev/null +++ b/axum-otel-metrics @@ -0,0 +1 @@ +Subproject commit a3ddca19ad9fa5aa2582d9c09cb904d666fbf835 diff --git a/base-dockerfile b/base-dockerfile index 091b798..8b91deb 100644 --- a/base-dockerfile +++ b/base-dockerfile @@ -9,6 +9,7 @@ RUN cargo chef prepare --recipe-path recipe.json FROM chef AS builder COPY --from=planner /misarch-order/recipe.json recipe.json +COPY --from=planner /misarch-order/axum-otel-metrics axum-otel-metrics RUN apt update && apt install -y pkg-config libssl-dev wget && rm -rf /var/lib/apt/lists/* From 364b2d79666c405b1b53227ae8c7f6c4fdd4254c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Mon, 21 Apr 2025 15:34:42 +0200 Subject: [PATCH 3/8] chore: add gitmodule --- .gitmodules | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitmodules b/.gitmodules index 0a298b1..4d00598 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "schemas"] path = schemas_repo url = ../schemas +[submodule "axum-otel-metrics"] + path = axum-otel-metrics + url = ../axum-otel-metrics \ No newline at end of file From 90964e2496fe10c870b2f4a5aa929aba191be099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Fri, 6 Jun 2025 08:40:46 +0200 Subject: [PATCH 4/8] chore: new schemas_repo version --- schemas_repo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schemas_repo b/schemas_repo index 1106b53..7521058 160000 --- a/schemas_repo +++ b/schemas_repo @@ -1 +1 @@ -Subproject commit 1106b53cfe00c973b27873376be3f08bcb7106dc +Subproject commit 7521058cd71724a7cb84f286047fbbd9f6914cb7 From e2322f753b3a572dbec51de6f5a5ea3090bb4234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Fri, 6 Jun 2025 09:48:56 +0200 Subject: [PATCH 5/8] chore: bump otel version --- Cargo.toml | 6 +++--- axum-otel-metrics | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 09c5b67..a990581 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,8 +24,8 @@ serde_json = "1.0.140" graphql_client = "0.14.0" reqwest = { version = "0.12.15", features = ["json"] } chrono = { version = "0.4.40", features = ["serde"] } -opentelemetry = "0.29.0" -opentelemetry_sdk = { version = "0.29.0", features = ["rt-tokio"]} -opentelemetry-otlp = "0.29.0" +opentelemetry = "0.30.0" +opentelemetry_sdk = { version = "0.30.0", features = ["rt-tokio"]} +opentelemetry-otlp = "0.30.0" axum-otel-metrics = { path = "axum-otel-metrics" } once_cell = "1.21.3" \ No newline at end of file diff --git a/axum-otel-metrics b/axum-otel-metrics index a3ddca1..aeda8a1 160000 --- a/axum-otel-metrics +++ b/axum-otel-metrics @@ -1 +1 @@ -Subproject commit a3ddca19ad9fa5aa2582d9c09cb904d666fbf835 +Subproject commit aeda8a1d4772e6174d32c90e2c88a82294e2060d From 3850e89510f4bd826469d7f654e1b9c8b9ab2205 Mon Sep 17 00:00:00 2001 From: eliasmueller Date: Mon, 16 Jun 2025 15:47:47 +0200 Subject: [PATCH 6/8] fix: fix version deps --- schemas_repo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schemas_repo b/schemas_repo index 7521058..27cf3fa 160000 --- a/schemas_repo +++ b/schemas_repo @@ -1 +1 @@ -Subproject commit 7521058cd71724a7cb84f286047fbbd9f6914cb7 +Subproject commit 27cf3fae9030172334138b025db2750f7562302b From 29feddba3b2d6b7d0ff59cf800dc269c6b6ac892 Mon Sep 17 00:00:00 2001 From: eliasmueller Date: Mon, 16 Jun 2025 16:50:58 +0200 Subject: [PATCH 7/8] fix: try to fix order version --- schemas_repo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schemas_repo b/schemas_repo index 27cf3fa..be410ef 160000 --- a/schemas_repo +++ b/schemas_repo @@ -1 +1 @@ -Subproject commit 27cf3fae9030172334138b025db2750f7562302b +Subproject commit be410ef2e1baa2e6bf2e4a2e84a7d31ad264b2ee From 1f6f37b303eb5ae93d371c508a2227a33327988f Mon Sep 17 00:00:00 2001 From: eliasmueller Date: Sun, 22 Jun 2025 20:47:26 +0200 Subject: [PATCH 8/8] chore: remove custom otel-metrics fork --- .gitmodules | 5 +---- Cargo.toml | 2 +- axum-otel-metrics | 1 - base-dockerfile | 1 - 4 files changed, 2 insertions(+), 7 deletions(-) delete mode 160000 axum-otel-metrics diff --git a/.gitmodules b/.gitmodules index 4d00598..aae8548 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ [submodule "schemas"] path = schemas_repo - url = ../schemas -[submodule "axum-otel-metrics"] - path = axum-otel-metrics - url = ../axum-otel-metrics \ No newline at end of file + url = ../schemas \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index a990581..00cec15 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,5 +27,5 @@ chrono = { version = "0.4.40", features = ["serde"] } opentelemetry = "0.30.0" opentelemetry_sdk = { version = "0.30.0", features = ["rt-tokio"]} opentelemetry-otlp = "0.30.0" -axum-otel-metrics = { path = "axum-otel-metrics" } +axum-otel-metrics = { version = "0.12.0" } once_cell = "1.21.3" \ No newline at end of file diff --git a/axum-otel-metrics b/axum-otel-metrics deleted file mode 160000 index aeda8a1..0000000 --- a/axum-otel-metrics +++ /dev/null @@ -1 +0,0 @@ -Subproject commit aeda8a1d4772e6174d32c90e2c88a82294e2060d diff --git a/base-dockerfile b/base-dockerfile index 8b91deb..091b798 100644 --- a/base-dockerfile +++ b/base-dockerfile @@ -9,7 +9,6 @@ RUN cargo chef prepare --recipe-path recipe.json FROM chef AS builder COPY --from=planner /misarch-order/recipe.json recipe.json -COPY --from=planner /misarch-order/axum-otel-metrics axum-otel-metrics RUN apt update && apt install -y pkg-config libssl-dev wget && rm -rf /var/lib/apt/lists/*