Skip to content

Commit

Permalink
refactor: separated logging of resources from detection method
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Egyed <[email protected]>
  • Loading branch information
adamegyed committed Jun 19, 2020
1 parent f0486e7 commit 4372c80
Showing 1 changed file with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Detector> = [envDetector, awsEc2Detector, gcpDetector];
Expand All @@ -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<Resource>
) => {
if (logger) {
resources.forEach((resource, index) => {
// Only print populated resources
if (Object.keys(resource.labels).length > 0) {
Expand All @@ -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()
);
};

0 comments on commit 4372c80

Please sign in to comment.