Skip to content

Commit

Permalink
Add support for opentelemetry 0.17, actix-web rc.2 (actix#74)
Browse files Browse the repository at this point in the history
* Add support for opentelemetry 0.17, actix-web rc.2

* Enable actix-web macros for tests

* Link to newest opentelemetry in readme

* Add opentelemetry_0_17 to workflows
  • Loading branch information
asonix authored Feb 4, 2022
1 parent ed35dc6 commit b3f3792
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:
- opentelemetry_0_14
- opentelemetry_0_15
- opentelemetry_0_16
- opentelemetry_0_17
steps:
- uses: actions/checkout@v2
- name: Cache dependencies
Expand Down Expand Up @@ -101,4 +102,4 @@ jobs:
- uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: -- -D warnings
args: -- -D warnings
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = [".", "examples/opentelemetry", "examples/custom-root-span"]

[package]
name = "tracing-actix-web"
version = "0.5.0-rc.1"
version = "0.5.0-rc.2"
authors = ["Luca Palmieri <[email protected]>"]
edition = "2018"

Expand All @@ -24,10 +24,11 @@ opentelemetry_0_13 = ["opentelemetry_0_13_pkg", "tracing-opentelemetry_0_12_pkg"
opentelemetry_0_14 = ["opentelemetry_0_14_pkg", "tracing-opentelemetry_0_13_pkg"]
opentelemetry_0_15 = ["opentelemetry_0_15_pkg", "tracing-opentelemetry_0_14_pkg"]
opentelemetry_0_16 = ["opentelemetry_0_16_pkg", "tracing-opentelemetry_0_16_pkg"]
opentelemetry_0_17 = ["opentelemetry_0_17_pkg", "tracing-opentelemetry_0_17_pkg"]
emit_event_on_error = []

[dependencies]
actix-web = { version = "=4.0.0-rc.1", default-features = false }
actix-web = { version = "=4.0.0-rc.2", default-features = false }
pin-project = "1.0.0"
tracing = "0.1.19"
tracing-futures = "0.2.4"
Expand All @@ -36,12 +37,15 @@ opentelemetry_0_13_pkg = { package = "opentelemetry", version = "0.13", optional
opentelemetry_0_14_pkg = { package = "opentelemetry", version = "0.14", optional = true }
opentelemetry_0_15_pkg = { package = "opentelemetry", version = "0.15", optional = true }
opentelemetry_0_16_pkg = { package = "opentelemetry", version = "0.16", optional = true }
opentelemetry_0_17_pkg = { package = "opentelemetry", version = "0.17", optional = true }
tracing-opentelemetry_0_12_pkg = { package = "tracing-opentelemetry",version = "0.12", optional = true }
tracing-opentelemetry_0_13_pkg = { package = "tracing-opentelemetry", version = "0.13", optional = true }
tracing-opentelemetry_0_14_pkg = { package = "tracing-opentelemetry",version = "0.14", optional = true }
tracing-opentelemetry_0_16_pkg = { package = "tracing-opentelemetry",version = "0.16", optional = true }
tracing-opentelemetry_0_17_pkg = { package = "tracing-opentelemetry",version = "0.17", optional = true }

[dev-dependencies]
actix-web = { version = "=4.0.0-rc.2", default-features = false, features = ["macros"] }
tracing-subscriber = { version = "0.3.0", features = ["registry", "env-filter"] }
tracing-bunyan-formatter = "0.3.0"
tracing-log = "0.1.1"
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ Add `tracing-actix-web` to your dependencies:
```toml
[dependencies]
# ...
tracing-actix-web = "=0.5.0-rc.1"
tracing-actix-web = "=0.5.0-rc.2"
tracing = "0.1"
actix-web = "=4.0.0-rc.1"
actix-web = "=4.0.0-rc.2"
```

`tracing-actix-web` exposes three feature flags:
Expand All @@ -48,6 +48,7 @@ actix-web = "=4.0.0-rc.1"
- `opentelemetry_0_14`: same as above but using `opentelemetry` 0.14;
- `opentelemetry_0_15`: same as above but using `opentelemetry` 0.15;
- `opentelemetry_0_16`: same as above but using `opentelemetry` 0.16;
- `opentelemetry_0_17`: same as above but using `opentelemetry` 0.17;
- `emit_event_on_error`: emit a [`tracing`] event when request processing fails with an error (enabled by default).

`tracing-actix-web` will release `0.5.0`, going out of beta, as soon as `actix-web` releases a stable `4.0.0`.
Expand Down Expand Up @@ -256,10 +257,10 @@ To fulfill a request you often have to perform additional I/O operations - e.g.
`tracing-actix-web` provides support for distributed tracing by supporting the [OpenTelemetry standard](https://opentelemetry.io/).
`tracing-actix-web` follows [OpenTelemetry's semantic convention](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/overview.md#spancontext)
for field names.
Furthermore, it provides an `opentelemetry_0_16` feature flag to automatically performs trace propagation: it tries to extract the OpenTelemetry context out of the headers of incoming requests and, when it finds one, it sets it as the remote context for the current root span. The context is then propagated to your downstream dependencies if your HTTP or gRPC clients are OpenTelemetry-aware - e.g. using [`reqwest-middleware` and `reqwest-tracing`](https://github.com/TrueLayer/reqwest-middleware) if you are using `reqwest` as your HTTP client.
Furthermore, it provides an `opentelemetry_0_17` feature flag to automatically performs trace propagation: it tries to extract the OpenTelemetry context out of the headers of incoming requests and, when it finds one, it sets it as the remote context for the current root span. The context is then propagated to your downstream dependencies if your HTTP or gRPC clients are OpenTelemetry-aware - e.g. using [`reqwest-middleware` and `reqwest-tracing`](https://github.com/TrueLayer/reqwest-middleware) if you are using `reqwest` as your HTTP client.
You can then find all logs for the same request across all the services it touched by looking for the `trace_id`, automatically logged by `tracing-actix-web`.

If you add [`tracing-opentelemetry::OpenTelemetryLayer`](https://docs.rs/tracing-opentelemetry/0.16.0/tracing_opentelemetry/struct.OpenTelemetryLayer.html)
If you add [`tracing-opentelemetry::OpenTelemetryLayer`](https://docs.rs/tracing-opentelemetry/0.17.0/tracing_opentelemetry/struct.OpenTelemetryLayer.html)
in your `tracing::Subscriber` you will be able to export the root span (and all its children) as OpenTelemetry spans.

Check out the [relevant example in the GitHub repository](https://github.com/LukeMathWalker/tracing-actix-web/tree/main/examples/opentelemetry) for reference.
Expand Down
8 changes: 4 additions & 4 deletions examples/custom-root-span/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-web = "=4.0.0-rc.1"
opentelemetry = { version = "0.16", features = ["rt-tokio-current-thread"] }
opentelemetry-jaeger = { version = "0.15", features = ["rt-tokio-current-thread"] }
tracing-opentelemetry = { version = "0.16" }
actix-web = "=4.0.0-rc.2"
opentelemetry = { version = "0.17", features = ["rt-tokio-current-thread"] }
opentelemetry-jaeger = { version = "0.16", features = ["rt-tokio-current-thread"] }
tracing-opentelemetry = { version = "0.17" }
tracing = "0.1.19"
tracing-subscriber = { version = "0.3", features = ["registry", "env-filter"] }
tracing-bunyan-formatter = "0.3"
Expand Down
10 changes: 5 additions & 5 deletions examples/opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ edition = "2018"
license = "MIT/Apache-2.0"

[dependencies]
actix-web = "=4.0.0-rc.1"
actix-web = "=4.0.0-rc.2"
tracing = "0.1.19"
opentelemetry = { version = "0.16", features = ["rt-tokio-current-thread"] }
opentelemetry-jaeger = { version = "0.15", features = ["rt-tokio-current-thread"] }
tracing-opentelemetry = { version = "0.16" }
opentelemetry = { version = "0.17", features = ["rt-tokio-current-thread"] }
opentelemetry-jaeger = { version = "0.16", features = ["rt-tokio-current-thread"] }
tracing-opentelemetry = { version = "0.17" }
tracing-subscriber = { version = "0.3", features = ["registry", "env-filter"] }
tracing-bunyan-formatter = "0.3"
tracing-actix-web = { path = "../..", features = ["opentelemetry_0_16"] }
tracing-actix-web = { path = "../..", features = ["opentelemetry_0_17"] }
24 changes: 19 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
//! ```toml
//! [dependencies]
//! # ...
//! tracing-actix-web = "=0.5.0-rc.1"
//! tracing-actix-web = "=0.5.0-rc.2"
//! tracing = "0.1"
//! actix-web = "=4.0.0-rc.1"
//! actix-web = "=4.0.0-rc.2"
//! ```
//!
//! `tracing-actix-web` exposes three feature flags:
Expand All @@ -21,6 +21,7 @@
//! - `opentelemetry_0_14`: same as above but using `opentelemetry` 0.14;
//! - `opentelemetry_0_15`: same as above but using `opentelemetry` 0.15;
//! - `opentelemetry_0_16`: same as above but using `opentelemetry` 0.16;
//! - `opentelemetry_0_17`: same as above but using `opentelemetry` 0.17;
//! - `emit_event_on_error`: emit a [`tracing`] event when request processing fails with an error (enabled by default).
//!
//! `tracing-actix-web` will release `0.5.0`, going out of beta, as soon as `actix-web` releases a stable `4.0.0`.
Expand Down Expand Up @@ -225,10 +226,10 @@
//! `tracing-actix-web` provides support for distributed tracing by supporting the [OpenTelemetry standard](https://opentelemetry.io/).
//! `tracing-actix-web` follows [OpenTelemetry's semantic convention](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/overview.md#spancontext)
//! for field names.
//! Furthermore, it provides an `opentelemetry_0_16` feature flag to automatically performs trace propagation: it tries to extract the OpenTelemetry context out of the headers of incoming requests and, when it finds one, it sets it as the remote context for the current root span. The context is then propagated to your downstream dependencies if your HTTP or gRPC clients are OpenTelemetry-aware - e.g. using [`reqwest-middleware` and `reqwest-tracing`](https://github.com/TrueLayer/reqwest-middleware) if you are using `reqwest` as your HTTP client.
//! Furthermore, it provides an `opentelemetry_0_17` feature flag to automatically performs trace propagation: it tries to extract the OpenTelemetry context out of the headers of incoming requests and, when it finds one, it sets it as the remote context for the current root span. The context is then propagated to your downstream dependencies if your HTTP or gRPC clients are OpenTelemetry-aware - e.g. using [`reqwest-middleware` and `reqwest-tracing`](https://github.com/TrueLayer/reqwest-middleware) if you are using `reqwest` as your HTTP client.
//! You can then find all logs for the same request across all the services it touched by looking for the `trace_id`, automatically logged by `tracing-actix-web`.
//!
//! If you add [`tracing-opentelemetry::OpenTelemetryLayer`](https://docs.rs/tracing-opentelemetry/0.16.0/tracing_opentelemetry/struct.OpenTelemetryLayer.html)
//! If you add [`tracing-opentelemetry::OpenTelemetryLayer`](https://docs.rs/tracing-opentelemetry/0.17.0/tracing_opentelemetry/struct.OpenTelemetryLayer.html)
//! in your `tracing::Subscriber` you will be able to export the root span (and all its children) as OpenTelemetry spans.
//!
//! Check out the [relevant example in the GitHub repository](https://github.com/LukeMathWalker/tracing-actix-web/tree/main/examples/opentelemetry) for reference.
Expand All @@ -252,7 +253,8 @@ pub mod root_span_macro;
feature = "opentelemetry_0_13",
feature = "opentelemetry_0_14",
feature = "opentelemetry_0_15",
feature = "opentelemetry_0_16"
feature = "opentelemetry_0_16",
feature = "opentelemetry_0_17"
))]
mod otel;

Expand All @@ -265,11 +267,23 @@ compile_error!("feature \"opentelemetry_0_13\" and feature \"opentelemetry_0_15\
#[cfg(all(feature = "opentelemetry_0_13", feature = "opentelemetry_0_16"))]
compile_error!("feature \"opentelemetry_0_13\" and feature \"opentelemetry_0_16\" cannot be enabled at the same time");

#[cfg(all(feature = "opentelemetry_0_13", feature = "opentelemetry_0_17"))]
compile_error!("feature \"opentelemetry_0_13\" and feature \"opentelemetry_0_17\" cannot be enabled at the same time");

#[cfg(all(feature = "opentelemetry_0_14", feature = "opentelemetry_0_15"))]
compile_error!("feature \"opentelemetry_0_14\" and feature \"opentelemetry_0_15\" cannot be enabled at the same time");

#[cfg(all(feature = "opentelemetry_0_14", feature = "opentelemetry_0_16"))]
compile_error!("feature \"opentelemetry_0_14\" and feature \"opentelemetry_0_16\" cannot be enabled at the same time");

#[cfg(all(feature = "opentelemetry_0_14", feature = "opentelemetry_0_17"))]
compile_error!("feature \"opentelemetry_0_14\" and feature \"opentelemetry_0_17\" cannot be enabled at the same time");

#[cfg(all(feature = "opentelemetry_0_15", feature = "opentelemetry_0_16"))]
compile_error!("feature \"opentelemetry_0_15\" and feature \"opentelemetry_0_16\" cannot be enabled at the same time");

#[cfg(all(feature = "opentelemetry_0_15", feature = "opentelemetry_0_17"))]
compile_error!("feature \"opentelemetry_0_15\" and feature \"opentelemetry_0_17\" cannot be enabled at the same time");

#[cfg(all(feature = "opentelemetry_0_16", feature = "opentelemetry_0_17"))]
compile_error!("feature \"opentelemetry_0_16\" and feature \"opentelemetry_0_17\" cannot be enabled at the same time");
12 changes: 12 additions & 0 deletions src/otel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use opentelemetry_0_14_pkg as opentelemetry;
use opentelemetry_0_15_pkg as opentelemetry;
#[cfg(feature = "opentelemetry_0_16")]
use opentelemetry_0_16_pkg as opentelemetry;
#[cfg(feature = "opentelemetry_0_17")]
use opentelemetry_0_17_pkg as opentelemetry;

#[cfg(feature = "opentelemetry_0_13")]
use tracing_opentelemetry_0_12_pkg as tracing_opentelemetry;
Expand All @@ -17,6 +19,8 @@ use tracing_opentelemetry_0_13_pkg as tracing_opentelemetry;
use tracing_opentelemetry_0_14_pkg as tracing_opentelemetry;
#[cfg(feature = "opentelemetry_0_16")]
use tracing_opentelemetry_0_16_pkg as tracing_opentelemetry;
#[cfg(feature = "opentelemetry_0_17")]
use tracing_opentelemetry_0_17_pkg as tracing_opentelemetry;

use opentelemetry::propagation::Extractor;

Expand Down Expand Up @@ -50,6 +54,14 @@ pub(crate) fn set_otel_parent(req: &ServiceRequest, span: &tracing::Span) {
span.set_parent(parent_context);
// If we have a remote parent span, this will be the parent's trace identifier.
// If not, it will be the newly generated trace identifier with this request as root span.
#[cfg(not(feature = "opentelemetry_0_17"))]
let trace_id = span.context().span().span_context().trace_id().to_hex();

#[cfg(feature = "opentelemetry_0_17")]
let trace_id = {
let id = span.context().span().span_context().trace_id();
format!("{:032x}", id)
};

span.record("trace_id", &tracing::field::display(trace_id));
}
3 changes: 2 additions & 1 deletion src/root_span_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ pub mod private {
feature = "opentelemetry_0_13",
feature = "opentelemetry_0_14",
feature = "opentelemetry_0_15",
feature = "opentelemetry_0_16"
feature = "opentelemetry_0_16",
feature = "opentelemetry_0_17"
))]
crate::otel::set_otel_parent(req, span);
}
Expand Down

0 comments on commit b3f3792

Please sign in to comment.