zrok/controller/metrics/usageIngester.go

96 lines
2.5 KiB
Go
Raw Normal View History

2023-03-03 19:58:54 +01:00
package metrics
import (
"github.com/sirupsen/logrus"
"reflect"
2023-03-03 22:45:18 +01:00
"time"
2023-03-03 19:58:54 +01:00
)
2023-03-03 22:45:18 +01:00
func Ingest(event map[string]interface{}) *Usage {
u := &Usage{ProcessedStamp: time.Now()}
2023-03-03 19:58:54 +01:00
if ns, found := event["namespace"]; found && ns == "fabric.usage" {
if v, found := event["interval_start_utc"]; found {
if vFloat64, ok := v.(float64); ok {
2023-03-03 22:45:18 +01:00
u.IntervalStart = time.Unix(int64(vFloat64), 0)
2023-03-03 19:58:54 +01:00
} else {
logrus.Error("unable to assert 'interval_start_utc'")
}
} else {
logrus.Error("missing 'interval_start_utc'")
}
if v, found := event["tags"]; found {
if tags, ok := v.(map[string]interface{}); ok {
if v, found := tags["serviceId"]; found {
if vStr, ok := v.(string); ok {
2023-03-03 22:45:18 +01:00
u.ZitiServiceId = vStr
2023-03-03 19:58:54 +01:00
} else {
logrus.Error("unable to assert 'tags/serviceId'")
}
} else {
logrus.Error("missing 'tags/serviceId'")
}
} else {
logrus.Errorf("unable to assert 'tags'")
}
} else {
logrus.Errorf("missing 'tags'")
}
if v, found := event["usage"]; found {
if usage, ok := v.(map[string]interface{}); ok {
2023-03-03 22:45:18 +01:00
if v, found := usage["ingress.tx"]; found {
if vFloat64, ok := v.(float64); ok {
u.FrontendTx = int64(vFloat64)
} else {
logrus.Error("unable to assert 'usage/ingress.tx'")
}
} else {
logrus.Warn("missing 'usage/ingress.tx'")
}
if v, found := usage["ingress.rx"]; found {
if vFloat64, ok := v.(float64); ok {
u.FrontendRx = int64(vFloat64)
} else {
logrus.Error("unable to assert 'usage/ingress.rx")
}
} else {
logrus.Warn("missing 'usage/ingress.rx")
}
2023-03-03 19:58:54 +01:00
if v, found := usage["egress.tx"]; found {
if vFloat64, ok := v.(float64); ok {
2023-03-03 22:45:18 +01:00
u.BackendTx = int64(vFloat64)
2023-03-03 19:58:54 +01:00
} else {
logrus.Error("unable to assert 'usage/egress.tx'")
}
} else {
2023-03-03 22:45:18 +01:00
logrus.Warn("missing 'usage/egress.tx'")
2023-03-03 19:58:54 +01:00
}
if v, found := usage["egress.rx"]; found {
if vFloat64, ok := v.(float64); ok {
2023-03-03 22:45:18 +01:00
u.BackendRx = int64(vFloat64)
2023-03-03 19:58:54 +01:00
} else {
logrus.Error("unable to assert 'usage/egress.rx'")
}
} else {
2023-03-03 22:45:18 +01:00
logrus.Warn("missing 'usage/egress.rx'")
2023-03-03 19:58:54 +01:00
}
} else {
logrus.Errorf("unable to assert 'usage' (%v) %v", reflect.TypeOf(v), event)
2023-03-03 19:58:54 +01:00
}
} else {
2023-03-03 22:45:18 +01:00
logrus.Warnf("missing 'usage'")
2023-03-03 19:58:54 +01:00
}
if v, found := event["circuit_id"]; found {
if vStr, ok := v.(string); ok {
2023-03-03 22:45:18 +01:00
u.ZitiCircuitId = vStr
2023-03-03 19:58:54 +01:00
} else {
logrus.Error("unable to assert 'circuit_id'")
}
} else {
2023-03-03 22:45:18 +01:00
logrus.Warn("missing 'circuit_id'")
2023-03-03 19:58:54 +01:00
}
} else {
logrus.Errorf("not 'fabric.usage'")
}
2023-03-03 22:45:18 +01:00
return u
2023-03-03 19:58:54 +01:00
}