|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
| 15 | +from logging import getLogger |
15 | 16 | from os import environ
|
16 | 17 |
|
17 | 18 | from opentelemetry.sdk.resources import ResourceDetector, Resource
|
|
26 | 27 | _WEBSITE_RESOURCE_GROUP = "WEBSITE_RESOURCE_GROUP"
|
27 | 28 | _WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME"
|
28 | 29 | _WEBSITE_SLOT_NAME = "WEBSITE_SLOT_NAME"
|
| 30 | +_logger = getLogger(__name__) |
29 | 31 |
|
30 | 32 |
|
31 | 33 | _APP_SERVICE_ATTRIBUTE_ENV_VARS = {
|
|
39 | 41 | class AzureAppServiceResourceDetector(ResourceDetector):
|
40 | 42 | def detect(self) -> Resource:
|
41 | 43 | attributes = {}
|
42 |
| - website_site_name = environ.get(_WEBSITE_SITE_NAME) |
43 |
| - if website_site_name: |
44 |
| - attributes[ResourceAttributes.SERVICE_NAME] = website_site_name |
45 |
| - attributes[ResourceAttributes.CLOUD_PROVIDER] = CloudProviderValues.AZURE.value |
46 |
| - attributes[ResourceAttributes.CLOUD_PLATFORM] = CloudPlatformValues.AZURE_APP_SERVICE.value |
47 |
| - |
48 |
| - azure_resource_uri = _get_azure_resource_uri(website_site_name) |
49 |
| - if azure_resource_uri: |
50 |
| - attributes[ResourceAttributes.CLOUD_RESOURCE_ID] = azure_resource_uri |
51 |
| - for (key, env_var) in _APP_SERVICE_ATTRIBUTE_ENV_VARS.items(): |
52 |
| - value = environ.get(env_var) |
53 |
| - if value: |
54 |
| - attributes[key] = value |
| 44 | + try: |
| 45 | + website_site_name = environ.get(_WEBSITE_SITE_NAME) |
| 46 | + if website_site_name: |
| 47 | + attributes[ResourceAttributes.SERVICE_NAME] = website_site_name |
| 48 | + attributes[ResourceAttributes.CLOUD_PROVIDER] = CloudProviderValues.AZURE.value |
| 49 | + attributes[ResourceAttributes.CLOUD_PLATFORM] = CloudPlatformValues.AZURE_APP_SERVICE.value |
55 | 50 |
|
| 51 | + azure_resource_uri = _get_azure_resource_uri(website_site_name) |
| 52 | + if azure_resource_uri: |
| 53 | + attributes[ResourceAttributes.CLOUD_RESOURCE_ID] = azure_resource_uri |
| 54 | + for (key, env_var) in _APP_SERVICE_ATTRIBUTE_ENV_VARS.items(): |
| 55 | + value = environ.get(env_var) |
| 56 | + if value: |
| 57 | + attributes[key] = value |
| 58 | + except Exception as e: |
| 59 | + _logger.info("Could not detect Azure App Service metadata: %s", e) |
56 | 60 | return Resource(attributes)
|
57 | 61 |
|
58 | 62 | def _get_azure_resource_uri(website_site_name):
|
|
0 commit comments