Skip to content

Commit

Permalink
Merge branch 'main' into update-release-please
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan authored Feb 28, 2022
2 parents 99020c7 + e7ab4d0 commit 2aa3cdd
Show file tree
Hide file tree
Showing 17 changed files with 306 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

import { context, trace, SpanStatusCode, SpanKind } from '@opentelemetry/api';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
DbSystemValues,
SemanticAttributes,
} from '@opentelemetry/semantic-conventions';
import * as util from 'util';
import * as testUtils from '@opentelemetry/contrib-test-utils';
import {
Expand Down Expand Up @@ -334,7 +337,10 @@ function assertSpan(span: ReadableSpan, expected: any) {
assert(span);
assert.strictEqual(span.name, expected.name);
assert.strictEqual(span.kind, SpanKind.CLIENT);
assert.strictEqual(span.attributes[SemanticAttributes.DB_SYSTEM], 'mssql');
assert.strictEqual(
span.attributes[SemanticAttributes.DB_SYSTEM],
DbSystemValues.MSSQL
);
assert.strictEqual(
span.attributes[SemanticAttributes.DB_NAME],
expected.database ?? database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
import { Span, SpanKind, Tracer } from '@opentelemetry/api';
import { RequestMetadata, ServiceExtension } from './ServiceExtension';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
DbSystemValues,
SemanticAttributes,
} from '@opentelemetry/semantic-conventions';
import {
AwsSdkInstrumentationConfig,
NormalizedRequest,
Expand All @@ -30,7 +33,7 @@ export class DynamodbServiceExtension implements ServiceExtension {
const operation = normalizedRequest.commandName;

const spanAttributes = {
[SemanticAttributes.DB_SYSTEM]: 'dynamodb',
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.DYNAMODB,
[SemanticAttributes.DB_NAME]: normalizedRequest.commandInput?.TableName,
[SemanticAttributes.DB_OPERATION]: operation,
[SemanticAttributes.DB_STATEMENT]: JSON.stringify(
Expand Down Expand Up @@ -61,7 +64,7 @@ export class DynamodbServiceExtension implements ServiceExtension {
const operation = response.request.commandName;

if (operation === 'BatchGetItem') {
if ('ConsumedCapacity' in response.data) {
if (Array.isArray(response.data?.ConsumedCapacity)) {
span.setAttribute(
SemanticAttributes.AWS_DYNAMODB_CONSUMED_CAPACITY,
response.data.ConsumedCapacity.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import * as AWS from 'aws-sdk';
import { AWSError } from 'aws-sdk';

import { mockV2AwsSend } from './testing-utils';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
DbSystemValues,
SemanticAttributes,
} from '@opentelemetry/semantic-conventions';
import * as expect from 'expect';
import type { ConsumedCapacity as ConsumedCapacityV2 } from 'aws-sdk/clients/dynamodb';
import type { ConsumedCapacity as ConsumedCapacityV3 } from '@aws-sdk/client-dynamodb';
Expand Down Expand Up @@ -73,7 +76,9 @@ describe('DynamoDB', () => {
const spans = getTestSpans();
expect(spans.length).toStrictEqual(1);
const attrs = spans[0].attributes;
expect(attrs[SemanticAttributes.DB_SYSTEM]).toStrictEqual('dynamodb');
expect(attrs[SemanticAttributes.DB_SYSTEM]).toStrictEqual(
DbSystemValues.DYNAMODB
);
expect(attrs[SemanticAttributes.DB_NAME]).toStrictEqual('test-table');
expect(attrs[SemanticAttributes.DB_OPERATION]).toStrictEqual('Query');
expect(
Expand Down Expand Up @@ -120,7 +125,9 @@ describe('DynamoDB', () => {
const spans = getTestSpans();
expect(spans.length).toStrictEqual(1);
const attrs = spans[0].attributes;
expect(attrs[SemanticAttributes.DB_SYSTEM]).toStrictEqual('dynamodb');
expect(attrs[SemanticAttributes.DB_SYSTEM]).toStrictEqual(
DbSystemValues.DYNAMODB
);
expect(attrs[SemanticAttributes.DB_OPERATION]).toStrictEqual(
'BatchGetItem'
);
Expand Down Expand Up @@ -165,7 +172,9 @@ describe('DynamoDB', () => {
const spans = getTestSpans();
expect(spans.length).toStrictEqual(1);
const attrs = spans[0].attributes;
expect(attrs[SemanticAttributes.DB_SYSTEM]).toStrictEqual('dynamodb');
expect(attrs[SemanticAttributes.DB_SYSTEM]).toStrictEqual(
DbSystemValues.DYNAMODB
);
expect(attrs[SemanticAttributes.DB_OPERATION]).toStrictEqual(
'BatchGetItem'
);
Expand All @@ -187,5 +196,50 @@ describe('DynamoDB', () => {
}
);
});

it('should populate BatchGetIem when consumedCapacity is undefined', done => {
mockV2AwsSend(responseMockSuccess, {
Responses: { 'test-table': [{ key1: { S: 'val1' } }] },
UnprocessedKeys: {},
ConsumedCapacity: undefined,
} as AWS.DynamoDB.Types.BatchGetItemOutput);

const dynamodb = new AWS.DynamoDB.DocumentClient();
const dynamodb_params = {
RequestItems: {
'test-table': {
Keys: [{ key1: { S: 'val1' } }],
ProjectionExpression: 'id',
},
},
ReturnConsumedCapacity: 'NONE',
};
dynamodb.batchGet(
dynamodb_params,
(
err: AWSError,
data: AWS.DynamoDB.DocumentClient.BatchGetItemOutput
) => {
const spans = getTestSpans();
expect(spans.length).toStrictEqual(1);
const attrs = spans[0].attributes;
expect(attrs[SemanticAttributes.DB_SYSTEM]).toStrictEqual('dynamodb');
expect(attrs[SemanticAttributes.DB_OPERATION]).toStrictEqual(
'BatchGetItem'
);
expect(
attrs[SemanticAttributes.AWS_DYNAMODB_TABLE_NAMES]
).toStrictEqual(['test-table']);
expect(
attrs[SemanticAttributes.AWS_DYNAMODB_CONSUMED_CAPACITY]
).toBeUndefined();
expect(
JSON.parse(attrs[SemanticAttributes.DB_STATEMENT] as string)
).toEqual(dynamodb_params);
expect(err).toBeFalsy();
done();
}
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import {
isWrapped,
} from '@opentelemetry/instrumentation';
import { IORedisInstrumentationConfig, IORedisCommand } from './types';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
DbSystemValues,
SemanticAttributes,
} from '@opentelemetry/semantic-conventions';
import { safeExecuteInTheMiddle } from '@opentelemetry/instrumentation';
import { endSpan, defaultDbStatementSerializer } from './utils';
import { VERSION } from './version';
Expand All @@ -34,8 +37,6 @@ const DEFAULT_CONFIG: IORedisInstrumentationConfig = {
export class IORedisInstrumentation extends InstrumentationBase<
typeof ioredisTypes
> {
static readonly DB_SYSTEM = 'redis';

constructor(_config: IORedisInstrumentationConfig = {}) {
super(
'@opentelemetry/instrumentation-ioredis',
Expand Down Expand Up @@ -113,7 +114,7 @@ export class IORedisInstrumentation extends InstrumentationBase<
const span = instrumentation.tracer.startSpan(cmd.name, {
kind: SpanKind.CLIENT,
attributes: {
[SemanticAttributes.DB_SYSTEM]: IORedisInstrumentation.DB_SYSTEM,
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.REDIS,
[SemanticAttributes.DB_STATEMENT]: dbStatementSerializer(
cmd.name,
cmd.args
Expand Down Expand Up @@ -186,7 +187,7 @@ export class IORedisInstrumentation extends InstrumentationBase<
const span = instrumentation.tracer.startSpan('connect', {
kind: SpanKind.CLIENT,
attributes: {
[SemanticAttributes.DB_SYSTEM]: IORedisInstrumentation.DB_SYSTEM,
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.REDIS,
[SemanticAttributes.DB_STATEMENT]: 'connect',
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ import {
DbStatementSerializer,
IORedisRequestHookInformation,
} from '../src/types';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
DbSystemValues,
SemanticAttributes,
} from '@opentelemetry/semantic-conventions';

const memoryExporter = new InMemorySpanExporter();

Expand All @@ -51,7 +54,7 @@ const CONFIG = {
const URL = `redis://${CONFIG.host}:${CONFIG.port}`;

const DEFAULT_ATTRIBUTES = {
[SemanticAttributes.DB_SYSTEM]: IORedisInstrumentation.DB_SYSTEM,
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.REDIS,
[SemanticAttributes.NET_PEER_NAME]: CONFIG.host,
[SemanticAttributes.NET_PEER_PORT]: CONFIG.port,
[SemanticAttributes.DB_CONNECTION_STRING]: URL,
Expand Down
6 changes: 4 additions & 2 deletions plugins/node/opentelemetry-instrumentation-knex/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { DbSystemValues } from '@opentelemetry/semantic-conventions';

type Exception = {
new (message: string): Exception;
constructor: Exception;
Expand Down Expand Up @@ -50,8 +52,8 @@ export const cloneErrorWithNewMessage = (err: Exception, message: string) => {
};

const systemMap = new Map([
['sqlite3', 'sqlite'],
['pg', 'postgresql'],
['sqlite3', DbSystemValues.SQLITE],
['pg', DbSystemValues.POSTGRESQL],
]);
export const mapSystem = (knexSystem: string) => {
return systemMap.get(knexSystem) || knexSystem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ import {
InstrumentationNodeModuleDefinition,
} from '@opentelemetry/instrumentation';
import type * as Memcached from 'memcached';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
DbSystemValues,
SemanticAttributes,
} from '@opentelemetry/semantic-conventions';
import * as utils from './utils';
import { InstrumentationConfig } from './types';
import { VERSION } from './version';

export class Instrumentation extends InstrumentationBase<typeof Memcached> {
static readonly COMPONENT = 'memcached';
static readonly COMMON_ATTRIBUTES = {
[SemanticAttributes.DB_SYSTEM]: Instrumentation.COMPONENT,
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.MEMCACHED,
};
static readonly DEFAULT_CONFIG: InstrumentationConfig = {
enhancedDatabaseReporting: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import {
import type * as Memcached from 'memcached';
import * as assert from 'assert';
import Instrumentation from '../src';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
DbSystemValues,
SemanticAttributes,
} from '@opentelemetry/semantic-conventions';
import * as util from 'util';

const instrumentation = new Instrumentation();
Expand All @@ -37,7 +40,7 @@ const CONFIG = {
};

const DEFAULT_ATTRIBUTES = {
[SemanticAttributes.DB_SYSTEM]: Instrumentation.COMPONENT,
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.MEMCACHED,
[SemanticAttributes.NET_PEER_NAME]: CONFIG.host,
[SemanticAttributes.NET_PEER_PORT]: CONFIG.port,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ import {
isWrapped,
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
DbSystemValues,
SemanticAttributes,
} from '@opentelemetry/semantic-conventions';
import type * as mongodb from 'mongodb';
import {
CursorState,
Expand Down Expand Up @@ -563,7 +566,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<
) {
// add database related attributes
span.setAttributes({
[SemanticAttributes.DB_SYSTEM]: 'mongodb',
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.MONGODB,
[SemanticAttributes.DB_NAME]: dbName,
[SemanticAttributes.DB_MONGODB_COLLECTION]: dbCollection,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import {
InstrumentationNodeModuleDefinition,
isWrapped,
} from '@opentelemetry/instrumentation';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
DbSystemValues,
SemanticAttributes,
} from '@opentelemetry/semantic-conventions';
import type * as mysqlTypes from 'mysql';
import { MySQLInstrumentationConfig } from './types';
import { getConnectionAttributes, getDbStatement, getSpanName } from './utils';
Expand All @@ -42,9 +45,8 @@ type getConnectionCallbackType = (
export class MySQLInstrumentation extends InstrumentationBase<
typeof mysqlTypes
> {
static readonly COMPONENT = 'mysql';
static readonly COMMON_ATTRIBUTES = {
[SemanticAttributes.DB_SYSTEM]: MySQLInstrumentation.COMPONENT,
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.MYSQL,
};

constructor(config?: MySQLInstrumentationConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

import { context, trace, SpanStatusCode } from '@opentelemetry/api';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
DbSystemValues,
SemanticAttributes,
} from '@opentelemetry/semantic-conventions';
import * as testUtils from '@opentelemetry/contrib-test-utils';
import {
BasicTracerProvider,
Expand Down Expand Up @@ -716,7 +719,10 @@ function assertSpan(
values?: any,
errorMessage?: string
) {
assert.strictEqual(span.attributes[SemanticAttributes.DB_SYSTEM], 'mysql');
assert.strictEqual(
span.attributes[SemanticAttributes.DB_SYSTEM],
DbSystemValues.MYSQL
);
assert.strictEqual(span.attributes[SemanticAttributes.DB_NAME], database);
assert.strictEqual(span.attributes[SemanticAttributes.NET_PEER_PORT], port);
assert.strictEqual(span.attributes[SemanticAttributes.NET_PEER_NAME], host);
Expand Down
8 changes: 8 additions & 0 deletions plugins/node/opentelemetry-instrumentation-mysql2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ registerInstrumentations({
})
```

### MySQL2 Instrumentation Options

You can set the following instrumentation options:

| Options | Type | Description |
| ------- | ---- | ----------- |
| `responseHook` | `MySQL2InstrumentationExecutionResponseHook` (function) | Function for adding custom attributes from db response |

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
Loading

0 comments on commit 2aa3cdd

Please sign in to comment.