diff --git a/apollo-router/src/axum_factory/axum_http_server_factory.rs b/apollo-router/src/axum_factory/axum_http_server_factory.rs index 0b414f25b1f..08ea743f765 100644 --- a/apollo-router/src/axum_factory/axum_http_server_factory.rs +++ b/apollo-router/src/axum_factory/axum_http_server_factory.rs @@ -402,9 +402,10 @@ where (license, Instant::now(), Arc::new(AtomicU64::new(0))), license_handler, )) - .layer(TraceLayer::new_for_http().make_span_with(PropagatingMakeSpan { license })) .layer(Extension(service_factory)) - .layer(cors); + .layer(cors) + .layer(TraceLayer::new_for_http().make_span_with(PropagatingMakeSpan { license })) + .layer(middleware::from_fn(metrics_handler)); let route = endpoints_on_main_listener .into_iter() @@ -414,6 +415,17 @@ where Ok(ListenAddrAndRouter(listener, route)) } +async fn metrics_handler(request: Request, next: Next) -> Response { + let resp = next.run(request).await; + u64_counter!( + "apollo.router.operations", + "The number of graphql operations performed by the Router", + 1, + "http.response.status_code" = resp.status().as_u16() as i64 + ); + resp +} + async fn license_handler( State((license, start, delta)): State<(LicenseState, Instant, Arc)>, request: Request, diff --git a/apollo-router/src/plugins/telemetry/mod.rs b/apollo-router/src/plugins/telemetry/mod.rs index 4c76dcf5b73..9a35aeabeee 100644 --- a/apollo-router/src/plugins/telemetry/mod.rs +++ b/apollo-router/src/plugins/telemetry/mod.rs @@ -833,12 +833,6 @@ impl Telemetry { if !parts.status.is_success() { metric_attrs.push(KeyValue::new("error", parts.status.to_string())); } - u64_counter!( - "apollo.router.operations", - "The number of graphql operations performed by the Router", - 1, - "http.response.status_code" = parts.status.as_u16() as i64 - ); let response = http::Response::from_parts( parts, once(ready(first_response.unwrap_or_default())) @@ -850,12 +844,6 @@ impl Telemetry { } Err(err) => { metric_attrs.push(KeyValue::new("status", "500")); - u64_counter!( - "apollo.router.operations", - "The number of graphql operations performed by the Router", - 1, - "http.response.status_code" = 500 - ); Err(err) } };