Skip to content

Commit

Permalink
[PLATFORM-1473] Support opentelemetry 0.21 (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaeIsBad authored Mar 11, 2024
1 parent 799f190 commit c584ab1
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 29 deletions.
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

---

## [0.16.0] - 2024-03-11

### Added

- Support for opentelemetry 0.21
- `tracing_opentelemetry_0_20` and `tracing_opentelemetry_0_21` features
- `tracing_opentelemetry` is now an alias for the latest version of otel(so `tracing_opentelemetry_0_21`)

Opentelemetry 0.20 support can be enabled by enabling the `tracing_opentelemetry_0_20` feature instead of tracing_opentelemetry.
We are going to support at least the last 3 versions of opentelemetry. After that we mightremove support for older otel version without it being a breaking change.

---

## [0.15.1] - 2023-10-20

### Added
Expand Down Expand Up @@ -324,7 +337,9 @@ Request::rest(&bridge).send()
The old API is still available but deprecated. It will be removed soon.


[Unreleased]: https://github.com/primait/bridge.rs/compare/0.15.1...HEAD

[Unreleased]: https://github.com/primait/bridge.rs/compare/0.16.0...HEAD
[0.16.0]: https://github.com/primait/bridge.rs/compare/0.15.1-rc.0...0.16.0
[0.15.1]: https://github.com/primait/bridge.rs/compare/0.15.0...0.15.1
[0.15.0]: https://github.com/primait/bridge.rs/compare/0.14.6...0.15.0
[0.14.6]: https://github.com/primait/bridge.rs/compare/0.14.5...0.14.6
Expand Down
20 changes: 15 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ license = "MIT"
name = "prima_bridge"
readme = "README.md"
repository = "https://github.com/primait/bridge.rs"
version = "0.15.1-rc.0"
version = "0.16.0"
# See https://github.com/rust-lang/rust/issues/107557
rust-version = "1.72"

[features]
auth0 = ["rand", "redis", "jsonwebtoken", "chrono", "chrono-tz", "aes", "cbc", "dashmap", "tracing"]
default = ["tracing_opentelemetry"]

auth0 = ["rand", "redis", "jsonwebtoken", "chrono", "chrono-tz", "aes", "cbc", "dashmap", "tracing"]
gzip = ["reqwest/gzip"]
redis-tls = ["redis/tls", "redis/tokio-native-tls-comp"]
tracing_opentelemetry = ["opentelemetry", "tracing", "tracing-opentelemetry"]
tracing_opentelemetry = [ "tracing_opentelemetry_0_21" ]

tracing_opentelemetry_0_20 = ["_any_otel_version", "tracing", "tracing-opentelemetry_0_21_pkg", "opentelemetry_0_20_pkg"]
tracing_opentelemetry_0_21 = ["_any_otel_version", "tracing", "tracing-opentelemetry_0_22_pkg", "opentelemetry_0_21_pkg", "opentelemetry_sdk_0_21_pkg"]

_any_otel_version = []

[dependencies]
aes = {version = "0.8", optional = true}
Expand All @@ -28,7 +34,6 @@ dashmap = {version = "5.1", optional = true}
futures = "0.3"
futures-util = "0.3"
jsonwebtoken = {version = "9.0", optional = true}
opentelemetry = {version = ">=0.17, <=0.20", optional = true}
rand = {version = "0.8", optional = true}
redis = {version = "0.23", features = ["tokio-comp"], optional = true}
reqwest = {version = "0.11", features = ["json", "multipart", "stream"]}
Expand All @@ -37,12 +42,17 @@ serde_json = "1.0"
thiserror = "1.0"
tokio = {version = "1.16", features = ["macros", "rt-multi-thread", "fs"]}
tracing = {version = "0.1", optional = true}
tracing-opentelemetry = {version = ">=0.17, <=0.21", optional = true}
uuid = {version = ">=0.7.0, <2.0.0", features = ["serde", "v4"]}

reqwest-middleware = "0.2.3"
http = "0.2.9"

opentelemetry_0_20_pkg = { package = "opentelemetry", version = "0.20", optional = true }
opentelemetry_0_21_pkg = { package = "opentelemetry", version = "0.21", optional = true }
opentelemetry_sdk_0_21_pkg = { package = "opentelemetry_sdk", version = "0.21", optional = true }
tracing-opentelemetry_0_21_pkg = { package = "tracing-opentelemetry", version = "0.21", optional = true }
tracing-opentelemetry_0_22_pkg = { package = "tracing-opentelemetry", version = "0.22", optional = true }

[dev-dependencies]
flate2 = "1.0"
mockito = "1.0"
Expand Down
12 changes: 10 additions & 2 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ command = "cargo"
args = ["fmt", "--", "--check"]

[tasks.test]
dependencies = ["test-base", "test-auth0"]
dependencies = ["test-base", "test-auth0", "test-otel-0_20", "test-otel-0_21"]

[tasks.test-base]
command = "cargo"
args = ["test", "--features", "gzip", "${@}"]
dependencies = ["build"]

[tasks.test-otel-0_20]
command = "cargo"
args = ["test", "--no-default-features", "--features", "tracing_opentelemetry_0_20"]

[tasks.test-otel-0_21]
command = "cargo"
args = ["test", "--no-default-features", "--features", "tracing_opentelemetry_0_21"]

[tasks.test-auth0]
command = "cargo"
args = ["test", "--features=auth0,gzip", "${@}"]
Expand All @@ -43,4 +51,4 @@ env = { "RUSTDOCFLAGS" = "-Dwarnings" }
[tasks.release]
description = "Task to release the package to crates.io"
command = "cargo"
args = ["publish", "--no-verify"]
args = ["publish", "--no-verify"]
2 changes: 1 addition & 1 deletion examples/rest_multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! ```shell
//! cargo run --example rest_multipart
//! ```
use std::{collections::HashSet, iter::FromIterator};
use std::collections::HashSet;

use prima_bridge::{prelude::*, MultipartFile, MultipartFormFileField, RestMultipart};

Expand Down
3 changes: 2 additions & 1 deletion src/auth0/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ impl Token {
// the exact issued_at (iat) and expiration (exp)
// reference: https://www.iana.org/assignments/jwt/jwt.xhtml
let issue_date: DateTime<Utc> = Utc::now();
let expire_date: DateTime<Utc> = Utc::now() + Duration::seconds(response.expires_in as i64);
let expire_date: DateTime<Utc> =
Utc::now() + Duration::try_seconds(response.expires_in as i64).unwrap_or(Duration::max_value());

Ok(Self {
token: access_token,
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
//! pass around.
//!
//! The bridge implement a type state pattern to build the external request.
//!
//! ### Features
//! * `auth0` - enable auth0 integration, allowing bridge.rs to retrieve tokens from auth0 for authentication
//! * `gzip` - provides response body gzip decompression.
//! * `redis-tls` - add support for connecting to redis with tls
//! * `tracing-opentelemetry` adds support for integration with opentelemetry.
//! This feature is an alias for the `tracing_opentelemetry_0_21` feature.
//! `tracing_opentelemetry_0_20` is also available as to support the 0.20 opentelemetry
//! libraries.
//!
//! We are going to support at least the last 3 versions of opentelemetry. After that we mightremove support for older otel version without it being a breaking change.
use errors::PrimaBridgeError;
use http::{header::HeaderName, HeaderValue, Method};
Expand Down
8 changes: 4 additions & 4 deletions src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{BridgeClient, BridgeImpl, Response};
mod body;
mod request_type;

#[cfg(feature = "tracing_opentelemetry")]
#[cfg(feature = "_any_otel_version")]
mod otel;

pub enum RequestType {
Expand Down Expand Up @@ -139,7 +139,7 @@ pub trait DeliverableRequest<'a>: Sized + Sealed + 'a {

fn get_all_headers(&self) -> HeaderMap {
let mut additional_headers = self.get_custom_headers().clone();
#[cfg(feature = "tracing_opentelemetry")]
#[cfg(feature = "_any_otel_version")]
additional_headers.extend(self.tracing_headers());
#[cfg(feature = "auth0")]
additional_headers.extend(self.get_auth0_headers());
Expand Down Expand Up @@ -244,7 +244,7 @@ pub trait DeliverableRequest<'a>: Sized + Sealed + 'a {
})
}

#[cfg(feature = "tracing_opentelemetry")]
#[cfg(feature = "_any_otel_version")]
fn tracing_headers(&self) -> HeaderMap {
use std::collections::HashMap;

Expand All @@ -266,7 +266,7 @@ pub trait DeliverableRequest<'a>: Sized + Sealed + 'a {
.collect()
}

#[cfg(not(feature = "tracing_opentelemetry"))]
#[cfg(not(feature = "_any_otel_version"))]
fn tracing_headers(&self) -> Vec<(HeaderName, HeaderValue)> {
vec![]
}
Expand Down
38 changes: 30 additions & 8 deletions src/request/otel.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
use opentelemetry::{
propagation::{Injector, TextMapPropagator},
sdk::propagation::TraceContextPropagator,
};
use tracing_opentelemetry::OpenTelemetrySpanExt;

pub fn inject_context(injector: &mut dyn Injector) {
TraceContextPropagator::new().inject_context(&tracing::Span::current().context(), injector);
#[cfg(not(feature = "tracing_opentelemetry_0_21"))]
#[cfg(feature = "tracing_opentelemetry_0_20")]
mod otel_0_20 {
pub use opentelemetry_0_20_pkg::{
propagation::{Injector, TextMapPropagator},
sdk::propagation::TraceContextPropagator,
};
pub use tracing_opentelemetry_0_21_pkg::OpenTelemetrySpanExt;

pub fn inject_context(injector: &mut dyn Injector) {
TraceContextPropagator::new().inject_context(&tracing::Span::current().context(), injector);
}
}

#[cfg(feature = "tracing_opentelemetry_0_21")]
mod otel_0_21 {
pub use opentelemetry_0_21_pkg::propagation::{Injector, TextMapPropagator};
pub use opentelemetry_sdk_0_21_pkg::propagation::TraceContextPropagator;
pub use tracing_opentelemetry_0_22_pkg::OpenTelemetrySpanExt;

pub fn inject_context(injector: &mut dyn Injector) {
TraceContextPropagator::new().inject_context(&tracing::Span::current().context(), injector);
}
}

#[cfg(not(feature = "tracing_opentelemetry_0_21"))]
#[cfg(feature = "tracing_opentelemetry_0_20")]
pub use otel_0_20::inject_context;

#[cfg(feature = "tracing_opentelemetry_0_21")]
pub use otel_0_21::inject_context;
10 changes: 5 additions & 5 deletions src/request/request_type/graphql.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::{HashMap, VecDeque};
use std::convert::TryInto;

use std::time::Duration;

use async_trait::async_trait;
Expand Down Expand Up @@ -43,7 +43,7 @@ impl<'a, Client: BridgeClient> GraphQLRequest<'a, Client> {
Ok(Self {
id: Uuid::new_v4(),
bridge,
body: serde_json::to_string(&graphql_body.into())?.try_into()?,
body: Body::from(serde_json::to_string(&graphql_body.into())?),
method: Method::POST,
path: Default::default(),
timeout: Duration::from_secs(60),
Expand Down Expand Up @@ -81,7 +81,7 @@ impl<'a, Client: BridgeClient> GraphQLRequest<'a, Client> {
Ok(Self {
id: Uuid::new_v4(),
bridge,
body: serde_json::to_string(&body_with_injected_variables)?.try_into()?,
body: Body::from(serde_json::to_string(&body_with_injected_variables)?),
method: Method::POST,
path: Default::default(),
timeout: Duration::from_secs(60),
Expand All @@ -105,7 +105,7 @@ impl<'a, Client: BridgeClient> DeliverableRequest<'a> for GraphQLRequest<'a, Cli

fn json_body<B: Serialize>(self, body: &B) -> PrimaBridgeResult<Self> {
Ok(Self {
body: serde_json::to_string(body)?.try_into()?,
body: Body::from(serde_json::to_string(body)?),
..self
})
}
Expand Down Expand Up @@ -334,7 +334,7 @@ mod tests {
use super::*;
use crate::Bridge;
use serde::Deserialize;
use serde_json::{json, Value};
use serde_json::json;
use std::str::FromStr;

#[derive(Deserialize)]
Expand Down
3 changes: 1 addition & 2 deletions src/request/request_type/rest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::convert::TryInto;
use std::time::Duration;
use std::{borrow::Cow, collections::HashSet};

Expand Down Expand Up @@ -72,7 +71,7 @@ impl<'a, Client: BridgeClient> DeliverableRequest<'a> for RestRequest<'a, Client
let mut custom_headers = self.custom_headers;
custom_headers.append(CONTENT_TYPE, HeaderValue::from_static("application/json"));
Ok(Self {
body: Some(serde_json::to_string(body)?.try_into()?),
body: Some(Body::from(serde_json::to_string(body)?)),
custom_headers,
..self
})
Expand Down

0 comments on commit c584ab1

Please sign in to comment.