Skip to content

Commit

Permalink
chore: add tracing headers for bria
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit committed Jan 18, 2024
1 parent b726ce6 commit c9e29d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
25 changes: 21 additions & 4 deletions bria-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ mod proto {
tonic::include_proto!("services.bria.v1");
}

use std::collections::HashMap;

use opentelemetry::propagation::TextMapPropagator;
use opentelemetry::sdk::propagation::TraceContextPropagator;
use tracing::instrument;
use tracing_opentelemetry::OpenTelemetrySpanExt;

use super::{config::BriaClientConfig, error::BriaClientError};

Expand Down Expand Up @@ -32,7 +37,7 @@ impl BriaClient {
})
}

pub fn inject_auth_token<T>(
pub fn inject_headers<T>(
&self,
mut request: tonic::Request<T>,
) -> Result<tonic::Request<T>, BriaClientError> {
Expand All @@ -41,6 +46,18 @@ impl BriaClient {
PROFILE_API_KEY_HEADER,
tonic::metadata::MetadataValue::try_from(profile_api_key)?,
);

let context = tracing::Span::current().context();
let propagator = TraceContextPropagator::new();
let mut headers = HashMap::new();
propagator.inject_context(&context, &mut headers);
for (key, value) in headers {
request.metadata_mut().insert(
tonic::metadata::MetadataKey::from_bytes(key.as_bytes())?,
tonic::metadata::MetadataValue::try_from(value.as_str())?,
);
}

Ok(request)
}

Expand All @@ -54,7 +71,7 @@ impl BriaClient {

match self
.proto_client
.get_address(self.inject_auth_token(request)?)
.get_address(self.inject_headers(request)?)
.await
{
Ok(response) => {
Expand All @@ -75,7 +92,7 @@ impl BriaClient {

let response = self
.proto_client
.new_address(self.inject_auth_token(request)?)
.new_address(self.inject_headers(request)?)
.await?;
Ok(OnchainAddress {
address: response.into_inner().address,
Expand Down Expand Up @@ -116,7 +133,7 @@ impl BriaClient {

let response = self
.proto_client
.submit_payout(self.inject_auth_token(request)?)
.submit_payout(self.inject_headers(request)?)
.await?;
Ok(response.into_inner().id)
}
Expand Down
7 changes: 6 additions & 1 deletion bria-client/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use thiserror::Error;
use tonic::{metadata::errors::InvalidMetadataValue, transport};
use tonic::{
metadata::errors::{InvalidMetadataKey, InvalidMetadataValue},
transport,
};

#[derive(Error, Debug)]
pub enum BriaClientError {
Expand All @@ -13,4 +16,6 @@ pub enum BriaClientError {
CouldNotConvertSatoshisToU64,
#[error("Tonic Error: {0}")]
TonicError(#[from] tonic::Status),
#[error("Invalid Metadata Key: {0}")]
InvalidMetadataKey(#[from] InvalidMetadataKey),
}

0 comments on commit c9e29d5

Please sign in to comment.