Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented callstack capture flow for a unit test #35

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ jobs:
- name: Install Protoc
uses: arduino/setup-protoc@v2

- name: Install libunwind-dev
uses: ConorMacBride/install-package@v1
with:
apt: libunwind-dev

- name: Environment
run: |
cargo --version
Expand Down Expand Up @@ -69,6 +74,10 @@ jobs:
- name: Install Protoc
uses: arduino/setup-protoc@v2

- name: Install libunwind-dev
run: |
sudo apt update && sudo apt install -y libunwind-dev

- name: Environment
run: |
cargo --version
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/target
**/target
**/*.rs.bk
.vs
packages
Expand Down
4 changes: 4 additions & 0 deletions .idea/proxide.iml

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

120 changes: 120 additions & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ wildmatch = "1"
glob = "0.3"
shell-words = "1"

[target.'cfg(unix)'.dependencies]
rstack = "0.3.3"

[dev-dependencies]
portpicker = "0.1.1"
grpc-tester = { version = "0.1.0", path = "test/rust_grpc"}
serial_test = "2.0.0"
lazy_static = "1.4.0"

[target.'cfg(unix)'.dev-dependencies]
rstack-self = "0.3.0"
rstack-launcher = { version = "0.1.0", path = "test/rstack-launcher" }

[profile.release]
debug = true
68 changes: 68 additions & 0 deletions src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use http::{HeaderMap, HeaderValue};
use snafu::{ResultExt, Snafu};
use std::convert::TryFrom;
use std::net::SocketAddr;
use std::sync::mpsc::Sender;
use std::sync::Arc;
Expand Down Expand Up @@ -124,6 +126,58 @@ impl<TClient, TServer> Streams<TClient, TServer>
}
}

/// When available, identifies the thread in the calling or client process.
/// The client should reports its process id with the proxide-client-process-id" header and
/// the thread id with the "proxide-client-thread-id" header.
/// This enables the proxide proxy to capture client's callstack when it is making the call if the proxide
/// and the client are running on the same host.
pub struct ClientThreadId
{
process_id: u32,
thread_id: i64,
}

impl ClientThreadId
{
pub fn process_id(&self) -> u32
{
self.process_id
}

pub fn thread_id(&self) -> i64
{
self.thread_id
}
}

impl TryFrom<&MessageData> for ClientThreadId
{
type Error = ();

fn try_from(value: &MessageData) -> std::result::Result<Self, Self::Error>
{
ClientThreadId::try_from(&value.headers)
}
}

impl TryFrom<&HeaderMap> for ClientThreadId
{
type Error = ();

fn try_from(value: &HeaderMap) -> std::result::Result<Self, Self::Error>
{
let process_id: Option<u32> = number_or_none(&value.get("proxide-client-process-id"));
let thread_id: Option<i64> = number_or_none(&value.get("proxide-client-thread-id"));
match (process_id, thread_id) {
(Some(process_id), Some(thread_id)) => Ok(ClientThreadId {
process_id,
thread_id,
}),
_ => Err(()),
}
}
}

/// Handles a single client connection.
///
/// The connection handling is split into multiple functions, but the functions are chained in a
Expand Down Expand Up @@ -311,3 +365,17 @@ where
log::info!("Exit");
});
}

fn number_or_none<N>(header: &Option<&HeaderValue>) -> Option<N>
where
N: std::str::FromStr,
{
if let Some(value) = header {
value
.to_str()
.map(|s| N::from_str(s).map(|n| Some(n)).unwrap_or(None))
.unwrap_or(None)
} else {
None
}
}
Loading
Loading