Skip to content

Commit 351efa3

Browse files
committed
Use WEBSITE_RESOURCE_GROUP as primary info source
1 parent 2086a9e commit 351efa3

File tree

3 files changed

+15
-42
lines changed

3 files changed

+15
-42
lines changed

newrelic/common/utilization.py

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -231,36 +231,19 @@ class AzureFunctionUtilization(CommonUtilization):
231231
HEADERS = {"Metadata": "true"} # noqa: RUF012
232232
VENDOR_NAME = "azurefunction"
233233

234-
RESOURCE_GROUP_NAME_RE = re.compile(r"\+([a-zA-Z0-9\-]+)-[a-zA-Z0-9]+(?:-Linux)")
235-
RESOURCE_GROUP_NAME_PARTIAL_RE = re.compile(r"\+([a-zA-Z0-9\-]+)(?:-Linux)?-[a-zA-Z0-9]+")
236-
SUBSCRIPTION_ID_RE = re.compile(r"(?:(?!\+).)*")
237-
238234
@classmethod
239235
def fetch(cls):
240236
cloud_region = os.environ.get("REGION_NAME")
241237
website_owner_name = os.environ.get("WEBSITE_OWNER_NAME")
242238
azure_function_app_name = os.environ.get("WEBSITE_SITE_NAME")
243-
244-
if all((cloud_region, website_owner_name, azure_function_app_name)):
245-
resource_group_name_re = (
246-
cls.RESOURCE_GROUP_NAME_RE
247-
if website_owner_name.endswith("-Linux")
248-
else cls.RESOURCE_GROUP_NAME_PARTIAL_RE
249-
)
250-
match = resource_group_name_re.search(website_owner_name)
251-
if match:
252-
resource_group_name = match.group(1)
253-
else:
254-
_logger.debug(
255-
"Unable to determine Azure Functions resource group name from WEBSITE_OWNER_NAME. %r",
256-
website_owner_name,
257-
)
258-
return None
259-
260-
match = cls.SUBSCRIPTION_ID_RE.search(website_owner_name)
261-
if match:
262-
subscription_id = match.group(0)
263-
else:
239+
resource_group_name = os.environ.get("WEBSITE_RESOURCE_GROUP")
240+
241+
if all((cloud_region, website_owner_name, azure_function_app_name, resource_group_name)):
242+
subscription_id = ""
243+
if "+" in website_owner_name:
244+
subscription_id = website_owner_name.split("+")[0]
245+
# Catch missing subscription_id or missing +
246+
if not subscription_id:
264247
_logger.debug(
265248
"Unable to determine Azure Functions subscription id from WEBSITE_OWNER_NAME. %r",
266249
website_owner_name,

newrelic/hooks/framework_azurefunctions.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,13 @@
1313
# limitations under the License.
1414

1515
import os
16-
import re
1716
import urllib.parse as urlparse
1817

1918
from newrelic.api.application import application_instance
2019
from newrelic.api.transaction import current_transaction
2120
from newrelic.api.web_transaction import WebTransaction
2221
from newrelic.common.object_wrapper import wrap_function_wrapper
2322
from newrelic.common.signature import bind_args
24-
from newrelic.common.utilization import AzureFunctionUtilization
25-
26-
RESOURCE_GROUP_NAME_PARTIAL_RE = AzureFunctionUtilization.RESOURCE_GROUP_NAME_PARTIAL_RE
27-
RESOURCE_GROUP_NAME_RE = AzureFunctionUtilization.RESOURCE_GROUP_NAME_RE
2823

2924

3025
def original_agent_instance():
@@ -38,18 +33,12 @@ def intrinsics_populator(application, context):
3833
trigger_type = "http"
3934

4035
website_owner_name = os.environ.get("WEBSITE_OWNER_NAME", None)
41-
if not website_owner_name:
42-
subscription_id = "Unknown"
43-
else:
44-
subscription_id = re.search(r"(?:(?!\+).)*", website_owner_name) and re.search(
45-
r"(?:(?!\+).)*", website_owner_name
46-
).group(0)
47-
if website_owner_name and website_owner_name.endswith("-Linux"):
48-
resource_group_name = RESOURCE_GROUP_NAME_RE.search(website_owner_name).group(1)
49-
elif website_owner_name:
50-
resource_group_name = RESOURCE_GROUP_NAME_PARTIAL_RE.search(website_owner_name).group(1)
51-
else:
52-
resource_group_name = os.environ.get("WEBSITE_RESOURCE_GROUP", "Unknown")
36+
37+
subscription_id = "Unknown"
38+
if website_owner_name and "+" in website_owner_name:
39+
subscription_id = website_owner_name.split("+")[0] or "Unknown"
40+
41+
resource_group_name = os.environ.get("WEBSITE_RESOURCE_GROUP", "Unknown")
5342
azure_function_app_name = os.environ.get("WEBSITE_SITE_NAME", getattr(application, "name", "Azure Function App"))
5443

5544
cloud_resource_id = f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Web/sites/{azure_function_app_name}/functions/{getattr(context, 'function_name', 'Unknown')}"

tests/framework_azurefunctions/test_utilization.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
def test_utilization(monkeypatch):
1919
monkeypatch.setenv("REGION_NAME", "EastUS2")
20+
monkeypatch.setenv("WEBSITE_RESOURCE_GROUP", "testing-rg")
2021
monkeypatch.setenv("WEBSITE_OWNER_NAME", "b999997b-cb91-49e0-b922-c9188372bdba+testing-rg-EastUS2webspace-Linux")
2122
monkeypatch.setenv("WEBSITE_SITE_NAME", "test-func-linux")
2223

0 commit comments

Comments
 (0)