grafana官网:https://grafana.com/zh-cn/grafana/

grafana下载:https://grafana.com/grafana/download?pg=graf&plcmt=deploy-box-1

promtheus官网:https://prometheus.io/docs/introduction/overview/

promtheus和采集服务下载:https://prometheus.io/download/

loki github地址:https://github.com/grafana/loki/

loki配置文件下载:https://raw.githubusercontent.com/grafana/loki/v1.5.0/cmd/loki/loki-local-config.yaml

loki push API:https://grafana.com/docs/loki/latest/reference/api/#push-log-entries-to-loki

1、说明

本文介绍使用grafana+prometheus+loki实现数据和日志的方法,其中数据和日志的采集使用自定义采集的方式,使用既有协议,发送到数据库源存储

  • grafana负责界面展示配置
  • prometheus负责数据的存储,为grafana提供数据源(exporter等应用负责数据采集)
  • loki负责日志的存储,为grafana提供数据源(promtail负责日志的采集)

架构图如下:

2、grafana部署

根据对应的系统下载对应包,以二进制为例

./bin/grafana server #启动grafana

默认配置文件是 /conf/default.ini,其中 http_port 一项是服务的http访问端口

启动后,打开浏览器访问:http://127.0.0.1:3000

默认用户名和密码都是:admin,登录后会提示改密码

3、prometheus部署

prometheus 是一种时序数据库,用于存放数据,也可以作为 grafana 的数据源提供数据

3.1、自定义采集程序

下面程序以随机数示例

package collector

import (
"github.com/prometheus/client_golang/prometheus"
"math/rand"
) type Test struct {
queryCountDesc *prometheus.Desc
} func (e *Test) Init(config *ExporterConfig) {
e.queryCountDesc = prometheus.NewDesc(
prometheus.BuildFQName(NAMESPACE, "", config.ExporterName), // 自定义指标名称
config.HelpInfo, // 指标的help信息
[]string{"sensor_type"}, //这里要和下面的metric一一对应上
prometheus.Labels{},
)
} func (e *Test) Describe(ch chan<- *prometheus.Desc) {
ch <- e.queryCountDesc
} func (e *Test) Collect(ch chan<- prometheus.Metric) {
queryCount := e.CalcFrequency()
ch <- prometheus.MustNewConstMetric(
e.queryCountDesc,
prometheus.CounterValue,
float64(queryCount),
"sensor_name", //这里是传感器名称,要和上面desc一一对应上
)
} func (e *Test) CalcFrequency() float64 {
return rand.Float64() //随机数
}

main入口

func main() {
// 实例化并注册数据采集器exporter
reg := prometheus.NewPedanticRegistry()
for _, sensorConfig := range yamlConfig.ROSConfig.SensorConfig {
if sensorConfig.Enable {
getCollector := collector.GetCollector(&collector.ExporterConfig{
Node: node,
HelpInfo: sensorConfig.Help,
TopicName: sensorConfig.Topic,
ExporterName: sensorConfig.Name,
})
reg.MustRegister(getCollector)
log.Info("register topic: %s", sensorConfig.Topic)
}
} // 定义一个采集数据的采集器集合,它可以合并多个不同的采集器数据到一个结果集合中
gatherers := prometheus.Gatherers{
reg, //自定义的采集器
} // 启动http服务
h := promhttp.HandlerFor(gatherers,
// HandlerFor函数传递上边的gatherers对象,并返回一个httpHandler对象h。
// 这个httpHandler对象h可以调用其自身的ServHTTP函数来接收HTTP请求,并返回响应
promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError, // 采集过程中如果出现错误,继续采集其他数据,不会中断采集器的工作
}) // 创建一个HTTP处理器来暴露指标
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
log.Debug("start...")
h.ServeHTTP(w, r)
}) // 启动Web服务器,对外接口9101,交由prometheus来访问获取数据
err = http.ListenAndServe(fmt.Sprintf(":%d", 9101), nil)
if err != nil {
log.Error("listen and server fail, error: %s", err.Error())
return
}
}

3.2、prometheus配置

官方提供了很多常见的数据采集服务,可以根据需要自用,比如:node_exporter服务主要采集服务器的系统参数。本次以自定义数据采集示例

prometheus 下载解压后,进入目录,如下:

.
├── console_libraries
├── consoles
├── data
├── LICENSE
├── NOTICE
├── prometheus
├── prometheus.yml
└── promtool

prometheus 是需要执行的二进制,prometheus.yaml 是配置文件,修改配置

scrape_configs:
- job_name: "node"
static_configs:
- targets: ["localhost:9101"]

其他配置不管,这里配置采集程序

job_name 是采集程序名称

static_configs 配置采集程序的http地址,可以是多个,这里配置端口是9101,就是上面自定义采集程序开放的9101端口

3.3、运行prometheus

./prometheus --config.file="prometheus.yml" #使用指定配置文件启动prometheus

peometheus 默认使用9090端口提供数据,也可以访问查看配置情况,

打开浏览器,访问:http://127.0.0.1:9090/

以此点击:Status -> Target,可以看到配置的采集服务,如下图:

这里是采集服务没有运行,如果运行,则可以点击EndPoint进入查看数据

4、loki部署

loki下载解压,看一下loki的配置文件,部分配置说明如下:

server:
http_listen_port: 3100 #loki接收的http端口

运行loki

./loki-linux-amd64 -config.file=loki-local-config.yaml

运行后查询采集内容,打开浏览器,访问:http://127.0.0.1:3100/metrics,即可看到原始数据上报

4.1、自定义日志采集

loki开放http接口接收日志,从文档中找到push api即可使用

POST /loki/api/v1/push

header中添加

Content-Type: application/json

body内容

{
"streams": [
{
"stream": {
"label": "value"
},
"values": [
[ "<unix epoch in nanoseconds>", "<log line>" ],
[ "<unix epoch in nanoseconds>", "<log line>" ]
]
}
]
}

测试

$ curl -v -H "Content-Type: application/json" -XPOST -s "http://localhost:3100/loki/api/v1/push" --data-raw \
'{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}'

5、grafana配置

5.1、添加数据源

登录grafana,打开左上角菜单栏 -> Connections -> Data sources -> Add new data source,添加数据源,这里要添加两个

选择 prometheus,输入URL:http://localhost:9090(根据实际url修改),点击最下方的Save& test,保存并测试

选择 loki,输入URL:http://localhost:3100(根据实际url修改),点击最下方的Save& test,保存并测试

5.2、自定义面板

打开左上角菜单栏 -> Dabshboards -> New,创建新面板,自己摸索配置即可

grafana+prometheus+loki的使用的更多相关文章

  1. Grafana+Prometheus系统监控之MySql

    架构 grafana和prometheus之前安装配置过,见:Grafana+Prometheus打造全方位立体监控系统 MySql安装 MySql的地位和重要性就不言而喻了,作为开源产品深受广大中小 ...

  2. Grafana+Prometheus系统监控之webhook

    概述 Webhook是一个API概念,并且变得越来越流行.我们能用事件描述的事物越多,webhook的作用范围也就越大.Webhook作为一个轻量的事件处理应用,正变得越来越有用. 准确的说webho ...

  3. Grafana+Prometheus打造全方位立体监控系统

    前言 本文主要介绍如何使用Grafana和Prometheus以及node_exporter对Linux服务器性能进行监控.下面两张图分别是两台服务器监控信息: 服务器A 服务器B 概述 Promet ...

  4. Grafana + Prometheus 监控PostgreSQL

    效果图 部署环境 服务器名称 IP地址 部署业务 备注 部署agent sht-sgmhadoopcm-01 172.16.101.54 PostgreSQL 监控服务器.被监控服务器 node_ex ...

  5. Grafana+Prometheus 监控 MySQL

    转自:Grafana+Prometheus 监控 MySQL 架构图 环境 IP 环境 需装软件 192.168.0.237 mysql-5.7.20 node_exporter-0.15.2.lin ...

  6. 性能测试之数据库监控分析工具Grafana+Prometheus

    使用到 Grafana+Prometheus+Mysql_exportor 使用Prometheus和Grafana,可以快速的构建我们性能测试的绝大多数的监控模型:数据库监控.服务器监控.Jvm监控 ...

  7. Grafana+Prometheus通过node_exporter监控Linux服务器信息

    Grafana+Prometheus通过node_exporter监控Linux服务器信息 一.Grafana+Prometheus通过node_exporter监控Linux服务器信息 1.1nod ...

  8. Grafana Prometheus系统监控Redis服务

    Grafana Prometheus系统监控Redis服务 一.Grafana Prometheus系统监控Redis服务 1.1流程 1.2安装redis_exporter 1.3配置prometh ...

  9. Docker系列——Grafana+Prometheus+Node-exporter微信推送(三)

    在之前博文中,已经成功的实现了邮件推送.目前主流的办公终端,就是企业微信.钉钉.飞书.今天来分享下微信推送,我们具体来看. 企业微信 在配置企业微信推送时,需要有微信企业,具体如何注册.使用,另外百度 ...

  10. Docker系列——Grafana+Prometheus+Node-exporter钉钉推送(四)

    近期搭建的服务器监控平台,来进行一个总结.主要分为监控平台的搭建.告警中心的配置以及消息的推送.推送的话,支持多种终端.具体详细可查看之前的博文,在这里罗列下,方便查看. Docker系列--Graf ...

随机推荐

  1. STM32CubeMX教程17 DAC - 输出三角波噪声波

    1.准备材料 正点原子stm32f407探索者开发板V2.4 STM32CubeMX软件(Version 6.10.0) keil µVision5 IDE(MDK-Arm) ST-LINK/V2驱动 ...

  2. C++ Idioms Pimpl

    References C++ Coding Standard 這本書的中文版不知道是不是翻譯問題,還是原作就有這種傾向,有些咬文嚼字的很不好懂. Exceptional C++ 這本比上面那本容易理解 ...

  3. Java虚拟机——内存区域及内存溢出异常

    一.Java内存区域 1.概述 对于java程序员来说,在虚拟机的自动内存管理机制的帮助下,不需要为每一个new操作去写delete/free代码,而且不容易出现内存泄漏和内存溢出问题.但是把内存控制 ...

  4. 从输入URL后浏览器的渲染逻辑

    从输入URL到浏览器渲染页面需要经过很多过程,本文简单说明下各个环节的内容 主要渲染节点如下: 一.浏览器进程说明 出于安全考虑,打开一个浏览器的Tab页签,会生成1个浏览器主进程.1个网络进程.1个 ...

  5. Java 21新特性-虚拟线程 审核中

    本文翻译自国外论坛 medium,原文地址:https://medium.com/@benweidig/looking-at-java-21-virtual-threads-0ddda4ac1be1 ...

  6. spring boot 整合kafka

    本文为博主原创,未经允许不得转载: 1. 引入spring boot kafka依赖 <dependency> <groupId>org.springframework.kaf ...

  7. 例2.6 设计一个高效的算法,从顺序表L中删除所有值为x的元素,要求时间复杂度为0(n)空间复杂度为0(1)。

    1.题目 例2.6 设计一个高效的算法,从顺序表L中删除所有值为x的元素,要求时间复杂度为0(n)空间复杂度为0(1). 2.算法思想 3.代码 void DeleteX(SeqList LA, Se ...

  8. 用CI/CD工具Vela部署Elasticsearch + C# 如何使用

    Vela 除了可以帮我们编译.部署程序,利用它的docker部署功能,也能用来部署其他线上的docker镜像,例如部署RabbitMQ.PostgreSql.Elasticsearch等等,便于集中管 ...

  9. Oracle表结构&数据类型&约束

    1.Oracle 表结构 1.1.创建表名的规范 推荐以"t_xxx" 不能含有"+- "等非法字符,eg:sql create table "t-1 ...

  10. 在Winform系统开发中,使用MediatR来实现类似事件总线的消息处理

    MediatR是一款进程内的消息订阅.发布框架,可实现请求/响应.命令.查询.通知和事件的消息传递,解耦了消息处理器和消息之间耦合.提供了Send方法用于发布到单个处理程序.Publish方法发布到多 ...