Akka HTTP API

The Cinnamon Akka HTTP API enables users to programmatically set the name of the metrics for both client requests and server endpoints. Below are two examples that illustrates this feature.

Server endpoint naming

There are two ways to name a server endpoint programmatically. Either by using an Akka HTTP directive or a naming wrapper function that needs to be applied as close to the response creation as possible.

Akka HTTP directive

The naming directive is available both as the Scala EndpointNameDirective and the Java EndpointNameDirective:

Scala
val route: Route =
  pathPrefix("customer") {
    path(LongNumber) { customerId =>
      complete(s"Customer id $customerId")
    } ~ path("account" / LongNumber) { accountId =>
      endpointName("CustomerAccount") {
        complete(s"Customer account id: $accountId")
      }
    }
  }
Java
Route theRoute =
    concat(
        pathPrefix(
            "customer",
            () ->
                concat(
                    path(longSegment(), customerId -> complete("Customer id " + customerId)),
                    endpointName(
                        "CustomerAccount",
                        () ->
                            path(
                                segment("account").slash(longSegment()),
                                accountId -> complete("Customer account id " + accountId))))));

In the example above the first path customer/<LongNumber> will use the default naming provided by Cinnamon. The second path customer/account/<LongNumber>, on the other hand, uses endpointName to set the name to “CustomerAccount”.

Akka HTTP naming block

The naming wrapper function is available in both the scala Endpoint object and the Java Endpoint class:

Scala
val route: Route =
  pathPrefix("customer") {
    path(LongNumber) { customerId =>
      complete(s"Customer id $customerId")
    } ~ path("account" / LongNumber) { accountId =>
      Endpoint.withName("CustomerAccount") {
        complete(s"Customer account id: $accountId")
      }
    }
  }
Java
Route theRoute =
    concat(
        pathPrefix(
            "customer",
            () ->
                concat(
                    path(longSegment(), customerId -> complete("Customer id " + customerId)),
                    path(
                        segment("account").slash(longSegment()),
                        accountId ->
                            Endpoint.withName(
                                "CustomerAccount",
                                () -> complete("Customer account id " + accountId))))));

In the example above the first path customer/<LongNumber> will use the default naming provided by Cinnamon. The second path customer/account/<LongNumber>, on the other hand, uses Endpoint.withName to set the name to “CustomerAccount”.

Client request naming

To name a request in a client use the Scala Request object or the Java Request class:

Scala
Request.withName("AccountService") {
  Http().singleRequest(HttpRequest(uri = someURI))
}
Java
CompletionStage<HttpResponse> response =
    Request.withName(
        "AccountService",
        () ->
            Http.get(system)
                .singleRequest(
                    HttpRequest.create("http://localhost:8080/customer/account/123")));

If the client metrics configuration is set to monitor the request path someURI then its associated metrics will be named “AccountService”.