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

chore(resource-detector-azure): use exported strings for attributes #2048

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"dependencies": {
"@opentelemetry/resources": "^1.10.1",
"@opentelemetry/semantic-conventions": "^1.0.0"
"@opentelemetry/semantic-conventions": "^1.22.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/detectors/node/opentelemetry-resource-detector-azure#readme"
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,22 @@ import {
FUNCTIONS_VERSION,
} from '../types';
import {
CloudProviderValues,
CloudPlatformValues,
SemanticResourceAttributes,
SEMRESATTRS_CLOUD_REGION,
SEMRESATTRS_DEPLOYMENT_ENVIRONMENT,
SEMRESATTRS_HOST_ID,
SEMRESATTRS_SERVICE_INSTANCE_ID,
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_CLOUD_PROVIDER,
SEMRESATTRS_CLOUD_PLATFORM,
CLOUDPROVIDERVALUES_AZURE,
CLOUDPLATFORMVALUES_AZURE_APP_SERVICE,
} from '@opentelemetry/semantic-conventions';

const APP_SERVICE_ATTRIBUTE_ENV_VARS = {
[SemanticResourceAttributes.CLOUD_REGION]: REGION_NAME,
[SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: WEBSITE_SLOT_NAME,
[SemanticResourceAttributes.HOST_ID]: WEBSITE_HOSTNAME,
[SemanticResourceAttributes.SERVICE_INSTANCE_ID]: WEBSITE_INSTANCE_ID,
[SEMRESATTRS_CLOUD_REGION]: REGION_NAME,
[SEMRESATTRS_DEPLOYMENT_ENVIRONMENT]: WEBSITE_SLOT_NAME,
[SEMRESATTRS_HOST_ID]: WEBSITE_HOSTNAME,
[SEMRESATTRS_SERVICE_INSTANCE_ID]: WEBSITE_INSTANCE_ID,
[AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE]: WEBSITE_HOME_STAMPNAME,
};

Expand All @@ -54,16 +60,15 @@ class AzureAppServiceDetector implements DetectorSync {
if (websiteSiteName && !isAzureFunction) {
attributes = {
...attributes,
[SemanticResourceAttributes.SERVICE_NAME]: websiteSiteName,
[SEMRESATTRS_SERVICE_NAME]: websiteSiteName,
};
attributes = {
...attributes,
[SemanticResourceAttributes.CLOUD_PROVIDER]: CloudProviderValues.AZURE,
[SEMRESATTRS_CLOUD_PROVIDER]: CLOUDPROVIDERVALUES_AZURE,
};
attributes = {
...attributes,
[SemanticResourceAttributes.CLOUD_PLATFORM]:
CloudPlatformValues.AZURE_APP_SERVICE,
[SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_AZURE_APP_SERVICE,
};

const azureResourceUri = this.getAzureResourceUri(websiteSiteName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
import { DetectorSync, IResource, Resource } from '@opentelemetry/resources';

import {
CloudProviderValues,
CloudPlatformValues,
SemanticResourceAttributes,
SEMRESATTRS_FAAS_NAME,
SEMRESATTRS_FAAS_VERSION,
SEMRESATTRS_FAAS_MAX_MEMORY,
SEMRESATTRS_FAAS_INSTANCE,
SEMRESATTRS_CLOUD_PROVIDER,
SEMRESATTRS_CLOUD_PLATFORM,
SEMRESATTRS_CLOUD_REGION,
CLOUDPROVIDERVALUES_AZURE,
CLOUDPLATFORMVALUES_AZURE_FUNCTIONS,
} from '@opentelemetry/semantic-conventions';
import {
WEBSITE_SITE_NAME,
Expand All @@ -30,10 +36,10 @@ import {
} from '../types';

const AZURE_FUNCTIONS_ATTRIBUTE_ENV_VARS = {
[SemanticResourceAttributes.FAAS_NAME]: WEBSITE_SITE_NAME,
[SemanticResourceAttributes.FAAS_VERSION]: FUNCTIONS_VERSION,
[SemanticResourceAttributes.FAAS_INSTANCE]: WEBSITE_INSTANCE_ID,
[SemanticResourceAttributes.FAAS_MAX_MEMORY]: FUNCTIONS_MEM_LIMIT,
[SEMRESATTRS_FAAS_NAME]: WEBSITE_SITE_NAME,
[SEMRESATTRS_FAAS_VERSION]: FUNCTIONS_VERSION,
[SEMRESATTRS_FAAS_INSTANCE]: WEBSITE_INSTANCE_ID,
[SEMRESATTRS_FAAS_MAX_MEMORY]: FUNCTIONS_MEM_LIMIT,
};

/**
Expand All @@ -50,34 +56,33 @@ class AzureFunctionsDetector implements DetectorSync {
const functionMemLimit = process.env[FUNCTIONS_MEM_LIMIT];

attributes = {
[SemanticResourceAttributes.CLOUD_PROVIDER]: CloudProviderValues.AZURE,
[SemanticResourceAttributes.CLOUD_PLATFORM]:
CloudPlatformValues.AZURE_FUNCTIONS,
[SemanticResourceAttributes.CLOUD_REGION]: process.env[REGION_NAME],
[SEMRESATTRS_CLOUD_PROVIDER]: CLOUDPROVIDERVALUES_AZURE,
[SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_AZURE_FUNCTIONS,
[SEMRESATTRS_CLOUD_REGION]: process.env[REGION_NAME],
};

if (functionName) {
attributes = {
...attributes,
[SemanticResourceAttributes.FAAS_NAME]: functionName,
[SEMRESATTRS_FAAS_NAME]: functionName,
};
}
if (functionVersion) {
attributes = {
...attributes,
[SemanticResourceAttributes.FAAS_VERSION]: functionVersion,
[SEMRESATTRS_FAAS_VERSION]: functionVersion,
};
}
if (functionInstance) {
attributes = {
...attributes,
[SemanticResourceAttributes.FAAS_INSTANCE]: functionInstance,
[SEMRESATTRS_FAAS_INSTANCE]: functionInstance,
};
}
if (functionMemLimit) {
attributes = {
...attributes,
[SemanticResourceAttributes.FAAS_MAX_MEMORY]: functionMemLimit,
[SEMRESATTRS_FAAS_MAX_MEMORY]: functionMemLimit,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ import {
ResourceAttributes,
} from '@opentelemetry/resources';
import {
CloudPlatformValues,
CloudProviderValues,
SemanticResourceAttributes,
CLOUDPLATFORMVALUES_AZURE_VM,
CLOUDPROVIDERVALUES_AZURE,
SEMRESATTRS_CLOUD_PLATFORM,
SEMRESATTRS_CLOUD_PROVIDER,
SEMRESATTRS_CLOUD_REGION,
SEMRESATTRS_HOST_ID,
SEMRESATTRS_HOST_NAME,
SEMRESATTRS_HOST_TYPE,
SEMRESATTRS_OS_VERSION,
} from '@opentelemetry/semantic-conventions';
import {
CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE,
Expand Down Expand Up @@ -90,14 +96,14 @@ class AzureVmResourceDetector implements DetectorSync {
const attributes = {
[AZURE_VM_SCALE_SET_NAME_ATTRIBUTE]: metadata['vmScaleSetName'],
[AZURE_VM_SKU_ATTRIBUTE]: metadata['sku'],
[SemanticResourceAttributes.CLOUD_PLATFORM]: CloudPlatformValues.AZURE_VM,
[SemanticResourceAttributes.CLOUD_PROVIDER]: CloudProviderValues.AZURE,
[SemanticResourceAttributes.CLOUD_REGION]: metadata['location'],
[SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_AZURE_VM,
[SEMRESATTRS_CLOUD_PROVIDER]: CLOUDPROVIDERVALUES_AZURE,
[SEMRESATTRS_CLOUD_REGION]: metadata['location'],
[CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE]: metadata['resourceId'],
[SemanticResourceAttributes.HOST_ID]: metadata['vmId'],
[SemanticResourceAttributes.HOST_NAME]: metadata['name'],
[SemanticResourceAttributes.HOST_TYPE]: metadata['vmSize'],
[SemanticResourceAttributes.OS_VERSION]: metadata['version'],
[SEMRESATTRS_HOST_ID]: metadata['vmId'],
[SEMRESATTRS_HOST_NAME]: metadata['name'],
[SEMRESATTRS_HOST_TYPE]: metadata['vmSize'],
[SEMRESATTRS_OS_VERSION]: metadata['version'],
};
return attributes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@

import * as assert from 'assert';
import { azureAppServiceDetector } from '../../src/detectors/AzureAppServiceDetector';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMRESATTRS_CLOUD_PLATFORM,
SEMRESATTRS_CLOUD_PROVIDER,
SEMRESATTRS_CLOUD_REGION,
SEMRESATTRS_DEPLOYMENT_ENVIRONMENT,
SEMRESATTRS_HOST_ID,
SEMRESATTRS_SERVICE_INSTANCE_ID,
SEMRESATTRS_SERVICE_NAME,
} from '@opentelemetry/semantic-conventions';
import { azureFunctionsDetector } from '../../src';
import { detectResourcesSync } from '@opentelemetry/resources';

Expand Down Expand Up @@ -45,36 +53,24 @@ describe('AzureAppServiceDetector', () => {
});
assert.ok(resource);
const attributes = resource.attributes;
assert.strictEqual(attributes[SEMRESATTRS_SERVICE_NAME], 'test-site');
assert.strictEqual(attributes[SEMRESATTRS_CLOUD_PROVIDER], 'azure');
assert.strictEqual(
attributes[SemanticResourceAttributes.SERVICE_NAME],
'test-site'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.CLOUD_PROVIDER],
'azure'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.CLOUD_PLATFORM],
attributes[SEMRESATTRS_CLOUD_PLATFORM],
'azure_app_service'
);
assert.strictEqual(
attributes['cloud.resource_id'],
`/subscriptions/${process.env.WEBSITE_OWNER_NAME}/resourceGroups/${process.env.WEBSITE_RESOURCE_GROUP}/providers/Microsoft.Web/sites/${process.env.WEBSITE_SITE_NAME}`
);
assert.strictEqual(attributes[SEMRESATTRS_CLOUD_REGION], 'test-region');
assert.strictEqual(
attributes[SemanticResourceAttributes.CLOUD_REGION],
'test-region'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT],
attributes[SEMRESATTRS_DEPLOYMENT_ENVIRONMENT],
'test-slot'
);
assert.strictEqual(attributes[SEMRESATTRS_HOST_ID], 'test-hostname');
assert.strictEqual(
attributes[SemanticResourceAttributes.HOST_ID],
'test-hostname'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.SERVICE_INSTANCE_ID],
attributes[SEMRESATTRS_SERVICE_INSTANCE_ID],
'test-instance-id'
);
assert.strictEqual(
Expand All @@ -97,20 +93,14 @@ describe('AzureAppServiceDetector', () => {
});
assert.ok(resource);
const attributes = resource.attributes;
assert.strictEqual(attributes[SEMRESATTRS_CLOUD_REGION], 'test-region');
assert.strictEqual(
attributes[SemanticResourceAttributes.CLOUD_REGION],
'test-region'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT],
attributes[SEMRESATTRS_DEPLOYMENT_ENVIRONMENT],
'test-slot'
);
assert.strictEqual(attributes[SEMRESATTRS_HOST_ID], 'test-hostname');
assert.strictEqual(
attributes[SemanticResourceAttributes.HOST_ID],
'test-hostname'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.SERVICE_INSTANCE_ID],
attributes[SEMRESATTRS_SERVICE_INSTANCE_ID],
'test-instance-id'
);
assert.strictEqual(
Expand All @@ -134,20 +124,14 @@ describe('AzureAppServiceDetector', () => {
});
assert.ok(resource);
const attributes = resource.attributes;
assert.strictEqual(attributes[SEMRESATTRS_CLOUD_REGION], 'test-region');
assert.strictEqual(
attributes[SemanticResourceAttributes.CLOUD_REGION],
'test-region'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT],
attributes[SEMRESATTRS_DEPLOYMENT_ENVIRONMENT],
'test-slot'
);
assert.strictEqual(attributes[SEMRESATTRS_HOST_ID], 'test-hostname');
assert.strictEqual(
attributes[SemanticResourceAttributes.HOST_ID],
'test-hostname'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.SERVICE_INSTANCE_ID],
attributes[SEMRESATTRS_SERVICE_INSTANCE_ID],
'test-instance-id'
);
assert.strictEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@
import * as assert from 'assert';
import { azureFunctionsDetector } from '../../src/detectors/AzureFunctionsDetector';
import { azureAppServiceDetector } from '../../src/detectors/AzureAppServiceDetector';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMRESATTRS_CLOUD_PLATFORM,
SEMRESATTRS_CLOUD_PROVIDER,
SEMRESATTRS_CLOUD_REGION,
SEMRESATTRS_FAAS_INSTANCE,
SEMRESATTRS_FAAS_MAX_MEMORY,
SEMRESATTRS_FAAS_NAME,
SEMRESATTRS_FAAS_VERSION,
SEMRESATTRS_SERVICE_INSTANCE_ID,
} from '@opentelemetry/semantic-conventions';
import { detectResourcesSync } from '@opentelemetry/resources';
import { AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE } from '../../src/types';

Expand All @@ -43,40 +52,22 @@ describe('AzureFunctionsDetector', () => {
});
assert.ok(resource);
const attributes = resource.attributes;
assert.strictEqual(attributes[SEMRESATTRS_FAAS_NAME], 'test-function');
assert.strictEqual(attributes[SEMRESATTRS_CLOUD_PROVIDER], 'azure');
assert.strictEqual(
attributes[SemanticResourceAttributes.FAAS_NAME],
'test-function'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.CLOUD_PROVIDER],
'azure'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.CLOUD_PLATFORM],
attributes[SEMRESATTRS_CLOUD_PLATFORM],
'azure_functions'
);
assert.strictEqual(attributes[SEMRESATTRS_CLOUD_REGION], 'test-region');
assert.strictEqual(
attributes[SemanticResourceAttributes.CLOUD_REGION],
'test-region'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.FAAS_INSTANCE],
attributes[SEMRESATTRS_FAAS_INSTANCE],
'test-instance-id'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.FAAS_MAX_MEMORY],
'1000'
);
assert.strictEqual(
attributes[SemanticResourceAttributes.FAAS_VERSION],
'~4'
);
assert.strictEqual(attributes[SEMRESATTRS_FAAS_MAX_MEMORY], '1000');
assert.strictEqual(attributes[SEMRESATTRS_FAAS_VERSION], '~4');

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

assert.strictEqual(
attributes[AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE],
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading