Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace OtlpPipeline with exporter builders #2221

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions examples/self-diagnostics/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use opentelemetry::global::{self, set_error_handler, Error as OtelError};
use opentelemetry::KeyValue;
use opentelemetry_appender_tracing::layer;
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_otlp::{LogExporter, MetricExporter, WithExporterConfig};
use opentelemetry_sdk::metrics::PeriodicReader;
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
use tracing_subscriber::fmt;
use tracing_subscriber::prelude::*;
Expand Down Expand Up @@ -51,15 +52,16 @@ fn custom_error_handler(err: OtelError) {
}

fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider {
let provider = opentelemetry_otlp::new_pipeline()
.logging()
.with_exporter(
opentelemetry_otlp::new_exporter()
.http()
.with_endpoint("http://localhost:4318/v1/logs"),
)
.install_batch(opentelemetry_sdk::runtime::Tokio)
let exporter = LogExporter::builder()
.with_http()
.with_endpoint("http://localhost:4318/v1/logs")
.build()
.unwrap();

let provider = opentelemetry_sdk::logs::LoggerProvider::builder()
.with_batch_exporter(exporter, opentelemetry_sdk::runtime::Tokio)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this direction.

.build();

let cloned_provider = provider.clone();

// Add a tracing filter to filter events from crates used by opentelemetry-otlp.
Expand Down Expand Up @@ -107,16 +109,20 @@ fn init_logger_provider() -> opentelemetry_sdk::logs::LoggerProvider {
}

fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
let provider = opentelemetry_otlp::new_pipeline()
.metrics(opentelemetry_sdk::runtime::Tokio)
.with_period(std::time::Duration::from_secs(1))
.with_exporter(
opentelemetry_otlp::new_exporter()
.http()
.with_endpoint("http://localhost:4318/v1/metrics"),
)
let exporter = MetricExporter::builder()
.with_tonic()
.with_endpoint("http://localhost:4318/v1/metrics")
.build()
.unwrap();

let reader = PeriodicReader::builder(exporter, opentelemetry_sdk::runtime::Tokio)
.with_interval(std::time::Duration::from_secs(1))
.build();

let provider = opentelemetry_sdk::metrics::SdkMeterProvider::builder()
.with_reader(reader)
.build();

let cloned_provider = provider.clone();
global::set_meter_provider(cloned_provider);
provider
Expand Down
19 changes: 9 additions & 10 deletions examples/tracing-jaeger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@ use opentelemetry::{
trace::{TraceContextExt, TraceError, Tracer},
KeyValue,
};
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::trace::TracerProvider;
use opentelemetry_sdk::{runtime, trace as sdktrace, Resource};
use opentelemetry_semantic_conventions::resource::SERVICE_NAME;

use std::error::Error;

fn init_tracer_provider() -> Result<opentelemetry_sdk::trace::TracerProvider, TraceError> {
opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint("http://localhost:4317"),
)
.with_trace_config(
let exporter = opentelemetry_otlp::SpanExporter::builder()
.with_tonic()
.build()?;

Ok(TracerProvider::builder()
.with_batch_exporter(exporter, runtime::Tokio)
.with_config(
sdktrace::Config::default().with_resource(Resource::new(vec![KeyValue::new(
SERVICE_NAME,
"tracing-jaeger",
)])),
)
.install_batch(runtime::Tokio)
.build())
}

#[tokio::main]
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-otlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ opentelemetry_sdk = { features = ["trace", "rt-tokio", "testing"], path = "../op
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
futures-util = { workspace = true }
temp-env = { workspace = true }
pretty_assertions = "1.4.1"

[features]
# telemetry pillars and functions
Expand Down
75 changes: 41 additions & 34 deletions opentelemetry-otlp/examples/basic-otlp-http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ use opentelemetry::{
KeyValue,
};
use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
use opentelemetry_otlp::Protocol;
use opentelemetry_otlp::{HttpExporterBuilder, WithExportConfig};
use opentelemetry_sdk::trace::{self as sdktrace, Config};
use opentelemetry_otlp::WithExporterConfig;
use opentelemetry_otlp::{LogExporter, MetricExporter, Protocol, SpanExporter};
use opentelemetry_sdk::{
logs::LoggerProvider,
metrics::{PeriodicReader, SdkMeterProvider},
runtime,
trace::{self as sdktrace, TracerProvider},
};
use opentelemetry_sdk::{
logs::{self as sdklogs},
Resource,
Expand All @@ -18,6 +23,9 @@ use tracing::info;
use tracing_subscriber::prelude::*;
use tracing_subscriber::EnvFilter;

#[cfg(feature = "hyper")]
use opentelemetry_otlp::WithHttpConfig;

#[cfg(feature = "hyper")]
mod hyper;

Expand All @@ -28,47 +36,46 @@ static RESOURCE: Lazy<Resource> = Lazy::new(|| {
)])
});

fn http_exporter() -> HttpExporterBuilder {
let exporter = opentelemetry_otlp::new_exporter().http();
fn init_logs() -> Result<sdklogs::LoggerProvider, opentelemetry::logs::LogError> {
let exporter_builder = LogExporter::builder()
.with_http()
.with_endpoint("http://localhost:4318/v1/logs")
.with_protocol(Protocol::HttpBinary);

#[cfg(feature = "hyper")]
let exporter = exporter.with_http_client(hyper::HyperClient::default());
exporter
}
let exporter_builder = exporter_builder.with_http_client(hyper::HyperClient::default());

fn init_logs() -> Result<sdklogs::LoggerProvider, opentelemetry::logs::LogError> {
opentelemetry_otlp::new_pipeline()
.logging()
let exporter = exporter_builder.build()?;

Ok(LoggerProvider::builder()
.with_batch_exporter(exporter, runtime::Tokio)
.with_resource(RESOURCE.clone())
.with_exporter(
http_exporter()
.with_protocol(Protocol::HttpBinary) //can be changed to `Protocol::HttpJson` to export in JSON format
.with_endpoint("http://localhost:4318/v1/logs"),
)
.install_batch(opentelemetry_sdk::runtime::Tokio)
.build())
}

fn init_tracer_provider() -> Result<sdktrace::TracerProvider, TraceError> {
opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
http_exporter()
.with_protocol(Protocol::HttpBinary) //can be changed to `Protocol::HttpJson` to export in JSON format
.with_endpoint("http://localhost:4318/v1/traces"),
)
.with_trace_config(Config::default().with_resource(RESOURCE.clone()))
.install_batch(opentelemetry_sdk::runtime::Tokio)
let exporter = SpanExporter::builder()
.with_http()
.with_protocol(Protocol::HttpBinary) //can be changed to `Protocol::HttpJson` to export in JSON format
.with_endpoint("http://localhost:4318/v1/traces")
.build()?;
Ok(TracerProvider::builder()
.with_batch_exporter(exporter, runtime::Tokio)
.with_resource(RESOURCE.clone())
.build())
}

fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, MetricsError> {
opentelemetry_otlp::new_pipeline()
.metrics(opentelemetry_sdk::runtime::Tokio)
.with_exporter(
http_exporter()
.with_protocol(Protocol::HttpBinary) //can be changed to `Protocol::HttpJson` to export in JSON format
.with_endpoint("http://localhost:4318/v1/metrics"),
)
let exporter = MetricExporter::builder()
.with_http()
.with_protocol(Protocol::HttpBinary) //can be changed to `Protocol::HttpJson` to export in JSON format
.with_endpoint("http://localhost:4318/v1/metrics")
.build()?;

Ok(SdkMeterProvider::builder()
.with_reader(PeriodicReader::builder(exporter, runtime::Tokio).build())
.with_resource(RESOURCE.clone())
.build()
.build())
}

#[tokio::main]
Expand Down
57 changes: 27 additions & 30 deletions opentelemetry-otlp/examples/basic-otlp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use opentelemetry::{
KeyValue,
};
use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
use opentelemetry_otlp::{ExportConfig, WithExportConfig};
use opentelemetry_otlp::{LogExporter, MetricExporter, SpanExporter, WithExporterConfig};
use opentelemetry_sdk::logs::LoggerProvider;
use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider};
use opentelemetry_sdk::trace::Config;
use opentelemetry_sdk::{runtime, trace as sdktrace, Resource};
use std::error::Error;
Expand All @@ -24,43 +26,38 @@ static RESOURCE: Lazy<Resource> = Lazy::new(|| {
});

fn init_tracer_provider() -> Result<sdktrace::TracerProvider, TraceError> {
opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint("http://localhost:4317"),
)
.with_trace_config(Config::default().with_resource(RESOURCE.clone()))
.install_batch(runtime::Tokio)
let exporter = SpanExporter::builder()
.with_tonic()
.with_endpoint("0.0.0.0:4317")
.build()?;
Ok(sdktrace::TracerProvider::builder()
.with_config(Config::default().with_resource(RESOURCE.clone()))
.with_batch_exporter(exporter, runtime::Tokio)
.build())
}

fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, MetricsError> {
let export_config = ExportConfig {
endpoint: "http://localhost:4317".to_string(),
..ExportConfig::default()
};
opentelemetry_otlp::new_pipeline()
.metrics(runtime::Tokio)
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_export_config(export_config),
)
let exporter = MetricExporter::builder().with_tonic().build()?;

let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();

Ok(SdkMeterProvider::builder()
.with_reader(reader)
.with_resource(RESOURCE.clone())
.build()
.build())
}

fn init_logs() -> Result<opentelemetry_sdk::logs::LoggerProvider, LogError> {
opentelemetry_otlp::new_pipeline()
.logging()
let exporter = LogExporter::builder()
.with_tonic()
.with_protocol(opentelemetry_otlp::Protocol::HttpBinary) //can be changed to `Protocol::HttpJson` to export in JSON format
.with_endpoint("http://localhost:4318/v1/logs")
.build()?;

Ok(LoggerProvider::builder()
.with_resource(RESOURCE.clone())
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint("http://localhost:4317"),
)
.install_batch(runtime::Tokio)
.with_batch_exporter(exporter, runtime::Tokio)
.build())
}

#[tokio::main]
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-otlp/src/exporter/http/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use http::{header::CONTENT_TYPE, Method};
use opentelemetry::metrics::{MetricsError, Result};
use opentelemetry_sdk::metrics::data::ResourceMetrics;

use crate::{metric::MetricsClient, Error};
use crate::{metric::MetricClient, Error};

use super::OtlpHttpClient;

#[async_trait]
impl MetricsClient for OtlpHttpClient {
impl MetricClient for OtlpHttpClient {
async fn export(&self, metrics: &mut ResourceMetrics) -> Result<()> {
let client = self
.client
Expand Down
Loading
Loading