Skip to content

Commit

Permalink
Change Into -> From impl traits
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelff committed Jan 18, 2023
1 parent 8fb2ae5 commit c2dd630
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions query-engine/core/src/interactive_transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Display for TxId {
}
}

impl Into<opentelemetry::Context> for TxId {
impl From<TxId> for opentelemetry::Context {
// This is a bit of a hack, but it's the only way to have a default trace span for a whole
// transaction when no traceparent is propagated from the client.
//
Expand All @@ -87,16 +87,16 @@ impl Into<opentelemetry::Context> for TxId {
//
// By generating this "fake" traceparent based on the transaction id, we can have a common trace_id
// for all transaction operations.
fn into(self) -> opentelemetry::Context {
let trace_id: opentelemetry::trace::TraceId = self.into();
fn from(id: TxId) -> Self {
let trace_id: opentelemetry::trace::TraceId = id.into();
let traceparent = format!("00-{}-0000000000000001-01", trace_id);

let extractor: HashMap<String, String> = HashMap::from_iter(vec![("traceparent".to_string(), traceparent)]);
opentelemetry::global::get_text_map_propagator(|propagator| propagator.extract(&extractor))
}
}

impl Into<opentelemetry::trace::TraceId> for TxId {
impl From<TxId> for opentelemetry::trace::TraceId {
// in order to convert a TxId (a 48 bytes cuid) into a TraceId (16 bytes), we remove the first byte,
// (always 'c') and get the next 16 bytes, which are random enough to be used as a trace id.
// this is a typical cuid: "c-lct0q6ma-0004-rb04-h6en1roa"
Expand All @@ -108,9 +108,9 @@ impl Into<opentelemetry::trace::TraceId> for TxId {
// - least significative 8 bytes. Totally random.
//
// We want the most entropic slice of 16 bytes that's deterministicly determined
fn into(self) -> opentelemetry::trace::TraceId {
fn from(id: TxId) -> Self {
let mut buffer = [0; 16];
let tx_id_bytes = self.0.as_bytes();
let tx_id_bytes = id.0.as_bytes();
let len = tx_id_bytes.len();

// bytes [len-20 to len-12): least significative 4 bytes of the timestamp + 4 bytes counter
Expand Down
4 changes: 2 additions & 2 deletions query-engine/query-engine/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ async fn transaction_commit_handler(
span: _,
trace_id: _,
capture_config,
} = ServerExecutionContext::builder(&req.headers())
} = ServerExecutionContext::builder(req.headers())
.with_tx_id(tx_id.clone())
.build();

Expand Down Expand Up @@ -359,7 +359,7 @@ async fn transaction_rollback_handler(
span: _,
trace_id: _,
capture_config,
} = ServerExecutionContext::builder(&req.headers())
} = ServerExecutionContext::builder(req.headers())
.with_tx_id(tx_id.clone())
.build();

Expand Down

0 comments on commit c2dd630

Please sign in to comment.