Skip to content

Commit

Permalink
Merge branch 'main' into instrumentation-sequelize
Browse files Browse the repository at this point in the history
  • Loading branch information
seemk committed Sep 18, 2024
2 parents f9efe6d + ee5c584 commit 558ffea
Show file tree
Hide file tree
Showing 25 changed files with 912 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"@opentelemetry/api": "^1.0.0"
},
"dependencies": {
"@opentelemetry/resources": "^1.0.0",
"@opentelemetry/resources": "^1.10.0",
"@opentelemetry/semantic-conventions": "^1.27.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/detectors/node/opentelemetry-resource-detector-alibaba-cloud#readme"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { context } from '@opentelemetry/api';
import { suppressTracing } from '@opentelemetry/core';
import {
DetectorSync,
IResource,
Expand Down Expand Up @@ -61,7 +63,10 @@ class AlibabaCloudEcsDetector implements DetectorSync {
* @param config (unused) The resource detection config
*/
detect(_config?: ResourceDetectionConfig): IResource {
return new Resource({}, this._getAttributes());
const attributes = context.with(suppressTracing(context.active()), () =>
this._getAttributes()
);
return new Resource({}, attributes);
}

/** Gets identity and host info and returns them as attribs. Empty object if fails */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 * as assert from 'assert';

import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import { NodeSDK } from '@opentelemetry/sdk-node';
import { IResource } from '@opentelemetry/resources';

describe('[Integration] AlibabaCloudEcsDetector', () => {
it('should not start spans for detector requests', async () => {
const memoryExporter = new InMemorySpanExporter();
const sdk = new NodeSDK({
instrumentations: [new HttpInstrumentation()],
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});

sdk.start();

// NOTE: detectors implementing the `DetectorSync` interface and starting
// HTTP requests within the `detect` method will produce Noop Spans since
// the SDK resolves the trace provider after resource detectors are triggered.
// Ref: https://github.com/open-telemetry/opentelemetry-js/blob/38f6689480d28dcbdafcb7b5ba4b14025328ffda/experimental/packages/opentelemetry-sdk-node/src/sdk.ts#L210-L240
//
// So having the detector in the config would result in no spans for Alibaba requests
// being exported which is what we want. Although we may think we're safe of sending
// internal tracing any change that delays these request will result in internal
// tracing being exported. We do the detection outside the SDK constructor to have such
// scenario.
const {
alibabaCloudEcsDetector,
} = require('../../build/src/detectors/AlibabaCloudEcsDetector');
const resource = alibabaCloudEcsDetector.detect() as IResource;
await resource.waitForAsyncAttributes?.();

// Wait for the next loop to let the span close properly
await new Promise(r => setTimeout(r, 0));
const spans = memoryExporter.getFinishedSpans();

assert.equal(spans.length, 0, 'no spans exported for GcpDetector');

await sdk.shutdown();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
* limitations under the License.
*/

import { diag } from '@opentelemetry/api';
import { context, diag } from '@opentelemetry/api';
import { suppressTracing } from '@opentelemetry/core';

import {
DetectorSync,
IResource,
Expand Down Expand Up @@ -63,7 +65,10 @@ export class AwsBeanstalkDetectorSync implements DetectorSync {
}

detect(config?: ResourceDetectionConfig): IResource {
return new Resource({}, this._getAttributes());
const attributes = context.with(suppressTracing(context.active()), () =>
this._getAttributes()
);
return new Resource({}, attributes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { context } from '@opentelemetry/api';
import { suppressTracing } from '@opentelemetry/core';
import {
DetectorSync,
IResource,
Expand Down Expand Up @@ -56,7 +58,10 @@ class AwsEc2DetectorSync implements DetectorSync {
readonly MILLISECOND_TIME_OUT = 5000;

detect(_config?: ResourceDetectionConfig): IResource {
return new Resource({}, this._getAttributes());
const attributes = context.with(suppressTracing(context.active()), () =>
this._getAttributes()
);
return new Resource({}, attributes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import { diag } from '@opentelemetry/api';
import { context, diag } from '@opentelemetry/api';
import { suppressTracing } from '@opentelemetry/core';
import {
DetectorSync,
IResource,
Expand Down Expand Up @@ -70,7 +71,10 @@ export class AwsEcsDetectorSync implements DetectorSync {
private static readFileAsync = util.promisify(fs.readFile);

detect(): IResource {
return new Resource({}, this._getAttributes());
const attributes = context.with(suppressTracing(context.active()), () =>
this._getAttributes()
);
return new Resource({}, attributes);
}

private async _getAttributes(): Promise<ResourceAttributes> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { context } from '@opentelemetry/api';
import { suppressTracing } from '@opentelemetry/core';
import {
DetectorSync,
IResource,
Expand Down Expand Up @@ -62,7 +64,10 @@ export class AwsEksDetectorSync implements DetectorSync {
private static fileAccessAsync = util.promisify(fs.access);

detect(_config?: ResourceDetectionConfig): IResource {
return new Resource({}, this._getAttributes());
const attributes = context.with(suppressTracing(context.active()), () =>
this._getAttributes()
);
return new Resource({}, attributes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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 * as assert from 'assert';

import { FsInstrumentation } from '@opentelemetry/instrumentation-fs';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import { NodeSDK } from '@opentelemetry/sdk-node';
import { DetectorSync } from '@opentelemetry/resources';

describe('[Integration] Internal tracing', () => {
it('should not start spans for any network or fs operation in any detector', async () => {
// For ECS detector we setup a mock URL to fetch metadata
process.env.ECS_CONTAINER_METADATA_URI_V4 =
'http://169.254.169.254/metadata';

const memoryExporter = new InMemorySpanExporter();
const sdk = new NodeSDK({
instrumentations: [new FsInstrumentation(), new HttpInstrumentation()],
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});
sdk.start();

// NOTE: detectors implementing the `DetectorSync` interface and starting
// HTTP requests within the `detect` method will produce Noop Spans since
// the SDK resolves the trace provider after resource detectors are triggered.
// Ref: https://github.com/open-telemetry/opentelemetry-js/blob/38f6689480d28dcbdafcb7b5ba4b14025328ffda/experimental/packages/opentelemetry-sdk-node/src/sdk.ts#L210-L240
//
// So having the detector in the config would result in no spans for Azure requests
// being exported which is what we want. Although we may think we're safe of sending
// internal tracing any change that delays these request will result in internal
// tracing being exported. We do the detection outside the SDK constructor to have such
// scenario.
const {
awsBeanstalkDetectorSync,
awsEc2DetectorSync,
awsEcsDetectorSync,
awsEksDetectorSync,
awsLambdaDetectorSync,
} = require('../../build/src');

// NOTE: the require process makes use of the fs API so spans are being exported.
// We reset the exporter to have a clean state for assertions
await new Promise(r => setTimeout(r, 0));
memoryExporter.reset();

const detectors = [
awsBeanstalkDetectorSync,
awsEc2DetectorSync,
awsEcsDetectorSync,
awsEksDetectorSync,
awsLambdaDetectorSync,
] as DetectorSync[];

for (const d of detectors) {
const r = d.detect();
await r.waitForAsyncAttributes?.();
}

// Wait for the next loop to let the span close properly
await new Promise(r => setTimeout(r, 0));
const spans = memoryExporter.getFinishedSpans();

assert.equal(
spans.length,
0,
'no spans exported from any AWS resource detector'
);

await sdk.shutdown();
delete process.env.ECS_CONTAINER_METADATA_URI_V4;
}).timeout(10000);
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import { SEMRESATTRS_CONTAINER_ID } from '@opentelemetry/semantic-conventions';

import * as fs from 'fs';
import * as util from 'util';
import { diag } from '@opentelemetry/api';
import { context, diag } from '@opentelemetry/api';
import { suppressTracing } from '@opentelemetry/core';
import { extractContainerIdFromLine } from './utils';

export class ContainerDetector implements DetectorSync {
Expand All @@ -43,7 +44,10 @@ export class ContainerDetector implements DetectorSync {
private static readFileAsync = util.promisify(fs.readFile);

detect(_config?: ResourceDetectionConfig): IResource {
return new Resource({}, this._getAttributes());
const attributes = context.with(suppressTracing(context.active()), () =>
this._getAttributes()
);
return new Resource({}, attributes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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 * as assert from 'assert';

import { FsInstrumentation } from '@opentelemetry/instrumentation-fs';
import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import { NodeSDK } from '@opentelemetry/sdk-node';
import { IResource } from '@opentelemetry/resources';

describe('[Integration] ContainerDetector', () => {
it('should not start spans for detector reads to filesystem', async () => {
const memoryExporter = new InMemorySpanExporter();
const sdk = new NodeSDK({
instrumentations: [new FsInstrumentation()],
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});

sdk.start();

// NOTE: detectors implementing the `DetectorSync` interface and starting
// HTTP requests within the `detect` method will produce Noop Spans since
// the SDK resolves the trace provider after resource detectors are triggered.
// Ref: https://github.com/open-telemetry/opentelemetry-js/blob/38f6689480d28dcbdafcb7b5ba4b14025328ffda/experimental/packages/opentelemetry-sdk-node/src/sdk.ts#L210-L240
//
// So having the detector in the config would result in no spans for Azure requests
// being exported which is what we want. Although we may think we're safe of sending
// internal tracing any change that delays these request will result in internal
// tracing being exported. We do the detection outside the SDK constructor to have such
// scenario.
const {
containerDetector,
} = require('../build/src/detectors/ContainerDetector');

// NOTE: the require process makes use of the fs API so spans are being exported.
// We need to check no new spans are exported when `detect` is called.
await new Promise(r => setTimeout(r, 0));
const numSpansAfterRequire = memoryExporter.getFinishedSpans().length;

const resource = containerDetector.detect() as IResource;
await resource.waitForAsyncAttributes?.();

// Wait for the next loop to let the span close properly
await new Promise(r => setTimeout(r, 0));
const spans = memoryExporter.getFinishedSpans();

assert.equal(
spans.length,
numSpansAfterRequire,
'no spans exported for ContainerDetector'
);

await sdk.shutdown();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
"dependencies": {
"@opentelemetry/core": "^1.0.0",
"@opentelemetry/resources": "^1.0.0",
"@opentelemetry/resources": "^1.10.0",
"@opentelemetry/semantic-conventions": "^1.27.0",
"gcp-metadata": "^6.0.0"
},
Expand Down
Loading

0 comments on commit 558ffea

Please sign in to comment.