generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfx.go
176 lines (159 loc) · 5.26 KB
/
fx.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package touchstone
import (
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/fx"
"go.uber.org/zap"
)
const (
// Module is the name of the fx module that touchstone components
// are provided within.
Module = "touchstone"
)
// In represents the components used by this package to bootstrap
// a prometheus environment. Provide uses these components.
type In struct {
fx.In
// Config is the prometheus configuration. This is optional,
// as a zero value for Config will result in a default environment.
Config Config `optional:"true"`
// Logger is the *zap.Logger to which this package writes messages.
// This is optional, and if unset no messages are written.
Logger *zap.Logger `optional:"true"`
}
// Provide bootstraps a prometheus environment for an uber/fx App.
// The following component types are provided by this function:
//
// - prometheus.Gatherer
// - promtheus.Registerer
// NOTE: Do not rely on the Registerer actually being a *prometheus.Registry.
// It may be decorated to arbitrary depth.
// - *touchstone.Factory
func Provide() fx.Option {
return fx.Module(
Module,
fx.Provide(
func(in In) (prometheus.Gatherer, prometheus.Registerer, error) {
return New(in.Config)
},
func(r prometheus.Registerer, in In) *Factory {
return NewFactory(in.Config, in.Logger, r)
},
),
)
}
// Metric emits a named component using the specified target. The target
// is expected to be a function (constructor) of the same form accepted
// by fx.Annotated.Target.
//
// If name is empty, application startup is short-circuited with an error.
//
// See: https://pkg.go.dev/go.uber.org/fx#Annotated
func Metric(name string, target interface{}) fx.Option {
if len(name) == 0 {
return fx.Error(ErrNoMetricName)
}
return fx.Provide(
fx.Annotated{
Name: name,
Target: target,
},
)
}
// Counter uses a Factory instance from the enclosing fx.App to create and register
// a prometheus.Counter with the same component name as the metric Name.
//
// If no Name is set, application startup is short-circuited with an error.
func Counter(o prometheus.CounterOpts) fx.Option {
return Metric(
o.Name,
func(f *Factory) (prometheus.Counter, error) {
return f.NewCounter(o)
},
)
}
// CounterVec uses a Factory instance from the enclosing fx.App to create and register
// a *prometheus.CounterVec with the same component name as the metric Name.
//
// If no Name is set, application startup is short-circuited with an error.
func CounterVec(o prometheus.CounterOpts, labelNames ...string) fx.Option {
return Metric(
o.Name,
func(f *Factory) (*prometheus.CounterVec, error) {
return f.NewCounterVec(o, labelNames...)
},
)
}
// Gauge uses a Factory instance from the enclosing fx.App to create and register
// a prometheus.Gauge with the same component name as the metric Name.
//
// If no Name is set, application startup is short-circuited with an error.
func Gauge(o prometheus.GaugeOpts) fx.Option {
return Metric(
o.Name,
func(f *Factory) (prometheus.Gauge, error) {
return f.NewGauge(o)
},
)
}
// GaugeVec uses a Factory instance from the enclosing fx.App to create and register
// a *prometheus.GaugeVec with the same component name as the metric Name.
//
// If no Name is set, application startup is short-circuited with an error.
func GaugeVec(o prometheus.GaugeOpts, labelNames ...string) fx.Option {
return Metric(
o.Name,
func(f *Factory) (*prometheus.GaugeVec, error) {
return f.NewGaugeVec(o, labelNames...)
},
)
}
// Histogram uses a Factory instance from the enclosing fx.App to create and register
// a prometheus.Observer with the same component name as the metric Name.
//
// If no Name is set, application startup is short-circuited with an error.
func Histogram(o prometheus.HistogramOpts) fx.Option {
return Metric(
o.Name,
func(f *Factory) (prometheus.Observer, error) {
return f.NewHistogram(o)
},
)
}
// HistogramVec uses a Factory instance from the enclosing fx.App to create and register
// a prometheus.ObserverVec with the same component name as the metric Name.
//
// If no Name is set, application startup is short-circuited with an error.
func HistogramVec(o prometheus.HistogramOpts, labelNames ...string) fx.Option {
return Metric(
o.Name,
func(f *Factory) (prometheus.ObserverVec, error) {
return f.NewHistogramVec(o, labelNames...)
},
)
}
// Summary uses a Factory instance from the enclosing fx.App to create and register
// a prometheus.Observer with the same component name as the metric Name.
//
// If no Name is set, application startup is short-circuited with an error.
func Summary(o prometheus.SummaryOpts) fx.Option {
return Metric(
o.Name,
func(f *Factory) (prometheus.Observer, error) {
return f.NewSummary(o)
},
)
}
// SummaryVec uses a Factory instance from the enclosing fx.App to create and register
// a prometheus.ObserverVec with the same component name as the metric Name.
//
// If no Name is set, application startup is short-circuited with an error.
func SummaryVec(o prometheus.SummaryOpts, labelNames ...string) fx.Option {
return Metric(
o.Name,
func(f *Factory) (prometheus.ObserverVec, error) {
return f.NewSummaryVec(o, labelNames...)
},
)
}