Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(azure-functions-resource-detector): Update Azure Functions Detector to Consider WEBSITE_SKU #2251

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
WEBSITE_SITE_NAME,
WEBSITE_SLOT_NAME,
CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE,
FUNCTIONS_VERSION,
} from '../types';
import {
SEMRESATTRS_CLOUD_REGION,
Expand All @@ -37,7 +36,7 @@ import {
CLOUDPROVIDERVALUES_AZURE,
CLOUDPLATFORMVALUES_AZURE_APP_SERVICE,
} from '@opentelemetry/semantic-conventions';
import { getAzureResourceUri } from '../utils';
import { getAzureResourceUri, isAzureFunction } from '../utils';

const APP_SERVICE_ATTRIBUTE_ENV_VARS = {
[SEMRESATTRS_CLOUD_REGION]: REGION_NAME,
Expand All @@ -55,8 +54,7 @@ class AzureAppServiceDetector implements DetectorSync {
detect(): IResource {
let attributes = {};
const websiteSiteName = process.env[WEBSITE_SITE_NAME];
const isAzureFunction = !!process.env[FUNCTIONS_VERSION];
if (websiteSiteName && !isAzureFunction) {
if (websiteSiteName && !isAzureFunction()) {
attributes = {
...attributes,
[SEMRESATTRS_SERVICE_NAME]: websiteSiteName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ import {
} from '@opentelemetry/semantic-conventions';
import {
WEBSITE_SITE_NAME,
FUNCTIONS_VERSION,
WEBSITE_INSTANCE_ID,
FUNCTIONS_MEM_LIMIT,
REGION_NAME,
CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE,
} from '../types';
import { getAzureResourceUri } from '../utils';
import { getAzureResourceUri, isAzureFunction } from '../utils';

const AZURE_FUNCTIONS_ATTRIBUTE_ENV_VARS = {
[SEMRESATTRS_SERVICE_NAME]: WEBSITE_SITE_NAME,
Expand All @@ -51,13 +50,13 @@ class AzureFunctionsDetector implements DetectorSync {
detect(): IResource {
let attributes = {};
const serviceName = process.env[WEBSITE_SITE_NAME];
const functionVersion = process.env[FUNCTIONS_VERSION];

/**
* Checks that we are operating within an Azure Function using the function version since WEBSITE_SITE_NAME
* will exist in Azure App Service as well and detectors should be mutually exclusive.
* If the function version is not present, we check for the website sku to determine if it is a function.
*/
if (serviceName && functionVersion) {
if (serviceName && isAzureFunction()) {
const functionInstance = process.env[WEBSITE_INSTANCE_ID];
const functionMemLimit = process.env[FUNCTIONS_MEM_LIMIT];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const WEBSITE_SITE_NAME = 'WEBSITE_SITE_NAME';
export const WEBSITE_SLOT_NAME = 'WEBSITE_SLOT_NAME';

export const FUNCTIONS_VERSION = 'FUNCTIONS_EXTENSION_VERSION';
export const WEBSITE_SKU = 'WEBSITE_SKU';
JacksonWeber marked this conversation as resolved.
Show resolved Hide resolved
export const FUNCTIONS_MEM_LIMIT = 'WEBSITE_MEMORY_LIMIT_MB';

export const AZURE_VM_METADATA_HOST = '169.254.169.254';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
* limitations under the License.
*/

import { WEBSITE_OWNER_NAME, WEBSITE_RESOURCE_GROUP } from './types';
import {
FUNCTIONS_VERSION,
WEBSITE_OWNER_NAME,
WEBSITE_RESOURCE_GROUP,
WEBSITE_SKU,
} from './types';

export function getAzureResourceUri(
websiteSiteName: string
Expand All @@ -33,3 +38,10 @@ export function getAzureResourceUri(

return `/subscriptions/${subscriptionId}/resourceGroups/${websiteResourceGroup}/providers/Microsoft.Web/sites/${websiteSiteName}`;
}

export function isAzureFunction(): boolean {
return !!(
process.env[FUNCTIONS_VERSION] ||
process.env[WEBSITE_SKU] === 'FlexConsumption'
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,26 @@ describe('AzureFunctionsDetector', () => {
);
});
});

it('should detect azure functions if websiteSku is defined as FlexConsumption', () => {
process.env.WEBSITE_SITE_NAME = 'test-service';
process.env.REGION_NAME = 'test-region';
process.env.WEBSITE_INSTANCE_ID = 'test-instance-id';
process.env.WEBSITE_SKU = 'FlexConsumption';
process.env.WEBSITE_MEMORY_LIMIT_MB = '1000';
process.env.WEBSITE_OWNER_NAME = 'test-owner-name';
process.env.WEBSITE_RESOURCE_GROUP = 'test-resource-group';
JacksonWeber marked this conversation as resolved.
Show resolved Hide resolved

const resource = detectResourcesSync({
detectors: [azureFunctionsDetector, azureAppServiceDetector],
});
assert.ok(resource);
const attributes = resource.attributes;
assert.strictEqual(attributes[SEMRESATTRS_SERVICE_NAME], 'test-service');
assert.strictEqual(attributes[SEMRESATTRS_CLOUD_PROVIDER], 'azure');

// Should not detect app service values
assert.strictEqual(attributes[SEMRESATTRS_SERVICE_INSTANCE_ID], undefined);
assert.strictEqual(attributes[SEMRESATTRS_PROCESS_PID], process.pid);
delete process.env.WEBSITE_SKU;
});
Loading