|
| 1 | +/*! |
| 2 | + * Copyright 2019, OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { BasePlugin } from '@opentelemetry/core'; |
| 18 | +import * as redisTypes from 'redis'; |
| 19 | +import * as shimmer from 'shimmer'; |
| 20 | +import { |
| 21 | + getTracedCreateClient, |
| 22 | + getTracedCreateStreamTrace, |
| 23 | + getTracedInternalSendCommand, |
| 24 | +} from './utils'; |
| 25 | + |
| 26 | +export class RedisPlugin extends BasePlugin<typeof redisTypes> { |
| 27 | + static readonly COMPONENT = 'redis'; |
| 28 | + readonly supportedVersions = ['^2.6.0']; // equivalent to >= 2.6.0 <3 |
| 29 | + |
| 30 | + constructor(readonly moduleName: string) { |
| 31 | + super(); |
| 32 | + } |
| 33 | + |
| 34 | + protected patch() { |
| 35 | + if (this._moduleExports.RedisClient) { |
| 36 | + this._logger.debug( |
| 37 | + 'Patching redis.RedisClient.prototype.internal_send_command' |
| 38 | + ); |
| 39 | + shimmer.wrap( |
| 40 | + this._moduleExports.RedisClient.prototype, |
| 41 | + 'internal_send_command', |
| 42 | + this._getPatchInternalSendCommand() |
| 43 | + ); |
| 44 | + |
| 45 | + this._logger.debug('patching redis.create_stream'); |
| 46 | + shimmer.wrap( |
| 47 | + this._moduleExports.RedisClient.prototype, |
| 48 | + 'create_stream', |
| 49 | + this._getPatchCreateStream() |
| 50 | + ); |
| 51 | + |
| 52 | + this._logger.debug('patching redis.createClient'); |
| 53 | + shimmer.wrap( |
| 54 | + this._moduleExports, |
| 55 | + 'createClient', |
| 56 | + this._getPatchCreateClient() |
| 57 | + ); |
| 58 | + } |
| 59 | + return this._moduleExports; |
| 60 | + } |
| 61 | + |
| 62 | + protected unpatch(): void { |
| 63 | + if (this._moduleExports) { |
| 64 | + shimmer.unwrap( |
| 65 | + this._moduleExports.RedisClient.prototype, |
| 66 | + 'internal_send_command' |
| 67 | + ); |
| 68 | + shimmer.unwrap( |
| 69 | + this._moduleExports.RedisClient.prototype, |
| 70 | + 'create_stream' |
| 71 | + ); |
| 72 | + shimmer.unwrap(this._moduleExports, 'createClient'); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Patch internal_send_command(...) to trace requests |
| 78 | + */ |
| 79 | + private _getPatchInternalSendCommand() { |
| 80 | + const tracer = this._tracer; |
| 81 | + return function internal_send_command(original: Function) { |
| 82 | + return getTracedInternalSendCommand(tracer, original); |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + private _getPatchCreateClient() { |
| 87 | + const tracer = this._tracer; |
| 88 | + return function createClient(original: Function) { |
| 89 | + return getTracedCreateClient(tracer, original); |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + private _getPatchCreateStream() { |
| 94 | + const tracer = this._tracer; |
| 95 | + return function createReadStream(original: Function) { |
| 96 | + return getTracedCreateStreamTrace(tracer, original); |
| 97 | + }; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export const plugin = new RedisPlugin(RedisPlugin.COMPONENT); |
0 commit comments