-
Notifications
You must be signed in to change notification settings - Fork 1k
Add Azure Kubernetes Service resource detector #5012
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
Open
JacksonWeber
wants to merge
10
commits into
open-telemetry:main
Choose a base branch
from
JacksonWeber:add-aks-resource-detector
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a08b59d
Add Azure Kubernetes Service resource detector
JacksonWeber e0bd961
Add changelog fragment for AKS detector
JacksonWeber 70bf31f
Merge upstream main into add-aks-resource-detector
JacksonWeber beca0da
Fix AKS detector CI checks
JacksonWeber ce3182a
Use current AKS semantic conventions
JacksonWeber 7a6870e
Add AKS cloud account ID
JacksonWeber b4d2222
Stop reporting AKS cluster name
JacksonWeber 6205503
Merge remote AKS detector updates
JacksonWeber 04c2191
Rerun CI after transient ASGI failure
JacksonWeber 6cb8f62
Merge branch 'main' into add-aks-resource-detector
JacksonWeber 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| [codespell] | ||
| # skipping auto generated folders | ||
| skip = ./.tox,./.mypy_cache,./docs/_build,./target,*/LICENSE,./venv,*/cassettes | ||
| ignore-words-list = ot | ||
| ignore-words-list = aks,ot | ||
1 change: 1 addition & 0 deletions
1
resource/opentelemetry-resource-detector-azure/.changelog/5012.added
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 @@ | ||
| `opentelemetry-resource-detector-azure`: add AKS resource detector |
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
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
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
86 changes: 86 additions & 0 deletions
86
...ce/opentelemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/aks.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,86 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from logging import getLogger | ||
| from os import environ | ||
| from pathlib import Path | ||
|
|
||
| from opentelemetry.sdk.resources import Resource, ResourceDetector | ||
|
|
||
| from ._constants import ( | ||
| _AKS_CLUSTER_RESOURCE_ID, | ||
| _AKS_CLUSTER_RESOURCE_ID_KEY, | ||
| _AKS_METADATA_FILE_PATH, | ||
| ) | ||
|
|
||
| _logger = getLogger(__name__) | ||
|
|
||
| _CLOUD_ACCOUNT_ID = "cloud.account.id" | ||
| _CLOUD_PLATFORM = "cloud.platform" | ||
| _CLOUD_PROVIDER = "cloud.provider" | ||
| _CLOUD_RESOURCE_ID = "cloud.resource_id" | ||
|
|
||
|
|
||
| def _extract_subscription_id(resource_id: str) -> str | None: | ||
| segments = resource_id.split("/") | ||
| for index, segment in enumerate(segments): | ||
| if segment.lower() == "subscriptions" and index < len(segments) - 1: | ||
| return segments[index + 1] or None | ||
| return None | ||
|
|
||
|
|
||
| def _parse_aks_metadata(content: str) -> str | None: | ||
| keyed_resource_id: str | None = None | ||
| bare_values: list[str] = [] | ||
|
|
||
| for line in content.splitlines(): | ||
| stripped_line = line.strip().lstrip("\ufeff") | ||
| if not stripped_line or stripped_line.startswith("#"): | ||
| continue | ||
|
|
||
| key, separator, value = stripped_line.partition("=") | ||
| if not separator: | ||
| bare_values.append(stripped_line) | ||
| elif key.strip() == _AKS_CLUSTER_RESOURCE_ID_KEY and value.strip(): | ||
| keyed_resource_id = value.strip() | ||
|
|
||
| if keyed_resource_id: | ||
| return keyed_resource_id | ||
| if len(bare_values) == 1: | ||
| return bare_values[0] | ||
| return None | ||
|
|
||
|
|
||
| def _get_aks_metadata_from_file() -> str | None: | ||
| metadata_path = Path(_AKS_METADATA_FILE_PATH) | ||
| try: | ||
| if metadata_path.is_dir(): | ||
| metadata_path = metadata_path / _AKS_CLUSTER_RESOURCE_ID_KEY | ||
| content = metadata_path.read_text(encoding="utf-8") | ||
| except (OSError, UnicodeError): | ||
| _logger.debug( | ||
| "Failed to read AKS metadata from %s", | ||
| metadata_path, | ||
| exc_info=True, | ||
| ) | ||
| return None | ||
|
|
||
| return _parse_aks_metadata(content) | ||
|
|
||
|
|
||
| class AzureAKSResourceDetector(ResourceDetector): | ||
| def detect(self) -> Resource: | ||
| resource_id = environ.get(_AKS_CLUSTER_RESOURCE_ID) or _get_aks_metadata_from_file() | ||
| if not resource_id: | ||
| return Resource({}) | ||
|
|
||
| attributes = { | ||
| _CLOUD_PROVIDER: "azure", | ||
| _CLOUD_PLATFORM: "azure.aks", | ||
| _CLOUD_RESOURCE_ID: resource_id, | ||
| } | ||
| subscription_id = _extract_subscription_id(resource_id) | ||
| if subscription_id: | ||
| attributes[_CLOUD_ACCOUNT_ID] = subscription_id | ||
|
|
||
| return Resource(attributes) |
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.
Uh oh!
There was an error while loading. Please reload this page.