diff --git a/incubator/opentelemetry-sampler-aws-xray/README.md b/incubator/opentelemetry-sampler-aws-xray/README.md index 96852f6ab0..3656050787 100644 --- a/incubator/opentelemetry-sampler-aws-xray/README.md +++ b/incubator/opentelemetry-sampler-aws-xray/README.md @@ -19,10 +19,7 @@ const { AWSXRayPropagator } = require("@opentelemetry/propagator-aws-xray"); const { AWSXRayIdGenerator } = require("@opentelemetry/id-generator-aws-xray"); -// Initialize resource, trace exporter, span processor, and ID generator -const _resource = Resource.default().merge(new Resource({ - [SemanticResourceAttributes.SERVICE_NAME]: "remote-sampler-app", - })); +// Initialize trace exporter, span processor, and ID generator const _traceExporter = new OTLPTraceExporter(); const _spanProcessor = new BatchSpanProcessor(_traceExporter); const _tracerConfig = { @@ -32,19 +29,19 @@ const _tracerConfig = { } const sdk = new opentelemetry.NodeSDK({ - textMapPropagator: new AWSXRayPropagator(), - instrumentations: [ - new HttpInstrumentation(), - new AwsInstrumentation({ - suppressInternalInstrumentation: true - }), - ], - resource: _resource, - spanProcessor: _spanProcessor, - traceExporter: _traceExporter, - }); - - sdk.configureTracerProvider(_tracerConfig, _spanProcessor); + serviceName: "remote-sampler-app", + textMapPropagator: new AWSXRayPropagator(), + instrumentations: [ + new HttpInstrumentation(), + new AwsInstrumentation({ + suppressInternalInstrumentation: true + }), + ], + spanProcessor: _spanProcessor, + traceExporter: _traceExporter, +}); + +sdk.configureTracerProvider(_tracerConfig, _spanProcessor); ``` diff --git a/metapackages/auto-instrumentations-node/README.md b/metapackages/auto-instrumentations-node/README.md index 6d74119d8d..221a0570a5 100644 --- a/metapackages/auto-instrumentations-node/README.md +++ b/metapackages/auto-instrumentations-node/README.md @@ -119,14 +119,14 @@ const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node'); const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node'); const { CollectorTraceExporter } = require('@opentelemetry/exporter-collector'); const { Resource } = require('@opentelemetry/resources'); -const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions'); +const { SEMRESATTRS_SERVICE_NAME } = require('@opentelemetry/semantic-conventions'); const { SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base'); const { registerInstrumentations } = require('@opentelemetry/instrumentation'); const exporter = new CollectorTraceExporter(); const provider = new NodeTracerProvider({ resource: new Resource({ - [SemanticResourceAttributes.SERVICE_NAME]: 'basic-service', + [SEMRESATTRS_SERVICE_NAME]: 'basic-service', }), }); provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); diff --git a/packages/opentelemetry-test-utils/src/resource-assertions.ts b/packages/opentelemetry-test-utils/src/resource-assertions.ts index eb248d41d8..51b1419187 100644 --- a/packages/opentelemetry-test-utils/src/resource-assertions.ts +++ b/packages/opentelemetry-test-utils/src/resource-assertions.ts @@ -18,7 +18,6 @@ import { SDK_INFO } from '@opentelemetry/core'; import * as assert from 'assert'; import { Resource } from '@opentelemetry/resources'; import { - SemanticResourceAttributes, SEMRESATTRS_CLOUD_ACCOUNT_ID, SEMRESATTRS_CLOUD_AVAILABILITY_ZONE, SEMRESATTRS_CLOUD_PROVIDER, @@ -49,6 +48,7 @@ import { SEMRESATTRS_TELEMETRY_SDK_NAME, SEMRESATTRS_TELEMETRY_SDK_VERSION, } from '@opentelemetry/semantic-conventions'; +import * as semconv from '@opentelemetry/semantic-conventions'; /** * Test utility method to validate a cloud resource @@ -65,7 +65,7 @@ export const assertCloudResource = ( zone?: string; } ) => { - assertHasOneLabel('CLOUD', resource); + assertHasOneLabel('cloud', resource); if (validations.provider) assert.strictEqual( resource.attributes[SEMRESATTRS_CLOUD_PROVIDER], @@ -103,7 +103,7 @@ export const assertContainerResource = ( imageTag?: string; } ) => { - assertHasOneLabel('CONTAINER', resource); + assertHasOneLabel('container', resource); if (validations.name) assert.strictEqual( resource.attributes[SEMRESATTRS_CONTAINER_NAME], @@ -143,7 +143,7 @@ export const assertHostResource = ( imageVersion?: string; } ) => { - assertHasOneLabel('HOST', resource); + assertHasOneLabel('host', resource); if (validations.id) assert.strictEqual( resource.attributes[SEMRESATTRS_HOST_ID], @@ -191,7 +191,7 @@ export const assertK8sResource = ( deploymentName?: string; } ) => { - assertHasOneLabel('K8S', resource); + assertHasOneLabel('k8s', resource); if (validations.clusterName) assert.strictEqual( resource.attributes[SEMRESATTRS_K8S_CLUSTER_NAME], @@ -335,26 +335,26 @@ export const assertEmptyResource = (resource: Resource) => { assert.strictEqual(Object.keys(resource.attributes).length, 0); }; +/** + * Assert that the `resource` has at least one known attribute with the given + * `prefix`. By "known", we mean it is an attribute defined in semconv. + */ const assertHasOneLabel = (prefix: string, resource: Resource): void => { - const hasOne = Object.entries(SemanticResourceAttributes).find( - ([key, value]) => { - return ( - key.startsWith(prefix) && - Object.prototype.hasOwnProperty.call(resource.attributes, value) - ); - } + const semconvModPrefix = `SEMRESATTRS_${prefix.toUpperCase()}_`; + const knownAttrs: Set = new Set( + Object.entries(semconv) + .filter( + ([k, v]) => typeof v === 'string' && k.startsWith(semconvModPrefix) + ) + .map(([, v]) => v as string) ); + const hasAttrs = Object.keys(resource.attributes).filter(k => + knownAttrs.has(k) + ); assert.ok( - hasOne, + hasAttrs.length > 0, 'Resource must have one of the following attributes: ' + - Object.entries(SemanticResourceAttributes) - .reduce((result, [key, value]) => { - if (key.startsWith(prefix)) { - result.push(value); - } - return result; - }) - .join(', ') + Array.from(knownAttrs).join(', ') ); }; diff --git a/plugins/node/instrumentation-socket.io/test/utils.ts b/plugins/node/instrumentation-socket.io/test/utils.ts index 86144d5f4d..1561a5ec4e 100644 --- a/plugins/node/instrumentation-socket.io/test/utils.ts +++ b/plugins/node/instrumentation-socket.io/test/utils.ts @@ -17,7 +17,7 @@ import { strict as assert } from 'assert'; import * as http from 'http'; import { AddressInfo } from 'net'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { SEMATTRS_MESSAGING_SYSTEM } from '@opentelemetry/semantic-conventions'; import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import { getTestSpans } from '@opentelemetry/contrib-test-utils'; @@ -57,7 +57,7 @@ export const createServerInstance = (server?: http.Server) => { export const getSocketIoSpans = (): ReadableSpan[] => getTestSpans().filter( - s => s.attributes[SemanticAttributes.MESSAGING_SYSTEM] === 'socket.io' + s => s.attributes[SEMATTRS_MESSAGING_SYSTEM] === 'socket.io' ) as ReadableSpan[]; export const expectSpan = ( diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts index 144c375c68..dba836dc3a 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts @@ -33,7 +33,11 @@ const instrumentation = registerInstrumentationTesting( import type { MongoClient, Collection } from 'mongodb'; import { assertSpans, accessCollection, DEFAULT_MONGO_HOST } from './utils'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { + SEMATTRS_DB_STATEMENT, + SEMATTRS_NET_PEER_NAME, + SEMATTRS_NET_PEER_PORT, +} from '@opentelemetry/semantic-conventions'; describe('MongoDBInstrumentation-Tracing-v3', () => { function create(config: MongoDBInstrumentationConfig = {}) { @@ -295,7 +299,7 @@ describe('MongoDBInstrumentation-Tracing-v3', () => { ); const mongoSpan = spans.find(s => s.name === operationName); const dbStatement = JSON.parse( - mongoSpan!.attributes[SemanticAttributes.DB_STATEMENT] as string + mongoSpan!.attributes[SEMATTRS_DB_STATEMENT] as string ); assert.strictEqual(dbStatement[key], '?'); done(); @@ -341,7 +345,7 @@ describe('MongoDBInstrumentation-Tracing-v3', () => { ); const mongoSpan = spans.find(s => s.name === operationName); const dbStatement = JSON.parse( - mongoSpan!.attributes[SemanticAttributes.DB_STATEMENT] as string + mongoSpan!.attributes[SEMATTRS_DB_STATEMENT] as string ); assert.strictEqual(dbStatement[key], value); done(); @@ -580,11 +584,11 @@ describe('MongoDBInstrumentation-Tracing-v3', () => { (err, address) => { if (err) return done(err); assert.strictEqual( - mongoSpan.attributes[SemanticAttributes.NET_PEER_NAME], + mongoSpan.attributes[SEMATTRS_NET_PEER_NAME], address ); assert.strictEqual( - mongoSpan.attributes[SemanticAttributes.NET_PEER_PORT], + mongoSpan.attributes[SEMATTRS_NET_PEER_PORT], process.env.MONGODB_PORT ? parseInt(process.env.MONGODB_PORT) : 27017 diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts index 1a8af5f081..df2c48be5f 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts @@ -35,7 +35,7 @@ const instrumentation = registerInstrumentationTesting( import type { MongoClient, Collection } from 'mongodb'; import { assertSpans, accessCollection, DEFAULT_MONGO_HOST } from './utils'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { SEMATTRS_DB_STATEMENT } from '@opentelemetry/semantic-conventions'; describe('MongoDBInstrumentation-Tracing-v4', () => { function create(config: MongoDBInstrumentationConfig = {}) { @@ -334,7 +334,7 @@ describe('MongoDBInstrumentation-Tracing-v4', () => { ); const mongoSpan = spans.find(s => s.name === operationName); const dbStatement = JSON.parse( - mongoSpan!.attributes[SemanticAttributes.DB_STATEMENT] as string + mongoSpan!.attributes[SEMATTRS_DB_STATEMENT] as string ); assert.strictEqual(dbStatement[key], '?'); done(); @@ -380,7 +380,7 @@ describe('MongoDBInstrumentation-Tracing-v4', () => { ); const mongoSpan = spans.find(s => s.name === operationName); const dbStatement = JSON.parse( - mongoSpan!.attributes[SemanticAttributes.DB_STATEMENT] as string + mongoSpan!.attributes[SEMATTRS_DB_STATEMENT] as string ); assert.strictEqual(dbStatement[key], value); done(); diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts index be6ac80ee0..546aba2e31 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts @@ -44,7 +44,7 @@ let instrumentation: MongoDBInstrumentation; import type { MongoClient, Collection } from 'mongodb'; import { assertSpans, accessCollection, DEFAULT_MONGO_HOST } from './utils'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { SEMATTRS_DB_STATEMENT } from '@opentelemetry/semantic-conventions'; describe('MongoDBInstrumentation-Tracing-v5', () => { function create(config: MongoDBInstrumentationConfig = {}) { @@ -337,7 +337,7 @@ describe('MongoDBInstrumentation-Tracing-v5', () => { ); const mongoSpan = spans.find(s => s.name === operationName); const dbStatement = JSON.parse( - mongoSpan!.attributes[SemanticAttributes.DB_STATEMENT] as string + mongoSpan!.attributes[SEMATTRS_DB_STATEMENT] as string ); assert.strictEqual(dbStatement[key], '?'); done(); @@ -383,7 +383,7 @@ describe('MongoDBInstrumentation-Tracing-v5', () => { ); const mongoSpan = spans.find(s => s.name === operationName); const dbStatement = JSON.parse( - mongoSpan!.attributes[SemanticAttributes.DB_STATEMENT] as string + mongoSpan!.attributes[SEMATTRS_DB_STATEMENT] as string ); assert.strictEqual(dbStatement[key], value); done(); diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts index a158cc9b16..07ce789975 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts @@ -15,7 +15,13 @@ */ import { SpanKind, SpanStatusCode } from '@opentelemetry/api'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { + SEMATTRS_DB_CONNECTION_STRING, + SEMATTRS_DB_OPERATION, + SEMATTRS_DB_STATEMENT, + SEMATTRS_DB_SYSTEM, + SEMATTRS_NET_PEER_NAME, +} from '@opentelemetry/semantic-conventions'; import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import * as assert from 'assert'; import type { MongoClient, MongoClientOptions, Collection } from 'mongodb'; @@ -93,28 +99,25 @@ export function assertSpans( assert.strictEqual(mongoSpan.name, expectedName); assert.strictEqual(mongoSpan.kind, expectedKind); assert.strictEqual( - mongoSpan.attributes[SemanticAttributes.DB_OPERATION], + mongoSpan.attributes[SEMATTRS_DB_OPERATION], expectedOperation ); + assert.strictEqual(mongoSpan.attributes[SEMATTRS_DB_SYSTEM], 'mongodb'); assert.strictEqual( - mongoSpan.attributes[SemanticAttributes.DB_SYSTEM], - 'mongodb' - ); - assert.strictEqual( - mongoSpan.attributes[SemanticAttributes.NET_PEER_NAME], + mongoSpan.attributes[SEMATTRS_NET_PEER_NAME], process.env.MONGODB_HOST || DEFAULT_MONGO_HOST ); assert.strictEqual(mongoSpan.status.code, SpanStatusCode.UNSET); if (expectedConnString) { assert.strictEqual( - mongoSpan.attributes[SemanticAttributes.DB_CONNECTION_STRING], + mongoSpan.attributes[SEMATTRS_DB_CONNECTION_STRING], expectedConnString ); } if (isEnhancedDatabaseReportingEnabled) { const dbStatement = JSON.parse( - mongoSpan.attributes[SemanticAttributes.DB_STATEMENT] as string + mongoSpan.attributes[SEMATTRS_DB_STATEMENT] as string ); for (const key in dbStatement) { assert.notStrictEqual(dbStatement[key], '?');