master
go 71 lines 1.8 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package oracledb
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 // Uses shared SQL connection from Collector (OracleDB metrics collection uses SQL).
15 type funcRouter struct {
16 collector *Collector // for shared DB access and config
17
18 handlers map[string]funcapi.MethodHandler
19 }
20
21 func newFuncRouter(c *Collector) *funcRouter {
22 r := &funcRouter{
23 collector: c,
24 handlers: make(map[string]funcapi.MethodHandler),
25 }
26 r.handlers[topQueriesMethodID] = newFuncTopQueries(r)
27 r.handlers[runningQueriesMethodID] = newFuncRunningQueries(r)
28 return r
29 }
30
31 // Compile-time interface check.
32 var _ funcapi.MethodHandler = (*funcRouter)(nil)
33
34 func (r *funcRouter) MethodParams(ctx context.Context, method string) ([]funcapi.ParamConfig, error) {
35 if h, ok := r.handlers[method]; ok {
36 return h.MethodParams(ctx, method)
37 }
38 return nil, fmt.Errorf("unknown method: %s", method)
39 }
40
41 func (r *funcRouter) Handle(ctx context.Context, method string, params funcapi.ResolvedParams) *funcapi.FunctionResponse {
42 if h, ok := r.handlers[method]; ok {
43 return h.Handle(ctx, method, params)
44 }
45 return funcapi.NotFoundResponse(method)
46 }
47
48 func (r *funcRouter) Cleanup(ctx context.Context) {
49 for _, h := range r.handlers {
50 h.Cleanup(ctx)
51 }
52 }
53
54 func (r *funcRouter) topQueriesLimit() int {
55 return r.collector.topQueriesLimit()
56 }
57
58 func oracledbMethods() []funcapi.MethodConfig {
59 return []funcapi.MethodConfig{
60 topQueriesMethodConfig(),
61 runningQueriesMethodConfig(),
62 }
63 }
64
65 func oracledbFunctionHandler(job collectorapi.RuntimeJob) funcapi.MethodHandler {
66 c, ok := job.Collector().(*Collector)
67 if !ok {
68 return nil
69 }
70 return c.funcRouter
71 }