diff --git a/azure-resource-detector/build.gradle.kts b/azure-resource-detector/build.gradle.kts new file mode 100644 index 0000000000..c589de04dd --- /dev/null +++ b/azure-resource-detector/build.gradle.kts @@ -0,0 +1,50 @@ +plugins { + id("java") + id("application") +} + +group = "com.example" +description = "OpenTelemetry Example for Azure Resource Detector" +val moduleName by extra { "io.opentelemetry.examples.azure-resource-detector" } + +val autoconfConfig = listOf( + "-Dotel.traces.exporter=logging-otlp", + "-Dotel.metrics.exporter=none", + "-Dotel.logs.exporter=none", + "-Dotel.java.global-autoconfigure.enabled=true" +) + +repositories { + mavenCentral() + mavenLocal() +} + +java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(8)) + } +} + +application { + mainClass = "io.opentelemetry.example.azure.resource.detector.AzureResourceDetectorExample" + applicationDefaultJvmArgs = autoconfConfig +} + +dependencies { + implementation("io.opentelemetry:opentelemetry-api:1.37.0") + implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure:1.37.0") + implementation("io.opentelemetry:opentelemetry-exporter-logging-otlp:1.37.0") + implementation("io.opentelemetry.contrib:opentelemetry-azure-resources:1.34.0-alpha-SNAPSHOT") +} +repositories { + mavenCentral() +} + +dependencies { + testImplementation(platform("org.junit:junit-bom:5.9.1")) + testImplementation("org.junit.jupiter:junit-jupiter") +} + +tasks.test { + useJUnitPlatform() +} \ No newline at end of file diff --git a/azure-resource-detector/src/main/java/io/opentelemetry/example/azure/resource/detector/AzureResourceDetectorExample.java b/azure-resource-detector/src/main/java/io/opentelemetry/example/azure/resource/detector/AzureResourceDetectorExample.java new file mode 100644 index 0000000000..07f9c94053 --- /dev/null +++ b/azure-resource-detector/src/main/java/io/opentelemetry/example/azure/resource/detector/AzureResourceDetectorExample.java @@ -0,0 +1,63 @@ +package io.opentelemetry.example.azure.resource.detector; + +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.context.Scope; +import io.opentelemetry.contrib.azure.resource.AzureAppServiceResourceProvider; +import io.opentelemetry.contrib.azure.resource.AzureContainersResourceProvider; +import io.opentelemetry.contrib.azure.resource.AzureEnvVarPlatform; +import io.opentelemetry.contrib.azure.resource.AzureFunctionsResourceProvider; +import io.opentelemetry.sdk.OpenTelemetrySdk; +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; +import io.opentelemetry.sdk.common.CompletableResultCode; +import java.util.concurrent.TimeUnit; + +public class AzureResourceDetectorExample { + private static final String INSTRUMENTATION_SCOPE_NAME = + AzureResourceDetectorExample.class.getName(); + + public static void main(String[] args) { + // Get the autoconfigured OpenTelemetry SDK + OpenTelemetrySdk openTelemetrySdk = + AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk(); + + AzureEnvVarPlatform detect = AzureEnvVarPlatform.detect(System.getenv()); + if (detect == AzureEnvVarPlatform.APP_SERVICE) { + // Shows the resource attributes detected by the Azure App Service Resource Provider + System.out.println("Detecting Azure App Service resource"); + AzureAppServiceResourceProvider resourceProvider = new AzureAppServiceResourceProvider(); + System.out.println(resourceProvider.getAttributes() + "\n"); + } else if (detect == AzureEnvVarPlatform.FUNCTIONS) { + // Shows the resource attributes detected by the Azure Function Resource Provider + System.out.println("Detecting Azure Function resource"); + AzureFunctionsResourceProvider resourceProvider = new AzureFunctionsResourceProvider(); + System.out.println(resourceProvider.getAttributes() + "\n"); + } else if (detect == AzureEnvVarPlatform.CONTAINER_APP) { + // Shows the resource attributes detected by the Azure Container Insights Resource Provider + System.out.println("Detecting Azure Container App resource"); + AzureContainersResourceProvider resourceProvider = new AzureContainersResourceProvider(); + System.out.println(resourceProvider.getAttributes() + "\n"); + } + + // Shows the attributes attached to the Resource that was set for TracerProvider + // via the autoconfiguration SPI. + System.out.println("Detecting resource: Azure resources"); + Span span = + openTelemetrySdk + .getTracer(INSTRUMENTATION_SCOPE_NAME) + .spanBuilder("azure-resource-detector") + .startSpan(); + try (Scope ignored = span.makeCurrent()) { + // Simulate work: this could be simulating a network request or an expensive disk operation + Thread.sleep(500); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } finally { + span.end(); + } + // Flush all buffered traces + CompletableResultCode completableResultCode = + openTelemetrySdk.getSdkTracerProvider().shutdown(); + // wait till export finishes + completableResultCode.join(10000, TimeUnit.MILLISECONDS); + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index e3640f2005..d681d113ba 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -45,6 +45,7 @@ gradleEnterprise { rootProject.name = "opentelemetry-java-examples" include( ":opentelemetry-examples-autoconfigure", + ":opentelemetry-examples-azure-resource-detector", ":opentelemetry-examples-file-configuration", ":opentelemetry-examples-http", ":opentelemetry-examples-jaeger", @@ -69,4 +70,4 @@ rootProject.children.forEach { it.projectDir = file( "$rootDir/${it.name}".replace("opentelemetry-examples-", "") ) -} +} \ No newline at end of file