Skip to content

Commit

Permalink
Merge branch 'main' into dluna/1950-express-handle-stack
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc authored Jun 10, 2024
2 parents 49fd215 + 93776fa commit aa93499
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 137 deletions.
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 @@ -25,6 +25,7 @@ export const WEBSITE_OWNER_NAME = 'WEBSITE_OWNER_NAME';
export const WEBSITE_RESOURCE_GROUP = 'WEBSITE_RESOURCE_GROUP';
export const WEBSITE_SITE_NAME = 'WEBSITE_SITE_NAME';
export const WEBSITE_SLOT_NAME = 'WEBSITE_SLOT_NAME';
export const WEBSITE_SKU = 'WEBSITE_SKU';

export const FUNCTIONS_VERSION = 'FUNCTIONS_EXTENSION_VERSION';
export const FUNCTIONS_MEM_LIMIT = 'WEBSITE_MEMORY_LIMIT_MB';
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,27 @@ describe('AzureFunctionsDetector', () => {
);
});
});

it('should detect azure functions if websiteSku is defined as FlexConsumption', () => {
assert.ok(!process.env.WEBSITE_SKU && !process.env.FUNCTIONS_VERSION);
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';

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;
});
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class ContainerDetector implements Detector {
private async _getContainerId(): Promise<string | undefined> {
try {
return (
(await this._getContainerIdV1()) ?? (await this._getContainerIdV2())
(await this._getContainerIdV1()) || (await this._getContainerIdV2())
);
} catch (e) {
if (e instanceof Error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ describe('ContainerDetector', () => {
assert.ok(resource);
});

it('should return a correctCgroupV2Data resource with v1Detector returns empty string ', async () => {
readStub = sinon.stub(ContainerDetector, 'readFileAsync' as any);
sinon.stub(containerDetector, '_getContainerIdV1' as any).resolves('');
sinon
.stub(containerDetector, '_getContainerIdV2' as any)
.resolves(correctCgroupV2Data);
const containerId = await containerDetector['_getContainerId']();
assert.strictEqual(containerId, correctCgroupV2Data);
});

it('should return a resource without attribute container.id when cgroup file does not contain valid Container ID', async () => {
readStub = sinon
.stub(ContainerDetector, 'readFileAsync' as any)
Expand Down
Loading

0 comments on commit aa93499

Please sign in to comment.