-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathtracing-policy.ts
211 lines (193 loc) · 6.17 KB
/
tracing-policy.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import {RequestDetails, TracePolicy} from './config';
import {Constants} from './constants';
import {TraceContext} from './util';
// Copyright 2015 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.
/**
* An enumeration of the different possible types of behavior when dealing with
* incoming trace context. Requests are still subject to local tracing policy.
*/
export enum TraceContextHeaderBehavior {
/**
* Respect the trace context header if it exists; otherwise, trace the
* request as a new trace.
*/
DEFAULT = 'default',
/**
* Respect the trace context header if it exists; otherwise, treat the
* request as unsampled and don't trace it.
*/
REQUIRE = 'require',
/**
* Trace every request as a new trace, even if trace context exists.
*/
IGNORE = 'ignore',
}
interface TracePolicyPredicate<T> {
shouldTrace: (value: T) => boolean;
}
class Sampler implements TracePolicyPredicate<number> {
private readonly traceWindow: number;
private nextTraceStart: number;
constructor(samplesPerSecond: number) {
if (samplesPerSecond > 1000) {
samplesPerSecond = 1000;
}
this.traceWindow = 1000 / samplesPerSecond;
this.nextTraceStart = Date.now();
}
shouldTrace(dateMillis: number): boolean {
if (dateMillis < this.nextTraceStart) {
return false;
}
this.nextTraceStart = dateMillis + this.traceWindow;
return true;
}
}
class URLFilter implements TracePolicyPredicate<string> {
constructor(private readonly filterUrls: Array<string | RegExp>) {}
shouldTrace(url: string) {
return !this.filterUrls.some(candidate => {
return (
(typeof candidate === 'string' && candidate === url) ||
!!url.match(candidate)
);
});
}
}
class MethodsFilter implements TracePolicyPredicate<string> {
constructor(private readonly filterMethods: string[]) {}
shouldTrace(method: string) {
return !this.filterMethods.some(candidate => {
return candidate.toLowerCase() === method.toLowerCase();
});
}
}
class ContextHeaderFilter
implements TracePolicyPredicate<Required<TraceContext> | null>
{
constructor(
private readonly contextHeaderBehavior: TraceContextHeaderBehavior
) {}
shouldTrace(header: Required<TraceContext> | null) {
switch (this.contextHeaderBehavior) {
case TraceContextHeaderBehavior.IGNORE: {
return true;
}
case TraceContextHeaderBehavior.REQUIRE: {
// There must be an incoming header, and its LSB must be 1.
return !!(
header && header.options & Constants.TRACE_OPTIONS_TRACE_ENABLED
);
}
default: {
// TraceContextHeaderBehavior.DEFAULT
// If there is a header, its LSB must be 1. Otherwise, we assume that
// it would be 1.
return !!(
!header || header.options & Constants.TRACE_OPTIONS_TRACE_ENABLED
);
}
}
}
}
/**
* Options for constructing a TracePolicy instance.
*/
export interface TracePolicyConfig {
/**
* A field that controls time-based sampling.
*/
samplingRate: number;
/**
* A field that controls a url-based filter.
*/
ignoreUrls: Array<string | RegExp>;
/**
* A field that controls a method filter.
*/
ignoreMethods: string[];
/**
* A field that controls filtering based on incoming trace context.
*/
contextHeaderBehavior: TraceContextHeaderBehavior;
}
/**
* A class that makes decisions about whether a trace should be created.
*/
export class BuiltinTracePolicy implements TracePolicy {
private readonly sampler: TracePolicyPredicate<number>;
private readonly urlFilter: TracePolicyPredicate<string>;
private readonly methodsFilter: TracePolicyPredicate<string>;
private readonly contextHeaderFilter: TracePolicyPredicate<Required<TraceContext> | null>;
/**
* Constructs a new TracePolicy instance.
* @param config Configuration for the TracePolicy instance.
*/
constructor(config: TracePolicyConfig) {
if (config.samplingRate === 0) {
this.sampler = {shouldTrace: () => true};
} else if (config.samplingRate < 0) {
this.sampler = {shouldTrace: () => false};
} else {
this.sampler = new Sampler(config.samplingRate);
}
if (config.ignoreUrls.length === 0) {
this.urlFilter = {shouldTrace: () => true};
} else {
this.urlFilter = new URLFilter(config.ignoreUrls);
}
if (config.ignoreMethods.length === 0) {
this.methodsFilter = {shouldTrace: () => true};
} else {
this.methodsFilter = new MethodsFilter(config.ignoreMethods);
}
if (config.contextHeaderBehavior === TraceContextHeaderBehavior.IGNORE) {
this.contextHeaderFilter = {shouldTrace: () => true};
} else {
this.contextHeaderFilter = new ContextHeaderFilter(
config.contextHeaderBehavior
);
}
}
/**
* Given a timestamp and URL, decides if a trace should be created.
* @param options Fields that help determine whether a trace should be
* created.
*/
shouldTrace(options: RequestDetails): boolean {
return (
this.urlFilter.shouldTrace(options.url) &&
this.methodsFilter.shouldTrace(options.method) &&
this.contextHeaderFilter.shouldTrace(options.traceContext) &&
this.sampler.shouldTrace(options.timestamp)
);
}
}
export function alwaysTrace(): BuiltinTracePolicy {
return new BuiltinTracePolicy({
samplingRate: 0,
ignoreUrls: [],
ignoreMethods: [],
contextHeaderBehavior: TraceContextHeaderBehavior.DEFAULT,
});
}
export function neverTrace(): BuiltinTracePolicy {
return new BuiltinTracePolicy({
samplingRate: -1,
ignoreUrls: [],
ignoreMethods: [],
contextHeaderBehavior: TraceContextHeaderBehavior.DEFAULT,
});
}