master
go 66 lines 1.59 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package postgres
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 r.handlers[runningQueriesMethodID] = newFuncRunningQueries(r)
27 return r
28 }
29
30 // Compile-time interface check.
31 var _ funcapi.MethodHandler = (*funcRouter)(nil)
32
33 func (r *funcRouter) MethodParams(ctx context.Context, method string) ([]funcapi.ParamConfig, error) {
34 if h, ok := r.handlers[method]; ok {
35 return h.MethodParams(ctx, method)
36 }
37 return nil, fmt.Errorf("unknown method: %s", method)
38 }
39
40 func (r *funcRouter) Handle(ctx context.Context, method string, params funcapi.ResolvedParams) *funcapi.FunctionResponse {
41 if h, ok := r.handlers[method]; ok {
42 return h.Handle(ctx, method, params)
43 }
44 return funcapi.NotFoundResponse(method)
45 }
46
47 func (r *funcRouter) Cleanup(ctx context.Context) {
48 for _, h := range r.handlers {
49 h.Cleanup(ctx)
50 }
51 }
52
53 func pgMethods() []funcapi.MethodConfig {
54 return []funcapi.MethodConfig{
55 topQueriesMethodConfig(),
56 runningQueriesMethodConfig(),
57 }
58 }
59
60 func pgFunctionHandler(job collectorapi.RuntimeJob) funcapi.MethodHandler {
61 c, ok := job.Collector().(*Collector)
62 if !ok {
63 return nil
64 }
65 return c.funcRouter
66 }