首页 > 编程语言 >2、go程序接入prometheus

2、go程序接入prometheus

时间:2023-05-02 12:34:13浏览次数:44  
标签:http 接入 golang prometheus func go main

参考:https://prometheus.io/docs/guides/go-application/

go默认基础指标

package main

import (
	"net/http"

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

func main() {
	http.Handle("/metrics", promhttp.Handler())
	http.ListenAndServe(":2112", nil)
}

go自定义指标

package main

import (
        "net/http"
        "time"

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

func recordMetrics() {
        go func() {
                for {
                        opsProcessed.Inc()
                        time.Sleep(2 * time.Second)
                }
        }()
}

var (
        opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
                Name: "myapp_processed_ops_total",
                Help: "The total number of processed events",
        })
)

func main() {
        recordMetrics()

        http.Handle("/metrics", promhttp.Handler())
        http.ListenAndServe(":2112", nil)
}

后续在prometheus接入datasource、dashboard即可查看信息

分布式go程序,需要prometheus配置服务发现机制
可参考:
https://prometheus.io/docs/guides/file-sd/
https://prometheus.io/docs/prometheus/latest/configuration/configuration/#consul_sd_config

标签:http,接入,golang,prometheus,func,go,main
From: https://www.cnblogs.com/mingliangge/p/17367540.html

相关文章

  • Google掀桌了,GLUE基准的时代终于过去了?
    文|Severus大家好,我是Severus,一个在某厂做中文文本理解的老程序员。今年11月,Google在NeurIPS2021投稿了一篇文章,名为AIandtheEverythingintheWholeWideWorldBenchmark,矛头直指评估AI模型“通用”能力的基准(ImageNet、GLUE/SuperGLUE),文章中用词相当凶狠,这里我简单截......
  • Go 汇编详解
    Go汇编详解https://mp.weixin.qq.com/s?__biz=MzI1MzYzMjE0MQ==&mid=2247502457&idx=1&sn=9b754e6b17ade0d48694d089b6537092&chksm=e9d3019bdea4888df6dbde663632d06d7a695b563669ba4a94b1eca78d8ecb2c041af9023959&scene=132#wechat_redirect翻译搜索复制......
  • Go
    macOSbrewinstallgoexportGOPROXY=https://proxy.golang.com.cn,directLinuxbrewinstallgoexportGOPROXY=https://proxy.golang.com.cn,directWindowsscoopinstallgo$env:GOPROXY="https://proxy.golang.com.cn,direct"......
  • Django教程
    1.前言复习该课程的时候已经2023年初了,2021年底发布了4.0,由于是复习,所以这里还是使用django3.X版本来进行练习。下面看一下django的版本演变过程。Django是基于Python的Web框架,依赖Python环境,所以需要提前安装好Python解释器。建议安装长期支持 LTS 版......
  • Go-net源码解析
    学习一门语言,那么我们必然要涉及到网络通信,而谈到网络通信却又离不开tcp,这里我们利用go标准库net来模拟一个服务端、客户端的流程,从而深入学习其中的代码流程(深入其中解析本质)funcmain(){ server,err:=net.Listen("tcp","127.0.0.1:8080")//开启服务端 iferr!=nil......
  • 对视图的对角线切割DiagonalView
    提供对视图的对角线切割,具有很好的用户定制基本用法:<com.intrusoft.squint.DiagonalViewandroid:id="@+id/diagonal"android:layout_width="match_parent"android:layout_height="240dp"......
  • MongoDB【常用命令】
    目录 1:基本常用命令1.1:演示案例1.2:数据库操作1.2.1:选择和创建数据库,查看当前正在使用的数据库命令1.2.2:数据库的删除1.3:集合操作1.3.1:集合的显式创建(了解)1.3.2:集合的隐式创建1.3.3:集合的删除1.4:文档基本CRUD1.4.1:文档的插入1.4.2:文档的基本查询1.4.3:文档的更新1.4.4:删除文档1.5:文......
  • [ABC146F] Sugoroku
    2023-03-03题目题目传送门翻译翻译难度&重要性(1~10):5题目来源AtCoder题目算法贪心解题思路对于第ii个点,只要到达\(s_{i+1}\cdotss_{i+m}\)中最后一个\(0\)的位置。但是这种方法求出的字典序肯定是最大的,但题目要求的是字典序最小。那么就可以倒序枚举,使第\(......
  • 使用 Docker Compose 安装 MongoDB
    最近学习Docker,试着在Docker里安装MongoDB,按照镜像mongo文档一顿操作猛如虎。快速开始写个docker-compose.yml文件:version:'3.8'services:db:image:mongocontainer_name:mongodb-containerports:-'27017:27017'......
  • google map初级尝试
    新建的是googlemap工程packagecom.wt.app;importandroid.os.Bundle;importcom.google.android.maps.GeoPoint;importcom.google.android.maps.MapActivity;importcom.google.android.maps.MapController;importcom.google.android.maps.MapVi......