add exports to tracing-util (#387)

<!-- Thank you for submitting this PR! :) -->

## Description

We don't want `v3-engine-multitenant` to import any opentelemetry
libraries directly, so we add more functions and re-exports here
instead.

V3_GIT_ORIGIN_REV_ID: 2e38b9b1ba6fda23d1bfe8e20549c3a95bd5a2fa
This commit is contained in:
Daniel Harvey 2024-03-25 09:48:24 +00:00 committed by hasura-bot
parent a34e0a23e1
commit e68db9136d
4 changed files with 36 additions and 5 deletions

12
v3/Cargo.lock generated
View File

@ -2155,6 +2155,17 @@ dependencies = [
"urlencoding",
]
[[package]]
name = "opentelemetry-contrib"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4717e7a7b45ef1c0664715a85a8e91ff966b58ffaaccb4d4cc7ff8f15cffd98a"
dependencies = [
"once_cell",
"opentelemetry 0.20.0",
"opentelemetry_api",
]
[[package]]
name = "opentelemetry-http"
version = "0.9.0"
@ -3577,6 +3588,7 @@ dependencies = [
"derive_more",
"http",
"opentelemetry 0.20.0",
"opentelemetry-contrib",
"opentelemetry-http",
"opentelemetry-otlp",
"opentelemetry-semantic-conventions",

View File

@ -11,6 +11,7 @@ bench = false
derive_more = "0.99.17"
# version > 0.20 of opentelemetry doesn't work well with our tracing-utils
opentelemetry = "0.20.0"
opentelemetry-contrib = "0.12.0"
opentelemetry-http = "0.9.0"
opentelemetry-otlp = "0.13.0"
opentelemetry_sdk = { version = "0.20.0", features = ["rt-tokio"] }

View File

@ -8,6 +8,15 @@ pub use crate::http::TraceableHttpResponse;
pub use setup::{shutdown_tracer, start_tracer};
pub use traceable::{ErrorVisibility, Traceable, TraceableError};
pub use tracer::{
add_event_on_active_span, global_tracer, set_attribute_on_active_span,
add_event_on_active_span, get_trace_context, global_tracer, set_attribute_on_active_span,
set_status_on_current_span, AttributeVisibility, SpanVisibility,
};
// re-export things from OpenTelemetry to avoid library users importing their own version and
// risking mismatches and multiple globals
pub use opentelemetry::propagation::text_map_propagator::TextMapPropagator;
pub use opentelemetry::trace::get_active_span;
pub use opentelemetry::trace::Status;
pub use opentelemetry::Context;
pub use opentelemetry_contrib::trace::propagator::trace_context_response::TraceContextResponsePropagator;
pub use opentelemetry_http::HeaderInjector;

View File

@ -1,13 +1,12 @@
use crate::traceable::{ErrorVisibility, Traceable, TraceableError};
use http::HeaderMap;
use opentelemetry::{
global::{self, BoxedTracer},
trace::{get_active_span, FutureExt, SpanRef, TraceContextExt, Tracer as OtelTracer},
Key,
Context, Key,
};
use opentelemetry_http::HeaderExtractor;
use std::{future::Future, pin::Pin};
use crate::traceable::{ErrorVisibility, Traceable, TraceableError};
use std::{collections::HashMap, future::Future, pin::Pin};
#[derive(derive_more::Display)]
pub enum SpanVisibility {
@ -181,6 +180,16 @@ impl Tracer {
}
}
/// Return the current trace context, useful for including it HTTP requests etc
pub fn get_trace_context() -> HashMap<String, String> {
let ctx = Context::current();
let mut trace_headers = HashMap::new();
global::get_text_map_propagator(|propagator| {
propagator.inject_context(&ctx, &mut trace_headers);
});
trace_headers
}
/// Util for accessing the globally installed tracer
pub fn global_tracer() -> Tracer {
Tracer::new(opentelemetry::global::tracer("engine-tracing-util"))