-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrpc.go
112 lines (92 loc) · 2.7 KB
/
grpc.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
package grpc
import (
"context"
"crypto/tls"
"fmt"
"log/slog"
"net"
"time"
"github.com/mcuadros/go-defaults"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health/grpc_health_v1"
ph "github.com/isometry/platform-health/pkg/platform_health"
"github.com/isometry/platform-health/pkg/provider"
"github.com/isometry/platform-health/pkg/utils"
)
var TypeGRPC = "grpc"
type GRPC struct {
Name string `mapstructure:"name"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Service string `mapstructure:"service"`
TLS bool `mapstructure:"tls" default:"false"`
Insecure bool `mapstructure:"insecure" default:"false"`
Timeout time.Duration `mapstructure:"timeout" default:"1s"`
}
func init() {
provider.Register(TypeGRPC, new(GRPC))
}
func (i *GRPC) LogValue() slog.Value {
logAttr := []slog.Attr{
slog.String("name", i.Name),
slog.String("host", i.Host),
slog.Int("port", i.Port),
slog.Any("timeout", i.Timeout),
}
return slog.GroupValue(logAttr...)
}
func (i *GRPC) SetDefaults() {
defaults.SetDefaults(i)
}
func (i *GRPC) GetType() string {
return TypeGRPC
}
func (i *GRPC) GetName() string {
return i.Name
}
func (i *GRPC) GetHealth(ctx context.Context) *ph.HealthCheckResponse {
log := utils.ContextLogger(ctx, slog.String("provider", TypeGRPC), slog.Any("instance", i))
log.Debug("checking")
component := &ph.HealthCheckResponse{
Type: TypeGRPC,
Name: i.Name,
}
defer component.LogStatus(log)
// query the standard grpc health service on host:port
// to check if the service is healthy
ctx, cancel := context.WithTimeout(ctx, i.Timeout)
defer cancel()
if i.Port == 443 {
i.TLS = true
}
dialOptions := []grpc.DialOption{}
if i.TLS {
tlsConf := &tls.Config{
ServerName: i.Host,
}
if i.Insecure {
tlsConf.InsecureSkipVerify = true
}
dialOptions = append(dialOptions, grpc.WithTransportCredentials(credentials.NewTLS(tlsConf)))
} else {
dialOptions = append(dialOptions, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
address := net.JoinHostPort(i.Host, fmt.Sprint(i.Port))
conn, err := grpc.NewClient(address, dialOptions...)
if err != nil {
return component.Unhealthy(err.Error())
}
defer conn.Close()
client := grpc_health_v1.NewHealthClient(conn)
request := &grpc_health_v1.HealthCheckRequest{Service: i.Service}
response, err := client.Check(ctx, request)
if err != nil {
return component.Unhealthy(err.Error())
}
if response.GetStatus() != grpc_health_v1.HealthCheckResponse_SERVING {
return component.Unhealthy(response.Status.String())
}
return component.Healthy()
}