|
26 | 26 |
|
27 | 27 | _logger = logging.getLogger(__name__) |
28 | 28 | VALID_CHARS_RE = re.compile(r"[0-9a-zA-Z_ ./-]") |
| 29 | +AZURE_RESOURCE_GROUP_NAME_RE = re.compile(r"\+([a-zA-Z0-9\-]+)-[a-zA-Z0-9]+(?:-Linux)") |
| 30 | +AZURE_RESOURCE_GROUP_NAME_PARTIAL_RE = re.compile(r"\+([a-zA-Z0-9\-]+)(?:-Linux)?-[a-zA-Z0-9]+") |
29 | 31 |
|
30 | 32 |
|
31 | 33 | class UtilizationHttpClient(InsecureHttpClient): |
@@ -207,6 +209,43 @@ class AzureUtilization(CommonUtilization): |
207 | 209 | VENDOR_NAME = "azure" |
208 | 210 |
|
209 | 211 |
|
| 212 | +class AzureFunctionUtilization(CommonUtilization): |
| 213 | + METADATA_HOST = "169.254.169.254" |
| 214 | + METADATA_PATH = "/metadata/instance/compute" |
| 215 | + METADATA_QUERY = {"api-version": "2017-03-01"} |
| 216 | + EXPECTED_KEYS = ("faas.app_name", "cloud.region") |
| 217 | + HEADERS = {"Metadata": "true"} |
| 218 | + VENDOR_NAME = "azurefunction" |
| 219 | + |
| 220 | + @staticmethod |
| 221 | + def fetch(): |
| 222 | + cloud_region = os.environ.get("REGION_NAME") |
| 223 | + website_owner_name = os.environ.get("WEBSITE_OWNER_NAME") |
| 224 | + azure_function_app_name = os.environ.get("WEBSITE_SITE_NAME") |
| 225 | + |
| 226 | + if all((cloud_region, website_owner_name, azure_function_app_name)): |
| 227 | + if website_owner_name.endswith("-Linux"): |
| 228 | + resource_group_name = AZURE_RESOURCE_GROUP_NAME_RE.search(website_owner_name).group(1) |
| 229 | + else: |
| 230 | + resource_group_name = AZURE_RESOURCE_GROUP_NAME_PARTIAL_RE.search(website_owner_name).group(1) |
| 231 | + subscription_id = re.search(r"(?:(?!\+).)*", website_owner_name).group(0) |
| 232 | + faas_app_name = f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Web/sites/{azure_function_app_name}" |
| 233 | + # Only send if all values are present |
| 234 | + return (faas_app_name, cloud_region) |
| 235 | + |
| 236 | + @classmethod |
| 237 | + def get_values(cls, response): |
| 238 | + if response is None or len(response) != 2: |
| 239 | + return |
| 240 | + |
| 241 | + values = {} |
| 242 | + for k, v in zip(cls.EXPECTED_KEYS, response): |
| 243 | + if hasattr(v, "decode"): |
| 244 | + v = v.decode("utf-8") |
| 245 | + values[k] = v |
| 246 | + return values |
| 247 | + |
| 248 | + |
210 | 249 | class GCPUtilization(CommonUtilization): |
211 | 250 | EXPECTED_KEYS = ("id", "machineType", "name", "zone") |
212 | 251 | HEADERS = {"Metadata-Flavor": "Google"} |
|
0 commit comments