From e81575b32eb54c7d145e057acaff9d2b95574107 Mon Sep 17 00:00:00 2001 From: Adam Egyed Date: Fri, 19 Jun 2020 00:09:58 +0000 Subject: [PATCH] refactor: separated logging of resources from detection method Signed-off-by: Adam Egyed --- .../src/platform/node/detect-resources.ts | 22 +++++++++++++------ 1 file changed, 15 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 81ff7b69ec8..6b10b2b3162 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,18 @@ export const detectResources = async ( } }) ); - if (config.logger) { + logResources(config.logger, resources); + return resources.reduce( + (acc, resource) => acc.merge(resource), + Resource.createTelemetrySDKResource() + ); +}; + +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 +65,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() - ); };