Skip to content

Commit

Permalink
Merge branch 'main' into fix_mongoose_model_method_session_instrument…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
ferrelucas committed May 23, 2024
2 parents 1764acf + 69cd638 commit 8e9630f
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 62 deletions.
31 changes: 14 additions & 17 deletions incubator/opentelemetry-sampler-aws-xray/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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);

```

Expand Down
4 changes: 2 additions & 2 deletions metapackages/auto-instrumentations-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
42 changes: 21 additions & 21 deletions packages/opentelemetry-test-utils/src/resource-assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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<string> = 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(', ')
);
};
4 changes: 2 additions & 2 deletions plugins/node/instrumentation-socket.io/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
21 changes: 12 additions & 9 deletions plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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], '?');
Expand Down

0 comments on commit 8e9630f

Please sign in to comment.