/
polardb_pg.go
106 lines (95 loc) · 2.93 KB
/
polardb_pg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*-------------------------------------------------------------------------
*
* polardb_pg.go
* Polardb pg metrics collector plugin wrapper
*
*
* Copyright (c) 2021, Alibaba Group Holding Limited
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* IDENTIFICATION
* plugins/polardb_pg_opensource/polardb_pg.go
*-------------------------------------------------------------------------
*/
package main
import (
"errors"
"github.com/ApsaraDB/PolarDB-NodeAgent/plugins/polardb_pg_opensource/service"
_ "github.com/lib/pq"
"github.com/ApsaraDB/PolarDB-NodeAgent/common/polardb_pg/log"
pg "github.com/ApsaraDB/PolarDB-NodeAgent/plugins/polardb_pg_opensource/collector"
)
func PluginInit(ctx interface{}) (interface{}, error) {
m, ok := ctx.(map[string]interface{})
if !ok {
return nil, errors.New("plugin polardb_pg invalid ctx")
}
log.Info("Initializing polardb_pg plugin")
collector := pg.New()
err := collector.Init(m)
if err != nil {
log.Error("plugin module polardb_pg init failed",
log.String("err", err.Error()),
log.Int("port", collector.Port),
log.String("name", collector.InsName))
return nil, err
}
svc := service.GetPolarDBPgService()
if svc == nil {
return nil, errors.New("failed to initialize PolarDBPgService")
}
return collector, err
}
func PluginRun(ctx interface{}, param interface{}) error {
collector, ok := ctx.(*pg.PolarDBPgCollector)
if !ok {
log.Error("plugin polardb_pg invalid collector_ctx")
return errors.New("plugin polardb_pg invalid collector_ctx")
}
out, ok := param.(map[string]interface{})
if !ok {
return errors.New("plugin polardb_pg invalid param")
}
err := collector.Collect(out)
if len(out) != 0 {
res := map[string]interface{}{}
for k, v := range out {
res[k] = v
}
if len(collector.HostInsIdStrList) > 0 {
for _, id := range collector.HostInsIdStrList {
service.GetPolarDBPgService().Set(id, res)
}
} else {
service.GetPolarDBPgService().Set(collector.HostInsIdStr, res)
}
}
return err
}
func PluginExit(ctx interface{}) error {
collector, ok := ctx.(*pg.PolarDBPgCollector)
if !ok {
return errors.New("plugin polardb_pg invalid ctx")
}
err := collector.Stop()
if err != nil {
log.Error("stop polardb_pg plugin err",
log.String("err", err.Error()),
log.Int("port", collector.Port))
return err
}
log.Info("stop polardb_pg plugin done",
log.String("name", collector.InsName),
log.Int("port", collector.Port))
return nil
}