Skip to content

Commit

Permalink
fix: Fix Azure Fns Detector When Running with App Service Detector (#…
Browse files Browse the repository at this point in the history
…1884)

* fix(azure-resource-detector): Ensure that resources aren't classified as app service when they're using functions and both detectors are imported.

* Fix lint.

* Clean up and unify tests.
  • Loading branch information
JacksonWeber authored Jan 4, 2024
1 parent 2d11b69 commit 006c963
Show file tree
Hide file tree
Showing 3 changed files with 31 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,10 @@

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';
import { AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE } from '../../src/types';

describe('AzureFunctionsDetector', () => {
let originalEnv: NodeJS.ProcessEnv;
Expand All @@ -35,7 +38,9 @@ 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(
Expand Down Expand Up @@ -66,5 +71,16 @@ describe('AzureFunctionsDetector', () => {
attributes[SemanticResourceAttributes.FAAS_VERSION],
'~4'
);

// Should not detect app service values
assert.strictEqual(
attributes[SemanticResourceAttributes.SERVICE_INSTANCE_ID],
undefined
);

assert.strictEqual(
attributes[AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE],
undefined
);
});
});

0 comments on commit 006c963

Please sign in to comment.