[MM-55268] Implement ServeMetrics plugins hook (#24249)

* Implement ServeMetrics plugins hook

* Update error id

* Simplify

* Revert "Simplify"

This reverts commit c9dc5d5eac6c933ff69e158cf3e34d0973bd3bcd.

* Add comment and error handler

* Wrap error

* Update translation file

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Claudio Costa
2023-11-17 14:39:06 -06:00
коммит произвёл GitHub
родитель 926142ca22
Коммит aa3a12f183
10 изменённых файлов: 297 добавлений и 0 удалений

Просмотреть файл

@@ -9,6 +9,7 @@ package plugin
import (
"errors"
"io"
"net/http"
"reflect"
"github.com/mattermost/mattermost/server/public/model"
@@ -134,6 +135,10 @@ type UserHasBeenDeactivatedIFace interface {
UserHasBeenDeactivated(c *Context, user *model.User)
}
type ServeMetricsIFace interface {
ServeMetrics(c *Context, w http.ResponseWriter, r *http.Request)
}
type HooksAdapter struct {
implemented map[int]struct{}
productHooks any
@@ -417,6 +422,15 @@ func NewAdapter(productHooks any) (*HooksAdapter, error) {
return nil, errors.New("hook has UserHasBeenDeactivated method but does not implement plugin.UserHasBeenDeactivated interface")
}
// Assessing the type of the productHooks if it individually implements ServeMetrics interface.
tt = reflect.TypeOf((*ServeMetricsIFace)(nil)).Elem()
if ft.Implements(tt) {
a.implemented[ServeMetricsID] = struct{}{}
} else if _, ok := ft.MethodByName("ServeMetrics"); ok {
return nil, errors.New("hook has ServeMetrics method but does not implement plugin.ServeMetrics interface")
}
return a, nil
}
@@ -689,3 +703,12 @@ func (a *HooksAdapter) UserHasBeenDeactivated(c *Context, user *model.User) {
a.productHooks.(UserHasBeenDeactivatedIFace).UserHasBeenDeactivated(c, user)
}
func (a *HooksAdapter) ServeMetrics(c *Context, w http.ResponseWriter, r *http.Request) {
if _, ok := a.implemented[ServeMetricsID]; !ok {
panic("product hooks must implement ServeMetrics")
}
a.productHooks.(ServeMetricsIFace).ServeMetrics(c, w, r)
}