Skip to content

Commit

Permalink
feat: add grpc healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Jun 5, 2024
1 parent ac7ba27 commit c1b601b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
22 changes: 15 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ import (
"path/filepath"
"strings"
"syscall"
"time"

_ "github.com/gripmock/grpc-interceptors"
"github.com/rs/zerolog"
_ "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
_ "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"google.golang.org/grpc"
_ "google.golang.org/grpc/health"
healthv1 "google.golang.org/grpc/health/grpc_health_v1"

_ "github.com/bavix/gripmock-sdk-go"
"github.com/bavix/gripmock/internal/pkg/patcher"
"github.com/bavix/gripmock/pkg/dependencies"
"github.com/bavix/gripmock/stub"
)

var version string
var version = "development"

//nolint:funlen,cyclop
func main() {
Expand Down Expand Up @@ -58,11 +62,6 @@ func main() {
logger.Fatal().Msg("at least one proto file is required")
}

// for backwards compatibility
if os.Args[1] == "gripmock" {
os.Args = append(os.Args[:1], os.Args[2:]...)
}

//nolint:godox
// fixme: move validation of required arguments to a separate service
logger.Info().Str("release", version).Msg("Starting GripMock")
Expand Down Expand Up @@ -105,7 +104,16 @@ func main() {
// I have an idea to combine gripmock and grpcmock services into one, then this check will be easier to do.
// Checking the grpc port of the service. If the port appears, the service has started successfully.
go func() {
if _, err := builder.Reflector().Services(ctx); err == nil {
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()

waiter := healthv1.NewHealthClient(builder.GRPCClient())
check, err := waiter.Check(ctx, &healthv1.HealthCheckRequest{Service: ""}, grpc.WaitForReady(true))
if err != nil {
return
}

if check.GetStatus() == healthv1.HealthCheckResponse_SERVING {
chReady <- struct{}{}
}
}()
Expand Down
3 changes: 3 additions & 0 deletions protoc-gen-gripmock/server.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/grpc/health"
healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
_ "google.golang.org/grpc/encoding/gzip"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
Expand Down Expand Up @@ -65,6 +67,7 @@ func main() {
{{ template "register_services" . }}
{{ end }}

healthgrpc.RegisterHealthServer(s, health.NewServer())
reflection.Register(s)

builder.Logger().Info().
Expand Down

0 comments on commit c1b601b

Please sign in to comment.