Skip to content

Commit 6bb17fb

Browse files
committed
pull common implementation of TracedHandler into shared TracedHandlerImpl
this allows for Scala version-specific interfaces but without duplicating the implementation logic
1 parent 4c217bd commit 6bb17fb

File tree

3 files changed

+25
-31
lines changed

3 files changed

+25
-31
lines changed

lambda/shared/src/main/scala-2/feral/lambda/TracedHandlerPlatform.scala

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,13 @@ package feral.lambda
1818

1919
import cats.effect.{Trace => _, _}
2020
import cats.mtl.Local
21-
import cats.syntax.all._
2221
import natchez._
23-
import natchez.mtl._
2422

2523
trait TracedHandlerPlatform {
2624
def apply[F[_]: MonadCancelThrow, Event, Result](entryPoint: EntryPoint[F])(
2725
handler: Trace[F] => F[Option[Result]])(
2826
implicit inv: Invocation[F, Event],
2927
KS: KernelSource[Event],
3028
L: Local[F, Span[F]]): F[Option[Result]] =
31-
for {
32-
event <- inv.event
33-
context <- inv.context
34-
kernel = KS.extract(event)
35-
result <- entryPoint.continueOrElseRoot(context.functionName, kernel).use {
36-
Local[F, Span[F]].scope {
37-
Trace[F].put(
38-
AwsTags.arn(context.invokedFunctionArn),
39-
AwsTags.requestId(context.awsRequestId)
40-
) >> handler(Trace[F])
41-
}
42-
}
43-
} yield result
44-
29+
TracedHandlerImpl[F, Event, Result](entryPoint)(handler)
4530
}

lambda/shared/src/main/scala-3/feral/lambda/TracedHandlerPlatform.scala

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,12 @@ package feral.lambda
1818

1919
import cats.effect.{Trace as _, *}
2020
import cats.mtl.Local
21-
import cats.syntax.all.*
2221
import natchez.*
23-
import natchez.mtl.*
2422

2523
trait TracedHandlerPlatform:
2624
def apply[F[_]: MonadCancelThrow, Event, Result](entryPoint: EntryPoint[F])(
2725
handler: Trace[F] ?=> F[Option[Result]])(
2826
using Invocation[F, Event],
2927
KernelSource[Event],
3028
Local[F, Span[F]]): F[Option[Result]] =
31-
for
32-
event <- Invocation[F, Event].event
33-
context <- Invocation[F, Event].context
34-
kernel = KernelSource[Event].extract(event)
35-
result <- entryPoint
36-
.continueOrElseRoot(context.functionName, kernel)
37-
.use:
38-
Local[F, Span[F]].scope:
39-
Trace[F].put(
40-
AwsTags.arn(context.invokedFunctionArn),
41-
AwsTags.requestId(context.awsRequestId)
42-
) >> handler
43-
yield result
29+
TracedHandlerImpl[F, Event, Result](entryPoint) { implicit t => handler }

lambda/shared/src/main/scala/feral/lambda/TracedHandler.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import cats.mtl.Local
2323
import cats.syntax.all._
2424
import fs2.compat.NotGiven
2525
import natchez._
26+
import natchez.mtl._
2627

2728
object TracedHandler extends TracedHandlerPlatform {
2829

@@ -81,3 +82,25 @@ object TracedHandler extends TracedHandlerPlatform {
8182
}
8283

8384
}
85+
86+
private[lambda] object TracedHandlerImpl {
87+
def apply[F[_]: MonadCancelThrow, Event, Result](entryPoint: EntryPoint[F])(
88+
handler: Trace[F] => F[Option[Result]])(
89+
implicit inv: Invocation[F, Event],
90+
KS: KernelSource[Event],
91+
L: Local[F, Span[F]]): F[Option[Result]] =
92+
for {
93+
event <- Invocation[F, Event].event
94+
context <- Invocation[F, Event].context
95+
kernel = KernelSource[Event].extract(event)
96+
result <- entryPoint.continueOrElseRoot(context.functionName, kernel).use {
97+
Local[F, Span[F]].scope {
98+
Trace[F].put(
99+
AwsTags.arn(context.invokedFunctionArn),
100+
AwsTags.requestId(context.awsRequestId)
101+
) >> handler(Trace[F])
102+
}
103+
}
104+
} yield result
105+
106+
}

0 commit comments

Comments
 (0)