forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.go
120 lines (97 loc) · 3.2 KB
/
backend.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
// Copyright The OpenTelemetry Authors
//
// 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.
package file
import (
"fmt"
"log"
"sync"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"go.opentelemetry.io/collector/config"
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/dynamicconfig/model"
pb "github.com/open-telemetry/opentelemetry-collector-contrib/extension/dynamicconfig/proto/experimental/metrics/configservice"
res "github.com/open-telemetry/opentelemetry-collector-contrib/extension/dynamicconfig/proto/resource/v1"
)
// A Backend is a ConfigBackend that uses a local file to determine what
// schedules to change. The file is read live, so changes to it will reflect
// immediately in the configs.
type Backend struct {
viper *viper.Viper
mu sync.Mutex
configModel *model.Config // protected by mutex
waitTime int32
updateCh chan struct{} // syncs updates; meant for testing
}
func NewBackend(configFile string) (*Backend, error) {
backend := &Backend{
viper: config.NewViper(),
waitTime: 30,
}
backend.viper.SetConfigFile(configFile)
if err := backend.viper.ReadInConfig(); err != nil {
return nil, fmt.Errorf("local backend failed to read config: %w", err)
}
if err := backend.updateConfig(); err != nil {
return nil, err
}
backend.viper.WatchConfig()
backend.viper.OnConfigChange(func(e fsnotify.Event) {
if err := backend.updateConfig(); err != nil {
log.Printf("failed to update configs: %v", err)
}
if backend.updateCh != nil {
backend.updateCh <- struct{}{}
}
})
return backend, nil
}
func (backend *Backend) updateConfig() error {
var configModel model.Config
if err := backend.viper.UnmarshalExact(&configModel); err != nil {
return fmt.Errorf("file backend failed to decode config: %w", err)
}
backend.mu.Lock()
defer backend.mu.Unlock()
backend.configModel = &configModel
return nil
}
// BuildConfigResponse builds a MetricConfigResponse based on the config data
// and backend settings.
func (backend *Backend) BuildConfigResponse(resource *res.Resource) (*pb.MetricConfigResponse, error) {
backend.mu.Lock()
defer backend.mu.Unlock()
configBlock := backend.configModel.Match(resource)
schedulesProto, err := configBlock.Proto()
if err != nil {
return nil, err
}
resp := &pb.MetricConfigResponse{
Fingerprint: configBlock.Hash(),
Schedules: schedulesProto,
SuggestedWaitTimeSec: backend.waitTime,
}
return resp, nil
}
func (backend *Backend) GetWaitTime() int32 {
return backend.waitTime
}
func (backend *Backend) SetWaitTime(waitTime int32) {
if waitTime > 0 {
backend.waitTime = waitTime
}
}
func (backend *Backend) Close() error {
// TODO: need to cleanup Viper resources?
return nil
}