zitadel/internal/view/repository/failed_events.go
Fabi 6b3f5b984c
feat: metrics (#1024)
* refactor: switch from opencensus to opentelemetry

* tempo works as designed nooooot

* fix: log traceids

* with grafana agent

* fix: http tracing

* fix: cleanup files

* chore: remove todo

* fix: bad test

* fix: ignore methods in grpc interceptors

* fix: remove test log

* clean up

* typo

* fix(config): configure tracing endpoint

* fix(span): add error id to span

* feat: metrics package

* feat: metrics package

* fix: counter

* fix: metric

* try metrics

* fix: coutner metrics

* fix: active sessin counter

* fix: active sessin counter

* fix: change current Sequence table

* fix: change current Sequence table

* fix: current sequences

* fix: spooler div metrics

* fix: console view

* fix: merge master

* fix: Last spool run on search result instead of eventtimestamp

* fix: go mod

* Update console/src/assets/i18n/de.json

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* fix: pr review

* fix: map

* update oidc pkg

* fix: handlers

* fix: value observer

* fix: remove fmt

* fix: handlers

* fix: tests

* fix: handler minimum cycle duration 1s

* fix(spooler): handler channel buffer

* fix interceptors

Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2020-12-02 08:50:59 +01:00

133 lines
3.6 KiB
Go

package repository
import (
"strings"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/model"
view_model "github.com/caos/zitadel/internal/view/model"
"github.com/jinzhu/gorm"
)
const (
errViewNameKey = "view_name"
errFailedSeqKey = "failed_sequence"
)
type FailedEvent struct {
ViewName string `gorm:"column:view_name;primary_key"`
FailedSequence uint64 `gorm:"column:failed_sequence;primary_key"`
FailureCount uint64 `gorm:"column:failure_count"`
ErrMsg string `gorm:"column:err_msg"`
}
type FailedEventSearchQuery struct {
Key FailedEventSearchKey
Method model.SearchMethod
Value interface{}
}
func (req FailedEventSearchQuery) GetKey() ColumnKey {
return failedEventSearchKey(req.Key)
}
func (req FailedEventSearchQuery) GetMethod() model.SearchMethod {
return req.Method
}
func (req FailedEventSearchQuery) GetValue() interface{} {
return req.Value
}
type FailedEventSearchKey int32
const (
FailedEventKeyUndefined FailedEventSearchKey = iota
FailedEventKeyViewName
FailedEventKeyFailedSequence
)
type failedEventSearchKey FailedEventSearchKey
func (key failedEventSearchKey) ToColumnName() string {
switch FailedEventSearchKey(key) {
case FailedEventKeyViewName:
return "view_name"
case FailedEventKeyFailedSequence:
return "failed_sequence"
default:
return ""
}
}
func FailedEventFromModel(failedEvent *view_model.FailedEvent) *FailedEvent {
return &FailedEvent{
ViewName: failedEvent.Database + "." + failedEvent.ViewName,
FailureCount: failedEvent.FailureCount,
FailedSequence: failedEvent.FailedSequence,
ErrMsg: failedEvent.ErrMsg,
}
}
func FailedEventToModel(failedEvent *FailedEvent) *view_model.FailedEvent {
dbView := strings.Split(failedEvent.ViewName, ".")
return &view_model.FailedEvent{
Database: dbView[0],
ViewName: dbView[1],
FailureCount: failedEvent.FailureCount,
FailedSequence: failedEvent.FailedSequence,
ErrMsg: failedEvent.ErrMsg,
}
}
func SaveFailedEvent(db *gorm.DB, table string, failedEvent *FailedEvent) error {
save := PrepareSave(table)
err := save(db, failedEvent)
if err != nil {
return errors.ThrowInternal(err, "VIEW-4F8us", "unable to updated failed events")
}
return nil
}
func RemoveFailedEvent(db *gorm.DB, table string, failedEvent *FailedEvent) error {
delete := PrepareDeleteByKeys(table,
Key{Key: failedEventSearchKey(FailedEventKeyViewName), Value: failedEvent.ViewName},
Key{Key: failedEventSearchKey(FailedEventKeyFailedSequence), Value: failedEvent.FailedSequence},
)
return delete(db)
}
func LatestFailedEvent(db *gorm.DB, table, viewName string, sequence uint64) (*FailedEvent, error) {
failedEvent := new(FailedEvent)
queries := []SearchQuery{
FailedEventSearchQuery{Key: FailedEventKeyViewName, Method: model.SearchMethodEqualsIgnoreCase, Value: viewName},
FailedEventSearchQuery{Key: FailedEventKeyFailedSequence, Method: model.SearchMethodEquals, Value: sequence},
}
query := PrepareGetByQuery(table, queries...)
err := query(db, failedEvent)
if err == nil && failedEvent.ViewName != "" {
return failedEvent, nil
}
if errors.IsNotFound(err) {
return &FailedEvent{
ViewName: viewName,
FailedSequence: sequence,
FailureCount: 0,
}, nil
}
return nil, errors.ThrowInternalf(err, "VIEW-9LyCB", "unable to get failed events of %s", viewName)
}
func AllFailedEvents(db *gorm.DB, table string) ([]*FailedEvent, error) {
failedEvents := make([]*FailedEvent, 0)
query := PrepareSearchQuery(table, GeneralSearchRequest{})
_, err := query(db, &failedEvents)
if err != nil {
return nil, err
}
return failedEvents, nil
}