From 07f0e0e8ab41a3a8fc2ddec260364000075df810 Mon Sep 17 00:00:00 2001 From: Adam Egyed Date: Wed, 17 Jun 2020 03:30:25 +0000 Subject: [PATCH 01/12] feat: add debug logging to resource auto-detection Signed-off-by: Adam Egyed --- .../src/platform/node/config.ts | 23 ++++++++++++++++ .../src/platform/node/detect-resources.ts | 26 ++++++++++++++++++- .../test/detect-resources.test.ts | 16 ++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 packages/opentelemetry-resources/src/platform/node/config.ts diff --git a/packages/opentelemetry-resources/src/platform/node/config.ts b/packages/opentelemetry-resources/src/platform/node/config.ts new file mode 100644 index 0000000000..ae7b89215b --- /dev/null +++ b/packages/opentelemetry-resources/src/platform/node/config.ts @@ -0,0 +1,23 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * ResourceDetectionConfig provides an interface for configuring resource auto-detection. + */ +export interface ResourceDetectionConfig { + /** Optional Logger. */ + logger?: (message: string) => void; +} diff --git a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts index d226ed4437..265c55b590 100644 --- a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts +++ b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts @@ -17,14 +17,20 @@ import { Resource } from '../../Resource'; import { envDetector, awsEc2Detector, gcpDetector } from './detectors'; import { Detector } from '../../types'; +import { ResourceDetectionConfig } from './config'; +import * as util from 'util'; const DETECTORS: Array = [envDetector, awsEc2Detector, gcpDetector]; /** * Runs all resource detectors and returns the results merged into a single * Resource. + * + * @param config Configuration object for resource detection */ -export const detectResources = async (): Promise => { +export const detectResources = async ( + config: ResourceDetectionConfig = {} +): Promise => { const resources: Array = await Promise.all( DETECTORS.map(d => { try { @@ -34,6 +40,24 @@ export const detectResources = async (): Promise => { } }) ); + if (config.logger) { + resources.forEach((resource, index) => { + // Only print populated resources + if (Object.keys(resource.labels).length > 0) { + const resourceDebugString = util.inspect(resource.labels, { + depth: 2, + breakLength: Infinity, + sorted: true, + compact: false, + }); + const detectorName = DETECTORS[index].constructor + ? DETECTORS[index].constructor.name + : 'Unknown detector'; + config.logger!(`${detectorName} found resource.`); + config.logger!(resourceDebugString); + } + }); + } return resources.reduce( (acc, resource) => acc.merge(resource), Resource.createTelemetrySDKResource() diff --git a/packages/opentelemetry-resources/test/detect-resources.test.ts b/packages/opentelemetry-resources/test/detect-resources.test.ts index ca609fb3c4..b63c611865 100644 --- a/packages/opentelemetry-resources/test/detect-resources.test.ts +++ b/packages/opentelemetry-resources/test/detect-resources.test.ts @@ -16,6 +16,7 @@ import * as nock from 'nock'; import * as sinon from 'sinon'; +import * as assert from 'assert'; import { URL } from 'url'; import { Resource, detectResources } from '../src'; import { awsEc2Detector } from '../src/platform/node/detectors'; @@ -162,4 +163,19 @@ describe('detectResources', async () => { stub.restore(); }); }); + + describe('with a debug logger', () => { + it('prints detected resources to the logger', async () => { + // This test depends on the env detector to be functioning as intended + const mockedLogger = sinon.fake(); + await detectResources({ logger: mockedLogger }); + + assert.deepStrictEqual(mockedLogger.getCall(0).args, [ + 'EnvDetector found resource.', + ]); + assert.deepStrictEqual(mockedLogger.getCall(1).args, [ + "{\n 'service.instance.id': '627cc493',\n 'service.name': 'my-service',\n 'service.namespace': 'default',\n 'service.version': '0.0.1'\n}", + ]); + }); + }); }); From f0486e769dd38b7fb08020821d1822f89ee3b463 Mon Sep 17 00:00:00 2001 From: Adam Egyed Date: Wed, 17 Jun 2020 21:05:50 +0000 Subject: [PATCH 02/12] refactor: change resource detection to use logger from opentelemetry-api Signed-off-by: Adam Egyed --- .../src/platform/node/config.ts | 4 +++- .../src/platform/node/detect-resources.ts | 4 ++-- .../test/detect-resources.test.ts | 15 +++++++++++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/opentelemetry-resources/src/platform/node/config.ts b/packages/opentelemetry-resources/src/platform/node/config.ts index ae7b89215b..a6d7a91177 100644 --- a/packages/opentelemetry-resources/src/platform/node/config.ts +++ b/packages/opentelemetry-resources/src/platform/node/config.ts @@ -14,10 +14,12 @@ * limitations under the License. */ +import { Logger } from '@opentelemetry/api'; + /** * ResourceDetectionConfig provides an interface for configuring resource auto-detection. */ export interface ResourceDetectionConfig { /** Optional Logger. */ - logger?: (message: string) => void; + logger?: Logger; } diff --git a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts index 265c55b590..81ff7b69ec 100644 --- a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts +++ b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts @@ -53,8 +53,8 @@ export const detectResources = async ( const detectorName = DETECTORS[index].constructor ? DETECTORS[index].constructor.name : 'Unknown detector'; - config.logger!(`${detectorName} found resource.`); - config.logger!(resourceDebugString); + config.logger!.debug(`${detectorName} found resource.`); + config.logger!.debug(resourceDebugString); } }); } diff --git a/packages/opentelemetry-resources/test/detect-resources.test.ts b/packages/opentelemetry-resources/test/detect-resources.test.ts index b63c611865..6d6586f081 100644 --- a/packages/opentelemetry-resources/test/detect-resources.test.ts +++ b/packages/opentelemetry-resources/test/detect-resources.test.ts @@ -167,13 +167,20 @@ describe('detectResources', async () => { describe('with a debug logger', () => { it('prints detected resources to the logger', async () => { // This test depends on the env detector to be functioning as intended - const mockedLogger = sinon.fake(); - await detectResources({ logger: mockedLogger }); + const mockedLoggerMethod = sinon.fake(); + await detectResources({ + logger: { + debug: mockedLoggerMethod, + info: sinon.fake(), + warn: sinon.fake(), + error: sinon.fake(), + }, + }); - assert.deepStrictEqual(mockedLogger.getCall(0).args, [ + assert.deepStrictEqual(mockedLoggerMethod.getCall(0).args, [ 'EnvDetector found resource.', ]); - assert.deepStrictEqual(mockedLogger.getCall(1).args, [ + assert.deepStrictEqual(mockedLoggerMethod.getCall(1).args, [ "{\n 'service.instance.id': '627cc493',\n 'service.name': 'my-service',\n 'service.namespace': 'default',\n 'service.version': '0.0.1'\n}", ]); }); From 4372c8040422f4f052724ffbd687b2ad042112bf Mon Sep 17 00:00:00 2001 From: Adam Egyed Date: Fri, 19 Jun 2020 00:09:58 +0000 Subject: [PATCH 03/12] refactor: separated logging of resources from detection method Signed-off-by: Adam Egyed --- .../src/platform/node/detect-resources.ts | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts index 81ff7b69ec..4ad9b4de65 100644 --- a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts +++ b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts @@ -18,6 +18,7 @@ import { Resource } from '../../Resource'; import { envDetector, awsEc2Detector, gcpDetector } from './detectors'; import { Detector } from '../../types'; import { ResourceDetectionConfig } from './config'; +import { Logger } from '@opentelemetry/api'; import * as util from 'util'; const DETECTORS: Array = [envDetector, awsEc2Detector, gcpDetector]; @@ -40,7 +41,24 @@ export const detectResources = async ( } }) ); - if (config.logger) { + logResources(config.logger, resources); + return resources.reduce( + (acc, resource) => acc.merge(resource), + Resource.createTelemetrySDKResource() + ); +}; + +/** + * Writes debug information about the detected resources to the logger defined in the resource detection config, if one is provided. + * + * @param logger The logger to write the debug information to. + * @param resources The array of resources that should be logged. Empty entried will be ignored. + */ +const logResources = ( + logger: Logger | undefined, + resources: Array +) => { + if (logger) { resources.forEach((resource, index) => { // Only print populated resources if (Object.keys(resource.labels).length > 0) { @@ -53,13 +71,9 @@ export const detectResources = async ( const detectorName = DETECTORS[index].constructor ? DETECTORS[index].constructor.name : 'Unknown detector'; - config.logger!.debug(`${detectorName} found resource.`); - config.logger!.debug(resourceDebugString); + logger.debug(`${detectorName} found resource.`); + logger.debug(resourceDebugString); } }); } - return resources.reduce( - (acc, resource) => acc.merge(resource), - Resource.createTelemetrySDKResource() - ); }; From 447ef632825b6124343318d97dac66c55dcc3c32 Mon Sep 17 00:00:00 2001 From: Adam Egyed Date: Fri, 19 Jun 2020 00:28:23 +0000 Subject: [PATCH 04/12] feat: changed config to support detector-level logging Signed-off-by: Adam Egyed --- .../src/platform/node/config.ts | 25 ------------------- .../src/platform/node/detect-resources.ts | 4 +-- .../platform/node/detectors/AwsEc2Detector.ts | 3 ++- .../platform/node/detectors/EnvDetector.ts | 3 ++- .../platform/node/detectors/GcpDetector.ts | 3 ++- packages/opentelemetry-resources/src/types.ts | 3 ++- 6 files changed, 10 insertions(+), 31 deletions(-) delete mode 100644 packages/opentelemetry-resources/src/platform/node/config.ts diff --git a/packages/opentelemetry-resources/src/platform/node/config.ts b/packages/opentelemetry-resources/src/platform/node/config.ts deleted file mode 100644 index a6d7a91177..0000000000 --- a/packages/opentelemetry-resources/src/platform/node/config.ts +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { Logger } from '@opentelemetry/api'; - -/** - * ResourceDetectionConfig provides an interface for configuring resource auto-detection. - */ -export interface ResourceDetectionConfig { - /** Optional Logger. */ - logger?: Logger; -} diff --git a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts index 4ad9b4de65..b4af7c40e9 100644 --- a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts +++ b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts @@ -17,7 +17,7 @@ import { Resource } from '../../Resource'; import { envDetector, awsEc2Detector, gcpDetector } from './detectors'; import { Detector } from '../../types'; -import { ResourceDetectionConfig } from './config'; +import { ResourceDetectionConfig } from '../../config'; import { Logger } from '@opentelemetry/api'; import * as util from 'util'; @@ -35,7 +35,7 @@ export const detectResources = async ( const resources: Array = await Promise.all( DETECTORS.map(d => { try { - return d.detect(); + return d.detect(config); } catch { return Resource.empty(); } diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts index e58c214db0..145cfd6e9c 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts @@ -18,6 +18,7 @@ import * as http from 'http'; import { Resource } from '../../../Resource'; import { CLOUD_RESOURCE, HOST_RESOURCE } from '../../../constants'; import { Detector } from '../../../types'; +import { ResourceDetectionConfig } from '../../../config'; /** * The AwsEc2Detector can be used to detect if a process is running in AWS EC2 @@ -39,7 +40,7 @@ class AwsEc2Detector implements Detector { * empty {@link Resource} if the connection or parsing of the identity * document fails. */ - async detect(): Promise { + async detect(config: ResourceDetectionConfig = {}): Promise { try { const { accountId, diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts index 58ba567c36..6c0e9e6698 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts @@ -16,6 +16,7 @@ import { Resource } from '../../../Resource'; import { Detector, ResourceLabels } from '../../../types'; +import { ResourceDetectionConfig } from '../../../config'; /** * EnvDetector can be used to detect the presence of and create a Resource @@ -46,7 +47,7 @@ class EnvDetector implements Detector { * OTEL_RESOURCE_LABELS environment variable. Note this is an async function * to conform to the Detector interface. */ - async detect(): Promise { + async detect(config: ResourceDetectionConfig = {}): Promise { try { const labelString = process.env.OTEL_RESOURCE_LABELS; if (!labelString) return Resource.empty(); diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts index 25518ed963..1eed30deb5 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts @@ -24,6 +24,7 @@ import { K8S_RESOURCE, CONTAINER_RESOURCE, } from '../../../constants'; +import { ResourceDetectionConfig } from '../../../config'; /** * The GcpDetector can be used to detect if a process is running in the Google @@ -31,7 +32,7 @@ import { * the instance. Returns an empty Resource if detection fails. */ class GcpDetector implements Detector { - async detect(): Promise { + async detect(config: ResourceDetectionConfig = {}): Promise { if (!(await gcpMetadata.isAvailable())) return Resource.empty(); const [projectId, instanceId, zoneId, clusterName] = await Promise.all([ diff --git a/packages/opentelemetry-resources/src/types.ts b/packages/opentelemetry-resources/src/types.ts index e1aa0d45b0..f294e8f923 100644 --- a/packages/opentelemetry-resources/src/types.ts +++ b/packages/opentelemetry-resources/src/types.ts @@ -15,6 +15,7 @@ */ import { Resource } from './Resource'; +import { ResourceDetectionConfig } from './config'; /** Interface for Resource labels */ export interface ResourceLabels { @@ -26,5 +27,5 @@ export interface ResourceLabels { * a detector returns a Promise containing a Resource. */ export interface Detector { - detect(): Promise; + detect(config?: ResourceDetectionConfig): Promise; } From 506c693768c804d947cfb489e14e08fe186b68ed Mon Sep 17 00:00:00 2001 From: Adam Egyed Date: Mon, 22 Jun 2020 22:42:10 +0000 Subject: [PATCH 05/12] fix: test whitespace behavior on old node versions Signed-off-by: Adam Egyed --- .../opentelemetry-resources/src/config.ts | 25 +++++++++++++++++++ .../test/detect-resources.test.ts | 8 +++--- 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 packages/opentelemetry-resources/src/config.ts diff --git a/packages/opentelemetry-resources/src/config.ts b/packages/opentelemetry-resources/src/config.ts new file mode 100644 index 0000000000..a6d7a91177 --- /dev/null +++ b/packages/opentelemetry-resources/src/config.ts @@ -0,0 +1,25 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Logger } from '@opentelemetry/api'; + +/** + * ResourceDetectionConfig provides an interface for configuring resource auto-detection. + */ +export interface ResourceDetectionConfig { + /** Optional Logger. */ + logger?: Logger; +} diff --git a/packages/opentelemetry-resources/test/detect-resources.test.ts b/packages/opentelemetry-resources/test/detect-resources.test.ts index 6d6586f081..730ed08ae1 100644 --- a/packages/opentelemetry-resources/test/detect-resources.test.ts +++ b/packages/opentelemetry-resources/test/detect-resources.test.ts @@ -180,9 +180,11 @@ describe('detectResources', async () => { assert.deepStrictEqual(mockedLoggerMethod.getCall(0).args, [ 'EnvDetector found resource.', ]); - assert.deepStrictEqual(mockedLoggerMethod.getCall(1).args, [ - "{\n 'service.instance.id': '627cc493',\n 'service.name': 'my-service',\n 'service.namespace': 'default',\n 'service.version': '0.0.1'\n}", - ]); + // Regex formatting accounts for whitespace variations in util.inspect output over different node versions + assert.match( + mockedLoggerMethod.getCall(1).args.toString(), + /{\s+'service\.instance\.id':\s+'627cc493',\s+'service\.name':\s+'my-service',\s+'service\.namespace':\s+'default',\s+'service\.version':\s+'0\.0\.1'\s+}\s*/ + ); }); }); }); From c9851d03ce5ee97e61b06419f22a4c26484b78b2 Mon Sep 17 00:00:00 2001 From: Adam Egyed Date: Tue, 23 Jun 2020 21:57:14 +0000 Subject: [PATCH 06/12] fix: adjusted regex testing for node 8 and 10 Signed-off-by: Adam Egyed --- .../opentelemetry-resources/test/detect-resources.test.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/opentelemetry-resources/test/detect-resources.test.ts b/packages/opentelemetry-resources/test/detect-resources.test.ts index 730ed08ae1..a9965b1e05 100644 --- a/packages/opentelemetry-resources/test/detect-resources.test.ts +++ b/packages/opentelemetry-resources/test/detect-resources.test.ts @@ -181,9 +181,10 @@ describe('detectResources', async () => { 'EnvDetector found resource.', ]); // Regex formatting accounts for whitespace variations in util.inspect output over different node versions - assert.match( - mockedLoggerMethod.getCall(1).args.toString(), - /{\s+'service\.instance\.id':\s+'627cc493',\s+'service\.name':\s+'my-service',\s+'service\.namespace':\s+'default',\s+'service\.version':\s+'0\.0\.1'\s+}\s*/ + assert.ok( + /{\s+'service\.instance\.id':\s+'627cc493',\s+'service\.name':\s+'my-service',\s+'service\.namespace':\s+'default',\s+'service\.version':\s+'0\.0\.1'\s+}\s*/.test( + mockedLoggerMethod.getCall(1).args.toString() + ) ); }); }); From e0e958397e9a5a09fb04884161802e18e6f0253d Mon Sep 17 00:00:00 2001 From: Adam Egyed Date: Wed, 24 Jun 2020 23:03:15 +0000 Subject: [PATCH 07/12] feat: added debug logging to each resource detector --- .../platform/node/detectors/AwsEc2Detector.ts | 5 +- .../platform/node/detectors/EnvDetector.ts | 14 ++- .../platform/node/detectors/GcpDetector.ts | 7 +- .../test/detect-resources.test.ts | 96 +++++++++++++++++-- 4 files changed, 112 insertions(+), 10 deletions(-) diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts index 145cfd6e9c..70533a08f5 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts @@ -57,7 +57,10 @@ class AwsEc2Detector implements Detector { [HOST_RESOURCE.ID]: instanceId, [HOST_RESOURCE.TYPE]: instanceType, }); - } catch { + } catch (e) { + if (config.logger) { + config.logger.debug(`AwsEc2Detector failed: ${e.message}`); + } return Resource.empty(); } } diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts index 6c0e9e6698..045fac14b8 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts @@ -50,12 +50,22 @@ class EnvDetector implements Detector { async detect(config: ResourceDetectionConfig = {}): Promise { try { const labelString = process.env.OTEL_RESOURCE_LABELS; - if (!labelString) return Resource.empty(); + if (!labelString) { + if (config.logger) { + config.logger.debug( + 'EnvDetector failed: Environmnet variable "OTEL_RESOURCE_LABELS" is missing.' + ); + } + return Resource.empty(); + } const labels = this._parseResourceLabels( process.env.OTEL_RESOURCE_LABELS ); return new Resource(labels); - } catch { + } catch (e) { + if (config.logger) { + config.logger.debug(`EnvDetector failed: ${e.message}`); + } return Resource.empty(); } } diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts index 1eed30deb5..bc2a4eb490 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts @@ -33,7 +33,12 @@ import { ResourceDetectionConfig } from '../../../config'; */ class GcpDetector implements Detector { async detect(config: ResourceDetectionConfig = {}): Promise { - if (!(await gcpMetadata.isAvailable())) return Resource.empty(); + if (!(await gcpMetadata.isAvailable())) { + if (config.logger) { + config.logger.debug('GcpDetector failed: GCP Metadata unavailable.'); + } + return Resource.empty(); + } const [projectId, instanceId, zoneId, clusterName] = await Promise.all([ this._getProjectId(), diff --git a/packages/opentelemetry-resources/test/detect-resources.test.ts b/packages/opentelemetry-resources/test/detect-resources.test.ts index a9965b1e05..7dc47aa047 100644 --- a/packages/opentelemetry-resources/test/detect-resources.test.ts +++ b/packages/opentelemetry-resources/test/detect-resources.test.ts @@ -165,7 +165,26 @@ describe('detectResources', async () => { }); describe('with a debug logger', () => { - it('prints detected resources to the logger', async () => { + // Local functions to test if a mocked method is ever called with a specific argument or regex matching for an argument. + // Needed because of race condition with parallel detectors. + const callArgsContains = ( + mockedFunction: sinon.SinonSpy, + arg: any + ): boolean => { + return mockedFunction.getCalls().some(call => { + return call.args.some(callarg => arg === callarg); + }); + }; + const callArgsMatches = ( + mockedFunction: sinon.SinonSpy, + regex: RegExp + ): boolean => { + return mockedFunction.getCalls().some(call => { + return regex.test(call.args.toString()); + }); + }; + + it('prints detected resources and debug messages to the logger', async () => { // This test depends on the env detector to be functioning as intended const mockedLoggerMethod = sinon.fake(); await detectResources({ @@ -177,15 +196,80 @@ describe('detectResources', async () => { }, }); - assert.deepStrictEqual(mockedLoggerMethod.getCall(0).args, [ - 'EnvDetector found resource.', - ]); + // Test for AWS and GCP Detector failure + assert.ok( + callArgsContains( + mockedLoggerMethod, + 'GcpDetector failed: GCP Metadata unavailable.' + ) + ); + assert.ok( + callArgsContains( + mockedLoggerMethod, + 'AwsEc2Detector failed: Nock: Disallowed net connect for "169.254.169.254:80/latest/dynamic/instance-identity/document"' + ) + ); + // Test that the Env Detector successfully found its resource and populated it with the right values. + assert.ok( + callArgsContains(mockedLoggerMethod, 'EnvDetector found resource.') + ); // Regex formatting accounts for whitespace variations in util.inspect output over different node versions assert.ok( - /{\s+'service\.instance\.id':\s+'627cc493',\s+'service\.name':\s+'my-service',\s+'service\.namespace':\s+'default',\s+'service\.version':\s+'0\.0\.1'\s+}\s*/.test( - mockedLoggerMethod.getCall(1).args.toString() + callArgsMatches( + mockedLoggerMethod, + /{\s+'service\.instance\.id':\s+'627cc493',\s+'service\.name':\s+'my-service',\s+'service\.namespace':\s+'default',\s+'service\.version':\s+'0\.0\.1'\s+}\s*/ ) ); }); + + describe('with missing environemnt variable', () => { + beforeEach(() => { + delete process.env.OTEL_RESOURCE_LABELS; + }); + + it('prints correct error messages when EnvDetector has no env variable', async () => { + const mockedLoggerMethod = sinon.fake(); + await detectResources({ + logger: { + debug: mockedLoggerMethod, + info: sinon.fake(), + warn: sinon.fake(), + error: sinon.fake(), + }, + }); + + assert.ok( + callArgsContains( + mockedLoggerMethod, + 'EnvDetector failed: Environmnet variable "OTEL_RESOURCE_LABELS" is missing.' + ) + ); + }); + }); + + describe('with a faulty environment variable', () => { + beforeEach(() => { + process.env.OTEL_RESOURCE_LABELS = 'bad=~label'; + }); + + it('prints correct error messages when EnvDetector has an invalid variable', async () => { + const mockedLoggerMethod = sinon.fake(); + await detectResources({ + logger: { + debug: mockedLoggerMethod, + info: sinon.fake(), + warn: sinon.fake(), + error: sinon.fake(), + }, + }); + + assert.ok( + callArgsContains( + mockedLoggerMethod, + 'EnvDetector failed: Label value should be a ASCII string with a length not exceed 255 characters.' + ) + ); + }); + }); }); }); From 3c7aa9c45604606e82d6ec057fbf96aa52d16aec Mon Sep 17 00:00:00 2001 From: Adam Egyed Date: Fri, 26 Jun 2020 00:31:06 +0000 Subject: [PATCH 08/12] refactor: resource detection config reorganized Signed-off-by: Adam Egyed --- .../opentelemetry-resources/src/config.ts | 6 ++ .../src/platform/node/detect-resources.ts | 59 +++++++++++-------- .../platform/node/detectors/AwsEc2Detector.ts | 11 ++-- .../platform/node/detectors/EnvDetector.ts | 19 +++--- .../platform/node/detectors/GcpDetector.ts | 11 ++-- packages/opentelemetry-resources/src/types.ts | 4 +- 6 files changed, 63 insertions(+), 47 deletions(-) diff --git a/packages/opentelemetry-resources/src/config.ts b/packages/opentelemetry-resources/src/config.ts index a6d7a91177..1f98b9718e 100644 --- a/packages/opentelemetry-resources/src/config.ts +++ b/packages/opentelemetry-resources/src/config.ts @@ -23,3 +23,9 @@ export interface ResourceDetectionConfig { /** Optional Logger. */ logger?: Logger; } + +export interface ResourceDetectionConfigWithLogger + extends ResourceDetectionConfig { + /** Required Logger */ + logger: Logger; +} diff --git a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts index b4af7c40e9..6ac4f3553d 100644 --- a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts +++ b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts @@ -17,9 +17,13 @@ import { Resource } from '../../Resource'; import { envDetector, awsEc2Detector, gcpDetector } from './detectors'; import { Detector } from '../../types'; -import { ResourceDetectionConfig } from '../../config'; +import { + ResourceDetectionConfig, + ResourceDetectionConfigWithLogger, +} from '../../config'; import { Logger } from '@opentelemetry/api'; import * as util from 'util'; +import { NoopLogger } from '@opentelemetry/core'; const DETECTORS: Array = [envDetector, awsEc2Detector, gcpDetector]; @@ -32,16 +36,26 @@ const DETECTORS: Array = [envDetector, awsEc2Detector, gcpDetector]; export const detectResources = async ( config: ResourceDetectionConfig = {} ): Promise => { + const internalConfig: ResourceDetectionConfigWithLogger = Object.assign( + { + logger: new NoopLogger(), + }, + config + ); + const resources: Array = await Promise.all( DETECTORS.map(d => { try { - return d.detect(config); + return d.detect(internalConfig); } catch { return Resource.empty(); } }) ); - logResources(config.logger, resources); + // Log Resources only if there is a user-provided logger + if (config.logger) { + logResources(config.logger, resources); + } return resources.reduce( (acc, resource) => acc.merge(resource), Resource.createTelemetrySDKResource() @@ -54,26 +68,21 @@ export const detectResources = async ( * @param logger The logger to write the debug information to. * @param resources The array of resources that should be logged. Empty entried will be ignored. */ -const logResources = ( - logger: Logger | undefined, - resources: Array -) => { - if (logger) { - resources.forEach((resource, index) => { - // Only print populated resources - if (Object.keys(resource.labels).length > 0) { - const resourceDebugString = util.inspect(resource.labels, { - depth: 2, - breakLength: Infinity, - sorted: true, - compact: false, - }); - const detectorName = DETECTORS[index].constructor - ? DETECTORS[index].constructor.name - : 'Unknown detector'; - logger.debug(`${detectorName} found resource.`); - logger.debug(resourceDebugString); - } - }); - } +const logResources = (logger: Logger, resources: Array) => { + resources.forEach((resource, index) => { + // Print only populated resources + if (Object.keys(resource.labels).length > 0) { + const resourceDebugString = util.inspect(resource.labels, { + depth: 2, + breakLength: Infinity, + sorted: true, + compact: false, + }); + const detectorName = DETECTORS[index].constructor + ? DETECTORS[index].constructor.name + : 'Unknown detector'; + logger.debug(`${detectorName} found resource.`); + logger.debug(resourceDebugString); + } + }); }; diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts index 70533a08f5..31aaf374b2 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts @@ -18,7 +18,8 @@ import * as http from 'http'; import { Resource } from '../../../Resource'; import { CLOUD_RESOURCE, HOST_RESOURCE } from '../../../constants'; import { Detector } from '../../../types'; -import { ResourceDetectionConfig } from '../../../config'; +import { ResourceDetectionConfigWithLogger } from '../../../config'; +import { NoopLogger } from '@opentelemetry/core'; /** * The AwsEc2Detector can be used to detect if a process is running in AWS EC2 @@ -40,7 +41,9 @@ class AwsEc2Detector implements Detector { * empty {@link Resource} if the connection or parsing of the identity * document fails. */ - async detect(config: ResourceDetectionConfig = {}): Promise { + async detect( + config: ResourceDetectionConfigWithLogger = { logger: new NoopLogger() } + ): Promise { try { const { accountId, @@ -58,9 +61,7 @@ class AwsEc2Detector implements Detector { [HOST_RESOURCE.TYPE]: instanceType, }); } catch (e) { - if (config.logger) { - config.logger.debug(`AwsEc2Detector failed: ${e.message}`); - } + config.logger.debug(`AwsEc2Detector failed: ${e.message}`); return Resource.empty(); } } diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts index 045fac14b8..568ec0b143 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts @@ -16,7 +16,8 @@ import { Resource } from '../../../Resource'; import { Detector, ResourceLabels } from '../../../types'; -import { ResourceDetectionConfig } from '../../../config'; +import { ResourceDetectionConfigWithLogger } from '../../../config'; +import { NoopLogger } from '@opentelemetry/core'; /** * EnvDetector can be used to detect the presence of and create a Resource @@ -47,15 +48,15 @@ class EnvDetector implements Detector { * OTEL_RESOURCE_LABELS environment variable. Note this is an async function * to conform to the Detector interface. */ - async detect(config: ResourceDetectionConfig = {}): Promise { + async detect( + config: ResourceDetectionConfigWithLogger = { logger: new NoopLogger() } + ): Promise { try { const labelString = process.env.OTEL_RESOURCE_LABELS; if (!labelString) { - if (config.logger) { - config.logger.debug( - 'EnvDetector failed: Environmnet variable "OTEL_RESOURCE_LABELS" is missing.' - ); - } + config.logger.debug( + 'EnvDetector failed: Environmnet variable "OTEL_RESOURCE_LABELS" is missing.' + ); return Resource.empty(); } const labels = this._parseResourceLabels( @@ -63,9 +64,7 @@ class EnvDetector implements Detector { ); return new Resource(labels); } catch (e) { - if (config.logger) { - config.logger.debug(`EnvDetector failed: ${e.message}`); - } + config.logger.debug(`EnvDetector failed: ${e.message}`); return Resource.empty(); } } diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts index bc2a4eb490..f2b8168931 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts @@ -24,7 +24,8 @@ import { K8S_RESOURCE, CONTAINER_RESOURCE, } from '../../../constants'; -import { ResourceDetectionConfig } from '../../../config'; +import { ResourceDetectionConfigWithLogger } from '../../../config'; +import { NoopLogger } from '@opentelemetry/core'; /** * The GcpDetector can be used to detect if a process is running in the Google @@ -32,11 +33,11 @@ import { ResourceDetectionConfig } from '../../../config'; * the instance. Returns an empty Resource if detection fails. */ class GcpDetector implements Detector { - async detect(config: ResourceDetectionConfig = {}): Promise { + async detect( + config: ResourceDetectionConfigWithLogger = { logger: new NoopLogger() } + ): Promise { if (!(await gcpMetadata.isAvailable())) { - if (config.logger) { - config.logger.debug('GcpDetector failed: GCP Metadata unavailable.'); - } + config.logger.debug('GcpDetector failed: GCP Metadata unavailable.'); return Resource.empty(); } diff --git a/packages/opentelemetry-resources/src/types.ts b/packages/opentelemetry-resources/src/types.ts index f294e8f923..c33562312f 100644 --- a/packages/opentelemetry-resources/src/types.ts +++ b/packages/opentelemetry-resources/src/types.ts @@ -15,7 +15,7 @@ */ import { Resource } from './Resource'; -import { ResourceDetectionConfig } from './config'; +import { ResourceDetectionConfigWithLogger } from './config'; /** Interface for Resource labels */ export interface ResourceLabels { @@ -27,5 +27,5 @@ export interface ResourceLabels { * a detector returns a Promise containing a Resource. */ export interface Detector { - detect(config?: ResourceDetectionConfig): Promise; + detect(config: ResourceDetectionConfigWithLogger): Promise; } From 665fb6634106934135285bb391db6910653acbfb Mon Sep 17 00:00:00 2001 From: Adam Egyed Date: Fri, 26 Jun 2020 18:50:45 +0000 Subject: [PATCH 09/12] fix: removed default parameter from detectors Signed-off-by: Adam Egyed --- .../src/platform/node/detectors/AwsEc2Detector.ts | 5 +---- .../src/platform/node/detectors/EnvDetector.ts | 5 +---- .../src/platform/node/detectors/GcpDetector.ts | 5 +---- .../test/detectors/AwsEc2Detector.test.ts | 9 +++++++-- .../test/detectors/EnvDetector.test.ts | 9 +++++++-- .../test/detectors/GcpDetector.test.ts | 11 +++++++---- 6 files changed, 24 insertions(+), 20 deletions(-) diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts index 31aaf374b2..7de9f06269 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts @@ -19,7 +19,6 @@ import { Resource } from '../../../Resource'; import { CLOUD_RESOURCE, HOST_RESOURCE } from '../../../constants'; import { Detector } from '../../../types'; import { ResourceDetectionConfigWithLogger } from '../../../config'; -import { NoopLogger } from '@opentelemetry/core'; /** * The AwsEc2Detector can be used to detect if a process is running in AWS EC2 @@ -41,9 +40,7 @@ class AwsEc2Detector implements Detector { * empty {@link Resource} if the connection or parsing of the identity * document fails. */ - async detect( - config: ResourceDetectionConfigWithLogger = { logger: new NoopLogger() } - ): Promise { + async detect(config: ResourceDetectionConfigWithLogger): Promise { try { const { accountId, diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts index 568ec0b143..908dabb9c8 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts @@ -17,7 +17,6 @@ import { Resource } from '../../../Resource'; import { Detector, ResourceLabels } from '../../../types'; import { ResourceDetectionConfigWithLogger } from '../../../config'; -import { NoopLogger } from '@opentelemetry/core'; /** * EnvDetector can be used to detect the presence of and create a Resource @@ -48,9 +47,7 @@ class EnvDetector implements Detector { * OTEL_RESOURCE_LABELS environment variable. Note this is an async function * to conform to the Detector interface. */ - async detect( - config: ResourceDetectionConfigWithLogger = { logger: new NoopLogger() } - ): Promise { + async detect(config: ResourceDetectionConfigWithLogger): Promise { try { const labelString = process.env.OTEL_RESOURCE_LABELS; if (!labelString) { diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts index f2b8168931..295e21e39c 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts @@ -25,7 +25,6 @@ import { CONTAINER_RESOURCE, } from '../../../constants'; import { ResourceDetectionConfigWithLogger } from '../../../config'; -import { NoopLogger } from '@opentelemetry/core'; /** * The GcpDetector can be used to detect if a process is running in the Google @@ -33,9 +32,7 @@ import { NoopLogger } from '@opentelemetry/core'; * the instance. Returns an empty Resource if detection fails. */ class GcpDetector implements Detector { - async detect( - config: ResourceDetectionConfigWithLogger = { logger: new NoopLogger() } - ): Promise { + async detect(config: ResourceDetectionConfigWithLogger): Promise { if (!(await gcpMetadata.isAvailable())) { config.logger.debug('GcpDetector failed: GCP Metadata unavailable.'); return Resource.empty(); diff --git a/packages/opentelemetry-resources/test/detectors/AwsEc2Detector.test.ts b/packages/opentelemetry-resources/test/detectors/AwsEc2Detector.test.ts index b60a6cc7f6..9b9e6eb035 100644 --- a/packages/opentelemetry-resources/test/detectors/AwsEc2Detector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/AwsEc2Detector.test.ts @@ -24,6 +24,7 @@ import { assertHostResource, assertEmptyResource, } from '../util/resource-assertions'; +import { NoopLogger } from '@opentelemetry/core'; const { origin: AWS_HOST, pathname: AWS_PATH } = new URL( awsEc2Detector.AWS_INSTANCE_IDENTITY_DOCUMENT_URI @@ -52,7 +53,9 @@ describe('awsEc2Detector', () => { const scope = nock(AWS_HOST) .get(AWS_PATH) .reply(200, () => mockedAwsResponse); - const resource: Resource = await awsEc2Detector.detect(); + const resource: Resource = await awsEc2Detector.detect({ + logger: new NoopLogger(), + }); scope.done(); assert.ok(resource); @@ -74,7 +77,9 @@ describe('awsEc2Detector', () => { const scope = nock(AWS_HOST).get(AWS_PATH).replyWithError({ code: 'ENOTFOUND', }); - const resource: Resource = await awsEc2Detector.detect(); + const resource: Resource = await awsEc2Detector.detect({ + logger: new NoopLogger(), + }); scope.done(); assert.ok(resource); diff --git a/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts b/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts index 20a63e64d5..894de6e870 100644 --- a/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/EnvDetector.test.ts @@ -21,6 +21,7 @@ import { assertEmptyResource, } from '../util/resource-assertions'; import { K8S_RESOURCE } from '../../src'; +import { NoopLogger } from '@opentelemetry/core'; describe('envDetector()', () => { describe('with valid env', () => { @@ -34,7 +35,9 @@ describe('envDetector()', () => { }); it('should return resource information from environment variable', async () => { - const resource: Resource = await envDetector.detect(); + const resource: Resource = await envDetector.detect({ + logger: new NoopLogger(), + }); assertK8sResource(resource, { [K8S_RESOURCE.POD_NAME]: 'pod-xyz-123', [K8S_RESOURCE.CLUSTER_NAME]: 'c1', @@ -45,7 +48,9 @@ describe('envDetector()', () => { describe('with empty env', () => { it('should return empty resource', async () => { - const resource: Resource = await envDetector.detect(); + const resource: Resource = await envDetector.detect({ + logger: new NoopLogger(), + }); assertEmptyResource(resource); }); }); diff --git a/packages/opentelemetry-resources/test/detectors/GcpDetector.test.ts b/packages/opentelemetry-resources/test/detectors/GcpDetector.test.ts index 75adb78980..c476feb5a7 100644 --- a/packages/opentelemetry-resources/test/detectors/GcpDetector.test.ts +++ b/packages/opentelemetry-resources/test/detectors/GcpDetector.test.ts @@ -32,6 +32,7 @@ import { assertContainerResource, assertEmptyResource, } from '../util/resource-assertions'; +import { NoopLogger } from '@opentelemetry/core'; const HEADERS = { [HEADER_NAME.toLowerCase()]: HEADER_VALUE, @@ -80,7 +81,9 @@ describe('gcpDetector', () => { const secondaryScope = nock(SECONDARY_HOST_ADDRESS) .get(INSTANCE_PATH) .reply(200, {}, HEADERS); - const resource: Resource = await gcpDetector.detect(); + const resource: Resource = await gcpDetector.detect({ + logger: new NoopLogger(), + }); secondaryScope.done(); scope.done(); @@ -111,7 +114,7 @@ describe('gcpDetector', () => { const secondaryScope = nock(SECONDARY_HOST_ADDRESS) .get(INSTANCE_PATH) .reply(200, {}, HEADERS); - const resource = await gcpDetector.detect(); + const resource = await gcpDetector.detect({ logger: new NoopLogger() }); secondaryScope.done(); scope.done(); @@ -143,7 +146,7 @@ describe('gcpDetector', () => { const secondaryScope = nock(SECONDARY_HOST_ADDRESS) .get(INSTANCE_PATH) .reply(200, {}, HEADERS); - const resource = await gcpDetector.detect(); + const resource = await gcpDetector.detect({ logger: new NoopLogger() }); secondaryScope.done(); scope.done(); @@ -155,7 +158,7 @@ describe('gcpDetector', () => { }); it('returns empty resource if not detected', async () => { - const resource = await gcpDetector.detect(); + const resource = await gcpDetector.detect({ logger: new NoopLogger() }); assertEmptyResource(resource); }); }); From 6140c2eada92689c604193ce6aa77c0d9a88d685 Mon Sep 17 00:00:00 2001 From: Adam Egyed Date: Fri, 26 Jun 2020 20:45:31 +0000 Subject: [PATCH 10/12] chore: add tsdoc comments Signed-off-by: Adam Egyed --- packages/opentelemetry-resources/src/config.ts | 4 ++++ .../src/platform/node/detect-resources.ts | 6 +++--- .../src/platform/node/detectors/AwsEc2Detector.ts | 2 ++ .../src/platform/node/detectors/EnvDetector.ts | 2 ++ .../src/platform/node/detectors/GcpDetector.ts | 8 ++++++++ 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/opentelemetry-resources/src/config.ts b/packages/opentelemetry-resources/src/config.ts index 1f98b9718e..8eb9007eb6 100644 --- a/packages/opentelemetry-resources/src/config.ts +++ b/packages/opentelemetry-resources/src/config.ts @@ -24,6 +24,10 @@ export interface ResourceDetectionConfig { logger?: Logger; } +/** + * ResourceDetectionConfigWithLogger provides an interface for interacting with + * {@link ResourceDetectionConfig} instances that must have a logger defined. + */ export interface ResourceDetectionConfigWithLogger extends ResourceDetectionConfig { /** Required Logger */ diff --git a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts index 6ac4f3553d..ca7add14b2 100644 --- a/packages/opentelemetry-resources/src/platform/node/detect-resources.ts +++ b/packages/opentelemetry-resources/src/platform/node/detect-resources.ts @@ -31,7 +31,7 @@ const DETECTORS: Array = [envDetector, awsEc2Detector, gcpDetector]; * Runs all resource detectors and returns the results merged into a single * Resource. * - * @param config Configuration object for resource detection + * @param config Configuration for resource detection */ export const detectResources = async ( config: ResourceDetectionConfig = {} @@ -65,8 +65,8 @@ export const detectResources = async ( /** * Writes debug information about the detected resources to the logger defined in the resource detection config, if one is provided. * - * @param logger The logger to write the debug information to. - * @param resources The array of resources that should be logged. Empty entried will be ignored. + * @param logger The {@link Logger} to write the debug information to. + * @param resources The array of {@link Resource} that should be logged. Empty entried will be ignored. */ const logResources = (logger: Logger, resources: Array) => { resources.forEach((resource, index) => { diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts index 7de9f06269..76fd11527a 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/AwsEc2Detector.ts @@ -39,6 +39,8 @@ class AwsEc2Detector implements Detector { * populated with instance metadata as labels. Returns a promise containing an * empty {@link Resource} if the connection or parsing of the identity * document fails. + * + * @param config The resource detection config with a required logger */ async detect(config: ResourceDetectionConfigWithLogger): Promise { try { diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts index 908dabb9c8..4fb8d1c349 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts @@ -46,6 +46,8 @@ class EnvDetector implements Detector { * Returns a {@link Resource} populated with labels from the * OTEL_RESOURCE_LABELS environment variable. Note this is an async function * to conform to the Detector interface. + * + * @param config The resource detection config with a required logger */ async detect(config: ResourceDetectionConfigWithLogger): Promise { try { diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts index 295e21e39c..a93173d1a1 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/GcpDetector.ts @@ -32,6 +32,14 @@ import { ResourceDetectionConfigWithLogger } from '../../../config'; * the instance. Returns an empty Resource if detection fails. */ class GcpDetector implements Detector { + /** + * Attempts to connect and obtain instance configuration data from the GCP metadata service. + * If the connection is succesful it returns a promise containing a {@link Resource} + * populated with instance metadata as labels. Returns a promise containing an + * empty {@link Resource} if the connection or parsing of the metadata fails. + * + * @param config The resource detection config with a required logger + */ async detect(config: ResourceDetectionConfigWithLogger): Promise { if (!(await gcpMetadata.isAvailable())) { config.logger.debug('GcpDetector failed: GCP Metadata unavailable.'); From afd3e08e6a17c93313807a53d9acdcc79fb28659 Mon Sep 17 00:00:00 2001 From: Adam Egyed Date: Fri, 26 Jun 2020 21:06:23 +0000 Subject: [PATCH 11/12] fix: logger typo Signed-off-by: Adam Egyed --- .../src/platform/node/detectors/EnvDetector.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts b/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts index 4fb8d1c349..718c566f12 100644 --- a/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts +++ b/packages/opentelemetry-resources/src/platform/node/detectors/EnvDetector.ts @@ -54,7 +54,7 @@ class EnvDetector implements Detector { const labelString = process.env.OTEL_RESOURCE_LABELS; if (!labelString) { config.logger.debug( - 'EnvDetector failed: Environmnet variable "OTEL_RESOURCE_LABELS" is missing.' + 'EnvDetector failed: Environment variable "OTEL_RESOURCE_LABELS" is missing.' ); return Resource.empty(); } From 8c2a95ef2835716b6c748ebf7c7c56ff6cd7c454 Mon Sep 17 00:00:00 2001 From: Adam Egyed Date: Fri, 26 Jun 2020 21:16:36 +0000 Subject: [PATCH 12/12] fix: adjust typo in unit test Signed-off-by: Adam Egyed --- packages/opentelemetry-resources/test/detect-resources.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opentelemetry-resources/test/detect-resources.test.ts b/packages/opentelemetry-resources/test/detect-resources.test.ts index 7dc47aa047..b607efa0d6 100644 --- a/packages/opentelemetry-resources/test/detect-resources.test.ts +++ b/packages/opentelemetry-resources/test/detect-resources.test.ts @@ -241,7 +241,7 @@ describe('detectResources', async () => { assert.ok( callArgsContains( mockedLoggerMethod, - 'EnvDetector failed: Environmnet variable "OTEL_RESOURCE_LABELS" is missing.' + 'EnvDetector failed: Environment variable "OTEL_RESOURCE_LABELS" is missing.' ) ); });