forked from googleapis/cloud-trace-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace.ts
182 lines (154 loc) · 5.4 KB
/
trace.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2018 Google LLC
//
// 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
//
// http://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.
/**
* This file exports an interface that is identical to that of the Trace Agent,
* for testing purposes. The differences are that:
* - The Trace Writer singleton is mocked to make no network requests, writing
* traces to a local store instead.
* - The Plugin Loader singleton is mocked to do nothing.
* - When started, the Trace Agent is initialized with a samplingRate of zero by
* default (but this can be overridden).
* - The Trace Agent is also initialized with a TestLogger instance. The only
* observable difference is that GCLOUD_TEST_LOG_LEVEL now tweaks the verbosity.
* This is preferable to GCLOUD_LOG_LEVEL because the latter is a testable
* "API", and GCLOUD_TEST_LOG_LEVEL can also control console log level when we
* are testing isolated components such as _just_ the Trace Writer or the
* Plugin Loader.
* - Additional methods to query/delete spans written locally are exposed.
* - Additional methods to override singleton values are exposed.
*
* Most tests should include this file instead of the main module root.
*/
import * as assert from 'assert';
import * as shimmer from 'shimmer';
import * as trace from '../src';
import {Config, PluginTypes} from '../src';
import {cls, TraceCLS, TraceCLSMechanism} from '../src/cls';
import * as logger from '../src/logger';
import {Trace, TraceSpan} from '../src/trace';
import {PluginLoader, pluginLoader} from '../src/trace-plugin-loader';
import {TraceWriter, traceWriter, TraceWriterConfig} from '../src/trace-writer';
import {tracing, Tracing} from '../src/tracing';
import {FORCE_NEW} from '../src/util';
import * as testLogger from './logger';
export {Config, PluginTypes};
const traces: Trace[] = [];
const spans: TraceSpan[] = [];
let capturedLogger: testLogger.TestLogger | null = null;
export class TestCLS extends TraceCLS {
constructor(config: {}, logger: logger.Logger) {
super({mechanism: TraceCLSMechanism.NONE}, logger);
}
}
export class TestLogger extends testLogger.TestLogger {
constructor(options?: Partial<logger.LoggerConfig>) {
super(options);
// eslint-disable-next-line @typescript-eslint/no-this-alias
capturedLogger = this;
}
}
export class TestTraceWriter extends TraceWriter {
constructor(config: TraceWriterConfig, logger: logger.Logger) {
super(config, logger);
this.getConfig().authOptions.projectId = '0';
}
async initialize(): Promise<void> {}
writeTrace(trace: Trace): void {
traces.push(trace);
trace.spans.forEach(span => {
spans.push(span);
});
}
}
export class TestPluginLoader extends PluginLoader {
activate(): PluginLoader {
return this;
}
deactivate(): PluginLoader {
return this;
}
}
// Make no modifications to Tracing. This class exists for symmetricality
// purposes.
export class TestTracing extends Tracing {}
setCLSForTest(TestCLS);
setLoggerForTest(TestLogger);
setTraceWriterForTest(TestTraceWriter);
setPluginLoaderForTest(TestPluginLoader);
setTracingForTest(TestTracing);
export type Predicate<T> = (value: T) => boolean;
export function start(projectConfig?: Config): PluginTypes.Tracer {
capturedLogger = null;
const agent = trace.start(
Object.assign(
{samplingRate: 0, logLevel: 4, [FORCE_NEW]: true},
projectConfig
)
);
return agent;
}
export function get(): PluginTypes.Tracer {
return trace.get();
}
export function setLoggerForTest(impl?: typeof logger.Logger) {
if ((logger.Logger as any).__wrapped) {
shimmer.unwrap(logger, 'Logger');
}
if (impl) {
const wrap = () => shimmer.wrap(logger, 'Logger', () => impl);
wrap();
}
}
export function setCLSForTest(impl?: typeof TraceCLS) {
cls['implementation'] = impl || TraceCLS;
}
export function setTraceWriterForTest(impl?: typeof TraceWriter) {
traceWriter['implementation'] = impl || TraceWriter;
}
export function setPluginLoaderForTest(impl?: typeof PluginLoader) {
pluginLoader['implementation'] = impl || PluginLoader;
}
export function setTracingForTest(impl?: typeof Tracing) {
tracing['implementation'] = impl || Tracing;
}
export function getLogger(): testLogger.TestLogger {
assert.ok(capturedLogger);
return capturedLogger!;
}
export function getTraces(predicate?: Predicate<Trace>): Trace[] {
if (!predicate) {
predicate = () => true;
}
return traces.filter(predicate);
}
export function getOneTrace(predicate?: Predicate<Trace>): Trace {
const traces = getTraces(predicate);
assert.strictEqual(traces.length, 1);
return traces[0];
}
export function getSpans(predicate?: Predicate<TraceSpan>): TraceSpan[] {
if (!predicate) {
predicate = () => true;
}
return spans.filter(predicate);
}
export function getOneSpan(predicate?: Predicate<TraceSpan>): TraceSpan {
const spans = getSpans(predicate);
assert.strictEqual(spans.length, 1);
return spans[0];
}
export function clearTraceData(): void {
traces.length = 0;
spans.length = 0;
}