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

fix: remove aws and gcp detector from SDK #3024

Merged
merged 4 commits into from
Jun 8, 2022
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
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ All notable changes to experimental packages in this project will be documented

### :boom: Breaking Change

* fix: remove aws and gcp detector from SDK #3024 @flarna

### :rocket: (Enhancement)

### :bug: (Bug Fix)
Expand Down
5 changes: 1 addition & 4 deletions experimental/packages/opentelemetry-sdk-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
"@opentelemetry/api-metrics": "0.29.2",
"@opentelemetry/core": "1.3.1",
"@opentelemetry/instrumentation": "0.29.2",
"@opentelemetry/resource-detector-aws": "~1.1.0",
"@opentelemetry/resource-detector-gcp": "~0.27.0",
"@opentelemetry/resources": "1.3.1",
"@opentelemetry/sdk-metrics-base": "0.29.2",
"@opentelemetry/sdk-trace-base": "1.3.1",
Expand All @@ -60,15 +58,14 @@
"devDependencies": {
"@opentelemetry/api": ">=1.0.0 <1.2.0",
"@opentelemetry/context-async-hooks": "1.3.1",
"@opentelemetry/semantic-conventions": "1.3.1",
"@types/mocha": "8.2.3",
"@types/node": "14.17.33",
"@types/semver": "7.3.9",
"@types/sinon": "10.0.6",
"codecov": "3.8.3",
"gcp-metadata": "^4.1.4",
"istanbul-instrumenter-loader": "3.0.1",
"mocha": "7.2.0",
"nock": "13.0.11",
"nyc": "15.1.0",
"semver": "7.3.5",
"sinon": "12.0.1",
Expand Down
4 changes: 1 addition & 3 deletions experimental/packages/opentelemetry-sdk-node/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import {
registerInstrumentations,
} from '@opentelemetry/instrumentation';
import { NodeTracerConfig, NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { awsEc2Detector } from '@opentelemetry/resource-detector-aws';
import { gcpDetector } from '@opentelemetry/resource-detector-gcp';
import {
detectResources,
envDetector,
Expand Down Expand Up @@ -117,7 +115,7 @@ export class NodeSDK {
/** Detect resource attributes */
public async detectResources(config?: ResourceDetectionConfig): Promise<void> {
const internalConfig: ResourceDetectionConfig = {
detectors: [awsEc2Detector, gcpDetector, envDetector, processDetector],
detectors: [ envDetector, processDetector],
...config,
};

Expand Down
183 changes: 8 additions & 175 deletions experimental/packages/opentelemetry-sdk-node/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,56 +30,19 @@ import {
import { CompositePropagator } from '@opentelemetry/core';
import { ConsoleMetricExporter, MeterProvider, PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics-base';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { awsEc2Detector } from '@opentelemetry/resource-detector-aws';
import { resetIsAvailableCache } from '@opentelemetry/resource-detector-gcp';
import { Resource } from '@opentelemetry/resources';
import {
assertCloudResource,
assertHostResource,
assertServiceResource,
} from './util/resource-assertions';
import {
ConsoleSpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import * as assert from 'assert';
import {
BASE_PATH,
HEADER_NAME,
HEADER_VALUE,
HOST_ADDRESS,
SECONDARY_HOST_ADDRESS,
} from 'gcp-metadata';
import * as nock from 'nock';
import * as semver from 'semver';
import * as Sinon from 'sinon';
import { NodeSDK } from '../src';
import { envDetector, processDetector } from '@opentelemetry/resources';

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 AWS_HOST = 'http://' + awsEc2Detector.AWS_IDMS_ENDPOINT;
const AWS_TOKEN_PATH = awsEc2Detector.AWS_INSTANCE_TOKEN_DOCUMENT_PATH;
const AWS_IDENTITY_PATH = awsEc2Detector.AWS_INSTANCE_IDENTITY_DOCUMENT_PATH;
const AWS_HOST_PATH = awsEc2Detector.AWS_INSTANCE_HOST_DOCUMENT_PATH;
const AWS_METADATA_TTL_HEADER = awsEc2Detector.AWS_METADATA_TTL_HEADER;
const AWS_METADATA_TOKEN_HEADER = awsEc2Detector.AWS_METADATA_TOKEN_HEADER;

const mockedTokenResponse = 'my-token';
const mockedIdentityResponse = {
instanceId: 'my-instance-id',
instanceType: 'my-instance-type',
accountId: 'my-account-id',
region: 'my-region',
availabilityZone: 'my-zone',
};
const mockedHostResponse = 'my-hostname';

const DefaultContextManager = semver.gte(process.version, '14.8.0')
? AsyncLocalStorageContextManager
Expand All @@ -90,10 +53,6 @@ describe('Node SDK', () => {
let propagator: any;
let delegate: any;

before(() => {
nock.disableNetConnect();
});

beforeEach(() => {
context.disable();
trace.disable();
Expand Down Expand Up @@ -190,138 +149,27 @@ describe('Node SDK', () => {

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

afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
delete process.env.OTEL_RESOURCE_ATTRIBUTES;
});

// GCP detector only works in 10+
(semver.satisfies(process.version, '>=10') ? describe : describe.skip)(
'in GCP environment',
() => {
after(() => {
resetIsAvailableCache();
});

it('returns a merged resource', async () => {
const sdk = new NodeSDK({
autoDetectResources: true,
});
const gcpScope = 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 gcpSecondaryScope = nock(SECONDARY_HOST_ADDRESS)
.get(INSTANCE_PATH)
.reply(200, {}, HEADERS);
const awsScope = nock(AWS_HOST)
.persist()
.put(AWS_TOKEN_PATH)
.matchHeader(AWS_METADATA_TTL_HEADER, '60')
.replyWithError({ code: 'ENOTFOUND' });
await sdk.detectResources();
const resource = sdk['_resource'];

awsScope.done();
gcpSecondaryScope.done();
gcpScope.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', () => {
describe('with a buggy detector', () => {
it('returns a merged resource', async () => {
const sdk = new NodeSDK({
autoDetectResources: true,
});
const awsScope = nock(AWS_HOST)
.persist()
.put(AWS_TOKEN_PATH)
.matchHeader(AWS_METADATA_TTL_HEADER, '60')
.reply(200, () => mockedTokenResponse)
.get(AWS_IDENTITY_PATH)
.matchHeader(AWS_METADATA_TOKEN_HEADER, mockedTokenResponse)
.reply(200, () => mockedIdentityResponse)
.get(AWS_HOST_PATH)
.matchHeader(AWS_METADATA_TOKEN_HEADER, mockedTokenResponse)
.reply(200, () => mockedHostResponse);
await sdk.detectResources();
const resource: Resource = sdk['_resource'];
awsScope.done();

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

describe('in no environment', () => {
it('should return empty resource', async () => {
const scope = nock(AWS_HOST).put(AWS_TOKEN_PATH).replyWithError({
code: 'ENOTFOUND',
});
const sdk = new NodeSDK({
autoDetectResources: true,
});
await sdk.detectResources({
detectors: [awsEc2Detector],
});
const resource: Resource = sdk['_resource'];
assert.ok(resource);
assert.deepStrictEqual(resource, Resource.empty());

scope.done();
});
});

describe('with a buggy detector', () => {
it('returns a merged resource', async () => {
const sdk = new NodeSDK({
autoDetectResources: true,
detectors: [ processDetector, {
detect() {
throw new Error('Buggy detector');
}
},
envDetector ]
});
const stub = Sinon.stub(awsEc2Detector, 'detect').throws();
await sdk.detectResources();
const resource = sdk['_resource'];

assertServiceResource(resource, {
Expand All @@ -330,8 +178,6 @@ describe('Node SDK', () => {
namespace: 'default',
version: '0.0.1',
});

stub.restore();
});
});

Expand Down Expand Up @@ -373,19 +219,6 @@ describe('Node SDK', () => {

await sdk.detectResources();

// Test for AWS and GCP Detector failure
assert.ok(
callArgsContains(
mockedLoggerMethod,
'GcpDetector failed: GCP Metadata unavailable.'
)
);
assert.ok(
callArgsContains(
mockedLoggerMethod,
'AwsEc2Detector failed: Nock: Disallowed net connect for "169.254.169.254:80/latest/api/token"'
)
);
// Test that the Env Detector successfully found its resource and populated it with the right values.
assert.ok(
callArgsContains(mockedLoggerMethod, 'EnvDetector found resource.')
Expand Down
3 changes: 3 additions & 0 deletions experimental/packages/opentelemetry-sdk-node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
{
"path": "../../../packages/opentelemetry-sdk-trace-node"
},
{
"path": "../../../packages/opentelemetry-semantic-conventions"
},
{
"path": "../opentelemetry-api-metrics"
},
Expand Down