UNPKG

4.53 kBTypeScriptView Raw
1/** @module trace */
2import { ConfigParams } from 'pip-services3-commons-node';
3import { IReferenceable } from 'pip-services3-commons-node';
4import { IReferences } from 'pip-services3-commons-node';
5import { IReconfigurable } from 'pip-services3-commons-node';
6import { ITracer } from './ITracer';
7import { TraceTiming } from './TraceTiming';
8import { OperationTrace } from './OperationTrace';
9/**
10 * Abstract tracer that caches recorded traces in memory and periodically dumps them.
11 * Child classes implement saving cached traces to their specified destinations.
12 *
13 * ### Configuration parameters ###
14 *
15 * - source: source (context) name
16 * - options:
17 * - interval: interval in milliseconds to save log messages (default: 10 seconds)
18 * - max_cache_size: maximum number of messages stored in this cache (default: 100)
19 *
20 * ### References ###
21 *
22 * - <code>\*:context-info:\*:\*:1.0</code> (optional) [[ContextInfo]] to detect the context id and specify counters source
23 *
24 * @see [[ITracer]]
25 * @see [[OperationTrace]]
26 */
27export declare abstract class CachedTracer implements ITracer, IReconfigurable, IReferenceable {
28 protected _source: string;
29 protected _cache: OperationTrace[];
30 protected _updated: boolean;
31 protected _lastDumpTime: number;
32 protected _maxCacheSize: number;
33 protected _interval: number;
34 /**
35 * Creates a new instance of the logger.
36 */
37 constructor();
38 /**
39 * Configures component by passing configuration parameters.
40 *
41 * @param config configuration parameters to be set.
42 */
43 configure(config: ConfigParams): void;
44 /**
45 * Sets references to dependent components.
46 *
47 * @param references references to locate the component dependencies.
48 */
49 setReferences(references: IReferences): void;
50 /**
51 * Writes a log message to the logger destination.
52 *
53 * @param correlationId (optional) transaction id to trace execution through call chain.
54 * @param component a name of called component
55 * @param operation a name of the executed operation.
56 * @param error an error object associated with this trace.
57 * @param duration execution duration in milliseconds.
58 */
59 protected write(correlationId: string, component: string, operation: string, error: Error, duration: number): void;
60 /**
61 * Records an operation trace with its name and duration
62 *
63 * @param correlationId (optional) transaction id to trace execution through call chain.
64 * @param component a name of called component
65 * @param operation a name of the executed operation.
66 * @param duration execution duration in milliseconds.
67 */
68 trace(correlationId: string, component: string, operation: string, duration: number): void;
69 /**
70 * Records an operation failure with its name, duration and error
71 *
72 * @param correlationId (optional) transaction id to trace execution through call chain.
73 * @param component a name of called component
74 * @param operation a name of the executed operation.
75 * @param error an error object associated with this trace.
76 * @param duration execution duration in milliseconds.
77 */
78 failure(correlationId: string, component: string, operation: string, error: Error, duration: number): void;
79 /**
80 * Begings recording an operation trace
81 *
82 * @param correlationId (optional) transaction id to trace execution through call chain.
83 * @param component a name of called component
84 * @param operation a name of the executed operation.
85 * @returns a trace timing object.
86 */
87 beginTrace(correlationId: string, component: string, operation: string): TraceTiming;
88 /**
89 * Saves log messages from the cache.
90 *
91 * @param messages a list with log messages
92 * @param callback callback function that receives error or null for success.
93 */
94 protected abstract save(messages: OperationTrace[], callback: (err: any) => void): void;
95 /**
96 * Clears (removes) all cached log messages.
97 */
98 clear(): void;
99 /**
100 * Dumps (writes) the currently cached log messages.
101 *
102 * @see [[write]]
103 */
104 dump(): void;
105 /**
106 * Makes trace cache as updated
107 * and dumps it when timeout expires.
108 *
109 * @see [[dump]]
110 */
111 protected update(): void;
112}