Skip to content

Conversation

@heat1q
Copy link

@heat1q heat1q commented Aug 30, 2023

Motivation

Span fields are currently not accessible at root level but only inside a nested object or the span list. This makes indexing log data more complex if a unique span identifier is required at root level, for example in the case of distributed trace Id (See also #1531).

Solution

This change adds two new options to Json for making span fields accessible at root level:

  • Json::flatten_current_span flattens all span fields into the root object. The name field is not carried over, since it might be ambiguous at root level.
  • Json::flatten_span_list flattens all fields for each span in the list into the root object. Colliding fields will be overwritten from root to leaf span.

Examples

Default

{
  "timestamp": "2023-08-30T13:28:57.999070Z",
  "level": "INFO",
  "fields": {
    "message": "shaving yaks"
  },
  "target": "fmt_json::yak_shave",
  "span": {
    "yaks": 3,
    "name": "shaving_yaks"
  },
  "spans": [
    {
      "yaks": 3,
      "name": "shaving_yaks"
    }
  ]
}

Flatten only current span

All fields (except name) of the current span are present at the root level.

tracing_subscriber::fmt()
    .json()
    .flatten_current_span(true)
    .flatten_span_list(false)
    .init();
{
  "timestamp": "2023-08-30T13:30:22.753644Z",
  "level": "INFO",
  "fields": {
    "message": "shaving yaks"
  },
  "target": "fmt_json::yak_shave",
  "yaks": 3,
  "spans": [
    {
      "yaks": 3,
      "name": "shaving_yaks"
    }
  ]
}

Flatten current span and span list

tracing_subscriber::fmt()
    .json()
    .flatten_current_span(true)
    .flatten_span_list(true)
    .init();
{
  "timestamp": "2023-08-30T13:31:17.733524Z",
  "level": "INFO",
  "fields": {
    "message": "shaving yaks"
  },
  "target": "fmt_json::yak_shave",
  "yaks": 3
}

Resolves: #2670

@heat1q heat1q requested review from a team, davidbarsky and hawkw as code owners August 30, 2023 13:47
@msabansal
Copy link

Is there any followup needed on this pr? Can we get this reviewed please.

@heat1q heat1q force-pushed the feat/subscriber-flat-spans branch from a52dde2 to 78929e7 Compare December 22, 2023 14:06
@heat1q
Copy link
Author

heat1q commented Jan 18, 2024

Is there any followup needed on this pr? Can we get this reviewed please.

Nothing from my side. We need to wait for the maintainers to take a look.

@coltfred
Copy link

I'd love to see this added as well. We're using spans to track ray_ids for our HTTP requests and want to be able to get rid of the span name and flatten the ray_id onto the top object.

@Turbo87
Copy link
Contributor

Turbo87 commented Oct 19, 2024

@hawkw @davidbarsky @hds any chance one of you could take a look? we would like to use this for crates.io too :)

@heat1q heat1q force-pushed the feat/subscriber-flat-spans branch from 78929e7 to 5d3cc11 Compare October 23, 2024 06:40
@mladedav
Copy link
Contributor

For anyone interested, I've created a json-subscriber and I'm trying to implement all these long standing feature requests there (or making it general enough that people can set most things themselves). I've added flattening of spans there too.

@dspatoulas
Copy link

dspatoulas commented Oct 30, 2024

Thank you @heat1q! This is exactly what I was hoping to find after enabling flatten_events and realizing that's only half the battle.

@rminderhoud
Copy link

Glad to see this PR thanks @heat1q, would also find this functionality very useful

Jesse-Bakker

This comment was marked as off-topic.

@heat1q heat1q force-pushed the feat/subscriber-flat-spans branch from 5d3cc11 to 3afe022 Compare September 2, 2025 15:13
@heat1q heat1q force-pushed the feat/subscriber-flat-spans branch 2 times, most recently from 5d3cc11 to 3afe022 Compare September 2, 2025 15:23
@heat1q heat1q changed the base branch from master to main September 2, 2025 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tracing-subscriber: Consider adding flattening option for spans

8 participants