-
Notifications
You must be signed in to change notification settings - Fork 41
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
Add lambda-otel4s module #459
base: main
Are you sure you want to change the base?
Changes from all commits
1170ca8
09d4127
375b814
7ebde29
a5506a4
a17882b
c596060
88fa15a
11be471
08122de
5bee0a3
6b80150
5489668
4ae91a8
29f8248
db56620
a737654
5d0fb2b
9abdd5e
a961320
96ce0a9
68ea923
b2fcbf2
8f06d74
4c1cbb3
2e0937b
18768c2
51aad4a
a208070
2d435fd
ad3dbae
fe22625
c8f2913
e8fbf9d
501e76a
5e05972
a47540b
81ce08d
84d303b
73ed59c
eefbc7d
b87ae6c
1010972
92dded7
7687287
d35d875
a6f8e6c
684cb50
353ba48
07d2bbe
20d7b5f
c52d901
c062551
8ebf71c
e538c3f
4231bca
29edc99
9bcbb13
ef7d883
bb2970f
a99a656
9b5d7d6
ea168f5
9cca0f0
c46d6d3
15d8feb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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.Monad | ||
import cats.effect.IO | ||
import cats.syntax.all._ | ||
import feral.lambda.INothing | ||
import feral.lambda.IOLambda | ||
import feral.lambda.Invocation | ||
import feral.lambda.events.SqsEvent | ||
import feral.lambda.events.SqsRecord | ||
import feral.lambda.otel4s._ | ||
import org.http4s.client.Client | ||
import org.http4s.ember.client.EmberClientBuilder | ||
import org.typelevel.otel4s.oteljava.OtelJava | ||
import org.typelevel.otel4s.trace.Tracer | ||
import org.typelevel.scalaccompat.annotation.unused | ||
|
||
object SqsOtelExample extends IOLambda[SqsEvent, INothing] { | ||
|
||
// Could be http4s-otel4s-middleware ClientMiddleware for example | ||
def clientMiddleware(client: Client[IO]): Client[IO] = client | ||
|
||
def handler = | ||
OtelJava.autoConfigured[IO]().map(_.tracerProvider).evalMap(_.get("tracer")).flatMap { | ||
implicit tracer: Tracer[IO] => | ||
for { | ||
client <- EmberClientBuilder.default[IO].build | ||
tracedClient = clientMiddleware(client) | ||
} yield { implicit inv: Invocation[IO, SqsEvent] => | ||
TracedHandler[IO, SqsEvent, INothing]( | ||
handleEvent[IO](tracedClient) | ||
) | ||
} | ||
} | ||
|
||
def handleEvent[F[_]: Monad: Tracer]( | ||
@unused client: Client[F] | ||
)(implicit inv: Invocation[F, SqsEvent]): F[Option[INothing]] = inv.event.flatMap { event => | ||
event | ||
.records | ||
.traverse(record => | ||
Tracer[F].span("handle-record", SqsRecordAttributes(record)).surround { | ||
handleRecord[F](record) | ||
}) | ||
.as(None) | ||
} | ||
|
||
def handleRecord[F[_]: Monad: Tracer](@unused record: SqsRecord): F[Unit] = { | ||
Tracer[F].span("some-operation").surround { | ||
Monad[F].unit | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
package feral.lambda | ||
package feral.lambda.natchez | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add Scalafix migrations for these renames? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay so I've added inputs and outputs but what's the common practice for adding the new v0.3.0 dependency, to run those tests? Do we require a 0.2->0.3 subproject and a 0.3->0.4 subproject? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see the examples in cats have subprojects per version, I will work on that later There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of more subprojects, I wonder if we can just delete / replace the old migrations with the new ones since we are doing a version bump? I would prefer not to maintain them forever esp. if Scalafix/Scalameta may issue deprecations in the future 🤔 These migrations are already configured in Scala Steward with a hard-coded version. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, well I added the second subproject (plus shuffling the old one into a nested directory) before reading that If it can be deleted, great! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||
|
||
import natchez.TraceValue | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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 feral.lambda.events.ApiGatewayProxyEvent | ||
import feral.lambda.events.ApiGatewayProxyEventV2 | ||
import feral.lambda.events.DynamoDbStreamEvent | ||
import feral.lambda.events.KinesisStreamEvent | ||
import feral.lambda.events.S3BatchEvent | ||
import feral.lambda.events.SqsRecordAttributes | ||
import natchez.Kernel | ||
import org.typelevel.ci._ | ||
|
||
trait KernelSource[Event] { | ||
def extract(event: Event): Kernel | ||
} | ||
|
||
object KernelSource { | ||
@inline def apply[E](implicit ev: KernelSource[E]): ev.type = ev | ||
|
||
def emptyKernelSource[E]: KernelSource[E] = _ => Kernel(Map.empty) | ||
|
||
private[this] val `X-Amzn-Trace-Id` = ci"X-Amzn-Trace-Id" | ||
|
||
implicit def apiGatewayProxyEvent: KernelSource[ApiGatewayProxyEvent] = | ||
e => Kernel(e.headers.getOrElse(Map.empty)) | ||
|
||
implicit def apiGatewayProxyEventV2: KernelSource[ApiGatewayProxyEventV2] = | ||
e => Kernel(e.headers) | ||
|
||
implicit def sqsRecordAttributes: KernelSource[SqsRecordAttributes] = | ||
a => Kernel(a.awsTraceHeader.map(`X-Amzn-Trace-Id` -> _).toMap) | ||
|
||
implicit def s3BatchEvent: KernelSource[S3BatchEvent] = KernelSource.emptyKernelSource | ||
|
||
@deprecated( | ||
"See feral.lambda.events.KinesisStreamEvent deprecation", | ||
since = "0.3.0" | ||
) | ||
implicit def kinesisStreamEvent: KernelSource[KinesisStreamEvent] = | ||
KernelSource.emptyKernelSource | ||
|
||
implicit def dynamoDbStreamEvent: KernelSource[DynamoDbStreamEvent] = | ||
KernelSource.emptyKernelSource | ||
} |
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.
@iRevive should we be concerned?Gah, it'sTest
, I'm sorry. It's all fine :)