/
service.go
122 lines (97 loc) · 2.42 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
// Package publisher implements
// github.com/the-anna-project/instrumentor.Publisher and provides
// instrumentation primitives to emit application metrics.
package publisher
import (
"net/http"
"strings"
"sync"
"github.com/the-anna-project/instrumentor/spec"
)
// ServiceConfig represents the configuration used to create a new memory
// publisher service.
type ServiceConfig struct {
}
// DefaultServiceConfig provides a default configuration to create a new memory
// publisher service by best effort.
func DefaultServiceConfig() ServiceConfig {
return ServiceConfig{}
}
// NewService creates a new memory publisher service.
func NewService(config ServiceConfig) (*Service, error) {
newService := &Service{
// Internals.
closer: make(chan struct{}, 1),
bootOnce: sync.Once{},
shutdownOnce: sync.Once{},
}
return newService, nil
}
type Service struct {
// Internals.
closer chan struct{}
bootOnce sync.Once
shutdownOnce sync.Once
}
func (s *Service) Boot() {
s.bootOnce.Do(func() {
// Service specific boot logic goes here.
})
}
func (s *Service) Counter(config spec.CounterConfig) (spec.Counter, error) {
newCounter, err := NewCounter(config)
if err != nil {
return nil, maskAny(err)
}
return newCounter, nil
}
func (s *Service) CounterConfig() spec.CounterConfig {
return DefaultCounterConfig()
}
func (s *Service) Gauge(config spec.GaugeConfig) (spec.Gauge, error) {
newGauge, err := NewGauge(config)
if err != nil {
return nil, maskAny(err)
}
return newGauge, nil
}
func (s *Service) GaugeConfig() spec.GaugeConfig {
return DefaultGaugeConfig()
}
func (s *Service) Histogram(config spec.HistogramConfig) (spec.Histogram, error) {
newHistogram, err := NewHistogram(config)
if err != nil {
return nil, maskAny(err)
}
return newHistogram, nil
}
func (s *Service) HistogramConfig() spec.HistogramConfig {
return DefaultHistogramConfig()
}
func (s *Service) HTTPEndpoint() string {
return ""
}
func (s *Service) HTTPHandler() http.Handler {
return nil
}
func (s *Service) Prefixes() []string {
return nil
}
func (s *Service) NewKey(str ...string) string {
return strings.Join(str, "_")
}
func (s *Service) Shutdown() {
s.shutdownOnce.Do(func() {
close(s.closer)
})
}
func (s *Service) WrapFunc(key string, action func() error) func() error {
wrappedFunc := func() error {
err := action()
if err != nil {
return maskAny(err)
}
return nil
}
return wrappedFunc
}