Skip to content

Commit 93776fa

Browse files
JacksonWeberhectorhdzgpichlermarc
authored
fix(azure-functions-resource-detector): Update Azure Functions Detector to Consider WEBSITE_SKU (#2251)
* fix: Update the functions detector check to include SKU. * Resolve PR comments. --------- Co-authored-by: Hector Hernandez <[email protected]> Co-authored-by: Marc Pichler <[email protected]>
1 parent cac5cc9 commit 93776fa

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

detectors/node/opentelemetry-resource-detector-azure/src/detectors/AzureAppServiceDetector.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
WEBSITE_SITE_NAME,
2525
WEBSITE_SLOT_NAME,
2626
CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE,
27-
FUNCTIONS_VERSION,
2827
} from '../types';
2928
import {
3029
SEMRESATTRS_CLOUD_REGION,
@@ -37,7 +36,7 @@ import {
3736
CLOUDPROVIDERVALUES_AZURE,
3837
CLOUDPLATFORMVALUES_AZURE_APP_SERVICE,
3938
} from '@opentelemetry/semantic-conventions';
40-
import { getAzureResourceUri } from '../utils';
39+
import { getAzureResourceUri, isAzureFunction } from '../utils';
4140

4241
const APP_SERVICE_ATTRIBUTE_ENV_VARS = {
4342
[SEMRESATTRS_CLOUD_REGION]: REGION_NAME,
@@ -55,8 +54,7 @@ class AzureAppServiceDetector implements DetectorSync {
5554
detect(): IResource {
5655
let attributes = {};
5756
const websiteSiteName = process.env[WEBSITE_SITE_NAME];
58-
const isAzureFunction = !!process.env[FUNCTIONS_VERSION];
59-
if (websiteSiteName && !isAzureFunction) {
57+
if (websiteSiteName && !isAzureFunction()) {
6058
attributes = {
6159
...attributes,
6260
[SEMRESATTRS_SERVICE_NAME]: websiteSiteName,

detectors/node/opentelemetry-resource-detector-azure/src/detectors/AzureFunctionsDetector.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ import {
2929
} from '@opentelemetry/semantic-conventions';
3030
import {
3131
WEBSITE_SITE_NAME,
32-
FUNCTIONS_VERSION,
3332
WEBSITE_INSTANCE_ID,
3433
FUNCTIONS_MEM_LIMIT,
3534
REGION_NAME,
3635
CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE,
3736
} from '../types';
38-
import { getAzureResourceUri } from '../utils';
37+
import { getAzureResourceUri, isAzureFunction } from '../utils';
3938

4039
const AZURE_FUNCTIONS_ATTRIBUTE_ENV_VARS = {
4140
[SEMRESATTRS_SERVICE_NAME]: WEBSITE_SITE_NAME,
@@ -51,13 +50,13 @@ class AzureFunctionsDetector implements DetectorSync {
5150
detect(): IResource {
5251
let attributes = {};
5352
const serviceName = process.env[WEBSITE_SITE_NAME];
54-
const functionVersion = process.env[FUNCTIONS_VERSION];
5553

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

detectors/node/opentelemetry-resource-detector-azure/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const WEBSITE_OWNER_NAME = 'WEBSITE_OWNER_NAME';
2525
export const WEBSITE_RESOURCE_GROUP = 'WEBSITE_RESOURCE_GROUP';
2626
export const WEBSITE_SITE_NAME = 'WEBSITE_SITE_NAME';
2727
export const WEBSITE_SLOT_NAME = 'WEBSITE_SLOT_NAME';
28+
export const WEBSITE_SKU = 'WEBSITE_SKU';
2829

2930
export const FUNCTIONS_VERSION = 'FUNCTIONS_EXTENSION_VERSION';
3031
export const FUNCTIONS_MEM_LIMIT = 'WEBSITE_MEMORY_LIMIT_MB';

detectors/node/opentelemetry-resource-detector-azure/src/utils.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { WEBSITE_OWNER_NAME, WEBSITE_RESOURCE_GROUP } from './types';
17+
import {
18+
FUNCTIONS_VERSION,
19+
WEBSITE_OWNER_NAME,
20+
WEBSITE_RESOURCE_GROUP,
21+
WEBSITE_SKU,
22+
} from './types';
1823

1924
export function getAzureResourceUri(
2025
websiteSiteName: string
@@ -33,3 +38,10 @@ export function getAzureResourceUri(
3338

3439
return `/subscriptions/${subscriptionId}/resourceGroups/${websiteResourceGroup}/providers/Microsoft.Web/sites/${websiteSiteName}`;
3540
}
41+
42+
export function isAzureFunction(): boolean {
43+
return !!(
44+
process.env[FUNCTIONS_VERSION] ||
45+
process.env[WEBSITE_SKU] === 'FlexConsumption'
46+
);
47+
}

detectors/node/opentelemetry-resource-detector-azure/test/detectors/AzureFunctionsDetector.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,27 @@ describe('AzureFunctionsDetector', () => {
102102
);
103103
});
104104
});
105+
106+
it('should detect azure functions if websiteSku is defined as FlexConsumption', () => {
107+
assert.ok(!process.env.WEBSITE_SKU && !process.env.FUNCTIONS_VERSION);
108+
process.env.WEBSITE_SITE_NAME = 'test-service';
109+
process.env.REGION_NAME = 'test-region';
110+
process.env.WEBSITE_INSTANCE_ID = 'test-instance-id';
111+
process.env.WEBSITE_SKU = 'FlexConsumption';
112+
process.env.WEBSITE_MEMORY_LIMIT_MB = '1000';
113+
process.env.WEBSITE_OWNER_NAME = 'test-owner-name';
114+
process.env.WEBSITE_RESOURCE_GROUP = 'test-resource-group';
115+
116+
const resource = detectResourcesSync({
117+
detectors: [azureFunctionsDetector, azureAppServiceDetector],
118+
});
119+
assert.ok(resource);
120+
const attributes = resource.attributes;
121+
assert.strictEqual(attributes[SEMRESATTRS_SERVICE_NAME], 'test-service');
122+
assert.strictEqual(attributes[SEMRESATTRS_CLOUD_PROVIDER], 'azure');
123+
124+
// Should not detect app service values
125+
assert.strictEqual(attributes[SEMRESATTRS_SERVICE_INSTANCE_ID], undefined);
126+
assert.strictEqual(attributes[SEMRESATTRS_PROCESS_PID], process.pid);
127+
delete process.env.WEBSITE_SKU;
128+
});

0 commit comments

Comments
 (0)