Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Netty-based server in observability examples #3224

Merged
merged 3 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package sttp.tapir.examples.observability

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Route
import com.timgroup.statsd.NonBlockingStatsDClientBuilder
import com.typesafe.scalalogging.StrictLogging
import io.circe.generic.auto._
import sttp.tapir._
import sttp.tapir.generic.auto._
import sttp.tapir.json.circe.jsonBody
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.server.akkahttp.{AkkaHttpServerInterpreter, AkkaHttpServerOptions}
import sttp.tapir.server.metrics.datadog.DatadogMetrics
import sttp.tapir.server.netty.{NettyFutureServer, NettyFutureServerOptions}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.io.StdIn

object DatadogMetricsExample extends App with StrictLogging {
implicit val actorSystem: ActorSystem = ActorSystem()
import actorSystem.dispatcher

case class Person(name: String)

Expand Down Expand Up @@ -46,17 +43,22 @@ object DatadogMetricsExample extends App with StrictLogging {

val datadogMetrics = DatadogMetrics.default[Future](client)

val serverOptions: AkkaHttpServerOptions =
AkkaHttpServerOptions.customiseInterceptors
val serverOptions: NettyFutureServerOptions =
NettyFutureServerOptions.customiseInterceptors
// Adds an interceptor which collects metrics by executing callbacks
.metricsInterceptor(datadogMetrics.metricsInterceptor())
.options

val routes: Route = AkkaHttpServerInterpreter(serverOptions).toRoute(personEndpoint)
val program = for {
binding <- NettyFutureServer().port(8080).addEndpoint(personEndpoint, serverOptions).start()
_ <- Future {
logger.info(s"""Server started. Try it with: curl -X POST localhost:8080/person -d '{"name": "Jacob"}'""")
logger.info(s"The metrics are sent to udp://$hostname:$port")
logger.info("Press ENTER key to exit.")
StdIn.readLine()
}
stop <- binding.stop()
} yield stop

Await.result(Http().newServerAt("localhost", 8080).bindFlow(routes), 1.minute)

logger.info(
s"Server started. POST persons under http://localhost:8080/person. The metrics sends to udp://$hostname:$port"
)
Await.result(program, Duration.Inf)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import sttp.tapir.server.netty.{NettyFutureServer, NettyFutureServerOptions}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.io.StdIn

/** This example uses a gRPC <a href="https://opentelemetry.io/docs/concepts/components/#exporters">exporter</a> to send metrics to a <a href="https://opentelemetry.io/docs/collector/">collector</a>, which by
* default is expected to be running on `localhost:4317`.
Expand Down Expand Up @@ -92,7 +93,15 @@ object OpenTelemetryMetricsExample extends App with StrictLogging {
.metricsInterceptor(openTelemetryMetrics.metricsInterceptor())
.options

Await.ready(NettyFutureServer().port(8080).addEndpoint(personEndpoint, serverOptions).start(), 1.minute)
val program = for {
binding <- NettyFutureServer().port(8080).addEndpoint(personEndpoint, serverOptions).start()
_ <- Future {
logger.info(s"""Server started. Try it with: curl -X POST localhost:8080/person -d '{"name": "Jacob"}'""")
logger.info("Press ENTER key to exit.")
StdIn.readLine()
}
stop <- binding.stop()
} yield stop

logger.info(s"""Server started. Try it with: curl -X POST localhost:8080/person -d '{"name": "Jacob"}'""")
Await.result(program, Duration.Inf)
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package sttp.tapir.examples.observability

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Route
import com.typesafe.scalalogging.StrictLogging
import io.circe.generic.auto._
import sttp.tapir._
import sttp.tapir.generic.auto._
import sttp.tapir.json.circe._
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.server.akkahttp.{AkkaHttpServerInterpreter, AkkaHttpServerOptions}
import sttp.tapir.server.metrics.prometheus.PrometheusMetrics
import sttp.tapir.server.netty.{NettyFutureServer, NettyFutureServerOptions}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.io.StdIn

object PrometheusMetricsExample extends App with StrictLogging {
implicit val actorSystem: ActorSystem = ActorSystem()
import actorSystem.dispatcher

case class Person(name: String)

Expand All @@ -32,24 +29,29 @@ object PrometheusMetricsExample extends App with StrictLogging {

val prometheusMetrics = PrometheusMetrics.default[Future]()

val serverOptions: AkkaHttpServerOptions =
AkkaHttpServerOptions.customiseInterceptors
val serverOptions: NettyFutureServerOptions =
NettyFutureServerOptions.customiseInterceptors
// Adds an interceptor which collects metrics by executing callbacks
.metricsInterceptor(prometheusMetrics.metricsInterceptor())
.options

val routes: Route =
AkkaHttpServerInterpreter(serverOptions).toRoute(
val endpoints =
List(
personEndpoint,
// Exposes GET endpoint under `metrics` path for prometheus and serializes metrics from `CollectorRegistry` to plain text response
prometheusMetrics.metricsEndpoint
)
)

Await.result(Http().newServerAt("localhost", 8080).bindFlow(routes), 1.minute)

logger.info(
"Server started. POST persons under http://localhost:8080/person and then GET metrics from http://localhost:8080/metrics"
)
val program = for {
binding <- NettyFutureServer().port(8080).addEndpoints(endpoints, serverOptions).start()
_ <- Future {
logger.info(s"""Server started. Try it with: curl -X POST localhost:8080/person -d '{"name": "Jacob"}'""")
logger.info("The metrics are available at http://localhost:8080/metrics")
logger.info("Press ENTER key to exit.")
StdIn.readLine()
}
stop <- binding.stop()
} yield stop

Await.result(program, Duration.Inf)
}
Loading