Skip to content
Merged
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
28 changes: 18 additions & 10 deletions newrelic/common/utilization.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,29 @@ class AzureFunctionUtilization(CommonUtilization):
HEADERS = {"Metadata": "true"} # noqa: RUF012
VENDOR_NAME = "azurefunction"

@staticmethod
def fetch():
@classmethod
def fetch(cls):
cloud_region = os.environ.get("REGION_NAME")
website_owner_name = os.environ.get("WEBSITE_OWNER_NAME")
azure_function_app_name = os.environ.get("WEBSITE_SITE_NAME")

if all((cloud_region, website_owner_name, azure_function_app_name)):
if website_owner_name.endswith("-Linux"):
resource_group_name = AZURE_RESOURCE_GROUP_NAME_RE.search(website_owner_name).group(1)
else:
resource_group_name = AZURE_RESOURCE_GROUP_NAME_PARTIAL_RE.search(website_owner_name).group(1)
subscription_id = re.search(r"(?:(?!\+).)*", website_owner_name).group(0)
faas_app_name = f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Web/sites/{azure_function_app_name}"
# Only send if all values are present
return (faas_app_name, cloud_region)
try:
if website_owner_name.endswith("-Linux"):
resource_group_name = AZURE_RESOURCE_GROUP_NAME_RE.search(website_owner_name).group(1)
else:
resource_group_name = AZURE_RESOURCE_GROUP_NAME_PARTIAL_RE.search(website_owner_name).group(1)
subscription_id = re.search(r"(?:(?!\+).)*", website_owner_name).group(0)
faas_app_name = f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Web/sites/{azure_function_app_name}"
# Only send if all values are present
return (faas_app_name, cloud_region)
except Exception:
_logger.debug(
"Unable to determine Azure Functions subscription id from WEBSITE_OWNER_NAME. %r",
website_owner_name,
)

return None

@classmethod
def get_values(cls, response):
Expand Down
40 changes: 40 additions & 0 deletions tests/framework_azurefunctions/test_utilization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from newrelic.common.utilization import AzureFunctionUtilization


def test_utilization(monkeypatch):
monkeypatch.setenv("REGION_NAME", "eastus2")
monkeypatch.setenv(
"WEBSITE_OWNER_NAME", "0b0d165f-aaaf-4a3b-b929-5f60588d95a3+testing-python-EastUS2webspace-Linux"
)
monkeypatch.setenv("WEBSITE_SITE_NAME", "test-func-linux")

result = AzureFunctionUtilization.fetch()
assert result, "Failed to parse utilization for Azure Functions."

faas_app_name, cloud_region = result
expected_faas_app_name = "/subscriptions/0b0d165f-aaaf-4a3b-b929-5f60588d95a3/resourceGroups/testing-python/providers/Microsoft.Web/sites/test-func-linux"
assert faas_app_name == expected_faas_app_name
assert cloud_region == "eastus2"


def test_utilization_bad_website_owner_name(monkeypatch):
monkeypatch.setenv("REGION_NAME", "eastus2")
monkeypatch.setenv("WEBSITE_OWNER_NAME", "ERROR")
monkeypatch.setenv("WEBSITE_SITE_NAME", "test-func-linux")

result = AzureFunctionUtilization.fetch()
assert result is None, f"Expected failure but got result instead. {result}"
Loading