Skip to content

Commit

Permalink
feat: add debug logging to resource auto-detection
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Egyed <[email protected]>
  • Loading branch information
adamegyed committed Jun 17, 2020
1 parent 5ae563e commit 07f0e0e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/opentelemetry-resources/src/platform/node/config.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Detector> = [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<Resource> => {
export const detectResources = async (
config: ResourceDetectionConfig = {}
): Promise<Resource> => {
const resources: Array<Resource> = await Promise.all(
DETECTORS.map(d => {
try {
Expand All @@ -34,6 +40,24 @@ export const detectResources = async (): Promise<Resource> => {
}
})
);
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()
Expand Down
16 changes: 16 additions & 0 deletions packages/opentelemetry-resources/test/detect-resources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}",
]);
});
});
});

0 comments on commit 07f0e0e

Please sign in to comment.