Skip to content

Commit

Permalink
feat: finish instrumenting grpc service
Browse files Browse the repository at this point in the history
  • Loading branch information
david-zk committed Oct 1, 2024
1 parent fdb469c commit c88ff0f
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 133 deletions.
103 changes: 0 additions & 103 deletions fhevm-engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion fhevm-engine/coprocessor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ actix-web = "4.9.0"
opentelemetry = "0.25"
opentelemetry-otlp = "0.25"
opentelemetry_sdk = { version = "0.25", features = ["rt-tokio"] }
tonic-tracing-opentelemetry = "0.21"

[dev-dependencies]
testcontainers = "0.21"
Expand Down
11 changes: 10 additions & 1 deletion fhevm-engine/coprocessor/src/db_queries.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
use std::collections::{BTreeSet, HashMap};
use std::str::FromStr;

use crate::server::GrpcTracer;
use crate::types::{CoprocessorError, TfheTenantKeys};
use fhevm_engine_common::utils::{safe_deserialize_versioned, safe_deserialize_versioned_sks};
use opentelemetry::trace::Span;
use opentelemetry::KeyValue;
use sqlx::{query, Postgres};

/// Returns tenant id upon valid authorization request
pub async fn check_if_api_key_is_valid<T>(
req: &tonic::Request<T>,
pool: &sqlx::Pool<Postgres>,
ctx: &GrpcTracer,
) -> Result<i32, CoprocessorError> {
let mut outer_span = ctx.child_span("check_api_key_validity");
match req.metadata().get("authorization") {
Some(auth) => {
let auth_header = String::from_utf8(auth.as_bytes().to_owned())
Expand All @@ -28,19 +33,23 @@ pub async fn check_if_api_key_is_valid<T>(
Err(_) => return Err(CoprocessorError::Unauthorized),
};

let span = ctx.child_span("db_query_api_key");
let tenant = query!(
"SELECT tenant_id FROM tenants WHERE tenant_api_key = $1",
api_key
)
.fetch_all(pool)
.await
.map_err(Into::<CoprocessorError>::into)?;
drop(span);

if tenant.is_empty() {
return Err(CoprocessorError::Unauthorized);
}

return Ok(tenant[0].tenant_id);
let tenant_id = tenant[0].tenant_id;
outer_span.set_attribute(KeyValue::new("tenant_id", tenant_id as i64));
return Ok(tenant_id);
}
None => {
return Err(CoprocessorError::Unauthorized);
Expand Down
Loading

0 comments on commit c88ff0f

Please sign in to comment.