11package org .hypertrace .gateway .service ;
22
33import com .google .common .base .Preconditions ;
4+ import com .google .common .collect .ImmutableMap ;
45import com .google .protobuf .ServiceException ;
56import com .typesafe .config .Config ;
67import io .grpc .ManagedChannel ;
78import io .grpc .ManagedChannelBuilder ;
89import io .grpc .stub .StreamObserver ;
10+ import io .micrometer .core .instrument .Counter ;
911import java .util .Optional ;
1012import java .util .concurrent .ExecutorService ;
1113import org .apache .commons .lang3 .StringUtils ;
1214import org .hypertrace .core .attribute .service .client .AttributeServiceClient ;
1315import org .hypertrace .core .attribute .service .client .config .AttributeServiceClientConfig ;
1416import org .hypertrace .core .query .service .client .QueryServiceClient ;
1517import org .hypertrace .core .query .service .client .QueryServiceConfig ;
18+ import org .hypertrace .core .serviceframework .metrics .PlatformMetricsRegistry ;
1619import org .hypertrace .entity .query .service .client .EntityQueryServiceClient ;
1720import org .hypertrace .entity .service .client .config .EntityServiceClientConfig ;
1821import org .hypertrace .gateway .service .baseline .BaselineService ;
@@ -61,6 +64,11 @@ public class GatewayServiceImpl extends GatewayServiceGrpc.GatewayServiceImplBas
6164 private static final String REQUEST_TIMEOUT_CONFIG_KEY = "request.timeout" ;
6265 private static final int DEFAULT_REQUEST_TIMEOUT_MILLIS = 10000 ;
6366
67+ private Counter requestStatusErrorCounter ;
68+ private Counter requestStatusSuccessCounter ;
69+ private static final String SERVICE_REQUESTS_STATUS_COUNTER =
70+ "hypertrace.gateway.service.requests.status" ;
71+
6472 private final TracesService traceService ;
6573 private final SpanService spanService ;
6674 private final EntityService entityService ;
@@ -134,6 +142,16 @@ public GatewayServiceImpl(Config appConfig) {
134142 entityIdColumnsConfigs );
135143 this .logEventsService =
136144 new LogEventsService (queryServiceClient , qsRequestTimeout , attributeMetadataProvider );
145+ initMetrics ();
146+ }
147+
148+ private void initMetrics () {
149+ requestStatusErrorCounter =
150+ PlatformMetricsRegistry .registerCounter (
151+ SERVICE_REQUESTS_STATUS_COUNTER , ImmutableMap .of ("error" , "true" ));
152+ requestStatusSuccessCounter =
153+ PlatformMetricsRegistry .registerCounter (
154+ SERVICE_REQUESTS_STATUS_COUNTER , ImmutableMap .of ("error" , "false" ));
137155 }
138156
139157 private static int getRequestTimeoutMillis (Config config ) {
@@ -152,6 +170,7 @@ public void getTraces(
152170 Optional <String > tenantId =
153171 org .hypertrace .core .grpcutils .context .RequestContext .CURRENT .get ().getTenantId ();
154172 if (tenantId .isEmpty ()) {
173+ requestStatusErrorCounter .increment ();
155174 responseObserver .onError (new ServiceException ("Tenant id is missing in the request." ));
156175 return ;
157176 }
@@ -167,8 +186,10 @@ public void getTraces(
167186 TracesResponse response = traceService .getTracesByFilter (requestContext , request );
168187 responseObserver .onNext (response );
169188 responseObserver .onCompleted ();
189+ requestStatusSuccessCounter .increment ();
170190 } catch (Exception e ) {
171191 LOG .error ("Error while handling traces request: {}" , request , e );
192+ requestStatusErrorCounter .increment ();
172193 responseObserver .onError (e );
173194 }
174195 }
@@ -181,6 +202,7 @@ public void getSpans(
181202 Optional <String > tenantId =
182203 org .hypertrace .core .grpcutils .context .RequestContext .CURRENT .get ().getTenantId ();
183204 if (tenantId .isEmpty ()) {
205+ requestStatusErrorCounter .increment ();
184206 responseObserver .onError (new ServiceException ("Tenant id is missing in the request." ));
185207 return ;
186208 }
@@ -195,8 +217,10 @@ public void getSpans(
195217 SpansResponse response = spanService .getSpansByFilter (context , request );
196218 responseObserver .onNext (response );
197219 responseObserver .onCompleted ();
220+ requestStatusSuccessCounter .increment ();
198221 } catch (Exception e ) {
199222 LOG .error ("Error while handling spans request: {}" , request , e );
223+ requestStatusErrorCounter .increment ();
200224 responseObserver .onError (e );
201225 }
202226 }
@@ -211,6 +235,7 @@ public void getEntities(
211235 Optional <String > tenantId =
212236 org .hypertrace .core .grpcutils .context .RequestContext .CURRENT .get ().getTenantId ();
213237 if (tenantId .isEmpty ()) {
238+ requestStatusErrorCounter .increment ();
214239 responseObserver .onError (new ServiceException ("Tenant id is missing in the request." ));
215240 return ;
216241 }
@@ -241,8 +266,10 @@ public void getEntities(
241266
242267 responseObserver .onNext (response );
243268 responseObserver .onCompleted ();
269+ requestStatusSuccessCounter .increment ();
244270 } catch (Exception e ) {
245271 LOG .error ("Error while handling entities request: {}." , request , e );
272+ requestStatusErrorCounter .increment ();
246273 responseObserver .onError (e );
247274 }
248275 }
@@ -257,6 +284,7 @@ public void updateEntity(
257284 Optional <String > tenantId =
258285 org .hypertrace .core .grpcutils .context .RequestContext .CURRENT .get ().getTenantId ();
259286 if (tenantId .isEmpty ()) {
287+ requestStatusErrorCounter .increment ();
260288 responseObserver .onError (new ServiceException ("Tenant id is missing in the request." ));
261289 return ;
262290 }
@@ -275,8 +303,10 @@ public void updateEntity(
275303 }
276304 responseObserver .onNext (response );
277305 responseObserver .onCompleted ();
306+ requestStatusSuccessCounter .increment ();
278307 } catch (Exception e ) {
279308 LOG .error ("Error while handling UpdateEntityRequest: {}." , request , e );
309+ requestStatusErrorCounter .increment ();
280310 responseObserver .onError (e );
281311 }
282312 }
@@ -305,8 +335,10 @@ public void bulkUpdateEntities(
305335 LOG .debug ("Received response: {}" , response );
306336 responseObserver .onNext (response );
307337 responseObserver .onCompleted ();
338+ requestStatusSuccessCounter .increment ();
308339 } catch (Exception e ) {
309340 LOG .error ("Error while handling bulkUpdateEntities: {}." , request , e );
341+ requestStatusErrorCounter .increment ();
310342 responseObserver .onError (e );
311343 }
312344 }
@@ -317,6 +349,7 @@ public void getBaselineForEntities(
317349 Optional <String > tenantId =
318350 org .hypertrace .core .grpcutils .context .RequestContext .CURRENT .get ().getTenantId ();
319351 if (tenantId .isEmpty ()) {
352+ requestStatusErrorCounter .increment ();
320353 responseObserver .onError (new ServiceException ("Tenant id is missing in the request." ));
321354 return ;
322355 }
@@ -334,8 +367,10 @@ public void getBaselineForEntities(
334367
335368 responseObserver .onNext (response );
336369 responseObserver .onCompleted ();
370+ requestStatusSuccessCounter .increment ();
337371 } catch (Exception e ) {
338372 LOG .error ("Error while handling entities request: {}." , request , e );
373+ requestStatusErrorCounter .increment ();
339374 responseObserver .onError (e );
340375 }
341376 }
@@ -345,6 +380,7 @@ public void explore(ExploreRequest request, StreamObserver<ExploreResponse> resp
345380 Optional <String > tenantId =
346381 org .hypertrace .core .grpcutils .context .RequestContext .CURRENT .get ().getTenantId ();
347382 if (tenantId .isEmpty ()) {
383+ requestStatusErrorCounter .increment ();
348384 responseObserver .onError (new ServiceException ("Tenant id is missing in the request." ));
349385 return ;
350386 }
@@ -359,8 +395,10 @@ public void explore(ExploreRequest request, StreamObserver<ExploreResponse> resp
359395 .getRequestHeaders ());
360396 responseObserver .onNext (response );
361397 responseObserver .onCompleted ();
398+ requestStatusSuccessCounter .increment ();
362399 } catch (Exception e ) {
363400 LOG .error ("Error while handling explore request: {}" , request , e );
401+ requestStatusErrorCounter .increment ();
364402 responseObserver .onError (e );
365403 }
366404 }
@@ -371,6 +409,7 @@ public void getLogEvents(
371409 Optional <String > tenantId =
372410 org .hypertrace .core .grpcutils .context .RequestContext .CURRENT .get ().getTenantId ();
373411 if (tenantId .isEmpty ()) {
412+ requestStatusErrorCounter .increment ();
374413 responseObserver .onError (new ServiceException ("Tenant id is missing in the request." ));
375414 return ;
376415 }
@@ -385,8 +424,10 @@ public void getLogEvents(
385424 LogEventsResponse response = logEventsService .getLogEventsByFilter (context , request );
386425 responseObserver .onNext (response );
387426 responseObserver .onCompleted ();
427+ requestStatusSuccessCounter .increment ();
388428 } catch (Exception e ) {
389429 LOG .error ("Error while handling logEvents request: {}" , request , e );
430+ requestStatusErrorCounter .increment ();
390431 responseObserver .onError (e );
391432 }
392433 }
0 commit comments