-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3213 from softwaremill/otel-example
Add OpenTelemetry metrics example
- Loading branch information
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
examples/src/main/scala/sttp/tapir/examples/observability/OpenTelemetryMetricsExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package sttp.tapir.examples.observability | ||
|
||
import com.typesafe.scalalogging.StrictLogging | ||
import io.circe.generic.auto._ | ||
import io.opentelemetry.api.OpenTelemetry | ||
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter | ||
import io.opentelemetry.sdk.OpenTelemetrySdk | ||
import io.opentelemetry.sdk.metrics.SdkMeterProvider | ||
import io.opentelemetry.sdk.metrics.`export`.PeriodicMetricReader | ||
import sttp.tapir._ | ||
import sttp.tapir.generic.auto._ | ||
import sttp.tapir.json.circe.jsonBody | ||
import sttp.tapir.server.ServerEndpoint | ||
import sttp.tapir.server.metrics.opentelemetry.OpenTelemetryMetrics | ||
import sttp.tapir.server.netty.{NettyFutureServer, NettyFutureServerOptions} | ||
|
||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.duration._ | ||
import scala.concurrent.{Await, Future} | ||
|
||
/** 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`. | ||
* | ||
* You can run a collector locally using Docker with the following command: | ||
* {{{ | ||
* docker run -p 4317:4317 otel/opentelemetry-collector:latest | ||
* }}} | ||
* | ||
* Please refer to the <a href="https://opentelemetry.io/docs/concepts/sdk-configuration/otlp-exporter-configuration/">exporter | ||
* configuration</a> if you need to use a different host/port. | ||
* | ||
* The example code requires the following dependencies: | ||
* {{{ | ||
* val openTelemetryVersion = <OpenTelemetry version> | ||
* | ||
* libraryDependencies ++= Seq( | ||
* "io.opentelemetry" % "opentelemetry-sdk" % openTelemetryVersion, | ||
* "io.opentelemetry" % "opentelemetry-sdk-metrics" % openTelemetryVersion, | ||
* "io.opentelemetry" % "opentelemetry-exporter-otlp" % openTelemetryVersion | ||
* ) | ||
* }}} | ||
* | ||
* Once this example app and the collector are running, and after you send some requests to the `/person` endpoint, you should start seeing | ||
* the metrics in the collector logs (look for `InstrumentationScope tapir 1.0.0`), e.g.: | ||
* {{{ | ||
* InstrumentationScope tapir 1.0.0 | ||
* Metric #0 | ||
* Descriptor: | ||
* -> Name: request_active | ||
* -> Description: Active HTTP requests | ||
* -> Unit: 1 | ||
* -> DataType: Sum | ||
* -> IsMonotonic: false | ||
* -> AggregationTemporality: Cumulative | ||
* | ||
* ... | ||
* }}} | ||
*/ | ||
object OpenTelemetryMetricsExample extends App with StrictLogging { | ||
|
||
case class Person(name: String) | ||
|
||
// Simple endpoint returning 200 or 400 response with string body | ||
val personEndpoint: ServerEndpoint[Any, Future] = | ||
endpoint.post | ||
.in("person") | ||
.in(jsonBody[Person]) | ||
.out(stringBody) | ||
.errorOut(stringBody) | ||
.serverLogic { p => | ||
Thread.sleep(3000) | ||
Future.successful(Either.cond(p.name == "Jacob", "Welcome", "Unauthorized")) | ||
} | ||
|
||
// An exporter that sends metrics to a collector over gRPC | ||
val grpcExporter = OtlpGrpcMetricExporter.builder().build() | ||
|
||
// A metric reader that exports using the gRPC exporter | ||
val metricReader: PeriodicMetricReader = PeriodicMetricReader.builder(grpcExporter).build() | ||
|
||
// A meter registry whose meters are read by the above reader | ||
val meterProvider: SdkMeterProvider = SdkMeterProvider.builder().registerMetricReader(metricReader).build() | ||
|
||
// An instance of OpenTelemetry using the above meter registry | ||
val otel: OpenTelemetry = OpenTelemetrySdk.builder().setMeterProvider(meterProvider).build() | ||
|
||
val openTelemetryMetrics = OpenTelemetryMetrics.default[Future](otel) | ||
|
||
val serverOptions: NettyFutureServerOptions = | ||
NettyFutureServerOptions.customiseInterceptors | ||
// Adds an interceptor which collects metrics by executing callbacks | ||
.metricsInterceptor(openTelemetryMetrics.metricsInterceptor()) | ||
.options | ||
|
||
Await.ready(NettyFutureServer().port(8080).addEndpoint(personEndpoint, serverOptions).start(), 1.minute) | ||
|
||
logger.info(s"""Server started. Try it with: curl -X POST localhost:8080/person -d '{"name": "Jacob"}'""") | ||
} |