master
go 68 lines 1.67 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package mssql
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[deadlockInfoMethodID] = newFuncDeadlockInfo(r)
27 r.handlers[errorInfoMethodID] = newFuncErrorInfo(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 mssqlMethods() []funcapi.MethodConfig {
55 return []funcapi.MethodConfig{
56 topQueriesMethodConfig(),
57 deadlockInfoMethodConfig(),
58 errorInfoMethodConfig(),
59 }
60 }
61
62 func mssqlFunctionHandler(job collectorapi.RuntimeJob) funcapi.MethodHandler {
63 c, ok := job.Collector().(*Collector)
64 if !ok {
65 return nil
66 }
67 return c.funcRouter
68 }