Skip to content

Commit

Permalink
Merge branch 'main' into fix/host-metrics-bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
Netail authored Apr 20, 2024
2 parents d1c1fca + a288410 commit a3b898f
Show file tree
Hide file tree
Showing 18 changed files with 340 additions and 98 deletions.
10 changes: 6 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions packages/winston-transport/src/OpenTelemetryTransportV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ export class OpenTelemetryTransportV3 extends TransportStream {
this._logger = logs.getLogger('@opentelemetry/winston-transport', VERSION);
}

public override log(info: any, next: () => void) {
public override log(info: any, callback: () => void) {
try {
emitLogRecord(info, this._logger);
} catch (error) {
this.emit('warn', error);
}
setImmediate(() => {
this.emit('logged', info);
});
if (next) {
setImmediate(next);
this.emit('logged', info);
if (callback) {
callback();
}
}
}
17 changes: 17 additions & 0 deletions plugins/node/opentelemetry-instrumentation-mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ See [examples/mysql](https://github.com/open-telemetry/opentelemetry-js-contrib/
| [`enhancedDatabaseReporting`](./src/types.ts#L24) | `boolean` | `false` | If true, an attribute containing the query's parameters will be attached the spans generated to represent the query |
|

## Semantic Conventions

This package uses `@opentelemetry/semantic-conventions` version `1.22+`, which implements Semantic Convention [Version 1.7.0](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/semantic_conventions/README.md)

Attributes collected:

| Attribute | Short Description |
| ----------------------- | ------------------------------------------------------------------------------ |
| `db.connection_string` | The connection string used to connect to the database. |
| `db.name` | This attribute is used to report the name of the database being accessed. |
| `db.operation` | The name of the operation being executed. |
| `db.statement` | The database statement being executed. |
| `db.system` | An identifier for the database management system (DBMS) product being used. |
| `db.user` | Username for accessing the database. |
| `net.peer.name` | Remote hostname or similar. |
| `net.peer.port` | Remote port number. |

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.50.0",
"@opentelemetry/semantic-conventions": "^1.0.0",
"@opentelemetry/semantic-conventions": "^1.22.0",
"@types/mysql": "2.15.22"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-mysql#readme"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import {
isWrapped,
} from '@opentelemetry/instrumentation';
import {
DbSystemValues,
SemanticAttributes,
DBSYSTEMVALUES_MYSQL,
SEMATTRS_DB_STATEMENT,
SEMATTRS_DB_SYSTEM,
} from '@opentelemetry/semantic-conventions';
import type * as mysqlTypes from 'mysql';
import { AttributeNames } from './AttributeNames';
Expand All @@ -54,7 +55,7 @@ export class MySQLInstrumentation extends InstrumentationBase<
typeof mysqlTypes
> {
static readonly COMMON_ATTRIBUTES = {
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.MYSQL,
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_MYSQL,
};
private _connectionsUsage!: UpDownCounter;

Expand Down Expand Up @@ -331,10 +332,7 @@ export class MySQLInstrumentation extends InstrumentationBase<
},
});

span.setAttribute(
SemanticAttributes.DB_STATEMENT,
getDbStatement(query)
);
span.setAttribute(SEMATTRS_DB_STATEMENT, getDbStatement(query));

const instrumentationConfig: MySQLInstrumentationConfig =
thisPlugin.getConfig();
Expand Down
40 changes: 19 additions & 21 deletions plugins/node/opentelemetry-instrumentation-mysql/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
* limitations under the License.
*/

import { SpanAttributes } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { Attributes } from '@opentelemetry/api';
import {
SEMATTRS_DB_CONNECTION_STRING,
SEMATTRS_DB_NAME,
SEMATTRS_DB_USER,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
} from '@opentelemetry/semantic-conventions';
import type {
ConnectionConfig,
PoolActualConfig,
Expand All @@ -25,37 +31,29 @@ import type {
import type * as mysqlTypes from 'mysql';

/**
* Get an SpanAttributes map from a mysql connection config object
* Get an Attributes map from a mysql connection config object
*
* @param config ConnectionConfig
*/
export function getConnectionAttributes(
config: ConnectionConfig | PoolActualConfig
): SpanAttributes {
): Attributes {
const { host, port, database, user } = getConfig(config);
const portNumber = parseInt(port, 10);
if (!isNaN(portNumber)) {
return {
[SemanticAttributes.NET_PEER_NAME]: host,
[SemanticAttributes.NET_PEER_PORT]: portNumber,
[SemanticAttributes.DB_CONNECTION_STRING]: getJDBCString(
host,
port,
database
),
[SemanticAttributes.DB_NAME]: database,
[SemanticAttributes.DB_USER]: user,
[SEMATTRS_NET_PEER_NAME]: host,
[SEMATTRS_NET_PEER_PORT]: portNumber,
[SEMATTRS_DB_CONNECTION_STRING]: getJDBCString(host, port, database),
[SEMATTRS_DB_NAME]: database,
[SEMATTRS_DB_USER]: user,
};
}
return {
[SemanticAttributes.NET_PEER_NAME]: host,
[SemanticAttributes.DB_CONNECTION_STRING]: getJDBCString(
host,
port,
database
),
[SemanticAttributes.DB_NAME]: database,
[SemanticAttributes.DB_USER]: user,
[SEMATTRS_NET_PEER_NAME]: host,
[SEMATTRS_DB_CONNECTION_STRING]: getJDBCString(host, port, database),
[SEMATTRS_DB_NAME]: database,
[SEMATTRS_DB_USER]: user,
};
}

Expand Down
24 changes: 13 additions & 11 deletions plugins/node/opentelemetry-instrumentation-mysql/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
import { context, Context, trace, SpanStatusCode } from '@opentelemetry/api';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
import {
DbSystemValues,
SemanticAttributes,
DBSYSTEMVALUES_MYSQL,
SEMATTRS_DB_NAME,
SEMATTRS_DB_STATEMENT,
SEMATTRS_DB_SYSTEM,
SEMATTRS_DB_USER,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
} from '@opentelemetry/semantic-conventions';
import * as testUtils from '@opentelemetry/contrib-test-utils';
import {
Expand Down Expand Up @@ -868,15 +873,12 @@ function assertSpan(
values?: any,
errorMessage?: string
) {
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);
assert.strictEqual(span.attributes[SemanticAttributes.DB_USER], user);
assert.strictEqual(span.attributes[SemanticAttributes.DB_STATEMENT], sql);
assert.strictEqual(span.attributes[SEMATTRS_DB_SYSTEM], DBSYSTEMVALUES_MYSQL);
assert.strictEqual(span.attributes[SEMATTRS_DB_NAME], database);
assert.strictEqual(span.attributes[SEMATTRS_NET_PEER_PORT], port);
assert.strictEqual(span.attributes[SEMATTRS_NET_PEER_NAME], host);
assert.strictEqual(span.attributes[SEMATTRS_DB_USER], user);
assert.strictEqual(span.attributes[SEMATTRS_DB_STATEMENT], sql);
if (errorMessage) {
assert.strictEqual(span.status.message, errorMessage);
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
Expand Down
16 changes: 16 additions & 0 deletions plugins/node/opentelemetry-instrumentation-mysql2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ You can set the following instrumentation options:
| `responseHook` | `MySQL2InstrumentationExecutionResponseHook` (function) | Function for adding custom attributes from db response |
| `addSqlCommenterCommentToQueries` | `boolean` | If true, adds [sqlcommenter](https://github.com/open-telemetry/opentelemetry-sqlcommenter) specification compliant comment to queries with tracing context (default false). _NOTE: A comment will not be added to queries that already contain `--` or `/* ... */` in them, even if these are not actually part of comments_ |

## Semantic Conventions

This package uses `@opentelemetry/semantic-conventions` version `1.22+`, which implements Semantic Convention [Version 1.7.0](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/semantic_conventions/README.md)

Attributes collected:

| Attribute | Short Description |
| ----------------------- | ------------------------------------------------------------------------------ |
| `db.connection_string` | The connection string used to connect to the database. |
| `db.name` | This attribute is used to report the name of the database being accessed. |
| `db.statement` | The database statement being executed. |
| `db.system` | An identifier for the database management system (DBMS) product being used. |
| `db.user` | Username for accessing the database. |
| `net.peer.name` | Remote hostname or similar. |
| `net.peer.port` | Remote port number. |

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.50.0",
"@opentelemetry/semantic-conventions": "^1.0.0",
"@opentelemetry/semantic-conventions": "^1.22.0",
"@opentelemetry/sql-common": "^0.40.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-mysql2#readme"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import {
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
import {
DbSystemValues,
SemanticAttributes,
DBSYSTEMVALUES_MYSQL,
SEMATTRS_DB_STATEMENT,
SEMATTRS_DB_SYSTEM,
} from '@opentelemetry/semantic-conventions';
import { addSqlCommenterComment } from '@opentelemetry/sql-common';
import type * as mysqlTypes from 'mysql2';
Expand All @@ -40,7 +41,7 @@ type formatType = typeof mysqlTypes.format;

export class MySQL2Instrumentation extends InstrumentationBase<any> {
static readonly COMMON_ATTRIBUTES = {
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.MYSQL,
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_MYSQL,
};

constructor(config?: MySQL2InstrumentationConfig) {
Expand Down Expand Up @@ -115,11 +116,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<any> {
attributes: {
...MySQL2Instrumentation.COMMON_ATTRIBUTES,
...getConnectionAttributes(this.config),
[SemanticAttributes.DB_STATEMENT]: getDbStatement(
query,
format,
values
),
[SEMATTRS_DB_STATEMENT]: getDbStatement(query, format, values),
},
});

Expand Down
40 changes: 19 additions & 21 deletions plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
* limitations under the License.
*/

import { SpanAttributes } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { Attributes } from '@opentelemetry/api';
import {
SEMATTRS_DB_CONNECTION_STRING,
SEMATTRS_DB_NAME,
SEMATTRS_DB_USER,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
} from '@opentelemetry/semantic-conventions';

/*
Following types declare an expectation on mysql2 types and define a subset we
Expand All @@ -42,35 +48,27 @@ interface Config {
connectionConfig?: Config;
}
/**
* Get an SpanAttributes map from a mysql connection config object
* Get an Attributes map from a mysql connection config object
*
* @param config ConnectionConfig
*/
export function getConnectionAttributes(config: Config): SpanAttributes {
export function getConnectionAttributes(config: Config): Attributes {
const { host, port, database, user } = getConfig(config);
const portNumber = parseInt(port, 10);
if (!isNaN(portNumber)) {
return {
[SemanticAttributes.NET_PEER_NAME]: host,
[SemanticAttributes.NET_PEER_PORT]: portNumber,
[SemanticAttributes.DB_CONNECTION_STRING]: getJDBCString(
host,
port,
database
),
[SemanticAttributes.DB_NAME]: database,
[SemanticAttributes.DB_USER]: user,
[SEMATTRS_NET_PEER_NAME]: host,
[SEMATTRS_NET_PEER_PORT]: portNumber,
[SEMATTRS_DB_CONNECTION_STRING]: getJDBCString(host, port, database),
[SEMATTRS_DB_NAME]: database,
[SEMATTRS_DB_USER]: user,
};
}
return {
[SemanticAttributes.NET_PEER_NAME]: host,
[SemanticAttributes.DB_CONNECTION_STRING]: getJDBCString(
host,
port,
database
),
[SemanticAttributes.DB_NAME]: database,
[SemanticAttributes.DB_USER]: user,
[SEMATTRS_NET_PEER_NAME]: host,
[SEMATTRS_DB_CONNECTION_STRING]: getJDBCString(host, port, database),
[SEMATTRS_DB_NAME]: database,
[SEMATTRS_DB_USER]: user,
};
}

Expand Down
Loading

0 comments on commit a3b898f

Please sign in to comment.