Telemetry for unmatched Endpoints
- Dominant language
- Scala
- Stars
- 1.6k
- Forks
- 217
- PR merge metrics
- No merged PRs in 30d
Description
It would be convenient if it was possible to asses for what reason an endpoint did not match. For example: If part of your API is versioned, you might want to differentiate between a request that did not match any paths (in which case the API responds 404 or 405) and one that matched a path but the client's version was outdated (e.g., 410).
To further clarify what I'm aiming at, here is how I currently solve the problem. I use a custom endpoint that checks a header-value and sets a field in the context of the finagle request if the endpoint did not match. This value is then read in a finagle filter and a corresponding response is constructed. Below you can find the sourcecode for a possible implementation of such a version-endpoint:
```
// this endpoint should be applied after all paths.
final case class VersionEndpoint(apiVersion: Int) extends Endpoint[HNil] {
private[this] val apiVersionString = apiVersion.toString
override def apply(input: Input): Result[HNil] = input.request.headerMap.get("X-API-VERSION") match {
case Some(s) if s == apiVersionString =>
EndpointResult.Matched(input, Trace.empty, EmptyOutput)
case _ =>
input.request.ctx.update(VersionEndpoint.versionUnmatched, true)
EndpointResult.NotMatched
}
}
object VersionEndpoint {
val EmptyOutput: Rerunnable[Output[HNil]] =
new Rerunnable[Output[HNil]] {
override val run: Future[Output[HNil]] = Future.value(Output.payload(HNil))
}
val versionUnmatched: Request.Schema.Field[Boolean] = Request.Schema.newField[Boolean]
}
```
Contributor guide
Research direction
Start by tracing how Endpoint.apply produces EndpointResult.NotMatched and how unmatched requests reach the Finagle filter. Compare that flow with the VersionEndpoint example and its request-context field; done should distinguish a request matching no path from one rejected because the client's API version is outdated.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- scala
- Domain
- api, backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100