76 lines
2.2 KiB
Go
76 lines
2.2 KiB
Go
package monitor
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"oc-datacenter/conf"
|
|
"sync"
|
|
"time"
|
|
|
|
oclib "cloud.o-forge.io/core/oc-lib"
|
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
"cloud.o-forge.io/core/oc-lib/models/booking"
|
|
"cloud.o-forge.io/core/oc-lib/models/common/models"
|
|
"cloud.o-forge.io/core/oc-lib/models/live"
|
|
"cloud.o-forge.io/core/oc-lib/models/resources"
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
type MonitorInterface interface {
|
|
Stream(ctx context.Context, bookingID string, interval time.Duration, ws *websocket.Conn)
|
|
}
|
|
|
|
var _monitorService = map[string]func() MonitorInterface{
|
|
"prometheus": func() MonitorInterface { return NewPrometheusService() },
|
|
"vector": func() MonitorInterface { return NewVectorService() },
|
|
}
|
|
|
|
func NewMonitorService() (MonitorInterface, error) {
|
|
service, ok := _monitorService[conf.GetConfig().MonitorMode]
|
|
if !ok {
|
|
return nil, errors.New("monitor service not found")
|
|
}
|
|
return service(), nil
|
|
}
|
|
|
|
func Call(book *booking.Booking,
|
|
f func(*live.LiveDatacenter, *resources.ComputeResourceInstance,
|
|
map[string]models.MetricsSnapshot, *sync.WaitGroup, *sync.Mutex)) (*booking.Booking, map[string]models.MetricsSnapshot) {
|
|
logger := oclib.GetLogger()
|
|
metrics := map[string]models.MetricsSnapshot{}
|
|
var wg sync.WaitGroup
|
|
var mu sync.Mutex
|
|
|
|
cUAccess := oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.LIVE_DATACENTER), nil)
|
|
cRAccess := oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.COMPUTE_RESOURCE), nil)
|
|
|
|
rr := cRAccess.LoadOne(book.ResourceID)
|
|
if rr.Err != "" {
|
|
logger.Err(fmt.Errorf("can't proceed because of unfound resource %s : %s", book.ResourceID, rr.Err))
|
|
return book, metrics
|
|
}
|
|
computeRes := rr.ToComputeResource()
|
|
for _, instance := range computeRes.Instances {
|
|
res := cUAccess.Search(&dbs.Filters{
|
|
And: map[string][]dbs.Filter{
|
|
"source": {{Operator: dbs.EQUAL.String(), Value: instance.Source}},
|
|
"abstractlive.resources_id": {{Operator: dbs.EQUAL.String(), Value: computeRes.GetID()}},
|
|
},
|
|
}, "", false)
|
|
if res.Err != "" {
|
|
continue
|
|
}
|
|
for _, r := range res.Data {
|
|
dc := r.(*live.LiveDatacenter)
|
|
if dc.MonitorPath == "" {
|
|
continue
|
|
}
|
|
wg.Add(1)
|
|
go f(dc, instance, metrics, &wg, &mu)
|
|
}
|
|
}
|
|
wg.Wait()
|
|
return book, metrics
|
|
}
|