-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
loop_registry.go
101 lines (85 loc) · 2.81 KB
/
loop_registry.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
package web
import (
"encoding/json"
"fmt"
"html"
"io"
"net/http"
"github.com/gin-gonic/gin"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/plugins"
)
type LoopRegistryServer struct {
exposedPromPort int
registry *plugins.LoopRegistry
logger logger.SugaredLogger
jsonMarshalFn func(any) ([]byte, error)
}
func NewLoopRegistryServer(app chainlink.Application) *LoopRegistryServer {
return &LoopRegistryServer{
exposedPromPort: int(app.GetConfig().Port()),
registry: app.GetLoopRegistry(),
logger: app.GetLogger(),
jsonMarshalFn: json.Marshal,
}
}
// discoveryHandler implements service discovery of prom endpoints for LOOPs in the registry
func (l *LoopRegistryServer) discoveryHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
var groups []*targetgroup.Group
for _, registeredPlugin := range l.registry.List() {
// create a metric target for each running plugin
target := &targetgroup.Group{
Targets: []model.LabelSet{
{model.AddressLabel: model.LabelValue(fmt.Sprintf("localhost:%d", l.exposedPromPort))},
},
Labels: map[model.LabelName]model.LabelValue{
model.MetricsPathLabel: model.LabelValue(pluginMetricPath(registeredPlugin.Name)),
},
}
groups = append(groups, target)
}
b, err := l.jsonMarshalFn(groups)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, err = w.Write([]byte(err.Error()))
if err != nil {
l.logger.Error(err)
}
return
}
_, err = w.Write(b)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
l.logger.Error(err)
}
}
// pluginMetricHandlers routes from endpoints published in service discovery to the the backing LOOP endpoint
func (l *LoopRegistryServer) pluginMetricHandler(gc *gin.Context) {
pluginName := gc.Param("name")
p, ok := l.registry.Get(pluginName)
if !ok {
gc.Data(http.StatusNotFound, "text/plain", []byte(fmt.Sprintf("plugin %q does not exist", html.EscapeString(pluginName))))
return
}
pluginURL := fmt.Sprintf("http://localhost:%d/metrics", p.EnvCfg.PrometheusPort())
res, err := http.Get(pluginURL) //nolint
if err != nil {
gc.Data(http.StatusInternalServerError, "text/plain", []byte(err.Error()))
return
}
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
if err != nil {
err = fmt.Errorf("error reading plugin %q metrics: %w", html.EscapeString(pluginName), err)
gc.Data(http.StatusInternalServerError, "text/plain", []byte(err.Error()))
return
}
gc.Data(http.StatusOK, "text/plain", b)
}
func pluginMetricPath(name string) string {
return fmt.Sprintf("/plugins/%s/metrics", name)
}