-
Notifications
You must be signed in to change notification settings - Fork 825
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
feat(plugin): implement redis plugin #503
Changes from all commits
94dc9b0
8202ac1
632b8c4
d6665c7
6c31a8a
73d2fbb
abddfe0
391326b
ca04189
0821b41
8654a71
9386c42
cf26841
b9e0047
bf1e1a7
d2e1f04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/*! | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export enum AttributeNames { | ||
// required by https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md#databases-client-calls | ||
COMPONENT = 'component', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should move these common attributes to either core or base package, this is being used in almost all the plugins. WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SGTM. I'll create an issue for it. |
||
DB_TYPE = 'db.type', | ||
DB_INSTANCE = 'db.instance', | ||
DB_STATEMENT = 'db.statement', | ||
PEER_ADDRESS = 'peer.address', | ||
PEER_HOSTNAME = 'peer.host', | ||
|
||
// optional | ||
DB_USER = 'db.user', | ||
PEER_PORT = 'peer.port', | ||
PEER_IPV4 = 'peer.ipv4', | ||
PEER_IPV6 = 'peer.ipv6', | ||
PEER_SERVICE = 'peer.service', | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/*! | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { BasePlugin } from '@opentelemetry/core'; | ||
import * as redisTypes from 'redis'; | ||
import * as shimmer from 'shimmer'; | ||
import { | ||
getTracedCreateClient, | ||
getTracedCreateStreamTrace, | ||
getTracedInternalSendCommand, | ||
} from './utils'; | ||
|
||
export class RedisPlugin extends BasePlugin<typeof redisTypes> { | ||
static readonly COMPONENT = 'redis'; | ||
readonly supportedVersions = ['^2.6.0']; // equivalent to >= 2.6.0 <3 | ||
|
||
constructor(readonly moduleName: string) { | ||
super(); | ||
} | ||
|
||
protected patch() { | ||
if (this._moduleExports.RedisClient) { | ||
this._logger.debug( | ||
'Patching redis.RedisClient.prototype.internal_send_command' | ||
); | ||
shimmer.wrap( | ||
this._moduleExports.RedisClient.prototype, | ||
'internal_send_command', | ||
this._getPatchInternalSendCommand() | ||
); | ||
|
||
this._logger.debug('patching redis.create_stream'); | ||
shimmer.wrap( | ||
this._moduleExports.RedisClient.prototype, | ||
'create_stream', | ||
this._getPatchCreateStream() | ||
); | ||
|
||
this._logger.debug('patching redis.createClient'); | ||
shimmer.wrap( | ||
this._moduleExports, | ||
'createClient', | ||
this._getPatchCreateClient() | ||
); | ||
} | ||
return this._moduleExports; | ||
} | ||
|
||
protected unpatch(): void { | ||
if (this._moduleExports) { | ||
shimmer.unwrap( | ||
this._moduleExports.RedisClient.prototype, | ||
'internal_send_command' | ||
); | ||
shimmer.unwrap( | ||
this._moduleExports.RedisClient.prototype, | ||
'create_stream' | ||
); | ||
shimmer.unwrap(this._moduleExports, 'createClient'); | ||
} | ||
} | ||
|
||
/** | ||
* Patch internal_send_command(...) to trace requests | ||
*/ | ||
private _getPatchInternalSendCommand() { | ||
const tracer = this._tracer; | ||
return function internal_send_command(original: Function) { | ||
return getTracedInternalSendCommand(tracer, original); | ||
}; | ||
} | ||
|
||
private _getPatchCreateClient() { | ||
const tracer = this._tracer; | ||
return function createClient(original: Function) { | ||
return getTracedCreateClient(tracer, original); | ||
}; | ||
} | ||
|
||
private _getPatchCreateStream() { | ||
const tracer = this._tracer; | ||
return function createReadStream(original: Function) { | ||
return getTracedCreateStreamTrace(tracer, original); | ||
}; | ||
} | ||
} | ||
|
||
export const plugin = new RedisPlugin(RedisPlugin.COMPONENT); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/*! | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as redisTypes from 'redis'; | ||
import { EventEmitter } from 'events'; | ||
|
||
// exported from | ||
// https://github.com/NodeRedis/node_redis/blob/master/lib/command.js | ||
export interface RedisCommand { | ||
command: string; | ||
args: string[]; | ||
buffer_args: boolean; | ||
callback: redisTypes.Callback<unknown>; | ||
call_on_write: boolean; | ||
} | ||
|
||
export interface RedisPluginClientTypes { | ||
options?: { | ||
host: string; | ||
port: string; | ||
}; | ||
|
||
address?: string; | ||
} | ||
|
||
export interface RedisPluginStreamTypes { | ||
stream?: { get(): EventEmitter; set(val: EventEmitter): void }; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we should remove
"private": true
to make this package available for the next release?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I removed it from this plugin. I think we should also remove it from other "ready" plugins (separate PR). Seems to just be postgres and http2 currently