ffi: Simplify OTLP tracing initialization

This commit is contained in:
Jonas Platte
2023-07-27 13:26:41 +02:00
committed by Jonas Platte
parent ab363aa420
commit 8703fea24f

View File

@@ -2,12 +2,8 @@ use std::collections::HashMap;
#[cfg(not(target_os = "android"))]
use std::io;
#[cfg(target_os = "android")]
use android as platform_impl;
use base64::{engine::general_purpose::STANDARD, Engine};
use futures_core::future::BoxFuture;
#[cfg(target_os = "ios")]
use ios as platform_impl;
use opentelemetry::{
sdk::{
trace::{BatchMessage, TraceRuntime, Tracer},
@@ -17,8 +13,6 @@ use opentelemetry::{
KeyValue,
};
use opentelemetry_otlp::{Protocol, WithExportConfig};
#[cfg(not(any(target_os = "ios", target_os = "android")))]
use other as platform_impl;
use tokio::runtime::Handle;
#[cfg(not(target_os = "android"))]
use tracing_subscriber::fmt;
@@ -113,7 +107,7 @@ fn setup_tracing_impl(configuration: String) {
#[cfg(target_os = "android")]
pub fn setup_tracing_impl(configuration: String) {
android::log_panics();
log_panics();
tracing_subscriber::registry()
.with(EnvFilter::new(configuration))
@@ -125,14 +119,15 @@ pub fn setup_tracing_impl(configuration: String) {
}
#[cfg(not(target_os = "android"))]
fn setup_otlp_tracing_helper(
fn setup_otlp_tracing_impl(
configuration: String,
colors: bool,
client_name: String,
user: String,
password: String,
otlp_endpoint: String,
) -> anyhow::Result<()> {
let colors = !cfg!(target_os = "ios");
let otlp_tracer = create_otlp_tracer(user, password, otlp_endpoint, client_name)?;
let otlp_layer = tracing_opentelemetry::layer().with_tracer(otlp_tracer);
@@ -152,77 +147,34 @@ fn setup_otlp_tracing_helper(
}
#[cfg(target_os = "android")]
mod android {
use tracing_subscriber::{prelude::*, EnvFilter};
pub fn setup_otlp_tracing_impl(
configuration: String,
client_name: String,
user: String,
password: String,
otlp_endpoint: String,
) -> anyhow::Result<()> {
log_panics();
pub fn log_panics() {
std::env::set_var("RUST_BACKTRACE", "1");
log_panics::init();
}
let otlp_tracer = super::create_otlp_tracer(user, password, otlp_endpoint, client_name)?;
let otlp_layer = tracing_opentelemetry::layer().with_tracer(otlp_tracer);
pub fn setup_otlp_tracing(
configuration: String,
client_name: String,
user: String,
password: String,
otlp_endpoint: String,
) -> anyhow::Result<()> {
log_panics();
tracing_subscriber::registry()
.with(EnvFilter::new(configuration))
.with(
tracing_android::layer("org.matrix.rust.sdk")
.expect("Could not configure the Android tracing layer"),
)
.with(otlp_layer)
.init();
let otlp_tracer = super::create_otlp_tracer(user, password, otlp_endpoint, client_name)?;
let otlp_layer = tracing_opentelemetry::layer().with_tracer(otlp_tracer);
tracing_subscriber::registry()
.with(EnvFilter::new(configuration))
.with(
tracing_android::layer("org.matrix.rust.sdk")
.expect("Could not configure the Android tracing layer"),
)
.with(otlp_layer)
.init();
Ok(())
}
Ok(())
}
#[cfg(target_os = "ios")]
mod ios {
pub fn setup_otlp_tracing(
configuration: String,
client_name: String,
user: String,
password: String,
otlp_endpoint: String,
) -> anyhow::Result<()> {
super::setup_otlp_tracing_helper(
configuration,
false,
client_name,
user,
password,
otlp_endpoint,
)
}
}
#[cfg(not(any(target_os = "ios", target_os = "android")))]
mod other {
pub fn setup_otlp_tracing(
configuration: String,
client_name: String,
user: String,
password: String,
otlp_endpoint: String,
) -> anyhow::Result<()> {
super::setup_otlp_tracing_helper(
configuration,
true,
client_name,
user,
password,
otlp_endpoint,
)
}
#[cfg(target_os = "android")]
pub fn log_panics() {
std::env::set_var("RUST_BACKTRACE", "1");
log_panics::init();
}
#[uniffi::export]
@@ -238,6 +190,6 @@ pub fn setup_otlp_tracing(
password: String,
otlp_endpoint: String,
) {
platform_impl::setup_otlp_tracing(filter, client_name, user, password, otlp_endpoint)
setup_otlp_tracing_impl(filter, client_name, user, password, otlp_endpoint)
.expect("Couldn't configure the OpenTelemetry tracer")
}