-
-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust): add a span exporter using a secure channel
- Loading branch information
1 parent
5d6ef60
commit 6fb11e6
Showing
9 changed files
with
738 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
implementations/rust/ockam/ockam_api/src/logs/http_forwarder.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::logs::secure_client_service::OckamRequest; | ||
use crate::{ApiError, Result}; | ||
use hyper::{http, Uri}; | ||
use ockam_core::api::{Method, Request}; | ||
use ockam_core::errcode::{Kind, Origin}; | ||
use ockam_core::{async_trait, Routed, Worker}; | ||
use ockam_node::Context; | ||
use std::future; | ||
use tonic::body::BoxBody; | ||
use tonic::client::GrpcService; | ||
use tonic::transport::Channel; | ||
|
||
pub const HTTP_FORWARDER: &str = "http_forwarder"; | ||
|
||
/// The HttpForwarder worker accepts http requests serialized as Ockam messages | ||
/// and forwards them to an HTTP endpoint. | ||
/// | ||
/// Note that we don't wait for a response from the endpoint. | ||
pub struct HttpForwarder { | ||
channel: Channel, | ||
} | ||
|
||
impl HttpForwarder { | ||
/// Create a Channel for the given URI | ||
pub async fn new(uri: Uri) -> Result<Self> { | ||
let channel = Channel::builder(uri) | ||
.connect() | ||
.await | ||
.map_err(ApiError::message)?; | ||
|
||
Ok(Self { channel }) | ||
} | ||
|
||
/// Forward an http Request. | ||
/// We don't wait for a response here. | ||
async fn forward_http_request(&mut self, request: http::Request<BoxBody>) -> Result<()> { | ||
self.ready().await.map_err(ApiError::core)?; | ||
let _ = self | ||
.channel | ||
.call(request) | ||
.await | ||
.map_err(ApiError::message)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Check if the channel is ready before making a call | ||
async fn ready(&mut self) -> Result<()> { | ||
future::poll_fn(|cx| self.channel.poll_ready(cx)) | ||
.await | ||
.map_err(ApiError::message) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Worker for HttpForwarder { | ||
type Message = Request<OckamRequest>; | ||
type Context = Context; | ||
|
||
async fn handle_message( | ||
&mut self, | ||
_ctx: &mut Context, | ||
message: Routed<Request<OckamRequest>>, | ||
) -> ockam_core::Result<()> { | ||
let (header, body) = message.into_body()?.into_parts(); | ||
match (header.method(), header.path(), body) { | ||
// Every posted message must be forwarded | ||
(Some(Method::Post), "/", Some(ockam_request)) => { | ||
let http_request = ockam_request.to_http_request().map_err(|e| { | ||
ockam_core::Error::new(Origin::Api, Kind::Serialization, format!("{e:?}")) | ||
})?; | ||
self.forward_http_request(http_request) | ||
.await | ||
.map_err(|e| ockam_core::Error::new(Origin::Api, Kind::Io, format!("{e:?}")))?; | ||
} | ||
_ => (), | ||
}; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.