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

To name an endpoint programmatically use the class com.lightbend.cinnamon.akka.http.scaladsl.server.Endpoint (for Scala) or com.lightbend.cinnamon.akka.http.javadsl.server.Endpoint (for Java):

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 =
        route(pathPrefix("customer", () ->
                route(path(longSegment(), customerId -> complete("Customer id " + customerId)),
                        Endpoint.withName("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 Endpoint.withName to set the name to “CustomerAccount”.

Client request naming

To name a request in a client use the class com.lightbend.cinnamon.akka.http.scaladsl.client.Request (for Scala) or com.lightbend.cinnamon.akka.http.javadsl.client.Request (for Java):

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"),
                        materializer));

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