-
-
Notifications
You must be signed in to change notification settings - Fork 49
TracedHandler should use an existing Local[F, Span[F]] #591
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
Open
bpholt
wants to merge
4
commits into
typelevel:main
Choose a base branch
from
Dwolla:traced-handler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a726335
Update cats-effect to 3.6.3
bpholt 9eb9b89
add TracedHandler.local when you already have a Local[F, Span[F]] for…
bpholt 4c217bd
use local semantics in TracedHandler and deprecate the explicit use o…
bpholt 6bb17fb
pull common implementation of TracedHandler into shared TracedHandler…
bpholt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
87 changes: 87 additions & 0 deletions
87
examples/shared/src/main/scala-3/feral/examples/Http4sHandler.scala
This file contains hidden or 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,87 @@ | ||
| /* | ||
| * Copyright 2021 Typelevel | ||
| * | ||
| * 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 feral.examples | ||
|
|
||
| import cats.effect.std.Random | ||
| import cats.effect.{Trace as _, *} | ||
| import cats.mtl.* | ||
| import feral.lambda.* | ||
| import feral.lambda.events.* | ||
| import feral.lambda.http4s.* | ||
| import natchez.* | ||
| import natchez.http4s.NatchezMiddleware | ||
| import natchez.xray.XRay | ||
| import org.http4s.HttpApp | ||
| import org.http4s.HttpRoutes | ||
| import org.http4s.client.Client | ||
| import org.http4s.dsl.Http4sDsl | ||
| import org.http4s.ember.client.EmberClientBuilder | ||
| import org.http4s.syntax.all.* | ||
|
|
||
| /** | ||
| * For a gentle introduction, please look at the `KinesisLambda` first which uses | ||
| * `IOLambda.Simple`. | ||
| * | ||
| * The `IOLambda` uses a slightly more complicated encoding by introducing an effect | ||
| * `Invocation[F]` which provides access to the event and context in `F`. This allows you to | ||
| * compose your handler as a stack of "middlewares", making it easy to e.g. add tracing to your | ||
| * Lambda. | ||
| */ | ||
| object http4sHandler | ||
| extends IOLambda[ApiGatewayProxyEventV2, ApiGatewayProxyStructuredResultV2]: | ||
|
|
||
| private type Handler = | ||
| Invocation[IO, ApiGatewayProxyEventV2] => IO[Option[ApiGatewayProxyStructuredResultV2]] | ||
|
|
||
| /** | ||
| * Actually, this is a `Resource` that builds your handler. The handler is acquired exactly | ||
| * once when your Lambda starts and is permanently installed to process all incoming events. | ||
| * | ||
| * The handler itself is a program expressed as `IO[Option[Result]]`, which is run every time | ||
| * that your Lambda is triggered. This may seem counter-intuitive at first: where does the | ||
| * event come from? Because accessing the event via `Invocation` is now also an effect in | ||
| * `IO`, it becomes a step in your program. | ||
| */ | ||
| def handler: Resource[IO, Handler] = | ||
| for | ||
| entrypoint <- Resource | ||
| .eval(Random.scalaUtilRandom[IO]) | ||
| .flatMap(implicit r => XRay.entryPoint[IO]()) | ||
| client <- EmberClientBuilder.default[IO].build | ||
| given Local[IO, Span[IO]] <- IO.local(Span.noop[IO]).toResource | ||
| yield implicit inv => // the Invocation provides access to the event and context | ||
|
|
||
| // a middleware to add tracing to any handler | ||
| // it extracts the kernel from the event and adds tags derived from the context | ||
| TracedHandler(entrypoint): | ||
| val tracedClient = NatchezMiddleware.client(client) | ||
|
|
||
| // a "middleware" that converts an HttpApp into a ApiGatewayProxyHandler | ||
| ApiGatewayProxyHandlerV2(myApp[IO](tracedClient)) | ||
|
|
||
| /** | ||
| * Nothing special about this method, including its existence, just an example :) | ||
| */ | ||
| def myApp[F[_]: Concurrent: Trace](client: Client[F]): HttpApp[F] = | ||
| implicit val dsl = Http4sDsl[F] | ||
| import dsl.* | ||
|
|
||
| val routes = HttpRoutes.of[F]: | ||
| case GET -> Root / "foo" => Ok("bar") | ||
| case GET -> Root / "joke" => Ok(client.expect[String](uri"icanhazdadjoke.com")) | ||
|
|
||
| NatchezMiddleware.server(routes).orNotFound |
30 changes: 30 additions & 0 deletions
30
lambda/shared/src/main/scala-2/feral/lambda/TracedHandlerPlatform.scala
This file contains hidden or 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,30 @@ | ||
| /* | ||
| * Copyright 2021 Typelevel | ||
| * | ||
| * 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 feral.lambda | ||
|
|
||
| import cats.effect.{Trace => _, _} | ||
| import cats.mtl.Local | ||
| import natchez._ | ||
|
|
||
| trait TracedHandlerPlatform { | ||
| def apply[F[_]: MonadCancelThrow, Event, Result](entryPoint: EntryPoint[F])( | ||
| handler: Trace[F] => F[Option[Result]])( | ||
| implicit inv: Invocation[F, Event], | ||
| KS: KernelSource[Event], | ||
| L: Local[F, Span[F]]): F[Option[Result]] = | ||
| TracedHandlerImpl[F, Event, Result](entryPoint)(handler) | ||
| } |
29 changes: 29 additions & 0 deletions
29
lambda/shared/src/main/scala-3/feral/lambda/TracedHandlerPlatform.scala
This file contains hidden or 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,29 @@ | ||
| /* | ||
| * Copyright 2021 Typelevel | ||
| * | ||
| * 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 feral.lambda | ||
|
|
||
| import cats.effect.{Trace as _, *} | ||
| import cats.mtl.Local | ||
| import natchez.* | ||
|
|
||
| trait TracedHandlerPlatform: | ||
| def apply[F[_]: MonadCancelThrow, Event, Result](entryPoint: EntryPoint[F])( | ||
| handler: Trace[F] ?=> F[Option[Result]])( | ||
| using Invocation[F, Event], | ||
| KernelSource[Event], | ||
| Local[F, Span[F]]): F[Option[Result]] = | ||
| TracedHandlerImpl[F, Event, Result](entryPoint) { implicit t => handler } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Splitting the
applymethod into Scala version-specific files lets us use a context function here, which improves the experience on Scala 3.