Prometheus-Basics 使用教程

Prometheus-Basics 使用教程

Prometheus-BasicsPrometheus-Basics is part of Prometheus Docs now, checkout 👇项目地址:https://gitcode.com/gh_mirrors/pr/Prometheus-Basics

项目介绍

Prometheus 是一个开源的系统监控和报警系统,最初由 SoundCloud 开发并在2012年开源。它被广泛应用于云原生计算基金会(CNCF)的项目中,并且已经成为监控领域的事实标准之一。Prometheus 通过抓取应用程序暴露的指标数据,将其存储为时间序列数据,并提供强大的查询语言 PromQL 来分析这些数据。

项目快速启动

安装 Prometheus

首先,从 Prometheus 的官方网站下载对应操作系统的二进制文件:

wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gz
tar xvfz prometheus-2.30.3.linux-amd64.tar.gz
cd prometheus-2.30.3.linux-amd64

配置 Prometheus

创建一个基本的配置文件 prometheus.yml

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

启动 Prometheus

使用以下命令启动 Prometheus:

./prometheus --config.file=prometheus.yml

Prometheus 将会在 http://localhost:9090 上运行。

应用案例和最佳实践

监控 Web 服务器

假设我们有一个简单的 Web 服务器,我们希望监控其请求次数。我们可以使用 Prometheus 客户端库来暴露这些指标。

package main

import (
	"fmt"
	"net/http"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

var pingCounter = prometheus.NewCounter(
	prometheus.CounterOpts{
		Name: "ping_request_count",
		Help: "No of request handled by Ping handler",
	},
)

func ping(w http.ResponseWriter, req *http.Request) {
	pingCounter.Inc()
	fmt.Fprintf(w, "pong")
}

func main() {
	prometheus.MustRegister(pingCounter)
	http.HandleFunc("/ping", ping)
	http.Handle("/metrics", promhttp.Handler())
	http.ListenAndServe(":8090", nil)
}

编译并运行这个服务器:

go build server.go
./server

现在,Prometheus 可以抓取 http://localhost:8090/metrics 上的指标数据。

典型生态项目

Grafana

Grafana 是一个开源的分析和监控平台,可以与 Prometheus 无缝集成,提供强大的可视化功能。

安装 Grafana
wget https://dl.grafana.com/oss/release/grafana-8.2.0.linux-amd64.tar.gz
tar -zxvf grafana-8.2.0.linux-amd64.tar.gz
cd grafana-8.2.0
启动 Grafana
./bin/grafana-server

Grafana 将会在 http://localhost:3000 上运行,默认用户名和密码是 admin/admin

配置 Grafana 数据源

在 Grafana 中添加 Prometheus 作为数据源:

  1. 登录 Grafana。
  2. 进入 Configuration -> Data Sources
  3. 点击 Add data source
  4. 选择 Prometheus,并配置 URL 为 http://localhost:9090
  5. 保存并测试连接。

现在,你可以在 Grafana 中创建仪表板,展示 Prometheus 收集的指标数据。

通过以上步骤,你可以快速启动并使用 Prometheus 进行系统监控,并结合 Grafana 进行数据可视化。

Prometheus-BasicsPrometheus-Basics is part of Prometheus Docs now, checkout 👇项目地址:https://gitcode.com/gh_mirrors/pr/Prometheus-Basics

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郦嵘贵Just

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值