Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource auto detection logging #1211

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/opentelemetry-resources/src/platform/node/config.ts
Original file line number Diff line number Diff line change
@@ -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;
}
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) => {
adamegyed marked this conversation as resolved.
Show resolved Hide resolved
// Only print populated resources
adamegyed marked this conversation as resolved.
Show resolved Hide resolved
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!.debug(`${detectorName} found resource.`);
config.logger!.debug(resourceDebugString);
}
});
}
return resources.reduce(
(acc, resource) => acc.merge(resource),
Resource.createTelemetrySDKResource()
Expand Down
23 changes: 23 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,26 @@ 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 mockedLoggerMethod = sinon.fake();
await detectResources({
logger: {
debug: mockedLoggerMethod,
info: sinon.fake(),
warn: sinon.fake(),
error: sinon.fake(),
},
});

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}",
]);
});
});
});