Skip to content

Commit

Permalink
fix(azure-resource-detectors): Clean up typos, deps and lint.
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonWeber committed Oct 19, 2023
1 parent 184915d commit 12a3fef
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ npm install --save @opentelemetry/resource-detector-azure
## Usage
```typescript
import { detectResources } from '@opentelemetry/resources';
import { azureAppServiceDetector } from '@opentelemetry/resource-detecotr-azure';
import { azureAppServiceDetector } from '@opentelemetry/resource-detector-azure';
const resource = detectResourcesSync({
detectors: [azureAppServiceDetector],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,11 @@
"nock": "13.3.3",
"nyc": "15.1.0",
"rimraf": "5.0.5",
"sinon": "15.2.0",
"ts-mocha": "10.0.0",
"typescript": "4.4.4"
},
"peerDependencies": {
"@opentelemetry/api": "^1.0.0"
},
"dependencies": {
"@opentelemetry/core": "^1.0.0",
"@opentelemetry/resources": "^1.0.0",
"@opentelemetry/resources": "^1.10.1",
"@opentelemetry/semantic-conventions": "^1.0.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/detectors/node/opentelemetry-resource-detector-azure#readme"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
* limitations under the License.
*/

import {
DetectorSync,
IResource,
Resource
} from '@opentelemetry/resources';
import { DetectorSync, IResource, Resource } from '@opentelemetry/resources';

import {
CloudProviderValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@
* limitations under the License.
*/

import {
DetectorSync,
IResource,
Resource
} from '@opentelemetry/resources';
import { DetectorSync, IResource, Resource } from '@opentelemetry/resources';

import {
CloudProviderValues,
CloudPlatformValues,
SemanticResourceAttributes,
CloudProviderValues,
CloudPlatformValues,
SemanticResourceAttributes,
} from '@opentelemetry/semantic-conventions';

const FUNCTION_NAME = 'WEBSITE_SITE_NAME';
Expand All @@ -33,67 +29,69 @@ const FUNCTIONS_MEM_LIMIT = 'WEBSITE_MEMORY_LIMIT_MB';
const FUNCTIONS_REGION = 'REGION_NAME';

const AZURE_FUNCTIONS_ATTRIBUTE_ENV_VARS = {
[SemanticResourceAttributes.FAAS_NAME]: FUNCTION_NAME,
[SemanticResourceAttributes.FAAS_VERSION]: FUNCTIONS_VERSION,
[SemanticResourceAttributes.FAAS_INSTANCE]: FUNCTIONS_INSTANCE,
[SemanticResourceAttributes.FAAS_MAX_MEMORY]: FUNCTIONS_MEM_LIMIT,
[SemanticResourceAttributes.FAAS_NAME]: FUNCTION_NAME,
[SemanticResourceAttributes.FAAS_VERSION]: FUNCTIONS_VERSION,
[SemanticResourceAttributes.FAAS_INSTANCE]: FUNCTIONS_INSTANCE,
[SemanticResourceAttributes.FAAS_MAX_MEMORY]: FUNCTIONS_MEM_LIMIT,
};

/**
* The AzureFunctionsDetector can be used to detect if a process is running in Azure Functions
* @returns a {@link Resource} populated with data about the environment or an empty Resource if detection fails.
*/
class AzureFunctionsDetector implements DetectorSync {
detect(): IResource {
let attributes = {};
const functionName = process.env[FUNCTION_NAME];
if (functionName) {
const functionVersion = process.env[FUNCTIONS_VERSION];
const functionInstance = process.env[FUNCTIONS_INSTANCE];
const functionMemLimit = process.env[FUNCTIONS_MEM_LIMIT];

attributes = {
[SemanticResourceAttributes.CLOUD_PROVIDER]: CloudProviderValues.AZURE,
[SemanticResourceAttributes.CLOUD_PLATFORM]: CloudPlatformValues.AZURE_FUNCTIONS,
[SemanticResourceAttributes.CLOUD_REGION]: process.env[FUNCTIONS_REGION],
};

if (functionName) {
attributes = {
...attributes,
[SemanticResourceAttributes.FAAS_NAME]: functionName,
};
}
if (functionVersion) {
attributes = {
...attributes,
[SemanticResourceAttributes.FAAS_VERSION]: functionVersion,
};
}
if (functionInstance) {
attributes = {
...attributes,
[SemanticResourceAttributes.FAAS_INSTANCE]: functionInstance,
};
}
if (functionMemLimit) {
attributes = {
...attributes,
[SemanticResourceAttributes.FAAS_MAX_MEMORY]: functionMemLimit,
};
}
detect(): IResource {
let attributes = {};
const functionName = process.env[FUNCTION_NAME];
if (functionName) {
const functionVersion = process.env[FUNCTIONS_VERSION];
const functionInstance = process.env[FUNCTIONS_INSTANCE];
const functionMemLimit = process.env[FUNCTIONS_MEM_LIMIT];

attributes = {
[SemanticResourceAttributes.CLOUD_PROVIDER]: CloudProviderValues.AZURE,
[SemanticResourceAttributes.CLOUD_PLATFORM]:
CloudPlatformValues.AZURE_FUNCTIONS,
[SemanticResourceAttributes.CLOUD_REGION]:
process.env[FUNCTIONS_REGION],
};

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

for (const [key, value] of Object.entries(
AZURE_FUNCTIONS_ATTRIBUTE_ENV_VARS
)) {
const envVar = process.env[value];
if (envVar) {
attributes = { ...attributes, ...{ [key]: envVar } };
}
}
for (const [key, value] of Object.entries(
AZURE_FUNCTIONS_ATTRIBUTE_ENV_VARS
)) {
const envVar = process.env[value];
if (envVar) {
attributes = { ...attributes, ...{ [key]: envVar } };
}
return new Resource(attributes);
}
}
return new Resource(attributes);
}
}

export const azureFunctionsDetector = new AzureFunctionsDetector();
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class AzureVmResourceDetector implements DetectorSync {
const metadata: any = await new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
req.destroy();
reject(new Error('EC2 metadata api request timed out.'));
reject(new Error('Azure metadata service request timed out.'));
}, 1000);

const req = http.request(options, res => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,52 @@ import { azureFunctionsDetector } from '../../src/detectors/AzureFunctionsDetect
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';

describe('AzureFunctionsDetector', () => {
let originalEnv: NodeJS.ProcessEnv;
beforeEach(() => {
originalEnv = process.env;
});

afterEach(() => {
process.env = originalEnv;
});

it('should test functions 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 = azureFunctionsDetector.detect();
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'
);
});
let originalEnv: NodeJS.ProcessEnv;
beforeEach(() => {
originalEnv = process.env;
});

afterEach(() => {
process.env = originalEnv;
});

it('should test functions 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 = azureFunctionsDetector.detect();
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'
);
});
});

0 comments on commit 12a3fef

Please sign in to comment.