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

tracing::instrument doesn't support fields as strings or raw identifiers when keywords are involved #2438

Open
goodspark opened this issue Jan 9, 2023 · 1 comment · May be fixed by #2924 or #2606

Comments

@goodspark
Copy link

goodspark commented Jan 9, 2023

Bug Report

Version

Rust nightly-2021-11-16
tracing v0.1.37
tracing-attributes v0.1.23
tracing-code v0.1.30
tracing-futures v0.2.5
tracing-log v0.1.3
tracing-opentelemetry v0.15.0
tracing-serde v0.1.3
tracing-subscriber v0.2.25

Platform

Ubuntu 18.04, 64bit
Linux my-machine 5.4.0-1092-gcp #101~18.04.1-Ubuntu SMP Mon Oct 17 18:29:06 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Description

When I specify a field with type in the name in the instrument macro, it fails.

#[instrument(fields(thing.type="blah"))
fn blah() {}
error: expected identifier, found keyword `type`
324 | #[instrument(fields(thing.type="blah"))]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected identifier, found keyword

Putting it in a string doesn't work, even though this works with span! syntax.

#[instrument(fields("thing.type"="blah"))]
fn blah() {}
error: expected ident
324 | #[instrument(fields("thing.type"="blah"))]
    |                     ^^^^^^^^^^^^

Trying to use raw identifiers compiles, but the wrong field name is recorded.

#[instrument(fields(thing.r#type="blah"))]
fn blah() {}

When I view my traces and events, the actual field name is thing.r#type, instead of thing.type.

I think of all the possible approaches, supporting strings as field names would be the most flexible and useful, especially since the span! macros already support that.

@valkum valkum linked a pull request Jun 1, 2023 that will close this issue
@dahlbaek
Copy link

dahlbaek commented Aug 3, 2023

I ran into a similar issue when trying to use tracing on GCP Cloud Run with the out-of-process log collector agent, where required the log format is described here https://cloud.google.com/logging/docs/structured-logging. In particular, I want to add a trace id and was not able to do so via

#[tracing::instrument(skip_all, fields("logging.googleapis.com/trace" = get_trace(headers)))]
async fn root(
    State(mut app_state): State<AppState>,
    headers: HeaderMap,
    Json(payload): Json<AppRequest>,
) -> impl IntoResponse {
    ...
}

I work around it by using the span_error! macro as well

async fn root(
    State(app_state): State<AppState>,
    headers: HeaderMap,
    Json(payload): Json<AppRequest>,
) -> impl IntoResponse {
    let root_span =
        tracing::error_span!("root", "logging.googleapis.com/trace" = get_trace(headers));
    root_impl(app_state, payload).instrument(root_span).await
}

async fn root_impl(mut app_state: AppState, payload: AppRequest) -> impl IntoResponse {
    ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants