/
source_instrumented.go
50 lines (43 loc) · 1.29 KB
/
source_instrumented.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
package monitoring
import (
"context"
"time"
)
// NewInstrumentedSourceFactory wraps a Source and transparently monitors it.
func NewInstrumentedSourceFactory(sourceFactory SourceFactory, chainMetrics ChainMetrics) SourceFactory {
return &instrumentedSourceFactory{sourceFactory, chainMetrics}
}
type instrumentedSourceFactory struct {
sourceFactory SourceFactory
chainMetrics ChainMetrics
}
func (i *instrumentedSourceFactory) NewSource(chainConfig ChainConfig, feedConfig FeedConfig) (Source, error) {
source, err := i.sourceFactory.NewSource(chainConfig, feedConfig)
if err != nil {
return nil, err
}
return &instrumentedSource{
i.sourceFactory.GetType(),
source,
NewFeedMetrics(chainConfig, feedConfig),
}, nil
}
func (i *instrumentedSourceFactory) GetType() string {
return i.sourceFactory.GetType()
}
type instrumentedSource struct {
sourceType string
source Source
feedMetrics FeedMetrics
}
func (i *instrumentedSource) Fetch(ctx context.Context) (interface{}, error) {
fetchStart := time.Now()
data, err := i.source.Fetch(ctx)
i.feedMetrics.ObserveFetchFromSourceDuraction(time.Since(fetchStart), i.sourceType)
if err != nil {
i.feedMetrics.IncFetchFromSourceFailed(i.sourceType)
} else {
i.feedMetrics.IncFetchFromSourceSucceeded(i.sourceType)
}
return data, err
}