技术控

    今日:41| 主题:49101
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Structuring data with Logstash

[复制链接]
浅忆︿梦微凉 发表于 5 天前
84 2

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Given the trend around microservices, it has become mandatory to be able to follow a transaction across multiple microservices.    Spring Cloud Sleuthis such a distributed tracing system fully integrated into the Spring Boot ecosystem. By adding the    spring-cloud-starter-sleuthinto a project’s POM, it instantly becomes Sleuth-enabled and every standard log call automatically adds additional data, such as    spanIdand    traceIdto the usual data.  
  [code]2016-11-25 19:05:53.221  INFO [demo-app,b4d33156bc6a49ec,432b43172c958450,false] 25305 ---\n
[nio-8080-exec-1] ch.frankel.blog.SleuthDemoApplication      : this is an example message[/code]  (broken on 2 lines for better readability)
  Now, instead of sending the data to Zipkin, let’s say I need to store it into Elasticsearch instead. A product is as good as the way it’s used. Indexing unstructured log messages is not very useful. Logstash configuration allows to pre-parse unstructured data and send structured data instead.
  Grok

      Grokkingdata is the usual way to structure data with pattern matching.  
  Last week, I wrote about    some hintsfor the configuration. Unfortunately, the hard part comes in writing the matching pattern itself, and those hints don’t help. While it might be possible to write a perfect Grok pattern on the first draft, the above log is complicated enough that it’s far from a certainty, and chances are high to stumble upon such message when starting Logstash with an unfit Grok filter:  
  [code]"tags" => [
    [0] "_grokparsefailure"
][/code]  However, there’s “an    appfor that” (sounds familiar?). It offers three fields:  
  
       
  • The first field accepts one (or more) log line(s)   
  • The second the grok pattern   
  • The 3      rdis the result of filtering the 1      stby the 2      nd  
      
Structuring data with Logstash-1 (additional,structured,multiple,standard,message)
  
  The process is now to match fields one by one, from left to right. The first data field    .e.g.    2016-11-25 19:05:53.221is obviously a timestamp. Among common    grok patterns, it looks as if the    TIMESTAMP_ISO8601pattern would be the best fit.  
  Enter    %{TIMESTAMP_ISO8601:timestamp}into the Pattern field. The result is:  
  [code]{
  "timestamp": [
    [
      "2016-11-25 17:05:53.221"
    ]
  ]
}
[/code]  The next field to handle looks like the log level. Among the patterns, there’s one    LOGLEVEL. The Pattern now becomes    %{TIMESTAMP_ISO8601:timestamp} *%{LOGLEVEL:level}and the result:  
  [code]{
  "timestamp": [
    [
      "2016-11-25 17:05:53.221"
    ]
  ],
  "level": [
    [
      "INFO"
    ]
  ]
}
[/code]  Rinse and repeat until all fields have been structured. Given the initial log line, the final pattern should look something along those lines:
  [code]%{TIMESTAMP_ISO8601:timestamp} *%{LOGLEVEL:level} \[%{DATA:application},%{DATA:traceId},%{DATA:spanId},%{DATA:zipkin}]\n
%{DATA:pid} --- *\[%{DATA:thread}] %{JAVACLASS:class} *: %{GREEDYDATA:log}[/code]  (broken on 2 lines for better readability)
  And the associated result:
  [code]{
        "traceId" => "b4d33156bc6a49ec",
         "zipkin" => "false",
          "level" => "INFO",
            "log" => "this is an example message",
            "pid" => "25305",
         "thread" => "nio-8080-exec-1",
           "tags" => [],
         "spanId" => "432b43172c958450",
           "path" => "/tmp/logstash.log",
     "@timestamp" => 2016-11-26T13:41:07.599Z,
    "application" => "demo-app",
       "@version" => "1",
           "host" => "LSNM33795267A",
          "class" => "ch.frankel.blog.SleuthDemoApplication",
      "timestamp" => "2016-11-25 17:05:53.221"
}
[/code]  Dissect

  The Grok filter gets the job done. But    it seemsto suffer from performance issues, especially if the pattern doesn’t match. An alternative is to use the    dissectfilter instead, which is based on    separators.  
  Unfortunately, there’s no app for that - but it’s much easier to write a separator-based filter than a regex-based one. The mapping equivalent to the above is:
  [code]%{timestamp} %{+timestamp} %{level}[%{application},%{traceId},%{spanId},%{zipkin}]\n
%{pid} %{}[%{thread}] %{class}:%{log}[/code]  (broken on 2 lines for better readability)
  This outputs the following:
  [code]{
        "traceId" => "b4d33156bc6a49ec",
         "zipkin" => "false",
            "log" => " this is an example message",
          "level" => "INFO ",
            "pid" => "25305",
         "thread" => "nio-8080-exec-1",
           "tags" => [],
         "spanId" => "432b43172c958450",
           "path" => "/tmp/logstash.log",
     "@timestamp" => 2016-11-26T13:36:47.165Z,
    "application" => "demo-app",
       "@version" => "1",
           "host" => "LSNM33795267A",
          "class" => "ch.frankel.blog.SleuthDemoApplication      ",
      "timestamp" => "2016-11-25 17:05:53.221"
}
[/code]  Notice the slight differences: by moving from a regex-based filter to a separator-based one, some strings end up padded with spaces. There are 2 ways to handle that:
  
       
  • change the logging pattern in the application - which might make direct log reading harder   
  • strip additional spaces with Logstash  
  With the second option, the final filter configuration snippet is:
  [code]filter {
  dissect {
        mapping => { "message" => ... }
  }
  mutate {
    strip => [ "log", "class" ]
  }
}[/code]  Conclusion

  In order to structure data, the    grokfilter is powerful and used by many. However, depending on the specific log format to parse, writing the filter expression might be quite complex a task. The    dissectfilter, based on separators, is an alternative that makes it much easier - at the price of some additional handling. It also is an option to consider in case of performance issues.
友荐云推荐




上一篇:ES6 Iterators and Generators: 6 Exercises and Solutions
下一篇:Probabilistic Programming
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

电商令狐冲 发表于 5 天前
楼主会很有节奏的吐槽!
回复 支持 反对

使用道具 举报

時間結束所有 发表于 3 天前
吾生也有涯,而吃也无涯.
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表