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

Do not report on path label mapper not found #2306

Closed
wants to merge 6 commits into from
Closed
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
Expand Up @@ -55,7 +55,6 @@ private[zio] trait Metrics { self: RequestHandlerMiddlewares =>
val requestsTotal: Counter[RuntimeFlags] = Metric.counterInt(totalRequestsName)
val concurrentRequests: Gauge[Double] = Metric.gauge(concurrentRequestsName)
val requestDuration: Histogram[Double] = Metric.histogram(requestDurationName, requestDurationBoundaries)
val status404: Set[MetricLabel] = Set(MetricLabel("status", "404"))
val status500: Set[MetricLabel] = Set(MetricLabel("status", "500"))
val nanosToSeconds: Double = 1e9d

Expand Down Expand Up @@ -93,29 +92,27 @@ private[zio] trait Metrics { self: RequestHandlerMiddlewares =>
http: HttpApp[R1, Err1],
)(implicit trace: Trace): HttpApp[R1, Err1] =
Http.fromOptionalHandlerZIO[Request] { req =>
val requestLabels = labelsForRequest(req)

for {
start <- Clock.nanoTime
_ <- concurrentRequests.tagged(requestLabels).increment
optionalHandler <- http.runHandler(req)
handler <-
optionalHandler match {
case Some(handler) =>
ZIO.succeed {
handler.onExit { exit =>
val labels =
requestLabels ++ exit.foldExit(
cause => cause.failureOption.fold(status500)(statusLabelForError),
labelsForResponse,
)
val requestLabels = labelsForRequest(req)

concurrentRequests.tagged(requestLabels).increment *>
ZIO.succeed {
handler.onExit { exit =>
val labels =
requestLabels ++ exit.foldExit(
cause => cause.failureOption.fold(status500)(statusLabelForError),
labelsForResponse,
)

report(start, requestLabels, labels)
report(start, requestLabels, labels)
}
}
}
case None =>
report(start, requestLabels, requestLabels ++ status404) *>
ZIO.fail(None)
case None => ZIO.fail(None)
}
} yield handler
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ object MetricsSpec extends ZIOSpecDefault with HttpAppTestExtensions {
test("http_requests_total & http_errors_total") {
val app = Http
.collectHandler[Request] {
case Method.GET -> Root / "ok" => Handler.ok
case Method.GET -> Root / "error" => Handler.error(HttpError.InternalServerError())
case Method.GET -> Root / "fail" => Handler.fail(Response.status(Status.Forbidden))
case Method.GET -> Root / "defect" => Handler.die(new Throwable("boom"))
case Method.GET -> Root / "ok" => Handler.ok
case Method.GET -> Root / "error" => Handler.error(HttpError.InternalServerError())
case Method.GET -> Root / "fail" => Handler.fail(Response.status(Status.Forbidden))
case Method.GET -> Root / "defect" => Handler.die(new Throwable("boom"))
case Method.GET -> Root / "not-found" => Handler.notFound
} @@ metrics(
extraLabels = Set(MetricLabel("test", "http_requests_total & http_errors_total")),
)
Expand All @@ -46,12 +47,12 @@ object MetricsSpec extends ZIOSpecDefault with HttpAppTestExtensions {
val totalNotFound = total.tagged("path", "/not-found").tagged("method", "GET").tagged("status", "404")

for {
_ <- app.runZIO(Request.get(url = URL(Root / "ok")))
_ <- app.runZIO(Request.get(url = URL(Root / "error")))
_ <- app.runZIO(Request.get(url = URL(Root / "fail"))).ignore
_ <- app.runZIO(Request.get(url = URL(Root / "defect"))).catchAllDefect(_ => ZIO.unit)
_ <- app.runZIO(Request.get(url = URL(Root / "not-found"))).ignore.catchAllDefect(_ => ZIO.unit)
totalOkCount <- totalOk.value
_ <- app.runZIO(Request.get(url = URL(Root / "ok")))
_ <- app.runZIO(Request.get(url = URL(Root / "error")))
_ <- app.runZIO(Request.get(url = URL(Root / "fail"))).ignore
_ <- app.runZIO(Request.get(url = URL(Root / "defect"))).catchAllDefect(_ => ZIO.unit)
_ <- app.runZIO(Request.get(url = URL(Root / "not-found")))
totalOkCount <- totalOk.value
totalErrorsCount <- totalErrors.value
totalFailsCount <- totalFails.value
totalDefectsCount <- totalDefects.value
Expand Down