-
Notifications
You must be signed in to change notification settings - Fork 727
Azure resource detectors #1901
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
Merged
lzchen
merged 28 commits into
open-telemetry:main
from
jeremydvoss:azur_app_service_resource_detector
Aug 17, 2023
Merged
Azure resource detectors #1901
Changes from 17 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
8b23f72
init commit for app service resource detector
jeremydvoss 67dbe64
Tests pass
jeremydvoss 0fcf824
changelog
jeremydvoss 68ca05c
readme
jeremydvoss 0a66179
readme
jeremydvoss 72d9797
Readme and commented code
jeremydvoss 09c4ee1
removed prints
jeremydvoss 120a246
manifest typo
jeremydvoss ffd7258
changelog
jeremydvoss e903aa4
Resource declaration
jeremydvoss e05dcc9
changelog
jeremydvoss be060f5
rename to combine
jeremydvoss fc9ce6e
Combined. Need to fix tests
jeremydvoss 40636fd
updated linux json
jeremydvoss 41a0136
tests pass
jeremydvoss 234d5da
changelog
jeremydvoss d133e59
Combined readme
jeremydvoss cad0e16
Update resource/opentelemetry-resource-detector-azure/README.rst
jeremydvoss 1e6b26f
Beta
jeremydvoss b177c8c
Improved readme
jeremydvoss 43e5a09
Fixed readme links
jeremydvoss 776a3c7
Updated readme
jeremydvoss 8168d22
Fixed hyperlinks
jeremydvoss b7af527
Reordered readme
jeremydvoss abf44aa
Replace MIT license with apache 2.0
jeremydvoss 057a58e
license
jeremydvoss 60c39a5
Merge branch 'main' into azur_app_service_resource_detector
lzchen a69f6d3
Merge branch 'main' into azur_app_service_resource_detector
jeremydvoss 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 hidden or 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 hidden or 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,21 @@ | ||
Copyright (c) Microsoft Corporation. | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
File renamed without changes.
This file contains hidden or 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,82 @@ | ||
OpenTelemetry Resource detectors for Azure | ||
========================================================== | ||
jeremydvoss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|pypi| | ||
|
||
.. |pypi| image:: https://badge.fury.io/py/opentelemetry-resource-detector-azure.svg | ||
:target: https://pypi.org/project/opentelemetry-resource-detector-azure/ | ||
|
||
|
||
jeremydvoss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
The Azure App Service Resource Detector sets the following Resource Attributes: | ||
* `service.name` set to the value of the WEBSITE_SITE_NAME environment variable. | ||
* `cloud.provider` set to the value of the "azure". | ||
* `cloud.platform` set to the value of the "azure_app_service". | ||
* `cloud.resource_id` set using the WEBSITE_RESOURCE_GROUP WEBSITE_OWNER_NAME and WEBSITE_SITE_NAME environment variables. | ||
* `cloud.region` set to the value of the REGION_NAME environment variable. | ||
* `deployment.environment` set to the value of the WEBSITE_SLOT_NAME environment variable. | ||
* `host.id` set to the value of the WEBSITE_HOSTNAME environment variable. | ||
* `service.instance.id` set to the value of the WEBSITE_INSTANCE_ID environment variable. | ||
* `azure.app.service.stamp` set to the value of the WEBSITE_HOME_STAMPNAME environment variable. | ||
|
||
The Azure VM Resource Detector sets the following Resource Attributes: | ||
* `azure.vm.scaleset.name` | ||
* `azure.vm.sku` | ||
* `cloud.platform` | ||
* `cloud.provider` | ||
* `cloud.region` | ||
* `cloud.resource_id` | ||
* `host.id` | ||
* `host.name` | ||
* `host.type` | ||
* `os.type` | ||
* `os.version` | ||
* `service.instance.id` | ||
|
||
For more information, see the Semantic Conventions for Cloud Resource Attributes. | ||
jeremydvoss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Installation | ||
------------ | ||
|
||
:: | ||
|
||
pip install opentelemetry-resource-detector-azure | ||
jeremydvoss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
--------------------------- | ||
|
||
Usage example for `opentelemetry-resource-detector-azure` | ||
|
||
.. code-block:: python | ||
|
||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.resource.detector.azure.app_service import ( | ||
AzureAppServiceResourceDetector, | ||
AzureVMResourceDetector, | ||
) | ||
from opentelemetry.resource.detector.azure.vm import ( | ||
AzureVMResourceDetector, | ||
) | ||
from opentelemetry.sdk.resources import get_aggregated_resources | ||
|
||
|
||
trace.set_tracer_provider( | ||
TracerProvider( | ||
resource=get_aggregated_resources( | ||
[ | ||
AzureAppServiceResourceDetector(), | ||
AzureVMResourceDetector(), | ||
] | ||
), | ||
) | ||
) | ||
|
||
You can also enable the Resource Detectors by adding `azure_app_service` and/or `azure_vm` to the `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS` environment variable: | ||
jeremydvoss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
`export OTEL_EXPERIMENTAL_RESOURCE_DETECTORS=azure_app_service,azure_vm` | ||
|
||
References | ||
---------- | ||
|
||
* `OpenTelemetry Project <https://opentelemetry.io/>`_ | ||
* `Resource Detector Docs <https://opentelemetry.io/docs/specs/otel/resource/sdk/#detecting-resource-information-from-the-environment>` | ||
* `Cloud Semantic Conventions <https://opentelemetry.io/docs/specs/otel/resource/semantic_conventions/cloud/>`_ |
51 changes: 51 additions & 0 deletions
51
resource/opentelemetry-resource-detector-azure/pyproject.toml
This file contains hidden or 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,51 @@ | ||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" | ||
|
||
[project] | ||
name = "opentelemetry-resource-detector-container" | ||
dynamic = ["version"] | ||
description = "Container Resource Detector for OpenTelemetry" | ||
readme = "README.rst" | ||
license = "Apache-2.0" | ||
requires-python = ">=3.7" | ||
authors = [ | ||
{ name = "OpenTelemetry Authors", email = "[email protected]" }, | ||
] | ||
classifiers = [ | ||
"Development Status :: 5 - Production/Stable", | ||
jeremydvoss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Intended Audience :: Developers", | ||
"License :: OSI Approved :: Apache Software License", | ||
"Programming Language :: Python", | ||
"Programming Language :: Python :: 3", | ||
"Programming Language :: Python :: 3.7", | ||
"Programming Language :: Python :: 3.8", | ||
"Programming Language :: Python :: 3.9", | ||
"Programming Language :: Python :: 3.10", | ||
"Programming Language :: Python :: 3.11", | ||
] | ||
dependencies = [ | ||
"opentelemetry-sdk ~= 1.19", | ||
] | ||
|
||
[project.optional-dependencies] | ||
test = [] | ||
|
||
[project.entry-points.opentelemetry_resource_detector] | ||
azure_app_service = "opentelemetry.resource.detector.azure.app_service:AzureAppServiceResourceDetector" | ||
azure_vm = "opentelemetry.resource.detector.azure.vm:AzureVMResourceDetector" | ||
|
||
[project.urls] | ||
Homepage = "https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/resource/opentelemetry-resource-detector-azure" | ||
|
||
[tool.hatch.version] | ||
path = "src/opentelemetry/resource/detector/azure/version.py" | ||
|
||
[tool.hatch.build.targets.sdist] | ||
include = [ | ||
"/src", | ||
"/tests", | ||
] | ||
|
||
[tool.hatch.build.targets.wheel] | ||
packages = ["src/opentelemetry"] |
63 changes: 63 additions & 0 deletions
63
...elemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/app_service.py
This file contains hidden or 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,63 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
from os import environ | ||
|
||
from opentelemetry.sdk.resources import ResourceDetector, Resource | ||
from opentelemetry.semconv.resource import ResourceAttributes, CloudPlatformValues, CloudProviderValues | ||
|
||
_AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE = "azure.app.service.stamp" | ||
# TODO: Remove once this resource attribute is no longer missing from SDK | ||
_CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE = "cloud.resource_id" | ||
_REGION_NAME = "REGION_NAME" | ||
_WEBSITE_HOME_STAMPNAME = "WEBSITE_HOME_STAMPNAME" | ||
_WEBSITE_HOSTNAME = "WEBSITE_HOSTNAME" | ||
_WEBSITE_INSTANCE_ID = "WEBSITE_INSTANCE_ID" | ||
_WEBSITE_OWNER_NAME = "WEBSITE_OWNER_NAME" | ||
_WEBSITE_RESOURCE_GROUP = "WEBSITE_RESOURCE_GROUP" | ||
_WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME" | ||
_WEBSITE_SLOT_NAME = "WEBSITE_SLOT_NAME" | ||
|
||
|
||
_APP_SERVICE_ATTRIBUTE_ENV_VARS = { | ||
ResourceAttributes.CLOUD_REGION: _REGION_NAME, | ||
ResourceAttributes.DEPLOYMENT_ENVIRONMENT: _WEBSITE_SLOT_NAME, | ||
ResourceAttributes.HOST_ID: _WEBSITE_HOSTNAME, | ||
ResourceAttributes.SERVICE_INSTANCE_ID: _WEBSITE_INSTANCE_ID, | ||
_AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE: _WEBSITE_HOME_STAMPNAME, | ||
} | ||
|
||
class AzureAppServiceResourceDetector(ResourceDetector): | ||
def detect(self) -> Resource: | ||
attributes = {} | ||
website_site_name = environ.get(_WEBSITE_SITE_NAME) | ||
if website_site_name: | ||
attributes[ResourceAttributes.SERVICE_NAME] = website_site_name | ||
attributes[ResourceAttributes.CLOUD_PROVIDER] = CloudProviderValues.AZURE.value | ||
attributes[ResourceAttributes.CLOUD_PLATFORM] = CloudPlatformValues.AZURE_APP_SERVICE.value | ||
|
||
azure_resource_uri = _get_azure_resource_uri(website_site_name) | ||
if azure_resource_uri: | ||
attributes[_CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE] = azure_resource_uri | ||
for (key, env_var) in _APP_SERVICE_ATTRIBUTE_ENV_VARS.items(): | ||
value = environ.get(env_var) | ||
if value: | ||
attributes[key] = value | ||
|
||
return Resource(attributes) | ||
|
||
def _get_azure_resource_uri(website_site_name): | ||
website_resource_group = environ.get(_WEBSITE_RESOURCE_GROUP) | ||
website_owner_name = environ.get(_WEBSITE_OWNER_NAME) | ||
|
||
subscription_id = website_owner_name | ||
if website_owner_name and '+' in website_owner_name: | ||
subscription_id = website_owner_name[0:website_owner_name.index('+')] | ||
|
||
if not (website_resource_group and subscription_id): | ||
return None | ||
|
||
return "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Web/sites/%s" % ( | ||
subscription_id, | ||
website_resource_group, | ||
website_site_name, | ||
) |
4 changes: 4 additions & 0 deletions
4
...pentelemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/version.py
This file contains hidden or 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,4 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
__version__ = "0.41b0.dev" |
85 changes: 85 additions & 0 deletions
85
...rce/opentelemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/vm.py
This file contains hidden or 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,85 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
from json import loads | ||
from logging import getLogger | ||
from os import environ | ||
from urllib.request import Request, urlopen | ||
from urllib.error import URLError | ||
|
||
from opentelemetry.sdk.resources import ResourceDetector, Resource | ||
from opentelemetry.semconv.resource import ResourceAttributes, CloudPlatformValues, CloudProviderValues | ||
|
||
|
||
# TODO: Remove when cloud resource id is no longer missing in Resource Attributes | ||
_CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE = "cloud.resource_id" | ||
_AZURE_VM_METADATA_ENDPOINT = "http://169.254.169.254/metadata/instance/compute?api-version=2021-12-13&format=json" | ||
_AZURE_VM_SCALE_SET_NAME_ATTRIBUTE = "azure.vm.scaleset.name" | ||
_AZURE_VM_SKU_ATTRIBUTE = "azure.vm.sku" | ||
_logger = getLogger(__name__) | ||
|
||
EXPECTED_AZURE_AMS_ATTRIBUTES = [ | ||
_AZURE_VM_SCALE_SET_NAME_ATTRIBUTE, | ||
_AZURE_VM_SKU_ATTRIBUTE, | ||
ResourceAttributes.CLOUD_PLATFORM, | ||
ResourceAttributes.CLOUD_PROVIDER, | ||
ResourceAttributes.CLOUD_REGION, | ||
_CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE, | ||
ResourceAttributes.HOST_ID, | ||
ResourceAttributes.HOST_NAME, | ||
ResourceAttributes.HOST_TYPE, | ||
ResourceAttributes.OS_TYPE, | ||
ResourceAttributes.OS_VERSION, | ||
ResourceAttributes.SERVICE_INSTANCE_ID, | ||
] | ||
|
||
class AzureVMResourceDetector(ResourceDetector): | ||
# pylint: disable=no-self-use | ||
def detect(self) -> "Resource": | ||
attributes = {} | ||
metadata_json = _AzureVMMetadataServiceRequestor().get_azure_vm_metadata() | ||
if not metadata_json: | ||
return Resource(attributes) | ||
for attribute_key in EXPECTED_AZURE_AMS_ATTRIBUTES: | ||
attributes[attribute_key] = _AzureVMMetadataServiceRequestor().get_attribute_from_metadata(metadata_json, attribute_key) | ||
return Resource(attributes) | ||
|
||
class _AzureVMMetadataServiceRequestor: | ||
def get_azure_vm_metadata(self): | ||
request = Request(_AZURE_VM_METADATA_ENDPOINT) | ||
request.add_header("Metadata", "True") | ||
try: | ||
response = urlopen(request).read() | ||
return loads(response) | ||
except URLError: | ||
# Not on Azure VM | ||
return None | ||
except Exception as e: | ||
_logger.exception("Failed to receive Azure VM metadata: %s", e) | ||
return None | ||
|
||
def get_attribute_from_metadata(self, metadata_json, attribute_key): | ||
ams_value = "" | ||
if attribute_key == _AZURE_VM_SCALE_SET_NAME_ATTRIBUTE: | ||
ams_value = metadata_json["vmScaleSetName"] | ||
elif attribute_key == _AZURE_VM_SKU_ATTRIBUTE: | ||
ams_value = metadata_json["sku"] | ||
elif attribute_key == ResourceAttributes.CLOUD_PLATFORM: | ||
ams_value = CloudPlatformValues.AZURE_VM.value | ||
elif attribute_key == ResourceAttributes.CLOUD_PROVIDER: | ||
ams_value = CloudProviderValues.AZURE.value | ||
elif attribute_key == ResourceAttributes.CLOUD_REGION: | ||
ams_value = metadata_json["location"] | ||
elif attribute_key == _CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE: | ||
ams_value = metadata_json["resourceId"] | ||
elif attribute_key == ResourceAttributes.HOST_ID or \ | ||
attribute_key == ResourceAttributes.SERVICE_INSTANCE_ID: | ||
ams_value = metadata_json["vmId"] | ||
elif attribute_key == ResourceAttributes.HOST_NAME: | ||
ams_value = metadata_json["name"] | ||
elif attribute_key == ResourceAttributes.HOST_TYPE: | ||
ams_value = metadata_json["vmSize"] | ||
elif attribute_key == ResourceAttributes.OS_TYPE: | ||
ams_value = metadata_json["osType"] | ||
elif attribute_key == ResourceAttributes.OS_VERSION: | ||
ams_value = metadata_json["version"] | ||
return ams_value |
2 changes: 2 additions & 0 deletions
2
resource/opentelemetry-resource-detector-azure/tests/__init__.py
This file contains hidden or 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,2 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. |
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.
Uh oh!
There was an error while loading. Please reload this page.