-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #747 from iRevive/contrib/aws/resource-detectors
sdk-contrib: add `aws-resource` module
- Loading branch information
Showing
6 changed files
with
394 additions
and
4 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,186 @@ | ||
# 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("```") | ||
AWSLambdaDetector[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 autoconfigure the SDK: | ||
|
||
@:select(sdk-entry-point) | ||
|
||
@:choice(sdk) | ||
|
||
`OpenTelemetrySdk.autoConfigured` configures both `MeterProvider` and `TracerProvider`: | ||
|
||
```scala mdoc:silent:reset | ||
import cats.effect.{IO, IOApp} | ||
import org.typelevel.otel4s.metrics.MeterProvider | ||
import org.typelevel.otel4s.sdk.OpenTelemetrySdk | ||
import org.typelevel.otel4s.sdk.contrib.aws.resource._ | ||
import org.typelevel.otel4s.sdk.exporter.otlp.autoconfigure.OtlpExportersAutoConfigure | ||
import org.typelevel.otel4s.trace.TracerProvider | ||
|
||
object TelemetryApp extends IOApp.Simple { | ||
|
||
def run: IO[Unit] = | ||
OpenTelemetrySdk | ||
.autoConfigured[IO]( | ||
// register OTLP exporters configurer | ||
_.addExportersConfigurer(OtlpExportersAutoConfigure[IO]) | ||
// register AWS Lambda detector | ||
.addResourceDetector(AWSLambdaDetector[IO]) | ||
) | ||
.use { autoConfigured => | ||
val sdk = autoConfigured.sdk | ||
program(sdk.meterProvider, sdk.tracerProvider) | ||
} | ||
|
||
def program( | ||
meterProvider: MeterProvider[IO], | ||
tracerProvider: TracerProvider[IO] | ||
): IO[Unit] = | ||
??? | ||
} | ||
``` | ||
|
||
@:choice(traces) | ||
|
||
`SdkTraces` configures only `TracerProvider`: | ||
|
||
```scala mdoc:silent:reset | ||
import cats.effect.{IO, IOApp} | ||
import org.typelevel.otel4s.sdk.contrib.aws.resource._ | ||
import org.typelevel.otel4s.sdk.exporter.otlp.trace.autoconfigure.OtlpSpanExporterAutoConfigure | ||
import org.typelevel.otel4s.sdk.trace.SdkTraces | ||
import org.typelevel.otel4s.trace.TracerProvider | ||
|
||
object TelemetryApp extends IOApp.Simple { | ||
|
||
def run: IO[Unit] = | ||
SdkTraces | ||
.autoConfigured[IO]( | ||
// register OTLP exporters configurer | ||
_.addExporterConfigurer(OtlpSpanExporterAutoConfigure[IO]) | ||
// register AWS Lambda detector | ||
.addResourceDetector(AWSLambdaDetector[IO]) | ||
) | ||
.use { autoConfigured => | ||
program(autoConfigured.tracerProvider) | ||
} | ||
|
||
def program( | ||
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
104 changes: 104 additions & 0 deletions
104
...urce/src/main/scala/org/typelevel/otel4s/sdk/contrib/aws/resource/AWSLambdaDetector.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,104 @@ | ||
/* | ||
* 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 AWSLambdaDetector[F[_]: Env: Monad] | ||
extends TelemetryResourceDetector[F] { | ||
|
||
import AWSLambdaDetector.Const | ||
import AWSLambdaDetector.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 AWSLambdaDetector { | ||
|
||
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") | ||
} | ||
|
||
/** 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. | ||
* | ||
* @example | ||
* {{{ | ||
* OpenTelemetrySdk | ||
* .autoConfigured[IO]( | ||
* // register OTLP exporters configurer | ||
* _.addExportersConfigurer(OtlpExportersAutoConfigure[IO]) | ||
* // register AWS Lambda detector | ||
* .addResourceDetector(AWSLambdaDetector[IO]) | ||
* ) | ||
* .use { autoConfigured => | ||
* val sdk = autoConfigured.sdk | ||
* ??? | ||
* } | ||
* }}} | ||
*/ | ||
def apply[F[_]: Env: Monad]: TelemetryResourceDetector[F] = | ||
new AWSLambdaDetector[F] | ||
|
||
} |
Oops, something went wrong.