1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.api.settings;
  27 
  28 import jdk.jfr.Description;
  29 import jdk.jfr.Event;
  30 import jdk.jfr.Label;
  31 import jdk.jfr.Recording;
  32 import jdk.jfr.SettingDefinition;
  33 import jdk.test.lib.jfr.Events;
  34 
  35 import static jdk.test.lib.Asserts.assertEquals;
  36 
  37 /*
  38  * @test
  39  * @summary The test uses SettingControl
  40  * @key jfr
  41  * @library /test/lib /test/jdk
  42  * @run main/othervm jdk.jfr.api.settings.TestFilterEvents
  43  */
  44 public class TestFilterEvents {
  45 
  46     private static class AbstractHTTPEvent extends Event {
  47         @Label("HTTP URI")
  48         protected String uri;
  49 
  50         @Label("URI Filter")
  51         @SettingDefinition
  52         protected boolean uriFilter(RegExpControl control) {
  53             return control.matches(uri);
  54         }
  55     }
  56 
  57     private static final class HTTPGetEvent extends AbstractHTTPEvent {
  58         @Label("Thread Names")
  59         @Description("List of thread names to accept, such as \"main\" or \"workerThread1\", \"taskThread\"")
  60         @SettingDefinition
  61         private boolean threadNames(StringListSetting setting) {
  62             return setting.accept(Thread.currentThread().getName());
  63         }
  64 
  65     }
  66     private static final class HTTPPostEvent extends AbstractHTTPEvent {
  67     }
  68 
  69     public static void main(String[] args) throws Exception {
  70         Recording continuous = new Recording();
  71         continuous.enable(HTTPGetEvent.class).with("threadNames", "\"unused-threadname-1\"");
  72         assertEquals(0, makeProfilingRecording("\"unused-threadname-2\""));
  73         assertEquals(1, makeProfilingRecording("\"" + Thread.currentThread().getName() + "\""));
  74         continuous.close();
  75     }
  76 
  77     private static int makeProfilingRecording(String threadNames) throws Exception {
  78         try (Recording recording = new Recording()) {
  79             recording.enable(HTTPGetEvent.class).with("threadNames", threadNames);
  80             recording.enable(HTTPGetEvent.class).with("uriFilter", "https://www.example.com/list/.*");
  81             recording.enable(HTTPPostEvent.class).with("uriFilter", "https://www.example.com/list/.*");
  82             recording.start();
  83 
  84             HTTPGetEvent getEvent = new HTTPGetEvent();
  85             getEvent.uri = "https://www.example.com/list/item?id=4";
  86             getEvent.commit();
  87 
  88             HTTPPostEvent postEvent = new HTTPPostEvent();
  89             postEvent.uri = "https://www.example.com/admin/login?name=john";
  90             postEvent.commit();
  91 
  92             recording.stop();
  93 
  94             return Events.fromRecording(recording).size();
  95         }
  96     }
  97 
  98 }