This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.go
317 lines (259 loc) · 9.39 KB
/
options.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright © 2023 gocosi authors. All Rights Reserved.
//
// 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 gocosi
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"os/user"
grpchandlers "github.com/doomshrine/gocosi/grpc/handlers"
grpclog "github.com/doomshrine/gocosi/internal/log"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
"github.com/hellofresh/health-go/v5"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/propagation"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"google.golang.org/grpc"
)
// WithCOSIEndpoint overrides the default COSI endpoint.
func WithCOSIEndpoint(url *url.URL) Option {
return func(d *Driver) error {
if url.Scheme != SchemeUNIX && url.Scheme != SchemeTCP {
return errors.New("scheme should be either unix or tcp")
}
d.endpoint.address = url
return nil
}
}
// WithSocketPermissions is used to override default permissions (0o660).
// Permissions that are being set must be between:
// - 0o600 - the minimum permissions
// - 0o766 - the maximum permissions
func WithSocketPermissions(perm os.FileMode) Option {
return func(d *Driver) error {
const (
minPermissions os.FileMode = 0o600
maxPermissions os.FileMode = 0o766
)
if perm < minPermissions || perm > maxPermissions {
return fmt.Errorf("permissions out of range, minimum %d, maximum %d",
minPermissions,
maxPermissions)
}
d.endpoint.permissions = perm
return nil
}
}
// WithSocketUser is used to override default user owning the socket (current user).
func WithSocketUser(user *user.User) Option {
return func(d *Driver) error {
d.endpoint.user = user
return nil
}
}
// WithSocketGroup is used to override default group owning the socket (current user's group).
func WithSocketGroup(group *user.Group) Option {
return func(d *Driver) error {
d.endpoint.group = group
return nil
}
}
// WithGRPCOptions overrides all previously applied gRPC ServerOptions by a default options.
//
// Default gRPC SeverOptions are:
// - ChainUnaryInterceptor - consists of:
// - grpc.UnaryServerInterceptor() - starts and configures tracer for each request,
// records events for request and response (error is recorded as normal event);
// - logging.UnaryServerInterceptor() - records and logs according to the global logger (wrapped around grpc/log.Logger);
// - recovery.UnaryServerInterceptor() - records metric for panics, and recovers (a log is created for each panic);
func WithDefaultGRPCOptions() Option {
return func(d *Driver) error {
grpclogger := &grpclog.Logger{LoggerImpl: log}
d.grpcOptions = []grpc.ServerOption{
grpc.StatsHandler(otelgrpc.NewServerHandler()),
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(grpclogger),
recovery.UnaryServerInterceptor(recovery.WithRecoveryHandler(grpchandlers.PanicRecovery(grpclogger,
func(ctx context.Context) { PanicsTotal.Add(ctx, 1) }))),
),
}
return nil
}
}
// WithGRPCOptions overrides all previously applied gRPC ServerOptions by a set provided as argument to this call.
func WithGRPCOptions(opts ...grpc.ServerOption) Option {
return func(d *Driver) error {
var (
joinedErrors error
grpcOptions []grpc.ServerOption
)
for _, opt := range opts {
if opt != nil {
joinedErrors = errors.Join(joinedErrors, errors.New("nil option provided"))
} else {
grpcOptions = append(grpcOptions, opt)
}
}
d.grpcOptions = grpcOptions
return joinedErrors
}
}
// ExporterKind is an enumeration representing different exporter types.
type ExporterKind int
const (
// HTTPExporter represents an HTTP telemetry exporter.
HTTPExporter ExporterKind = iota
// GRPCExporter represents a gRPC telemetry exporter.
GRPCExporter ExporterKind = iota
)
// WithDefaultMetricExporter returns an Option function to set the default metric exporter based on the provided kind.
func WithDefaultMetricExporter(kind ExporterKind) Option {
switch kind {
case HTTPExporter:
return WithHTTPMetricExporter()
case GRPCExporter:
return WithGRPCMetricExporter()
default:
panic(fmt.Sprintf("unexpected kind: %#+v", kind))
}
}
// WithHTTPMetricExporter returns an Option function to configure an HTTP metric exporter.
func WithHTTPMetricExporter(opt ...otlpmetrichttp.Option) Option {
return func(d *Driver) error {
exp, err := otlpmetrichttp.New(context.TODO(), opt...)
if err != nil {
return fmt.Errorf("unable to create new OTLP Metric HTTP Exporter: %w", err)
}
shutdown, err := registerMetricExporter(d.resource, exp)
if err != nil {
return fmt.Errorf("unable to register OTLP Metric HTTP Exporter: %w", err)
}
d.metricShutdownFunc = shutdown
return nil
}
}
// WithGRPCMetricExporter returns an Option function to configure a gRPC metric exporter.
func WithGRPCMetricExporter(opt ...otlpmetricgrpc.Option) Option {
return func(d *Driver) error {
exp, err := otlpmetricgrpc.New(context.TODO(), opt...)
if err != nil {
return fmt.Errorf("unable to create new OTLP Metric GRPC Exporter: %w", err)
}
shutdown, err := registerMetricExporter(d.resource, exp)
if err != nil {
return fmt.Errorf("unable to register OTLP Metric GRPC Exporter: %w", err)
}
d.metricShutdownFunc = shutdown
return nil
}
}
// WithDefaultTraceExporter returns an Option function to set the default trace exporter based on the provided kind.
func WithDefaultTraceExporter(kind ExporterKind) Option {
switch kind {
case HTTPExporter:
return WithHTTPTraceExporter()
case GRPCExporter:
return WithGRPCTraceExporter()
default:
panic(fmt.Sprintf("unexpected kind: %#+v", kind))
}
}
// WithHTTPTraceExporter returns an Option function to configure an HTTP trace exporter.
func WithHTTPTraceExporter(opt ...otlptracehttp.Option) Option {
return func(d *Driver) error {
exp, err := otlptracehttp.New(context.TODO(), opt...)
if err != nil {
return fmt.Errorf("unable to create new OTLP Trace HTTP Exporter: %w", err)
}
shutdown, err := registerTraceExporter(d.resource, exp)
if err != nil {
return fmt.Errorf("unable to register OTLP Trace HTTP Exporter: %w", err)
}
d.traceShutdownFunc = shutdown
return nil
}
}
// WithGRPCTraceExporter returns an Option function to configure a gRPC trace exporter.
func WithGRPCTraceExporter(opt ...otlptracegrpc.Option) Option {
return func(d *Driver) error {
exp, err := otlptracegrpc.New(context.TODO(), opt...)
if err != nil {
return fmt.Errorf("unable to create new OTLP Trace GRPC Exporter: %w", err)
}
shutdown, err := registerTraceExporter(d.resource, exp)
if err != nil {
return fmt.Errorf("unable to register OTLP Trace GRPC Exporter: %w", err)
}
d.traceShutdownFunc = shutdown
return nil
}
}
// WithHealthcheck returns an Option function that sets up a healthcheck service for the driver.
// It accepts options for configuring the healthcheck service.
func WithHealthcheck(options ...health.Option) Option {
return func(d *Driver) error {
h, err := health.New(options...)
if err != nil {
return fmt.Errorf("unable to create new healthcheck service: %w", err)
}
d.healthz = h
if d.mux == nil {
// This should not occur, but just for my sanity...
return ErrNilMux
}
d.mux.Handle(HealthcheckEndpoint, d.healthz.Handler())
return nil
}
}
func registerTraceExporter(res *resource.Resource, exporter sdktrace.SpanExporter) (func(context.Context) error, error) {
bsp := sdktrace.NewBatchSpanProcessor(exporter)
options := []sdktrace.TracerProviderOption{
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSpanProcessor(bsp),
}
if res != nil {
options = append(options, sdktrace.WithResource(res))
}
tp := sdktrace.NewTracerProvider(options...)
otel.SetTracerProvider(tp)
// set global propagator to tracecontext (the default is no-op).
otel.SetTextMapPropagator(propagation.TraceContext{})
// Shutdown will flush any remaining spans and shut down the exporter.
return tp.Shutdown, nil
}
func registerMetricExporter(res *resource.Resource, exporter sdkmetric.Exporter) (func(context.Context) error, error) {
// This reader is used as a stand-in for a reader that will actually export
// data. See exporters in the go.opentelemetry.io/otel/exporters package
// for more information.
reader := sdkmetric.NewPeriodicReader(exporter)
options := []sdkmetric.Option{
sdkmetric.WithReader(reader),
}
if res != nil {
options = append(options, sdkmetric.WithResource(res))
}
mp := sdkmetric.NewMeterProvider(options...)
otel.SetMeterProvider(mp)
return mp.Shutdown, nil
}