Skip to content

Commit 3f91296

Browse files
committed
resource-detector-azure
1 parent 1b1088c commit 3f91296

File tree

9 files changed

+198
-57
lines changed

9 files changed

+198
-57
lines changed

detectors/node/opentelemetry-resource-detector-azure/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ npm install --save @opentelemetry/resource-detector-azure
1818
```typescript
1919
import { detectResources } from '@opentelemetry/resources';
2020
import { azureAppServiceDetector } from '@opentelemetry/resource-detector-azure';
21-
const resource = detectResourcesSync({
21+
const resource = detectResources({
2222
detectors: [azureAppServiceDetector],
2323
});
2424

detectors/node/opentelemetry-resource-detector-azure/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"author": "OpenTelemetry Authors",
2121
"license": "Apache-2.0",
2222
"engines": {
23-
"node": ">=14"
23+
"node": "^18.19.0 || >=20.6.0"
2424
},
2525
"files": [
2626
"build/src/**/*.js",
@@ -36,7 +36,7 @@
3636
"devDependencies": {
3737
"@opentelemetry/api": "^1.0.0",
3838
"@opentelemetry/contrib-test-utils": "^0.45.1",
39-
"@opentelemetry/instrumentation-http": "^0.57.2",
39+
"@opentelemetry/instrumentation-http": "^0.200.0-dev.0",
4040
"@types/mocha": "10.0.10",
4141
"@types/node": "18.18.14",
4242
"@types/sinon": "17.0.4",
@@ -49,8 +49,8 @@
4949
"@opentelemetry/api": "^1.0.0"
5050
},
5151
"dependencies": {
52-
"@opentelemetry/core": "^1.25.1",
53-
"@opentelemetry/resources": "^1.10.1",
52+
"@opentelemetry/core": "^2.0.0-dev.0",
53+
"@opentelemetry/resources": "^2.0.0-dev.0",
5454
"@opentelemetry/semantic-conventions": "^1.27.0"
5555
},
5656
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/detectors/node/opentelemetry-resource-detector-azure#readme",

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

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

17-
import { DetectorSync, IResource, Resource } from '@opentelemetry/resources';
17+
import { ResourceDetector, DetectedResource } from '@opentelemetry/resources';
1818
import {
1919
AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE,
2020
REGION_NAME,
@@ -50,8 +50,8 @@ const APP_SERVICE_ATTRIBUTE_ENV_VARS = {
5050
* The AzureAppServiceDetector can be used to detect if a process is running in an Azure App Service
5151
* @returns a {@link Resource} populated with data about the environment or an empty Resource if detection fails.
5252
*/
53-
class AzureAppServiceDetector implements DetectorSync {
54-
detect(): IResource {
53+
class AzureAppServiceDetector implements ResourceDetector {
54+
detect(): DetectedResource {
5555
let attributes = {};
5656
const websiteSiteName = process.env[WEBSITE_SITE_NAME];
5757
if (websiteSiteName && !isAzureFunction()) {
@@ -85,7 +85,7 @@ class AzureAppServiceDetector implements DetectorSync {
8585
}
8686
}
8787
}
88-
return new Resource(attributes);
88+
return { attributes };
8989
}
9090
}
9191

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { DetectorSync, IResource, Resource } from '@opentelemetry/resources';
18-
17+
import { ResourceDetector, DetectedResource } from '@opentelemetry/resources';
1918
import {
2019
SEMRESATTRS_FAAS_MAX_MEMORY,
2120
SEMRESATTRS_FAAS_INSTANCE,
@@ -46,8 +45,8 @@ const AZURE_FUNCTIONS_ATTRIBUTE_ENV_VARS = {
4645
* The AzureFunctionsDetector can be used to detect if a process is running in Azure Functions
4746
* @returns a {@link Resource} populated with data about the environment or an empty Resource if detection fails.
4847
*/
49-
class AzureFunctionsDetector implements DetectorSync {
50-
detect(): IResource {
48+
class AzureFunctionsDetector implements ResourceDetector {
49+
detect(): DetectedResource {
5150
let attributes = {};
5251
const serviceName = process.env[WEBSITE_SITE_NAME];
5352

@@ -102,7 +101,7 @@ class AzureFunctionsDetector implements DetectorSync {
102101
}
103102
}
104103
}
105-
return new Resource(attributes);
104+
return { attributes };
106105
}
107106
}
108107

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ import * as http from 'http';
1919
import { context } from '@opentelemetry/api';
2020
import { suppressTracing } from '@opentelemetry/core';
2121
import {
22-
DetectorSync,
23-
IResource,
24-
Resource,
25-
ResourceAttributes,
22+
ResourceDetector,
23+
DetectedResource,
24+
DetectedResourceAttributes,
2625
} from '@opentelemetry/resources';
2726
import {
2827
CLOUDPLATFORMVALUES_AZURE_VM,
@@ -48,15 +47,15 @@ import {
4847
* The AzureVmDetector can be used to detect if a process is running in an Azure VM.
4948
* @returns a {@link Resource} populated with data about the environment or an empty Resource if detection fails.
5049
*/
51-
class AzureVmResourceDetector implements DetectorSync {
52-
detect(): IResource {
50+
class AzureVmResourceDetector implements ResourceDetector {
51+
detect(): DetectedResource {
5352
const attributes = context.with(suppressTracing(context.active()), () =>
5453
this.getAzureVmMetadata()
5554
);
56-
return new Resource({}, attributes);
55+
return { attributes };
5756
}
5857

59-
async getAzureVmMetadata(): Promise<ResourceAttributes> {
58+
getAzureVmMetadata(): DetectedResourceAttributes {
6059
const options = {
6160
host: AZURE_VM_METADATA_HOST,
6261
path: AZURE_VM_METADATA_PATH,
@@ -66,7 +65,7 @@ class AzureVmResourceDetector implements DetectorSync {
6665
Metadata: 'True',
6766
},
6867
};
69-
const metadata: AzureVmMetadata = await new Promise((resolve, reject) => {
68+
const metadataP: Promise<AzureVmMetadata> = new Promise((resolve, reject) => {
7069
const timeoutId = setTimeout(() => {
7170
req.destroy();
7271
reject(new Error('Azure metadata service request timed out.'));
@@ -100,16 +99,16 @@ class AzureVmResourceDetector implements DetectorSync {
10099
});
101100

102101
const attributes = {
103-
[AZURE_VM_SCALE_SET_NAME_ATTRIBUTE]: metadata['vmScaleSetName'],
104-
[AZURE_VM_SKU_ATTRIBUTE]: metadata['sku'],
105-
[SEMRESATTRS_CLOUD_PLATFORM]: CLOUDPLATFORMVALUES_AZURE_VM,
106-
[SEMRESATTRS_CLOUD_PROVIDER]: CLOUDPROVIDERVALUES_AZURE,
107-
[SEMRESATTRS_CLOUD_REGION]: metadata['location'],
108-
[CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE]: metadata['resourceId'],
109-
[SEMRESATTRS_HOST_ID]: metadata['vmId'],
110-
[SEMRESATTRS_HOST_NAME]: metadata['name'],
111-
[SEMRESATTRS_HOST_TYPE]: metadata['vmSize'],
112-
[SEMRESATTRS_OS_VERSION]: metadata['version'],
102+
[AZURE_VM_SCALE_SET_NAME_ATTRIBUTE]: metadataP.then(metadata => metadata['vmScaleSetName']),
103+
[AZURE_VM_SKU_ATTRIBUTE]: metadataP.then(metadata => metadata['sku']),
104+
[SEMRESATTRS_CLOUD_PLATFORM]: metadataP.then(() => CLOUDPLATFORMVALUES_AZURE_VM),
105+
[SEMRESATTRS_CLOUD_PROVIDER]: metadataP.then(() => CLOUDPROVIDERVALUES_AZURE),
106+
[SEMRESATTRS_CLOUD_REGION]: metadataP.then(metadata => metadata['location']),
107+
[CLOUD_RESOURCE_ID_RESOURCE_ATTRIBUTE]: metadataP.then(metadata => metadata['resourceId']),
108+
[SEMRESATTRS_HOST_ID]: metadataP.then(metadata => metadata['vmId']),
109+
[SEMRESATTRS_HOST_NAME]: metadataP.then(metadata => metadata['name']),
110+
[SEMRESATTRS_HOST_TYPE]: metadataP.then(metadata => metadata['vmSize']),
111+
[SEMRESATTRS_OS_VERSION]: metadataP.then(metadata => metadata['version']),
113112
};
114113
return attributes;
115114
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
SEMRESATTRS_SERVICE_NAME,
2727
} from '@opentelemetry/semantic-conventions';
2828
import { azureFunctionsDetector } from '../../src';
29-
import { detectResourcesSync } from '@opentelemetry/resources';
29+
import { detectResources } from '@opentelemetry/resources';
3030

3131
describe('AzureAppServiceDetector', () => {
3232
let originalEnv: NodeJS.ProcessEnv;
@@ -48,7 +48,7 @@ describe('AzureAppServiceDetector', () => {
4848
process.env.WEBSITE_RESOURCE_GROUP = 'test-resource-group';
4949
process.env.WEBSITE_OWNER_NAME = 'test-owner-name';
5050

51-
const resource = detectResourcesSync({
51+
const resource = detectResources({
5252
detectors: [azureFunctionsDetector, azureAppServiceDetector],
5353
});
5454
assert.ok(resource);
@@ -88,7 +88,7 @@ describe('AzureAppServiceDetector', () => {
8888
process.env.WEBSITE_HOME_STAMPNAME = 'test-home-stamp';
8989
process.env.WEBSITE_OWNER_NAME = 'test-owner-name';
9090

91-
const resource = detectResourcesSync({
91+
const resource = detectResources({
9292
detectors: [azureFunctionsDetector, azureAppServiceDetector],
9393
});
9494
assert.ok(resource);
@@ -119,7 +119,7 @@ describe('AzureAppServiceDetector', () => {
119119
process.env.WEBSITE_RESOURCE_GROUP = 'test-resource-group';
120120
delete process.env.WEBSITE_OWNER_NAME;
121121

122-
const resource = detectResourcesSync({
122+
const resource = detectResources({
123123
detectors: [azureFunctionsDetector, azureAppServiceDetector],
124124
});
125125
assert.ok(resource);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
SEMRESATTRS_SERVICE_INSTANCE_ID,
2828
SEMRESATTRS_SERVICE_NAME,
2929
} from '@opentelemetry/semantic-conventions';
30-
import { detectResourcesSync } from '@opentelemetry/resources';
30+
import { detectResources } from '@opentelemetry/resources';
3131
import { AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE } from '../../src/types';
3232

3333
describe('AzureFunctionsDetector', () => {
@@ -49,7 +49,7 @@ describe('AzureFunctionsDetector', () => {
4949
process.env.WEBSITE_OWNER_NAME = 'test-owner-name';
5050
process.env.WEBSITE_RESOURCE_GROUP = 'test-resource-group';
5151

52-
const resource = detectResourcesSync({
52+
const resource = detectResources({
5353
detectors: [azureFunctionsDetector, azureAppServiceDetector],
5454
});
5555
assert.ok(resource);
@@ -91,7 +91,7 @@ describe('AzureFunctionsDetector', () => {
9191
process.env.WEBSITE_RESOURCE_GROUP = 'test-resource-group';
9292

9393
const expectedWebsiteOwnerName = 'test-owner-name';
94-
const resource = detectResourcesSync({
94+
const resource = detectResources({
9595
detectors: [azureFunctionsDetector, azureAppServiceDetector],
9696
});
9797
assert.ok(resource);
@@ -113,7 +113,7 @@ it('should detect azure functions if websiteSku is defined as FlexConsumption',
113113
process.env.WEBSITE_OWNER_NAME = 'test-owner-name';
114114
process.env.WEBSITE_RESOURCE_GROUP = 'test-resource-group';
115115

116-
const resource = detectResourcesSync({
116+
const resource = detectResources({
117117
detectors: [azureFunctionsDetector, azureAppServiceDetector],
118118
});
119119
assert.ok(resource);

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import * as assert from 'assert';
1818
import * as nock from 'nock';
1919
import { azureVmDetector } from '../../src/detectors/AzureVmDetector';
20-
import { IResource } from '@opentelemetry/resources';
2120
import { AzureVmMetadata } from '../../src/types';
21+
import { detectResources } from '@opentelemetry/resources';
2222

2323
const linuxTestResponse: AzureVmMetadata = {
2424
additionalCapabilities: {
@@ -387,10 +387,8 @@ describe('AzureVmServiceDetector', () => {
387387
.get(AZURE_VM_METADATA_PATH)
388388
.reply(200, linuxTestResponse);
389389

390-
const resource: IResource = azureVmDetector.detect();
391-
if (resource.waitForAsyncAttributes) {
392-
await resource.waitForAsyncAttributes();
393-
}
390+
const resource = detectResources({ detectors: [azureVmDetector] });
391+
await resource.waitForAsyncAttributes?.();
394392
const attributes = resource.attributes;
395393
for (let i = 0; i < Object.keys(attributes).length; i++) {
396394
const key = Object.keys(attributes)[i];
@@ -404,10 +402,8 @@ describe('AzureVmServiceDetector', () => {
404402
.get(AZURE_VM_METADATA_PATH)
405403
.reply(200, windowsTestResponse);
406404

407-
const resource: IResource = azureVmDetector.detect();
408-
if (resource.waitForAsyncAttributes) {
409-
await resource.waitForAsyncAttributes();
410-
}
405+
const resource = detectResources({ detectors: [azureVmDetector] });
406+
await resource.waitForAsyncAttributes?.();
411407
const attributes = resource.attributes;
412408
for (let i = 0; i < Object.keys(attributes).length; i++) {
413409
const key = Object.keys(attributes)[i];

0 commit comments

Comments
 (0)