UNPKG

3.71 kBTypeScriptView Raw
1/**
2 * Copyright 2018, OpenCensus 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 * http://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 */
16import { Logger } from '../../common/types';
17import { Stats } from '../../stats/types';
18import * as modelTypes from '../model/types';
19import * as types from './types';
20/**
21 * Maps a name (key) representing a internal file module and its exports
22 */
23export interface ModuleExportsMapping {
24 [key: string]: any;
25}
26/** This class represent the base to patch plugin. */
27export declare abstract class BasePlugin implements types.Plugin {
28 /** Exports from the nodejs module to be instrumented */
29 protected moduleExports: any;
30 /** The module name */
31 protected moduleName: string;
32 /** A tracer object. */
33 protected tracer: modelTypes.Tracer;
34 /** The module version. */
35 protected version: string;
36 /** a logger */
37 protected logger: Logger;
38 /** list of internal files that need patch and are not exported by default */
39 protected readonly internalFileList: types.PluginInternalFiles;
40 /** internal files loaded */
41 protected internalFilesExports: ModuleExportsMapping;
42 /** module directory - used to load internal files */
43 protected basedir: string;
44 /** plugin options */
45 protected options: types.PluginConfig;
46 /** A stats object. */
47 protected stats?: Stats;
48 /**
49 * Constructs a new BasePlugin instance.
50 * @param moduleName The module name.
51 */
52 constructor(moduleName: string);
53 /**
54 * Sets modified plugin to the context.
55 * @param moduleExports nodejs module exports to set as context
56 * @param tracer tracer relating to context
57 * @param version module version description
58 * @param options plugin options
59 * @param basedir module absolute path
60 * @param stats a stats instance
61 */
62 private setPluginContext;
63 /**
64 * Method that enables the instrumentation patch.
65 *
66 * This method implements the GoF Template Method Pattern
67 * 'enable' is the invariant part of the pattern and
68 * 'applyPatch' the variant.
69 *
70 * @param moduleExports nodejs module exports from the module to patch
71 * @param tracer a tracer instance
72 * @param version version of the current instaled module to patch
73 * @param options plugin options
74 * @param basedir an optional module absolute path
75 * @param stats an optional stats instance
76 */
77 enable<T>(moduleExports: T, tracer: modelTypes.Tracer, version: string, options: types.PluginConfig, basedir?: string, stats?: Stats): any;
78 /** Method to disable the instrumentation */
79 disable(): void;
80 /**
81 * This method implements the GoF Template Method Pattern,
82 * 'applyPatch' is the variant part, each instrumentation should
83 * implement its own version, 'enable' method is the invariant.
84 * It will be called when enable is called.
85 *
86 */
87 protected abstract applyPatch(): any;
88 protected abstract applyUnpatch(): void;
89 /**
90 * Load internal files according to version range
91 */
92 private loadInternalFiles;
93 /**
94 * Load internal files from a module and set internalFilesExports
95 */
96 private loadInternalModuleFiles;
97}