From 7e935a0793be16e05382265367da62b0bc7862f2 Mon Sep 17 00:00:00 2001 From: heyams Date: Thu, 18 Apr 2024 14:18:59 -0700 Subject: [PATCH 1/2] Add an example to detect azure resource provider --- azure-resource-detector/build.gradle.kts | 50 +++++++++++++++++ .../AzureResourceDetectorExample.java | 55 +++++++++++++++++++ settings.gradle.kts | 3 +- 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 azure-resource-detector/build.gradle.kts create mode 100644 azure-resource-detector/src/main/java/io/opentelemetry/example/azure/resource/detector/AzureResourceDetectorExample.java 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..6f11837568 --- /dev/null +++ b/azure-resource-detector/src/main/java/io/opentelemetry/example/azure/resource/detector/AzureResourceDetectorExample.java @@ -0,0 +1,55 @@ +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.sdk.OpenTelemetrySdk; +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; +import io.opentelemetry.sdk.autoconfigure.ResourceConfiguration; +import io.opentelemetry.sdk.common.CompletableResultCode; +import io.opentelemetry.sdk.resources.Resource; +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(); + + // Shows the resource attributes detected from the environment variables + // and system properties. + System.out.println("Detecting resource: Environment"); + Resource autoResource = ResourceConfiguration.createEnvironmentResource(); + System.out.println(autoResource.getAttributes() + "\n"); + + // Shows the resource attributes detected by the Azure App Service Resource Provider + System.out.println("Detecting resource: hardcoded"); + AzureAppServiceResourceProvider resourceProvider = new AzureAppServiceResourceProvider(); + 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-providers-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 From 4d5a5c6e54076abb1f9784daa984c438956bbcb8 Mon Sep 17 00:00:00 2001 From: heyams Date: Thu, 18 Apr 2024 15:52:55 -0700 Subject: [PATCH 2/2] Update --- .../AzureResourceDetectorExample.java | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) 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 index 6f11837568..07f9c94053 100644 --- 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 @@ -3,11 +3,12 @@ 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.autoconfigure.ResourceConfiguration; import io.opentelemetry.sdk.common.CompletableResultCode; -import io.opentelemetry.sdk.resources.Resource; import java.util.concurrent.TimeUnit; public class AzureResourceDetectorExample { @@ -19,16 +20,23 @@ public static void main(String[] args) { OpenTelemetrySdk openTelemetrySdk = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk(); - // Shows the resource attributes detected from the environment variables - // and system properties. - System.out.println("Detecting resource: Environment"); - Resource autoResource = ResourceConfiguration.createEnvironmentResource(); - System.out.println(autoResource.getAttributes() + "\n"); - - // Shows the resource attributes detected by the Azure App Service Resource Provider - System.out.println("Detecting resource: hardcoded"); - AzureAppServiceResourceProvider resourceProvider = new AzureAppServiceResourceProvider(); - System.out.println(resourceProvider.getAttributes() + "\n"); + 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. @@ -36,7 +44,7 @@ public static void main(String[] args) { Span span = openTelemetrySdk .getTracer(INSTRUMENTATION_SCOPE_NAME) - .spanBuilder("azure-resource-providers-detector") + .spanBuilder("azure-resource-detector") .startSpan(); try (Scope ignored = span.makeCurrent()) { // Simulate work: this could be simulating a network request or an expensive disk operation