Skip to content

Commit

Permalink
fix(azure-resource-detector): Ensure that resources aren't classified…
Browse files Browse the repository at this point in the history
… as app service when they're using functions and both detectors are imported.
  • Loading branch information
JacksonWeber committed Jan 3, 2024
1 parent ec7125b commit 35bb260
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
WEBSITE_SITE_NAME,
WEBSITE_SLOT_NAME,
CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE,
FUNCTIONS_VERSION
} from '../types';
import {
CloudProviderValues,
Expand All @@ -49,7 +50,8 @@ class AzureAppServiceDetector implements DetectorSync {
detect(): IResource {
let attributes = {};
const websiteSiteName = process.env[WEBSITE_SITE_NAME];
if (websiteSiteName) {
const isAzureFunction = !!process.env[FUNCTIONS_VERSION];
if (websiteSiteName && !isAzureFunction) {
attributes = {
...attributes,
[SemanticResourceAttributes.SERVICE_NAME]: websiteSiteName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import * as assert from 'assert';
import { azureAppServiceDetector } from '../../src/detectors/AzureAppServiceDetector';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { azureFunctionsDetector } from '../../src';
import { detectResourcesSync } from '@opentelemetry/resources';

describe('AzureAppServiceDetector', () => {
let originalEnv: NodeJS.ProcessEnv;
Expand All @@ -38,7 +40,9 @@ describe('AzureAppServiceDetector', () => {
process.env.WEBSITE_RESOURCE_GROUP = 'test-resource-group';
process.env.WEBSITE_OWNER_NAME = 'test-owner-name';

const resource = azureAppServiceDetector.detect();
const resource = detectResourcesSync({
detectors: [azureFunctionsDetector, azureAppServiceDetector],
});
assert.ok(resource);
const attributes = resource.attributes;
assert.strictEqual(
Expand Down Expand Up @@ -88,7 +92,9 @@ describe('AzureAppServiceDetector', () => {
process.env.WEBSITE_HOME_STAMPNAME = 'test-home-stamp';
process.env.WEBSITE_OWNER_NAME = 'test-owner-name';

const resource = azureAppServiceDetector.detect();
const resource = detectResourcesSync({
detectors: [azureFunctionsDetector, azureAppServiceDetector],
});
assert.ok(resource);
const attributes = resource.attributes;
assert.strictEqual(
Expand Down Expand Up @@ -123,7 +129,9 @@ describe('AzureAppServiceDetector', () => {
process.env.WEBSITE_RESOURCE_GROUP = 'test-resource-group';
delete process.env.WEBSITE_OWNER_NAME;

const resource = azureAppServiceDetector.detect();
const resource = detectResourcesSync({
detectors: [azureFunctionsDetector, azureAppServiceDetector],
});
assert.ok(resource);
const attributes = resource.attributes;
assert.strictEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

import * as assert from 'assert';
import { azureFunctionsDetector } from '../../src/detectors/AzureFunctionsDetector';
import { azureAppServiceDetector } from '../../src/detectors/AzureAppServiceDetector';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { detectResourcesSync } from '@opentelemetry/resources';

describe('AzureFunctionsDetector', () => {
let originalEnv: NodeJS.ProcessEnv;
Expand All @@ -35,7 +37,51 @@ describe('AzureFunctionsDetector', () => {
process.env.FUNCTIONS_EXTENSION_VERSION = '~4';
process.env.WEBSITE_MEMORY_LIMIT_MB = '1000';

const resource = azureFunctionsDetector.detect();
const resource = detectResourcesSync({
detectors: [azureFunctionsDetector, azureAppServiceDetector],
});
assert.ok(resource);
const attributes = resource.attributes;
assert.strictEqual(
attributes[SemanticResourceAttributes.FAAS_NAME],
'test-function'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.CLOUD_PROVIDER],
'azure'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.CLOUD_PLATFORM],
'azure_functions'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.CLOUD_REGION],
'test-region'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.FAAS_INSTANCE],
'test-instance-id'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.FAAS_MAX_MEMORY],
'1000'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.FAAS_VERSION],
'~4'
);
});

it('should not detect azure app service values', () => {
process.env.WEBSITE_SITE_NAME = 'test-function';
process.env.REGION_NAME = 'test-region';
process.env.WEBSITE_INSTANCE_ID = 'test-instance-id';
process.env.FUNCTIONS_EXTENSION_VERSION = '~4';
process.env.WEBSITE_MEMORY_LIMIT_MB = '1000';

const resource = detectResourcesSync({
detectors: [azureFunctionsDetector, azureAppServiceDetector],
});
assert.ok(resource);
const attributes = resource.attributes;
assert.strictEqual(
Expand Down

0 comments on commit 35bb260

Please sign in to comment.