-
Notifications
You must be signed in to change notification settings - Fork 130
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
Azure resource providers #1228
Open
zeitlinger
wants to merge
16
commits into
open-telemetry:main
Choose a base branch
from
zeitlinger:azure-resource-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Azure resource providers #1228
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
acfc597
add Azure resource provider
zeitlinger 34f89fa
add Azure VM resource provider
zeitlinger cfa5d4e
add Azure functions resource provider
zeitlinger bd049dd
add Azure container resource provider
zeitlinger 90a294f
add Azure vm resource provider
zeitlinger edd867f
docs
zeitlinger 13a1d13
Update
heyams b9396b7
Update
heyams f54a88b
Rename enum
heyams 534f401
Fix deprecated ResourceAttributes
heyams 0597f41
Fix tests
heyams 5b4a90e
Fix import
heyams 43ba1ae
add aks resource provider
zeitlinger 1d41f6f
add component owners
zeitlinger d81cfab
fix semconv usage
zeitlinger 5b7a469
fix visibility
zeitlinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Azure Resource Detectors for OpenTelemetry | ||
|
||
This module provides Azure resource detectors for OpenTelemetry. | ||
|
||
The following OpenTelemetry semantic conventions will be detected: | ||
|
||
| Resource attribute | VM | Functions | App Service | Containers | | ||
|-------------------------|----------|-----------------|-------------------|----------------------| | ||
| cloud.platform | azure_vm | azure_functions | azure_app_service | azure_container_apps | | ||
| cloud.provider | azure | azure | azure | azure | | ||
| cloud.resource.id | auto | | auto | | | ||
| cloud.region | auto | auto | auto | | | ||
| deployment.environment | | | auto | | | ||
| host.id | auto | | auto | | | ||
| host.name | auto | | | | | ||
| host.type | auto | | | | | ||
| os.type | auto | | | | | ||
| os.version | auto | | | | | ||
| azure.vm.scaleset.name | auto | | | | | ||
| azure.vm.sku | auto | | | | | ||
| service.name | | | auto | auto | | ||
| service.version | | | | auto | | ||
| service.instance.id | | | auto | auto | | ||
| azure.app.service.stamp | | | auto | | | ||
| faas.name | | auto | | | | ||
| faas.version | | auto | | | | ||
| faas.instance | | auto | | | | ||
| faas.faas.max_memory | | auto | | | | ||
|
||
## Component Owners | ||
|
||
- [Trask Stalnaker](https://github.com/trask), Microsoft | ||
- [Gregor Zeitlinger](https://github.com/zeitlinger), Grafana | ||
|
||
Learn more about component owners in [component_owners.yml](../.github/component_owners.yml). |
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,37 @@ | ||
plugins { | ||
id("otel.java-conventions") | ||
|
||
id("otel.publish-conventions") | ||
id("maven-publish") | ||
} | ||
|
||
description = "OpenTelemetry GCP Resources Support" | ||
otelJava.moduleName.set("io.opentelemetry.contrib.gcp.resource") | ||
|
||
// enable publishing to maven local | ||
java { | ||
withSourcesJar() | ||
} | ||
|
||
dependencies { | ||
api("io.opentelemetry:opentelemetry-api") | ||
api("io.opentelemetry:opentelemetry-sdk") | ||
|
||
implementation("io.opentelemetry.semconv:opentelemetry-semconv") | ||
|
||
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") | ||
|
||
implementation("com.fasterxml.jackson.core:jackson-core") | ||
implementation("com.squareup.okhttp3:okhttp") | ||
|
||
testImplementation("io.opentelemetry.semconv:opentelemetry-semconv-incubating") | ||
testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") | ||
testImplementation("io.opentelemetry:opentelemetry-sdk-testing") | ||
|
||
// testImplementation("org.mockito:mockito-core") | ||
testImplementation("com.google.guava:guava") | ||
|
||
testImplementation("org.junit.jupiter:junit-jupiter-api") | ||
testImplementation("org.assertj:assertj-core") | ||
testImplementation("com.linecorp.armeria:armeria-junit5") | ||
} |
74 changes: 74 additions & 0 deletions
74
...urces/src/main/java/io/opentelemetry/contrib/azure/resource/AzureAksResourceProvider.java
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,74 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.azure.resource; | ||
|
||
import static io.opentelemetry.contrib.azure.resource.IncubatingAttributes.CloudPlatformIncubatingValues.AZURE_AKS; | ||
import static io.opentelemetry.contrib.azure.resource.IncubatingAttributes.K8S_CLUSTER_NAME; | ||
|
||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
public class AzureAksResourceProvider extends CloudResourceProvider { | ||
|
||
private static final Map<String, AzureVmResourceProvider.Entry> COMPUTE_MAPPING = new HashMap<>(); | ||
|
||
static { | ||
COMPUTE_MAPPING.put( | ||
"resourceGroupName", | ||
new AzureVmResourceProvider.Entry( | ||
K8S_CLUSTER_NAME, AzureAksResourceProvider::parseClusterName)); | ||
} | ||
|
||
// visible for testing | ||
static String parseClusterName(String resourceGroup) { | ||
// Code inspired by | ||
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/exporter/datadogexporter/internal/hostmetadata/internal/azure/provider.go#L36 | ||
String[] splitAll = resourceGroup.split("_"); | ||
if (splitAll.length == 4 && splitAll[0].equalsIgnoreCase("mc")) { | ||
return splitAll[splitAll.length - 2]; | ||
} | ||
return resourceGroup; | ||
} | ||
|
||
// Environment variable that is set when running on Kubernetes | ||
static final String KUBERNETES_SERVICE_HOST = "KUBERNETES_SERVICE_HOST"; | ||
private final Supplier<Optional<String>> client; | ||
private final Map<String, String> environment; | ||
|
||
// SPI | ||
public AzureAksResourceProvider() { | ||
this(AzureMetadataService.defaultClient(), System.getenv()); | ||
} | ||
|
||
// visible for testing | ||
public AzureAksResourceProvider( | ||
zeitlinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Supplier<Optional<String>> client, Map<String, String> environment) { | ||
this.client = client; | ||
this.environment = environment; | ||
} | ||
|
||
@Override | ||
public int order() { | ||
// run after the fast cloud resource providers that only check environment variables | ||
// and before the AKS provider | ||
return 100; | ||
} | ||
|
||
@Override | ||
public Resource createResource(ConfigProperties configProperties) { | ||
if (environment.get(KUBERNETES_SERVICE_HOST) == null) { | ||
return Resource.empty(); | ||
} | ||
return client | ||
.get() | ||
.map(body -> AzureVmResourceProvider.parseMetadata(body, COMPUTE_MAPPING, AZURE_AKS)) | ||
.orElse(Resource.empty()); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
...rc/main/java/io/opentelemetry/contrib/azure/resource/AzureAppServiceResourceProvider.java
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,107 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.azure.resource; | ||
|
||
import static io.opentelemetry.contrib.azure.resource.IncubatingAttributes.CLOUD_REGION; | ||
import static io.opentelemetry.contrib.azure.resource.IncubatingAttributes.CLOUD_RESOURCE_ID; | ||
import static io.opentelemetry.contrib.azure.resource.IncubatingAttributes.CloudPlatformIncubatingValues.AZURE_APP_SERVICE; | ||
import static io.opentelemetry.contrib.azure.resource.IncubatingAttributes.DEPLOYMENT_ENVIRONMENT_NAME; | ||
import static io.opentelemetry.contrib.azure.resource.IncubatingAttributes.HOST_ID; | ||
import static io.opentelemetry.contrib.azure.resource.IncubatingAttributes.SERVICE_INSTANCE_ID; | ||
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.api.internal.StringUtils; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import javax.annotation.Nullable; | ||
|
||
public class AzureAppServiceResourceProvider extends CloudResourceProvider { | ||
|
||
static final AttributeKey<String> AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE = | ||
AttributeKey.stringKey("azure.app.service.stamp"); | ||
static final String REGION_NAME = "REGION_NAME"; | ||
private static final String WEBSITE_HOME_STAMPNAME = "WEBSITE_HOME_STAMPNAME"; | ||
private static final String WEBSITE_HOSTNAME = "WEBSITE_HOSTNAME"; | ||
static final String WEBSITE_INSTANCE_ID = "WEBSITE_INSTANCE_ID"; | ||
private static final String WEBSITE_OWNER_NAME = "WEBSITE_OWNER_NAME"; | ||
private static final String WEBSITE_RESOURCE_GROUP = "WEBSITE_RESOURCE_GROUP"; | ||
static final String WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME"; | ||
private static final String WEBSITE_SLOT_NAME = "WEBSITE_SLOT_NAME"; | ||
|
||
private static final Map<AttributeKey<String>, String> ENV_VAR_MAPPING = new HashMap<>(); | ||
|
||
static { | ||
ENV_VAR_MAPPING.put(CLOUD_REGION, REGION_NAME); | ||
ENV_VAR_MAPPING.put(DEPLOYMENT_ENVIRONMENT_NAME, WEBSITE_SLOT_NAME); | ||
ENV_VAR_MAPPING.put(HOST_ID, WEBSITE_HOSTNAME); | ||
ENV_VAR_MAPPING.put(SERVICE_INSTANCE_ID, WEBSITE_INSTANCE_ID); | ||
ENV_VAR_MAPPING.put(AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE, WEBSITE_HOME_STAMPNAME); | ||
} | ||
|
||
private final Map<String, String> env; | ||
|
||
// SPI | ||
public AzureAppServiceResourceProvider() { | ||
this(System.getenv()); | ||
} | ||
|
||
// Visible for testing | ||
AzureAppServiceResourceProvider(Map<String, String> env) { | ||
this.env = env; | ||
} | ||
|
||
@Override | ||
public Resource createResource(ConfigProperties config) { | ||
return Resource.create(getAttributes()); | ||
} | ||
|
||
public Attributes getAttributes() { | ||
AzureEnvVarPlatform detect = AzureEnvVarPlatform.detect(env); | ||
if (detect != AzureEnvVarPlatform.APP_SERVICE) { | ||
return Attributes.empty(); | ||
} | ||
String name = Objects.requireNonNull(env.get(WEBSITE_SITE_NAME)); | ||
AttributesBuilder builder = AzureVmResourceProvider.azureAttributeBuilder(AZURE_APP_SERVICE); | ||
builder.put(SERVICE_NAME, name); | ||
|
||
String resourceUri = resourceUri(name); | ||
if (resourceUri != null) { | ||
builder.put(CLOUD_RESOURCE_ID, resourceUri); | ||
} | ||
|
||
AzureEnvVarPlatform.addAttributesFromEnv(ENV_VAR_MAPPING, env, builder); | ||
|
||
return builder.build(); | ||
} | ||
|
||
@Nullable | ||
private String resourceUri(String websiteName) { | ||
String websiteResourceGroup = env.get(WEBSITE_RESOURCE_GROUP); | ||
String websiteOwnerName = env.get(WEBSITE_OWNER_NAME); | ||
|
||
String subscriptionId; | ||
if (websiteOwnerName != null && websiteOwnerName.contains("+")) { | ||
subscriptionId = websiteOwnerName.substring(0, websiteOwnerName.indexOf("+")); | ||
} else { | ||
subscriptionId = websiteOwnerName; | ||
} | ||
|
||
if (StringUtils.isNullOrEmpty(websiteResourceGroup) | ||
|| StringUtils.isNullOrEmpty(subscriptionId)) { | ||
return null; | ||
} | ||
|
||
return String.format( | ||
"/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Web/sites/%s", | ||
subscriptionId, websiteResourceGroup, websiteName); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...rc/main/java/io/opentelemetry/contrib/azure/resource/AzureContainersResourceProvider.java
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,65 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.azure.resource; | ||
|
||
import static io.opentelemetry.contrib.azure.resource.IncubatingAttributes.SERVICE_INSTANCE_ID; | ||
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME; | ||
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_VERSION; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class AzureContainersResourceProvider extends CloudResourceProvider { | ||
|
||
static final String CONTAINER_APP_NAME = "CONTAINER_APP_NAME"; | ||
|
||
private static final String CONTAINER_APP_REPLICA_NAME = "CONTAINER_APP_REPLICA_NAME"; | ||
private static final String CONTAINER_APP_REVISION = "CONTAINER_APP_REVISION"; | ||
|
||
private static final Map<AttributeKey<String>, String> ENV_VAR_MAPPING = new HashMap<>(); | ||
|
||
static { | ||
ENV_VAR_MAPPING.put(SERVICE_NAME, CONTAINER_APP_NAME); | ||
ENV_VAR_MAPPING.put(SERVICE_INSTANCE_ID, CONTAINER_APP_REPLICA_NAME); | ||
ENV_VAR_MAPPING.put(SERVICE_VERSION, CONTAINER_APP_REVISION); | ||
} | ||
|
||
private final Map<String, String> env; | ||
|
||
// SPI | ||
public AzureContainersResourceProvider() { | ||
this(System.getenv()); | ||
} | ||
|
||
// Visible for testing | ||
AzureContainersResourceProvider(Map<String, String> env) { | ||
this.env = env; | ||
} | ||
|
||
@Override | ||
public Resource createResource(ConfigProperties config) { | ||
return Resource.create(getAttributes()); | ||
} | ||
|
||
public Attributes getAttributes() { | ||
AzureEnvVarPlatform detect = AzureEnvVarPlatform.detect(env); | ||
if (detect != AzureEnvVarPlatform.CONTAINER_APP) { | ||
return Attributes.empty(); | ||
} | ||
|
||
AttributesBuilder builder = | ||
AzureVmResourceProvider.azureAttributeBuilder("azure_container_apps"); | ||
|
||
AzureEnvVarPlatform.addAttributesFromEnv(ENV_VAR_MAPPING, env, builder); | ||
|
||
return builder.build(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...-resources/src/main/java/io/opentelemetry/contrib/azure/resource/AzureEnvVarPlatform.java
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,45 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.azure.resource; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import java.util.Map; | ||
|
||
public enum AzureEnvVarPlatform { | ||
APP_SERVICE, | ||
FUNCTIONS, | ||
CONTAINER_APP, | ||
NONE; | ||
|
||
public static AzureEnvVarPlatform detect(Map<String, String> env) { | ||
String appName = env.get(AzureContainersResourceProvider.CONTAINER_APP_NAME); | ||
if (appName != null) { | ||
return CONTAINER_APP; | ||
} | ||
String name = env.get(AzureAppServiceResourceProvider.WEBSITE_SITE_NAME); | ||
if (name == null) { | ||
return NONE; | ||
} | ||
if (env.get(AzureFunctionsResourceProvider.FUNCTIONS_VERSION) != null) { | ||
return FUNCTIONS; | ||
} | ||
return APP_SERVICE; | ||
} | ||
|
||
static void addAttributesFromEnv( | ||
Map<AttributeKey<String>, String> mapping, | ||
Map<String, String> env, | ||
AttributesBuilder builder) { | ||
mapping.forEach( | ||
(key, value) -> { | ||
String envValue = env.get(value); | ||
if (envValue != null) { | ||
builder.put(key, envValue); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add aks (azure Kubernetes service)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zeitlinger do you think it's a good idea to add AKS?
cloud.provider=Azure
cloud.platform=azure_aks
cloud.resource_id=${armId},
cloud.region=${armRegion},
k8s.cluster.name=
k8s.namespace.name=
k8s.node.name=
k8s.pod.name=
k8s.pod.uid
k8s.container.name
k8s.deployment.name
k8s.deployment.uid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, definitely. So far, I've started with the existing implementations in other languages.
Can you point me to a starting point to implement an AKS provider?