Skip to content

Commit

Permalink
Fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Dec 10, 2024
1 parent 10f15d0 commit f6f239f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ val zio2InteropRsVersion = "2.0.2"

val oxVersion = "0.5.1"
val sttpModelVersion = "1.7.11"
val sttpSharedVersion = "1.4.1"
val sttpSharedVersion = "1.4.2"

val logback = "ch.qos.logback" % "logback-classic" % "1.5.12"

Expand Down
9 changes: 7 additions & 2 deletions docs/backends/wrappers/custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ Below is an example on how to implement a backend wrapper, which sends
metrics for completed requests and wraps any `Future`-based backend:

```scala mdoc:compile-only
import sttp.attributes.AttributeKey
import sttp.capabilities.Effect
import sttp.client4._
import sttp.client4.akkahttp._
import sttp.client4.wrappers.DelegateBackend
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util._

// the metrics infrastructure
trait MetricsServer {
def reportDuration(name: String, duration: Long): Unit
Expand All @@ -58,6 +60,9 @@ class CloudMetricsServer extends MetricsServer {
override def reportDuration(name: String, duration: Long): Unit = ???
}

case class MetricPrefix(prefix: String)
val MetricPrefixAttributeKey = AttributeKey[MetricPrefix]

// the backend wrapper
abstract class MetricWrapper[P](delegate: GenericBackend[Future, P],
metrics: MetricsServer)
Expand All @@ -67,7 +72,7 @@ abstract class MetricWrapper[P](delegate: GenericBackend[Future, P],
val start = System.currentTimeMillis()

def report(metricSuffix: String): Unit = {
val metricPrefix = request.tag("metric").getOrElse("?")
val metricPrefix = request.attribute(MetricPrefixAttributeKey).getOrElse(MetricPrefix("?"))
val end = System.currentTimeMillis()
metrics.reportDuration(metricPrefix + "-" + metricSuffix, end - start)
}
Expand All @@ -93,7 +98,7 @@ val backend = MetricWrapper(AkkaHttpBackend(), new CloudMetricsServer())

basicRequest
.get(uri"http://company.com/api/service1")
.tag("metric", "service1")
.attribute(MetricPrefixAttributeKey, MetricPrefix("service1"))
.send(backend)
```

Expand Down

0 comments on commit f6f239f

Please sign in to comment.