manual propagators for context from client to node

This commit is contained in:
Floriane TUERNAL SABOTINOV
2025-09-06 12:57:02 +02:00
parent 65b5c258e8
commit b7b399d71c
6 changed files with 49 additions and 66 deletions
Generated
+3
View File
@@ -5698,6 +5698,7 @@ dependencies = [
"mock_instant",
"nym-api-requests",
"nym-authenticator-requests",
"nym-bin-common",
"nym-client-core",
"nym-credential-verification",
"nym-credentials",
@@ -5787,6 +5788,7 @@ dependencies = [
"bs58",
"futures",
"generic-array 0.14.7",
"nym-bin-common",
"nym-compact-ecash",
"nym-credentials",
"nym-credentials-interface",
@@ -5798,6 +5800,7 @@ dependencies = [
"nym-task",
"nym-test-utils",
"opentelemetry",
"opentelemetry_sdk",
"rand 0.8.5",
"serde",
"serde_json",
+2
View File
@@ -23,6 +23,7 @@ time = { workspace = true }
subtle = { workspace = true }
zeroize = { workspace = true }
nym-bin-common = { path = "../bin-common" }
nym-crypto = { path = "../crypto", features = ["aead", "hashing"] }
nym-pemstore = { path = "../pemstore" }
nym-sphinx = { path = "../nymsphinx" }
@@ -34,6 +35,7 @@ nym-credentials = { path = "../credentials" }
nym-credentials-interface = { path = "../credentials-interface" }
opentelemetry = { workspace = true, features = ["trace"], optional = true }
opentelemetry_sdk = { workspace = true }
tracing = { workspace = true, features = ["std", "attributes", "tracing-attributes"], optional = true }
[target."cfg(not(target_arch = \"wasm32\"))".dependencies.tokio]
@@ -4,6 +4,7 @@
use crate::{AuthenticationFailure, GatewayRequestsError, SharedGatewayKey};
use nym_crypto::asymmetric::ed25519;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::iter;
use std::time::Duration;
use subtle::ConstantTimeEq;
@@ -18,7 +19,7 @@ pub struct AuthenticateRequest {
pub request_signature: ed25519::Signature,
#[serde(default)]
pub debug_trace_id: Option<String>,
pub otel_context: Option<HashMap<String, String>>,
}
impl AuthenticateRequest {
@@ -26,7 +27,7 @@ impl AuthenticateRequest {
protocol_version: u8,
shared_key: &SharedGatewayKey,
identity_keys: &ed25519::KeyPair,
debug_trace_id: Option<String>,
otel_context: Option<HashMap<String, String>>,
) -> Result<AuthenticateRequest, GatewayRequestsError> {
let content = AuthenticateRequestContent::new(
protocol_version,
@@ -39,7 +40,7 @@ impl AuthenticateRequest {
Ok(AuthenticateRequest {
content,
request_signature,
debug_trace_id,
otel_context,
})
}
@@ -12,11 +12,14 @@ use nym_credentials_interface::CredentialSpendingData;
use nym_crypto::asymmetric::ed25519;
use nym_sphinx::DestinationAddressBytes;
use nym_statistics_common::types::SessionType;
use opentelemetry::trace::TraceContextExt;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::str::FromStr;
use tracing::{instrument, warn};
use tungstenite::Message;
use nym_bin_common::opentelemetry::context::ContextCarrier;
use opentelemetry::propagation::TextMapPropagator;
use opentelemetry_sdk::propagation::TraceContextPropagator;
pub mod authenticate;
@@ -81,9 +84,10 @@ pub enum ClientControlRequest {
/// this is a trace id that is used in testing and performance verification
/// in mainnet, this will always be set to None
#[serde(default)]
debug_trace_id: Option<String>,
otel_context: Option<HashMap<String, String>>,
},
AuthenticateV2(Box<AuthenticateRequest>),
#[serde(alias = "handshakePayload")]
@@ -133,17 +137,21 @@ impl ClientControlRequest {
let nonce = shared_key.random_nonce_or_iv();
let ciphertext = shared_key.encrypt_naive(address.as_bytes_ref(), Some(&nonce))?;
let otel_context = opentelemetry::Context::current();
let span = otel_context.span();
let context = span.span_context();
let trace_id = context.trace_id();
use nym_bin_common::opentelemetry::context::ContextCarrier;
use opentelemetry::propagation::TextMapPropagator;
use opentelemetry_sdk::propagation::TraceContextPropagator;
let context = opentelemetry::Context::current();
let propagator = TraceContextPropagator::new();
let mut carrier = ContextCarrier::new();
propagator.inject_context(&context, &mut carrier);
Ok(ClientControlRequest::Authenticate {
protocol_version,
address: address.as_base58_string(),
enc_address: bs58::encode(&ciphertext).into_string(),
iv: bs58::encode(&nonce).into_string(),
debug_trace_id: Some(trace_id.to_string()),
otel_context: Some(carrier.into_map()),
})
}
@@ -156,16 +164,16 @@ impl ClientControlRequest {
let protocol_version = AUTHENTICATE_V2_PROTOCOL_VERSION;
let otel_context = opentelemetry::Context::current();
let span = otel_context.span();
let context = span.span_context();
let trace_id = context.trace_id();
let propagator = TraceContextPropagator::new();
let mut carrier =ContextCarrier::new();
propagator.inject_context(&otel_context, &mut carrier);
Ok(ClientControlRequest::AuthenticateV2(Box::new(
AuthenticateRequest::new(
protocol_version,
shared_key,
identity_keys,
Some(trace_id.to_string()),
Some(carrier.into_map())
)?,
)))
}
+1
View File
@@ -50,6 +50,7 @@ zeroize = { workspace = true }
# internal
nym-api-requests = { path = "../nym-api/nym-api-requests" }
nym-bin-common = { path = "../common/bin-common" }
nym-credentials = { path = "../common/credentials" }
nym-credentials-interface = { path = "../common/credentials-interface" }
nym-credential-verification = { path = "../common/credential-verification" }
@@ -35,8 +35,10 @@ use nym_sphinx::DestinationAddressBytes;
use nym_task::ShutdownToken;
use opentelemetry::trace::TraceContextExt;
use opentelemetry::TraceId;
use opentelemetry_sdk::logs::TraceContext;
use opentelemetry_sdk::trace::{IdGenerator, RandomIdGenerator};
use rand::CryptoRng;
use std::hash::Hash;
use std::net::SocketAddr;
use std::time::Duration;
use thiserror::Error;
@@ -863,57 +865,26 @@ impl<R, S> FreshHandler<R, S> {
S: AsyncRead + AsyncWrite + Unpin + Send,
R: CryptoRng + RngCore + Send,
{
let span = if let ClientControlRequest::AuthenticateV2(ref auth_req) = request {
if let Some(ref trace_id) = auth_req.debug_trace_id {
warn!("Parsed trace id: {trace_id}");
let trace_id = opentelemetry::trace::TraceId::from_hex(&trace_id)
.expect("Invalid trace ID format");
use nym_bin_common::opentelemetry::context::ContextCarrier;
use opentelemetry_sdk::propagation::TraceContextPropagator;
use opentelemetry::propagation::TextMapPropagator;
let id_generator = RandomIdGenerator::default();
let span_id = id_generator.new_span_id();
let otel_context = match &request {
ClientControlRequest::Authenticate { otel_context, .. } => otel_context.as_ref(),
_ => None,
};
if let Some(otel_context) = otel_context {
let carrier = ContextCarrier::from_map(otel_context.clone());
let propagator = TraceContextPropagator::new();
let extracted_context = propagator.extract(&carrier);
let span_context = opentelemetry::trace::SpanContext::new(
trace_id,
span_id,
opentelemetry::trace::TraceFlags::SAMPLED,
true, // is_remote = true since this comes from another service
Default::default(),
);
let remote_context = opentelemetry::Context::current()
.with_remote_span_context(span_context);
let _context_guard = remote_context.clone().attach();
let span = info_span!(
"websocket_authentication",
trace_id = %trace_id
);
span.set_parent(remote_context.clone());
Some(span)
} else {
warn!("AuthenticateV2 request but no trace_id provided");
None
}
let span = tracing::info_span!("websocket_authentication");
span.set_parent(extracted_context.clone());
error!("==== Context propagation successful! Current context: {:?} ====", extracted_context);
} else {
warn!("Not an AuthenticateV2 request");
None
};
let _guard = match &span{
Some(s) => {
warn!("==== ENTERED SPAN ====");
Some(s.enter())
}
None => {
warn!("==== COULDN'T ENTER SPAN ====");
None
}
};
let current_context = opentelemetry::Context::current();
let initial_trace_id = current_context.span().span_context().trace_id();
error!("==== trace_id at the start of initial authentication: {:?} ====", initial_trace_id);
warn!("No OpenTelemetry context provided in the request");
}
let auth_result = match request {
ClientControlRequest::Authenticate {
@@ -921,7 +892,7 @@ impl<R, S> FreshHandler<R, S> {
address,
enc_address,
iv,
debug_trace_id: _,
otel_context: _,
} => {
self.handle_legacy_authenticate(protocol_version, address, enc_address, iv)
.await
@@ -973,9 +944,6 @@ impl<R, S> FreshHandler<R, S> {
return Err(InitialAuthenticationError::EmptyClientDetails);
};
let current_context = opentelemetry::Context::current();
let final_trace_id = current_context.span().span_context().trace_id();
error!("==== trace_id at the end of initial authentication: {:?} ====", final_trace_id);
Ok(Some(client_details))
}