Skip to content

Commit

Permalink
Improve REST endpoint details in prometheus metrics endpoint (#100)
Browse files Browse the repository at this point in the history
* Decorate endpoints for Metrics fetch

Signed-off-by: “Nithin <[email protected]>

* Introduce aspects for decorating REST endpoints for metrics publish

Signed-off-by: “Nithin <[email protected]>

---------

Signed-off-by: “Nithin <[email protected]>
Co-authored-by: “Nithin <[email protected]>
  • Loading branch information
nithin-pankaj and “Nithin authored Aug 22, 2023
1 parent da99aeb commit 766504b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hlf.java.rest.client.controller;

import hlf.java.rest.client.metrics.MetricsTrackedEndpoint;
import hlf.java.rest.client.model.ChaincodeOperations;
import hlf.java.rest.client.model.ChaincodeOperationsType;
import hlf.java.rest.client.service.ChaincodeOperationsService;
Expand Down Expand Up @@ -27,6 +28,10 @@ public class ChaincodeOperationsController {
@Autowired private ChaincodeOperationsService chaincodeOperationsService;

@PutMapping(value = "/operations")
@MetricsTrackedEndpoint(
name = "Chaincode Operations",
method = "PUT",
uri = "/chaincode/operations")
public ResponseEntity<String> performChaincodeOperation(
@RequestParam("network_name") @Validated String networkName,
@RequestParam("operations_type") @Validated ChaincodeOperationsType operationsType,
Expand All @@ -48,6 +53,7 @@ public ResponseEntity<String> performChaincodeOperation(
}

@GetMapping(value = "/sequence")
@MetricsTrackedEndpoint(name = "Get Sequence", method = "GET", uri = "/chaincode/sequence")
public ResponseEntity<String> getCurrentSequence(
@RequestParam("network_name") @Validated String networkName,
@RequestParam("chaincode_name") @Validated String chaincodeName,
Expand All @@ -62,6 +68,7 @@ public ResponseEntity<String> getCurrentSequence(
}

@GetMapping(value = "/packageId")
@MetricsTrackedEndpoint(name = "Get PackageId", method = "GET", uri = "/chaincode/packageId")
public ResponseEntity<String> getCurrentPackageId(
@RequestParam("network_name") @Validated String networkName,
@RequestParam("chaincode_name") @Validated String chaincodeName,
Expand All @@ -77,6 +84,10 @@ public ResponseEntity<String> getCurrentPackageId(
}

@GetMapping(value = "/approved-organisations")
@MetricsTrackedEndpoint(
name = "Fetch Approved Organizations",
method = "GET",
uri = "/chaincode/approved-organisations")
public ResponseEntity<Set<String>> getApprovedOrganisationListForSmartContract(
@RequestParam("network_name") @Validated String networkName,
@RequestParam("chaincode_name") String chaincodeName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hlf.java.rest.client.controller;

import hlf.java.rest.client.metrics.MetricsTrackedEndpoint;
import hlf.java.rest.client.model.ChannelOperationRequest;
import hlf.java.rest.client.model.ClientResponseModel;
import hlf.java.rest.client.service.ChannelService;
Expand All @@ -23,13 +24,15 @@ public class ChannelOperationController {
@Autowired private ChannelService channelService;

@PostMapping("/create")
@MetricsTrackedEndpoint(name = "Create Channel", method = "POST", uri = "/channel/create")
public ResponseEntity<ClientResponseModel> createChannel(
@RequestBody ChannelOperationRequest channelCreationRequest) {
ClientResponseModel response = channelService.createChannel(channelCreationRequest);
return ResponseEntity.status(response.getCode()).body(response);
}

@PostMapping("/join")
@MetricsTrackedEndpoint(name = "Join Channel", method = "POST", uri = "/channel/join")
public ResponseEntity<ClientResponseModel> joinChannel(
@RequestBody ChannelOperationRequest channelJoinRequest) {
ClientResponseModel response = channelService.joinChannel(channelJoinRequest);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hlf.java.rest.client.controller;

import hlf.java.rest.client.metrics.MetricsTrackedEndpoint;
import hlf.java.rest.client.model.ClientResponseModel;
import hlf.java.rest.client.model.EventAPIResponseModel;
import hlf.java.rest.client.model.MultiDataTransactionPayload;
Expand Down Expand Up @@ -42,6 +43,7 @@ public class FabricClientController {
* @return responseEntity ResponseEntity Transaction Response
*/
@GetMapping(value = "/write_transaction")
@MetricsTrackedEndpoint(name = "Write Transaction", method = "GET", uri = "/write_transaction")
public ResponseEntity<ClientResponseModel> postTransaction(
@RequestParam("network_name") @Validated String networkName,
@RequestParam("contract_name") @Validated String contractName,
Expand Down Expand Up @@ -69,6 +71,7 @@ public ResponseEntity<ClientResponseModel> postTransaction(
* @return responseEntity ResponseEntity Transaction Response
*/
@PostMapping(value = "/init_transaction")
@MetricsTrackedEndpoint(name = "Initialise Chaincode", method = "POST", uri = "/init_transaction")
public ResponseEntity<ClientResponseModel> initTransaction(
@RequestHeader("channel") @Validated String channelName,
@RequestHeader("chaincode") @Validated String chaincodeName,
Expand Down Expand Up @@ -103,6 +106,7 @@ public ResponseEntity<ClientResponseModel> initTransaction(
* @return responseEntity ResponseEntity Transaction Response
*/
@PostMapping(value = "/invoke_transaction")
@MetricsTrackedEndpoint(name = "Invoke Transaction", method = "POST", uri = "/invoke_transaction")
public ResponseEntity<ClientResponseModel> invokeTransaction(
@RequestHeader("channel") @Validated String channelName,
@RequestHeader("chaincode") @Validated String chaincodeName,
Expand Down Expand Up @@ -171,6 +175,7 @@ public ResponseEntity<ClientResponseModel> invokeTransaction(
* @return responseEntity ResponseEntity Transaction Response
*/
@GetMapping(value = "/query_transaction")
@MetricsTrackedEndpoint(name = "Query Transaction", method = "GET", uri = "/query_transaction")
public ResponseEntity<ClientResponseModel> getTransaction(
@RequestParam("channel") @Validated String networkName,
@RequestParam("chaincode") @Validated String contractName,
Expand Down Expand Up @@ -212,6 +217,10 @@ public ResponseEntity<ClientResponseModel> getTransaction(
* @return the EventAPIResponseModel which contain all the events
*/
@GetMapping(value = "/blocks/{block-number}/transactions/{transaction-id}/events")
@MetricsTrackedEndpoint(
name = "Query Transaction By Block Number",
method = "GET",
uri = "/blocks/{block-number}/transactions/{transaction-id}/events")
public ResponseEntity<EventAPIResponseModel> getTransactionByBlockNumber(
@PathVariable("transaction-id") String transactionId,
@PathVariable("block-number") Long blockNumber,
Expand All @@ -237,6 +246,10 @@ public ResponseEntity<EventAPIResponseModel> getTransactionByBlockNumber(
* @return responseEntity ResponseEntity Transaction Response
*/
@PostMapping(value = "/invoke_transaction/v2")
@MetricsTrackedEndpoint(
name = "Invoke Transaction V2",
method = "POST",
uri = "/invoke_transaction/v2")
public ResponseEntity<ClientResponseModel> invokeTransaction(
@RequestParam("channel") @Validated String channelName,
@RequestParam("chaincode") @Validated String chaincodeName,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package hlf.java.rest.client.metrics;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MetricsTrackedEndpoint {
String name();

String uri();

String method();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package hlf.java.rest.client.metrics;

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MetricsTrackedEndpointAspect {

@Autowired private MeterRegistry meterRegistry;

private Map<String, Boolean> trackedEndpoints = new ConcurrentHashMap<>();

@After("@annotation(metricsTrackedEndpoint)")
public void afterTimedEndpoint(
JoinPoint joinPoint, MetricsTrackedEndpoint metricsTrackedEndpoint) {

trackedEndpoints.computeIfAbsent(
metricsTrackedEndpoint.name(),
val -> {
Gauge.builder("tracked_endpoints", () -> 1L)
.tag("name", metricsTrackedEndpoint.name())
.tag("method", metricsTrackedEndpoint.method())
.tag("uri", metricsTrackedEndpoint.uri())
.register(meterRegistry);

return true;
});
}
}

0 comments on commit 766504b

Please sign in to comment.