Skip to content

Commit f1bd3d4

Browse files
committed
✨ allow to create span for opentelemetry at level info with feature flag tracing_level_info
FIX #126
1 parent 23c3138 commit f1bd3d4

File tree

11 files changed

+92
-23
lines changed

11 files changed

+92
-23
lines changed

examples/load/src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::time::Instant;
22

33
use memory_stats::memory_stats;
44
use tracing::field::Empty;
5-
use tracing_opentelemetry_instrumentation_sdk::TRACING_TARGET;
5+
use tracing_opentelemetry_instrumentation_sdk::otel_trace_span;
66

77
#[tokio::main]
88
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -25,8 +25,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2525
);
2626
}
2727
for _i in 1..10000 {
28-
let _span = tracing::trace_span!(
29-
target: TRACING_TARGET,
28+
let _span = otel_trace_span!(
3029
"Load",
3130
http.request.method = "GET",
3231
http.route = Empty,

tonic-tracing-opentelemetry/src/tracing_opentelemetry_instrumentation_sdk/http/grpc_client.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::error::Error;
22

33
use super::super::http::{extract_service_method, http_host, user_agent};
4-
use super::super::TRACING_TARGET;
4+
use super::super::otel_trace_span;
55
use tracing::field::Empty;
66

77
use super::grpc_update_span_from_response;
@@ -10,8 +10,7 @@ use super::grpc_update_span_from_response;
1010
//TODO create similar but with tonic::Request<B> ?
1111
pub fn make_span_from_request<B>(req: &http::Request<B>) -> tracing::Span {
1212
let (service, method) = extract_service_method(req.uri());
13-
tracing::trace_span!(
14-
target: TRACING_TARGET,
13+
otel_trace_span!(
1514
"GRPC request",
1615
http.user_agent = %user_agent(req),
1716
otel.name = format!("{service}/{method}"),

tonic-tracing-opentelemetry/src/tracing_opentelemetry_instrumentation_sdk/http/grpc_server.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::super::{BoxError, TRACING_TARGET};
1+
use super::super::{otel_trace_span, BoxError};
22
use super::{extract_service_method, http_host, user_agent};
33
use tracing::field::Empty;
44

@@ -8,8 +8,7 @@ use super::grpc_update_span_from_response;
88
/// see [Semantic Conventions for gRPC | OpenTelemetry](https://opentelemetry.io/docs/specs/semconv/rpc/grpc/#grpc-status)
99
pub fn make_span_from_request<B>(req: &http::Request<B>) -> tracing::Span {
1010
let (service, method) = extract_service_method(req.uri());
11-
tracing::trace_span!(
12-
target: TRACING_TARGET,
11+
otel_trace_span!(
1312
"GRPC request",
1413
http.user_agent = %user_agent(req),
1514
otel.name = format!("{service}/{method}"),

tonic-tracing-opentelemetry/src/tracing_opentelemetry_instrumentation_sdk/http/http_server.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::error::Error;
22

3+
use super::super::otel_trace_span;
34
use super::super::span_type::SpanType;
4-
use super::super::TRACING_TARGET;
55
use super::{http_flavor, http_host, http_method, url_scheme, user_agent};
66
use tracing::field::Empty;
77

@@ -10,8 +10,7 @@ pub fn make_span_from_request<B>(req: &http::Request<B>) -> tracing::Span {
1010
// [opentelemetry-specification/.../span-general.md](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/span-general.md)
1111
// Can not use const or opentelemetry_semantic_conventions::trace::* for name of records
1212
let http_method = http_method(req.method());
13-
tracing::trace_span!(
14-
target: TRACING_TARGET,
13+
otel_trace_span!(
1514
"HTTP request",
1615
http.request.method = %http_method,
1716
http.route = Empty, // to set by router of "webframework" after

tonic-tracing-opentelemetry/src/tracing_opentelemetry_instrumentation_sdk/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ use opentelemetry::Context;
1313
/// tracing's target used by instrumentation library to create span
1414
pub const TRACING_TARGET: &str = "otel::tracing";
1515

16+
#[cfg(not(feature = "tracing_level_info"))]
17+
pub const TRACING_LEVEL: tracing::Level = tracing::Level::TRACE;
18+
19+
#[cfg(feature = "tracing_level_info")]
20+
pub const TRACING_LEVEL: tracing::Level = tracing::Level::INFO;
21+
1622
// const SPAN_NAME_FIELD: &str = "otel.name";
1723
// const SPAN_KIND_FIELD: &str = "otel.kind";
1824
// const SPAN_STATUS_CODE_FIELD: &str = "otel.status_code";
@@ -22,6 +28,38 @@ pub const TRACING_TARGET: &str = "otel::tracing";
2228
// const FIELD_EXCEPTION_STACKTRACE: &str = "exception.stacktrace";
2329
// const HTTP_TARGET: &str = opentelemetry_semantic_conventions::trace::HTTP_TARGET.as_str();
2430

31+
/// Constructs a span for the target `TRACING_TARGET` with the level `TRACING_LEVEL`.
32+
///
33+
/// [Fields] and [attributes] are set using the same syntax as the [`tracing::span!`]
34+
/// macro.
35+
// #[macro_export]
36+
macro_rules! otel_trace_span {
37+
(parent: $parent:expr, $name:expr, $($field:tt)*) => {
38+
tracing::span!(
39+
target: $crate::tracing_opentelemetry_instrumentation_sdk::TRACING_TARGET,
40+
parent: $parent,
41+
$crate::tracing_opentelemetry_instrumentation_sdk::TRACING_LEVEL,
42+
$name,
43+
$($field)*
44+
)
45+
};
46+
(parent: $parent:expr, $name:expr) => {
47+
$crate::otel_trace_span!(parent: $parent, $name,)
48+
};
49+
($name:expr, $($field:tt)*) => {
50+
tracing::span!(
51+
target: $crate::tracing_opentelemetry_instrumentation_sdk::TRACING_TARGET,
52+
$crate::tracing_opentelemetry_instrumentation_sdk::TRACING_LEVEL,
53+
$name,
54+
$($field)*
55+
)
56+
};
57+
($name:expr) => {
58+
$crate::otel_trace_span!($name,)
59+
};
60+
}
61+
pub(crate) use otel_trace_span;
62+
2563
#[inline]
2664
#[must_use]
2765
pub fn find_current_context() -> Context {

tracing-opentelemetry-instrumentation-sdk/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ rstest = "0.18"
2828
[features]
2929
default = []
3030
http = ["dep:http"]
31+
# to use level `info` instead of `trace` to create otel span
32+
tracing_level_info = []

tracing-opentelemetry-instrumentation-sdk/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ Use `tracing::instrumented` (no propagation & no update on response)
4949
fn make_otel_span(db_operation: &str) -> tracing::Span {
5050
// NO parsing of statement to extract information, not recommended by Specification and time-consuming
5151
// warning: providing the statement could leek information
52-
tracing::trace_span!(
53-
target: tracing_opentelemetry_instrumentation_sdk::TRACING_TARGET,
52+
tracing_opentelemetry_instrumentation_sdk::otel_trace_span!(
5453
"DB request",
5554
db.system = "postgresql",
5655
// db.statement = stmt,

tracing-opentelemetry-instrumentation-sdk/src/http/grpc_client.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::error::Error;
22

33
use crate::http::{extract_service_method, http_host, user_agent};
4-
use crate::TRACING_TARGET;
4+
use crate::otel_trace_span;
55
use tracing::field::Empty;
66

77
use super::grpc_update_span_from_response;
@@ -10,8 +10,7 @@ use super::grpc_update_span_from_response;
1010
//TODO create similar but with tonic::Request<B> ?
1111
pub fn make_span_from_request<B>(req: &http::Request<B>) -> tracing::Span {
1212
let (service, method) = extract_service_method(req.uri());
13-
tracing::trace_span!(
14-
target: TRACING_TARGET,
13+
otel_trace_span!(
1514
"GRPC request",
1615
http.user_agent = %user_agent(req),
1716
otel.name = format!("{service}/{method}"),

tracing-opentelemetry-instrumentation-sdk/src/http/grpc_server.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::http::{extract_service_method, http_host, user_agent};
2-
use crate::{BoxError, TRACING_TARGET};
2+
use crate::{otel_trace_span, BoxError};
33
use tracing::field::Empty;
44

55
use super::grpc_update_span_from_response;
@@ -8,8 +8,7 @@ use super::grpc_update_span_from_response;
88
/// see [Semantic Conventions for gRPC | OpenTelemetry](https://opentelemetry.io/docs/specs/semconv/rpc/grpc/#grpc-status)
99
pub fn make_span_from_request<B>(req: &http::Request<B>) -> tracing::Span {
1010
let (service, method) = extract_service_method(req.uri());
11-
tracing::trace_span!(
12-
target: TRACING_TARGET,
11+
otel_trace_span!(
1312
"GRPC request",
1413
http.user_agent = %user_agent(req),
1514
otel.name = format!("{service}/{method}"),

tracing-opentelemetry-instrumentation-sdk/src/http/http_server.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
use std::error::Error;
22

33
use crate::http::{http_flavor, http_host, http_method, url_scheme, user_agent};
4+
use crate::otel_trace_span;
45
use crate::span_type::SpanType;
5-
use crate::TRACING_TARGET;
66
use tracing::field::Empty;
77

88
pub fn make_span_from_request<B>(req: &http::Request<B>) -> tracing::Span {
99
// [semantic-conventions/.../http-spans.md](https://github.com/open-telemetry/semantic-conventions/blob/v1.25.0/docs/http/http-spans.md)
1010
// [semantic-conventions/.../general/attributes.md](https://github.com/open-telemetry/semantic-conventions/blob/v1.25.0/docs/general/attributes.md)
1111
// Can not use const or opentelemetry_semantic_conventions::trace::* for name of records
1212
let http_method = http_method(req.method());
13-
tracing::trace_span!(
14-
target: TRACING_TARGET,
13+
otel_trace_span!(
1514
"HTTP request",
1615
http.request.method = %http_method,
1716
http.route = Empty, // to set by router of "webframework" after

0 commit comments

Comments
 (0)