1、日志报警 查看详情 优化
https://help.aliyun.com/document_detail/346631.html#section-4wz-6i6-dtg
三、报警的日志如果过长是会被截断的,查过1024个字节就会被截断
截断的结果如下,无法很好的排查报警原因,目前这个问题阿里说是难以解决,只能暂时搁置
相关文档:https://help.aliyun.com/document_detail/209203.html 查看字段值长度这个配置
因为大部分需要查看的错误信息是放在后面的,那么报警就只展示后面1024个字符,比如我要展示abplaygameapi的错误日志,报警数据查询sql如下
__tag__:__path__:/www/logs/abplaygameapi.37gapi.com/error* | select substr(content, length(content)-1024, 1024)
使用substr截取content字段的后1024个字符
2、添加分析语句后可突破100条限制
五、告警时效性
因sls查询告警时日志存在时延,所以在设置查询时间区间时建议往前查询10s的数据
例如:查询区间为最近2分钟,应设置为:
参考文档:https://help.aliyun.com/document_detail/209188.html
2、grafana图表接入
1、编写查询sql
举个图中的例子,要查询nginx-access-log日志关于每个host的pv,也就是需要针对remote_host进行group by算总数,sql如下
remove_host:gstore.* and cost > 10 | select remove_host, num, to_unixtime(t) as t from (
select remove_host, COUNT(*) as num, time_series(__time__, '1m', '%Y-%m-%d %H:%i:%s', '0') as t from nginx-access-log group by t,remove_host order by t limit 10000
)
to_unixtime : 将datetime时间格式转为timestamp时间戳,因为grafana使用sls数据时,横轴的时间只接受时间戳,这里得做个转换
time_series : 补全时间序列,有时候查出来的数据在时间上是不连续的,离散的,那么那些没有数据的时间就使用该函数补全,这个函数需要搭配group by t order by t 使用,group by可以加入其它字段,但必须要有t
更多的时间函数见wiki:https://help.aliyun.com/document_detail/63451.html
字符串处理函数见wiki:https://help.aliyun.com/document_detail/63450.html
记住sql子句里不要加入时间限制,也就是where条件不要加入时间限制,这样就会用到grafana图表本身的时间间隔和区间
grafana的配置就比较简单了
Query:填入查询sql
Y-column:填入Y轴需要展示的字段,比如Y轴要展示每个host的数量变化,语法就是 需要展示字段#:#数量字段
X-column:填入时间,这里需要的是timestamp时间戳,而不能是datetime的时间格式
1、编写查询sql
举个图中的例子,需要制作nginx-access-log日志里每个请求的host的占比,sql如下
remove_host:gstore.* and cost > 10 | select remove_host, COUNT(*) as num group by remove_host order by num desc
这里就需要group by 时间字段了,比较简单
Y-column:填入 需要展示字段#:#数量字段
X-column: 因为饼状图其实不需要时间轴,所以这里就可以直接把查出来的num放进去即可,或者是如下图,只要填入的是数字即可