Golang 如何做 APM 监控
本文中业务 golang 程序 和 ELK、APM-Server均运行在 K8S 中
1. 部署 apm-server
前提:ES 已部署好
参照文末 apm-server 的部署方式,使用 helm 部署 apm-server。
apm-server:
host: "0.0.0.0:8200"
output.elasticsearch:
hosts: ["elasticsearch:9200"]
1
2
3
4
2
3
4
2. 修改业务 Golang 客户端
由于 web-server 使用 httprouter,所以 apm agent 使用 modules-apmhttprouter
import (
"github.com/julienschmidt/httprouter"
"go.elastic.co/apm/module/apmhttprouter"
)
func main() {
r := httprouter.New()
r.GET("/", apmhttprouter.Wrap(getIP, "/"))
...
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
3. 修改业务 golang 程序的 系统环境变量
由于业务golang程序运行在 K8S 中,所以修改 deployment 的配置文件。
主要设置这 2 个环境变量:ELASTIC_APM_SERVICE_NAME
、ELASTIC_APM_SERVER_URL
ELASTIC_APM_SERVICE_NAME:标识服务 ELASTIC_APM_SERVER_URL:apm server 地址,默认 http://localhost:8200
spec:
containers:
- env:
- name: ELASTIC_APM_SERVICE_NAME
value: <YOUR_ELASTIC_APM_SERVICE_NAME>
- name: ELASTIC_APM_SERVER_URL
value: <YOUR_ELASTIC_APM_SERVER_URL>
1
2
3
4
5
6
7
2
3
4
5
6
7
都部署好了后,访问服务,在 Kibana 上就可以看到 APM 的数据了。
FAQ
GO GET 报错:error_unix.go:29:9: undefined: unix.ErrnoName
go get 如下报错
go get go.elastic.co/apm/module/apmhttprouter
# go.elastic.co/apm
../../go/src/go.elastic.co/apm/error_unix.go:29:9: undefined: unix.ErrnoName
1
2
3
2
3
解决办法:Could not build on macOS for linux amd64 undefined: unix.GetsockoptLinger
open in new window
Could you please update your golang.org/x/sys/unix package by go get -u golang.org/x/sys/unix?
your local unix package may not be up to date with the change.
1
2
3
2
3
reference
- [1] Elastic. Running APM Server on Dockeropen in new window
- [2] Elastic. Instrumenting Go Source Codeopen in new window
- [3] Helm. apm-serveropen in new window