/
option.go
66 lines (58 loc) · 1.61 KB
/
option.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
// Copyright (c) 2024 The nilgo authors
// Use of this source code is governed by a MIT license found in the LICENSE file.
//nolint:ireturn
package grpc
import (
"log/slog"
"github.com/nil-go/konf"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
)
// WithTelemetry enables trace and metrics instruments on the gRPC server.
func WithTelemetry(opts ...otelgrpc.Option) grpc.ServerOption {
return serverOptionFunc{
fn: func(options *serverOptions) {
if options.otelOpts == nil {
options.otelOpts = []otelgrpc.Option{}
}
options.otelOpts = append(options.otelOpts, opts...)
},
}
}
// WithLogHandler provides the slog.Handler for gRPC logs.
//
// If the handler is not provided, it uses handler from slog.Default().
func WithLogHandler(handler slog.Handler) grpc.ServerOption {
return serverOptionFunc{
fn: func(options *serverOptions) {
if handler != nil {
options.handler = handler
}
},
}
}
// WithConfigService registers the pb.ConfigServiceServer implement to the gRPC server.
//
// It uses the global konf.Config if the configs are not provided.
func WithConfigService(configs ...*konf.Config) grpc.ServerOption {
return serverOptionFunc{
fn: func(options *serverOptions) {
if options.configs == nil {
options.configs = []*konf.Config{}
}
options.configs = append(options.configs, configs...)
},
}
}
type (
serverOptionFunc struct {
grpc.EmptyServerOption
fn func(*serverOptions)
}
serverOptions struct {
handler slog.Handler
configs []*konf.Config
otelOpts []otelgrpc.Option
grpcOpts []grpc.ServerOption
}
)