Skip to content

Commit

Permalink
refactor(instrumentation-redis): Use exported semconv strings (open-t…
Browse files Browse the repository at this point in the history
…elemetry#2075)

* refactor(instrumentation-redis): Use exported semconv strings

Part of open-telemetry#2025.

* chore(instrumentation-redis): Update package-lock

---------

Co-authored-by: Jamie Danielson <[email protected]>
Co-authored-by: Amir Blum <[email protected]>
Co-authored-by: Marc Pichler <[email protected]>
  • Loading branch information
4 people authored and maryliag committed Apr 17, 2024
1 parent a66419e commit dcb5f75
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

14 changes: 14 additions & 0 deletions plugins/node/opentelemetry-instrumentation-redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ const redisInstrumentation = new RedisInstrumentation({
});
```

## 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 | Notes |
|------------------------|--------------------------------------------------------------|--------------------------------------|
| `db.connection_string` | URL to Redis server address, of the form `redis://host:port` | Key: `SEMATTRS_DB_CONNECTION_STRING` |
| `db.statement` | Executed Redis statement | Key: `SEMATTRS_DB_STATEMENT` |
| `db.system` | Database identifier; always `redis` | Key: `SEMATTRS_DB_SYSTEM` |
| `net.peer.name` | Hostname or IP of the connected Redis server | Key: `SEMATTRS_NET_PEER_NAME` |
| `net.peer.port` | Port of the connected Redis server | Key: `SEMATTRS_NET_PORT_NAME` |

## 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 @@ -68,7 +68,7 @@
"dependencies": {
"@opentelemetry/instrumentation": "^0.50.0",
"@opentelemetry/redis-common": "^0.36.1",
"@opentelemetry/semantic-conventions": "^1.0.0"
"@opentelemetry/semantic-conventions": "^1.22.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-redis#readme"
}
21 changes: 11 additions & 10 deletions plugins/node/opentelemetry-instrumentation-redis/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ import { RedisCommand, RedisInstrumentationConfig } from './types';
import { EventEmitter } from 'events';
import { RedisInstrumentation } from './';
import {
DbSystemValues,
SemanticAttributes,
DBSYSTEMVALUES_REDIS,
SEMATTRS_DB_CONNECTION_STRING,
SEMATTRS_DB_STATEMENT,
SEMATTRS_DB_SYSTEM,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
} from '@opentelemetry/semantic-conventions';
import { safeExecuteInTheMiddle } from '@opentelemetry/instrumentation';
import { RedisPluginClientTypes } from './internal-types';
Expand Down Expand Up @@ -100,25 +104,22 @@ export const getTracedInternalSendCommand = (
{
kind: SpanKind.CLIENT,
attributes: {
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.REDIS,
[SemanticAttributes.DB_STATEMENT]: dbStatementSerializer(
cmd.command,
cmd.args
),
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_REDIS,
[SEMATTRS_DB_STATEMENT]: dbStatementSerializer(cmd.command, cmd.args),
},
}
);

// Set attributes for not explicitly typed RedisPluginClientTypes
if (this.options) {
span.setAttributes({
[SemanticAttributes.NET_PEER_NAME]: this.options.host,
[SemanticAttributes.NET_PEER_PORT]: this.options.port,
[SEMATTRS_NET_PEER_NAME]: this.options.host,
[SEMATTRS_NET_PEER_PORT]: this.options.port,
});
}
if (this.address) {
span.setAttribute(
SemanticAttributes.DB_CONNECTION_STRING,
SEMATTRS_DB_CONNECTION_STRING,
`redis://${this.address}`
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ import {
import * as assert from 'assert';
import { RedisInstrumentation } from '../src';
import {
DbSystemValues,
SemanticAttributes,
DBSYSTEMVALUES_REDIS,
SEMATTRS_DB_CONNECTION_STRING,
SEMATTRS_DB_STATEMENT,
SEMATTRS_DB_SYSTEM,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
} from '@opentelemetry/semantic-conventions';

const instrumentation = new RedisInstrumentation();
Expand All @@ -53,10 +57,10 @@ const CONFIG = {
const URL = `redis://${CONFIG.host}:${CONFIG.port}`;

const DEFAULT_ATTRIBUTES = {
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.REDIS,
[SemanticAttributes.NET_PEER_NAME]: CONFIG.host,
[SemanticAttributes.NET_PEER_PORT]: CONFIG.port,
[SemanticAttributes.DB_CONNECTION_STRING]: URL,
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_REDIS,
[SEMATTRS_NET_PEER_NAME]: CONFIG.host,
[SEMATTRS_NET_PEER_PORT]: CONFIG.port,
[SEMATTRS_DB_CONNECTION_STRING]: URL,
};

const unsetStatus: SpanStatus = {
Expand Down Expand Up @@ -192,7 +196,7 @@ describe('[email protected]', () => {
it(`should create a child span for ${operation.description}`, done => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[SemanticAttributes.DB_STATEMENT]: `${operation.command} ${operation.expectedDbStatement}`,
[SEMATTRS_DB_STATEMENT]: `${operation.command} ${operation.expectedDbStatement}`,
};
const span = tracer.startSpan('test span');
context.with(trace.setSpan(context.active(), span), () => {
Expand Down Expand Up @@ -281,7 +285,7 @@ describe('[email protected]', () => {
operation.args
);
assert.strictEqual(
endedSpans[0].attributes[SemanticAttributes.DB_STATEMENT],
endedSpans[0].attributes[SEMATTRS_DB_STATEMENT],
expectedStatement
);
done();
Expand Down

0 comments on commit dcb5f75

Please sign in to comment.