forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
153 lines (125 loc) · 2.92 KB
/
service.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package marathon
import (
"context"
"fmt"
"log"
"sync"
"time"
"github.com/influxdata/kapacitor/services/scraper"
"github.com/prometheus/prometheus/config"
pmarathon "github.com/prometheus/prometheus/discovery/marathon"
)
// Service is the marathon discovery service
type Service struct {
Configs []Config
mu sync.Mutex
registry scraper.Registry
logger *log.Logger
open bool
}
// NewService creates a new unopened service
func NewService(c []Config, r scraper.Registry, l *log.Logger) *Service {
return &Service{
Configs: c,
registry: r,
logger: l,
}
}
// Open starts the service
func (s *Service) Open() error {
if s.open {
return nil
}
s.mu.Lock()
defer s.mu.Unlock()
s.open = true
s.register()
return s.registry.Commit()
}
// Close stops the service
func (s *Service) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
if !s.open {
return nil
}
s.open = false
s.deregister()
return s.registry.Commit()
}
func (s *Service) deregister() {
// Remove all the configurations in the registry
for _, d := range s.Configs {
s.registry.RemoveDiscoverer(&d)
}
}
func (s *Service) register() {
// Add all configurations to registry
for _, d := range s.Configs {
if d.Enabled {
s.registry.AddDiscoverer(&d)
}
}
}
// Update updates configuration while running
func (s *Service) Update(newConfigs []interface{}) error {
s.mu.Lock()
defer s.mu.Unlock()
configs := make([]Config, len(newConfigs))
for i, c := range newConfigs {
if config, ok := c.(Config); ok {
configs[i] = config
} else {
return fmt.Errorf("unexpected config object type, got %T exp %T", c, config)
}
}
s.deregister()
s.Configs = configs
s.register()
return s.registry.Commit()
}
type testOptions struct {
ID string `json:"id"`
}
// TestOptions returns an object that is in turn passed to Test.
func (s *Service) TestOptions() interface{} {
return &testOptions{}
}
// Test a service with the provided options.
func (s *Service) Test(options interface{}) error {
o, ok := options.(*testOptions)
if !ok {
return fmt.Errorf("unexpected options type %T", options)
}
found := -1
for i := range s.Configs {
if s.Configs[i].ID == o.ID && s.Configs[i].Enabled {
found = i
}
}
if found < 0 {
return fmt.Errorf("discoverer %q is not enabled or does not exist", o.ID)
}
sd := s.Configs[found].PromConfig()
discoverer, err := pmarathon.NewDiscovery(sd, scraper.NewLogger(s.logger))
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
updates := make(chan []*config.TargetGroup)
go discoverer.Run(ctx, updates)
select {
case _, ok := <-updates:
// Handle the case that a target provider exits and closes the channel
// before the context is done.
if !ok {
err = fmt.Errorf("discoverer %q exited ", o.ID)
}
break
case <-time.After(30 * time.Second):
err = fmt.Errorf("timeout waiting for discoverer %q to return", o.ID)
break
}
cancel()
return err
}