Skip to content

Commit

Permalink
✨ allow to create span for opentelemetry at level info with feature…
Browse files Browse the repository at this point in the history
… flag `tracing_level_info`

FIX #126
  • Loading branch information
davidB committed Apr 24, 2024
1 parent 23c3138 commit f1bd3d4
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 23 deletions.
5 changes: 2 additions & 3 deletions examples/load/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::Instant;

use memory_stats::memory_stats;
use tracing::field::Empty;
use tracing_opentelemetry_instrumentation_sdk::TRACING_TARGET;
use tracing_opentelemetry_instrumentation_sdk::otel_trace_span;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -25,8 +25,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
);
}
for _i in 1..10000 {
let _span = tracing::trace_span!(
target: TRACING_TARGET,
let _span = otel_trace_span!(
"Load",
http.request.method = "GET",
http.route = Empty,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::error::Error;

use super::super::http::{extract_service_method, http_host, user_agent};
use super::super::TRACING_TARGET;
use super::super::otel_trace_span;
use tracing::field::Empty;

use super::grpc_update_span_from_response;
Expand All @@ -10,8 +10,7 @@ use super::grpc_update_span_from_response;
//TODO create similar but with tonic::Request<B> ?
pub fn make_span_from_request<B>(req: &http::Request<B>) -> tracing::Span {
let (service, method) = extract_service_method(req.uri());
tracing::trace_span!(
target: TRACING_TARGET,
otel_trace_span!(
"GRPC request",
http.user_agent = %user_agent(req),
otel.name = format!("{service}/{method}"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::super::{BoxError, TRACING_TARGET};
use super::super::{otel_trace_span, BoxError};
use super::{extract_service_method, http_host, user_agent};
use tracing::field::Empty;

Expand All @@ -8,8 +8,7 @@ use super::grpc_update_span_from_response;
/// see [Semantic Conventions for gRPC | OpenTelemetry](https://opentelemetry.io/docs/specs/semconv/rpc/grpc/#grpc-status)
pub fn make_span_from_request<B>(req: &http::Request<B>) -> tracing::Span {
let (service, method) = extract_service_method(req.uri());
tracing::trace_span!(
target: TRACING_TARGET,
otel_trace_span!(
"GRPC request",
http.user_agent = %user_agent(req),
otel.name = format!("{service}/{method}"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::error::Error;

use super::super::otel_trace_span;
use super::super::span_type::SpanType;
use super::super::TRACING_TARGET;
use super::{http_flavor, http_host, http_method, url_scheme, user_agent};
use tracing::field::Empty;

Expand All @@ -10,8 +10,7 @@ pub fn make_span_from_request<B>(req: &http::Request<B>) -> tracing::Span {
// [opentelemetry-specification/.../span-general.md](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/span-general.md)
// Can not use const or opentelemetry_semantic_conventions::trace::* for name of records
let http_method = http_method(req.method());
tracing::trace_span!(
target: TRACING_TARGET,
otel_trace_span!(
"HTTP request",
http.request.method = %http_method,
http.route = Empty, // to set by router of "webframework" after
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ use opentelemetry::Context;
/// tracing's target used by instrumentation library to create span
pub const TRACING_TARGET: &str = "otel::tracing";

#[cfg(not(feature = "tracing_level_info"))]
pub const TRACING_LEVEL: tracing::Level = tracing::Level::TRACE;

#[cfg(feature = "tracing_level_info")]
pub const TRACING_LEVEL: tracing::Level = tracing::Level::INFO;

// const SPAN_NAME_FIELD: &str = "otel.name";
// const SPAN_KIND_FIELD: &str = "otel.kind";
// const SPAN_STATUS_CODE_FIELD: &str = "otel.status_code";
Expand All @@ -22,6 +28,38 @@ pub const TRACING_TARGET: &str = "otel::tracing";
// const FIELD_EXCEPTION_STACKTRACE: &str = "exception.stacktrace";
// const HTTP_TARGET: &str = opentelemetry_semantic_conventions::trace::HTTP_TARGET.as_str();

/// Constructs a span for the target `TRACING_TARGET` with the level `TRACING_LEVEL`.
///
/// [Fields] and [attributes] are set using the same syntax as the [`tracing::span!`]
/// macro.
// #[macro_export]
macro_rules! otel_trace_span {
(parent: $parent:expr, $name:expr, $($field:tt)*) => {
tracing::span!(
target: $crate::tracing_opentelemetry_instrumentation_sdk::TRACING_TARGET,
parent: $parent,
$crate::tracing_opentelemetry_instrumentation_sdk::TRACING_LEVEL,
$name,
$($field)*
)
};
(parent: $parent:expr, $name:expr) => {
$crate::otel_trace_span!(parent: $parent, $name,)
};
($name:expr, $($field:tt)*) => {
tracing::span!(
target: $crate::tracing_opentelemetry_instrumentation_sdk::TRACING_TARGET,
$crate::tracing_opentelemetry_instrumentation_sdk::TRACING_LEVEL,
$name,
$($field)*
)
};
($name:expr) => {
$crate::otel_trace_span!($name,)
};
}
pub(crate) use otel_trace_span;

#[inline]
#[must_use]
pub fn find_current_context() -> Context {
Expand Down
2 changes: 2 additions & 0 deletions tracing-opentelemetry-instrumentation-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ rstest = "0.18"
[features]
default = []
http = ["dep:http"]
# to use level `info` instead of `trace` to create otel span
tracing_level_info = []
3 changes: 1 addition & 2 deletions tracing-opentelemetry-instrumentation-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ Use `tracing::instrumented` (no propagation & no update on response)
fn make_otel_span(db_operation: &str) -> tracing::Span {
// NO parsing of statement to extract information, not recommended by Specification and time-consuming
// warning: providing the statement could leek information
tracing::trace_span!(
target: tracing_opentelemetry_instrumentation_sdk::TRACING_TARGET,
tracing_opentelemetry_instrumentation_sdk::otel_trace_span!(
"DB request",
db.system = "postgresql",
// db.statement = stmt,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::error::Error;

use crate::http::{extract_service_method, http_host, user_agent};
use crate::TRACING_TARGET;
use crate::otel_trace_span;
use tracing::field::Empty;

use super::grpc_update_span_from_response;
Expand All @@ -10,8 +10,7 @@ use super::grpc_update_span_from_response;
//TODO create similar but with tonic::Request<B> ?
pub fn make_span_from_request<B>(req: &http::Request<B>) -> tracing::Span {
let (service, method) = extract_service_method(req.uri());
tracing::trace_span!(
target: TRACING_TARGET,
otel_trace_span!(
"GRPC request",
http.user_agent = %user_agent(req),
otel.name = format!("{service}/{method}"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::http::{extract_service_method, http_host, user_agent};
use crate::{BoxError, TRACING_TARGET};
use crate::{otel_trace_span, BoxError};
use tracing::field::Empty;

use super::grpc_update_span_from_response;
Expand All @@ -8,8 +8,7 @@ use super::grpc_update_span_from_response;
/// see [Semantic Conventions for gRPC | OpenTelemetry](https://opentelemetry.io/docs/specs/semconv/rpc/grpc/#grpc-status)
pub fn make_span_from_request<B>(req: &http::Request<B>) -> tracing::Span {
let (service, method) = extract_service_method(req.uri());
tracing::trace_span!(
target: TRACING_TARGET,
otel_trace_span!(
"GRPC request",
http.user_agent = %user_agent(req),
otel.name = format!("{service}/{method}"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use std::error::Error;

use crate::http::{http_flavor, http_host, http_method, url_scheme, user_agent};
use crate::otel_trace_span;
use crate::span_type::SpanType;
use crate::TRACING_TARGET;
use tracing::field::Empty;

pub fn make_span_from_request<B>(req: &http::Request<B>) -> tracing::Span {
// [semantic-conventions/.../http-spans.md](https://github.com/open-telemetry/semantic-conventions/blob/v1.25.0/docs/http/http-spans.md)
// [semantic-conventions/.../general/attributes.md](https://github.com/open-telemetry/semantic-conventions/blob/v1.25.0/docs/general/attributes.md)
// Can not use const or opentelemetry_semantic_conventions::trace::* for name of records
let http_method = http_method(req.method());
tracing::trace_span!(
target: TRACING_TARGET,
otel_trace_span!(
"HTTP request",
http.request.method = %http_method,
http.route = Empty, // to set by router of "webframework" after
Expand Down
37 changes: 37 additions & 0 deletions tracing-opentelemetry-instrumentation-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ use opentelemetry::Context;
/// tracing's target used by instrumentation library to create span
pub const TRACING_TARGET: &str = "otel::tracing";

#[cfg(not(feature = "tracing_level_info"))]
pub const TRACING_LEVEL: tracing::Level = tracing::Level::TRACE;

#[cfg(feature = "tracing_level_info")]
pub const TRACING_LEVEL: tracing::Level = tracing::Level::INFO;

// const SPAN_NAME_FIELD: &str = "otel.name";
// const SPAN_KIND_FIELD: &str = "otel.kind";
// const SPAN_STATUS_CODE_FIELD: &str = "otel.status_code";
Expand All @@ -23,6 +29,37 @@ pub const TRACING_TARGET: &str = "otel::tracing";
// const FIELD_EXCEPTION_STACKTRACE: &str = "exception.stacktrace";
// const HTTP_TARGET: &str = opentelemetry_semantic_conventions::trace::HTTP_TARGET.as_str();

/// Constructs a span for the target `TRACING_TARGET` with the level `TRACING_LEVEL`.
///
/// [Fields] and [attributes] are set using the same syntax as the [`tracing::span!`]
/// macro.
#[macro_export]
macro_rules! otel_trace_span {
(parent: $parent:expr, $name:expr, $($field:tt)*) => {
tracing::span!(
target: $crate::TRACING_TARGET,
parent: $parent,
$crate::TRACING_LEVEL,
$name,
$($field)*
)
};
(parent: $parent:expr, $name:expr) => {
$crate::otel_trace_span!(parent: $parent, $name,)
};
($name:expr, $($field:tt)*) => {
tracing::span!(
target: $crate::TRACING_TARGET,
$crate::TRACING_LEVEL,
$name,
$($field)*
)
};
($name:expr) => {
$crate::otel_trace_span!($name,)
};
}

#[inline]
#[must_use]
pub fn find_current_context() -> Context {
Expand Down

0 comments on commit f1bd3d4

Please sign in to comment.