12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package prometheus
- import (
- extConfig "go-admin/config"
- "net"
- "net/http"
- "time"
- log "github.com/go-admin-team/go-admin-core/logger"
- "github.com/go-admin-team/go-admin-core/sdk/pkg"
- "github.com/prometheus/client_golang/api"
- v1 "github.com/prometheus/client_golang/api/prometheus/v1"
- )
- var promeApi *v1.API
- func Setup() {
- config := extConfig.ExtConfig.Prometheus
- client, err := api.NewClient(api.Config{
- Address: config.Address,
- RoundTripper: &http.Transport{
- Proxy: http.ProxyFromEnvironment,
- DialContext: (&net.Dialer{
- Timeout: 3 * time.Second,
- KeepAlive: 30 * time.Second,
- }).DialContext,
- TLSHandshakeTimeout: 1 * time.Second,
- },
- })
- if err != nil {
- // return errors.Wrap(err, "初始化prometheus client失败")
- log.Fatal(pkg.Red("prometheus connect error:"), err)
- }
- v1api := v1.NewAPI(client)
- promeApi = &v1api
- }
- func PromeApi() v1.API {
- if promeApi == nil {
- Setup()
- }
- return *promeApi
- }
|