Skip to content

Commit

Permalink
Merge branch 'main' into trace-set-resource
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored May 24, 2024
2 parents 34f5e97 + e699233 commit 5f7a564
Show file tree
Hide file tree
Showing 43 changed files with 584 additions and 536 deletions.
7 changes: 7 additions & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
"Cijo",
"clippy",
"codecov",
"datapoint",
"deque",
"Dirkjan",
"EPYC",
"hasher",
"isahc",
"Isobel",
Expand All @@ -43,6 +45,9 @@
"Lalit",
"LIBCLANG",
"msrv",
"mykey",
"myvalue",
"nocapture",
"Ochtman",
"opentelemetry",
"OTLP",
Expand All @@ -53,8 +58,10 @@
"runtimes",
"rustc",
"shoppingcart",
"struct",
"Tescher",
"tracerprovider",
"updown",
"Zhongyang",
"zipkin"
],
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,8 @@ jobs:
- uses: dtolnay/[email protected]
- name: Patch dependencies versions # some dependencies bump MSRV without major version bump
run: bash ./scripts/patch_dependencies.sh
- name: Run tests
run: cargo --version &&
cargo test --manifest-path=opentelemetry/Cargo.toml --features trace,metrics,testing &&
cargo test --manifest-path=opentelemetry-zipkin/Cargo.toml
- name: Check MSRV for all crates
run: bash ./scripts/msrv.sh
cargo-deny:
runs-on: ubuntu-latest # This uses the step `EmbarkStudios/cargo-deny-action@v1` which is only supported on Linux
continue-on-error: true # Prevent sudden announcement of a new advisory from failing ci
Expand Down
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ You can provide the protocol compiler protoc path programmatically (only works w
export PROTOC=$(which protoc)
```

It is recommended to use "3.15" or newer of protoc, as some of the proto
definitions include "optional" fields, that are not supported in older versions,
resulting in errors as shown
[here](https://github.com/open-telemetry/opentelemetry-proto/issues/451).

Prerequisites to build the protocol compiler protoc from source

- [protoc](https://github.com/protocolbuffers/protobuf)
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ debug = 1
async-std = "1.10"
async-trait = "0.1"
bytes = "1"
env_logger = { version = "0.10", default-features = false } # env_logger requires a newer MSRV
futures-core = "0.3"
futures-executor = "0.3"
futures-util = { version = "0.3", default-features = false }
Expand Down
7 changes: 3 additions & 4 deletions examples/metrics-advanced/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use opentelemetry::global;
use opentelemetry::metrics::Unit;
use opentelemetry::Key;
use opentelemetry::KeyValue;
use opentelemetry_sdk::metrics::{
Expand All @@ -15,7 +14,7 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
Some(
Stream::new()
.name("my_histogram_renamed")
.unit(Unit::new("milliseconds")),
.unit("milliseconds"),
)
} else {
None
Expand Down Expand Up @@ -76,7 +75,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// using view.
let histogram = meter
.f64_histogram("my_histogram")
.with_unit(Unit::new("ms"))
.with_unit("ms")
.with_description("My histogram example description")
.init();

Expand Down Expand Up @@ -114,7 +113,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
// use a custom set of boundaries, and min/max values will not be recorded.
let histogram2 = meter
.f64_histogram("my_second_histogram")
.with_unit(Unit::new("ms"))
.with_unit("ms")
.with_description("My histogram example description")
.init();

Expand Down
9 changes: 4 additions & 5 deletions examples/metrics-basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use opentelemetry::global;
use opentelemetry::metrics::Unit;
use opentelemetry::KeyValue;
use opentelemetry_sdk::metrics::{PeriodicReader, SdkMeterProvider};
use opentelemetry_sdk::{runtime, Resource};
Expand Down Expand Up @@ -47,7 +46,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let _observable_counter = meter
.u64_observable_counter("my_observable_counter")
.with_description("My observable counter example description")
.with_unit(Unit::new("myunit"))
.with_unit("myunit")
.with_callback(|observer| {
observer.observe(
100,
Expand Down Expand Up @@ -75,7 +74,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let _observable_up_down_counter = meter
.i64_observable_up_down_counter("my_observable_updown_counter")
.with_description("My observable updown counter example description")
.with_unit(Unit::new("myunit"))
.with_unit("myunit")
.with_callback(|observer| {
observer.observe(
100,
Expand Down Expand Up @@ -108,7 +107,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let gauge = meter
.f64_gauge("my_gauge")
.with_description("A gauge set to 1.0")
.with_unit(Unit::new("myunit"))
.with_unit("myunit")
.init();

gauge.record(
Expand All @@ -123,7 +122,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let _observable_gauge = meter
.f64_observable_gauge("my_observable_gauge")
.with_description("An observable gauge set to 1.0")
.with_unit(Unit::new("myunit"))
.with_unit("myunit")
.with_callback(|observer| {
observer.observe(
1.0,
Expand Down
5 changes: 3 additions & 2 deletions examples/tracing-jaeger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use opentelemetry_semantic_conventions::resource::SERVICE_NAME;

use std::error::Error;

fn init_tracer() -> Result<opentelemetry_sdk::trace::Tracer, TraceError> {
fn init_tracer_provider() -> Result<opentelemetry_sdk::trace::TracerProvider, TraceError> {
opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
Expand All @@ -29,7 +29,8 @@ fn init_tracer() -> Result<opentelemetry_sdk::trace::Tracer, TraceError> {

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let _tracer = init_tracer().expect("Failed to initialize tracer.");
let tracer_provider = init_tracer_provider().expect("Failed to initialize tracer provider.");
global::set_tracer_provider(tracer_provider.clone());

let tracer = global::tracer("tracing-jaeger");
tracer.in_span("main-operation", |cx| {
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-appender-tracing/benches/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
| noop_layer_disabled | 12 ns |
| noop_layer_enabled | 25 ns |
| ot_layer_disabled | 19 ns |
| ot_layer_enabled | 446 ns |
| ot_layer_enabled | 371 ns |
*/

use async_trait::async_trait;
Expand Down
79 changes: 39 additions & 40 deletions opentelemetry-appender-tracing/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ use tracing_subscriber::Layer;
const INSTRUMENTATION_LIBRARY_NAME: &str = "opentelemetry-appender-tracing";

/// Visitor to record the fields from the event record.
#[derive(Default)]
struct EventVisitor {
log_record_attributes: Vec<(Key, AnyValue)>,
log_record_body: Option<AnyValue>,
struct EventVisitor<'a, LR: LogRecord> {
log_record: &'a mut LR,
}

/// Logs from the log crate have duplicated attributes that we removed here.
Expand All @@ -37,59 +35,61 @@ fn get_filename(filepath: &str) -> &str {
filepath
}

impl EventVisitor {
impl<'a, LR: LogRecord> EventVisitor<'a, LR> {
fn new(log_record: &'a mut LR) -> Self {
EventVisitor { log_record }
}
fn visit_metadata(&mut self, meta: &Metadata) {
self.log_record_attributes
.push(("name".into(), meta.name().into()));
self.log_record
.add_attribute(Key::new("name"), AnyValue::from(meta.name()));

#[cfg(feature = "experimental_metadata_attributes")]
self.visit_experimental_metadata(meta);
}

#[cfg(feature = "experimental_metadata_attributes")]
fn visit_experimental_metadata(&mut self, meta: &Metadata) {
self.log_record_attributes
.push(("log.target".into(), meta.target().to_owned().into()));
self.log_record.add_attribute(
Key::new("log.target"),
AnyValue::from(meta.target().to_owned()),
);

if let Some(module_path) = meta.module_path() {
self.log_record_attributes
.push(("code.namespace".into(), module_path.to_owned().into()));
self.log_record.add_attribute(
Key::new("code.namespace"),
AnyValue::from(module_path.to_owned()),
);
}

if let Some(filepath) = meta.file() {
self.log_record_attributes
.push(("code.filepath".into(), filepath.to_owned().into()));
self.log_record_attributes.push((
"code.filename".into(),
get_filename(filepath).to_owned().into(),
));
self.log_record.add_attribute(
Key::new("code.filepath"),
AnyValue::from(filepath.to_owned()),
);
self.log_record.add_attribute(
Key::new("code.filename"),
AnyValue::from(get_filename(filepath).to_owned()),
);
}

if let Some(line) = meta.line() {
self.log_record_attributes
.push(("code.lineno".into(), line.into()));
}
}

fn push_to_otel_log_record<LR: LogRecord>(self, log_record: &mut LR) {
if let Some(body) = self.log_record_body {
log_record.set_body(body);
self.log_record
.add_attribute(Key::new("code.lineno"), AnyValue::from(line));
}
log_record.add_attributes(self.log_record_attributes);
}
}

impl tracing::field::Visit for EventVisitor {
impl<'a, LR: LogRecord> tracing::field::Visit for EventVisitor<'a, LR> {
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
#[cfg(feature = "experimental_metadata_attributes")]
if is_duplicated_metadata(field.name()) {
return;
}
if field.name() == "message" {
self.log_record_body = Some(format!("{value:?}").into());
self.log_record.set_body(format!("{:?}", value).into());
} else {
self.log_record_attributes
.push((field.name().into(), format!("{value:?}").into()));
self.log_record
.add_attribute(Key::new(field.name()), AnyValue::from(format!("{value:?}")));
}
}

Expand All @@ -98,27 +98,27 @@ impl tracing::field::Visit for EventVisitor {
if is_duplicated_metadata(field.name()) {
return;
}
self.log_record_attributes
.push((field.name().into(), value.to_owned().into()));
self.log_record
.add_attribute(Key::new(field.name()), AnyValue::from(value.to_owned()));
}

fn record_bool(&mut self, field: &tracing_core::Field, value: bool) {
self.log_record_attributes
.push((field.name().into(), value.into()));
self.log_record
.add_attribute(Key::new(field.name()), AnyValue::from(value));
}

fn record_f64(&mut self, field: &tracing::field::Field, value: f64) {
self.log_record_attributes
.push((field.name().into(), value.into()));
self.log_record
.add_attribute(Key::new(field.name()), AnyValue::from(value));
}

fn record_i64(&mut self, field: &tracing::field::Field, value: i64) {
#[cfg(feature = "experimental_metadata_attributes")]
if is_duplicated_metadata(field.name()) {
return;
}
self.log_record_attributes
.push((field.name().into(), value.into()));
self.log_record
.add_attribute(Key::new(field.name()), AnyValue::from(value));
}

// TODO: Remaining field types from AnyValue : Bytes, ListAny, Boolean
Expand Down Expand Up @@ -173,11 +173,10 @@ where
log_record.set_severity_number(severity_of_level(meta.level()));
log_record.set_severity_text(meta.level().to_string().into());

let mut visitor = EventVisitor::default();
let mut visitor = EventVisitor::new(&mut log_record);
visitor.visit_metadata(meta);
// Visit fields.
event.record(&mut visitor);
visitor.push_to_otel_log_record(&mut log_record);

self.logger.emit(log_record);
}
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-jaeger-propagator/src/propagator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Propagator {
/// First bit controls whether to sample
/// Second bit controls whether it's a debug trace
/// Third bit is not used.
/// Forth bit is firehose flag, which is not supported in OT now.
/// Fourth bit is firehose flag, which is not supported in OT now.
fn extract_trace_flags(&self, flag: &str) -> Result<TraceFlags, ()> {
if flag.len() > 2 {
return Err(());
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-otlp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Instead of using
`.with_config(Config::default().with_resource(RESOURCE::default()))` users must
now use `.with_resource(RESOURCE::default())` to configure Resource when using
`OtlpLogPipeline`.
- **Breaking** The methods `OtlpTracePipeline::install_simple()` and `OtlpTracePipeline::install_batch()` would now return `TracerProvider` instead of `Tracer`.
These methods would also no longer set the global tracer provider. It would now be the responsibility of users to set it by calling `global::set_tracer_provider(tracer_provider.clone());`. Refer to the [basic-otlp](https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry-otlp/examples/basic-otlp/src/main.rs) and [basic-otlp-http](https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs) examples on how to initialize OTLP Trace Exporter.

## v0.16.0

Expand Down
11 changes: 7 additions & 4 deletions opentelemetry-otlp/examples/basic-otlp-http/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use once_cell::sync::Lazy;
use opentelemetry::{
global,
metrics::{MetricsError, Unit},
metrics::MetricsError,
trace::{TraceContextExt, TraceError, Tracer, TracerProvider as _},
Key, KeyValue,
};
Expand Down Expand Up @@ -37,7 +37,7 @@ fn init_logs() -> Result<sdklogs::LoggerProvider, opentelemetry::logs::LogError>
.install_batch(opentelemetry_sdk::runtime::Tokio)
}

fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
fn init_tracer_provider() -> Result<sdktrace::TracerProvider, TraceError> {
opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
Expand Down Expand Up @@ -67,13 +67,16 @@ fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, Metric

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let result = init_tracer();
let result = init_tracer_provider();
assert!(
result.is_ok(),
"Init tracer failed with error: {:?}",
result.err()
);

let tracer_provider = result.unwrap();
global::set_tracer_provider(tracer_provider.clone());

let result = init_metrics();
assert!(
result.is_ok(),
Expand Down Expand Up @@ -125,7 +128,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let counter = meter
.u64_counter("test_counter")
.with_description("a simple counter for demo purposes.")
.with_unit(Unit::new("my_unit"))
.with_unit("my_unit")
.init();
for _ in 0..10 {
counter.add(1, &[KeyValue::new("test_key", "test_value")]);
Expand Down
Loading

0 comments on commit 5f7a564

Please sign in to comment.