Skip to content

Commit

Permalink
Propagate OpenTelemetry spans
Browse files Browse the repository at this point in the history
  • Loading branch information
garryod committed Mar 25, 2024
1 parent b255764 commit b97cde8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 20 deletions.
52 changes: 48 additions & 4 deletions Cargo.lock

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

6 changes: 4 additions & 2 deletions sessions/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "sessions"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

[dependencies]
anyhow = "1.0.81"
anyhow = { version = "1.0.81" }
async-graphql = { version = "7.0.3", default-features = false, features = [
"chrono",
"graphiql",
Expand All @@ -13,11 +13,13 @@ async-graphql = { version = "7.0.3", default-features = false, features = [
async-graphql-axum = { version = "7.0.3" }
axum = { version = "0.7.4" }
axum-extra = { version = "0.9.2", features = ["typed-header"] }
axum-tracing-opentelemetry = "0.18.0"
chrono = { version = "0.4.35" }
clap = { version = "4.5.3", features = ["derive", "env"] }
dotenvy = { version = "0.15.7" }
models = { path = "../models" }
opentelemetry = { version = "0.22.0", features = ["metrics"] }
opentelemetry-http = { version = "0.11.0" }
opentelemetry-otlp = { version = "0.15.0", features = ["metrics", "tokio"] }
opentelemetry-semantic-conventions = { version = "0.14.0" }
opentelemetry_sdk = { version = "0.22.1", features = ["rt-tokio"] }
Expand Down
22 changes: 13 additions & 9 deletions sessions/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ use crate::{
opa::OpaClient,
route_handlers::GraphQLHandler,
};
use async_graphql::{extensions::Tracing, http::GraphiQLSource, SDLExportOptions};
use async_graphql::{http::GraphiQLSource, SDLExportOptions};
use axum::{response::Html, routing::get, Router};
use axum_tracing_opentelemetry::middleware::{OtelAxumLayer, OtelInResponseLayer};
use clap::Parser;
use opentelemetry_otlp::WithExportConfig;
use sea_orm::{ConnectOptions, Database, DatabaseConnection, DbErr, TransactionError};
Expand Down Expand Up @@ -84,7 +85,7 @@ async fn main() {
let database = setup_database(args.database_url).await.unwrap();
let opa_client = OpaClient::new(args.opa_url);
let schema = root_schema_builder()
.extension(Tracing)
.extension(async_graphql::extensions::Tracing)
.data(database)
.data(opa_client)
.finish();
Expand Down Expand Up @@ -117,13 +118,16 @@ fn setup_router(schema: RootSchema) -> Router {
#[allow(clippy::missing_docs_in_private_items)]
const GRAPHQL_ENDPOINT: &str = "/";

Router::new().route(
GRAPHQL_ENDPOINT,
get(Html(
GraphiQLSource::build().endpoint(GRAPHQL_ENDPOINT).finish(),
))
.post(GraphQLHandler::new(schema)),
)
Router::new()
.route(
GRAPHQL_ENDPOINT,
get(Html(
GraphiQLSource::build().endpoint(GRAPHQL_ENDPOINT).finish(),
))
.post(GraphQLHandler::new(schema)),
)
.layer(OtelInResponseLayer)
.layer(OtelAxumLayer::default())
}

/// Serves the endpoints on the specified port forever
Expand Down
19 changes: 14 additions & 5 deletions sessions/src/opa.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use axum_extra::headers::{authorization::Bearer, Authorization};
use serde::{Deserialize, Serialize};
use tracing::instrument;
use url::Url;

/// Parametrers required by OPA to make the policy decision
Expand Down Expand Up @@ -50,14 +51,22 @@ impl OpaClient {
}

/// Queries OPA with the [`OpaInput`] and returns the [`Decision`]
#[instrument(skip(input))]
async fn query<P: Serialize>(&self, input: OpaInput<P>) -> Result<Decision, reqwest::Error> {
self.client
let mut request = self
.client
.post(self.endpoint.clone())
.json(&input)
.send()
.await?
.json()
.await
.build()?;

opentelemetry::global::get_text_map_propagator(|propagator| {
propagator.inject_context(
&opentelemetry::Context::current(),
&mut opentelemetry_http::HeaderInjector(request.headers_mut()),
)
});

self.client.execute(request).await?.json().await
}

/// Queries OPA with the [`OpaInput`] and returns a [`Result`]
Expand Down

0 comments on commit b97cde8

Please sign in to comment.