Skip to content

Commit

Permalink
Merge branch 'update/otel4s-0.5.0' into feature/otel-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
lhns committed Mar 29, 2024
2 parents 15f4ffe + 70e6cd9 commit 939036b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 148 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ val catsEffectV = "3.5.4"
val http4sV = "0.23.26"

val openTelemetryV = "1.35.0"
val otel4sV = "0.5.0-RC3"
val otel4sV = "0.5.0"

val munitCatsEffectV = "2.0.0-M4"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ object ClientMiddleware {
def withIncludeUrl(includeUrl: Request[F] => Boolean): ClientMiddlewareBuilder[F] =
copy(includeUrl = includeUrl)

def build: Client[F] => Client[F] = { (client: Client[F]) =>
def build: Client[F] => Client[F] = (client: Client[F]) =>
Client[F] { (req: Request[F]) => // Resource[F, Response[F]]

val base = request(req, allowedRequestHeaders, includeUrl) ++ additionalRequestTags(req)
Expand All @@ -123,45 +123,41 @@ object ClientMiddleware {
.build
.resource
span = res.span
traceHeaders <- Resource.eval(Tracer[F].propagate(Headers.empty))
trace = res.trace
traceHeaders <- Resource.eval(Tracer[F].propagate(Headers.empty)).mapK(trace)
newReq = req.withHeaders(traceHeaders ++ req.headers)
resp <- poll(client.run(newReq)).guaranteeCase {
case Outcome.Succeeded(fa) =>
Resource.eval(span.addAttribute(Attribute("exit.case", "succeeded"))) >>
fa.flatMap(resp =>
Resource.eval(
span.addAttributes(
response(resp, allowedResponseHeaders) ++ additionalResponseTags(resp): _*
)
)
)
case Outcome.Errored(e) =>
Resource.eval(
span.recordException(e) >>
span.addAttribute(Attribute("exit.case", "errored"))
)
case Outcome.Canceled() =>
// Canceled isn't always an error, but it generally is for http
// TODO decide if this should add error, we do for the server side.
Resource.eval(
span.addAttributes(
Attribute("exit.case", "canceled"),
Attribute("canceled", true),
)
)

resp <- poll(client.run(newReq)).guaranteeCase { outcome =>
(outcome match {
case Outcome.Succeeded(fa) =>
fa.flatMap { resp =>
Resource
.eval {
span.addAttributes(
response(resp, allowedResponseHeaders) ++ additionalResponseTags(resp): _*
)
}
.mapK(trace)
}
case Outcome.Errored(e) =>
Resource.eval(span.recordException(e)).mapK(trace)
case Outcome.Canceled() =>
// Canceled isn't always an error, but it generally is for http
// TODO: decide if this should add "error", we do for the server side.
Resource.eval(span.addAttribute(CustomAttributes.Canceled(true))).mapK(trace)
}) >> Resource.eval(span.addAttribute(CustomAttributes.exitCase(outcome))).mapK(trace)
}
// Automatically handle client processing errors. Since this is after the response,
// the error case will only get hit if the use block of the resulting resource happens,
// which is the request processing stage.
_ <- Resource.makeCase(Applicative[F].unit) {
case (_, Resource.ExitCase.Errored(e)) => span.recordException(e)
case (_, _) => Applicative[F].unit
}
_ <- Resource
.makeCase(Applicative[F].unit) {
case (_, Resource.ExitCase.Errored(e)) => span.recordException(e)
case (_, _) => Applicative[F].unit
}
.mapK(trace)
} yield resp
}
}
}
}

val ExtraAttributesKey: Key[List[Attribute[_]]] =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2023 http4s.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.http4s.otel4s.middleware

import cats.effect.kernel.Outcome
import org.typelevel.otel4s.Attribute
import org.typelevel.otel4s.AttributeKey

private object CustomAttributes {
private val ExitCase: AttributeKey[String] = AttributeKey.string("exit.case")

val Canceled: AttributeKey[Boolean] = AttributeKey.boolean("canceled")
val Error: AttributeKey[Boolean] = AttributeKey.boolean("error")

def exitCase[F[_]](outcome: Outcome[F, _, _]): Attribute[String] =
outcome match {
case Outcome.Succeeded(_) => ExitCase("succeeded")
case Outcome.Errored(_) => ExitCase("errored")
case Outcome.Canceled() => ExitCase("canceled")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ object ServerMiddleware {
f: Http[G, F]
)(implicit kt: KindTransformer[F, G]): Http[G, F] =
Kleisli { (req: Request[F]) =>
if (doNotTrace(req.requestPrelude)) f(req)
if (doNotTrace(req.requestPrelude) || !Tracer[F].meta.isEnabled) f(req)
else {
val init =
request(
Expand All @@ -150,26 +150,24 @@ object ServerMiddleware {
.build
.use { span =>
poll(f.run(req))
.guaranteeCase {
case Outcome.Succeeded(fa) =>
span.addAttribute(Attribute("exit.case", "succeeded")) >>
.guaranteeCase { outcome =>
(outcome match {
case Outcome.Succeeded(fa) =>
fa.flatMap { resp =>
val out =
response(resp, allowedResponseHeaders) ++ additionalResponseTags(resp)
span.addAttributes(out: _*)
}
case Outcome.Errored(e) =>
span.recordException(e) >>
span.addAttribute(Attribute("exit.case", "errored"))
case Outcome.Canceled() =>
span.addAttributes(
Attribute("exit.case", "canceled"),
Attribute("canceled", true),
Attribute(
"error",
true,
), // A canceled http is an error for the server. The connection got cut for some reason.
)
case Outcome.Errored(e) =>
span.recordException(e)
case Outcome.Canceled() =>
span.addAttributes(
CustomAttributes.Canceled(true),
CustomAttributes.Error(
true
), // A canceled http is an error for the server. The connection got cut for some reason.
)
}) >> span.addAttribute(CustomAttributes.exitCase(outcome))
}
}
}
Expand Down
99 changes: 0 additions & 99 deletions core/src/main/scala/org/http4s/otel4s/middleware/helpers.scala

This file was deleted.

0 comments on commit 939036b

Please sign in to comment.