// Code generated by gowrap. DO NOT EDIT. // template: backend_prometheus.tmpl // gowrap: http://github.com/hexdigest/gowrap package identity import ( "context" "net/url" "time" "github.com/CiscoM31/godata" libregraph "github.com/opencloud-eu/libre-graph-api-go" "github.com/opencloud-eu/opencloud/services/graph/pkg/errorcode" "github.com/prometheus/client_golang/prometheus" ) // PrometheusBackend implements Backend interface with all methods wrapped // with Prometheus metrics type PrometheusBackend struct { delegate Backend metric *prometheus.HistogramVec } var _ Backend = &PrometheusBackend{} // returns an instance of the Backend decorated with prometheus metric func NewPrometheusBackend(delegate Backend, metric *prometheus.HistogramVec) PrometheusBackend { return PrometheusBackend{ delegate: delegate, metric: metric, } } // AddMembersToGroup implements Backend.AddMembersToGroup func (_d PrometheusBackend) AddMembersToGroup(ctx context.Context, groupID string, memberID []string) (err error) { _since := time.Now() defer func() { duration := time.Since(_since).Seconds() result := MetricResultSuccess if err != nil { result = MetricResultFailure if err == ErrReadOnly { result = MetricResultReadOnly } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } else { if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } _d.metric.WithLabelValues(MetricOpAddMembersToGroup, result).Observe(duration) }() return _d.delegate.AddMembersToGroup(ctx, groupID, memberID) } // CreateGroup implements Backend.CreateGroup func (_d PrometheusBackend) CreateGroup(ctx context.Context, group libregraph.Group) (gp1 *libregraph.Group, err error) { _since := time.Now() defer func() { duration := time.Since(_since).Seconds() result := MetricResultSuccess if err != nil { result = MetricResultFailure if err == ErrReadOnly { result = MetricResultReadOnly } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } else { if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } _d.metric.WithLabelValues(MetricOpCreateGroup, result).Observe(duration) }() return _d.delegate.CreateGroup(ctx, group) } // CreateUser implements Backend.CreateUser func (_d PrometheusBackend) CreateUser(ctx context.Context, user libregraph.User) (up1 *libregraph.User, err error) { _since := time.Now() defer func() { duration := time.Since(_since).Seconds() result := MetricResultSuccess if err != nil { result = MetricResultFailure if err == ErrReadOnly { result = MetricResultReadOnly } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } else { if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } _d.metric.WithLabelValues(MetricOpCreateUser, result).Observe(duration) }() return _d.delegate.CreateUser(ctx, user) } // DeleteGroup implements Backend.DeleteGroup func (_d PrometheusBackend) DeleteGroup(ctx context.Context, id string) (err error) { _since := time.Now() defer func() { duration := time.Since(_since).Seconds() result := MetricResultSuccess if err != nil { result = MetricResultFailure if err == ErrReadOnly { result = MetricResultReadOnly } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } else { if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } _d.metric.WithLabelValues(MetricOpDeleteGroup, result).Observe(duration) }() return _d.delegate.DeleteGroup(ctx, id) } // DeleteUser implements Backend.DeleteUser func (_d PrometheusBackend) DeleteUser(ctx context.Context, nameOrID string) (err error) { _since := time.Now() defer func() { duration := time.Since(_since).Seconds() result := MetricResultSuccess if err != nil { result = MetricResultFailure if err == ErrReadOnly { result = MetricResultReadOnly } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } else { if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } _d.metric.WithLabelValues(MetricOpDeleteUser, result).Observe(duration) }() return _d.delegate.DeleteUser(ctx, nameOrID) } // FilterUsers implements Backend.FilterUsers func (_d PrometheusBackend) FilterUsers(ctx context.Context, oreq *godata.GoDataRequest, filter *godata.ParseNode) (upa1 []*libregraph.User, err error) { _since := time.Now() defer func() { duration := time.Since(_since).Seconds() result := MetricResultSuccess if err != nil { result = MetricResultFailure if err == ErrReadOnly { result = MetricResultReadOnly } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } else { if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } _d.metric.WithLabelValues(MetricOpFilterUsers, result).Observe(duration) }() return _d.delegate.FilterUsers(ctx, oreq, filter) } // GetGroup implements Backend.GetGroup func (_d PrometheusBackend) GetGroup(ctx context.Context, nameOrID string, queryParam url.Values) (gp1 *libregraph.Group, err error) { _since := time.Now() defer func() { duration := time.Since(_since).Seconds() result := MetricResultSuccess if err != nil { result = MetricResultFailure if err == ErrReadOnly { result = MetricResultReadOnly } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } else { // it's a get operation that returns a pointer (and not an array): check whether that's nil or not if gp1 == nil { result = MetricResultNotFound } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } _d.metric.WithLabelValues(MetricOpGetGroup, result).Observe(duration) }() return _d.delegate.GetGroup(ctx, nameOrID, queryParam) } // GetGroupMembers implements Backend.GetGroupMembers func (_d PrometheusBackend) GetGroupMembers(ctx context.Context, id string, oreq *godata.GoDataRequest) (upa1 []*libregraph.User, err error) { _since := time.Now() defer func() { duration := time.Since(_since).Seconds() result := MetricResultSuccess if err != nil { result = MetricResultFailure if err == ErrReadOnly { result = MetricResultReadOnly } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } else { if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } _d.metric.WithLabelValues(MetricOpGetGroupMembers, result).Observe(duration) }() return _d.delegate.GetGroupMembers(ctx, id, oreq) } // GetGroups implements Backend.GetGroups func (_d PrometheusBackend) GetGroups(ctx context.Context, oreq *godata.GoDataRequest) (gpa1 []*libregraph.Group, err error) { _since := time.Now() defer func() { duration := time.Since(_since).Seconds() result := MetricResultSuccess if err != nil { result = MetricResultFailure if err == ErrReadOnly { result = MetricResultReadOnly } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } else { if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } _d.metric.WithLabelValues(MetricOpGetGroups, result).Observe(duration) }() return _d.delegate.GetGroups(ctx, oreq) } // GetUser implements Backend.GetUser func (_d PrometheusBackend) GetUser(ctx context.Context, nameOrID string, oreq *godata.GoDataRequest) (up1 *libregraph.User, err error) { _since := time.Now() defer func() { duration := time.Since(_since).Seconds() result := MetricResultSuccess if err != nil { result = MetricResultFailure if err == ErrReadOnly { result = MetricResultReadOnly } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } else { // it's a get operation that returns a pointer (and not an array): check whether that's nil or not if up1 == nil { result = MetricResultNotFound } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } _d.metric.WithLabelValues(MetricOpGetUser, result).Observe(duration) }() return _d.delegate.GetUser(ctx, nameOrID, oreq) } // GetUsers implements Backend.GetUsers func (_d PrometheusBackend) GetUsers(ctx context.Context, oreq *godata.GoDataRequest) (upa1 []*libregraph.User, err error) { _since := time.Now() defer func() { duration := time.Since(_since).Seconds() result := MetricResultSuccess if err != nil { result = MetricResultFailure if err == ErrReadOnly { result = MetricResultReadOnly } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } else { if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } _d.metric.WithLabelValues(MetricOpGetUsers, result).Observe(duration) }() return _d.delegate.GetUsers(ctx, oreq) } // RemoveMemberFromGroup implements Backend.RemoveMemberFromGroup func (_d PrometheusBackend) RemoveMemberFromGroup(ctx context.Context, groupID string, memberID string) (err error) { _since := time.Now() defer func() { duration := time.Since(_since).Seconds() result := MetricResultSuccess if err != nil { result = MetricResultFailure if err == ErrReadOnly { result = MetricResultReadOnly } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } else { if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } _d.metric.WithLabelValues(MetricOpRemoveMemberFromGroup, result).Observe(duration) }() return _d.delegate.RemoveMemberFromGroup(ctx, groupID, memberID) } // UpdateGroupName implements Backend.UpdateGroupName func (_d PrometheusBackend) UpdateGroupName(ctx context.Context, groupID string, groupName string) (err error) { _since := time.Now() defer func() { duration := time.Since(_since).Seconds() result := MetricResultSuccess if err != nil { result = MetricResultFailure if err == ErrReadOnly { result = MetricResultReadOnly } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } else { if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } _d.metric.WithLabelValues(MetricOpUpdateGroupName, result).Observe(duration) }() return _d.delegate.UpdateGroupName(ctx, groupID, groupName) } // UpdateLastSignInDate implements Backend.UpdateLastSignInDate func (_d PrometheusBackend) UpdateLastSignInDate(ctx context.Context, userID string, timestamp time.Time) (err error) { _since := time.Now() defer func() { duration := time.Since(_since).Seconds() result := MetricResultSuccess if err != nil { result = MetricResultFailure if err == ErrReadOnly { result = MetricResultReadOnly } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } else { if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } _d.metric.WithLabelValues(MetricOpUpdateLastSignInDate, result).Observe(duration) }() return _d.delegate.UpdateLastSignInDate(ctx, userID, timestamp) } // UpdateUser implements Backend.UpdateUser func (_d PrometheusBackend) UpdateUser(ctx context.Context, nameOrID string, user libregraph.UserUpdate) (up1 *libregraph.User, err error) { _since := time.Now() defer func() { duration := time.Since(_since).Seconds() result := MetricResultSuccess if err != nil { result = MetricResultFailure if err == ErrReadOnly { result = MetricResultReadOnly } if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } else { if e, ok := errorcode.ToError(err); ok { if e.GetCode() == errorcode.ItemNotFound { result = MetricResultNotFound } } } _d.metric.WithLabelValues(MetricOpUpdateUser, result).Observe(duration) }() return _d.delegate.UpdateUser(ctx, nameOrID, user) }