Skip to content

Commit

Permalink
Add more context variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjoy committed Feb 20, 2024
1 parent 4722721 commit 9ca4ead
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ use std::{
use tracing::{error, instrument, trace, warn};
use url::Url;

const INJECT_CONTEXT_SERVICE_NAME: &str = "SW_SERVICE_NAME";
const INJECT_CONTEXT_INSTANCE_NAME: &str = "SW_INSTANCE_NAME";
const INJECT_CONTEXT_TRACE_ID: &str = "SW_TRACE_ID";
const INJECT_CONTEXT_TRACE_SEGMENT_ID: &str = "SW_TRACE_SEGMENT_ID";

#[instrument(skip_all)]
pub fn init() {
Expand Down Expand Up @@ -80,11 +83,8 @@ fn request_shutdown_for_fpm() -> crate::Result<()> {

fn inject_server_var_for_fpm() -> crate::Result<()> {
if *INJECT_CONTEXT {
let trace_id =
RequestContext::try_with_global_ctx(None, |ctx| Ok(ctx.trace_id().to_owned()))?;

let server = get_mut_page_request_server()?;
server.insert(INJECT_CONTEXT_TRACE_ID, trace_id);
inject_server_var(None, server)?;
}

Ok(())
Expand Down Expand Up @@ -275,10 +275,7 @@ fn request_shutdown_for_swoole(response: &mut ZVal) -> crate::Result<()> {

fn inject_server_var_for_swoole(request_id: Option<i64>, server: &mut ZArr) -> crate::Result<()> {
if *INJECT_CONTEXT {
let trace_id =
RequestContext::try_with_global_ctx(request_id, |ctx| Ok(ctx.trace_id().to_owned()))?;

server.insert(INJECT_CONTEXT_TRACE_ID, trace_id);
inject_server_var(request_id, server)?;
}

Ok(())
Expand Down Expand Up @@ -379,3 +376,13 @@ fn finish_request_context(request_id: Option<i64>, status_code: i32) -> crate::R

Ok(())
}

fn inject_server_var(request_id: Option<i64>, server: &mut ZArr) -> crate::Result<()> {
Ok(RequestContext::try_with_global_ctx(request_id, |ctx| {
server.insert(INJECT_CONTEXT_SERVICE_NAME, ctx.service());
server.insert(INJECT_CONTEXT_INSTANCE_NAME, ctx.service_instance());
server.insert(INJECT_CONTEXT_TRACE_ID, ctx.trace_id());
server.insert(INJECT_CONTEXT_TRACE_SEGMENT_ID, ctx.trace_segment_id());
Ok(())
})?)
}

0 comments on commit 9ca4ead

Please # to comment.