当前位置: 首页 > news >正文

北京自助建站软件画册设计排版的技巧和规则

北京自助建站软件,画册设计排版的技巧和规则,谁能给个网址免费的,泰安房产信息网网签查询本节重点介绍 : 使用golang sdk打prometheus4种指标,推送到pushgateway gauge、counter、histogram、summary的初始化4种类似的设置值的方法推送到pushgateway的方法 prometheus配置采集pushgateway,grafana上配大盘 golang-sdk 项目地址 https://git…

本节重点介绍 :

  • 使用golang sdk打prometheus4种指标,推送到pushgateway
    • gauge、counter、histogram、summary的初始化
    • 4种类似的设置值的方法
    • 推送到pushgateway的方法
  • prometheus配置采集pushgateway,grafana上配大盘

golang-sdk

  • 项目地址 https://github.com/prometheus/client_golang

使用sdk打点并推送到pushgateway

首先导入包,初始化pusher 推送对象

import (
"github.com/prometheus/client_golang/prometheus/push"
)
var (// pusher对象pusher *push.Pusher
)

初始化4种数据metrics对象

	// 带标签的gaugeTestMetricGauge01 = prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: "test_metric_gauge_01",Help: "gauge metic test 01",}, []string{"idc", "ip"})// 带标签的counterTestMetricCounter01 = prometheus.NewCounterVec(prometheus.CounterOpts{Name: "test_metric_counter_01",Help: "gauge metic counter 01",}, []string{"path", "code"})// histogramhisStart        = 0.1histWidth       = 0.2TestHistogram01 = prometheus.NewHistogram(prometheus.HistogramOpts{Name:    "test_histogram_01",Help:    "RPC latency distributions.",// histogram 需要传入 bucket的start 和width参数Buckets: prometheus.LinearBuckets(hisStart, histWidth, 20),})// summaryTestSummary01 = prometheus.NewSummaryVec(prometheus.SummaryOpts{Name:       "test_summary_01",Help:       "RPC latency distributions.",// summary需要固定好最后的分位值结果Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},},[]string{"service"},)

编写初始化pusher对象和注册metrics的Init函数

func Init(url string, jobName string) {pusher = push.New(url, jobName)// collector 注册metricspusher.Collector(TestMetricGauge01)pusher.Collector(TestMetricCounter01)pusher.Collector(TestHistogram01)pusher.Collector(TestSummary01)}

编写设置gauge和counter值的函数 setValueGaugeAndCounter

  • counter 只能恒增,需要使用Add函数
// gauge和counter设置值的方法
func setValueGaugeAndCounter() {for {TestMetricGauge01.With(prometheus.Labels{"idc": "bj", "ip": "1.1"}).Set(float64(rand.Intn(100)))TestMetricCounter01.With(prometheus.Labels{"path": "/login", "code": "200"}).Add(float64(rand.Intn(100)))time.Sleep(5 * time.Second)}
}

编写设置histogram值的函数 setValueHistogram

  • histogram 使用Observe函数设置bucket的值
func setValueHistogram() {for {v := rand.NormFloat64()TestHistogram01.Observe(v)time.Sleep(100 * time.Millisecond)}
}

编写设置summary值的函数 setValueGaugeAndCounter

  • summary使用Observe设置值
// Summary设置值的方法
func setValueSummary() {for {v := rand.Float64()TestSummary01.WithLabelValues("uniform").Observe(v)time.Sleep(100 * time.Millisecond)}
}

编写推送到pushgateway的函数

func PushWork() {for {err := pusher.Push()if err != nil {fmt.Println("Could not push completion time to Pushgateway:", err)}time.Sleep(5 * time.Second)}}

main函数启动任务

  • 依次启动设置值的协程
  • 启动push的协程
func main() {rand.Seed(time.Now().UnixNano())Init("http://172.20.70.205:9091/", "my_job")go setValueGaugeAndCounter()go setValueHistogram()go setValueSummary()go PushWork()select {}
}

pushgateway中查看对应指标

  • 举例图片
  • image.png

将单个pushgateway加入prometheus采集job中

  - job_name: 'pushgateway'honor_timestamps: truescrape_interval: 15sscrape_timeout: 10smetrics_path: /metricsscheme: httpstatic_configs:- targets:- 172.20.70.205:9091- 172.20.70.215:9091

在prometheus查询相关指标

promql

  • histogram histogram_quantile(0.95, sum by(le) (rate(test_histogram_01_bucket[5m])))
  • image.png
  • summary test_summary_01
  • counter rate(test_metric_counter_01[1m])
  • image.png
  • gauge test_metric_gauge_01

在grafana上设置相关图表

举例图片

image.png

grafana json

{"annotations": {"list": [{"builtIn": 1,"datasource": "-- Grafana --","enable": true,"hide": true,"iconColor": "rgba(0, 211, 255, 1)","name": "Annotations & Alerts","type": "dashboard"}]},"editable": true,"gnetId": null,"graphTooltip": 0,"id": 11,"links": [],"panels": [{"aliasColors": {},"bars": false,"dashLength": 10,"dashes": false,"datasource": null,"fieldConfig": {"defaults": {},"overrides": []},"fill": 1,"fillGradient": 0,"gridPos": {"h": 9,"w": 12,"x": 0,"y": 0},"hiddenSeries": false,"id": 2,"legend": {"avg": false,"current": false,"max": false,"min": false,"show": true,"total": false,"values": false},"lines": true,"linewidth": 1,"nullPointMode": "null","options": {"alertThreshold": true},"percentage": false,"pluginVersion": "7.5.1","pointradius": 2,"points": false,"renderer": "flot","seriesOverrides": [],"spaceLength": 10,"stack": false,"steppedLine": false,"targets": [{"exemplar": true,"expr": "test_metric_gauge_01","interval": "","legendFormat": "","refId": "A"}],"thresholds": [],"timeFrom": null,"timeRegions": [],"timeShift": null,"title": "test_metric_gauge_01","tooltip": {"shared": true,"sort": 0,"value_type": "individual"},"type": "graph","xaxis": {"buckets": null,"mode": "time","name": null,"show": true,"values": []},"yaxes": [{"format": "short","label": null,"logBase": 1,"max": null,"min": null,"show": true},{"format": "short","label": null,"logBase": 1,"max": null,"min": null,"show": true}],"yaxis": {"align": false,"alignLevel": null}},{"aliasColors": {},"bars": false,"dashLength": 10,"dashes": false,"datasource": null,"fieldConfig": {"defaults": {},"overrides": []},"fill": 1,"fillGradient": 0,"gridPos": {"h": 9,"w": 12,"x": 12,"y": 0},"hiddenSeries": false,"id": 3,"legend": {"avg": false,"current": false,"max": false,"min": false,"show": true,"total": false,"values": false},"lines": true,"linewidth": 1,"nullPointMode": "null","options": {"alertThreshold": true},"percentage": false,"pluginVersion": "7.5.1","pointradius": 2,"points": false,"renderer": "flot","seriesOverrides": [],"spaceLength": 10,"stack": false,"steppedLine": false,"targets": [{"exemplar": true,"expr": "rate(test_metric_counter_01[1m])","interval": "","legendFormat": "","refId": "A"}],"thresholds": [],"timeFrom": null,"timeRegions": [],"timeShift": null,"title": "qps","tooltip": {"shared": true,"sort": 0,"value_type": "individual"},"type": "graph","xaxis": {"buckets": null,"mode": "time","name": null,"show": true,"values": []},"yaxes": [{"format": "short","label": null,"logBase": 1,"max": null,"min": null,"show": true},{"format": "short","label": null,"logBase": 1,"max": null,"min": null,"show": true}],"yaxis": {"align": false,"alignLevel": null}},{"aliasColors": {},"bars": false,"dashLength": 10,"dashes": false,"datasource": null,"fieldConfig": {"defaults": {},"overrides": []},"fill": 1,"fillGradient": 0,"gridPos": {"h": 9,"w": 12,"x": 0,"y": 9},"hiddenSeries": false,"id": 4,"legend": {"avg": false,"current": false,"max": false,"min": false,"show": true,"total": false,"values": false},"lines": true,"linewidth": 1,"nullPointMode": "null","options": {"alertThreshold": true},"percentage": false,"pluginVersion": "7.5.1","pointradius": 2,"points": false,"renderer": "flot","seriesOverrides": [],"spaceLength": 10,"stack": false,"steppedLine": false,"targets": [{"exemplar": true,"expr": "histogram_quantile(0.95, sum by(le) (rate(test_histogram_01_bucket[5m])))","interval": "","legendFormat": "","refId": "A"}],"thresholds": [],"timeFrom": null,"timeRegions": [],"timeShift": null,"title": "histogram 分位置","tooltip": {"shared": true,"sort": 0,"value_type": "individual"},"type": "graph","xaxis": {"buckets": null,"mode": "time","name": null,"show": true,"values": []},"yaxes": [{"format": "short","label": null,"logBase": 1,"max": null,"min": null,"show": true},{"format": "short","label": null,"logBase": 1,"max": null,"min": null,"show": true}],"yaxis": {"align": false,"alignLevel": null}},{"aliasColors": {},"bars": false,"dashLength": 10,"dashes": false,"datasource": null,"fieldConfig": {"defaults": {},"overrides": []},"fill": 1,"fillGradient": 0,"gridPos": {"h": 9,"w": 12,"x": 12,"y": 9},"hiddenSeries": false,"id": 5,"legend": {"avg": false,"current": false,"max": false,"min": false,"show": true,"total": false,"values": false},"lines": true,"linewidth": 1,"nullPointMode": "null","options": {"alertThreshold": true},"percentage": false,"pluginVersion": "7.5.1","pointradius": 2,"points": false,"renderer": "flot","seriesOverrides": [],"spaceLength": 10,"stack": false,"steppedLine": false,"targets": [{"exemplar": true,"expr": "test_summary_01","interval": "","legendFormat": "","refId": "A"}],"thresholds": [],"timeFrom": null,"timeRegions": [],"timeShift": null,"title": "summary 分位值","tooltip": {"shared": true,"sort": 0,"value_type": "individual"},"type": "graph","xaxis": {"buckets": null,"mode": "time","name": null,"show": true,"values": []},"yaxes": [{"format": "short","label": null,"logBase": 1,"max": null,"min": null,"show": true},{"format": "short","label": null,"logBase": 1,"max": null,"min": null,"show": true}],"yaxis": {"align": false,"alignLevel": null}}],"refresh": "10s","schemaVersion": 27,"style": "dark","tags": [],"templating": {"list": []},"time": {"from": "now-30m","to": "now"},"timepicker": {},"timezone": "","title": "自打点pushgatway指标","uid": "Kqdgmyn7k","version": 2
}

全量代码

package mainimport ("fmt""github.com/prometheus/client_golang/prometheus""github.com/prometheus/client_golang/prometheus/push""math/rand""time"
)var (// 带标签的gaugeTestMetricGauge01 = prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: "test_metric_gauge_01",Help: "gauge metic test 01",}, []string{"idc", "ip"})// 带标签的counterTestMetricCounter01 = prometheus.NewCounterVec(prometheus.CounterOpts{Name: "test_metric_counter_01",Help: "gauge metic counter 01",}, []string{"path", "code"})// histogramhisStart        = 0.1histWidth       = 0.2TestHistogram01 = prometheus.NewHistogram(prometheus.HistogramOpts{Name:    "test_histogram_01",Help:    "RPC latency distributions.",Buckets: prometheus.LinearBuckets(hisStart, histWidth, 20),})// summaryTestSummary01 = prometheus.NewSummaryVec(prometheus.SummaryOpts{Name:       "test_summary_01",Help:       "RPC latency distributions.",Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},},[]string{"service"},)// pusher对象pusher *push.Pusher
)func Init(url string, jobName string) {pusher = push.New(url, jobName)// collector 注册metricspusher.Collector(TestMetricGauge01)pusher.Collector(TestMetricCounter01)pusher.Collector(TestHistogram01)pusher.Collector(TestSummary01)}// Summary设置值的方法
func setValueSummary() {for {v := rand.Float64()TestSummary01.WithLabelValues("uniform").Observe(v)time.Sleep(100 * time.Millisecond)}
}// gauge和counter设置值的方法
func setValueGaugeAndCounter() {for {TestMetricGauge01.With(prometheus.Labels{"idc": "bj", "ip": "1.1"}).Set(float64(rand.Intn(100)))TestMetricCounter01.With(prometheus.Labels{"path": "/login", "code": "200"}).Add(float64(rand.Intn(100)))time.Sleep(5 * time.Second)}
}func setValueHistogram() {for {v := rand.NormFloat64()TestHistogram01.Observe(v)time.Sleep(100 * time.Millisecond)}
}func PushWork() {for {err := pusher.Push()if err != nil {fmt.Println("Could not push completion time to Pushgateway:", err)}time.Sleep(5 * time.Second)}}
func main() {rand.Seed(time.Now().UnixNano())Init("http://172.20.70.205:9091/", "my_job")go setValueGaugeAndCounter()go setValueHistogram()go setValueSummary()go PushWork()select {}
}

本节重点总结 :

  • 使用golang sdk打prometheus4种指标,推送到pushgateway
    • gauge、counter、histogram、summary的初始化
    • 4种类似的设置值的方法
    • 推送到pushgateway的方法
  • prometheus配置采集pushgateway,grafana上配大盘
http://www.sczhlp.com/news/142668/

相关文章:

  • 网站gif图标素材企业建设电子商务网站的目的
  • 新网站建设需要什么找别人做网站注意事项
  • 网站运营做内容小程序在哪里
  • 2025 丹东店推荐:丽格门窗,用 20 年技术沉淀守护家的舒适
  • NOIP2025模拟赛23
  • step
  • 2025 呼和浩特店推荐:丽格门窗,用 20 年技术沉淀守护家的温度
  • 深入解析:浏览器端音视频处理新选择:Mediabunny 让 Web 媒体开发飞起来
  • 网站建设对接流程网站建设丶金手指下拉15
  • seo网站优化方案摘要wordpress网站自适应
  • 企业网站怎么做百度seo网站搭建是什么
  • 静安网站开发网站建设的目的是什么
  • 用易语言做抢购网站软件下载11电影网
  • 2025 宁波门窗店推荐:丽格门窗,甬城品质家居的安心之选
  • 2025 贵阳门窗店优选:丽格门窗,用 20 年匠心适配高原宜居需求
  • 2025 济南门窗店选购指南:丽格门窗凭硬实力圈粉品质家庭
  • 唐山网站制作工具网站简繁转换代码
  • 网址大全123官方网站北京购物网站建设
  • 做网站怎么与客户谈判网站 建设app
  • 免费个人网站下载做推送的网站有哪些
  • 网站策划岗位要求app设计方案
  • 易语言可以做网站嘛莱芜在线最新消息
  • 菏泽网站建设谁最出名手机软件开发公司简介
  • 成都网站建设吧网页制作与设计课本
  • 网站推广的方法有哪些?网站的主机选择
  • 网页制作与网站开发模板为什么简洁网站会受到用户欢迎
  • 网站建设合同付款比例网站建设叁金手指花总7
  • 9/27
  • 2025 常熟门窗店优选:丽格门窗,20 年技术沉淀的品质之选
  • 系部网站建设需求分析wow313做宏的网站