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

subscriber: add flatten span option to json formatter #2705

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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 hawkw, davidbarsky and a team 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.

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
6 participants