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

Tracing doodles #50

Closed
wants to merge 10 commits into from
25 changes: 25 additions & 0 deletions lambda-natchez/src/main/scala/feral/lambda/natchez/Tags.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.natchez

import natchez.TraceValue

object Tags {
private[this] val prefix = "aws"
def arn(s: String): (String, TraceValue) = s"$prefix.arn" -> s
def requestId(s: String): (String, TraceValue) = s"$prefix.requestId" -> s
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@

package feral.lambda.natchez

import cats.Monad
import cats.effect.kernel.MonadCancelThrow
import cats.syntax.all._
import feral.lambda.Lambda
import natchez.EntryPoint
import natchez.Trace

object TracedLambda {
def apply[F[_]: MonadCancelThrow, G[_], Event: HasKernel, Result](entryPoint: EntryPoint[F])(
lambda: Trace[G] => Lambda[G, Event, Result])(
implicit lift: LiftTrace[F, G]): Lambda[F, Event, Result] = { (event, context) =>
def apply[F[_]: MonadCancelThrow, G[_]: Monad, Event: HasKernel, Result](
entryPoint: EntryPoint[F])(
lambda: Trace[G] => Lambda[G, Event, Result]
)(implicit lift: LiftTrace[F, G]): Lambda[F, Event, Result] = { (event, context) =>
val kernel = HasKernel[Event].extract(event)
entryPoint.continueOrElseRoot(context.functionName, kernel).use { span =>
lift.run(span)(lambda(_)(event, context.mapK(lift.liftK)))
lift.run(span) { trace =>
trace.put(
Tags.arn(context.invokedFunctionArn),
Tags.requestId(context.awsRequestId)
) >> lambda(trace)(event, context.mapK(lift.liftK))
bpholt marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}