Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into 1464

# Conflicts:
#	opentelemetry-otlp/src/lib.rs
  • Loading branch information
TommyCpp committed Jan 29, 2024
2 parents 1bc03b6 + 9ca638d commit 1b1aa56
Show file tree
Hide file tree
Showing 60 changed files with 1,354 additions and 531 deletions.
1 change: 0 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"Lalit",
"msrv",
"Ochtman",
"openetelemetry",
"opentelemetry",
"OTLP",
"protoc",
Expand Down
8 changes: 4 additions & 4 deletions .github/codecov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ ignore:
- "opentelemetry-jaeger/examples"
- "opentelemetry-zipkin/examples"
- "opentelemetry-otlp/examples"
- "opentelemetry-aws/examples"
- "opentelemetry-datadog/examples"
- "opentelemetry-dynatrace/examples"
- "opentelemetry-http/examples"
- "opentelemetry-prometheus/examples"
- "opentelemetry-zpages/examples"
- "opentelemetry-appender-tracing/examples"
- "opentelemetry-appender-log/examples"
# stress test
- "stress"
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ For a deeper discussion, see:

Currently, the Opentelemetry Rust SDK has two ways to handle errors. In the situation where errors are not allowed to return. One should call global error handler to process the errors. Otherwise, one should return the errors.

The Opentelemetry Rust SDK comes with an error type `openetelemetry::Error`. For different function, one error has been defined. All error returned by trace module MUST be wrapped in `opentelemetry::trace::TraceError`. All errors returned by metrics module MUST be wrapped in `opentelemetry::metrics::MetricsError`.
The Opentelemetry Rust SDK comes with an error type `opentelemetry::Error`. For different function, one error has been defined. All error returned by trace module MUST be wrapped in `opentelemetry::trace::TraceError`. All errors returned by metrics module MUST be wrapped in `opentelemetry::metrics::MetricsError`. All errors returned by logs module MUST be wrapped in `opentelemetry::logs::LogsError`.

For users that want to implement their own exporters. It's RECOMMENDED to wrap all errors from the exporter into a crate-level error type, and implement `ExporterError` trait.

Expand Down
16 changes: 7 additions & 9 deletions examples/metrics-basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use opentelemetry::metrics::Unit;
use opentelemetry::AttributeSet;
use opentelemetry::{metrics::MeterProvider as _, KeyValue};
use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider};
use opentelemetry_sdk::{runtime, Resource};
Expand Down Expand Up @@ -52,11 +53,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
observer.observe_u64(
&observable_counter,
100,
[
AttributeSet::from(&[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
]
.as_ref(),
]),
)
})?;

Expand Down Expand Up @@ -84,11 +84,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
observer.observe_i64(
&observable_up_down_counter,
100,
[
AttributeSet::from(&[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
]
.as_ref(),
]),
)
})?;

Expand Down Expand Up @@ -142,11 +141,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
observer.observe_f64(
&observable_gauge,
1.0,
[
AttributeSet::from(&[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
]
.as_ref(),
]),
)
})?;

Expand Down
9 changes: 5 additions & 4 deletions opentelemetry-otlp/examples/basic-otlp-http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use opentelemetry_sdk::metrics as sdkmetrics;
use opentelemetry_sdk::resource;
use opentelemetry_sdk::trace as sdktrace;

use opentelemetry::AttributeSet;
use std::error::Error;
use tracing::info;
use tracing_subscriber::prelude::*;
Expand Down Expand Up @@ -62,13 +63,13 @@ fn init_metrics() -> metrics::Result<sdkmetrics::SdkMeterProvider> {
const LEMONS_KEY: Key = Key::from_static_str("ex.com/lemons");
const ANOTHER_KEY: Key = Key::from_static_str("ex.com/another");

static COMMON_ATTRIBUTES: Lazy<[KeyValue; 4]> = Lazy::new(|| {
[
static COMMON_ATTRIBUTES: Lazy<AttributeSet> = Lazy::new(|| {
AttributeSet::from(&[
LEMONS_KEY.i64(10),
KeyValue::new("A", "1"),
KeyValue::new("B", "2"),
KeyValue::new("C", "3"),
]
])
});

#[tokio::main]
Expand Down Expand Up @@ -104,7 +105,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
info!(target: "my-target", "hello from {}. My price is {}", "apple", 1.99);

let histogram = meter.f64_histogram("ex.com.two").init();
histogram.record(5.5, COMMON_ATTRIBUTES.as_ref());
histogram.record(5.5, COMMON_ATTRIBUTES.clone());

global::shutdown_tracer_provider();
global::shutdown_logger_provider();
Expand Down
11 changes: 6 additions & 5 deletions opentelemetry-otlp/examples/basic-otlp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use opentelemetry::global;
use opentelemetry::global::{logger_provider, shutdown_logger_provider, shutdown_tracer_provider};
use opentelemetry::logs::LogError;
use opentelemetry::trace::TraceError;
use opentelemetry::AttributeSet;
use opentelemetry::{
metrics,
trace::{TraceContextExt, Tracer},
Expand Down Expand Up @@ -72,13 +73,13 @@ fn init_logs() -> Result<opentelemetry_sdk::logs::Logger, LogError> {
const LEMONS_KEY: Key = Key::from_static_str("lemons");
const ANOTHER_KEY: Key = Key::from_static_str("ex.com/another");

static COMMON_ATTRIBUTES: Lazy<[KeyValue; 4]> = Lazy::new(|| {
[
static COMMON_ATTRIBUTES: Lazy<AttributeSet> = Lazy::new(|| {
AttributeSet::from(&[
LEMONS_KEY.i64(10),
KeyValue::new("A", "1"),
KeyValue::new("B", "2"),
KeyValue::new("C", "3"),
]
])
});

#[tokio::main]
Expand Down Expand Up @@ -109,11 +110,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
.init();

meter.register_callback(&[gauge.as_any()], move |observer| {
observer.observe_f64(&gauge, 1.0, COMMON_ATTRIBUTES.as_ref())
observer.observe_f64(&gauge, 1.0, COMMON_ATTRIBUTES.clone())
})?;

let histogram = meter.f64_histogram("ex.com.two").init();
histogram.record(5.5, COMMON_ATTRIBUTES.as_ref());
histogram.record(5.5, COMMON_ATTRIBUTES.clone());

tracer.in_span("operation", |cx| {
let span = cx.span();
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-otlp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
//! [`async-std`]: https://async.rs
//!
//! # Feature Flags
//! The following feature flags can enable exporters for different telemetry pillars:
//! The following feature flags can enable exporters for different telemetry signals:
//!
//! * `trace`: Includes the trace exporters (enabled by default).
//! * `metrics`: Includes the metrics exporters.
Expand Down
21 changes: 18 additions & 3 deletions opentelemetry-otlp/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl OtlpPipeline {
OtlpLogPipeline {
log_config: None,
exporter_builder: NoExporterConfig(()),
batch_config: None,
}
}
}
Expand Down Expand Up @@ -124,6 +125,7 @@ impl opentelemetry_sdk::export::logs::LogExporter for LogExporter {
pub struct OtlpLogPipeline<EB> {
exporter_builder: EB,
log_config: Option<opentelemetry_sdk::logs::Config>,
batch_config: Option<opentelemetry_sdk::logs::BatchConfig>,
}

impl<EB> OtlpLogPipeline<EB> {
Expand All @@ -132,6 +134,12 @@ impl<EB> OtlpLogPipeline<EB> {
self.log_config = Some(log_config);
self
}

/// Set the batch log processor configuration, and it will override the env vars.
pub fn with_batch_config(mut self, batch_config: opentelemetry_sdk::logs::BatchConfig) -> Self {
self.batch_config = Some(batch_config);
self
}
}

impl OtlpLogPipeline<NoExporterConfig> {
Expand All @@ -143,6 +151,7 @@ impl OtlpLogPipeline<NoExporterConfig> {
OtlpLogPipeline {
exporter_builder: pipeline.into(),
log_config: self.log_config,
batch_config: self.batch_config,
}
}
}
Expand All @@ -160,7 +169,7 @@ impl OtlpLogPipeline<LogExporterBuilder> {
))
}

/// Install the configured log exporter and a batch span processor using the
/// Install the configured log exporter and a batch log processor using the
/// specified runtime.
///
/// Returns a [`Logger`] with the name `opentelemetry-otlp` and the current crate version.
Expand All @@ -174,6 +183,7 @@ impl OtlpLogPipeline<LogExporterBuilder> {
self.exporter_builder.build_log_exporter()?,
self.log_config,
runtime,
self.batch_config,
))
}
}
Expand Down Expand Up @@ -202,9 +212,14 @@ fn build_batch_with_exporter<R: RuntimeChannel>(
exporter: LogExporter,
log_config: Option<opentelemetry_sdk::logs::Config>,
runtime: R,
batch_config: Option<opentelemetry_sdk::logs::BatchConfig>,
) -> opentelemetry_sdk::logs::Logger {
let mut provider_builder =
opentelemetry_sdk::logs::LoggerProvider::builder().with_batch_exporter(exporter, runtime);
let mut provider_builder = opentelemetry_sdk::logs::LoggerProvider::builder();
let batch_processor = opentelemetry_sdk::logs::BatchLogProcessor::builder(exporter, runtime)
.with_batch_config(batch_config.unwrap_or_default())
.build();
provider_builder = provider_builder.with_log_processor(batch_processor);

if let Some(config) = log_config {
provider_builder = provider_builder.with_config(config);
}
Expand Down
10 changes: 6 additions & 4 deletions opentelemetry-prometheus/examples/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use hyper::{
Body, Method, Request, Response, Server,
};
use once_cell::sync::Lazy;
use opentelemetry::AttributeSet;
use opentelemetry::{
metrics::{Counter, Histogram, MeterProvider as _, Unit},
KeyValue,
Expand All @@ -14,7 +15,8 @@ use std::convert::Infallible;
use std::sync::Arc;
use std::time::SystemTime;

static HANDLER_ALL: Lazy<[KeyValue; 1]> = Lazy::new(|| [KeyValue::new("handler", "all")]);
static HANDLER_ALL: Lazy<AttributeSet> =
Lazy::new(|| AttributeSet::from(&[KeyValue::new("handler", "all")]));

async fn serve_req(
req: Request<Body>,
Expand All @@ -23,7 +25,7 @@ async fn serve_req(
println!("Receiving request at path {}", req.uri());
let request_start = SystemTime::now();

state.http_counter.add(1, HANDLER_ALL.as_ref());
state.http_counter.add(1, HANDLER_ALL.clone());

let response = match (req.method(), req.uri().path()) {
(&Method::GET, "/metrics") => {
Expand All @@ -33,7 +35,7 @@ async fn serve_req(
encoder.encode(&metric_families, &mut buffer).unwrap();
state
.http_body_gauge
.record(buffer.len() as u64, HANDLER_ALL.as_ref());
.record(buffer.len() as u64, HANDLER_ALL.clone());

Response::builder()
.status(200)
Expand All @@ -53,7 +55,7 @@ async fn serve_req(

state.http_req_histogram.record(
request_start.elapsed().map_or(0.0, |d| d.as_secs_f64()),
&[],
AttributeSet::default(),
);
Ok(response)
}
Expand Down
9 changes: 6 additions & 3 deletions opentelemetry-prometheus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
//! [Prometheus]: https://prometheus.io
//!
//! ```
//! use opentelemetry::{metrics::MeterProvider, KeyValue};
//! use opentelemetry::{AttributeSet, metrics::MeterProvider, KeyValue};
//! use opentelemetry_sdk::metrics::SdkMeterProvider;
//! use prometheus::{Encoder, TextEncoder};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//!
//! // create a new prometheus registry
//! use opentelemetry::AttributeSet;
//! let registry = prometheus::Registry::new();
//!
//! // configure OpenTelemetry to use this registry
Expand All @@ -31,8 +32,10 @@
//! .with_description("Records values")
//! .init();
//!
//! counter.add(100, &[KeyValue::new("key", "value")]);
//! histogram.record(100, &[KeyValue::new("key", "value")]);
//! let attributes = AttributeSet::from(&[KeyValue::new("key", "value")]);
//!
//! counter.add(100, attributes.clone());
//! histogram.record(100, attributes);
//!
//! // Encode data as text or protobuf
//! let encoder = TextEncoder::new();
Expand Down
Loading

0 comments on commit 1b1aa56

Please sign in to comment.