Skip to content

Commit

Permalink
Merge pull request #141 from 4t145/support-set-tracing-service-name
Browse files Browse the repository at this point in the history
support tracing service name
  • Loading branch information
4t145 authored Sep 5, 2024
2 parents 75931d7 + a650f65 commit e5e9b59
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 35 deletions.
2 changes: 1 addition & 1 deletion examples/tracing-otlp/config/conf-default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ level = "debug"
endpoint = "http://localhost:4317"
# protocol = "http/protobuf"
protocol = "grpc"
server_name = "tracing示例"
server_name = "tracing-示例"
1 change: 0 additions & 1 deletion examples/tracing-otlp/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub struct Api;

#[poem_openapi::OpenApi]
impl Api {
#[instrument]
#[oai(path = "/send_email", method = "get")]
async fn send_email(&self) -> TardisApiResult<String> {
let params = gen_params(&TaskKind::SendEmail);
Expand Down
69 changes: 37 additions & 32 deletions tardis/src/basic/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ where
TardisTracing { configure: configer_list }
}
}

#[cfg(feature = "tracing")]
pub(crate) fn tracing_service_name() -> String {
std::env::var_os(OTEL_SERVICE_NAME).map(|s| s.to_string_lossy().to_string()).unwrap_or_else(|| "tardis-tracing".to_string())
}
impl TardisTracing<LogConfig> {
/// Get an initializer for tardis tracing
pub fn initializer() -> TardisTracingInitializer<Registry, LogConfig> {
Expand Down Expand Up @@ -249,39 +252,41 @@ impl TardisTracing<LogConfig> {
#[cfg(feature = "tracing")]
fn create_otlp_tracer() -> opentelemetry_sdk::trace::Tracer {
use crate::config::config_dto::OtlpProtocol;
use std::sync::OnceLock;
static INITIALIZED: OnceLock<opentelemetry_sdk::trace::TracerProvider> = OnceLock::new();
use opentelemetry::trace::TracerProvider;
INITIALIZED
.get_or_init(|| {
tracing::debug!("[Tardis.Tracing] Initializing otlp tracer");
let protocol = std::env::var(OTEL_EXPORTER_OTLP_PROTOCOL).ok().map(|s| s.parse::<OtlpProtocol>().unwrap_or_default()).unwrap_or_default();
let tracer = opentelemetry_otlp::new_pipeline().tracing();
let tracer = match protocol {
OtlpProtocol::Grpc => {
let mut exporter = opentelemetry_otlp::new_exporter().tonic();
// Check if we need TLS
if let Ok(endpoint) = std::env::var(OTEL_EXPORTER_OTLP_ENDPOINT) {
if endpoint.to_lowercase().starts_with("https") {
exporter = exporter.with_tls_config(Default::default());
}
}
tracer.with_exporter(exporter)
tracing::debug!("[Tardis.Tracing] Initializing otlp tracer");
let protocol = std::env::var(OTEL_EXPORTER_OTLP_PROTOCOL).ok().map(|s| s.parse::<OtlpProtocol>().unwrap_or_default()).unwrap_or_default();
let tracer = opentelemetry_otlp::new_pipeline().tracing();
let tracer = match protocol {
OtlpProtocol::Grpc => {
let mut exporter = opentelemetry_otlp::new_exporter().tonic();
// Check if we need TLS
if let Ok(endpoint) = std::env::var(OTEL_EXPORTER_OTLP_ENDPOINT) {
if endpoint.to_lowercase().starts_with("https") {
exporter = exporter.with_tls_config(Default::default());
}
OtlpProtocol::HttpProtobuf => {
let headers = Self::parse_otlp_headers_from_env();
let exporter = opentelemetry_otlp::new_exporter().http().with_headers(headers.into_iter().collect());
tracer.with_exporter(exporter)
}
};
tracing::debug!("[Tardis.Tracing] Batch installing tracer. If you are blocked here, try running tokio in multithread.");
let provider = tracer.install_batch(opentelemetry_sdk::runtime::Tokio).expect("fail to install otlp tracer");
tracing::debug!("[Tardis.Tracing] Initialized otlp tracer");
opentelemetry::global::set_tracer_provider(provider.clone());
provider
})
.clone()
.tracer("")
}
tracer.with_exporter(exporter)
}
OtlpProtocol::HttpProtobuf => {
let headers = Self::parse_otlp_headers_from_env();
let exporter = opentelemetry_otlp::new_exporter().http().with_headers(headers.into_iter().collect());
tracer.with_exporter(exporter)
}
};
tracing::debug!("[Tardis.Tracing] Batch installing tracer. If you are blocked here, try running tokio in multithread.");
let provider = tracer
.with_trace_config(
opentelemetry_sdk::trace::Config::default()
.with_resource(opentelemetry_sdk::Resource::new([opentelemetry::KeyValue::new("service.name", tracing_service_name())])),
)
.install_batch(opentelemetry_sdk::runtime::Tokio)
.expect("fail to install otlp tracer");
tracing::debug!("[Tardis.Tracing] Initialized otlp tracer");
opentelemetry::global::shutdown_tracer_provider();
opentelemetry::global::set_tracer_provider(provider.clone());
provider.tracer(tracing_service_name())


}

#[cfg(feature = "tracing")]
Expand Down
2 changes: 1 addition & 1 deletion tardis/src/web/web_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ impl TardisWebServer {
let route = route.with(middleware);
#[cfg(feature = "tracing")]
let route = {
let tracer = opentelemetry::global::tracer("");
let tracer = opentelemetry::global::tracer(crate::basic::tracing::tracing_service_name());
route.with(poem::middleware::OpenTelemetryTracing::new(tracer))
};
if module_options.uniform_error || module_config.uniform_error {
Expand Down

0 comments on commit e5e9b59

Please sign in to comment.