@@ -50,6 +50,16 @@ class URLFilter implements TracePolicyPredicate<string> {
5050 }
5151}
5252
53+ class MethodsFilter implements TracePolicyPredicate < string > {
54+ constructor ( private readonly filterMethods : string [ ] ) { }
55+
56+ shouldTrace ( method : string ) {
57+ return ! this . filterMethods . some ( ( candidate ) => {
58+ return ( candidate . toLowerCase ( ) === method . toLowerCase ( ) ) ;
59+ } ) ;
60+ }
61+ }
62+
5363/**
5464 * Options for constructing a TracePolicy instance.
5565 */
@@ -62,6 +72,10 @@ export interface TracePolicyConfig {
6272 * A field that controls a url-based filter.
6373 */
6474 ignoreUrls : Array < string | RegExp > ;
75+ /**
76+ * A field that controls a method filter.
77+ */
78+ ignoreMethods : string [ ] ;
6579}
6680
6781/**
@@ -70,6 +84,7 @@ export interface TracePolicyConfig {
7084export class TracePolicy {
7185 private readonly sampler : TracePolicyPredicate < number > ;
7286 private readonly urlFilter : TracePolicyPredicate < string > ;
87+ private readonly methodsFilter : TracePolicyPredicate < string > ;
7388
7489 /**
7590 * Constructs a new TracePolicy instance.
@@ -88,23 +103,32 @@ export class TracePolicy {
88103 } else {
89104 this . urlFilter = new URLFilter ( config . ignoreUrls ) ;
90105 }
106+ if ( config . ignoreMethods . length === 0 ) {
107+ this . methodsFilter = { shouldTrace : ( ) => true } ;
108+ } else {
109+ this . methodsFilter = new MethodsFilter ( config . ignoreMethods ) ;
110+ }
91111 }
92112
93113 /**
94114 * Given a timestamp and URL, decides if a trace should be created.
95115 * @param options Fields that help determine whether a trace should be
96116 * created.
97117 */
98- shouldTrace ( options : { timestamp : number , url : string } ) : boolean {
118+ shouldTrace ( options : { timestamp : number , url : string , method : string } ) :
119+ boolean {
99120 return this . sampler . shouldTrace ( options . timestamp ) &&
100- this . urlFilter . shouldTrace ( options . url ) ;
121+ this . urlFilter . shouldTrace ( options . url ) &&
122+ this . methodsFilter . shouldTrace ( options . method ) ;
101123 }
102124
103125 static always ( ) : TracePolicy {
104- return new TracePolicy ( { samplingRate : 0 , ignoreUrls : [ ] } ) ;
126+ return new TracePolicy (
127+ { samplingRate : 0 , ignoreUrls : [ ] , ignoreMethods : [ ] } ) ;
105128 }
106129
107130 static never ( ) : TracePolicy {
108- return new TracePolicy ( { samplingRate : - 1 , ignoreUrls : [ ] } ) ;
131+ return new TracePolicy (
132+ { samplingRate : - 1 , ignoreUrls : [ ] , ignoreMethods : [ ] } ) ;
109133 }
110134}
0 commit comments