diff --git a/detectors/node/opentelemetry-resource-detector-aws/src/detectors/AwsLambdaDetector.ts b/detectors/node/opentelemetry-resource-detector-aws/src/detectors/AwsLambdaDetector.ts index 0e38a04a56..1b3d254540 100644 --- a/detectors/node/opentelemetry-resource-detector-aws/src/detectors/AwsLambdaDetector.ts +++ b/detectors/node/opentelemetry-resource-detector-aws/src/detectors/AwsLambdaDetector.ts @@ -18,12 +18,12 @@ import { Detector, Resource, ResourceDetectionConfig, -} from '@opentelemetry/resources'; +} from "@opentelemetry/resources"; import { CloudProviderValues, CloudPlatformValues, SemanticResourceAttributes, -} from '@opentelemetry/semantic-conventions'; +} from "@opentelemetry/semantic-conventions"; /** * The AwsLambdaDetector can be used to detect if a process is running in AWS Lambda @@ -31,7 +31,7 @@ import { * Returns an empty Resource if detection fails. */ export class AwsLambdaDetector implements Detector { - async detect(_config?: ResourceDetectionConfig): Promise { + detectSync(_config?: ResourceDetectionConfig): Resource { const functionName = process.env.AWS_LAMBDA_FUNCTION_NAME; if (!functionName) { return Resource.empty(); @@ -61,6 +61,10 @@ export class AwsLambdaDetector implements Detector { return new Resource(attributes); } + + async detect(_config?: ResourceDetectionConfig): Promise { + return this.detectSync(_config); + } } export const awsLambdaDetector = new AwsLambdaDetector(); diff --git a/detectors/node/opentelemetry-resource-detector-aws/test/detectors/AwsLambdaDetector.test.ts b/detectors/node/opentelemetry-resource-detector-aws/test/detectors/AwsLambdaDetector.test.ts index daaa34c61b..7a679e7075 100644 --- a/detectors/node/opentelemetry-resource-detector-aws/test/detectors/AwsLambdaDetector.test.ts +++ b/detectors/node/opentelemetry-resource-detector-aws/test/detectors/AwsLambdaDetector.test.ts @@ -14,15 +14,15 @@ * limitations under the License. */ -import * as assert from 'assert'; +import * as assert from "assert"; import { assertCloudResource, assertEmptyResource, -} from '@opentelemetry/contrib-test-utils'; +} from "@opentelemetry/contrib-test-utils"; -import { awsLambdaDetector } from '../../src'; +import { awsLambdaDetector } from "../../src"; -describe('awsLambdaDetector', () => { +describe("awsLambdaDetector", () => { let oldEnv: NodeJS.ProcessEnv; beforeEach(() => { @@ -33,28 +33,44 @@ describe('awsLambdaDetector', () => { process.env = oldEnv; }); - describe('on lambda', () => { - it('fills resource', async () => { - process.env.AWS_LAMBDA_FUNCTION_NAME = 'name'; - process.env.AWS_LAMBDA_FUNCTION_VERSION = 'v1'; - process.env.AWS_REGION = 'us-east-1'; + describe("on lambda", () => { + it("fills resource", async () => { + process.env.AWS_LAMBDA_FUNCTION_NAME = "name"; + process.env.AWS_LAMBDA_FUNCTION_VERSION = "v1"; + process.env.AWS_REGION = "us-east-1"; const resource = await awsLambdaDetector.detect(); assertCloudResource(resource, { - provider: 'aws', - region: 'us-east-1', + provider: "aws", + region: "us-east-1", }); - assert.strictEqual(resource.attributes['faas.name'], 'name'); - assert.strictEqual(resource.attributes['faas.version'], 'v1'); + assert.strictEqual(resource.attributes["faas.name"], "name"); + assert.strictEqual(resource.attributes["faas.version"], "v1"); + }); + + it("also works synchronously", async () => { + process.env.AWS_LAMBDA_FUNCTION_NAME = "name"; + process.env.AWS_LAMBDA_FUNCTION_VERSION = "v1"; + process.env.AWS_REGION = "us-east-1"; + + const resource = awsLambdaDetector.detectSync(); + + assertCloudResource(resource, { + provider: "aws", + region: "us-east-1", + }); + + assert.strictEqual(resource.attributes["faas.name"], "name"); + assert.strictEqual(resource.attributes["faas.version"], "v1"); }); }); - describe('not on lambda', () => { - it('returns empty resource', async () => { - process.env.AWS_LAMBDA_FUNCTION_VERSION = 'v1'; - process.env.AWS_REGION = 'us-east-1'; + describe("not on lambda", () => { + it("returns empty resource", async () => { + process.env.AWS_LAMBDA_FUNCTION_VERSION = "v1"; + process.env.AWS_REGION = "us-east-1"; const resource = await awsLambdaDetector.detect();