Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
46 changes: 46 additions & 0 deletions .github/scripts/install_azure_functions_worker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash
# 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.

set -euo pipefail

# Create build dir
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
BUILD_DIR="${TOX_ENV_DIR:-${SCRIPT_DIR}}/build/azure-functions-worker"
rm -rf ${BUILD_DIR}
mkdir -p ${BUILD_DIR}

# Clone repository
git clone https://github.com/Azure/azure-functions-python-worker.git ${BUILD_DIR}

# Setup virtual environment and install dependencies
python -m venv "${BUILD_DIR}/.venv"
PYTHON="${BUILD_DIR}/.venv/bin/python"
PIP="${BUILD_DIR}/.venv/bin/pip"
PIPCOMPILE="${BUILD_DIR}/.venv/bin/pip-compile"
INVOKE="${BUILD_DIR}/.venv/bin/invoke"
${PIP} install pip-tools build invoke

# Install proto build dependencies
$(cd ${BUILD_DIR} && ${PIPCOMPILE} >${BUILD_DIR}/requirements.txt)
${PIP} install -r ${BUILD_DIR}/requirements.txt

# Build proto files into pb2 files
cd ${BUILD_DIR}/tests && ${INVOKE} -c test_setup build-protos

# Build and install the package into the original environment (not the build venv)
pip install ${BUILD_DIR}

# Clean up and return to the original directory
rm -rf ${BUILD_DIR}
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ on:
schedule:
- cron: "0 15 * * *"

permissions:
contents: read

concurrency:
group: ${{ github.ref || github.run_id }}-${{ github.workflow }}
cancel-in-progress: true
Expand Down
39 changes: 39 additions & 0 deletions newrelic/common/utilization.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

_logger = logging.getLogger(__name__)
VALID_CHARS_RE = re.compile(r"[0-9a-zA-Z_ ./-]")
AZURE_RESOURCE_GROUP_NAME_RE = re.compile(r"\+([a-zA-Z0-9\-]+)-[a-zA-Z0-9]+(?:-Linux)")
AZURE_RESOURCE_GROUP_NAME_PARTIAL_RE = re.compile(r"\+([a-zA-Z0-9\-]+)(?:-Linux)?-[a-zA-Z0-9]+")


class UtilizationHttpClient(InsecureHttpClient):
Expand Down Expand Up @@ -207,6 +209,43 @@ class AzureUtilization(CommonUtilization):
VENDOR_NAME = "azure"


class AzureFunctionUtilization(CommonUtilization):
METADATA_HOST = "169.254.169.254"
METADATA_PATH = "/metadata/instance/compute"
METADATA_QUERY = {"api-version": "2017-03-01"}
EXPECTED_KEYS = ("faas.app_name", "cloud.region")
HEADERS = {"Metadata": "true"}
VENDOR_NAME = "azurefunction"

@staticmethod
def fetch():
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)

@classmethod
def get_values(cls, response):
if response is None or len(response) != 2:
return

values = {}
for k, v in zip(cls.EXPECTED_KEYS, response):
if hasattr(v, "decode"):
v = v.decode("utf-8")
values[k] = v
return values


class GCPUtilization(CommonUtilization):
EXPECTED_KEYS = ("id", "machineType", "name", "zone")
HEADERS = {"Metadata-Flavor": "Google"}
Expand Down
9 changes: 9 additions & 0 deletions newrelic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ def _process_configuration(section):
_process_setting(section, "process_host.display_name", "get", None)
_process_setting(section, "utilization.detect_aws", "getboolean", None)
_process_setting(section, "utilization.detect_azure", "getboolean", None)
_process_setting(section, "utilization.detect_azurefunction", "getboolean", None)
_process_setting(section, "utilization.detect_docker", "getboolean", None)
_process_setting(section, "utilization.detect_kubernetes", "getboolean", None)
_process_setting(section, "utilization.detect_gcp", "getboolean", None)
Expand Down Expand Up @@ -4091,6 +4092,14 @@ def _process_module_builtin_defaults():
)
_process_module_definition("tornado.routing", "newrelic.hooks.framework_tornado", "instrument_tornado_routing")
_process_module_definition("tornado.web", "newrelic.hooks.framework_tornado", "instrument_tornado_web")
_process_module_definition(
"azure.functions._http", "newrelic.hooks.framework_azurefunctions", "instrument_azure_function__http"
)
_process_module_definition(
"azure_functions_worker.dispatcher",
"newrelic.hooks.framework_azurefunctions",
"instrument_azure_functions_worker_dispatcher",
)


def _process_module_entry_points():
Expand Down
5 changes: 4 additions & 1 deletion newrelic/core/agent_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from newrelic.common.encoding_utils import json_decode, json_encode, serverless_payload_encode
from newrelic.common.utilization import (
AWSUtilization,
AzureFunctionUtilization,
AzureUtilization,
DockerUtilization,
ECSUtilization,
Expand Down Expand Up @@ -302,7 +303,7 @@ def _connect_payload(app_name, linked_applications, environment, settings):

utilization_settings = {}
# metadata_version corresponds to the utilization spec being used.
utilization_settings["metadata_version"] = 5
utilization_settings["metadata_version"] = 6
utilization_settings["logical_processors"] = system_info.logical_processor_count()
utilization_settings["total_ram_mib"] = system_info.total_physical_memory()
utilization_settings["hostname"] = hostname
Expand Down Expand Up @@ -342,6 +343,8 @@ def _connect_payload(app_name, linked_applications, environment, settings):
vendors.append(GCPUtilization)
if settings["utilization.detect_azure"]:
vendors.append(AzureUtilization)
if settings["utilization.detect_azurefunction"]:
vendors.append(AzureFunctionUtilization)

for vendor in vendors:
metadata = vendor.detect()
Expand Down
5 changes: 5 additions & 0 deletions newrelic/core/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,11 @@ def connect_to_data_collector(self, activate_agent):
if self._agent_control.health_check_enabled:
internal_metric("Supportability/AgentControl/Health/enabled", 1)

# Azure Function mode metric
# Note: This environment variable will be set by the Azure Functions runtime
if os.environ.get("FUNCTIONS_WORKER_RUNTIME", None):
internal_metric("Supportability/Python/AzureFunctionMode/enabled", 1)

self._stats_engine.merge_custom_metrics(internal_metrics.metrics())

# Update the active session in this object. This will the
Expand Down
4 changes: 4 additions & 0 deletions newrelic/core/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
"error.expected",
"error.group.name",
"error.message",
"faas.name",
"faas.trigger",
"faas.invocation_id",
"faas.coldStart",
"graphql.field.name",
"graphql.field.parentType",
"graphql.field.path",
Expand Down
1 change: 1 addition & 0 deletions newrelic/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,7 @@ def default_otlp_host(host):

_settings.utilization.detect_aws = True
_settings.utilization.detect_azure = True
_settings.utilization.detect_azurefunction = True
_settings.utilization.detect_docker = True
_settings.utilization.detect_kubernetes = True
_settings.utilization.detect_gcp = True
Expand Down
Loading
Loading