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

feat: resource auto-detection #899

Merged
merged 21 commits into from
Apr 15, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: make detection platform aware; extract to detectResources() fn
  • Loading branch information
mwear committed Apr 12, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 9d540360f3a7b580bd173ad315bc374ead33fe09
4 changes: 4 additions & 0 deletions packages/opentelemetry-resources/package.json
Original file line number Diff line number Diff line change
@@ -3,6 +3,10 @@
"version": "0.6.1",
"description": "OpenTelemetry SDK resources",
"main": "build/src/index.js",
"browser": {
"./src/platform/index.ts": "./src/platform/browser/index.ts",
"./build/src/platform/index.js": "./build/src/platform/browser/index.js"
},
"types": "build/src/index.d.ts",
"repository": "open-telemetry/opentelemetry-js",
"scripts": {
17 changes: 0 additions & 17 deletions packages/opentelemetry-resources/src/Resource.ts
Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@

import { SDK_INFO } from '@opentelemetry/base';
import { TELEMETRY_SDK_RESOURCE } from './constants';
import { AwsEc2Detector, EnvDetector, GcpDetector } from './detectors';

/**
* A Resource describes the entity for which a signals (metrics or trace) are
@@ -43,22 +42,6 @@ export class Resource {
});
}

static DETECTORS = [EnvDetector, AwsEc2Detector, GcpDetector];

/**
* Runs all resource detectors and returns the results merged into a single
* Resource.
*/
static async detect(detectors = Resource.DETECTORS): Promise<Resource> {
const resources: Array<Resource> = await Promise.all(
detectors.map(d => d.detect())
);
return resources.reduce(
(acc, resource) => acc.merge(resource),
Resource.createTelemetrySDKResource()
);
}

constructor(
/**
* A dictionary of labels with string keys and values that provide information
2 changes: 1 addition & 1 deletion packages/opentelemetry-resources/src/index.ts
Original file line number Diff line number Diff line change
@@ -15,5 +15,5 @@
*/

export { Resource, Labels } from './Resource';
export * from './detectors';
export * from './platform';
export * from './constants';
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2020, 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 { Resource } from '../../Resource';

/**
* Detects resources for the browser platform, which is currently only the
* telemetry SDK resource. More could be added in the future. This method
* is async to match the signature of corresponding method for node.
*/
export const detectResources = async (): Promise<Resource> => {
return Resource.createTelemetrySDKResource();
};
17 changes: 17 additions & 0 deletions packages/opentelemetry-resources/src/platform/browser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*!
* Copyright 2020, 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.
*/

export * from './detect-resources';
20 changes: 20 additions & 0 deletions packages/opentelemetry-resources/src/platform/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*!
* Copyright 2020, 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.
*/

// Use the node platform by default. The "browser" field of package.json is used
// to override this file to use `./browser/index.ts` when packaged with
// webpack, Rollup, etc.
export * from './node';
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2020, 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 { Resource } from '../../Resource';
import { EnvDetector, AwsEc2Detector, GcpDetector } from './detectors';

const DETECTORS = [EnvDetector, AwsEc2Detector, GcpDetector];

/**
* Runs all resource detectors and returns the results merged into a single
* Resource.
*/
export const detectResources = async (): Promise<Resource> => {
const resources: Array<Resource> = await Promise.all(
DETECTORS.map(d => d.detect())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if a detector throws?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a try / catch.

);
return resources.reduce(
(acc, resource) => acc.merge(resource),
Resource.createTelemetrySDKResource()
);
};
Original file line number Diff line number Diff line change
@@ -15,8 +15,8 @@
*/

import * as http from 'http';
dyladan marked this conversation as resolved.
Show resolved Hide resolved
import { Resource } from '../Resource';
import { CLOUD_RESOURCE, HOST_RESOURCE } from '../constants';
import { Resource } from '../../../Resource';
import { CLOUD_RESOURCE, HOST_RESOURCE } from '../../../constants';

/**
* The AwsEc2Detector can be used to detect if a process is running in AWS EC2
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
* limitations under the License.
*/

import { Resource, Labels } from '../Resource';
import { Resource, Labels } from '../../../Resource';

/**
* EnvDetector can be used to detect the presence of and create a Resource
Original file line number Diff line number Diff line change
@@ -16,13 +16,13 @@

import * as os from 'os';
import * as gcpMetadata from 'gcp-metadata';
import { Resource, Labels } from '../Resource';
import { Resource, Labels } from '../../../Resource';
import {
CLOUD_RESOURCE,
HOST_RESOURCE,
K8S_RESOURCE,
CONTAINER_RESOURCE,
} from '../constants';
} from '../../../constants';

/**
* The GcpDetector can be used to detect if a process is running in the Google
18 changes: 18 additions & 0 deletions packages/opentelemetry-resources/src/platform/node/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*!
* Copyright 2020, 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.
*/

export * from './detect-resources';
export * from './detectors';
mwear marked this conversation as resolved.
Show resolved Hide resolved
33 changes: 2 additions & 31 deletions packages/opentelemetry-resources/test/Resource.test.ts
Original file line number Diff line number Diff line change
@@ -14,14 +14,10 @@
* limitations under the License.
*/

import * as nock from 'nock';
import * as assert from 'assert';
import { SDK_INFO } from '@opentelemetry/base';
import { Resource, EnvDetector, K8S_RESOURCE } from '../src';
import {
assertTelemetrySDKResource,
assertK8sResource,
} from './util/resource-assertions';
import { Resource } from '../src';
import { assertTelemetrySDKResource } from './util/resource-assertions';

describe('Resource', () => {
const resource1 = new Resource({
@@ -114,29 +110,4 @@ describe('Resource', () => {
});
});
});

describe('.detect', () => {
before(() => {
process.env.OTEL_RESOURCE_LABELS =
'k8s.pod.name=my-pod,k8s.cluster.name=my-cluster,k8s.namespace.name=default';
});

after(() => {
delete process.env.OTEL_RESOURCE_LABELS;
});

it('returns merged resource', async () => {
const resource = await Resource.detect([EnvDetector]);
assertTelemetrySDKResource(resource, {
language: SDK_INFO.LANGUAGE,
name: SDK_INFO.NAME,
version: SDK_INFO.VERSION,
});
assertK8sResource(resource, {
podName: 'my-pod',
clusterName: 'my-cluster',
namespaceName: 'default',
});
});
});
});
139 changes: 139 additions & 0 deletions packages/opentelemetry-resources/test/detect-resources.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*!
* Copyright 2020, 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 * as nock from 'nock';
import { Resource } from '../src';
import { detectResources, AwsEc2Detector } from '../src';
import {
assertServiceResource,
assertCloudResource,
assertHostResource,
} from './util/resource-assertions';
import {
BASE_PATH,
HEADER_NAME,
HEADER_VALUE,
HOST_ADDRESS,
SECONDARY_HOST_ADDRESS,
resetIsAvailableCache,
} from 'gcp-metadata';

const HEADERS = {
[HEADER_NAME.toLowerCase()]: HEADER_VALUE,
};
const INSTANCE_PATH = BASE_PATH + '/instance';
const INSTANCE_ID_PATH = BASE_PATH + '/instance/id';
const PROJECT_ID_PATH = BASE_PATH + '/project/project-id';
const ZONE_PATH = BASE_PATH + '/instance/zone';
const CLUSTER_NAME_PATH = BASE_PATH + '/instance/attributes/cluster-name';

const { origin: AWS_HOST, pathname: AWS_PATH } = new URL(
AwsEc2Detector.AWS_INSTANCE_IDENTITY_DOCUMENT_URI
);

const mockedAwsResponse = {
instanceId: 'my-instance-id',
accountId: 'my-account-id',
region: 'my-region',
};

describe('detectResource', async () => {
before(() => {
nock.disableNetConnect();
process.env.OTEL_RESOURCE_LABELS =
'service.instance.id=627cc493,service.name=my-service,service.namespace=default,service.version=0.0.1';
});

after(() => {
nock.cleanAll();
nock.enableNetConnect();
delete process.env.OTEL_RESOURCE_LABELS;
});

describe('in GCP environment', () => {
after(() => {
resetIsAvailableCache();
});

it('returns a merged resource', async () => {
const scope = nock(HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS)
.get(INSTANCE_ID_PATH)
.reply(200, () => 452003179927758, HEADERS)
.get(PROJECT_ID_PATH)
.reply(200, () => 'my-project-id', HEADERS)
.get(ZONE_PATH)
.reply(200, () => 'project/zone/my-zone', HEADERS)
.get(CLUSTER_NAME_PATH)
.reply(404);
const secondaryScope = nock(SECONDARY_HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS);
const resource: Resource = await detectResources();
secondaryScope.done();
scope.done();

assertCloudResource(resource, {
provider: 'gcp',
accountId: 'my-project-id',
zone: 'my-zone',
});
assertHostResource(resource, { id: '452003179927758' });
assertServiceResource(resource, {
instanceId: '627cc493',
name: 'my-service',
namespace: 'default',
version: '0.0.1',
});
});
});

describe('in AWS environment', () => {
it('returns a merged resource', async () => {
const gcpScope = nock(HOST_ADDRESS)
.get(INSTANCE_PATH)
.replyWithError({
code: 'ENOTFOUND',
});
const gcpSecondaryScope = nock(SECONDARY_HOST_ADDRESS)
.get(INSTANCE_PATH)
.replyWithError({
code: 'ENOTFOUND',
});
const awsScope = nock(AWS_HOST)
.get(AWS_PATH)
.reply(200, () => mockedAwsResponse);
const resource: Resource = await detectResources();
gcpSecondaryScope.done();
gcpScope.done();
awsScope.done();

assertCloudResource(resource, {
provider: 'aws',
accountId: 'my-account-id',
region: 'my-region',
});
assertHostResource(resource, { id: 'my-instance-id' });
assertServiceResource(resource, {
instanceId: '627cc493',
name: 'my-service',
namespace: 'default',
version: '0.0.1',
});
});
});
});
Loading