Skip to content

Commit

Permalink
refactor(instrumentation-redis): Use exported semconv strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Zirak committed Apr 3, 2024
1 parent a9b1063 commit d34856b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
14 changes: 14 additions & 0 deletions plugins/node/opentelemetry-instrumentation-redis/README.md
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
Expand Up @@ -68,7 +68,7 @@
"dependencies": {
"@opentelemetry/instrumentation": "^0.49.1",
"@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
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
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('redis@2.x', () => {
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('redis@2.x', () => {
operation.args
);
assert.strictEqual(
endedSpans[0].attributes[SemanticAttributes.DB_STATEMENT],
endedSpans[0].attributes[SEMATTRS_DB_STATEMENT],
expectedStatement
);
done();
Expand Down

0 comments on commit d34856b

Please sign in to comment.