master
go 68 lines 1.67 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package mysqlfunc
4
5 import (
6 "context"
7 "fmt"
8
9 "github.com/netdata/netdata/go/plugins/logger"
10 "github.com/netdata/netdata/go/plugins/pkg/funcapi"
11 )
12
13 // funcRouter routes method calls to appropriate function handlers.
14 type router struct {
15 deps Deps
16 cfg FunctionsConfig
17 log *logger.Logger
18
19 handlers map[string]funcapi.MethodHandler
20 }
21
22 func newRouter(deps Deps, log *logger.Logger, cfg FunctionsConfig) *router {
23 r := &router{
24 deps: deps,
25 cfg: cfg,
26 log: log,
27 handlers: make(map[string]funcapi.MethodHandler),
28 }
29 r.handlers[topQueriesMethodID] = newFuncTopQueries(r)
30 r.handlers[deadlockInfoMethodID] = newFuncDeadlockInfo(r)
31 r.handlers[errorInfoMethodID] = newFuncErrorInfo(r)
32 return r
33 }
34
35 // Compile-time interface check.
36 var _ funcapi.MethodHandler = (*router)(nil)
37
38 func (r *router) MethodParams(ctx context.Context, method string) ([]funcapi.ParamConfig, error) {
39 if h, ok := r.handlers[method]; ok {
40 return h.MethodParams(ctx, method)
41 }
42 return nil, fmt.Errorf("unknown method: %s", method)
43 }
44
45 func (r *router) Handle(ctx context.Context, method string, params funcapi.ResolvedParams) *funcapi.FunctionResponse {
46 if h, ok := r.handlers[method]; ok {
47 return h.Handle(ctx, method, params)
48 }
49 return funcapi.NotFoundResponse(method)
50 }
51
52 func (r *router) Cleanup(ctx context.Context) {
53 for _, h := range r.handlers {
54 h.Cleanup(ctx)
55 }
56 }
57
58 func Methods() []funcapi.MethodConfig {
59 return []funcapi.MethodConfig{
60 topQueriesMethodConfig(),
61 deadlockInfoMethodConfig(),
62 errorInfoMethodConfig(),
63 }
64 }
65
66 func NewRouter(deps Deps, log *logger.Logger, cfg FunctionsConfig) funcapi.MethodHandler {
67 return newRouter(deps, log, cfg)
68 }