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

backport commits from main & fix terminology #1539

Merged
merged 7 commits into from
Sep 4, 2021
Merged
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
10 changes: 10 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ jobs:
- log-always
- std log-always
- std
fail-fast: false
steps:
- uses: actions/checkout@main
- uses: actions-rs/toolchain@v1
Expand Down Expand Up @@ -128,6 +129,7 @@ jobs:
strategy:
matrix:
rust: [stable, beta, nightly]
fail-fast: false
steps:
- uses: actions/checkout@main
- uses: actions-rs/toolchain@v1
Expand Down Expand Up @@ -160,6 +162,7 @@ jobs:
- tracing-serde
- tracing-subscriber
- tracing-tower
fail-fast: false
steps:
- uses: actions/checkout@main
- uses: actions-rs/toolchain@v1
Expand Down Expand Up @@ -214,6 +217,9 @@ jobs:

features-stable:
# Feature flag tests that run on stable Rust.
# TODO(david): once tracing's MSRV goes up to Rust 1.51, we should be able to switch to
# using cargo's V2 feature resolver (https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions)
# and avoid cd'ing into each crate's directory.
needs: check
runs-on: ubuntu-latest
steps:
Expand All @@ -231,6 +237,10 @@ jobs:
run: (cd tracing-core && cargo test --no-default-features)
- name: "Test tracing no-std support"
run: (cd tracing && cargo test --no-default-features)
# this skips running doctests under the `--no-default-features` flag,
# as rustdoc isn't aware of cargo's feature flags.
- name: "Test tracing-subscriber with all features disabled"
run: (cd tracing-subscriber && cargo test --lib --tests --no-default-features)

style:
# Check style.
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/fmt-multiple-writers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
.with(EnvFilter::from_default_env().add_directive(tracing::Level::TRACE.into()))
.with(fmt::Layer::new().with_writer(io::stdout))
.with(fmt::Layer::new().with_writer(non_blocking));
tracing::subscriber::set_global_default(subscriber).expect("Unable to set a global collector");
tracing::subscriber::set_global_default(subscriber).expect("Unable to set a global subscriber");

let number_of_yaks = 3;
// this creates a new event, outside of any spans.
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/fmt-pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
.with_thread_names(true)
// enable everything
.with_max_level(tracing::Level::TRACE)
// sets this to be the default, global collector for this application.
// sets this to be the default, global subscriber for this application.
.init();

let number_of_yaks = 3;
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() {
tracing_subscriber::fmt()
// enable everything
.with_max_level(tracing::Level::TRACE)
// sets this to be the default, global collector for this application.
// sets this to be the default, global subscriber for this application.
.init();

let number_of_yaks = 3;
Expand Down
6 changes: 4 additions & 2 deletions examples/examples/tower-load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ use std::{
};
use tokio::{time, try_join};
use tower::{Service, ServiceBuilder, ServiceExt};
use tracing::{self, debug, error, info, span, trace, warn, Instrument as _, Level, Span};
use tracing::{
self, debug, error, info, info_span, span, trace, warn, Instrument as _, Level, Span,
};
use tracing_subscriber::{filter::EnvFilter, reload::Handle};
use tracing_tower::{request_span, request_span::make};

Expand Down Expand Up @@ -368,7 +370,7 @@ async fn load_gen(addr: SocketAddr) -> Result<(), Err> {
.instrument(span)
.await
}
.instrument(span!(target: "gen", Level::INFO, "generated_request", remote.addr=%addr));
.instrument(info_span!(target: "gen", "generated_request", remote.addr=%addr).or_current());
tokio::spawn(f);
}
}
Expand Down
4 changes: 3 additions & 1 deletion tracing-attributes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ enum RecordType {

impl RecordType {
/// Array of primitive types which should be recorded as [RecordType::Value].
const TYPES_FOR_VALUE: [&'static str; 23] = [
const TYPES_FOR_VALUE: &'static [&'static str] = &[
"bool",
"str",
"u8",
Expand All @@ -1078,6 +1078,8 @@ impl RecordType {
"i32",
"u64",
"i64",
"f32",
"f64",
"usize",
"isize",
"NonZeroU8",
Expand Down
20 changes: 16 additions & 4 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
//! will contain any fields attached to each event.
//!
//! `tracing` represents values as either one of a set of Rust primitives
//! (`i64`, `u64`, `bool`, and `&str`) or using a `fmt::Display` or `fmt::Debug`
//! implementation. `Subscriber`s are provided these primitive value types as
//! `dyn Value` trait objects.
//! (`i64`, `u64`, `f64`, `bool`, and `&str`) or using a `fmt::Display` or
//! `fmt::Debug` implementation. `Subscriber`s are provided these primitive
//! value types as `dyn Value` trait objects.
//!
//! These trait objects can be formatted using `fmt::Debug`, but may also be
//! recorded as typed data by calling the [`Value::record`] method on these
Expand Down Expand Up @@ -184,6 +184,11 @@ pub struct Iter {
/// [`Event`]: ../event/struct.Event.html
/// [`ValueSet`]: struct.ValueSet.html
pub trait Visit {
/// Visit a double-precision floating point value.
fn record_f64(&mut self, field: &Field, value: f64) {
self.record_debug(field, &value)
}

/// Visit a signed 64-bit integer value.
fn record_i64(&mut self, field: &Field, value: i64) {
self.record_debug(field, &value)
Expand Down Expand Up @@ -335,6 +340,12 @@ macro_rules! ty_to_nonzero {
}

macro_rules! impl_one_value {
(f32, $op:expr, $record:ident) => {
impl_one_value!(normal, f32, $op, $record);
};
(f64, $op:expr, $record:ident) => {
impl_one_value!(normal, f64, $op, $record);
};
(bool, $op:expr, $record:ident) => {
impl_one_value!(normal, bool, $op, $record);
};
Expand Down Expand Up @@ -387,7 +398,8 @@ impl_values! {
record_u64(usize, u32, u16, u8 as u64),
record_i64(i64),
record_i64(isize, i32, i16, i8 as i64),
record_bool(bool)
record_bool(bool),
record_f64(f64, f32 as f64)
}

impl<T: crate::sealed::Sealed> crate::sealed::Sealed for Wrapping<T> {}
Expand Down
22 changes: 22 additions & 0 deletions tracing-opentelemetry/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ impl<'a> field::Visit for SpanEventVisitor<'a> {
}
}

/// Record events on the underlying OpenTelemetry [`Span`] from `f64` values.
///
/// [`Span`]: opentelemetry::trace::Span
fn record_f64(&mut self, field: &field::Field, value: f64) {
match field.name() {
"message" => self.0.name = value.to_string().into(),
// Skip fields that are actually log metadata that have already been handled
#[cfg(feature = "tracing-log")]
name if name.starts_with("log.") => (),
name => {
self.0.attributes.push(KeyValue::new(name, value));
}
}
}

/// Record events on the underlying OpenTelemetry [`Span`] from `i64` values.
///
/// [`Span`]: opentelemetry::trace::Span
Expand Down Expand Up @@ -197,6 +212,13 @@ impl<'a> field::Visit for SpanAttributeVisitor<'a> {
self.record(KeyValue::new(field.name(), value));
}

/// Set attributes on the underlying OpenTelemetry [`Span`] from `f64` values.
///
/// [`Span`]: opentelemetry::trace::Span
fn record_f64(&mut self, field: &field::Field, value: f64) {
self.record(KeyValue::new(field.name(), value));
}

/// Set attributes on the underlying OpenTelemetry [`Span`] from `i64` values.
///
/// [`Span`]: opentelemetry::trace::Span
Expand Down
71 changes: 70 additions & 1 deletion tracing-opentelemetry/src/span_ext.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::layer::WithContext;
use opentelemetry::Context;
use opentelemetry::{trace::SpanContext, Context, KeyValue};

/// Utility functions to allow tracing [`Span`]s to accept and return
/// [OpenTelemetry] [`Context`]s.
Expand Down Expand Up @@ -42,6 +42,50 @@ pub trait OpenTelemetrySpanExt {
/// ```
fn set_parent(&self, cx: Context);

/// Associates `self` with a given OpenTelemetry trace, using the provided
/// followed span [`SpanContext`].
///
/// [`SpanContext`]: opentelemetry::trace::SpanContext
///
/// # Examples
///
/// ```rust
/// use opentelemetry::{propagation::TextMapPropagator, trace::TraceContextExt};
/// use opentelemetry::sdk::propagation::TraceContextPropagator;
/// use tracing_opentelemetry::OpenTelemetrySpanExt;
/// use std::collections::HashMap;
/// use tracing::Span;
///
/// // Example carrier, could be a framework header map that impls otel's `Extract`.
/// let mut carrier = HashMap::new();
///
/// // Propagator can be swapped with b3 propagator, jaeger propagator, etc.
/// let propagator = TraceContextPropagator::new();
///
/// // Extract otel context of linked span via the chosen propagator
/// let linked_span_otel_context = propagator.extract(&carrier);
///
/// // Extract the linked span context from the otel context
/// let linked_span_context = linked_span_otel_context.span().span_context().clone();
///
/// // Generate a tracing span as usual
/// let app_root = tracing::span!(tracing::Level::INFO, "app_start");
///
/// // Assign linked trace from external context
/// app_root.add_link(linked_span_context);
///
/// // Or if the current span has been created elsewhere:
/// let linked_span_context = linked_span_otel_context.span().span_context().clone();
/// Span::current().add_link(linked_span_context);
/// ```
fn add_link(&self, cx: SpanContext);

/// Associates `self` with a given OpenTelemetry trace, using the provided
/// followed span [`SpanContext`] and attributes.
///
/// [`SpanContext`]: opentelemetry::trace::SpanContext
fn add_link_with_attributes(&self, cx: SpanContext, attributes: Vec<KeyValue>);

/// Extracts an OpenTelemetry [`Context`] from `self`.
///
/// [`Context`]: opentelemetry::Context
Expand Down Expand Up @@ -86,6 +130,31 @@ impl OpenTelemetrySpanExt for tracing::Span {
});
}

fn add_link(&self, cx: SpanContext) {
self.add_link_with_attributes(cx, Vec::new())
}

fn add_link_with_attributes(&self, cx: SpanContext, attributes: Vec<KeyValue>) {
if cx.is_valid() {
let mut cx = Some(cx);
let mut att = Some(attributes);
self.with_subscriber(move |(id, subscriber)| {
if let Some(get_context) = subscriber.downcast_ref::<WithContext>() {
get_context.with_context(subscriber, id, move |builder, _tracer| {
if let Some(cx) = cx.take() {
let attr = att.take().unwrap_or_default();
let follows_link = opentelemetry::trace::Link::new(cx, attr);
builder
.links
.get_or_insert_with(|| Vec::with_capacity(1))
.push(follows_link);
}
});
}
});
}
}

fn context(&self) -> Context {
let mut cx = None;
self.with_subscriber(|(id, subscriber)| {
Expand Down
12 changes: 12 additions & 0 deletions tracing-serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ where
}
}

fn record_f64(&mut self, field: &Field, value: f64) {
if self.state.is_ok() {
self.state = self.serializer.serialize_entry(field.name(), &value)
}
}

fn record_str(&mut self, field: &Field, value: &str) {
if self.state.is_ok() {
self.state = self.serializer.serialize_entry(field.name(), &value)
Expand Down Expand Up @@ -430,6 +436,12 @@ where
}
}

fn record_f64(&mut self, field: &Field, value: f64) {
if self.state.is_ok() {
self.state = self.serializer.serialize_field(field.name(), &value)
}
}

fn record_str(&mut self, field: &Field, value: &str) {
if self.state.is_ok() {
self.state = self.serializer.serialize_field(field.name(), &value)
Expand Down
5 changes: 5 additions & 0 deletions tracing-subscriber/src/field/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ impl<V> Visit for Alt<V>
where
V: Visit,
{
#[inline]
fn record_f64(&mut self, field: &Field, value: f64) {
self.0.record_f64(field, value)
}

#[inline]
fn record_i64(&mut self, field: &Field, value: i64) {
self.0.record_i64(field, value)
Expand Down
5 changes: 5 additions & 0 deletions tracing-subscriber/src/field/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ impl<V> Visit for Messages<V>
where
V: Visit,
{
#[inline]
fn record_f64(&mut self, field: &Field, value: f64) {
self.0.record_f64(field, value)
}

#[inline]
fn record_i64(&mut self, field: &Field, value: i64) {
self.0.record_i64(field, value)
Expand Down
Loading