Skip to content

Commit

Permalink
feat(deps): upgrade to opentelemetry 0.27 (#181)
Browse files Browse the repository at this point in the history
> I followed the migration guide described in open-telemetry/opentelemetry-rust#2221
  • Loading branch information
msuret authored Nov 17, 2024
1 parent 0e5912d commit 8085d6d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 60 deletions.
18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ axum = { version = "0.7", default-features = false }
http = "^1"
hyper = "1"
insta = { version = "1.40", features = ["yaml", "redactions"] }
opentelemetry = { version = "0.26", features = [
opentelemetry = { version = "0.27", features = [
"trace",
], default-features = false }
opentelemetry_sdk = { version = "0.26", default-features = false, features = [
opentelemetry_sdk = { version = "0.27", default-features = false, features = [
"rt-tokio",
] }
opentelemetry-aws = { version = "0.14", default-features = false }
opentelemetry-jaeger-propagator = { version = "0.26", default-features = false }
opentelemetry-jaeger-propagator = { version = "0.27", default-features = false }
opentelemetry-resource-detectors = { version = "0.5", default-features = false }
opentelemetry-semantic-conventions = { version = "0.26", default-features = false }
opentelemetry-zipkin = { version = "0.26", default-features = false }
opentelemetry-otlp = { version = "0.26", default-features = false }
opentelemetry-proto = { version = "0.26", default-features = false }
opentelemetry-stdout = { version = "0.26" }
opentelemetry-semantic-conventions = { version = "0.27", default-features = false }
opentelemetry-zipkin = { version = "0.27", default-features = false }
opentelemetry-otlp = { version = "0.27", default-features = false }
opentelemetry-proto = { version = "0.27", default-features = false }
opentelemetry-stdout = { version = "0.27" }
rstest = "0.23"
tokio = { version = "1", default-features = false }
tokio-stream = { version = "0.1", default-features = false }
tonic = { version = "0.12", default-features = false } #should be sync with opentelemetry-proto
tower = { version = "0.5", default-features = false }
tracing = "0.1"
tracing-opentelemetry = "0.27"
tracing-opentelemetry = "0.28"

[profile.dev.package.insta]
opt-level = 3
Expand Down
37 changes: 20 additions & 17 deletions fake-opentelemetry-collector/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::time::{Duration, Instant};

use futures::StreamExt;
use opentelemetry::trace::TracerProvider;
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_otlp::{LogExporter, SpanExporter, WithExportConfig};
use opentelemetry_proto::tonic::collector::logs::v1::logs_service_server::LogsServiceServer;
use opentelemetry_proto::tonic::collector::trace::v1::trace_service_server::TraceServiceServer;
use tokio::sync::mpsc;
Expand Down Expand Up @@ -98,30 +98,33 @@ async fn recv_many<T>(rx: &mut Receiver<T>, at_least: usize, timeout: Duration)
pub async fn setup_tracer(fake_server: &FakeCollectorServer) -> opentelemetry_sdk::trace::Tracer {
// if the environment variable is set (in test or in caller), `with_endpoint` value is ignored
std::env::remove_var("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT");
opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint(fake_server.endpoint()),

opentelemetry_sdk::trace::TracerProvider::builder()
.with_batch_exporter(
SpanExporter::builder()
.with_tonic()
.with_endpoint(fake_server.endpoint())
.build()
.expect("failed to install tracer"),
opentelemetry_sdk::runtime::Tokio,
)
.install_batch(opentelemetry_sdk::runtime::Tokio)
.expect("failed to install tracer")
.build()
.tracer("")
}

pub async fn setup_logger(
fake_server: &FakeCollectorServer,
) -> opentelemetry_sdk::logs::LoggerProvider {
opentelemetry_otlp::new_pipeline()
.logging()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint(fake_server.endpoint()),
opentelemetry_sdk::logs::LoggerProvider::builder()
//Install simple so we don't have to wait for batching in tests
.with_simple_exporter(
LogExporter::builder()
.with_tonic()
.with_endpoint(fake_server.endpoint())
.build()
.expect("failed to install logging"),
)
.install_simple() //Install simple so we don't have to wait for batching in tests
.expect("failed to install logging")
.build()
}

#[cfg(test)]
Expand Down
47 changes: 21 additions & 26 deletions init-tracing-opentelemetry/src/otlp.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use std::{collections::HashMap, str::FromStr};

use opentelemetry::trace::TraceError;
use opentelemetry_otlp::SpanExporterBuilder;
use opentelemetry_otlp::{SpanExporter, WithHttpConfig};
use opentelemetry_sdk::{trace::Sampler, trace::TracerProvider, Resource};
#[cfg(feature = "tls")]
use tonic::transport::ClientTlsConfig;
use {opentelemetry_otlp::WithTonicConfig, tonic::transport::ClientTlsConfig};

#[must_use]
pub fn identity(
v: opentelemetry_otlp::OtlpTracePipeline<SpanExporterBuilder>,
) -> opentelemetry_otlp::OtlpTracePipeline<SpanExporterBuilder> {
pub fn identity(v: opentelemetry_sdk::trace::Builder) -> opentelemetry_sdk::trace::Builder {
v
}

Expand All @@ -19,9 +17,7 @@ pub fn init_tracerprovider<F>(
transform: F,
) -> Result<TracerProvider, TraceError>
where
F: FnOnce(
opentelemetry_otlp::OtlpTracePipeline<SpanExporterBuilder>,
) -> opentelemetry_otlp::OtlpTracePipeline<SpanExporterBuilder>,
F: FnOnce(opentelemetry_sdk::trace::Builder) -> opentelemetry_sdk::trace::Builder,
{
use opentelemetry_otlp::WithExportConfig;

Expand All @@ -30,35 +26,34 @@ where
infer_protocol_and_endpoint(maybe_protocol.as_deref(), maybe_endpoint.as_deref());
tracing::debug!(target: "otel::setup", OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = endpoint);
tracing::debug!(target: "otel::setup", OTEL_EXPORTER_OTLP_TRACES_PROTOCOL = protocol);
let exporter: SpanExporterBuilder = match protocol.as_str() {
"http/protobuf" => opentelemetry_otlp::new_exporter()
.http()
let exporter: SpanExporter = match protocol.as_str() {
"http/protobuf" => SpanExporter::builder()
.with_http()
.with_endpoint(endpoint)
.with_headers(read_headers_from_env())
.into(),
.build()?,
#[cfg(feature = "tls")]
"grpc/tls" => opentelemetry_otlp::new_exporter()
.tonic()
"grpc/tls" => SpanExporter::builder()
.with_tonic()
.with_tls_config(ClientTlsConfig::new().with_native_roots())
.with_endpoint(endpoint)
.into(),
_ => opentelemetry_otlp::new_exporter()
.tonic()
.build()?,
_ => SpanExporter::builder()
.with_tonic()
.with_endpoint(endpoint)
.into(),
.build()?,
};

let mut pipeline = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(exporter)
.with_trace_config(
let mut trace_provider: opentelemetry_sdk::trace::Builder = TracerProvider::builder()
.with_config(
opentelemetry_sdk::trace::Config::default()
.with_resource(resource)
.with_sampler(read_sampler_from_env()),
);
pipeline = transform(pipeline);
let provider = pipeline.install_batch(opentelemetry_sdk::runtime::Tokio)?;
Ok(provider)
)
.with_batch_exporter(exporter, opentelemetry_sdk::runtime::Tokio);

trace_provider = transform(trace_provider);
Ok(trace_provider.build())
}

/// turn a string of "k1=v1,k2=v2,..." into an iterator of (key, value) tuples
Expand Down
14 changes: 6 additions & 8 deletions init-tracing-opentelemetry/src/stdio.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use opentelemetry::trace::{TraceError, TracerProvider as _};
use opentelemetry::InstrumentationLibrary;
use opentelemetry::InstrumentationScope;
use opentelemetry_sdk::trace as sdktrace;
use opentelemetry_sdk::trace::BatchSpanProcessor;
use opentelemetry_sdk::trace::TracerProvider;
Expand Down Expand Up @@ -31,13 +31,11 @@ where
);
provider_builder = transform(provider_builder);
// tracer used in libraries/crates that optionally includes version and schema url
let library = std::sync::Arc::new(
InstrumentationLibrary::builder(env!("CARGO_PKG_NAME"))
.with_version(env!("CARGO_PKG_VERSION"))
.with_schema_url("https://opentelemetry.io/schema/1.0.0")
.build(),
);
Ok(provider_builder.build().library_tracer(library))
let scope = InstrumentationScope::builder(env!("CARGO_PKG_NAME"))
.with_version(env!("CARGO_PKG_VERSION"))
.with_schema_url("https://opentelemetry.io/schema/1.0.0")
.build();
Ok(provider_builder.build().tracer_with_scope(scope))
}

#[derive(Debug, Default)]
Expand Down

0 comments on commit 8085d6d

Please sign in to comment.