Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions backend/graph-proxy/src/graphql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@
auth_token_header: Option<TypedHeader<Authorization<Bearer>>>,
request: GraphQLRequest,
) -> GraphQLResponse {
let start = std::time::Instant::now();
let query = request.into_inner();
let mut request_type = "unparseable";

if let Ok(query) = parse_query(&query.query) {
let operation = query.operations;
Expand All @@ -130,19 +132,24 @@
.map(|operation| operation.1.node.ty)
.collect(),
};
let mut has_mutation = false;
for operation in operations {
match operation {
async_graphql::parser::types::OperationType::Query => state
.metrics_state
.total_requests
.add(1, &[KeyValue::new("request_type", "query")]),
async_graphql::parser::types::OperationType::Mutation => state
async_graphql::parser::types::OperationType::Mutation => {
has_mutation = true;
state
.metrics_state
.total_requests
.add(1, &[KeyValue::new("request_type", "mutation")]),
.add(1, &[KeyValue::new("request_type", "mutation")])
},
async_graphql::parser::types::OperationType::Subscription => {}
};
}
request_type = if has_mutation { "mutation" } else { "query" };
} else {
state
.metrics_state
Expand All @@ -151,7 +158,22 @@
};

let auth_token = auth_token_header.map(|header| header.0);
state.schema.execute(query.data(auth_token)).await.into()

Check failure on line 161 in backend/graph-proxy/src/graphql/mod.rs

View workflow job for this annotation

GitHub Actions / graph_proxy_code / lint

expected `;`, found keyword `let`
let response = state.schema.execute(query.data(auth_token)).await;
let elapsed_ms = start.elapsed().as_secs_f64() * 1000.0;
let status = if response.errors.is_empty() {
"ok"
} else {
"error"
};
state.metrics_state.request_duration_ms.record(
elapsed_ms,
&[
KeyValue::new("request_type", request_type),
KeyValue::new("status", status),
],
);
response.into()
}

lazy_static! {
Expand Down
15 changes: 13 additions & 2 deletions backend/graph-proxy/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use opentelemetry::metrics::{Counter, MeterProvider};
use opentelemetry::metrics::{Counter, Histogram, MeterProvider};
use opentelemetry_sdk::metrics::SdkMeterProvider;

/// Thread-safe wrapper for OTEL metrics
Expand All @@ -11,6 +11,8 @@ pub type MetricsState = Arc<Metrics>;
pub struct Metrics {
/// Total requests on all routes
pub total_requests: Counter<u64>,
/// Request duration in miliseconds on every request
pub request_duration_ms: Histogram<f64>,
}

impl Metrics {
Expand All @@ -23,6 +25,15 @@ impl Metrics {
.with_description("The total requests on all routes made since the last restart.")
.build();

Metrics { total_requests }
let request_duration_ms = meter
.f64_histogram("graph_proxy_request_duration_ms")
.with_description("GraphQL request duration")
.with_unit("ms")
.build();

Metrics {
total_requests,
request_duration_ms,
}
}
}
Loading