Skip to content

Commit

Permalink
Added grpc healthcheck by default (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaypj authored Apr 3, 2024
1 parent ed963fa commit 69bfb53
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
2 changes: 2 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ dependencies {
implementation 'com.github.wnameless:json-flattener:0.6.0'
implementation 'io.dropwizard.metrics5:metrics-jvm:5.0.0'
implementation 'io.dropwizard.metrics5:metrics-jmx:5.0.0'
implementation rootProject.subprojects.ext.libraries.grpc_stub
implementation rootProject.subprojects.ext.libraries.grpc_services

testImplementation libraries.junit4
testImplementation libraries.assertj
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.flipkart.gjex.core.healthcheck;

import io.grpc.health.v1.HealthCheckRequest;
import io.grpc.health.v1.HealthCheckResponse;
import io.grpc.health.v1.HealthGrpc;
import io.grpc.stub.StreamObserver;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

/**
* Default GRPC HealthCheck Service
* @author ajay.jalgaonkar
*/

@Singleton
@Named("GrpcHealthCheckService")
public class GrpcHealthCheckService extends HealthGrpc.HealthImplBase {
private RotationManagementBasedHealthCheck rotationManagementBasedHealthCheck;

@Inject
public GrpcHealthCheckService(RotationManagementBasedHealthCheck rotationManagementBasedHealthCheck){
this.rotationManagementBasedHealthCheck = rotationManagementBasedHealthCheck;
}

@Override
public void check(HealthCheckRequest request,
StreamObserver<HealthCheckResponse> responseObserver) {
HealthCheckResponse.Builder builder = HealthCheckResponse.newBuilder();
if (rotationManagementBasedHealthCheck.inRotation()) {
builder.setStatus(HealthCheckResponse.ServingStatus.SERVING);
} else {
builder.setStatus(HealthCheckResponse.ServingStatus.NOT_SERVING);
}
responseObserver.onNext(builder.build());
responseObserver.onCompleted();
}
}
8 changes: 7 additions & 1 deletion guice/src/main/java/com/flipkart/gjex/guice/GuiceBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.flipkart.gjex.core.job.ScheduledJob;
import com.flipkart.gjex.grpc.service.ScheduledJobManager;
import io.grpc.health.v1.HealthGrpc;
import org.glassfish.jersey.server.ResourceConfig;

import io.dropwizard.metrics5.health.HealthCheck;
Expand Down Expand Up @@ -138,8 +139,13 @@ public void run(T configuration, U configMap, Environment environment) {
setEnvironment(configuration, environment); // NOTE
GrpcServer grpcServer = baseInjector.getInstance(GrpcServer.class);

List<BindableService> bindableServices = new ArrayList<>();
// Add all Grpc Services to the Grpc Server
List<BindableService> bindableServices = getInstances(baseInjector, BindableService.class);
bindableServices.addAll(getInstances(baseInjector, BindableService.class));

// Add all Grpc Health Check Services to the Grpc Server
bindableServices.addAll(getInstances(baseInjector, HealthGrpc.HealthImplBase.class));

grpcServer.registerServices(bindableServices);

// Add all Grpc Filters to the Grpc Server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.flipkart.gjex.guice.module;

import com.flipkart.gjex.core.healthcheck.GrpcHealthCheckService;
import com.flipkart.gjex.core.service.Service;
import com.flipkart.gjex.grpc.interceptor.FilterInterceptor;
import com.flipkart.gjex.grpc.interceptor.StatusMetricInterceptor;
Expand All @@ -26,6 +27,8 @@
import com.flipkart.gjex.grpc.service.ScheduledJobManager;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
import io.grpc.BindableService;
import io.grpc.health.v1.HealthGrpc;

/**
* <code>ServerModule</code> is a Guice {@link AbstractModule} implementation used for configuring the Grpc Server and Dashboard server.
Expand All @@ -41,6 +44,7 @@ protected void configure() {
bind(TracingInterceptor.class).annotatedWith(Names.named("TracingInterceptor")).to(TracingInterceptor.class);
bind(StatusMetricInterceptor.class).annotatedWith(Names.named("StatusMetricInterceptor")).to(StatusMetricInterceptor.class);
bind(Service.class).annotatedWith(Names.named("GrpcServer")).to(GrpcServer.class);
bind(HealthGrpc.HealthImplBase.class).annotatedWith(Names.named("GrpcHealthCheckService")).to(GrpcHealthCheckService.class);
bind(Service.class).annotatedWith(Names.named("DashboardServer")).to(DashboardServer.class);
bind(Service.class).annotatedWith(Names.named("APIServer")).to(ApiServer.class);
bind(Service.class).annotatedWith(Names.named("ScheduleJobManager")).to(ScheduledJobManager.class);
Expand Down

0 comments on commit 69bfb53

Please sign in to comment.