Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(instrumentation-redis): Use exported semconv strings #2075

Merged
merged 5 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading