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