-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sdk-contrib: add
aws-resource
module
- Loading branch information
Showing
6 changed files
with
341 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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,146 @@ | ||
# AWS | Resource detectors | ||
|
||
Resource detectors can add environment-specific attributes to the telemetry resource. | ||
AWS detectors are implemented as a third-party library, and you need to enable them manually. | ||
|
||
## The list of detectors | ||
|
||
### 1. aws-lambda | ||
|
||
The detector relies on the `AWS_REGION`, `AWS_LAMBDA_FUNCTION_NAME`, and `AWS_LAMBDA_FUNCTION_VERSION` environment variables | ||
to configure the telemetry resource. | ||
Either `AWS_LAMBDA_FUNCTION_NAME` or `AWS_LAMBDA_FUNCTION_VERSION` must be present. | ||
|
||
```scala mdoc:passthrough | ||
import cats.effect.IO | ||
import cats.effect.std.Env | ||
import cats.effect.unsafe.implicits.global | ||
import org.typelevel.otel4s.sdk.contrib.aws.resource._ | ||
import scala.collection.immutable | ||
|
||
val envEntries = Map( | ||
"AWS_REGION" -> "eu-west-1", | ||
"AWS_LAMBDA_FUNCTION_NAME" -> "function", | ||
"AWS_LAMBDA_FUNCTION_VERSION" -> "0.0.1" | ||
) | ||
|
||
implicit val env: Env[IO] = | ||
new Env[IO] { | ||
def get(name: String): IO[Option[String]] = IO.pure(envEntries.get(name)) | ||
def entries: IO[immutable.Iterable[(String, String)]] = IO.pure(envEntries) | ||
} | ||
|
||
println("Environment: ") | ||
println("```") | ||
envEntries.foreach { case (k, v) => println(s"${k.replace("_", "_")}=$v") } | ||
println("```") | ||
|
||
println("Detected resource: ") | ||
println("```") | ||
LambdaDetector[IO].detect.unsafeRunSync().foreach { resource => | ||
resource.attributes.toList.sortBy(_.key.name).foreach { attribute => | ||
println(attribute.key.name + ": " + attribute.value) | ||
} | ||
} | ||
println("```") | ||
``` | ||
|
||
## Getting Started | ||
|
||
@:select(build-tool) | ||
|
||
@:choice(sbt) | ||
|
||
Add settings to the `build.sbt`: | ||
|
||
```scala | ||
libraryDependencies ++= Seq( | ||
"org.typelevel" %%% "otel4s-sdk" % "@VERSION@", // <1> | ||
"org.typelevel" %%% "otel4s-sdk-exporter" % "@VERSION@", // <2> | ||
"org.typelevel" %%% "otel4s-sdk-contrib-aws-resource" % "@VERSION@" // <3> | ||
) | ||
``` | ||
|
||
@:choice(scala-cli) | ||
|
||
Add directives to the `*.scala` file: | ||
|
||
```scala | ||
//> using lib "org.typelevel::otel4s-sdk::@VERSION@" // <1> | ||
//> using lib "org.typelevel::otel4s-sdk-exporter::@VERSION@" // <2> | ||
//> using lib "org.typelevel::otel4s-sdk-contrib-aws-resource::@VERSION@" // <3> | ||
``` | ||
|
||
@:@ | ||
|
||
1. Add the `otel4s-sdk` library | ||
2. Add the `otel4s-sdk-exporter` library. Without the exporter, the application will crash | ||
3. Add the `otel4s-sdk-contrib-aws-resource` library | ||
|
||
_______ | ||
|
||
Then use `OpenTelemetrySdk.autoConfigured` to autoconfigure the SDK: | ||
```scala mdoc:silent:reset | ||
import cats.effect.{IO, IOApp} | ||
import org.typelevel.otel4s.sdk.OpenTelemetrySdk | ||
import org.typelevel.otel4s.sdk.exporter.otlp.autoconfigure.OtlpExportersAutoConfigure | ||
import org.typelevel.otel4s.metrics.MeterProvider | ||
import org.typelevel.otel4s.trace.TracerProvider | ||
import org.typelevel.otel4s.sdk.contrib.aws.resource._ | ||
|
||
object TelemetryApp extends IOApp.Simple { | ||
|
||
def run: IO[Unit] = | ||
OpenTelemetrySdk | ||
.autoConfigured[IO]( | ||
// register OTLP exporters configurer | ||
_.addExportersConfigurer(OtlpExportersAutoConfigure[IO]) | ||
// register AWS Lambda detector | ||
.addResourceDetector(LambdaDetector[IO]) | ||
) | ||
.use { autoConfigured => | ||
val sdk = autoConfigured.sdk | ||
program(sdk.meterProvider, sdk.tracerProvider) | ||
} | ||
|
||
def program( | ||
meterProvider: MeterProvider[IO], | ||
tracerProvider: TracerProvider[IO] | ||
): IO[Unit] = | ||
??? | ||
} | ||
``` | ||
|
||
|
||
## Configuration | ||
|
||
The `OpenTelemetrySdk.autoConfigured(...)` and `SdkTraces.autoConfigured(...)` rely on the environment variables and system properties to configure the SDK. | ||
Check out the [configuration details](configuration.md#telemetry-resource-detectors). | ||
|
||
There are several ways to configure the options: | ||
|
||
@:select(sdk-options-source) | ||
|
||
@:choice(sbt) | ||
|
||
Add settings to the `build.sbt`: | ||
|
||
```scala | ||
javaOptions += "-Dotel.otel4s.resource.detectors.enabled=aws-lambda" | ||
envVars ++= Map("OTEL_OTEL4S_RESOURCE_DETECTORS_ENABLE" -> "aws-lambda") | ||
``` | ||
|
||
@:choice(scala-cli) | ||
|
||
Add directives to the `*.scala` file: | ||
|
||
```scala | ||
//> using javaOpt -Dotel.otel4s.resource.detectors.enabled=aws-lambda | ||
``` | ||
|
||
@:choice(shell) | ||
|
||
```shell | ||
$ export OTEL_OTEL4S_RESOURCE_DETECTORS_ENABLED=aws-lambda | ||
``` | ||
@:@ |
This file contains 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 |
---|---|---|
|
@@ -3,4 +3,5 @@ laika.title = SDK | |
laika.navigationOrder = [ | ||
overview.md | ||
configuration.md | ||
aws-resource-detectors.md | ||
] |
82 changes: 82 additions & 0 deletions
82
...esource/src/main/scala/org/typelevel/otel4s/sdk/contrib/aws/resource/LambdaDetector.scala
This file contains 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,82 @@ | ||
/* | ||
* Copyright 2024 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 org.typelevel.otel4s.sdk.contrib.aws.resource | ||
|
||
import cats.Monad | ||
import cats.effect.std.Env | ||
import cats.syntax.flatMap._ | ||
import cats.syntax.functor._ | ||
import org.typelevel.otel4s.AttributeKey | ||
import org.typelevel.otel4s.Attributes | ||
import org.typelevel.otel4s.sdk.TelemetryResource | ||
import org.typelevel.otel4s.sdk.resource.TelemetryResourceDetector | ||
import org.typelevel.otel4s.semconv.SchemaUrls | ||
|
||
private class LambdaDetector[F[_]: Env: Monad] | ||
extends TelemetryResourceDetector[F] { | ||
|
||
import LambdaDetector.Const | ||
import LambdaDetector.Keys | ||
|
||
def name: String = Const.Name | ||
|
||
def detect: F[Option[TelemetryResource]] = | ||
for { | ||
region <- Env[F].get("AWS_REGION") | ||
functionName <- Env[F].get("AWS_LAMBDA_FUNCTION_NAME") | ||
functionVersion <- Env[F].get("AWS_LAMBDA_FUNCTION_VERSION") | ||
} yield build(region, functionName, functionVersion) | ||
|
||
private def build( | ||
region: Option[String], | ||
functionName: Option[String], | ||
functionVersion: Option[String] | ||
): Option[TelemetryResource] = | ||
Option.when(functionName.nonEmpty || functionVersion.nonEmpty) { | ||
val builder = Attributes.newBuilder | ||
|
||
builder.addOne(Keys.CloudProvider, Const.CloudProvider) | ||
builder.addOne(Keys.CloudPlatform, Const.CloudPlatform) | ||
|
||
region.foreach(r => builder.addOne(Keys.CloudRegion, r)) | ||
functionName.foreach(name => builder.addOne(Keys.FaasName, name)) | ||
functionVersion.foreach(v => builder.addOne(Keys.FaasVersion, v)) | ||
|
||
TelemetryResource(builder.result(), Some(SchemaUrls.Current)) | ||
} | ||
} | ||
|
||
object LambdaDetector { | ||
|
||
private object Const { | ||
val Name = "aws-lambda" | ||
val CloudProvider = "aws" | ||
val CloudPlatform = "aws_lambda" | ||
} | ||
|
||
private object Keys { | ||
val CloudProvider: AttributeKey[String] = AttributeKey("cloud.provider") | ||
val CloudPlatform: AttributeKey[String] = AttributeKey("cloud.platform") | ||
val CloudRegion: AttributeKey[String] = AttributeKey("cloud.region") | ||
val FaasName: AttributeKey[String] = AttributeKey("faas.name") | ||
val FaasVersion: AttributeKey[String] = AttributeKey("faas.version") | ||
} | ||
|
||
def apply[F[_]: Env: Monad]: TelemetryResourceDetector[F] = | ||
new LambdaDetector[F] | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
...ce/src/test/scala/org/typelevel/otel4s/sdk/contrib/aws/resource/LambdaDetectorSuite.scala
This file contains 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,85 @@ | ||
/* | ||
* Copyright 2024 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 org.typelevel.otel4s.sdk.contrib.aws.resource | ||
|
||
import cats.effect.IO | ||
import cats.effect.std.Env | ||
import munit.CatsEffectSuite | ||
import org.typelevel.otel4s.Attributes | ||
import org.typelevel.otel4s.sdk.TelemetryResource | ||
import org.typelevel.otel4s.semconv.SchemaUrls | ||
import org.typelevel.otel4s.semconv.experimental.attributes.CloudExperimentalAttributes._ | ||
import org.typelevel.otel4s.semconv.experimental.attributes.FaasExperimentalAttributes._ | ||
|
||
import scala.collection.immutable | ||
|
||
class LambdaDetectorSuite extends CatsEffectSuite { | ||
|
||
test("add only defined attributes") { | ||
implicit val env: Env[IO] = | ||
constEnv("AWS_LAMBDA_FUNCTION_NAME" -> "function") | ||
|
||
val expected = TelemetryResource( | ||
Attributes( | ||
CloudProvider(CloudProviderValue.Aws.value), | ||
CloudPlatform(CloudPlatformValue.AwsLambda.value), | ||
FaasName("function") | ||
), | ||
Some(SchemaUrls.Current) | ||
) | ||
|
||
LambdaDetector[IO].detect.assertEquals(Some(expected)) | ||
} | ||
|
||
test("add all attributes") { | ||
implicit val env: Env[IO] = constEnv( | ||
"AWS_REGION" -> "eu-west-1", | ||
"AWS_LAMBDA_FUNCTION_NAME" -> "function", | ||
"AWS_LAMBDA_FUNCTION_VERSION" -> "0.0.1" | ||
) | ||
|
||
val expected = TelemetryResource( | ||
Attributes( | ||
CloudProvider(CloudProviderValue.Aws.value), | ||
CloudPlatform(CloudPlatformValue.AwsLambda.value), | ||
CloudRegion("eu-west-1"), | ||
FaasName("function"), | ||
FaasVersion("0.0.1") | ||
), | ||
Some(SchemaUrls.Current) | ||
) | ||
|
||
LambdaDetector[IO].detect.assertEquals(Some(expected)) | ||
} | ||
|
||
test("return None when both NAME and VERSION are undefined") { | ||
implicit val env: Env[IO] = constEnv("AWS_REGION" -> "eu-west-1") | ||
|
||
LambdaDetector[IO].detect.assertEquals(None) | ||
} | ||
|
||
private def constEnv(pairs: (String, String)*): Env[IO] = | ||
new Env[IO] { | ||
private val params = pairs.toMap | ||
|
||
def get(name: String): IO[Option[String]] = | ||
IO.pure(params.get(name)) | ||
|
||
def entries: IO[immutable.Iterable[(String, String)]] = | ||
IO.pure(params) | ||
} | ||
} |