Browse Source

[upd] ping (uptime...)

main
loveckiy.ivan 3 months ago
parent
commit
e67c442954
  1. 10
      cmd/logbox/main.go
  2. 2
      go.mod
  3. 2
      go.sum
  4. 5
      pkg/model/config.go
  5. 42
      pkg/service/ping.go
  6. 30
      pkg/transport/httpserver/routers.go
  7. 7
      vendor/git.lowcodeplatform.net/fabric/models/ping.go
  8. 2
      vendor/modules.txt

10
cmd/logbox/main.go

@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"syscall"
"time"
"git.lowcodeplatform.net/fabric/lib"
"git.lowcodeplatform.net/packages/logger"
@ -92,7 +93,14 @@ func Start(ctxm context.Context, configfile, dir, port, mode, proxy, loader, reg
cfg.Workingdir, _ = filepath.Abs(dir)
cfg.HashRun = lib.UUID()
cfg.Name = "controller"
cfg.Environment = cfg.EnvironmentPointsrc
cfg.RunTime = time.Now()
if cfg.Environment == "" {
cfg.Environment = cfg.EnvironmentPointsrc
}
if cfg.Cluster == "" {
cfg.Cluster = cfg.ClusterPointsrc
}
///////////////// ЛОГИРОВАНИЕ //////////////////
// формирование пути к лог-файлам и метрикам

2
go.mod

@ -4,7 +4,7 @@ go 1.20
require (
git.lowcodeplatform.net/fabric/lib v0.2.14
git.lowcodeplatform.net/fabric/models v0.1.27
git.lowcodeplatform.net/fabric/models v0.1.32
git.lowcodeplatform.net/packages/cache v0.1.8
git.lowcodeplatform.net/packages/logger v0.1.21
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751

2
go.sum

@ -48,6 +48,8 @@ git.lowcodeplatform.net/fabric/logbox-client v0.1.16 h1:7qeQrmqYqI1M4W77AMER+AnK
git.lowcodeplatform.net/fabric/logbox-client v0.1.16/go.mod h1:0fSeuQBhKikxfCCVlZGhuuNRMF7KVD2BHQF39r5qBuE=
git.lowcodeplatform.net/fabric/models v0.1.27 h1:LXS9y6/NWyDwBlb59kZLYxviHNAfY99H/qVLzMUGyK8=
git.lowcodeplatform.net/fabric/models v0.1.27/go.mod h1:RSR+ysusHS7bhYOCDuWbkuGQkFL0Mum4r/FXPzStUQQ=
git.lowcodeplatform.net/fabric/models v0.1.32 h1:tkz46cUv6zc6j0vQ0ZR6rwWsn2Lwm6/qbaqg7Hq/910=
git.lowcodeplatform.net/fabric/models v0.1.32/go.mod h1:RSR+ysusHS7bhYOCDuWbkuGQkFL0Mum4r/FXPzStUQQ=
git.lowcodeplatform.net/packages/cache v0.1.8 h1:LBUNza0n3z+Z9VMIDvF5AI9ui9WSF3tOg7hb9TPyS6A=
git.lowcodeplatform.net/packages/cache v0.1.8/go.mod h1:+w6N8ipAhpEw0FzTQFQ56JY4OgcIjk1HP9SsYQuajyQ=
git.lowcodeplatform.net/packages/grpcbalancer v0.0.0-20230625153511-db2331a573d5 h1:bQ4r7z77sROu4I4Ra/ja30AQeAINL7YQdcynOPBxA0s=

5
pkg/model/config.go

@ -19,6 +19,11 @@ type Config struct {
HashCommit string `envconfig:"HASH_COMMIT" default:""`
Environment string `envconfig:"ENVIRONMENT" default:"dev"`
EnvironmentPointsrc string `envconfig:"ENVIRONMENT_POINTSRC" default:"dev"`
Cluster string `envconfig:"CLUSTER" default:"alpha"`
ClusterPointsrc string `envconfig:"CLUSTER_POINTSRC" default:"alpha"`
RunTime time.Time `envconfig:"RUN_TIME" default:""`
UpTime string `envconfig:"UP_TIME" default:""`
MetricIntervalCached Duration `envconfig:"METRIC_INTERVAL_CACHED" default:"10s"`

42
pkg/service/ping.go

@ -2,38 +2,34 @@ package service
import (
"context"
"encoding/json"
"fmt"
"os"
"strconv"
"time"
"git.lowcodeplatform.net/fabric/models"
"git.lowcodeplatform.net/packages/cache"
"git.lowcodeplatform.net/packages/logger"
dto "github.com/prometheus/client_model/go"
"go.uber.org/zap"
)
// Ping ...
func (s *service) Ping(ctx context.Context) (result []models.Pong, err error) {
var mobj []*dto.MetricFamily
metrics, err := cache.Cache().Get("prometheus")
if err != nil {
metrics = fmt.Sprintf("error. %s", err)
logger.Error(ctx, "error cache (ping)", zap.Error(err))
}
bmetric, ok := metrics.([]byte)
if ok {
err = json.Unmarshal(bmetric, &mobj)
if err != nil {
metrics = fmt.Sprintf("error. %s", err)
logger.Error(ctx, "cache.Cache Unmarshal",
zap.Error(err),
zap.String("metrics failed body", fmt.Sprintf("%+v", metrics)))
}
}
//metrics, err := cache.Cache().Get("prometheus")
//if err != nil {
// metrics = fmt.Sprintf("error. %s", err)
// logger.Error(ctx, "error cache (ping)", zap.Error(err))
//}
//
//bmetric, ok := metrics.([]byte)
//if ok {
// err = json.Unmarshal(bmetric, &mobj)
// if err != nil {
// metrics = fmt.Sprintf("error. %s", err)
// logger.Error(ctx, "cache.Cache Unmarshal",
// zap.Error(err),
// zap.String("metrics failed body", fmt.Sprintf("%+v", metrics)))
// }
//}
pong := models.Pong{}
pong.Uid = s.cfg.HashRun
@ -48,6 +44,10 @@ func (s *service) Ping(ctx context.Context) (result []models.Pong, err error) {
pong.PortGrpc = s.cfg.GrpcPort
pong.PortMetric = s.cfg.HTTPPortMetric
pong.Metrics = mobj
pong.Environment = s.cfg.Environment
pong.Cluster = s.cfg.Cluster
pong.Uptime = time.Now().Sub(s.cfg.RunTime).String()
pong.Runtime = s.cfg.RunTime
pong.ServiceVersion = s.cfg.ServiceVersion
pong.HashCommit = s.cfg.HashCommit

30
pkg/transport/httpserver/routers.go

@ -1,21 +1,16 @@
package httpserver
import (
"encoding/json"
"fmt"
"net/http"
"net/http/pprof"
"path/filepath"
"git.lowcodeplatform.net/fabric/logbox/pkg/transport/httpserver/handlers"
"git.lowcodeplatform.net/packages/cache"
"git.lowcodeplatform.net/packages/logger"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
httpSwagger "github.com/swaggo/http-swagger"
"go.uber.org/zap"
)
type Result struct {
@ -37,7 +32,6 @@ type prometheusReader struct {
}
func (h *httpserver) NewRouter() *mux.Router {
var err error
router := mux.NewRouter().StrictSlash(true)
handler := handlers.New(h.ctx, h.src, h.cfg)
@ -50,18 +44,18 @@ func (h *httpserver) NewRouter() *mux.Router {
version.Version = h.serviceVersion
version.Revision = h.hashCommit
err = cache.Cache().Upsert("prometheus", func() (res interface{}, err error) {
mf, err := prometheus.DefaultGatherer.Gather()
if err != nil {
err = fmt.Errorf("error prometheus Gather. err: %s", err)
return
}
res, err = json.Marshal(mf)
return res, nil
}, h.cfg.MetricIntervalCached.Value)
if err != nil {
logger.Panic(h.ctx, "cache collection is not init", zap.Error(err))
}
//err = cache.Cache().Upsert("prometheus", func() (res interface{}, err error) {
// mf, err := prometheus.DefaultGatherer.Gather()
// if err != nil {
// err = fmt.Errorf("error prometheus Gather. err: %s", err)
// return
// }
// res, err = json.Marshal(mf)
// return res, nil
//}, h.cfg.MetricIntervalCached.Value)
//if err != nil {
// logger.Panic(h.ctx, "cache collection is not init", zap.Error(err))
//}
logboxDir, _ := filepath.Abs(h.cfg.Dir)
router.PathPrefix("/download/").Handler(http.StripPrefix("/download/", http.FileServer(http.Dir(logboxDir))))

7
vendor/git.lowcodeplatform.net/fabric/models/ping.go

@ -1,5 +1,7 @@
package models
import "time"
// Pong тип ответа, который сервис отдает прокси при периодическом опросе (ping-е)
type Pong struct {
Uid string `json:"uid"`
@ -15,10 +17,13 @@ type Pong struct {
PortMetric int `json:"portMetric"`
PortHTTPS int `json:"portHTTPS"`
EnableHttps bool `json:"enableHttps"`
DeadTime int64 `json:"dead_time"`
Follower string `json:"follower"`
Metrics interface{} `json:"metrics"`
Environment string `json:"environment"`
Cluster string `json:"cluster"`
DeadTime int64 `json:"deadtime"`
Runtime time.Time `json:"runtime"`
Uptime string `json:"uptime"`
ServiceVersion string `json:"service_version"`
HashCommit string `json:"hash_commit"`

2
vendor/modules.txt

@ -27,7 +27,7 @@ git.lowcodeplatform.net/fabric/lib/pkg/s3
# git.lowcodeplatform.net/fabric/logbox-client v0.1.16
## explicit; go 1.18
git.lowcodeplatform.net/fabric/logbox-client
# git.lowcodeplatform.net/fabric/models v0.1.27
# git.lowcodeplatform.net/fabric/models v0.1.32
## explicit; go 1.17
git.lowcodeplatform.net/fabric/models
# git.lowcodeplatform.net/packages/cache v0.1.8

Loading…
Cancel
Save