页面“Monitoring with prometheus”与“Gin”之间的差异

来自linux中国网wiki
(页面间的差异)
跳到导航 跳到搜索
 
 
第1行: 第1行:
==
+
[[category:go]]
 
+
=install=
 
+
==pre install golang==
=book=
+
[[Golang]]
 
 
[https://www.bookstack.cn/read/prometheus-book/README.md  Prometheus操作指南]
 
=进阶=
 
以后全改成自己 docker-compose build
 
 
 
=ins on docker-composer=
 
https://gitee.com/atompi/PrometheusStackDockerCompose
 
 
 
=安全=
 
 
<pre>
 
<pre>
  node exporter 添加防火墙
+
#apt
 +
#可以指定版本的
 +
sudo apt install golang
  
curl -Lo /etc/yum.repos.d/_copr_ibotty-prometheus-exporters.repo https://copr.fedorainfracloud.org/coprs/ibotty/prometheus-exporters/repo/epel-7/ibotty-prometheus-exporters-epel-7.repo
+
sudo apt  install golang-go gccgo-go
# yum install node_exporter
+
sudo apt  install golang-go
 
</pre>
 
</pre>
  
=prometheus=
+
==FQ==
[[Nginx用户密码认证配置 Basic HTTP authentication]]
+
<pre>
 +
tail  /etc/profile
 +
#export http_proxy=http://192.168.10.173:8888
 +
#export https_proxy=http://192.168.10.173:8888
  
不错的书
 
https://yunlzheng.gitbook.io/prometheus-book/
 
  
 +
source /etc/profile
 +
</pre>
  
 +
== install Gin package==
 +
  export PATH=/usr/local/go/bin/:$PATH
 +
  export GOPATH=/root/go #if U user is root
 +
=== If you're in China===
 +
====如果您使用的 Go 版本是 1.13 及以上 ====
 +
<pre>
 +
go env -w GO111MODULE=on #注意这个打开后面可能会问题导致
 +
go env -w GOPROXY=https://goproxy.io,direct
  
 +
#or
 +
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
  
[https://blog.csdn.net/lijiaocn/article/details/81865120 新型监控告警工具prometheus(普罗米修斯)的入门使用(附视频讲解)]
+
# 设置不走 proxy 的私有仓库,多个用逗号相隔(可选)
 +
go env -w GOPRIVATE=*.corp.example.com
  
[https://www.lijiaocn.com/%E9%A1%B9%E7%9B%AE/2018/08/03/prometheus-usage.html#prometheus%E7%9A%84%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6 【视频】新型监控告警工具prometheus(普罗米修斯)入门使用(附视频讲解)]
+
# 设置不走 proxy 的私有组织(可选)
 +
go env -w GOPRIVATE=example.com/org_name
 +
</pre>
  
[https://zhuanlan.zhihu.com/p/91215371 为什么说 Prometheus 是足以取代 Zabbix 的监控神器?]
+
====Go 版本是 1.12 及以下 ====
 
+
<pre>
[https://zhuanlan.zhihu.com/p/24811652 基于Prometheus的分布式在线服务监控实践]
+
# 启用 Go Modules 功能
 
+
export GO111MODULE=on
==usage==
+
# 配置 GOPROXY 环境变量
[https://www.jianshu.com/p/03cf0fc9c746 Grafana单图表告警邮件&面板图(Dashboard)日报邮件]
+
export GOPROXY=https://goproxy.io
 +
</pre>
  
[https://prometheus.io/docs/practices/rules/ Recording rules ]
+
或者,根据[https://goproxy.io/zh/docs/getting-started.html 文档]可以把上面的命令写到.profile或.bash_profile文件中长期生效
  
https://github.com/prometheus/blackbox_exporter/blob/master/blackbox.yml
 
  
=grafana=
+
=初始化 gin=
 +
==go mod==
 
<pre>
 
<pre>
#防火墙  grafana ng 在 10.3.10.10
+
go env -w GO111MODULE=on #注意这个打开后面可能会问题导致
firewall-cmd  --permanent --add-rich-rule="rule family="ipv4" source address="10.3.10.10" port protocol="tcp" port="3000" accept "
+
go env -w GOPROXY=https://goproxy.io,direct
firewall-cmd --reload
 
 
 
</pre>
 
  
 +
cd  mygo/
 +
mkdir  ginweb
 +
cd ginweb/
 +
go mod init ginweb(名字自定义)
 +
go get -u github.com/gin-gonic/gin #注意 ,用go mod 来管理  june  08 2020  goverdor 已过期 请用 go mod  安装一般用下面的goverdor 版本管理 tool 去安装 这一块可以直接跳过
 +
go run example.go
  
[https://grafana.com/docs/grafana/latest/installation/docker/ grafana.com Run Grafana Docker image]
+
ps
 +
evan@myxps:~/mygo/ginweb$ echo $GOROOT
 +
/home/evan/go
 +
evan@myxps:~/mygo/ginweb$ echo $GOPATH
 +
/home/evan/go
  
[https://grafana.com/grafana/dashboards/8919 中文 Node Exporte for Prometheus Dashboard]
+
#别人的例子
 +
编写 HelloWorld 文件,测试运行环境。
  
usage  creat-->import --> id  8919
+
package main
 +
import "fmt"
 +
func main(){
 +
    fmt.Println("hello,world!")
 +
}
  
Container
+
go run hello.go
 +
go build hello.go
  
 +
创建 go.mod 文件
  
 +
go mod init hello
  
https://grafana.com/grafana/dashboards/11174
+
用 Gin 实现一个简单的 http 服务
  
+
import (
docker-grafana/config.env  https://github.com/stefanwalther/docker-grafana/blob/master/config.env 
+
    "gopkg.in/gin-gonic/gin.v1"
 +
    "net/http"
 +
)
  
==grafana 添加源和数据 ==
+
func main(){
 +
   
 +
    router := gin.Default()
  
[https://www.bookstack.cn/read/prometheus-book/quickstart-use-grafana-create-dashboard.md 使用Grafana创建可视化Dashboard]
+
    router.GET("/", func(c *gin.Context) {
==grafana 添加用户 ==
+
        c.String(http.StatusOK, "Hello World")
<pre>
+
    })
1.Users-->  2.Invite -->
+
    router.Run(":8000")
 +
}
  
3.进行邀请
+
直接编译执行
Email or Username:输入用户邮箱,建议先跟用户名保持相同,然后在后面在写邮箱,后面会说到,这样不会报错
 
Name:输入用户名
 
Role:配置用户权限(只读Viewer 编辑Editor 管理员Admin)
 
Send invite email:勾选时,会将新用户的设置密码的链接发到邮箱
 
  
点击创建之后回调到新用户设置密码
+
go run hello
  
-->
+
可以看到引用的包都被自动下载了
 +
</pre>
  
4.创建好之后,点击Pending Invites
+
== 放弃了 Use a vendor tool like Govendor==
复制链接到浏览器的地址栏中访问(也可从邮件中收到链接)
+
  go get -u -v github.com/kardianos/govendor
进去之后是下面的界面,这里有个坑,复制的IP地址是localhost,我们这里改为自己搭建的IP地址,然后在Email填写邮箱地址,设置新用户的密码
+
  go get github.com/kardianos/govendor #在墙外非常快
设置完成后,会直接以新用户身份登陆
 
  
 +
sudo apt  install govendor # apt
  
http://localhost:3000/invite/PAhyq4gzfA3ugRiw0PH2RC2vsIuSTa
+
mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_"
  
  -->
+
===Vendor init your project and add gin ===
 +
<pre>
 +
govendor init
 +
govendor fetch github.com/gin-gonic/[email protected]
  
 +
#有时要绝对路径  @后面也可以不加  有时一定要加
 +
/root/go/bin/govendor fetch github.com/gin-gonic/[email protected]
 
</pre>
 
</pre>
  
[https://blog.csdn.net/liuxiangyang_/article/details/86737103  grafana的用户及权限的配置]
+
=learn=
 +
https://github.com/astaxie/build-web-application-with-golang
  
[https://blog.csdn.net/GX_1_11_real/article/details/85119451  grafana的用户及权限的配置]
+
https://github.com/avelino/awesome-go
  
[https://blog.csdn.net/kozazyh/article/details/93759717?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase  grafana-利用Google OAuth2 身份验证]
 
  
==使用教程==
+
https://github.com/gin-gonic/examples
[https://yuerblog.cc/2019/01/04/grafana-usage/ grafana配置与简单使用]
 
  
[https://blog.csdn.net/bbwangj/article/details/81109615  Grafana使用教程]
 
  
[https://blog.csdn.net/Sagitarioo/article/details/102805852?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2  grafana数据可视化-使用教程]
+
https://learnku.com/docs/gin-gonic/2019
  
[https://www.cnblogs.com/xkus/p/7462742.html  grafana快速入门 ]
 
  
[https://blog.csdn.net/u012062455/article/details/79214700  grafana使用教程之基本概念(basic concepts)]
+
[https://www.jianshu.com/p/2a1a74ad3c3a Gin middleware中间件使用实例]
  
[https://blog.csdn.net/weixin_42674978/article/details/102681969  Grafana从无到有的一个相当粗略的资料总结]
+
=IDE=
  
 +
[https://www.jianshu.com/p/c5368dbd8bf9 golnad and 50.Go Mod 来创建 Gin 项目]
  
[https://blog.csdn.net/Sagitarioo/article/details/102805852?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2  grafana数据可视化-使用教程]
+
=trouble=
 +
== working directory is not part of a module==
 +
<pre>
  
[https://ken.io/note/grafana-quickstart-influxdb-datasource-graph Grafana快速入门:InfluxDB数据源以及曲线图表仪表盘配置]
+
关闭go代理就行了
  
 +
ct$ go run main.go
 +
main.go:6:2: cannot find module providing package github.com/gin-gonic/gin: working directory is not part of a module
 +
evan@myxps:~/go/src/github.com/myusername/project$ go env -w GO111MODULE=off
  
 +
</pre>
  
  
 
+
==1.6 err==
apached conf
 
 
<pre>
 
<pre>
##/etc/httpd/conf.d/gf.conf
+
# go  run main.go
 +
# github.com/myusername/project/vendor/github.com/gin-gonic/gin
 +
vendor/github.com/gin-gonic/gin/context.go:36:26: undefined: binding.MIMEYAML
 +
vendor/github.com/gin-gonic/gin/context.go:600:29: undefined: binding.YAML
 +
vendor/github.com/gin-gonic/gin/context.go:605:29: undefined: binding.Header
 +
vendor/github.com/gin-gonic/gin/context.go:659:31: undefined: binding.YAML
 +
vendor/github.com/gin-gonic/gin/context.go:664:31: undefined: binding.Header
 +
vendor/github.com/gin-gonic/gin/context.go:673:9: undefined: binding.Uri
 +
vendor/github.com/gin-gonic/gin/context.go:896:17: undefined: render.PureJSON
 +
vendor/github.com/gin-gonic/gin/context.go:912:17: undefined: render.ProtoBuf
 +
vendor/github.com/gin-gonic/gin/context.go:1027:7: undefined: binding.MIMEYAML
 +
vendor/github.com/gin-gonic/gin/mode.go:83:2: undefined: binding.EnableDecoderDisallowUnknownFields
  
 +
</pre>
  
<VirtualHost *:80>
+
=see also=
      ServerName gf.linuxsa.org
 
      ServerAlias gf.linuxsa.org
 
   
 
      ProxyRequests off
 
   
 
      <Proxy *>
 
        Order deny,allow
 
        Allow from all
 
      </Proxy>
 
   
 
      <Location />
 
        ProxyPass http://localhost:3000/
 
        ProxyPassReverse http://localhost:3000/
 
      </Location>
 
    </VirtualHost>
 
  
</pre>
 
  
=vm监控[[Node-exporter]]=
+
[https://www.jianshu.com/p/a3f63b5da74c Gin实战:Gin+Mysql简单的Restful风格的API]
可以如上的  node
 
[[Node-exporter]]
 
  
=Blackbox=
 
  
[[Blackbox exporter]]
+
[https://blog.csdn.net/qq_34284638/article/details/104944319  Golang 入门-Gin框架安装及使用]
  
=应用监控=
 
[[prometheus监控Redis集群]]
 
  
==监控MQ==
 
[https://www.rabbitmq.com/prometheus.html  Monitoring with Prometheus & Grafana ]
 
  
==监控ES==
+
[https://blog.csdn.net/VanciorH/article/details/96314896 Golang(不存在)的包管理]
https://github.com/slok/ecs-exporter
 
==docker==
 
[https://blog.csdn.net/palet/article/details/82889493?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-7.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-7.nonecase Prometheus+grafana监控:cAdvisor输出的容器内存相关的指标的解读]
 
  
== mysql==
 
[https://blog.csdn.net/qq_25934401/article/details/82594478  prometheus 监控之 mysql 篇(含mysql报警规则)]
 
==Spring boot==
 
https://www.bookstack.cn/read/prometheus_practice/application-spring.md
 
  
=告警=
+
==go mod==
 +
[https://www.cnblogs.com/apocelipes/p/9534885.html  golang包管理解决之道——go modules初探 ]
  
[[Alertmanager]]
+
[https://blog.csdn.net/yangyangye/article/details/97243826 go mod常用命令 以及 常见问题]
=教程=
 
[https://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_liunx_60_prometheus_config.html  prometheus学习系列五: Prometheus配置文件 ]
 
  
=see also=
+
[https://blog.csdn.net/sanbingyutuoniao123/article/details/100903028  go语言web框架gin安装(go mod方式)]
  
[https://www.jianshu.com/p/93412a925da2 规划 Prometheus 的存储用量]
+
==放弃govendor==
 +
[https://blog.csdn.net/huwh_/article/details/77169858  Golang包管理工具(一)之govendor的使用]
  
[https://blog.csdn.net/qq_21816375/article/details/80462445?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-12.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-12.nonecase prometheus部署node,server以及域名加告警]
+
[https://blog.csdn.net/studyhard232/article/details/81637607 govendor的安装与实践]
  
[https://www.cnblogs.com/chenqionghe/p/10494868.html 从零搭建Prometheus监控报警系统]
+
[https://my.oschina.net/u/3628490/blog/2245119 golang使用govendor教程]
  
[https://www.jianshu.com/p/1f05476ebcee 使用prometheus自定义监控]
+
[https://www.cnblogs.com/shockerli/p/go-package-manage-tool-govendor.html Go 包依赖管理工具 —— govendor]
  
[https://zhuanlan.zhihu.com/p/91215371 为什么说 Prometheus 是足以取代 Zabbix 的监控神器?]
+
[https://zhuanlan.zhihu.com/p/59191567 Go Mod对比Go Vendor]
  
[[category:ops]] [[category:container]] [[category:prom]]
+
[https://www.jianshu.com/p/88669ba57d04 Govendor使用]

2020年6月28日 (日) 03:18的版本

install

pre install golang

Golang

#apt 
#可以指定版本的
sudo apt install  golang

sudo apt  install golang-go gccgo-go
sudo apt  install golang-go 

FQ

tail  /etc/profile
#export http_proxy=http://192.168.10.173:8888
#export https_proxy=http://192.168.10.173:8888


source /etc/profile

install Gin package

 export PATH=/usr/local/go/bin/:$PATH
 export GOPATH=/root/go #if U user is root 

If you're in China

如果您使用的 Go 版本是 1.13 及以上

go env -w GO111MODULE=on #注意这个打开后面可能会问题导致
go env -w GOPROXY=https://goproxy.io,direct

#or
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct

# 设置不走 proxy 的私有仓库,多个用逗号相隔(可选)
go env -w GOPRIVATE=*.corp.example.com

# 设置不走 proxy 的私有组织(可选)
go env -w GOPRIVATE=example.com/org_name

Go 版本是 1.12 及以下

# 启用 Go Modules 功能
export GO111MODULE=on
# 配置 GOPROXY 环境变量
export GOPROXY=https://goproxy.io

或者,根据文档可以把上面的命令写到.profile或.bash_profile文件中长期生效


初始化 gin

go mod

go env -w GO111MODULE=on #注意这个打开后面可能会问题导致
go env -w GOPROXY=https://goproxy.io,direct

cd   mygo/
mkdir  ginweb
cd ginweb/
go mod init ginweb(名字自定义)
go get -u github.com/gin-gonic/gin #注意 ,用go mod 来管理  june   08 2020  goverdor 已过期 请用 go mod  安装一般用下面的goverdor 版本管理 tool 去安装 这一块可以直接跳过
 go run example.go 

ps 
evan@myxps:~/mygo/ginweb$ echo $GOROOT
/home/evan/go
evan@myxps:~/mygo/ginweb$ echo $GOPATH
/home/evan/go

#别人的例子
编写 HelloWorld 文件,测试运行环境。

package main
import "fmt"
func main(){
    fmt.Println("hello,world!")
}

go run hello.go
go build hello.go 

创建 go.mod 文件

go mod init hello

用 Gin 实现一个简单的 http 服务

import (
    "gopkg.in/gin-gonic/gin.v1"
    "net/http"
)

func main(){
    
    router := gin.Default()

    router.GET("/", func(c *gin.Context) {
        c.String(http.StatusOK, "Hello World")
    })
    router.Run(":8000")
}

直接编译执行 

go run hello

可以看到引用的包都被自动下载了

放弃了 Use a vendor tool like Govendor

 go get -u -v github.com/kardianos/govendor
 go get github.com/kardianos/govendor #在墙外非常快
sudo apt  install govendor # apt 
mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_"

Vendor init your project and add gin

govendor init
govendor fetch github.com/gin-gonic/[email protected]

#有时要绝对路径  @后面也可以不加  有时一定要加
/root/go/bin/govendor fetch github.com/gin-gonic/[email protected]

learn

https://github.com/astaxie/build-web-application-with-golang

https://github.com/avelino/awesome-go


https://github.com/gin-gonic/examples


https://learnku.com/docs/gin-gonic/2019


Gin middleware中间件使用实例

IDE

golnad and 50.Go Mod 来创建 Gin 项目

trouble

working directory is not part of a module


关闭go代理就行了 

ct$ go run main.go 
main.go:6:2: cannot find module providing package github.com/gin-gonic/gin: working directory is not part of a module
evan@myxps:~/go/src/github.com/myusername/project$ go env -w GO111MODULE=off


1.6 err

# go   run main.go 
# github.com/myusername/project/vendor/github.com/gin-gonic/gin
vendor/github.com/gin-gonic/gin/context.go:36:26: undefined: binding.MIMEYAML
vendor/github.com/gin-gonic/gin/context.go:600:29: undefined: binding.YAML
vendor/github.com/gin-gonic/gin/context.go:605:29: undefined: binding.Header
vendor/github.com/gin-gonic/gin/context.go:659:31: undefined: binding.YAML
vendor/github.com/gin-gonic/gin/context.go:664:31: undefined: binding.Header
vendor/github.com/gin-gonic/gin/context.go:673:9: undefined: binding.Uri
vendor/github.com/gin-gonic/gin/context.go:896:17: undefined: render.PureJSON
vendor/github.com/gin-gonic/gin/context.go:912:17: undefined: render.ProtoBuf
vendor/github.com/gin-gonic/gin/context.go:1027:7: undefined: binding.MIMEYAML
vendor/github.com/gin-gonic/gin/mode.go:83:2: undefined: binding.EnableDecoderDisallowUnknownFields

see also

Gin实战:Gin+Mysql简单的Restful风格的API


Golang 入门-Gin框架安装及使用


Golang(不存在)的包管理


go mod

golang包管理解决之道——go modules初探

go mod常用命令 以及 常见问题

go语言web框架gin安装(go mod方式)

放弃govendor

Golang包管理工具(一)之govendor的使用

govendor的安装与实践

golang使用govendor教程

Go 包依赖管理工具 —— govendor

Go Mod对比Go Vendor

Govendor使用