Skip to content
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 an example to detect azure resource providers #391

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions azure-resource-detector/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -69,4 +70,4 @@ rootProject.children.forEach {
it.projectDir = file(
"$rootDir/${it.name}".replace("opentelemetry-examples-", "")
)
}
}
Loading