initialize.go 947 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package prometheus
  2. import (
  3. extConfig "go-admin/config"
  4. "net"
  5. "net/http"
  6. "time"
  7. log "github.com/go-admin-team/go-admin-core/logger"
  8. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  9. "github.com/prometheus/client_golang/api"
  10. v1 "github.com/prometheus/client_golang/api/prometheus/v1"
  11. )
  12. var promeApi *v1.API
  13. func Setup() {
  14. config := extConfig.ExtConfig.Prometheus
  15. client, err := api.NewClient(api.Config{
  16. Address: config.Address,
  17. RoundTripper: &http.Transport{
  18. Proxy: http.ProxyFromEnvironment,
  19. DialContext: (&net.Dialer{
  20. Timeout: 3 * time.Second,
  21. KeepAlive: 30 * time.Second,
  22. }).DialContext,
  23. TLSHandshakeTimeout: 1 * time.Second,
  24. },
  25. })
  26. if err != nil {
  27. // return errors.Wrap(err, "初始化prometheus client失败")
  28. log.Fatal(pkg.Red("prometheus connect error:"), err)
  29. }
  30. v1api := v1.NewAPI(client)
  31. promeApi = &v1api
  32. }
  33. func PromeApi() v1.API {
  34. if promeApi == nil {
  35. Setup()
  36. }
  37. return *promeApi
  38. }