Skip to content

[feature request] Parsing 1 JSON Blob per line (log analysis) #638

Closed

Description

@rrichardson

I find myself looking at output like :

{"level":20,"time":1568138958880,"pid":41,"hostname":"metal-manager-6f8756975-l5kmf","reqId":75,"msg":"DEBUG: StateTransition: from=boot-1 to=boot-1 mac=08:00:27:57:3f:ac","v":1}
{"level":30,"time":1568138958884,"pid":41,"hostname":"metal-manager-6f8756975-l5kmf","reqId":75,"res":{"statusCode":200},"responseTime":8.08464601635933,"msg":"request completed","v":1}
{"level":30,"time":1568138959571,"pid":41,"hostname":"metal-manager-6f8756975-l5kmf","reqId":76,"req":{"method":"GET","url":"/api/v1/cluster/3/discovery/initrd/08:00:27:57:3f:ac","hostname":"api.app.rick0i.dev.superhub.io","remoteAd
dress":"10.2.0.27","remotePort":44214},"msg":"incoming request","v":1}
{"level":20,"time":1568138959574,"pid":41,"hostname":"metal-manager-6f8756975-l5kmf","reqId":76,"msg":"DEBUG: StateTransition: from=boot-1 to=discover mac=08:00:27:57:3f:ac","v":1}
{"level":30,"time":1568138959576,"pid":41,"hostname":"metal-manager-6f8756975-l5kmf","reqId":76,"res":{"statusCode":200},"responseTime":5.444788008928299,"msg":"request completed","v":1}
{"level":30,"time":1568138969953,"pid":41,"hostname":"metal-manager-6f8756975-l5kmf","reqId":77,"req":{"method":"POST","url":"/api/v1/cluster/3/discovery/facts/08:00:27:5d:fa:f8","hostname":"api.app.rick0i.dev.superhub.io","remoteAd
dress":"10.2.0.27","remotePort":44398},"msg":"incoming request","v":1}

I would like to filter/cut and sort it. It would be nice if there was a --lines switch for the from-json function.

Activity

  1. wycats commented on Sep 10, 2019

    @wycats
    Contributor

    Can you pipe to lines?

  2. sophiajt commented on Sep 10, 2019

    @sophiajt
    Contributor

    @wycats that won't work as from-json will collect the lines. The suggestion here, I think, is to have two modes for converting files. Sometimes, as when you're working with stdout from an external command, you want to collect up all the lines before you do conversion. Other times, you may want to do what the author here requests, which is to operate over the lines individually.

  3. andrasio commented on Sep 10, 2019

    @andrasio
    Member

    @rrichardson As a workaround, save your output to file.txt like so:

    contents of file.txt
    {"level":20,"time":1568138958880,"pid":41,"hostname":"metal-manager-6f8756975-l5kmf","reqId":75,"msg":"DEBUG: StateTransition: from=boot-1 to=boot-1 mac=08:00:27:57:3f:ac","v":1}
    {"level":30,"time":1568138958884,"pid":41,"hostname":"metal-manager-6f8756975-l5kmf","reqId":75,"res":{"statusCode":200},"responseTime":8.08464601635933,"msg":"request completed","v":1}
    {"level":30,"time":1568138959571,"pid":41,"hostname":"metal-manager-6f8756975-l5kmf","reqId":76,"req":{"method":"GET","url":"/api/v1/cluster/3/discovery/initrd/08:00:27:57:3f:ac","hostname":"api.app.rick0i.dev.superhub.io","remoteAddress":"10.2.0.27","remotePort":44214},"msg":"incoming request","v":1}
    {"level":20,"time":1568138959574,"pid":41,"hostname":"metal-manager-6f8756975-l5kmf","reqId":76,"msg":"DEBUG: StateTransition: from=boot-1 to=discover mac=08:00:27:57:3f:ac","v":1}
    {"level":30,"time":1568138959576,"pid":41,"hostname":"metal-manager-6f8756975-l5kmf","reqId":76,"res":{"statusCode":200},"responseTime":5.444788008928299,"msg":"request completed","v":1}
    {"level":30,"time":1568138969953,"pid":41,"hostname":"metal-manager-6f8756975-l5kmf","reqId":77,"req":{"method":"POST","url":"/api/v1/cluster/3/discovery/facts/08:00:27:5d:fa:f8","hostname":"api.app.rick0i.dev.superhub.io","remoteAddress":"10.2.0.27","remotePort":44398},"msg":"incoming request","v":1}
    

    Followed by open file.txt | from-json --objects

    > open file.txt | from-json --objects
    ━━━┯━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━
     # │ level │ time          │ pid │ hostname                 │ reqId │ msg                     │ v │ res            │ responseTime      │ req
    ───┼───────┼───────────────┼─────┼──────────────────────────┼───────┼─────────────────────────┼───┼────────────────┼───────────────────┼────────────────
     0 │    20 │ 1568138958880 │  41 │ metal-manager-6f8756975- │    75 │ DEBUG: StateTransition: │ 1 │                │                   │
       │       │               │     │ l5kmf                    │       │ from=boot-1 to=boot-1   │   │                │                   │
       │       │               │     │                          │       │ mac=08:00:27:57:3f:ac   │   │                │                   │
     1 │    30 │ 1568138958884 │  41 │ metal-manager-6f8756975- │    75 │ request completed       │ 1 │ [table: 1 row] │ 8.084646016359329 │
       │       │               │     │ l5kmf                    │       │                         │   │                │                   │
     2 │    30 │ 1568138959571 │  41 │ metal-manager-6f8756975- │    76 │ incoming request        │ 1 │                │                   │ [table: 1 row]
       │       │               │     │ l5kmf                    │       │                         │   │                │                   │
     3 │    20 │ 1568138959574 │  41 │ metal-manager-6f8756975- │    76 │ DEBUG: StateTransition: │ 1 │                │                   │
       │       │               │     │ l5kmf                    │       │ from=boot-1 to=discover │   │                │                   │
       │       │               │     │                          │       │ mac=08:00:27:57:3f:ac   │   │                │                   │
     4 │    30 │ 1568138959576 │  41 │ metal-manager-6f8756975- │    76 │ request completed       │ 1 │ [table: 1 row] │ 5.444788008928299 │
       │       │               │     │ l5kmf                    │       │                         │   │                │                   │
     5 │    30 │ 1568138969953 │  41 │ metal-manager-6f8756975- │    77 │ incoming request        │ 1 │                │                   │ [table: 1 row]
       │       │               │     │ l5kmf                    │       │                         │   │                │                   │
    ━━━┷━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━
    

    It should work equally well if you pipe your output as long as each line is a string json object and you are using --objects flag for from-json

  4. andrasio commented on Sep 10, 2019

    @andrasio
    Member

    @rrichardson I guess this is what you were asking. The flag is --objects, should we rename it to --per-line (as @jonathandturner suggested before) @wycats ?

    What do you think? @rrichardson

  5. rrichardson commented on Sep 10, 2019

    @rrichardson
    ContributorAuthor

    --per-line is more clear to me than --objects

    So this works for open but not for piping from stdout? What is the disconnect there?

    (I just tested this trying to pipe from my log source and it hung)

    kubectl logs mysvc | from-json --objects
    ^C
    thread 'main' panicked at 'index out of bounds: the len is 2 but the index is 2', /rustc/9eae1fc0ea9b00341b8fe191582c4decb5cb86a3/src/libcore/slice/mod.rs:2715:10
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
    
  6. andrasio commented on Sep 10, 2019

    @andrasio
    Member

    @rrichardson It should work for piping from stdout. Please, could you file a new issue with your message and, if you can, also paste the output from kubectl logs mysvc ?

  7. andrasio commented on Sep 10, 2019

    @andrasio
    Member

    @rrichardson also paste the output when running with RUST_BACKTRACE=1 if you can. Thank you!

  8. rrichardson commented on Sep 10, 2019

    @rrichardson
    ContributorAuthor

    @androbtech - Actually, it's not as bad as I first thought. I had some non-json lines in the log. I filtered past those and | from-json --objects worked.

    There is still an issue, though, and that is if JSON parsing fails, the operation hangs.

  9. rrichardson commented on Sep 10, 2019

    @rrichardson
    ContributorAuthor

    I have updated #514 , it seemed close enough
    #514 (comment)

  10. added a commit that references this issue on Feb 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions