1- import { InstrumentationBase } from "@opentelemetry/instrumentation" ;
2- import { RedisInstrumentationConfig } from "./types" ;
3- import { PACKAGE_NAME , PACKAGE_VERSION } from "./version" ;
4- import { RedisInstrumentationV2_3 } from "./v2-3/instrumentation" ;
5- import { TracerProvider } from "@opentelemetry/api" ;
6- import { RedisInstrumentationV4 } from "./v4/instrumentation" ;
1+ /*
2+ * Copyright The 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+ import { InstrumentationBase } from '@opentelemetry/instrumentation' ;
17+ import { RedisInstrumentationConfig } from './types' ;
18+ import { PACKAGE_NAME , PACKAGE_VERSION } from './version' ;
19+ import { RedisInstrumentationV2_3 } from './v2-3/instrumentation' ;
20+ import { TracerProvider } from '@opentelemetry/api' ;
21+ import { RedisInstrumentationV4 } from './v4/instrumentation' ;
722
823const DEFAULT_CONFIG : RedisInstrumentationConfig = {
9- requireParentSpan : false ,
24+ requireParentSpan : false ,
1025} ;
1126
1227// Wrapper RedisInstrumentation that address all supported versions
1328export class RedisInstrumentation extends InstrumentationBase < RedisInstrumentationConfig > {
29+ private instrumentationV2_3 : RedisInstrumentationV2_3 ;
30+ private instrumentationV4 : RedisInstrumentationV4 ;
1431
15- private instrumentationV2_3 : RedisInstrumentationV2_3 ;
16- private instrumentationV4 : RedisInstrumentationV4 ;
32+ // this is used to bypass a flaw in the base class constructor, which is calling
33+ // member functions before the constructor has a chance to fully initialize the member variables.
34+ private initialized = false ;
1735
18- // this is used to bypass a flaw in the base class constructor, which is calling
19- // member functions before the constructor has a chance to fully initialize the member variables.
20- private initialized = false ;
36+ constructor ( config : RedisInstrumentationConfig = { } ) {
37+ const resolvedConfig = { ... DEFAULT_CONFIG , ... config } ;
38+ super ( PACKAGE_NAME , PACKAGE_VERSION , resolvedConfig ) ;
2139
22- constructor ( config : RedisInstrumentationConfig = { } ) {
23- const resolvedConfig = { ...DEFAULT_CONFIG , ...config } ;
24- super ( PACKAGE_NAME , PACKAGE_VERSION , resolvedConfig ) ;
40+ this . instrumentationV2_3 = new RedisInstrumentationV2_3 ( this . getConfig ( ) ) ;
41+ this . instrumentationV4 = new RedisInstrumentationV4 ( this . getConfig ( ) ) ;
42+ this . initialized = true ;
43+ }
2544
26- this . instrumentationV2_3 = new RedisInstrumentationV2_3 ( this . getConfig ( ) ) ;
27- this . instrumentationV4 = new RedisInstrumentationV4 ( this . getConfig ( ) ) ;
28- this . initialized = true ;
45+ override setConfig ( config : RedisInstrumentationConfig = { } ) {
46+ const newConfig = { ...DEFAULT_CONFIG , ...config } ;
47+ super . setConfig ( newConfig ) ;
48+ if ( ! this . initialized ) {
49+ return ;
2950 }
3051
31- override setConfig ( config : RedisInstrumentationConfig = { } ) {
32- const newConfig = { ...DEFAULT_CONFIG , ...config } ;
33- super . setConfig ( newConfig ) ;
34- if ( ! this . initialized ) {
35- return
36- }
52+ this . instrumentationV2_3 . setConfig ( newConfig ) ;
53+ this . instrumentationV4 . setConfig ( newConfig ) ;
54+ }
3755
38- this . instrumentationV2_3 . setConfig ( newConfig ) ;
39- this . instrumentationV4 . setConfig ( newConfig ) ;
40- }
41-
42- override init ( ) {
43- }
56+ override init ( ) { }
4457
45- override setTracerProvider ( tracerProvider : TracerProvider ) {
46- super . setTracerProvider ( tracerProvider ) ;
47- if ( ! this . initialized ) {
48- return
49- }
50- this . instrumentationV2_3 . setTracerProvider ( tracerProvider ) ;
51- this . instrumentationV4 . setTracerProvider ( tracerProvider ) ;
58+ override setTracerProvider ( tracerProvider : TracerProvider ) {
59+ super . setTracerProvider ( tracerProvider ) ;
60+ if ( ! this . initialized ) {
61+ return ;
5262 }
63+ this . instrumentationV2_3 . setTracerProvider ( tracerProvider ) ;
64+ this . instrumentationV4 . setTracerProvider ( tracerProvider ) ;
65+ }
5366
54- override enable ( ) {
55- super . enable ( ) ;
56- if ( ! this . initialized ) {
57- return
58- }
59- this . instrumentationV2_3 . enable ( ) ;
60- this . instrumentationV4 . enable ( ) ;
67+ override enable ( ) {
68+ super . enable ( ) ;
69+ if ( ! this . initialized ) {
70+ return ;
6171 }
72+ this . instrumentationV2_3 . enable ( ) ;
73+ this . instrumentationV4 . enable ( ) ;
74+ }
6275
63- override disable ( ) {
64- super . disable ( ) ;
65- if ( ! this . initialized ) {
66- return
67- }
68- this . instrumentationV2_3 . disable ( ) ;
69- this . instrumentationV4 . disable ( ) ;
76+ override disable ( ) {
77+ super . disable ( ) ;
78+ if ( ! this . initialized ) {
79+ return ;
7080 }
71- }
81+ this . instrumentationV2_3 . disable ( ) ;
82+ this . instrumentationV4 . disable ( ) ;
83+ }
84+ }
0 commit comments