forked from Metrika-Inc/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter_registry.go
95 lines (81 loc) · 2.83 KB
/
exporter_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
// Copyright 2022 Metrika Inc.
// 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 global
import (
"context"
"sync"
"time"
"agent/api/v1/model"
"go.uber.org/zap"
)
// DefaultExporterRegisterer default exporter registerer to use for
// registering exporter implementations (see contrib package).
var DefaultExporterRegisterer = new(ExporterRegisterer)
// DefaultExporterTimeout exporter channel send timeout
// TODO: make timeout configurable per exporter basis
var DefaultExporterTimeout = 5 * time.Second
// Exporter interface describes the interface to be implemented for accessing
// the data stream generated by the enabled agent watchers.
type Exporter interface {
// HandleMessage optionally processes and then exports an
// incoming Metrika Agent Message (Metric or Event).
// Used as a callback function by ExporterRegisterer on
// every new message emitted by agent watchers.
HandleMessage(ctx context.Context, msg *model.Message)
}
// ExporterHandler is the registerer's subscription unit.
type ExporterHandler struct {
exporter Exporter
subscriptionCh <-chan interface{}
}
// ExporterRegisterer exporter handlers registry.
type ExporterRegisterer struct {
handlers []ExporterHandler
}
// Register registers a new exporter and its channel.
func (e *ExporterRegisterer) Register(exporter Exporter, subCh chan interface{}) error {
e.handlers = append(e.handlers, ExporterHandler{exporter: exporter, subscriptionCh: subCh})
return nil
}
// Start starts a goroutine for each configured handler.
func (e *ExporterRegisterer) Start(ctx context.Context, wg *sync.WaitGroup) error {
for i := range e.handlers {
wg.Add(1)
go func(e ExporterHandler) {
MessageListener(ctx, wg, e.subscriptionCh, e.exporter)
}(e.handlers[i])
}
return nil
}
// MessageListener reads from one Watcher emit channel
// and sequentially passes received messages to the exporter's
// HandleMessage method.
func MessageListener(ctx context.Context, wg *sync.WaitGroup, ch <-chan interface{}, e Exporter) {
defer wg.Done()
for {
select {
case m := <-ch:
message, ok := m.(*model.Message)
if !ok {
zap.S().Warnf("Unexpected type %T, skipping item", m)
continue
}
ctx, cancel := context.WithTimeout(ctx, DefaultExporterTimeout)
e.HandleMessage(ctx, message)
cancel()
case <-ctx.Done():
zap.S().Info("exiting listener")
return
}
}
}