Stacked column chart for average values of arbitrary keysAverage timeslice

Comments

4 comments

  • Avatar
    Jorge Silva


    Give this query a try:

    _sourceCategory="<source>" AND "<filter>"
    | json field=_raw "my_field" as my_field
    | parse regex field=my_field "(?<key>[^\"]+?)\"\s*:(?<value>\d+\.\d+)" multi
    | fields -_raw
    | timeslice 2m
    | avg(value) by _timeslice, key
    | transpose row _timeslice column key

    Here we use parse regex to extract each key and its value, then we average the value by timeslice and key and finally we use the "transpose" operator to create your chart.

    1
    Comment actions Permalink
  • Avatar
    Daniel Olshansky

    That works perfectly Jorge, thanks so much!

    Just for anyone else who might stumble upon this post, the above query can be used to easily created a stacked column chart where different log lines have an arbitrary number of key-value pairs and the keys are not known ahead of time, like so:

    Source:

    log1: {"key1": 1} 
    log2: {"key1": 2, "key2": 3} 
    log3: {"key1": 4, "key2": 5, "key3": 6}

    Output:

    key1: avg(1, 2, 4) 
    key2: avg(3, 5) 
    key3: avg(6)
    0
    Comment actions Permalink
  • Avatar
    Anthony Manning-Franklin

    I'm having a similar issue, except I want to work json values, so I have an object schema like

    ```

    {
      data: {
    [SOME_UNKNOWN_KEY]: {
    avgMs: number,
    sumMs: number,
    frequency: number,
    minMs: number,
    maxMs: number
    }
    }
    }

    I want to perform aggregation, analysis, and build stacked area line charts where each [SOME_UNKNOWN_KEY] is a line, and I chart one of its numeric values over time.

    0
    Comment actions Permalink
  • Avatar
    Anthony Manning-Franklin

    Wound up solving this one myself with

    | parse regex field=%message.data "(?<key>[^\"]*?)\"\s*:(?<value>[^}]*})" multi
    | json field=value "{{Field}}"
    | fields -_raw
    | timeslice 30m
    | avg({{Field}}) by _timeslice, key
    | transpose row _timeslice column key

    Where Field is one of avgMs, sumMs, maxMs, minMs, or frequency

    I just changed the regex to capture an object, and then ran | json on it again

    0
    Comment actions Permalink

Please sign in to leave a comment.