网站建设功能需求方案,济南logo设计制作,青岛做网站的网络公司,网站为什么建设中系列文章 
Grafana 系列文章 
概述 
我们是基于这篇文章: Grafana 系列文章#xff08;十二#xff09;#xff1a;如何使用 Loki 创建一个用于搜索日志的 Grafana 仪表板, 创建一个类似的, 但是基于 ElasticSearch 的日志快速搜索仪表板. 
最终完整效果如下: #x1f4dd;…系列文章 
Grafana 系列文章 
概述 
我们是基于这篇文章: Grafana 系列文章十二如何使用 Loki 创建一个用于搜索日志的 Grafana 仪表板, 创建一个类似的, 但是基于 ElasticSearch 的日志快速搜索仪表板. 
最终完整效果如下: Notes: 其实我基于 ElasticSearch 做了2个仪表板 用于检索 Applog 的用于检索 accesslog 的 在下面的讲解中会综合2个仪表板来进行说明. 这次不会讲述详细细节, 只选择部分关键点进行说明. 
知识储备 
创建 Query 
使用自定义的JSON字符串编写查询field 在Elasticsearch索引映射中被映射为一个 keyword。 
如果查询是 multi-field 的 text 和 keyword 类型使用 field: fieldname.keyword有时是fieldname.raw来指定你查询中的关键字字段。 
Query 
QueryDescription{find: fields, type: keyword}返回一个索引类型为keyword 的字段名列表。{find: terms, field: hostname.keyword, size: 1000}使用 terms 聚合返回一个 keyword 的值列表。查询将使用当前仪表板的时间范围作为时间范围查询。{find: terms, field: hostname, query: Lucene query}使用terms 聚合和指定的Lucene查询过滤器返回一个keyword field 的值列表。查询将使用当前仪表板的时间范围作为查询的时间范围。 
terms 的查询默认有500个结果的限制。要设置一个自定义的限制需要在你的查询中设置size属性。 
Variable 语法 
面板标题和 metric 查询可以使用多种不同的语法来引用变量 
$varname, 这种语法很容易阅读但它不允许你在词的中间使用变量。例如apps.frontend.$server.requests.count${var_name}, 当你想在表达式的中间插值一个变量时请使用这种语法。${var_name:format} 这种格式让你对Grafana如何插值有更多控制。[[varname]] 不建议使用。废弃的旧语法将在未来的版本中删除。 
高级变量格式选项 
变量插值的格式取决于数据源但在有些情况下你可能想改变默认的格式。 
例如MySql数据源的默认格式是以逗号分隔的方式连接多个值并加引号, 如server01, server02.在某些情况下你可能希望有一个不带引号的逗号分隔的字符串, 如server01,server02。你可以用下面列出的高级变量格式化选项来实现这一目的。 
通用语法 
语法: ${var_name:option} 
可以在Grafana Play网站上测试格式化选项。 
如果指定了任何无效的格式化选项那么 glob 就是默认/回退选项。 
CSV 
将具有多个值的变量形成一个逗号分隔的字符串。 
servers  [test1, test2]
String to interpolate: ${servers:csv}
Interpolation result: test1,test2 
分布式 - OpenTSDB 
以OpenTSDB的自定义格式对具有多个值的变量进行格式化。 
servers  [test1, test2]
String to interpolate: ${servers:distributed}
Interpolation result: test1,serverstest2 
双引号 
将单值和多值变量形成一个逗号分隔的字符串在单个值中用\转义并将每个值用引号括起来。 
servers  [test1, test2]
String to interpolate: ${servers:doublequote}
Interpolation result: test1,test2 
Glob - Graphite 
将具有多个值的变量组成一个glob用于Graphite查询。 
servers  [test1, test2]
String to interpolate: ${servers:glob}
Interpolation result: {test1,test2} 
JSON 
将具有多个值的变量形成一个逗号分隔的字符串。 
servers  [test1, test2]
String to interpolate: ${servers:json}
Interpolation result: [test1, test2] 
Lucene - Elasticsearch 
以Lucene格式对Elasticsearch的多值变量进行格式化。 
servers  [test1, test2]
String to interpolate: ${servers:lucene}
Interpolation result: (test1 OR test2) 
URL 编码 (Percentencode) 
对单值和多值变量进行格式化以便在URL参数中使用。 
servers  [foo()bar BAZ, test2]
String to interpolate: ${servers:percentencode}
Interpolation result: foo%28%29bar%20BAZ%2Ctest2 
Pipe 
将具有多个值的变量形成一个管道分隔的字符串。 
servers  [test1., test2]
String to interpolate: ${servers:pipe}
Interpolation result: test1.|test2 
Raw 
关闭数据源特定的格式化如SQL查询中的单引号。 
servers  [test.1, test2]
String to interpolate: ${var_name:raw}
Interpolation result: test.1,test2 
Regex 
将有多个值的变量形成一个regex字符串。 
servers  [test1., test2]
String to interpolate: ${servers:regex}
Interpolation result: (test1\.|test2) 
单引号 
将单值和多值变量形成一个逗号分隔的字符串在单个值中用\转义并将每个值用引号括起来。 
servers  [test1, test2]
String to interpolate: ${servers:singlequote}
Interpolation result: test1,test2 
Sqlstring 
将单值和多值变量组成一个逗号分隔的字符串每个值中的用转义每个值用引号括起来。 
servers  [test1, test2]
String to interpolate: ${servers:sqlstring}
Interpolation result: test1,test2 
Text 
将单值和多值变量转换成其文本表示法。对于一个单变量它将只返回文本表示法。对于多值变量它将返回与相结合的文本表示法。 
servers  [test1, test2]
String to interpolate: ${servers:text}
Interpolation result: test1  test2 
查询参数 
将单值和多值变量编入其查询参数表示法。例如var-foovalue1var-foovalue2 
servers  [test1, test2]
String to interpolate: ${servers:queryparam}
Interpolation result: serverstest1serverstest2 
配置变量选择选项 
Selection Options 是一个你可以用来管理变量选项选择的功能。所有的选择选项都是可选的它们在默认情况下是关闭的。 
Multi-value Variables 
内插一个选择了多个值的变量是很棘手的因为如何将多个值格式化为一个在使用该变量的给定环境中有效的字符串并不直接。Grafana试图通过允许每个数据源插件告知模板插值引擎对多个值使用什么格式来解决这个问题。 Notes: 变量上的Custom all value选项必须为空以便Grafana将所有值格式化为一个字符串。如果它留空那么Grafana就会把查询中的所有值连接起来加在一起。类似于value1,value2,value3。如果使用了一个自定义的所有值那么该值将是类似于*或all的东西。 带有Prometheus或InfluxDB数据源的多值变量 
InfluxDB和Prometheus使用regex表达式所以host1, host2, host3 变量会被插值为{host1,host2,host3}。每个值都会被regex转义。 
使用Elastic数据源的多值变量 
Elasticsearch使用lucene查询语法所以同样的变量会被格式化为(host1 OR host2 OR host3)。在这种情况下每一个值都必须被转义以便该值只包含lucene控制词和引号。 
Include All 选项 
Grafana在变量下拉列表中添加了一个 All 选项。如果用户选择了这个选项那么所有的变量选项都被选中。 
自定义 all 的值 
这个选项只有在选择了 Include All option 时才可见。 
在Custom all value字段中可以输入regex、globs或lucene语法来定义All选项的值。 
默认情况下All 值包括组合表达式中的所有选项。这可能会变得非常长而且会产生性能问题。有时指定一个自定义的所有值可能会更好比如通配符。 
为了在 Custom all value 选项中拥有自定义的regex、globs或lucene语法它永远不会被转义所以你将不得不考虑什么是你的数据源的有效值。 
ElasticSearch Template Variables 
选择一种 Variable 语法 
如上文所述, Elasticsearch数据源支持在查询字段中使用多种变量语法. 
当启用 Multi-value 或 Include all value 选项时Grafana 会将标签从纯文本转换为与 Lucene 兼容的条件。即隐式转换 $varname 为 ${varname:lucene} 
实战 
1. 弄清楚有哪些索引字段 
首先, 最重要的, 就是弄清楚该索引有哪些索引字段(fields), 以及有哪些keywords, 选择部分字段和 keywords 作为 varibles. 可以直接通过 Kibana 界面进行查询和尝试. 
如本次选择的有: 
app_namelevelrequest_path ( 通过多次在 Kibana 上使用发现, 查询时应该使用 request_path.keyword 而不是 request_path)request_methodstatus_code 
2. 创建 Variables 
app_name 
设置如下: 
Name: app_nameType: QueryData source: ESQuery: {find: terms, field: current_app_name}, 另外, 如果嵌套使用, 可以类似这样 {find: terms, field: pod_name, query: app_name:$app_name} 
request_path 
设置如下: 
Name: request_pathType: QueryData source: ESQuery: {find: terms, field: request_path.keyword, query: app_name:$app_name}Multi-value: ✔️Include All option: ✔️Custom all value: * 注意, 这里使用了 Custom all value, 最终 Query All 的表达式就会变成: request_path.keyword:* 而不是 request_path.keyword:(path1 OR path2 ...) 
request_method 
request_method 常用的就这么几个: 
GETPOSTDELETEHEADPUTPATCHOPTIONS 
所以可以将其设置为 Custom variable, 设置如下: 
Name: request_methodType: CustomValues separated by comma: GET,POST,DELETE,HEAD, PUT,PATCH,OPTIONSMulti-value: ✔️Include All option: ✔️Custom all value: * 
level 
日志级别可以直接使用 Custom 类型变量. 如下: 
Name: levelType: CustomValues separated by comma: INFO, WARN, ERROR,FATALMulti-value: ✔️Include All option: ✔️ 
如果只关注错误日志, 那么 level 变量的默认值可以设置为同时勾选: ERROR 和 FATAL 
status_code 
这里会将 status_code variable 用于 Lucene 的范围语法 [](包括开头和结尾的2个数字), 所以有用到Custom all value 以及 Variable 语法配置. 
Name: status_codeType: CustomValues separated by comma: 200 TO 299, 300 TO 399, 400 TO 499, 500 TO 599Include All option: ✔️Custom all value: 200 TO 599 (Note: 即包括所有的 http 状态码, 从 200 到 599) 
后续要在 Query 中使用, 用法如下: 
status_code:[${status_code:raw}] 
直接使用 ${status_code:raw}, 这样传入就会变成: 
status_code:[200 TO 299]
status_code:[200 TO 599] 
按期望完成对 ES 的查询. 
filter 
最后, 还添加一个 Ad hoc filters variable, 方便用户进行更多自定义的过滤筛选. 
Name: filterType: Ad hoc filtersData source: ${datasource} 
后续会在该 Dashboard 的所有 Query 中自动使用. 一个典型使用场景如下: 
对于 request_path, 需要过滤监控/健康检查等请求(包含info health metric 等关键词), 那么可以将该 filter 保存为默认的变量值. 3. Panel 
Dashboard 只有 2 个面板组成: 
上图: Time series, 显示日志柱状图, 并着色, INFO日志为绿色, WARN 日志为黄色, ERROR 和 FATAL 日志为红色.下日志 
Time series panel 
如下图: 可以通过如下 Query 实现: 
app_name:$app_name AND level:($level AND INFO)
app_name:$app_name AND level:($level AND ERROR or FATAL) 
$level AND INFO 这种写法是一个 workaround, 为的是在 level 变量改变时, Time series panel 随之改变. 
另外一个需要注意的点是, Metric 是 Count(日志条数) 而不是 Logs (具体日志). 
还有, 需要配置 Override - Color, 如下: 最后, 如果柱子太密, 可以通过调整如 3 Colors Time series panel 图中的 Interval 来调整时间间隔, 本例调整为 1m 
Logs panel 
在 Logs panel 中, 也可以根据实际情况做一系列调整. 
如下图, 可以对日志展示方式做调整: Time: 是否加时间戳Unique labels: 是否每条日志加 labelCommon labels: 是否对 logs panel 左上角对所有日志加 common labelsWrap linesPretify JSON: JSON 美化Enable log details: 启用查看日志详细信息Deduplication: 日志去重, 去重方式有: None: 不去重Exact: 精确去重Numbers: 不同数字记为同一类的去重方式Signature: 根据计算得出的 Signature 去重 Order: 排序. 
另外, 考虑到 ES 日志的 log details 会有很多我们不关注的 fields, 如: _source _id 等, 可以通过 Transform 进行转换调整. 具体如下图: 总结 
这篇文章算是该系列文章的一个重点了. 包含了非常多的实用细节. 
如: 
ES QueryVariable 语法 Variable raw 语法Lucene - Elasticsearch 语法… Multi-value VariablesInclude All 选项自定义 all 的值Ad hoc filters VariableES Metric Type CountLogs… 调整Query 时间间隔Logs panel 设置Panel Transform 
希望对你有所帮助. 三人行, 必有我师; 知识共享, 天下为公. 本文由东风微鸣技术博客 EWhisper.cn 编写. 
 文章转载自: http://www.morning.mrbmc.cn.gov.cn.mrbmc.cn http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.wqgr.cn.gov.cn.wqgr.cn http://www.morning.mpgfk.cn.gov.cn.mpgfk.cn http://www.morning.rtjhw.cn.gov.cn.rtjhw.cn http://www.morning.smzr.cn.gov.cn.smzr.cn http://www.morning.pudejun.com.gov.cn.pudejun.com http://www.morning.tlpsd.cn.gov.cn.tlpsd.cn http://www.morning.mzmqg.cn.gov.cn.mzmqg.cn http://www.morning.mlnby.cn.gov.cn.mlnby.cn http://www.morning.xnpml.cn.gov.cn.xnpml.cn http://www.morning.gtmdq.cn.gov.cn.gtmdq.cn http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn http://www.morning.ptwqf.cn.gov.cn.ptwqf.cn http://www.morning.ctwwq.cn.gov.cn.ctwwq.cn http://www.morning.rbkgp.cn.gov.cn.rbkgp.cn http://www.morning.lcwhn.cn.gov.cn.lcwhn.cn http://www.morning.ssxlt.cn.gov.cn.ssxlt.cn http://www.morning.kndt.cn.gov.cn.kndt.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.fktlg.cn.gov.cn.fktlg.cn http://www.morning.dfmjm.cn.gov.cn.dfmjm.cn http://www.morning.jfbpf.cn.gov.cn.jfbpf.cn http://www.morning.pnfwd.cn.gov.cn.pnfwd.cn http://www.morning.rwmft.cn.gov.cn.rwmft.cn http://www.morning.hlppp.cn.gov.cn.hlppp.cn http://www.morning.dwyyf.cn.gov.cn.dwyyf.cn http://www.morning.shawls.com.cn.gov.cn.shawls.com.cn http://www.morning.sjwws.cn.gov.cn.sjwws.cn http://www.morning.pxdgy.cn.gov.cn.pxdgy.cn http://www.morning.gwqkk.cn.gov.cn.gwqkk.cn http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.dhqzc.cn.gov.cn.dhqzc.cn http://www.morning.jyjqh.cn.gov.cn.jyjqh.cn http://www.morning.lflnb.cn.gov.cn.lflnb.cn http://www.morning.zqbrd.cn.gov.cn.zqbrd.cn http://www.morning.ymqrc.cn.gov.cn.ymqrc.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.nkcfh.cn.gov.cn.nkcfh.cn http://www.morning.gsjfn.cn.gov.cn.gsjfn.cn http://www.morning.ryfpx.cn.gov.cn.ryfpx.cn http://www.morning.horihe.com.gov.cn.horihe.com http://www.morning.phgz.cn.gov.cn.phgz.cn http://www.morning.mxdiy.com.gov.cn.mxdiy.com http://www.morning.hqnsf.cn.gov.cn.hqnsf.cn http://www.morning.dktyc.cn.gov.cn.dktyc.cn http://www.morning.wrlxy.cn.gov.cn.wrlxy.cn http://www.morning.gwwtm.cn.gov.cn.gwwtm.cn http://www.morning.dwdjj.cn.gov.cn.dwdjj.cn http://www.morning.kqpxb.cn.gov.cn.kqpxb.cn http://www.morning.mqgqf.cn.gov.cn.mqgqf.cn http://www.morning.nkpls.cn.gov.cn.nkpls.cn http://www.morning.gynkr.cn.gov.cn.gynkr.cn http://www.morning.dhqyh.cn.gov.cn.dhqyh.cn http://www.morning.fwblh.cn.gov.cn.fwblh.cn http://www.morning.mrncd.cn.gov.cn.mrncd.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.wqpm.cn.gov.cn.wqpm.cn http://www.morning.scjtr.cn.gov.cn.scjtr.cn http://www.morning.tyjnr.cn.gov.cn.tyjnr.cn http://www.morning.qsswb.cn.gov.cn.qsswb.cn http://www.morning.wcqxj.cn.gov.cn.wcqxj.cn http://www.morning.twwzk.cn.gov.cn.twwzk.cn http://www.morning.xprq.cn.gov.cn.xprq.cn http://www.morning.rjkfj.cn.gov.cn.rjkfj.cn http://www.morning.yxnkr.cn.gov.cn.yxnkr.cn http://www.morning.zwtp.cn.gov.cn.zwtp.cn http://www.morning.wgtnz.cn.gov.cn.wgtnz.cn http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.npfkw.cn.gov.cn.npfkw.cn http://www.morning.nynyj.cn.gov.cn.nynyj.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn http://www.morning.wrtbx.cn.gov.cn.wrtbx.cn http://www.morning.kxrld.cn.gov.cn.kxrld.cn http://www.morning.daidudu.com.gov.cn.daidudu.com http://www.morning.xjqkh.cn.gov.cn.xjqkh.cn http://www.morning.gmgyt.cn.gov.cn.gmgyt.cn http://www.morning.rnqnp.cn.gov.cn.rnqnp.cn http://www.morning.dysgr.cn.gov.cn.dysgr.cn